Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug related to false positive resolution in if statement #12

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.3",
"version": "0.2.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
17 changes: 10 additions & 7 deletions src/utils/ExpressionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ExpressionHelper {

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

if (result === null) {
if (result === null || result === undefined) {
return [null, visitor.getErrors()];
}

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

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

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

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

if (this.variableContext[variableName] === undefined) {
if (
this.variableContext[variableName] === undefined ||
this.variableContext[variableName] === null
) {
this.addError(
`Variable ${variableName} is not defined`,
ctx.identifierStatement(),
Expand Down Expand Up @@ -184,7 +187,7 @@ class ExpressionVisitor extends ExtendedCircomVisitor<CircomValueType | null> {
for (let i = 0; i < ctx.expressionList().expression_list().length; i++) {
const resolvedItem = this.visit(ctx.expressionList().expression(i));

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

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

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

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

if (secondExpression === null) {
if (secondExpression === null || secondExpression === undefined) {
this.addError(
"Failed to resolve the second expression of an operation",
ctx.expression(1),
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function parseSimpleIdentifierList(
): string[] {
const result: string[] = [];

if (!ctx) {
if (ctx === null || ctx === undefined) {
return result;
}

Expand Down
31 changes: 29 additions & 2 deletions test/circom-template-inputs-visitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ describe("Circom Template Inputs Visitor", () => {

visitor.startParse();

console.log(visitor.templateInputs);

expect(visitor.errors.length).to.equal(0);

expect(visitor.templateInputs.base.type).to.equal("input");
Expand All @@ -206,4 +204,33 @@ describe("Circom Template Inputs Visitor", () => {
expect(visitor.templateInputs.another.type).to.equal("output");
expect(visitor.templateInputs.another.dimension).to.deep.equal([4]);
});

it("should analyse the EcAdd.circom circuit", () => {
const data = getData("EcAdd.circom");

const visitor = new CircomTemplateInputsVisitor(
"EcAdd.circom",
data.templates[data.mainComponentInfo.templateName!].context,
buildVariableContext(
data.templates[data.mainComponentInfo.templateName!].parameters,
data.mainComponentInfo.parameters,
),
);

visitor.startParse();

expect(visitor.errors.length).to.equal(0);

expect(visitor.templateInputs.in1.type).to.equal("input");
expect(visitor.templateInputs.in1.dimension).to.deep.equal([2, 4]);

expect(visitor.templateInputs.in2.type).to.equal("input");
expect(visitor.templateInputs.in2.dimension).to.deep.equal([2, 4]);

expect(visitor.templateInputs.dummy.type).to.equal("input");
expect(visitor.templateInputs.dummy.dimension).to.deep.equal([]);

expect(visitor.templateInputs.out.type).to.equal("output");
expect(visitor.templateInputs.out.dimension).to.deep.equal([2, 4]);
});
});
11 changes: 11 additions & 0 deletions test/data/EcAdd.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma circom 2.1.6;

template EllipticCurveAdd(CHUNK_SIZE, CHUNK_NUMBER, A, B, P) {
signal input in1[2][CHUNK_NUMBER];
signal input in2[2][CHUNK_NUMBER];
signal input dummy;

signal output out[2][CHUNK_NUMBER];
}

component main = EllipticCurveAdd(64, 4, [0, 0, 0, 0], [7, 0, 0, 0],[18446744069414583343, 18446744073709551615, 18446744073709551615, 18446744073709551615]);
Loading