import hre from "hardhat"; import { BigNumber } from "ethers"; import { describe } from "mocha"; import { ZKSMPSVerifier } from "../types"; import { ZKSMPSVerifier as ZKSMPSVerifierOffchain } from "../packages/verifier-js/src"; import { expect } from "chai"; import { MerkleTree, MerkleTreeData } from "@sismo-core/merkle-tree"; import { buildPoseidon, EddsaSignature } from "@sismo-core/crypto"; import { SnarkProof, ZKSMPSProver } from "../packages/prover-js/src"; import { CommitmentMapperTester } from "../packages/commitment-mapper-tester-js/src/commitmentMapperTester"; type Account = { address: string, secret: BigNumber, commitmentReceipt: EddsaSignature } describe("ZK-SMPS Verifier", () => { let commitmentMapperTester: CommitmentMapperTester; let accounts: Account[]; let externalNullifier: BigNumber; let ZKSMPSVerifierContract: ZKSMPSVerifier; let proof: SnarkProof; let worldMerkleTree: MerkleTree; let accountMerkleTree: MerkleTree; let merkleTreeData: MerkleTreeData; before(async () => { // init poseidon hash function and elliptic curve setup const poseidon = await buildPoseidon(); // generate an commitmentMapper that make the link between an ethereum account and a eddsa account. commitmentMapperTester = await CommitmentMapperTester.generate(); const signers = await hre.ethers.getSigners(); accounts = []; for(let i = 0; i < 10; i++) { const address = (await signers[i].getAddress()).toLowerCase(); const signature = await signers[i].signMessage("Eddsa private key account ownership"); const secret = BigNumber.from(i); const commitment = poseidon([secret]); const commitmentReceipt = await commitmentMapperTester.commit(address, signature, commitment); accounts.push({ address, secret, commitmentReceipt }) } externalNullifier = BigNumber.from(123); merkleTreeData = { [accounts[0].address]: 1, [accounts[1].address]: 2, [accounts[2].address]: 3, [accounts[3].address]: 4 }; accountMerkleTree = new MerkleTree(merkleTreeData, poseidon); worldMerkleTree = new MerkleTree({ [accountMerkleTree.getRoot().toHexString()]: accountMerkleTree.getHeight() }, poseidon, 13); //deploy contracts ({ zkSmpsVerifier: ZKSMPSVerifierContract } = await hre.run( "deploy-zk-smps-verifier", { deploymentNamePrefix: "TEST" } )); }) it("Should be able to generate the proof using the prover package", async () => { const prover = new ZKSMPSProver( worldMerkleTree, commitmentMapperTester.getPubKey() ); const source = accounts[0]; const destination = accounts[4]; const selectedValue = BigNumber.from(merkleTreeData[source.address]); proof = await prover.generateSnarkProof( source.address, source.secret, source.commitmentReceipt, destination.address, destination.secret, destination.commitmentReceipt, selectedValue, parseInt(await hre.getChainId()), accountMerkleTree, externalNullifier ); }) it("Should be able to verify the proof using the verifier contract and offchain verifier", async () => { const isValidContract = await ZKSMPSVerifierContract.verifyProof(proof.a, proof.b, proof.c, proof.input); const isValidOffChain = await ZKSMPSVerifierOffchain.verifyProof(proof.a, proof.b, proof.c, proof.input); expect(isValidContract).to.equals(true); expect(isValidOffChain).to.equals(true); }); it("Should change a public input and expect the verifier to revert", async () => { const invalidInput = proof.input; invalidInput[3] = BigNumber.from(123) // override signal corresponding to worldMerkleTreesRoot const isValidContract = await ZKSMPSVerifierContract.verifyProof(proof.a, proof.b, proof.c, invalidInput); const isValidOffChain = await ZKSMPSVerifierOffchain.verifyProof(proof.a, proof.b, proof.c, invalidInput); expect(isValidContract).to.equals(false); expect(isValidOffChain).to.equals(false); }); });