import { fillArrayUntil } from "./utils/fill"; import { groth16 } from "snarkjs"; import { BigNumber } from "ethers"; import { buildPoseidon, EddsaPublicKey, EddsaSignature, } from "@sismo-core/crypto"; import { MerkleTree } from "@sismo-core/merkle-tree"; import { Inputs, SnarkProof } from "./types"; import { EXTERNAL_DATA_MAX_LEVELS } from "./constants"; import { ZkProof } from "."; export class ZKSMPSProver { private wasmPath: string; private zkeyPath: string; private worldMerkleTree: MerkleTree; private commitmentMapperPubKey: EddsaPublicKey; constructor( worldMerkleTree: MerkleTree, commitmentMapperPubKey: EddsaPublicKey ) { this.worldMerkleTree = worldMerkleTree; this.commitmentMapperPubKey = commitmentMapperPubKey; this.wasmPath = __dirname + "/smps.wasm"; this.zkeyPath = __dirname + "/smps.zkey"; } public async generateInputs( source: string, sourceSecret: BigNumber, sourceCommitmentReceipt: EddsaSignature, destination: string, destinationSecret: BigNumber, destinationCommitmentReceipt: EddsaSignature, selectedValue: BigNumber, hostId: number, accountMerkleTree: MerkleTree, externalNullifier: BigNumber ): Promise { const poseidon = await buildPoseidon(); const accountMerklePath = accountMerkleTree.getMerklePathFromKey(source); const accountValue = accountMerkleTree.getValue(source); const worldMerklePath = this.worldMerkleTree.getMerklePathFromKey( accountMerkleTree.getRoot().toHexString() ); const accountTreePosition = this.worldMerkleTree.getPosition( accountMerkleTree.getRoot().toHexString() ); const sourceIdNullifier = poseidon([sourceSecret, 1]); const nullifierHash = poseidon([sourceIdNullifier, externalNullifier]); const privateInputs = { // Source source: BigNumber.from(source), sourceSecret: sourceSecret, sourceCommitmentReceipt: sourceCommitmentReceipt, // Destination destinationSecret: destinationSecret, destinationCommitmentReceipt: destinationCommitmentReceipt, // Data source accountMerkleTreeRoot: accountMerkleTree.getRoot(), // merkle elements for the account validity (address and value) accountMerkleTreeHeight: accountMerkleTree.getHeight(), accountMerkleTreePathElements: fillArrayUntil( accountMerklePath.elements, EXTERNAL_DATA_MAX_LEVELS ), accountMerkleTreePathIndices: fillArrayUntil( accountMerklePath.indices, EXTERNAL_DATA_MAX_LEVELS ), worldMerkleTreePathElements: worldMerklePath.elements, // merkle proof for the account merkle tree validity along the compressedMerkleTree worldMerkleTreePathIndices: worldMerklePath.indices, // nullifierModifier to switch nullifier computation accountValue: accountValue }; const publicInputs = { // Destination destination: BigNumber.from(destination), hostId: BigNumber.from(hostId), // Commitment commitmentMapperPubKey: this.commitmentMapperPubKey, // Data source worldMerkleTreesRoot: this.worldMerkleTree.getRoot(), accountTreePosition: accountTreePosition, // Nullifier externalNullifier: externalNullifier, nullifierHash: nullifierHash, // Value selectedValue: selectedValue, // Define if the selected value can be inferior or not as the accountValue // If it's a score it can be inferior isScore: accountTreePosition % 2, }; return { privateInputs, publicInputs, }; } public async generateSnarkProof( source: string, sourceSecret: BigNumber, sourceCommitmentReceipt: EddsaSignature, destination: string, destinationSecret: BigNumber, destinationCommitmentReceipt: EddsaSignature, selectedValue: BigNumber, hostId: number, accountMerkleTree: MerkleTree, externalNullifier: BigNumber ): Promise { const inputs = await this.generateInputs( source, sourceSecret, sourceCommitmentReceipt, destination, destinationSecret, destinationCommitmentReceipt, selectedValue, hostId, accountMerkleTree, externalNullifier ); const wasmPath = this.wasmPath; const zkeyPath = this.zkeyPath; const { proof, publicSignals } = await groth16.fullProve( { ...inputs.privateInputs, ...inputs.publicInputs }, wasmPath, zkeyPath ); const zkProof: ZkProof = { a: [proof.pi_a[0], proof.pi_a[1]], b: [ [proof.pi_b[0][1], proof.pi_b[0][0]], [proof.pi_b[1][1], proof.pi_b[1][0]], ], c: [proof.pi_c[0], proof.pi_c[1]], }; const input: BigNumber[] = publicSignals; return { ...zkProof, input, }; } }