iden3 / circom_old

Circuit compiler for zkSNARKs
GNU General Public License v3.0
472 stars 82 forks source link

Compiler fails to parse if statements #9

Closed weijiekoh closed 5 years ago

weijiekoh commented 5 years ago

With circom 0.0.12, this circuit works fine:

template Test() {
    signal input in;
    signal output out;
    var t = 0;
    for (var i=0; i<5; i++) {
        t += i;
    }

    out <== t;
}

component main = Test();

But this doesn't:

template Test() {
    signal input in;
    signal output out;
    var t = 0;
    for (var i=0; i<5; i++) {
        if (i == 0) {
            t += i;
        }
    }

    out <== t;
}

component main = Test();

The error message from circom circuit.circom -o circuit.json:

TypeError: Cannot read property 'first_line' of undefined
    at error (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:146:29)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:46:16)
    at execIf (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:581:9)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:127:16)
    at execBlock (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:223:9)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:121:16)
    at execFor (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:549:9)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:123:16)
    at execBlock (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:223:9)
    at instantiateComponent (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:369:9)
(node:13134) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first_line' of undefined
    at compiler.then (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/cli.js:51:54)
    at <anonymous>
    at runMicrotasksCallback (internal/process/next_tick.js:121:5)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
(node:13134) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:13134) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Another case:

template Test() {
    signal input in;
    signal output out;
    var t = 0;
    if (in == 0) {
        t += 2;
    }

    out <== t;
}

component main = Test();

Error message:

TypeError: Cannot read property 'neq' of undefined
    at execIf (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:577:18)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:127:16)
    at execBlock (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:223:9)
    at instantiateComponent (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:369:9)
    at execInstantiateComponet (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:328:5)
    at execVarAssignement (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:600:41)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:56:20)
    at execBlock (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:223:9)
    at exec (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/exec.js:121:16)
    at compile (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/src/compiler.js:65:5)
(node:13664) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first_line' of undefined
    at compiler.then (/home/user/.nvm/versions/node/v8.11.3/lib/node_modules/circom/cli.js:51:54)
    at <anonymous>
    at runMicrotasksCallback (internal/process/next_tick.js:121:5)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
(node:13664) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:13664) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
jbaylina commented 5 years ago

Good catch!

Just fixed.

Check v0.0.13

weijiekoh commented 5 years ago

I tried again with this:

template X() {
    signal input i;
    signal input j;
    signal output out;

    if (i == 0) {
        out <-- i;
    }
    else {
        out <-- j;
    }
}

component main = X();

But still received this error.

TypeError: Cannot read property 'neq' of undefined
    at execIf (/home/user/circom/src/exec.js:579:18)
    at exec (/home/user/circom/src/exec.js:129:16)
    at execBlock (/home/user/circom/src/exec.js:225:9)
    at instantiateComponent (/home/user/circom/src/exec.js:371:9)
    at execInstantiateComponet (/home/user/circom/src/exec.js:330:5)
    at execVarAssignement (/home/user/circom/src/exec.js:604:41)
    at exec (/home/user/circom/src/exec.js:56:20)
    at execBlock (/home/user/circom/src/exec.js:225:9)
    at exec (/home/user/circom/src/exec.js:123:16)
    at compile (/home/user/circom/src/compiler.js:66:5)
(node:21899) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'nPubInputs' of undefined
    at new Circuit (/home/user/circom-experiments/node_modules/snarkjs/src/circuit.js:28:38)
    at main (/home/user/circom-experiments/build/test.js:30:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
(node:21899) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:21899) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm compiling the circuit not using v0.0.18. Rather, I'm importing the code from commit 9d0b27a7e8ee45308773d6665d874b5d3d6e59cb.

However, this code works:

template X() {
    signal input i;
    signal input j;
    signal output out;
    out <-- i ? j : i;
}

component main = X();
jbaylina commented 5 years ago

Fixed. Just check version 0.0.19

Be aware also that the above circuit will not generate any constrain. So it will be valid fo any output....

weijiekoh commented 5 years ago

Noted with gratitude. I should use <== to generate the constraints.