From bfdf17fd89241567dde51c3f2b23a735f4866d24 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Mon, 29 Oct 2018 07:09:28 +0100 Subject: [PATCH] Fix undefined if --- src/exec.js | 42 ++++++++++++++++++-------------- test/cases.js | 14 +++++++++++ test/circuits/undefinedif.circom | 14 +++++++++++ 3 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 test/cases.js create mode 100644 test/circuits/undefinedif.circom diff --git a/src/exec.js b/src/exec.js index a280a28..cd5b9d3 100644 --- a/src/exec.js +++ b/src/exec.js @@ -547,15 +547,17 @@ function execFor(ctx, ast) { let v = exec(ctx, ast.condition); if (ctx.error) return; - while ((v.value.neq(0))&&(!ctx.returnValue)) { - exec(ctx, ast.body); - if (ctx.error) return; + if (typeof v.value != "undefined") { + while ((v.value.neq(0))&&(!ctx.returnValue)) { + exec(ctx, ast.body); + if (ctx.error) return; - exec(ctx, ast.step); - if (ctx.error) return; + exec(ctx, ast.step); + if (ctx.error) return; - v = exec(ctx, ast.condition); - if (ctx.error) return; + v = exec(ctx, ast.condition); + if (ctx.error) return; + } } } @@ -563,12 +565,14 @@ function execWhile(ctx, ast) { let v = exec(ctx, ast.condition); if (ctx.error) return; - while ((v.value.neq(0))&&(!ctx.returnValue)) { - exec(ctx, ast.body); - if (ctx.error) return; + if (typeof v.value != "undefined") { + while ((v.value.neq(0))&&(!ctx.returnValue)) { + exec(ctx, ast.body); + if (ctx.error) return; - v = exec(ctx, ast.condition); - if (ctx.error) return; + v = exec(ctx, ast.condition); + if (ctx.error) return; + } } } @@ -576,13 +580,15 @@ function execIf(ctx, ast) { let v = exec(ctx, ast.condition); if (ctx.error) return; - if ((v.value.neq(0))&&(!ctx.returnValue)) { - exec(ctx, ast.then); - if (ctx.error) return; - } else { - if (ast.else) { - exec(ctx, ast.else); + if (typeof v.value != "undefined") { + if ((v.value.neq(0))&&(!ctx.returnValue)) { + exec(ctx, ast.then); if (ctx.error) return; + } else { + if (ast.else) { + exec(ctx, ast.else); + if (ctx.error) return; + } } } } diff --git a/test/cases.js b/test/cases.js new file mode 100644 index 0000000..49df23f --- /dev/null +++ b/test/cases.js @@ -0,0 +1,14 @@ +const chai = require("chai"); +const path = require("path"); +const snarkjs = require("snarkjs"); +const crypto = require("crypto"); + +const compiler = require("../index.js"); + +const assert = chai.assert; + +describe("Sum test", () => { + it("Should compile a code with an undefined if", async() => { + await compiler(path.join(__dirname, "circuits", "undefinedif.circom")); + }); +}); diff --git a/test/circuits/undefinedif.circom b/test/circuits/undefinedif.circom new file mode 100644 index 0000000..ec117f8 --- /dev/null +++ b/test/circuits/undefinedif.circom @@ -0,0 +1,14 @@ +template X() { + signal input i; + signal input j; + signal output out; + + if (i == 0) { + out <-- i; + } + else { + out <-- j; + } +} + +component main = X();