From 5b514c080e3594ebfc7f99d2df66b2af7b0c0b8a Mon Sep 17 00:00:00 2001 From: cedoor Date: Mon, 14 Feb 2022 18:17:14 +0100 Subject: [PATCH] feat: set binary tree for semaphore protocol Former-commit-id: 4470df6ff258e21411bfcc74d3388a0398f3f46a [formerly 378f35fd4813b389ae7c6b90b248c5154e704868] [formerly f79a8fe36217321f8b02b7b6786bb5413a9b1a50 [formerly 7213b343ffb281c8e81d447a179009bdb04b3c54]] [formerly 695155b5c1e4758f36af45097f2cf521c3f1c854 [formerly 2b70d23f1ffd70ff28ba9ac214c113534c92e77e] [formerly 1b46c2fd8ba488de78eaa68abdf125021ad3e6d1 [formerly f5411edad3b82ca46f88109cc43a0f50d8b0f73f]]] Former-commit-id: d968886745f403b5415d0738310e56b9e0cf38c9 [formerly 02f33b46e896683807cebe621a429c8b244a7a0d] [formerly 7d3098a31babe73e1206219582b759e79beb4989 [formerly bf80a5fe52804b657594a35b34b0eb3d17fe5cee]] Former-commit-id: 6084776c23b2e0f82d3d49613bed6334674339f3 [formerly 2b0a41a2d5792ec35d7248ada6a0e175025f9584] Former-commit-id: b052d15edbcbe7d2dc5dec46ac2153ff73ae3af3 --- packages/protocols/README.md | 2 +- packages/protocols/src/semaphore.ts | 12 ++++++------ packages/protocols/src/types/index.ts | 12 ++++++------ packages/protocols/src/utils.ts | 9 ++++++++- packages/protocols/tests/semaphore.test.ts | 4 ++-- packages/protocols/zkeyFiles.zip.REMOVED.git-id | 2 +- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/packages/protocols/README.md b/packages/protocols/README.md index bfa3110..4a0a440 100644 --- a/packages/protocols/README.md +++ b/packages/protocols/README.md @@ -66,7 +66,7 @@ import { generateMerkleProof } from "@zk-kit/protocols" const depth = 20 const zeroValue = BigInt(0) -const arity = 5 +const arity = 2 const identity = new ZkIdentity() const identityCommitment = identity.genIdentityCommitment() const identityCommitments = [BigInt(1), identityCommitment, BigInt(2)] diff --git a/packages/protocols/src/semaphore.ts b/packages/protocols/src/semaphore.ts index d49d9f3..03e6bd7 100644 --- a/packages/protocols/src/semaphore.ts +++ b/packages/protocols/src/semaphore.ts @@ -24,12 +24,12 @@ export default class Semaphore extends ZkProtocol { shouldHash = true ): SemaphoreWitness { return { - identity_nullifier: identityNullifier, - identity_trapdoor: identityTrapdoor, - identity_path_index: merkleProof.pathIndices, - path_elements: merkleProof.siblings, - external_nullifier: externalNullifier, - signal_hash: shouldHash ? genSignalHash(signal) : signal + identityNullifier, + identityTrapdoor, + treePathIndices: merkleProof.pathIndices, + treeSiblings: merkleProof.siblings, + externalNullifier, + signalHash: shouldHash ? genSignalHash(signal) : signal } } diff --git a/packages/protocols/src/types/index.ts b/packages/protocols/src/types/index.ts index cedefa7..2d75845 100644 --- a/packages/protocols/src/types/index.ts +++ b/packages/protocols/src/types/index.ts @@ -16,10 +16,10 @@ export type FullProof = { export type SolidityProof = StrBigInt[] export type SemaphoreWitness = { - identity_nullifier: StrBigInt - identity_trapdoor: StrBigInt - path_elements: StrBigInt[] - identity_path_index: number[] - external_nullifier: StrBigInt - signal_hash: StrBigInt + identityNullifier: StrBigInt + identityTrapdoor: StrBigInt + treeSiblings: StrBigInt[] + treePathIndices: number[] + externalNullifier: StrBigInt + signalHash: StrBigInt } diff --git a/packages/protocols/src/utils.ts b/packages/protocols/src/utils.ts index 3b63c83..2ff9f6d 100644 --- a/packages/protocols/src/utils.ts +++ b/packages/protocols/src/utils.ts @@ -72,5 +72,12 @@ export function generateMerkleProof( throw new Error("The leaf does not exists") } - return tree.createProof(leafIndex) + 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]) + } + + return merkleProof } diff --git a/packages/protocols/tests/semaphore.test.ts b/packages/protocols/tests/semaphore.test.ts index 373a6dc..29f27ea 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), 5, leaves, identityCommitment) + const merkleProof = generateMerkleProof(20, BigInt(0), 2, 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), 5, leaves, identityCommitment) + const merkleProof = generateMerkleProof(20, BigInt(0), 2, leaves, identityCommitment) const witness = Semaphore.genWitness( identity.getTrapdoor(), diff --git a/packages/protocols/zkeyFiles.zip.REMOVED.git-id b/packages/protocols/zkeyFiles.zip.REMOVED.git-id index a3a7d2f..e8f0da3 100644 --- a/packages/protocols/zkeyFiles.zip.REMOVED.git-id +++ b/packages/protocols/zkeyFiles.zip.REMOVED.git-id @@ -1 +1 @@ -2daec67f953ad6155f56d4c0c16ac1b67eb97a94 \ No newline at end of file +83a3802ca5e94bb251704b767deab0b7202c3ecd \ No newline at end of file