iden3 / circom

zkSnark circuit compiler
GNU General Public License v3.0
1.25k stars 232 forks source link

Compiler Panic when using nested Ternary Expressions #263

Open MarkCampbell90 opened 2 months ago

MarkCampbell90 commented 2 months ago

I was playing around with ternary expressions and found following unreachable code error during the c and wasm witness code generation. I am using circom 2.1.8.

Commands circom --wasm circuit.circom or circom --c circuit.circom

Circom File

// circuit.circom

pragma circom 2.1.8;

template main_template() {
    signal input in;
    signal output out;
    out <-- (((in ? 1 : 0) ? 1 : 0));
}

component main = main_template();

For both circuit targets (c and wasm) I get following error:

thread 'main' panicked at compiler/src/intermediate_representation/translate.rs:687:9:
internal error: entered unreachable code: This expression is syntactic sugar
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

What I find particularly interesting is that when I create a temporary variable everything compiles as expected:

pragma circom 2.1.8;

template main_template() {
    signal input in;
    signal output out;
    var tmp = (in ? 1 : 0);
    out <-- (tmp ? 1 : 0);
    // out <-- (((in ? 1 : 0) ? 1 : 0));
}

component main = main_template();

Is it not possible to have multiple nested ternary expressions in the condition part? Or is this simply not implemented yet?

Personally, I find the triggered unreachable!("This expression is syntactic sugar") error confusing. The code is clearly reachable and I am unsure what the message This expression is syntactic sugar is referring to.