@maci-protocol/circuits 0.0.0-ci.a128a72 → 0.0.0-ci.a577366

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,12 @@ include "../../utils/hashers.circom";
9
9
  include "../../utils/messageToCommand.circom";
10
10
  include "../../utils/privToPubKey.circom";
11
11
  include "../../utils/non-qv/stateLeafAndBallotTransformer.circom";
12
- include "../../utils/trees/incrementalMerkleTree.circom";
12
+ // Merkle tree utilities (split from incrementalMerkleTree.circom)
13
+ include "../../utils/trees/MerkleTreeInclusionProof.circom";
14
+ include "../../utils/trees/LeafExists.circom";
15
+ include "../../utils/trees/CheckRoot.circom";
16
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
17
+ include "../../utils/trees/BinaryMerkleRoot.circom";
13
18
  include "../../utils/trees/incrementalQuinaryTree.circom";
14
19
 
15
20
  /**
@@ -441,3 +446,4 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
441
446
 
442
447
  newBallotRoot <== computedNewBallotQip;
443
448
  }
449
+
@@ -5,7 +5,9 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
- include "../../utils/trees/incrementalMerkleTree.circom";
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
10
+ include "../../utils/trees/LeafExists.circom";
9
11
  include "../../utils/trees/incrementalQuinaryTree.circom";
10
12
  include "../../utils/calculateTotal.circom";
11
13
  include "../../utils/hashers.circom";
@@ -230,3 +232,4 @@ template TallyVotesNonQv(
230
232
 
231
233
  computedNewTallyCommitment === newTallyCommitment;
232
234
  }
235
+
@@ -10,7 +10,12 @@ include "../../utils/messageToCommand.circom";
10
10
  include "../../utils/privToPubKey.circom";
11
11
  include "../../utils/qv/stateLeafAndBallotTransformer.circom";
12
12
  include "../../utils/trees/incrementalQuinaryTree.circom";
13
- include "../../utils/trees/incrementalMerkleTree.circom";
13
+ // Merkle tree utilities (split from incrementalMerkleTree.circom)
14
+ include "../../utils/trees/MerkleTreeInclusionProof.circom";
15
+ include "../../utils/trees/LeafExists.circom";
16
+ include "../../utils/trees/CheckRoot.circom";
17
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
18
+ include "../../utils/trees/BinaryMerkleRoot.circom";
14
19
 
15
20
  /**
16
21
  * Proves the correctness of processing a batch of MACI messages.
@@ -443,3 +448,4 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
443
448
 
444
449
  newBallotRoot <== computedNewBallotQip;
445
450
  }
451
+
@@ -5,7 +5,9 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
- include "../../utils/trees/incrementalMerkleTree.circom";
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
10
+ include "../../utils/trees/LeafExists.circom";
9
11
  include "../../utils/trees/incrementalQuinaryTree.circom";
10
12
  include "../../utils/calculateTotal.circom";
11
13
  include "../../utils/hashers.circom";
@@ -0,0 +1,54 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./mux1.circom";
5
+ include "./comparators.circom";
6
+ // local import
7
+ include "../hashers.circom";
8
+
9
+ // @note taken from @zk-kit/circuits
10
+ // if used directly in processMessages circom complains about duplicated
11
+ // templates (Ark, Poseidon, etc.)
12
+ // This circuit is designed to calculate the root of a binary Merkle
13
+ // tree given a leaf, its depth, and the necessary sibling
14
+ // information (aka proof of membership).
15
+ // A circuit is designed without the capability to iterate through
16
+ // a dynamic array. To address this, a parameter with the static maximum
17
+ // tree depth is defined (i.e. 'MAX_DEPTH'). And additionally, the circuit
18
+ // receives a dynamic depth as an input, which is utilized in calculating the
19
+ // true root of the Merkle tree. The actual depth of the Merkle tree
20
+ // may be equal to or less than the static maximum depth.
21
+ // NOTE: This circuit will successfully verify `out = 0` for `depth > MAX_DEPTH`.
22
+ // Make sure to enforce `depth <= MAX_DEPTH` outside the circuit.
23
+ template BinaryMerkleRoot(MAX_DEPTH) {
24
+ signal input leaf, depth, indices[MAX_DEPTH], siblings[MAX_DEPTH][1];
25
+
26
+ signal output out;
27
+
28
+ signal nodes[MAX_DEPTH + 1];
29
+ nodes[0] <== leaf;
30
+
31
+ signal roots[MAX_DEPTH];
32
+ var root = 0;
33
+
34
+ for (var i = 0; i < MAX_DEPTH; i++) {
35
+ var isDepth = IsEqual()([depth, i]);
36
+
37
+ roots[i] <== isDepth * nodes[i];
38
+
39
+ root += roots[i];
40
+
41
+ var c[2][2] = [
42
+ [nodes[i], siblings[i][0]],
43
+ [siblings[i][0], nodes[i]]
44
+ ];
45
+
46
+ var childNodes[2] = MultiMux1(2)(c, indices[i]);
47
+
48
+ nodes[i + 1] <== PoseidonHasher(2)(childNodes);
49
+ }
50
+
51
+ var isDepth = IsEqual()([depth, MAX_DEPTH]);
52
+
53
+ out <== root + isDepth * nodes[MAX_DEPTH];
54
+ }
@@ -0,0 +1,45 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // local import
4
+ include "../hashers.circom";
5
+
6
+ /**
7
+ * Verifies the correct construction of a Merkle tree from a set of leaves.
8
+ * Given a Merkle root and a list of leaves, check if the root is the
9
+ * correct result of inserting all the leaves into the tree (in the given order).
10
+ */
11
+ template CheckRoot(levels) {
12
+ // The total number of leaves in the Merkle tree, calculated as 2 to the power of `levels`.
13
+ var totalLeaves = 2 ** levels;
14
+ // The number of first-level hashers needed, equal to half the total leaves, as each hasher combines two leaves.
15
+ var numLeafHashers = totalLeaves / 2;
16
+ // The number of intermediate hashers, one less than the number of leaf hashers,
17
+ // as each level of hashing reduces the number of hash elements by about half.
18
+ var numIntermediateHashers = numLeafHashers - 1;
19
+
20
+ // Array of leaf values input to the circuit.
21
+ signal input leaves[totalLeaves];
22
+
23
+ // Output signal for the Merkle root that results from hashing all the input leaves.
24
+ signal output root;
25
+
26
+ // Total number of hashers used in constructing the tree, one less than the total number of leaves,
27
+ // since each level of the tree combines two elements into one.
28
+ var numHashers = totalLeaves - 1;
29
+ var computedLevelHashers[numHashers];
30
+
31
+ // Initialize hashers for the leaves, each taking two adjacent leaves as inputs.
32
+ for (var i = 0; i < numLeafHashers; i++){
33
+ computedLevelHashers[i] = PoseidonHasher(2)([leaves[i*2], leaves[i*2+1]]);
34
+ }
35
+
36
+ // Initialize hashers for intermediate levels, each taking the outputs of two hashers from the previous level.
37
+ var k = 0;
38
+ for (var i = numLeafHashers; i < numLeafHashers + numIntermediateHashers; i++) {
39
+ computedLevelHashers[i] = PoseidonHasher(2)([computedLevelHashers[k*2], computedLevelHashers[k*2+1]]);
40
+ k++;
41
+ }
42
+
43
+ // Connect the output of the final hasher in the array to the root output signal.
44
+ root <== computedLevelHashers[numHashers-1];
45
+ }
@@ -0,0 +1,27 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // local import
4
+ include "./MerkleTreeInclusionProof.circom";
5
+
6
+ /**
7
+ * Ensures that a leaf exists within a Merkle tree with a given root.
8
+ */
9
+ template LeafExists(levels){
10
+ // The leaf whose existence within the tree is being verified.
11
+ signal input leaf;
12
+
13
+ // The elements along the path needed for the inclusion proof.
14
+ signal input path_elements[levels][1];
15
+ // The indices indicating the path taken through the tree for the leaf.
16
+ signal input path_index[levels];
17
+ // The root of the Merkle tree, against which the inclusion is verified.
18
+ signal input root;
19
+
20
+ var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
21
+ leaf,
22
+ path_index,
23
+ path_elements
24
+ );
25
+
26
+ root === computedMerkleRoot;
27
+ }
@@ -0,0 +1,40 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit imports
4
+ include "./safe-comparators.circom";
5
+ // local import
6
+ include "../calculateTotal.circom";
7
+
8
+ /**
9
+ * Calculates the path indices required for Merkle proof verifications.
10
+ * Given a node index within an IMT and the total tree levels, it outputs the path indices leading to that node.
11
+ * The template handles the modulo and division operations to break down the tree index into its constituent path indices.
12
+ */
13
+ template MerkleGeneratePathIndices(levels) {
14
+ var BASE = 2;
15
+
16
+ signal input in;
17
+ signal output out[levels];
18
+
19
+ var m = in;
20
+ var computedResults[levels];
21
+
22
+ for (var i = 0; i < levels; i++) {
23
+ // circom's best practices suggests to avoid using <-- unless you
24
+ // are aware of what's going on. This is the only way to do modulo operation.
25
+ out[i] <-- m % BASE;
26
+ m = m \ BASE;
27
+
28
+ // Check that each output element is less than the base.
29
+ var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
30
+ computedIsOutputElementLessThanBase === 1;
31
+
32
+ // Re-compute the total sum.
33
+ computedResults[i] = out[i] * (BASE ** i);
34
+ }
35
+
36
+ // Check that the total sum matches the index.
37
+ var computedCalculateTotal = CalculateTotal(levels)(computedResults);
38
+
39
+ computedCalculateTotal === in;
40
+ }
@@ -0,0 +1,49 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./mux1.circom";
5
+ // local import
6
+ include "../hashers.circom";
7
+
8
+ /**
9
+ * Recomputes a Merkle root from a given leaf and its path in a Merkle tree.
10
+ */
11
+ template MerkleTreeInclusionProof(n_levels) {
12
+ // The leaf node from which the Merkle root is calculated.
13
+ signal input leaf;
14
+ // Indices indicating left or right child for each level of the tree.
15
+ signal input path_index[n_levels];
16
+ // Sibling node values required to compute the hash at each level.
17
+ signal input path_elements[n_levels][1];
18
+
19
+ signal output root;
20
+
21
+ // Stores the hash at each level starting from the leaf to the root.
22
+ signal levelHashes[n_levels + 1];
23
+ // Initialize the first level with the given leaf.
24
+ levelHashes[0] <== leaf;
25
+
26
+ for (var i = 0; i < n_levels; i++) {
27
+ // Validate path_index to be either 0 or 1, ensuring no other values.
28
+ path_index[i] * (1 - path_index[i]) === 0;
29
+
30
+ // Configure the multiplexer based on the path index for the current level.
31
+ var c[2][2] = [
32
+ [levelHashes[i], path_elements[i][0]],
33
+ [path_elements[i][0], levelHashes[i]]
34
+ ];
35
+
36
+ var mux[2] = MultiMux1(2)(
37
+ c,
38
+ path_index[i]
39
+ );
40
+
41
+ var computedLevelHash = PoseidonHasher(2)([mux[0], mux[1]]);
42
+
43
+ // Store the resulting hash as the next level's hash.
44
+ levelHashes[i + 1] <== computedLevelHash;
45
+ }
46
+
47
+ // Set the final level hash as the root.
48
+ root <== levelHashes[n_levels];
49
+ }
@@ -3,7 +3,7 @@ pragma circom 2.0.0;
3
3
  // local imports
4
4
  include "../utils/hashers.circom";
5
5
  include "../utils/privToPubKey.circom";
6
- include "../utils/trees/incrementalMerkleTree.circom";
6
+ include "../utils/trees/BinaryMerkleRoot.circom";
7
7
 
8
8
  // Poll Joining Circuit
9
9
  // Allows a user to prove knowledge of a private key that is signed up to
@@ -89,3 +89,4 @@ template PollJoined(stateTreeDepth) {
89
89
 
90
90
  stateLeafQip === stateRoot;
91
91
  }
92
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maci-protocol/circuits",
3
- "version": "0.0.0-ci.a128a72",
3
+ "version": "0.0.0-ci.a577366",
4
4
  "private": false,
5
5
  "description": "zk-SNARK circuits for MACI",
6
6
  "main": "build/ts/index.js",
@@ -43,10 +43,10 @@
43
43
  "test:pollJoined": "pnpm run mocha-test ts/__tests__/PollJoined.test.ts"
44
44
  },
45
45
  "dependencies": {
46
- "@maci-protocol/core": "0.0.0-ci.a128a72",
47
- "@maci-protocol/crypto": "0.0.0-ci.a128a72",
48
- "@maci-protocol/domainobjs": "0.0.0-ci.a128a72",
49
- "@maci-protocol/sdk": "0.0.0-ci.a128a72",
46
+ "@maci-protocol/core": "0.0.0-ci.a577366",
47
+ "@maci-protocol/crypto": "0.0.0-ci.a577366",
48
+ "@maci-protocol/domainobjs": "0.0.0-ci.a577366",
49
+ "@maci-protocol/sdk": "0.0.0-ci.a577366",
50
50
  "@zk-kit/circuits": "^0.4.0",
51
51
  "circomkit": "^0.3.2",
52
52
  "circomlib": "^2.0.5"
@@ -60,12 +60,12 @@
60
60
  "@zk-kit/baby-jubjub": "^1.0.3",
61
61
  "chai": "^4.3.10",
62
62
  "chai-as-promised": "^7.1.2",
63
- "fast-check": "^4.1.0",
63
+ "fast-check": "^4.1.1",
64
64
  "glob": "^11.0.1",
65
65
  "mocha": "^11.1.0",
66
66
  "ts-mocha": "^11.1.0",
67
67
  "ts-node": "^10.9.1",
68
68
  "typescript": "^5.8.3"
69
69
  },
70
- "gitHead": "45c2c0f83bf3646e41632e7ab3cd283af898cc85"
70
+ "gitHead": "1cfa661921cd66314655e5ee707159dba59b5838"
71
71
  }
@@ -1,198 +0,0 @@
1
- pragma circom 2.0.0;
2
-
3
- // zk-kit imports
4
- include "./safe-comparators.circom";
5
- // circomlib import
6
- include "./mux1.circom";
7
- include "./comparators.circom";
8
- // local import
9
- include "../hashers.circom";
10
- include "../calculateTotal.circom";
11
-
12
- /**
13
- * Recomputes a Merkle root from a given leaf and its path in a Merkle tree.
14
- */
15
- template MerkleTreeInclusionProof(n_levels) {
16
- // The leaf node from which the Merkle root is calculated.
17
- signal input leaf;
18
- // Indices indicating left or right child for each level of the tree.
19
- signal input path_index[n_levels];
20
- // Sibling node values required to compute the hash at each level.
21
- signal input path_elements[n_levels][1];
22
-
23
- signal output root;
24
-
25
- // Stores the hash at each level starting from the leaf to the root.
26
- signal levelHashes[n_levels + 1];
27
- // Initialize the first level with the given leaf.
28
- levelHashes[0] <== leaf;
29
-
30
- for (var i = 0; i < n_levels; i++) {
31
- // Validate path_index to be either 0 or 1, ensuring no other values.
32
- path_index[i] * (1 - path_index[i]) === 0;
33
-
34
- // Configure the multiplexer based on the path index for the current level.
35
- var c[2][2] = [
36
- [levelHashes[i], path_elements[i][0]],
37
- [path_elements[i][0], levelHashes[i]]
38
- ];
39
-
40
- var mux[2] = MultiMux1(2)(
41
- c,
42
- path_index[i]
43
- );
44
-
45
- var computedLevelHash = PoseidonHasher(2)([mux[0], mux[1]]);
46
-
47
- // Store the resulting hash as the next level's hash.
48
- levelHashes[i + 1] <== computedLevelHash;
49
- }
50
-
51
- // Set the final level hash as the root.
52
- root <== levelHashes[n_levels];
53
- }
54
-
55
- /**
56
- * Ensures that a leaf exists within a Merkle tree with a given root.
57
- */
58
- template LeafExists(levels){
59
- // The leaf whose existence within the tree is being verified.
60
- signal input leaf;
61
-
62
- // The elements along the path needed for the inclusion proof.
63
- signal input path_elements[levels][1];
64
- // The indices indicating the path taken through the tree for the leaf.
65
- signal input path_index[levels];
66
- // The root of the Merkle tree, against which the inclusion is verified.
67
- signal input root;
68
-
69
- var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
70
- leaf,
71
- path_index,
72
- path_elements
73
- );
74
-
75
- root === computedMerkleRoot;
76
- }
77
-
78
- /**
79
- * Verifies the correct construction of a Merkle tree from a set of leaves.
80
- * Given a Merkle root and a list of leaves, check if the root is the
81
- * correct result of inserting all the leaves into the tree (in the given order).
82
- */
83
- template CheckRoot(levels) {
84
- // The total number of leaves in the Merkle tree, calculated as 2 to the power of `levels`.
85
- var totalLeaves = 2 ** levels;
86
- // The number of first-level hashers needed, equal to half the total leaves, as each hasher combines two leaves.
87
- var numLeafHashers = totalLeaves / 2;
88
- // The number of intermediate hashers, one less than the number of leaf hashers,
89
- // as each level of hashing reduces the number of hash elements by about half.
90
- var numIntermediateHashers = numLeafHashers - 1;
91
-
92
- // Array of leaf values input to the circuit.
93
- signal input leaves[totalLeaves];
94
-
95
- // Output signal for the Merkle root that results from hashing all the input leaves.
96
- signal output root;
97
-
98
- // Total number of hashers used in constructing the tree, one less than the total number of leaves,
99
- // since each level of the tree combines two elements into one.
100
- var numHashers = totalLeaves - 1;
101
- var computedLevelHashers[numHashers];
102
-
103
- // Initialize hashers for the leaves, each taking two adjacent leaves as inputs.
104
- for (var i = 0; i < numLeafHashers; i++){
105
- computedLevelHashers[i] = PoseidonHasher(2)([leaves[i*2], leaves[i*2+1]]);
106
- }
107
-
108
- // Initialize hashers for intermediate levels, each taking the outputs of two hashers from the previous level.
109
- var k = 0;
110
- for (var i = numLeafHashers; i < numLeafHashers + numIntermediateHashers; i++) {
111
- computedLevelHashers[i] = PoseidonHasher(2)([computedLevelHashers[k*2], computedLevelHashers[k*2+1]]);
112
- k++;
113
- }
114
-
115
- // Connect the output of the final hasher in the array to the root output signal.
116
- root <== computedLevelHashers[numHashers-1];
117
- }
118
-
119
- /**
120
- * Calculates the path indices required for Merkle proof verifications.
121
- * Given a node index within an IMT and the total tree levels, it outputs the path indices leading to that node.
122
- * The template handles the modulo and division operations to break down the tree index into its constituent path indices.
123
- */
124
- template MerkleGeneratePathIndices(levels) {
125
- var BASE = 2;
126
-
127
- signal input in;
128
- signal output out[levels];
129
-
130
- var m = in;
131
- var computedResults[levels];
132
-
133
- for (var i = 0; i < levels; i++) {
134
- // circom's best practices suggests to avoid using <-- unless you
135
- // are aware of what's going on. This is the only way to do modulo operation.
136
- out[i] <-- m % BASE;
137
- m = m \ BASE;
138
-
139
- // Check that each output element is less than the base.
140
- var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
141
- computedIsOutputElementLessThanBase === 1;
142
-
143
- // Re-compute the total sum.
144
- computedResults[i] = out[i] * (BASE ** i);
145
- }
146
-
147
- // Check that the total sum matches the index.
148
- var computedCalculateTotal = CalculateTotal(levels)(computedResults);
149
-
150
- computedCalculateTotal === in;
151
- }
152
-
153
- // @note taken from @zk-kit/circuits
154
- // if used directly in processMessages circom complains about duplicated
155
- // templates (Ark, Poseidon, etc.)
156
- // This circuit is designed to calculate the root of a binary Merkle
157
- // tree given a leaf, its depth, and the necessary sibling
158
- // information (aka proof of membership).
159
- // A circuit is designed without the capability to iterate through
160
- // a dynamic array. To address this, a parameter with the static maximum
161
- // tree depth is defined (i.e. 'MAX_DEPTH'). And additionally, the circuit
162
- // receives a dynamic depth as an input, which is utilized in calculating the
163
- // true root of the Merkle tree. The actual depth of the Merkle tree
164
- // may be equal to or less than the static maximum depth.
165
- // NOTE: This circuit will successfully verify `out = 0` for `depth > MAX_DEPTH`.
166
- // Make sure to enforce `depth <= MAX_DEPTH` outside the circuit.
167
- template BinaryMerkleRoot(MAX_DEPTH) {
168
- signal input leaf, depth, indices[MAX_DEPTH], siblings[MAX_DEPTH][1];
169
-
170
- signal output out;
171
-
172
- signal nodes[MAX_DEPTH + 1];
173
- nodes[0] <== leaf;
174
-
175
- signal roots[MAX_DEPTH];
176
- var root = 0;
177
-
178
- for (var i = 0; i < MAX_DEPTH; i++) {
179
- var isDepth = IsEqual()([depth, i]);
180
-
181
- roots[i] <== isDepth * nodes[i];
182
-
183
- root += roots[i];
184
-
185
- var c[2][2] = [
186
- [nodes[i], siblings[i][0]],
187
- [siblings[i][0], nodes[i]]
188
- ];
189
-
190
- var childNodes[2] = MultiMux1(2)(c, indices[i]);
191
-
192
- nodes[i + 1] <== PoseidonHasher(2)(childNodes);
193
- }
194
-
195
- var isDepth = IsEqual()([depth, MAX_DEPTH]);
196
-
197
- out <== root + isDepth * nodes[MAX_DEPTH];
198
- }