diff --git a/packages/protocols/README.md b/packages/protocols/README.md index 4a0a440..f089dde 100644 --- a/packages/protocols/README.md +++ b/packages/protocols/README.md @@ -66,12 +66,11 @@ import { generateMerkleProof } from "@zk-kit/protocols" const depth = 20 const zeroValue = BigInt(0) -const arity = 2 const identity = new ZkIdentity() const identityCommitment = identity.genIdentityCommitment() const identityCommitments = [BigInt(1), identityCommitment, BigInt(2)] -const merkleProof = generateMerkleProof(depth, zeroValue, arity, identityCommitments, 1) +const merkleProof = generateMerkleProof(depth, zeroValue, identityCommitments, 1) ``` ### Creating Semaphore proofs diff --git a/packages/protocols/src/utils.ts b/packages/protocols/src/utils.ts index c3b85d8..f895aab 100644 --- a/packages/protocols/src/utils.ts +++ b/packages/protocols/src/utils.ts @@ -29,17 +29,11 @@ export function genExternalNullifier(plaintext: string): string { * Creates a Merkle tree. * @param depth The depth of the tree. * @param zeroValue The zero value of the tree. - * @param arity The number of leaves per node. * @param leaves The list of the leaves of the tree. * @returns The Merkle tree. */ -export function generateMerkleTree( - depth: number, - zeroValue: StrBigInt, - arity: number, - leaves: StrBigInt[] -): IncrementalMerkleTree { - const tree = new IncrementalMerkleTree(poseidon, depth, zeroValue, arity) +export function generateMerkleTree(depth: number, zeroValue: StrBigInt, leaves: StrBigInt[]): IncrementalMerkleTree { + const tree = new IncrementalMerkleTree(poseidon, depth, zeroValue, 2) for (const leaf of leaves) { tree.insert(BigInt(leaf)) @@ -52,7 +46,6 @@ export function generateMerkleTree( * Creates a Merkle proof. * @param depth The depth of the tree. * @param zeroValue The zero value of the tree. - * @param arity The number of leaves per node. * @param leaves The list of the leaves of the tree. * @param leaf The leaf for which Merkle proof should be created. * @returns The Merkle proof. @@ -60,13 +53,12 @@ export function generateMerkleTree( export function generateMerkleProof( depth: number, zeroValue: StrBigInt, - arity: number, leaves: StrBigInt[], leaf: StrBigInt ): MerkleProof { if (leaf === zeroValue) throw new Error("Can't generate a proof for a zero leaf") - const tree = generateMerkleTree(depth, zeroValue, arity, leaves) + const tree = generateMerkleTree(depth, zeroValue, leaves) const leafIndex = tree.leaves.indexOf(BigInt(leaf)) @@ -76,10 +68,7 @@ export function generateMerkleProof( const merkleProof = tree.createProof(leafIndex) - if (arity === 2) { - // It makes it compatible with the parameter in the contracts. - merkleProof.siblings = merkleProof.siblings.map((s) => s[0]) - } + merkleProof.siblings = merkleProof.siblings.map((s) => s[0]) return merkleProof } diff --git a/packages/protocols/tests/rln.test.ts b/packages/protocols/tests/rln.test.ts index 7f88508..ff1c16d 100644 --- a/packages/protocols/tests/rln.test.ts +++ b/packages/protocols/tests/rln.test.ts @@ -41,7 +41,7 @@ describe("RLN", () => { const epoch: string = genExternalNullifier("test-epoch") const rlnIdentifier = RLN.genIdentifier() - const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, identityCommitment) + const merkleProof = generateMerkleProof(15, BigInt(0), leaves, identityCommitment) const witness = RLN.genWitness(secretHash, merkleProof, epoch, signal, rlnIdentifier) expect(typeof witness).toBe("object") @@ -52,7 +52,7 @@ describe("RLN", () => { const leaves = Object.assign([], identityCommitments) leaves.push(zeroIdCommitment) - const fun = () => generateMerkleProof(15, zeroIdCommitment, 2, leaves, zeroIdCommitment) + const fun = () => generateMerkleProof(15, zeroIdCommitment, leaves, zeroIdCommitment) expect(fun).toThrow("Can't generate a proof for a zero leaf") }) @@ -90,7 +90,7 @@ describe("RLN", () => { const epoch = genExternalNullifier("test-epoch") const rlnIdentifier = RLN.genIdentifier() - const merkleProof = generateMerkleProof(15, BigInt(0), 2, leaves, identityCommitment) + const merkleProof = generateMerkleProof(15, BigInt(0), leaves, identityCommitment) const witness = RLN.genWitness(secretHash, merkleProof, epoch, signal, rlnIdentifier) const vkeyPath = path.join(zkeyFiles, "rln", "verification_key.json") diff --git a/packages/protocols/tests/semaphore.test.ts b/packages/protocols/tests/semaphore.test.ts index a7b20ce..6a3ffa2 100644 --- a/packages/protocols/tests/semaphore.test.ts +++ b/packages/protocols/tests/semaphore.test.ts @@ -26,7 +26,7 @@ describe("Semaphore", () => { const leaves = [BigInt(3), BigInt(2), identityCommitment, BigInt(4)] const signal = "0x111" - const merkleProof = generateMerkleProof(20, BigInt(0), 2, leaves, identityCommitment) + const merkleProof = generateMerkleProof(20, BigInt(0), leaves, identityCommitment) const witness = Semaphore.genWitness( identity.getTrapdoor(), @@ -47,7 +47,7 @@ describe("Semaphore", () => { const leaves = [BigInt(3), BigInt(2), identityCommitment, BigInt(4)] const signal = "0x111" - const merkleProof = generateMerkleProof(20, BigInt(0), 2, leaves, identityCommitment) + const merkleProof = generateMerkleProof(20, BigInt(0), leaves, identityCommitment) const witness = Semaphore.genWitness( identity.getTrapdoor(),