Skip to content

Commit

Permalink
add smart contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xavi-pinsach committed Apr 8, 2024
1 parent b0c5da7 commit a9f2243
Show file tree
Hide file tree
Showing 5 changed files with 1,043 additions and 2 deletions.
38 changes: 38 additions & 0 deletions smart_contract_tests/test/smart_contracts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ describe("Smart contracts test suite", function () {
)).to.be.equal(true);
});

it("Groth16 smart contract 1 aliased input", async () => {
expect(
await groth16VerifyAliased(
path.join("../test", "groth16", "circuit.r1cs"),
path.join("../test", "groth16", "witness.wtns")
)
).to.be.equal(false);
});

it("Groth16 smart contract 3 inputs", async () => {
expect(await groth16Verify(
path.join("../test", "circuit2", "circuit.r1cs"),
Expand Down Expand Up @@ -100,6 +109,35 @@ describe("Smart contracts test suite", function () {
return await verifierContract.verifyProof(proofA, proofB, proofC, publicInputs);
}

async function groth16VerifyAliased(r1csFilename, wtnsFilename) {
const solidityVerifierFilename = path.join("contracts", "groth16.sol");

const zkeyFilename = { type: "mem" };

await snarkjs.zKey.newZKey(r1csFilename, ptauFilename, zkeyFilename);
const { proof: proof, publicSignals: publicInputs } = await snarkjs.groth16.prove(zkeyFilename, wtnsFilename);

const proofA = [proof.pi_a[0], proof.pi_a[1]];
const proofB = [[proof.pi_b[0][1], proof.pi_b[0][0]], [proof.pi_b[1][1], proof.pi_b[1][0]],];
const proofC = [proof.pi_c[0], proof.pi_c[1]];

// Generate groth16 verifier solidity file from groth16 template + zkey
const verifierCode = await snarkjs.zKey.exportSolidityVerifier(zkeyFilename, templates);
fs.writeFileSync(solidityVerifierFilename, verifierCode, "utf-8");

// Compile the groth16 verifier smart contract
await run("compile");

let pi_with_alias = [...publicInputs];
pi_with_alias[1] = BigInt(publicInputs[1]) + 21888242871839275222246405745257275088548364400416034343698204186575808495617n;

// Deploy mock groth16 verifier
const VerifierFactory = await ethers.getContractFactory("Groth16Verifier");
verifierContract = await VerifierFactory.deploy();

return await verifierContract.verifyProof(proofA, proofB, proofC, pi_with_alias);
}

async function plonkVerify(r1csFilename, wtnsFilename) {
const solidityVerifierFilename = path.join("contracts", "plonk.sol");

Expand Down
3 changes: 1 addition & 2 deletions test/fullprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ describe("Full process", function () {
proof = res.proof;
publicSignals = res.publicSignals;
publicSignalsWithAlias = [...res.publicSignals];
publicSignalsWithAlias[1] = BigInt(res.publicSignals[1]) + BigInt(21888242871839275222246405745257275088548364400416034343698204186575808495617n);
publicSignalsWithAlias[1] = BigInt(res.publicSignals[1]) + 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
});


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

0 comments on commit a9f2243

Please sign in to comment.