Skip to content

Commit

Permalink
Merge pull request #11 from distributed-lab/fix/unary-sub
Browse files Browse the repository at this point in the history
Fix bug with incorrect handle of unary sub operator
  • Loading branch information
Arvolear authored Dec 18, 2024
2 parents 5d1639a + 80bd23c commit 6aa09f2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
6 changes: 4 additions & 2 deletions src/utils/ExpressionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,10 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
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));
Expand Down
37 changes: 37 additions & 0 deletions test/circom-template-inputs-visitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
17 changes: 17 additions & 0 deletions test/data/BigModNonEqual.circom
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 6aa09f2

Please sign in to comment.