Skip to content

Commit

Permalink
PI check and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xavi-pinsach committed Apr 8, 2024
1 parent e44656d commit b0c5da7
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion templates/verifier_groth16.sol.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract Groth16Verifier {
function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[<%=IC.length-1%>] calldata _pubSignals) public view returns (bool) {
assembly {
function checkField(v) {
if iszero(lt(v, q)) {
if iszero(lt(v, r)) {
mstore(0, 0)
return(0, 0x20)
}
Expand Down
14 changes: 10 additions & 4 deletions test/fullprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("Full process", function () {
const wtns = {type: "mem"};
let proof;
let publicSignals;
let publicSignalsWithAlias;

before( async () => {
curve = await getCurveFromName("bn128");
Expand Down Expand Up @@ -70,7 +71,7 @@ describe("Full process", function () {
});

it ("groth16 setup", async () => {
await snarkjs.zKey.newZKey(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_0);
await snarkjs.zKey.newZKey(path.join("test", "groth16", "circuit.r1cs"), ptau_final, zkey_0);
});

it ("zkey contribute ", async () => {
Expand All @@ -94,7 +95,7 @@ describe("Full process", function () {
});

it ("zkey verify r1cs", async () => {
const res = await snarkjs.zKey.verifyFromR1cs(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_final);
const res = await snarkjs.zKey.verifyFromR1cs(path.join("test", "groth16", "circuit.r1cs"), ptau_final, zkey_final);
assert(res);
});

Expand All @@ -108,23 +109,28 @@ describe("Full process", function () {
});

it ("witness calculate", async () => {
await snarkjs.wtns.calculate({a: 11, b:2}, path.join("test", "circuit", "circuit.wasm"), wtns);
await snarkjs.wtns.calculate({a: 11, b:2}, path.join("test", "groth16", "circuit.wasm"), wtns);
});

it ("checks witness complies with r1cs", async () => {
await snarkjs.wtns.check(path.join("test", "circuit", "circuit.r1cs"), wtns);
await snarkjs.wtns.check(path.join("test", "groth16", "circuit.r1cs"), wtns);
});

it ("groth16 proof", async () => {
const res = await snarkjs.groth16.prove(zkey_final, wtns);
proof = res.proof;
publicSignals = res.publicSignals;
publicSignalsWithAlias = [...res.publicSignals];
publicSignalsWithAlias[1] = BigInt(res.publicSignals[1]) + BigInt(21888242871839275222246405745257275088548364400416034343698204186575808495617n);
});


it ("groth16 verify", async () => {
const res = await snarkjs.groth16.verify(vKey, publicSignals, proof);
assert(res == true);

const res2 = await snarkjs.groth16.verify(vKey, publicSignalsWithAlias, proof);
assert(res2 == false);
});

it ("plonk setup", async () => {
Expand Down
16 changes: 16 additions & 0 deletions test/groth16/circuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
template Multiplier(n) {
signal input a;
signal input b;
signal output c;

signal int[n];

int[0] <== a*a + b;
for (var i=1; i<n; i++) {
int[i] <== int[i-1]*int[i-1] + b;
}

c <== int[n-1];
}

component main {public [a]} = Multiplier(1000);
Binary file modified test/groth16/circuit.r1cs
Binary file not shown.
Binary file added test/groth16/circuit.wasm
Binary file not shown.
Binary file removed test/groth16/circuit.zkey
Binary file not shown.
4 changes: 0 additions & 4 deletions test/groth16/witness.json

This file was deleted.

Binary file removed test/groth16/witness.wtns
Binary file not shown.

0 comments on commit b0c5da7

Please sign in to comment.