From 7fd496c515bf33bbf21fc490c3f97a0bb91722ae Mon Sep 17 00:00:00 2001 From: cedoor Date: Fri, 25 Feb 2022 18:50:36 +0100 Subject: [PATCH] refactor: remove arity parameter from mt functions Former-commit-id: be4dc8a721b2859276a8f820b4261fa0e9817714 [formerly 9e29c730f8333e1d7f905706b3646b4986f5ac17] [formerly ba28450065e767a3ba0a9126009d63444460af70 [formerly 604ae430f3c97a3a74fae613a43c4ce20ee68f2c]] [formerly d7f8ec4f7f8d252aa1d3d4de71942575fcaeaf99 [formerly b777595daf418d6bfe886f90c3ba8623a6ed89df] [formerly 8e3f37319640acda556c80f7603c120eb98ac60e [formerly 8bc23d43acc80a7c920739ebbdbffdd12ff6366e]]] Former-commit-id: d3672ba986685174d7b56bd0691e26f31ee90068 [formerly 505f9d6c7bb5f61c35d1a6951a1dec75591633a7] [formerly 846ff76039920c91c9cf5924468058b73843e422 [formerly 45b05069d210b18e5ed6d5940074781725e14bdc]] Former-commit-id: c07ca2f8095f41993ca4726d58e4d1ab55402143 [formerly 37d28b5ba3597d38b6deb64b054f801e4922ea37] Former-commit-id: 24400d0825b6086db394e8a3b6fe5242c5b8b805 --- packages/protocols/README.md | 3 +-- packages/protocols/src/utils.ts | 19 ++++--------------- packages/protocols/tests/rln.test.ts | 6 +++--- packages/protocols/tests/semaphore.test.ts | 4 ++-- 4 files changed, 10 insertions(+), 22 deletions(-) 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(),