import { MerkleTree, MerkleTreeData } from "@sismo-core/merkle-tree"; import { BigNumber } from "ethers"; import hre from "hardhat"; import path from "path"; import { describe } from "mocha"; import { buildPoseidon, EddsaSignature, Poseidon } from "@sismo-core/crypto"; import { circuitShouldFail } from "./utils/circuitShouldFail"; import { EXTERNAL_DATA_MAX_LEVELS, fillArrayUntil, PrivateInputs, PublicInputs, ZKSMPSProver } from "../packages/prover-js/src"; import { CommitmentMapperTester } from "../packages/commitment-mapper-tester-js/src/commitmentMapperTester"; import { wasm, WasmTester } from "circom_tester"; type Account = { address: string, secret: BigNumber, commitmentReceipt: EddsaSignature } describe("ZK-SMPS Prover", () => { let commitmentMapperTester: CommitmentMapperTester; let accounts: Account[]; let circuitTester: WasmTester; let externalNullifier: BigNumber; let worldMerkleTree: MerkleTree; let poseidon: Poseidon; let inputsInfo: PublicInputs & PrivateInputs; let inputsScore: PublicInputs & PrivateInputs; let accountMerkleTreeInfo: MerkleTree; let accountMerkleTreeScore: MerkleTree; let merkleTreeDataInfo: MerkleTreeData; let merkleTreeDataScore: MerkleTreeData; before(async () => { poseidon = await buildPoseidon(); 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 }) } circuitTester = await wasm(path.join(__dirname, "../circuits", "smps.circom")); externalNullifier = BigNumber.from(123); // This account Merkle tree is store in position 0 of the Merkle world which is even // This mean the value is an information // The user can't select a value inferior or equal // For example this could be a Twitter id merkleTreeDataInfo = { [accounts[0].address]: 4, [accounts[1].address]: 5, [accounts[2].address]: 6, [accounts[3].address]: 7 }; accountMerkleTreeInfo = new MerkleTree(merkleTreeDataInfo, poseidon); // This account Merkle tree is store in position 1 of the Merkle world which is odd // This mean the value is an information // The user can select a value inferior or equal of his score // For example this could be an Ether balance merkleTreeDataScore = { [accounts[0].address]: 4, [accounts[1].address]: 2, [accounts[2].address]: 5, [accounts[3].address]: 6 } accountMerkleTreeScore = new MerkleTree(merkleTreeDataScore, poseidon); worldMerkleTree = new MerkleTree({ [accountMerkleTreeInfo.getRoot().toHexString()]: accountMerkleTreeInfo.getHeight(), [accountMerkleTreeScore.getRoot().toHexString()]: accountMerkleTreeScore.getHeight(), }, poseidon, 13); }) describe("test merkle formula circuit", async () => { it("Snark proof of simple value in a merkleTree with simple nullifierHash", async () => { const prover = new ZKSMPSProver( worldMerkleTree, commitmentMapperTester.getPubKey() ); const host = parseInt(await hre.getChainId()); const source = accounts[0]; const destination = accounts[4]; const accountValue = BigNumber.from(merkleTreeDataInfo[source.address]); const { privateInputs, publicInputs } = await prover.generateInputs( source.address, source.secret, source.commitmentReceipt, destination.address, destination.secret, destination.commitmentReceipt, accountValue, host, accountMerkleTreeInfo, externalNullifier ); inputsInfo = { ...privateInputs, ...publicInputs }; const w = await circuitTester.calculateWitness(inputsInfo, true); await circuitTester.checkConstraints(w); }); describe("Verifing source address constraints are good", async () => { it("should throw with wrong source address", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ source: BigNumber.from(accounts[1].address) } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) it("should throw with wrong sourceSecret", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ sourceSecret: BigNumber.from(123) } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) it("should throw with wrong sourceCommitmentReceipt", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ sourceCommitmentReceipt: [BigNumber.from(1),BigNumber.from(2),BigNumber.from(3)] } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) }); describe("Verifing destination address contraints are good", async () => { it("should throw with wrong destination address", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ destination: BigNumber.from(accounts[5].address) } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) it("should throw with wrong destinationSecret", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ destinationSecret: BigNumber.from(123) } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) it("should throw with wrong destinationCommitmentReceipt", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ destinationCommitmentReceipt: [BigNumber.from(1),BigNumber.from(2),BigNumber.from(3)] } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) }); it("should throw with wrong commitmentMapperPubKey", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ commitmentMapperPubKey: [BigNumber.from(1),BigNumber.from(2)] } }, "Error: Assert Failed. Error in template ForceEqualIfEnabled"); }) describe("Verify externalDataMerkleTree constraint against the globalSismoTree", async () => { it("Should verify the data merkle tree position constraint", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ accountTreePosition: 12 } }, "Error: Assert Failed. Error in template computeFormula"); }) it("Should verify the global sismo tree root constraint", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ worldMerkleTreesRoot: worldMerkleTree.getRoot().add(1), } }, "Error: Assert Failed. Error in template VerifyMerkleTreePath"); }) it("Should verify the accountMerkleTreeRoot constraint along the global sismo merkle tree", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ worldMerkleTreePathElements: inputsInfo.worldMerkleTreePathElements.slice().reverse(), } }, "Error: Assert Failed. Error in template VerifyMerkleTreePath"); }) }); describe("Verify data merkle tree constraint is good", async () => { it("Should throw when having a bad data merkle tree root or a bad data merkle tree height", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ accountMerkleTreeRoot: BigNumber.from(123) } // changing only the merkle Root }, "Error: Assert Failed. Error in template VerifyMerkleTreePath"); await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ accountMerkleTreeHeight: inputsInfo.accountMerkleTreeHeight + 1 } // changing only the merkle Height }, "Error: Assert Failed. Error in template VerifyMerkleTreePath"); }); it("Should throw when using a bad Merkle path", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ accountMerkleTreePathElements: fillArrayUntil([123, 345], EXTERNAL_DATA_MAX_LEVELS) } }, "Error: Assert Failed. Error in template VerifyMerkleTreePath"); await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ accountMerkleTreePathIndices: fillArrayUntil([1, 0], EXTERNAL_DATA_MAX_LEVELS) } }, "Error: Assert Failed. Error in template VerifyMerkleTreePath"); }); it("Should throw when using a good Merkle path but for an other source address than the specified one", async () => { const externalDataMerklePathSource2 = accountMerkleTreeInfo.getMerklePathFromKey(accounts[2].address); await circuitShouldFail(circuitTester, { ...inputsInfo, accountMerkleTreePathElements: externalDataMerklePathSource2.elements, accountMerkleTreePathIndices: externalDataMerklePathSource2.indices }, "Constraint doesn't match"); }); }); describe("Verify the value selected by the user", async () => { it("Snark proof of simple value in a merkleTree with simple nullifierHash", async () => { const prover = new ZKSMPSProver( worldMerkleTree, commitmentMapperTester.getPubKey() ); const host = parseInt(await hre.getChainId()); const source = accounts[0]; const destination = accounts[4]; const accountValue = BigNumber.from(merkleTreeDataScore[source.address]); const { privateInputs, publicInputs } = await prover.generateInputs( source.address, source.secret, source.commitmentReceipt, destination.address, destination.secret, destination.commitmentReceipt, accountValue, host, accountMerkleTreeScore, externalNullifier ); inputsScore = { ...privateInputs, ...publicInputs }; const w = await circuitTester.calculateWitness(inputsScore, true); await circuitTester.checkConstraints(w); }); it("Should throw when using a value superior of the Merkle tree value for info attestation", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ selectedValue: BigNumber.from(5) } // the good one is value: 4 }, "Error: Assert Failed. Error in template computeFormula"); }); it("Should throw when using a value superior of the Merkle tree value for score attestation", async () => { await circuitShouldFail(circuitTester, { ...inputsScore, ...{ selectedValue: BigNumber.from(5) } // the good one is value: 4 }, "Error: Assert Failed. Error in template computeFormula"); }); it("Should throw when using negative value for info attestation", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ selectedValue: BigNumber.from(-5) } // the good one is value: 4 }, "Error: Assert Failed. Error in template computeFormula"); }); it("Should throw when using negative value for score attestation", async () => { await circuitShouldFail(circuitTester, { ...inputsScore, ...{ selectedValue: BigNumber.from(-5) } // the good one is value: 4 }, "Error: Assert Failed. Error in template computeFormula"); }); it("Should throw when using a value inferior of the Merkle tree value for info attestation", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ selectedValue: BigNumber.from(3) } // the good one is value: 4 }, "Error: Assert Failed. Error in template computeFormula"); }); it("Should generate a Snark proof when using a value inferior of the Merkle tree value for score attestation", async () => { const w = await circuitTester.calculateWitness({ ...inputsScore, ...{ selectedValue: BigNumber.from(3) } // the good one is value: 4 }, true); await circuitTester.checkConstraints(w); }); }); describe("Verify nullifierHash validity", async () => { it("Should throw when the externalNullifier does not corresponds to the nullifierHash ", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ externalNullifier: BigNumber.from(456) } // good one is 123 }, "Error: Assert Failed. Error in template computeFormula"); }); it("Should throw when the nullifierHash does not corresponds to the externalNullifier and sourceNullifier", async () => { await circuitShouldFail(circuitTester, { ...inputsInfo, ...{ nullifierHash: BigNumber.from(789) } }, "Error: Assert Failed. Error in template computeFormula"); }); }); }); })