Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snarkJS: Error:Scalar size does not match #330

Closed
Baicha-ll opened this issue Mar 13, 2023 · 3 comments
Closed

snarkJS: Error:Scalar size does not match #330

Baicha-ll opened this issue Mar 13, 2023 · 3 comments

Comments

@Baicha-ll
Copy link

Baicha-ll commented Mar 13, 2023

circom version 2.1.4 and snarkjs version 0.6.8 were used.

The circom source code looks like this,the goal is to achieve a linear regression proof,

pragma circom 2.1.4; ``
template linear(N) {
   signal input X[N];
   signal input y[N];
   signal mid_w;
   signal mid_b;
   var lr = 1/10000;
   var w = -1/10;
   var b = 3;
   var y_hat;
   var det_w;
   var det_b;
   var temp;
   signal output w_result;
   signal output b_result;

   var X_train[N];
   var y_train[N];
   var epoches = 100;
   for(var i=0;i<N;i++){
       X_train[i] = X[i];
       y_train[i] = y[i];
   }
   
   for(var i = 0;i < epoches;i++){
      var sum_w = 0;
      var sum_b = 0;
      for(var j= 0;j<N;j++){
         y_hat = w * X_train[j] + b ;
         temp = y_train[j] - y_hat;
         sum_w = temp * (-X_train[j]) + sum_w;
         sum_b = temp * (-1) + sum_b;
      }
   det_w = 2 * sum_w;
   det_b = 2 * sum_b;
   w =  w - lr * det_w;
   b =  b - lr * det_b;
   }
   mid_w <-- w; 
   mid_b <-- b;
   w_result <== mid_w;
   b_result <== mid_b;  
}
component main = linear(30);``

Then I ran the following command as prompted by snarkjs,

snarkjs powersoftau new bn128 14 pot14_0000.ptau -v
snarkjs powersoftau contribute pot14_0000.ptau pot14_0001.ptau --name="First contribution" -v
snarkjs powersoftau contribute pot14_0001.ptau pot14_0002.ptau --name="Second contribution" -v -e="some random text"
snarkjs powersoftau export challenge pot14_0002.ptau challenge_0003
snarkjs powersoftau challenge contribute bn128 challenge_0003 response_0003 -e="some random text"
snarkjs powersoftau import response pot14_0002.ptau response_0003 pot14_0003.ptau -n="Third contribution name"
snarkjs powersoftau verify pot14_0003.ptau
snarkjs powersoftau beacon pot14_0003.ptau pot14_beacon.ptau 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon"
snarkjs powersoftau prepare phase2 pot14_beacon.ptau pot14_final.ptau -v
snarkjs powersoftau verify pot14_final.ptau
circom circuit.circom --r1cs --wasm --sym
snarkjs r1cs info circuit.r1cs
snarkjs r1cs print circuit.r1cs circuit.sym
snarkjs r1cs export json circuit.r1cs circuit.r1cs.json
cat circuit.r1cs.json
circuit_js$ node generate_witness.js circuit.wasm ../input.json ../witness.wtns
node generate_witness.js circuit.wasm ../input.json ../witness.wtns
snarkjs groth16 setup circuit.r1cs pot14_final.ptau circuit_0000.zkey
snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="1st Contributor Name" -v
snarkjs zkey contribute circuit_0001.zkey circuit_0002.zkey --name="Second contribution Name" -v -e="Another random entropy"
snarkjs zkey export bellman circuit_0002.zkey  challenge_phase2_0003
snarkjs zkey bellman contribute bn128 challenge_phase2_0003 response_phase2_0003 -e="some random text"
snarkjs zkey import bellman circuit_0002.zkey response_phase2_0003 circuit_0003.zkey -n="Third contribution name"
snarkjs zkey verify circuit.r1cs pot14_final.ptau circuit_0003.zkey
snarkjs zkey beacon circuit_0003.zkey circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify circuit.r1cs pot14_final.ptau circuit_final.zkey
snarkjs zkey export verificationkey circuit_final.zkey verification_key.json
snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json

The above commands are all copied and pasted snarkjs, and the circom source code can be compiled correctly, but an error is reported when executing "snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json".

~2VX54W`KFH$J4PYET3YZ6D

@Baicha-ll
Copy link
Author

Baicha-ll commented Mar 13, 2023

input.json:

{"X": [12, 14, 14, 14, 16, 16, 16, 15, 17, 17, 18, 20, 22, 19, 15, 16, 17, 18, 19,17,19, 19, 21, 23, 23, 24, 25, 25, 25, 26],
"y":  [11, 12, 13, 11, 14, 15, 13, 13, 14, 14, 15, 18, 20, 15,14, 14, 14, 16, 17,14, 15, 16, 19, 19, 20, 20, 21, 22, 22, 22]}

where X and y each have 30 data.

@phated
Copy link
Contributor

phated commented Mar 13, 2023

@Baicha-ll
Copy link
Author

I see where I went wrong, my circuit didn't have a formal constraint

@phated phated closed this as completed Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants