Skip to content

Commit 2b0ab23

Browse files
authored
Merge pull request #13 from distributed-lab/feature/parenthesis
Add support for parentheses and QUO operation
2 parents b426bac + 9f8d106 commit 2b0ab23

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@distributedlab/circom-parser",
33
"description": "Circom circuit parser built with ANTLR4",
4-
"version": "0.2.4",
4+
"version": "0.2.5",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [

src/utils/ExpressionHelper.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
177177
};
178178

179179
visitPParentheses = (ctx: PParenthesesContext): CircomValueType | null => {
180-
this.addError("Parentheses are not supported", ctx);
181-
return null;
180+
let expressions = ctx.expressionList().expression_list();
181+
if (expressions.length !== 1) {
182+
this.addError("Parentheses can only contain one expression", ctx);
183+
return null;
184+
}
185+
186+
return this.visit(expressions[0]);
182187
};
183188

184189
visitPArray = (ctx: PArrayContext): CircomValueType | null => {
@@ -283,8 +288,8 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
283288
case CircomParser.DIV:
284289
return firstExpression / secondExpression;
285290
case CircomParser.QUO:
286-
this.addError("QUO operation is not supported", ctx);
287-
return null;
291+
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Division
292+
return firstExpression / secondExpression;
288293
case CircomParser.MOD:
289294
return firstExpression % secondExpression;
290295
case CircomParser.ADD:

test/circom-template-inputs-visitor.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,44 @@ describe("Circom Template Inputs Visitor", () => {
233233
expect(visitor.templateInputs.out.type).to.equal("output");
234234
expect(visitor.templateInputs.out.dimension).to.deep.equal([2, 4]);
235235
});
236+
237+
it("should analyse the Math.circom circuit", () => {
238+
const data = getData("Math.circom");
239+
240+
const visitor = new CircomTemplateInputsVisitor(
241+
"Math.circom",
242+
data.templates[data.mainComponentInfo.templateName!].context,
243+
buildVariableContext(
244+
data.templates[data.mainComponentInfo.templateName!].parameters,
245+
data.mainComponentInfo.parameters,
246+
),
247+
);
248+
249+
visitor.startParse();
250+
251+
expect(visitor.errors.length).to.equal(0);
252+
253+
expect(visitor.templateInputs.out1.type).to.equal("output");
254+
expect(visitor.templateInputs.out1.dimension).to.deep.equal([13]);
255+
256+
expect(visitor.templateInputs.out2.type).to.equal("output");
257+
expect(visitor.templateInputs.out2.dimension).to.deep.equal([16]);
258+
259+
expect(visitor.templateInputs.tmp1.type).to.equal("intermediate");
260+
expect(visitor.templateInputs.tmp1.dimension).to.deep.equal([6, 8, 2, 20]);
261+
262+
expect(visitor.templateInputs.tmp2.type).to.equal("intermediate");
263+
expect(visitor.templateInputs.tmp2.dimension).to.deep.equal([6, 2, 20]);
264+
265+
expect(visitor.templateInputs.tmp3.type).to.equal("intermediate");
266+
expect(visitor.templateInputs.tmp3.dimension).to.deep.equal([6, 2, 2, 20]);
267+
268+
expect(visitor.templateInputs.tmp4.type).to.equal("intermediate");
269+
expect(visitor.templateInputs.tmp4.dimension).to.deep.equal([5, 2, 2, 20]);
270+
271+
expect(visitor.templateInputs.powers.type).to.equal("intermediate");
272+
expect(visitor.templateInputs.powers.dimension).to.deep.equal([
273+
2, 256, 2, 6,
274+
]);
275+
});
236276
});

test/data/Math.circom

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma circom 2.1.6;
2+
3+
template Math(a, b, c) {
4+
var input1 = (a + b) / 2;
5+
var input2 = a + b / 2;
6+
7+
var PRECOMPUTE_NUMBER = 2 ** c;
8+
9+
signal tmp1 [a][PRECOMPUTE_NUMBER][2][b];
10+
11+
var STRIDE = 8;
12+
var parts = a * c \ STRIDE;
13+
14+
signal tmp2[a] [2] [b];
15+
signal tmp3[a] [2][2][b];
16+
signal tmp4[a - 1][2][2][b];
17+
18+
signal powers[parts][2 ** STRIDE][2][a];
19+
20+
signal output out1[input1];
21+
signal output out2[input2];
22+
}
23+
24+
component main = Math(6, 20, 3);

0 commit comments

Comments
 (0)