Skip to content

Commit 88004d9

Browse files
authored
fix: under constrained bug binary merkle root (#1012)
* Fix/update binary merkle root circuit version (#1000) * fix(circuits)!: update binary merkle root circuit version re #999 * chore: update snarkjs version from 0.7.4 to 0.7.5 * refactor: update the proof package and smart contracts (#1011) refactor: update the proof package and smart contracts with new zk artifacts
1 parent e7ca1d9 commit 88004d9

File tree

13 files changed

+1108
-990
lines changed

13 files changed

+1108
-990
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"prettier": "^3.2.5",
8181
"rimraf": "^5.0.5",
8282
"semver": "^7.6.2",
83-
"snarkjs": "0.7.4",
83+
"snarkjs": "0.7.5",
8484
"ts-jest": "^29.1.2",
8585
"ts-node": "^10.9.2",
8686
"tslib": "^2.6.2",

packages/circuits/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
"access": "public"
2525
},
2626
"dependencies": {
27-
"@zk-kit/binary-merkle-root.circom": "1.0.0",
27+
"@zk-kit/binary-merkle-root.circom": "2.0.0",
2828
"circomlib": "2.0.5"
2929
},
3030
"devDependencies": {
3131
"@semaphore-protocol/core": "workspace:^",
3232
"@types/mocha": "^10.0.6",
3333
"@zk-kit/baby-jubjub": "1.0.3",
34-
"circomkit": "0.0.19",
34+
"circomkit": "0.3.3",
3535
"mocha": "^10.2.0",
3636
"poseidon-lite": "^0.3.0"
3737
}

packages/circuits/src/semaphore.circom

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ template Semaphore(MAX_DEPTH) {
2727
// See the Semaphore identity package to know more about how the identity is generated:
2828
// https://github.com/semaphore-protocol/semaphore/tree/main/packages/identity.
2929
signal input secret;
30-
signal input merkleProofLength, merkleProofIndices[MAX_DEPTH], merkleProofSiblings[MAX_DEPTH];
30+
signal input merkleProofLength, merkleProofIndex, merkleProofSiblings[MAX_DEPTH];
3131
signal input message;
3232
signal input scope;
3333

@@ -58,7 +58,7 @@ template Semaphore(MAX_DEPTH) {
5858
// the circuit through the inputs of the Merkle proof.
5959
// See https://github.com/privacy-scaling-explorations/zk-kit.circom/blob/main/packages/binary-merkle-root/src/binary-merkle-root.circom
6060
// to know more about how the 'BinaryMerkleRoot' template works.
61-
merkleRoot <== BinaryMerkleRoot(MAX_DEPTH)(identityCommitment, merkleProofLength, merkleProofIndices, merkleProofSiblings);
61+
merkleRoot <== BinaryMerkleRoot(MAX_DEPTH)(identityCommitment, merkleProofLength, merkleProofIndex, merkleProofSiblings);
6262

6363
// Nullifier generation.
6464
// The nullifier is a value that essentially identifies the proof generated in a specific scope

packages/circuits/tests/common.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@ export const circomkit = new Circomkit({
1212
})
1313

1414
export function generateMerkleProof(group: Group, _index: number, maxDepth: number) {
15-
const { siblings: merkleProofSiblings, index } = group.generateMerkleProof(_index)
15+
const { siblings: merkleProofSiblings, index: merkleProofIndex } = group.generateMerkleProof(_index)
1616

17-
// The index must be converted to a list of indices, 1 for each tree level.
18-
// The circuit tree depth is 20, so the number of siblings must be 20, even if
19-
// the tree depth is actually 3. The missing siblings can be set to 0, as they
20-
// won't be used to calculate the root in the circuit.
21-
const merkleProofIndices: number[] = []
17+
// For example, if the circuit expects a Merkle tree of depth 20,
18+
// the input must always include 20 sibling nodes, even if the actual
19+
// tree depth is smaller (e.g., 3). The unused sibling positions can be
20+
// filled with 0, as they won't affect the root calculation in the circuit.
2221

2322
for (let i = 0; i < maxDepth; i += 1) {
24-
merkleProofIndices.push((index >> i) & 1)
25-
2623
if (merkleProofSiblings[i] === undefined) {
2724
merkleProofSiblings[i] = BigInt(0)
2825
}
2926
}
3027

31-
return { merkleProofSiblings, merkleProofIndices }
28+
return { merkleProofSiblings, merkleProofIndex }
3229
}

packages/circuits/tests/semaphore.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const r = 2188824287183927522224640574525727508854836440041603434369820418657580
1212

1313
describe("semaphore", () => {
1414
let circuit: WitnessTester<
15-
["secret", "merkleProofLength", "merkleProofIndices", "merkleProofSiblings", "scope", "message"],
15+
["secret", "merkleProofLength", "merkleProofIndex", "merkleProofSiblings", "scope", "message"],
1616
["nullifier", "merkleRoot"]
1717
>
1818

@@ -36,12 +36,12 @@ describe("semaphore", () => {
3636

3737
const group = new Group([commitment, 2n, 3n])
3838

39-
const { merkleProofSiblings, merkleProofIndices } = generateMerkleProof(group, 0, MAX_DEPTH)
39+
const { merkleProofSiblings, merkleProofIndex } = generateMerkleProof(group, 0, MAX_DEPTH)
4040

4141
const INPUT = {
4242
secret,
4343
merkleProofLength: group.depth,
44-
merkleProofIndices,
44+
merkleProofIndex,
4545
merkleProofSiblings,
4646
scope,
4747
message
@@ -61,12 +61,12 @@ describe("semaphore", () => {
6161
const commitment = poseidon2(mulPointEscalar(Base8, secret))
6262
const group = new Group([commitment, 2n, 3n])
6363

64-
const { merkleProofSiblings, merkleProofIndices } = generateMerkleProof(group, 0, MAX_DEPTH)
64+
const { merkleProofSiblings, merkleProofIndex } = generateMerkleProof(group, 0, MAX_DEPTH)
6565

6666
const INPUT = {
6767
secret,
6868
merkleProofLength: group.depth,
69-
merkleProofIndices,
69+
merkleProofIndex,
7070
merkleProofSiblings,
7171
scope,
7272
message
@@ -81,12 +81,12 @@ describe("semaphore", () => {
8181
const commitment = poseidon2(mulPointEscalar(Base8, secret))
8282
const group = new Group([commitment, 2n, 3n])
8383

84-
const { merkleProofSiblings, merkleProofIndices } = generateMerkleProof(group, 0, MAX_DEPTH)
84+
const { merkleProofSiblings, merkleProofIndex } = generateMerkleProof(group, 0, MAX_DEPTH)
8585

8686
const INPUT = {
8787
secret,
8888
merkleProofLength: group.depth,
89-
merkleProofIndices,
89+
merkleProofIndex,
9090
merkleProofSiblings,
9191
scope,
9292
message
@@ -100,12 +100,12 @@ describe("semaphore", () => {
100100

101101
const group = new Group([commitment, 2n, 3n])
102102

103-
const { merkleProofSiblings, merkleProofIndices } = generateMerkleProof(group, 0, MAX_DEPTH)
103+
const { merkleProofSiblings, merkleProofIndex } = generateMerkleProof(group, 0, MAX_DEPTH)
104104

105105
const INPUT = {
106106
secret,
107107
merkleProofLength: group.depth,
108-
merkleProofIndices,
108+
merkleProofIndex,
109109
merkleProofSiblings,
110110
scope,
111111
message

packages/contracts/contracts/base/Constants.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
pragma solidity >=0.8.23 <=0.8.28;
2+
pragma solidity >=0.8.23 <0.9.0;
33

44
/// @dev Minimum supported tree depth.
55
uint8 constant MIN_DEPTH = 1;

packages/contracts/contracts/base/SemaphoreGroups.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//SPDX-License-Identifier: MIT
2-
pragma solidity >=0.8.23 <=0.8.28;
2+
pragma solidity >=0.8.23 <0.9.0;
33

44
import {ISemaphoreGroups} from "../interfaces/ISemaphoreGroups.sol";
55
import {InternalLeanIMT, LeanIMTData} from "@zk-kit/lean-imt.sol/InternalLeanIMT.sol";

packages/contracts/contracts/base/SemaphoreVerifier.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
// Part of this file was generated with [snarkJS](https://github.com/iden3/snarkjs).
33

4-
pragma solidity >=0.8.23 <=0.8.28;
4+
pragma solidity >=0.8.23 <0.9.0;
55

66
import {MAX_DEPTH} from "./Constants.sol";
77
import {SemaphoreVerifierKeyPts} from "./SemaphoreVerifierKeyPts.sol";

0 commit comments

Comments
 (0)