iden3 / circom

zkSnark circuit compiler
GNU General Public License v3.0
1.28k stars 244 forks source link

unexpected result of negative input #208

Closed okwme closed 11 months ago

okwme commented 1 year ago

Reposting this from https://github.com/0xPARC/zkrepl/issues/11 in case it's not a zkrepl issue but a circom issue:


I have been grappling with handling negative numbers in circom and came across this issue. As I understand it circom divides the finite field so that negative numbers span p / 2 and p - 1. When confirming some of this behaviour in zkrepl I saw unexpected results for negative input values:

https://zkrepl.dev/?gist=c9691b5e248e1b080cc6ea0c532b5c38

pragma circom 2.1.4;

template Example () {
    signal input a;
    log("a", a);
    signal input p_minus_one;
    log("p_minus_one", p_minus_one);

    signal output c;

    assert(-1 == p_minus_one);
    assert(-1 == a);
}

component main { public [a]  } = Example();

/* INPUT = {
    "a": "-1",
    "p_minus_one": "21888242871839275222246405745257275088548364400416034343698204186575808495616"
} */

The resulting log is:

a 4294967295
p_minus_one 21888242871839275222246405745257275088548364400416034343698204186575808495616

and the compilation fails on assert(-1 == a);

Is this expected behaviour?

clararod9 commented 12 months ago

Hi, thank you for reporting!

Circom compiler allows the use of negative inputs in the json file since version 2.1.1.

I could recreate the issue using previous versions, but for any version >=v2.1.1 the behavior is the expected:

a 21888242871839275222246405745257275088548364400416034343698204186575808495616
p_minus_one 21888242871839275222246405745257275088548364400416034343698204186575808495616

Thank you for reporting the issue :-)

okwme commented 11 months ago

Thank you for the reply @clararod9!
zkrepl shows version 2.1.4 being used and the error still occurs. That makes me think it must be an issue on the webapp instead of with circom. Thank you for confirming expected behavior at the circom level!