Skip to content

Commit

Permalink
add chai-zkit
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvolear committed Nov 18, 2024
1 parent a36b3d8 commit 5002c18
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 56 deletions.
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "@nomicfoundation/hardhat-toolbox";
import "@solarity/hardhat-zkit";
import "@solarity/chai-zkit";
import "tsconfig-paths/register";

import { HardhatUserConfig } from "hardhat/config";
Expand Down
30 changes: 28 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"@solarity/solidity-lib": "^2.7.11",
"@solarity/hardhat-zkit": "^0.5.2",
"@solarity/chai-zkit": "^0.3.0",
"@typechain/ethers-v6": "^0.5.1",
"@types/chai": "^4.3.14",
"@types/mocha": "^10.0.9",
Expand Down
23 changes: 11 additions & 12 deletions test/blinders/Commitment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@ describe("Commitment", () => {
afterEach(reverter.revert);

it("should return commitment and hash of nullifier", async () => {
await expect(commitmentCircuit)
.with.witnessInputs({
nullifier: 1,
secret: 2,
})
.to.have.witnessOutputs({
commitment: "7853200120776062878684798364095072458815029376092732009249414926327459813530",
nullifierHash: "18586133768512220936620570745912940619677854269274689475585506675881198879027",
});

const proofStruct = await commitmentCircuit.generateProof({
nullifier: 1,
secret: 2,
});

// Poseidon(1, 2)
expect(proofStruct.publicSignals.commitment).to.equal(
"7853200120776062878684798364095072458815029376092732009249414926327459813530",
);
// Poseidon(1)
expect(proofStruct.publicSignals.nullifierHash).to.equal(
"18586133768512220936620570745912940619677854269274689475585506675881198879027",
);

const [pA, pB, pC, publicSignals] = await commitmentCircuit.generateCalldata(proofStruct);

expect(await commitmentVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(commitmentCircuit).to.useSolidityVerifier(commitmentVerifier).and.verifyProof(proofStruct);
});
});
68 changes: 26 additions & 42 deletions test/data-structures/SparseMerkleTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ describe("SparseMerkleTree", () => {
isExclusion: 0,
});

const [pA, pB, pC, publicSignals] = await smtCircuit.generateCalldata(proofStruct);

expect(await smtVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(smtCircuit).to.useSolidityVerifier(smtVerifier).and.verifyProof(proofStruct);
});

it("should prove the tree inclusion for each depth of bamboo", async () => {
Expand All @@ -92,9 +90,7 @@ describe("SparseMerkleTree", () => {
isExclusion: 0,
});

const [pA, pB, pC, publicSignals] = await smtCircuit.generateCalldata(proofStruct);

expect(await smtVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(smtCircuit).to.useSolidityVerifier(smtVerifier).and.verifyProof(proofStruct);
}
});

Expand All @@ -115,9 +111,7 @@ describe("SparseMerkleTree", () => {
isExclusion: 0,
});

const [pA, pB, pC, publicSignals] = await smtCircuit.generateCalldata(proofStruct);

expect(await smtVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(smtCircuit).to.useSolidityVerifier(smtVerifier).and.verifyProof(proofStruct);
});

it("should prove the tree exclusion", async () => {
Expand All @@ -140,9 +134,7 @@ describe("SparseMerkleTree", () => {
isExclusion: 1,
});

const [pA, pB, pC, publicSignals] = await smtCircuit.generateCalldata(proofStruct);

expect(await smtVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(smtCircuit).to.useSolidityVerifier(smtVerifier).and.verifyProof(proofStruct);
});

it("should prove the tree exclusion for each depth of bamboo", async () => {
Expand All @@ -166,9 +158,7 @@ describe("SparseMerkleTree", () => {
isExclusion: 1,
});

const [pA, pB, pC, publicSignals] = await smtCircuit.generateCalldata(proofStruct);

expect(await smtVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(smtCircuit).to.useSolidityVerifier(smtVerifier).and.verifyProof(proofStruct);
}
});

Expand All @@ -188,9 +178,7 @@ describe("SparseMerkleTree", () => {
isExclusion: 1,
});

const [pA, pB, pC, publicSignals] = await smtCircuit.generateCalldata(proofStruct);

expect(await smtVerifier.verifyProof(pA, pB, pC, publicSignals)).to.be.true;
await expect(smtCircuit).to.useSolidityVerifier(smtVerifier).and.verifyProof(proofStruct);
});

describe("when data is incorrect", () => {
Expand All @@ -213,18 +201,16 @@ describe("SparseMerkleTree", () => {

const incorrectValue = merkleProof.value + 1n;

await expect(
smtCircuit.generateProof({
root: BigInt(merkleProof.root),
siblings: merkleProof.siblings.map((e) => BigInt(e)),
key: BigInt(merkleProof.key),
value: BigInt(incorrectValue),
auxKey: 0,
auxValue: 0,
auxIsEmpty: 0,
isExclusion: 0,
}),
).to.be.rejected;
await expect(smtCircuit).to.not.generateProof({
root: BigInt(merkleProof.root),
siblings: merkleProof.siblings.map((e) => BigInt(e)),
key: BigInt(merkleProof.key),
value: BigInt(incorrectValue),
auxKey: 0,
auxValue: 0,
auxIsEmpty: 0,
isExclusion: 0,
});
});

it("should revert an incorrect tree exclusion", async () => {
Expand All @@ -238,18 +224,16 @@ describe("SparseMerkleTree", () => {

auxIsEmpty = auxIsEmpty == 0 ? 1 : 0;

await expect(
smtCircuit.generateProof({
root: BigInt(merkleProof.root),
siblings: merkleProof.siblings.map((e) => BigInt(e)),
key: BigInt(merkleProof.key),
value: 0,
auxKey: BigInt(merkleProof.auxKey),
auxValue: BigInt(merkleProof.auxValue),
auxIsEmpty: auxIsEmpty,
isExclusion: 1,
}),
).to.be.rejected;
await expect(smtCircuit).to.not.generateProof({
root: BigInt(merkleProof.root),
siblings: merkleProof.siblings.map((e) => BigInt(e)),
key: BigInt(merkleProof.key),
value: 0,
auxKey: BigInt(merkleProof.auxKey),
auxValue: BigInt(merkleProof.auxValue),
auxIsEmpty: auxIsEmpty,
isExclusion: 1,
});
});
});
});

0 comments on commit 5002c18

Please sign in to comment.