diff --git a/package-lock.json b/package-lock.json index f3593c5..63a4d49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@distributedlab/circom-parser", - "version": "0.2.2", + "version": "0.2.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@distributedlab/circom-parser", - "version": "0.2.2", + "version": "0.2.3", "license": "MIT", "dependencies": { "antlr4": "4.13.1-patch-1", diff --git a/package.json b/package.json index dd440c2..941bb46 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@distributedlab/circom-parser", "description": "Circom circuit parser built with ANTLR4", - "version": "0.2.2", + "version": "0.2.3", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ diff --git a/src/utils/ExpressionHelper.ts b/src/utils/ExpressionHelper.ts index dc749c4..199df12 100644 --- a/src/utils/ExpressionHelper.ts +++ b/src/utils/ExpressionHelper.ts @@ -248,8 +248,10 @@ class ExpressionVisitor extends ExtendedCircomVisitor { return firstExpression ? 0n : 1n; case CircomParser.BNOT: return ~firstExpression; - case CircomParser.SUB: - return -firstExpression; + } + + if (operationType == CircomParser.SUB && ctx.expression(1) == null) { + return -firstExpression; } const secondExpression = this.visit(ctx.expression(1)); diff --git a/test/circom-template-inputs-visitor.test.ts b/test/circom-template-inputs-visitor.test.ts index 292683e..563c1d4 100644 --- a/test/circom-template-inputs-visitor.test.ts +++ b/test/circom-template-inputs-visitor.test.ts @@ -169,4 +169,41 @@ describe("Circom Template Inputs Visitor", () => { expect(visitor.templateInputs.out.type).to.equal("output"); expect(visitor.templateInputs.out.dimension).to.deep.equal([]); }); + + it("should analyse the BigModNonEqual.circom circuit", () => { + const data = getData("BigModNonEqual.circom"); + + const visitor = new CircomTemplateInputsVisitor( + "BigModNonEqual.circom", + data.templates[data.mainComponentInfo.templateName!].context, + buildVariableContext( + data.templates[data.mainComponentInfo.templateName!].parameters, + data.mainComponentInfo.parameters, + ), + ); + + visitor.startParse(); + + console.log(visitor.templateInputs); + + expect(visitor.errors.length).to.equal(0); + + expect(visitor.templateInputs.base.type).to.equal("input"); + expect(visitor.templateInputs.base.dimension).to.deep.equal([6]); + + expect(visitor.templateInputs.modulus.type).to.equal("input"); + expect(visitor.templateInputs.modulus.dimension).to.deep.equal([4]); + + expect(visitor.templateInputs.dummy.type).to.equal("input"); + expect(visitor.templateInputs.dummy.dimension).to.deep.equal([]); + + expect(visitor.templateInputs.div.type).to.equal("output"); + expect(visitor.templateInputs.div.dimension).to.deep.equal([3]); + + expect(visitor.templateInputs.mod.type).to.equal("output"); + expect(visitor.templateInputs.mod.dimension).to.deep.equal([4]); + + expect(visitor.templateInputs.another.type).to.equal("output"); + expect(visitor.templateInputs.another.dimension).to.deep.equal([4]); + }); }); diff --git a/test/data/BigModNonEqual.circom b/test/data/BigModNonEqual.circom new file mode 100644 index 0000000..9cd27c2 --- /dev/null +++ b/test/data/BigModNonEqual.circom @@ -0,0 +1,17 @@ +pragma circom 2.1.8; + +template BigModNonEqual(CHUNK_SIZE, CHUNK_NUMBER_BASE, CHUNK_NUMBER_MODULUS){ + var CHUNK_NUMBER_DIV = CHUNK_NUMBER_BASE - CHUNK_NUMBER_MODULUS + 1; + + var SOME_RANDOM = -CHUNK_NUMBER_BASE + 10; + + signal input base[CHUNK_NUMBER_BASE]; + signal input modulus[CHUNK_NUMBER_MODULUS]; + signal input dummy; + + signal output div[CHUNK_NUMBER_DIV]; + signal output mod[CHUNK_NUMBER_MODULUS]; + signal output another[SOME_RANDOM]; +} + +component main = BigModNonEqual(64, 6, 4);