Skip to content

Commit b2da0a6

Browse files
authored
fix(proof): replace root public signal with actual root (#843)
The public signal of proof related to the Merkle root could obviously be different from what is expected to be the root of the group. Therefore, for the proof to be valid, it is necessary that the group root passed as a parameter matches the proof/circuit root output. re #842
1 parent 9329eed commit b2da0a6

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

packages/proof/src/generate-proof.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Group, MerkleProof } from "@semaphore-protocol/group"
22
import type { Identity } from "@semaphore-protocol/identity"
33
import { MAX_DEPTH, MIN_DEPTH } from "@semaphore-protocol/utils/constants"
4+
import { Project, maybeGetSnarkArtifacts, type SnarkArtifacts } from "@zk-kit/artifacts"
45
import { requireDefined, requireNumber, requireObject, requireTypes } from "@zk-kit/utils/error-handlers"
56
import { packGroth16Proof } from "@zk-kit/utils/proof-packing"
6-
import { maybeGetSnarkArtifacts, Project, type SnarkArtifacts } from "@zk-kit/artifacts"
77
import type { BigNumberish } from "ethers"
8-
import { type NumericString, groth16 } from "snarkjs"
8+
import { groth16, type NumericString } from "snarkjs"
99
import hash from "./hash"
1010
import toBigInt from "./to-bigint"
1111
import type { SemaphoreProof } from "./types"
@@ -118,7 +118,7 @@ export default async function generateProof(
118118

119119
return {
120120
merkleTreeDepth,
121-
merkleTreeRoot: publicSignals[0],
121+
merkleTreeRoot: merkleProof.root.toString(),
122122
nullifier: publicSignals[1],
123123
message: message.toString() as NumericString,
124124
scope: scope.toString() as NumericString,

packages/proof/tests/index.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("Proof", () => {
5555

5656
expect(typeof proof).toBe("object")
5757
expect(BigInt(proof.merkleTreeRoot)).toBe(group.root)
58-
}, 80000)
58+
})
5959

6060
it("Should generate a Semaphore proof passing a Merkle proof instead of a group", async () => {
6161
const group = new Group([1n, 2n, identity.commitment])
@@ -64,7 +64,7 @@ describe("Proof", () => {
6464

6565
expect(typeof proof).toBe("object")
6666
expect(BigInt(proof.merkleTreeRoot)).toBe(group.root)
67-
}, 80000)
67+
})
6868

6969
it("Should generate a Semaphore proof without passing the tree depth", async () => {
7070
const group = new Group([1n, 2n, identity.commitment])
@@ -73,7 +73,7 @@ describe("Proof", () => {
7373

7474
expect(typeof proof).toBe("object")
7575
expect(BigInt(proof.merkleTreeRoot)).toBe(group.root)
76-
}, 80000)
76+
})
7777

7878
it("Should throw an error because snarkArtifacts is not an object", async () => {
7979
const group = new Group([1n, 2n, identity.commitment])
@@ -103,14 +103,24 @@ describe("Proof", () => {
103103
await expect(fun).rejects.toThrow("tree depth must be")
104104
})
105105

106-
it("Should verify a Semaphore proof", async () => {
106+
it("Should return true if the proof is valid", async () => {
107107
const group = new Group([1n, 2n, identity.commitment])
108108

109109
const proof = await generateProof(identity, group, message, scope, treeDepth)
110110

111111
const response = await verifyProof(proof)
112112

113113
expect(response).toBe(true)
114-
}, 80_000)
114+
})
115+
116+
it("Should return false if the proof is not valid", async () => {
117+
const group = new Group([1n, 2n, identity.commitment])
118+
119+
const proof = await generateProof(identity, group.generateMerkleProof(0), message, scope, treeDepth)
120+
121+
const response = await verifyProof(proof)
122+
123+
expect(response).toBe(false)
124+
})
115125
})
116126
})

0 commit comments

Comments
 (0)