iden3 / circom

zkSnark circuit compiler
GNU General Public License v3.0
1.31k stars 253 forks source link

Constraints are optimized away mistakenly by using O2 #252

Open Yu3H0 opened 6 months ago

Yu3H0 commented 6 months ago

Hello, when I use a circuit from circomlib, I found that the constraints were opmitized away. The code is as below:

pragma circom 2.0.0;

template Bits2Num(n) {
    signal input in[n];
    signal output out;
    var lc1=0;

    var e2 = 1;
    for (var i = 0; i<n; i++) {
        lc1 += in[i] * e2;
        e2 = e2 + e2;
    }

    lc1 ==> out;
}

component main = Bits2Num(8);

You can see from the following picture that there is no constraints when using O2 while there is a constraint when using O0.

image

From your document, we can know that, the constraints can be skipped if it is linear. But I think the rule is only for the intermediate signal.

https://docs.circom.io/circom-language/circom-insight/simplification/

I would appreciate it a lot if you can add an end-of-optimization determination for this situation.