iden3 / circom

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

Different Results for Complement Bitwise Operator #270

Closed LukiMueller closed 3 weeks ago

LukiMueller commented 1 month ago

Greetings circom devs,

I found an interesting behavior regarding the bit-wise complement operator ~. The following minimal example has different results for output a and b.

// circuit.circom

template T() {
    signal output a;
    signal output b;

    signal temporary_signal;
    temporary_signal <-- 1;

    a <-- (~temporary_signal);
    b <-- (~1);

    log("a:", a);
    log("b:", b);
}

component main = T();

I compiled with circom --wasm --c circuit.circom on the currently newest version v2.1.9. Both witness generators produce following output:

a: 7059779437489773633646340506914701874769131765994106666166191815402473914365
b: 6350874878119819312338956282401532410528162663560392320966563075034087161849

Shouldn't the output signals be equal or am I missing something? The documentation does not say anything about different behaviors for the ~-operator regarding constants or signals. Is this a bug in the compiler?

Additionally, the CPP generator seems to print the log-statements twice on this instance. At least for me the logs appear two times in the console.

miguelis commented 3 weeks ago

Hi Lukas, thank you for reporting this bug: when bitwise complement operator was applied to a constant, the complement used 256 bits instead of 254. That is why b value was incorrect. We have fixed it here.

Thank you so much. Best regards, circom team