iden3 / snarkjs

zkSNARK implementation in JavaScript & WASM
GNU General Public License v3.0
1.71k stars 413 forks source link

"snarkJS: Error: Scalar size does not match" on Num2Bits #252

Open arc0035 opened 1 year ago

arc0035 commented 1 year ago

Hi, i'm new to zksnark and i'v installed circom2.0 and snarkjs.

Now I'm testing tutorials from https://docs.circom.io/circom-language/basic-operators/:

pragma circom 2.0.0;

template Num2Bits(n) {
    signal input in;
    signal output out[n];
    var lc1=0;
    var e2=1;
    for (var i = 0; i<n; i++) {
        out[i] <-- (in >> i) & 1;
        out[i] * (out[i] -1 ) === 0;
        lc1 += out[i] * e2;
        e2 = e2+e2;
    }
    lc1 === in;
}

component main {public [in]}= Num2Bits(3);

I set the input.json as:

{
    "in": 1
}

Then I compile the circom, calculate the witness, and do the trusted setup:

circom circuit.circom --r1cs --wasm --sym

node circuit_js/generate_witness.js circuit_js/circuit.wasm input.json witness.wtns

snarkjs powersoftau new bn128 12 pot12_0000.ptau -v 
snarkjs powersoftau contribute pot12_0000.ptau pot12_0001.ptau --name="First contribution" -v 
snarkjs powersoftau prepare phase2 pot12_0001.ptau pot12_final.ptau -v 
snarkjs groth16 setup circuit.r1cs pot12_final.ptau circuit_0000.zkey 
snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="First contribution" -v
snarkjs zkey export verificationkey circuit_0001.zkey verification_key.json

But when I generate the proof, the error happens:

snarkjs groth16 prove circuit_0001.zkey witness.wtns proof.json public.json
[ERROR] snarkJS: Error: Scalar size does not match
    at _multiExp (/Users/chubingnan/.nvm/versions/node/v16.14.0/lib/node_modules/snarkjs/node_modules/ffjavascript/build/main.cjs:4975:19)
    at WasmCurve.multiExpAffine (/Users/chubingnan/.nvm/versions/node/v16.14.0/lib/node_modules/snarkjs/node_modules/ffjavascript/build/main.cjs:5012:22)
    at groth16Prove$1 (/Users/chubingnan/.nvm/versions/node/v16.14.0/lib/node_modules/snarkjs/build/cli.cjs:5550:33)
    at async Object.groth16Prove [as action] (/Users/chubingnan/.nvm/versions/node/v16.14.0/lib/node_modules/snarkjs/build/cli.cjs:8459:36)
    at async clProcessor (/Users/chubingnan/.nvm/versions/node/v16.14.0/lib/node_modules/snarkjs/build/cli.cjs:454:27)

Any insights are greatly appreciated!

adam-maj commented 1 year ago

Encountering the same error here myself - I made a simple XOR circuit, and it worked, but now its throwing this error. The difference is that I made both the signals public in the main component.

I would try removing the {public [in]} and rerunning everything, then it should work.

Besides that though - I'm curious what causes the error. We must passing inputs incorrectly or something like that.

ytham commented 1 year ago

I tried removing the {public [inputs]} part after component main, but am still also seeing the same Scalar size does not match error after re-compiling the circuit and re-running groth16 phase 2 before attempting the proof.

Qyanjia commented 1 year ago

Do you have any solution about this ERROR? I also encountered the same problem...even though removed the {public [inputs]} part.

adamiak commented 1 year ago

I have the same issue on a trivial program:

pragma circom 2.0.6;

template Sum() {
    signal input inA;
    signal input inB;
    signal output outSum;
    outSum <== inA + inB;
    outSum === 5;
}

component main {public [inA]} = Sum();

Error:

$ snarkjs groth16 prove simple_sum_0001.zkey witness.wtns proof.json public.json
[ERROR] snarkJS: Error: Scalar size does not match
    at _multiExp (/usr/local/share/nvm/versions/node/v16.19.0/lib/node_modules/snarkjs/node_modules/ffjavascript/build/main.cjs:4975:19)
    at WasmCurve.multiExpAffine (/usr/local/share/nvm/versions/node/v16.19.0/lib/node_modules/snarkjs/node_modules/ffjavascript/build/main.cjs:5012:22)
    at groth16Prove$1 (/usr/local/share/nvm/versions/node/v16.19.0/lib/node_modules/snarkjs/build/cli.cjs:5550:33)
    at async Object.groth16Prove [as action] (/usr/local/share/nvm/versions/node/v16.19.0/lib/node_modules/snarkjs/build/cli.cjs:8459:36)
    at async clProcessor (/usr/local/share/nvm/versions/node/v16.19.0/lib/node_modules/snarkjs/build/cli.cjs:454:27)
adamiak commented 1 year ago

I've been able to fix my problem by adding (inA*inB)*0 to output generation. It seems that the expression must be quadratic (i.e. involve multiplication of exactly two signals)... I'm quite new to this so I'm not sure. Anyway, replacing:

outSum <== inA + inB;

with

outSum <== (inA*inB)*0 + inA + inB;

made it work.

nihaoqingtuan commented 1 year ago

i meet the same question: snarkjs groth16 setup circuit.r1cs pot12_final.ptau circuit_0000.zkey [INFO] snarkJS: Reading r1cs [INFO] snarkJS: Reading tauG1 [INFO] snarkJS: Reading tauG2 [INFO] snarkJS: Reading alphatauG1 [INFO] snarkJS: Reading betatauG1 [ERROR] snarkJS: Error: Scalar size does not match

here is my code: `pragma circom 2.0.0;

template Multiplier2() { signal input a; signal input b; signal output c[100000];

for(var i=0;i<100000;i++){
    c[i] <== a*b;
}

}

component main = Multiplier2();

`

who knows how to fix it?

0x-stan commented 1 year ago
pragma circom 2.0.0;

template Num2Bits(n) {
    ...
    signal d;            // add intermediate signal

    d <-- 1;

    for (var i = 0; i<n; i++) {
        ...
    }
    lc1 === in * d;   // times 1
}

component main {public [in]}= Num2Bits(3);

I had fixed it by adding a intermediate signal d <-- 1

Zvezdin commented 3 months ago

Any thoughts / updates on this? It's still an issue as of the current version.

ideiowo commented 3 months ago

https://github.com/iden3/snarkjs/issues/301 I used the method provided here, it works.