Skip to content

Commit 7b137d2

Browse files
authored
Merge pull request #588 from semaphore-protocol/refactor/merkle-proof
Support `MerkleProof` parameter in the `generateProof` function Former-commit-id: 2b52ea3
2 parents 253344f + 34f40d4 commit 7b137d2

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/proof/src/generate-proof.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BigNumber } from "@ethersproject/bignumber"
22
import { BytesLike, Hexable } from "@ethersproject/bytes"
3-
import { Group } from "@semaphore-protocol/group"
3+
import type { Group, MerkleProof } from "@semaphore-protocol/group"
44
import type { Identity } from "@semaphore-protocol/identity"
55
import { NumericString, groth16 } from "snarkjs"
66
import getSnarkArtifacts from "./get-snark-artifacts.node"
@@ -11,7 +11,7 @@ import { SemaphoreProof, SnarkArtifacts } from "./types"
1111
/**
1212
* Generates a Semaphore proof.
1313
* @param identity The Semaphore identity.
14-
* @param group The Semaphore group or its Merkle proof.
14+
* @param groupOrMerkleProof The Semaphore group or its Merkle proof.
1515
* @param scope The external nullifier.
1616
* @param message The Semaphore signal.
1717
* @param merkleTreeDepth The depth of the tree with which the circuit was compiled.
@@ -20,14 +20,21 @@ import { SemaphoreProof, SnarkArtifacts } from "./types"
2020
*/
2121
export default async function generateProof(
2222
identity: Identity,
23-
group: Group,
23+
groupOrMerkleProof: Group | MerkleProof,
2424
message: BytesLike | Hexable | number | bigint,
2525
scope: BytesLike | Hexable | number | bigint,
2626
merkleTreeDepth?: number,
2727
snarkArtifacts?: SnarkArtifacts
2828
): Promise<SemaphoreProof> {
29-
const leafIndex = group.indexOf(identity.commitment)
30-
const merkleProof = group.generateMerkleProof(leafIndex)
29+
let merkleProof
30+
31+
if ("siblings" in groupOrMerkleProof) {
32+
merkleProof = groupOrMerkleProof
33+
} else {
34+
const leafIndex = groupOrMerkleProof.indexOf(identity.commitment)
35+
merkleProof = groupOrMerkleProof.generateMerkleProof(leafIndex)
36+
}
37+
3138
const merkleProofLength = merkleProof.siblings.length
3239

3340
if (merkleTreeDepth !== undefined) {

packages/proof/tests/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ describe("Proof", () => {
4848
expect(typeof fullProof).toBe("object")
4949
expect(fullProof.merkleTreeRoot).toBe(group.root)
5050
}, 20000)
51+
52+
it("Should generate a Semaphore proof passing a Merkle proof instead of a group", async () => {
53+
const group = new Group([BigInt(1), BigInt(2), identity.commitment])
54+
55+
fullProof = await generateProof(identity, group.generateMerkleProof(2), message, scope, treeDepth)
56+
57+
expect(typeof fullProof).toBe("object")
58+
expect(fullProof.merkleTreeRoot).toBe(group.root)
59+
}, 20000)
5160
})
5261

5362
describe("# verifyProof", () => {

0 commit comments

Comments
 (0)