Skip to content

Commit b426bac

Browse files
authored
Merge pull request #12 from distributed-lab/fix/not-0
Fix bug related to false positive resolution in if statement
2 parents 6aa09f2 + 754edcf commit b426bac

File tree

6 files changed

+54
-13
lines changed

6 files changed

+54
-13
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.3",
4+
"version": "0.2.4",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [

src/utils/ExpressionHelper.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class ExpressionHelper {
6363

6464
const result = visitor.visitExpression(this._expressionContext);
6565

66-
if (result === null) {
66+
if (result === null || result === undefined) {
6767
return [null, visitor.getErrors()];
6868
}
6969

@@ -91,7 +91,7 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
9191
if (ctx.TERNARY_CONDITION() && ctx.TERNARY_ALTERNATIVE()) {
9292
const conditionResult = this.visit(ctx._cond);
9393

94-
if (conditionResult === null) {
94+
if (conditionResult === null || conditionResult === undefined) {
9595
this.addError(
9696
"Failed to resolve the condition of a ternary expression",
9797
ctx._cond,
@@ -124,7 +124,7 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
124124
const variableValue =
125125
this.variableContext[ctx.identifierStatement().ID().getText()];
126126

127-
if (variableValue === undefined) {
127+
if (variableValue === undefined || variableValue === null) {
128128
this.addError(
129129
`Variable ${ctx.identifierStatement().ID().getText()} is not defined`,
130130
ctx.identifierStatement(),
@@ -153,7 +153,10 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
153153

154154
const variableName = ctx.identifierStatement().ID().getText() + reference;
155155

156-
if (this.variableContext[variableName] === undefined) {
156+
if (
157+
this.variableContext[variableName] === undefined ||
158+
this.variableContext[variableName] === null
159+
) {
157160
this.addError(
158161
`Variable ${variableName} is not defined`,
159162
ctx.identifierStatement(),
@@ -184,7 +187,7 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
184187
for (let i = 0; i < ctx.expressionList().expression_list().length; i++) {
185188
const resolvedItem = this.visit(ctx.expressionList().expression(i));
186189

187-
if (!resolvedItem) {
190+
if (resolvedItem === null || resolvedItem === undefined) {
188191
this.addError(
189192
`Failed to resolve the ${i} element of an array.`,
190193
ctx.expressionList().expression(i),
@@ -220,7 +223,7 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
220223

221224
const firstExpression = this.visit(ctx.expression(0));
222225

223-
if (firstExpression === null) {
226+
if (firstExpression === null || firstExpression === undefined) {
224227
this.addError(
225228
"Failed to resolve the first expression of an operation",
226229
ctx.expression(0),
@@ -256,7 +259,7 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
256259

257260
const secondExpression = this.visit(ctx.expression(1));
258261

259-
if (secondExpression === null) {
262+
if (secondExpression === null || secondExpression === undefined) {
260263
this.addError(
261264
"Failed to resolve the second expression of an operation",
262265
ctx.expression(1),

src/utils/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function parseSimpleIdentifierList(
1010
): string[] {
1111
const result: string[] = [];
1212

13-
if (!ctx) {
13+
if (ctx === null || ctx === undefined) {
1414
return result;
1515
}
1616

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ describe("Circom Template Inputs Visitor", () => {
184184

185185
visitor.startParse();
186186

187-
console.log(visitor.templateInputs);
188-
189187
expect(visitor.errors.length).to.equal(0);
190188

191189
expect(visitor.templateInputs.base.type).to.equal("input");
@@ -206,4 +204,33 @@ describe("Circom Template Inputs Visitor", () => {
206204
expect(visitor.templateInputs.another.type).to.equal("output");
207205
expect(visitor.templateInputs.another.dimension).to.deep.equal([4]);
208206
});
207+
208+
it("should analyse the EcAdd.circom circuit", () => {
209+
const data = getData("EcAdd.circom");
210+
211+
const visitor = new CircomTemplateInputsVisitor(
212+
"EcAdd.circom",
213+
data.templates[data.mainComponentInfo.templateName!].context,
214+
buildVariableContext(
215+
data.templates[data.mainComponentInfo.templateName!].parameters,
216+
data.mainComponentInfo.parameters,
217+
),
218+
);
219+
220+
visitor.startParse();
221+
222+
expect(visitor.errors.length).to.equal(0);
223+
224+
expect(visitor.templateInputs.in1.type).to.equal("input");
225+
expect(visitor.templateInputs.in1.dimension).to.deep.equal([2, 4]);
226+
227+
expect(visitor.templateInputs.in2.type).to.equal("input");
228+
expect(visitor.templateInputs.in2.dimension).to.deep.equal([2, 4]);
229+
230+
expect(visitor.templateInputs.dummy.type).to.equal("input");
231+
expect(visitor.templateInputs.dummy.dimension).to.deep.equal([]);
232+
233+
expect(visitor.templateInputs.out.type).to.equal("output");
234+
expect(visitor.templateInputs.out.dimension).to.deep.equal([2, 4]);
235+
});
209236
});

test/data/EcAdd.circom

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pragma circom 2.1.6;
2+
3+
template EllipticCurveAdd(CHUNK_SIZE, CHUNK_NUMBER, A, B, P) {
4+
signal input in1[2][CHUNK_NUMBER];
5+
signal input in2[2][CHUNK_NUMBER];
6+
signal input dummy;
7+
8+
signal output out[2][CHUNK_NUMBER];
9+
}
10+
11+
component main = EllipticCurveAdd(64, 4, [0, 0, 0, 0], [7, 0, 0, 0],[18446744069414583343, 18446744073709551615, 18446744073709551615, 18446744073709551615]);

0 commit comments

Comments
 (0)