@maci-protocol/circuits 0.0.0-ci.c74cc8b → 0.0.0-ci.cf26211
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.
- package/build/ts/types.d.ts +0 -1
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/coordinator/non-qv/processMessages.circom +12 -10
- package/circom/coordinator/non-qv/tallyVotes.circom +4 -1
- package/circom/coordinator/qv/processMessages.circom +12 -10
- package/circom/coordinator/qv/tallyVotes.circom +3 -1
- package/circom/utils/trees/BinaryMerkleRoot.circom +54 -0
- package/circom/utils/trees/CheckRoot.circom +45 -0
- package/circom/utils/trees/LeafExists.circom +27 -0
- package/circom/utils/trees/MerkleGeneratePathIndices.circom +40 -0
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +49 -0
- package/circom/voter/poll.circom +3 -4
- package/package.json +6 -6
- package/circom/utils/trees/incrementalMerkleTree.circom +0 -198
|
@@ -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
|
-
|
|
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
|
/**
|
|
@@ -32,14 +37,13 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
32
37
|
var STATE_TREE_ARITY = 2;
|
|
33
38
|
var MSG_LENGTH = 10;
|
|
34
39
|
var PACKED_CMD_LENGTH = 4;
|
|
35
|
-
var STATE_LEAF_LENGTH =
|
|
40
|
+
var STATE_LEAF_LENGTH = 3;
|
|
36
41
|
var BALLOT_LENGTH = 2;
|
|
37
42
|
var BALLOT_NONCE_IDX = 0;
|
|
38
43
|
var BALLOT_VO_ROOT_IDX = 1;
|
|
39
44
|
var STATE_LEAF_PUB_X_IDX = 0;
|
|
40
45
|
var STATE_LEAF_PUB_Y_IDX = 1;
|
|
41
46
|
var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
|
|
42
|
-
var STATE_LEAF_TIMESTAMP_IDX = 3;
|
|
43
47
|
var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
44
48
|
// Number of options for this poll.
|
|
45
49
|
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
@@ -266,7 +270,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
266
270
|
*/
|
|
267
271
|
template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
268
272
|
// Constants defining the structure and size of state and ballots.
|
|
269
|
-
var STATE_LEAF_LENGTH =
|
|
273
|
+
var STATE_LEAF_LENGTH = 3;
|
|
270
274
|
var BALLOT_LENGTH = 2;
|
|
271
275
|
var MSG_LENGTH = 10;
|
|
272
276
|
var PACKED_CMD_LENGTH = 4;
|
|
@@ -282,8 +286,6 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
282
286
|
var STATE_LEAF_PUB_Y_IDX = 1;
|
|
283
287
|
// Voice Credit balance.
|
|
284
288
|
var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
|
|
285
|
-
// Timestamp.
|
|
286
|
-
var STATE_LEAF_TIMESTAMP_IDX = 3;
|
|
287
289
|
var N_BITS = 252;
|
|
288
290
|
|
|
289
291
|
// Number of users that have completed the sign up.
|
|
@@ -357,7 +359,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
357
359
|
var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
|
|
358
360
|
|
|
359
361
|
// 3. Verify that the original state leaf exists in the given state root.
|
|
360
|
-
var stateLeafHash = PoseidonHasher(
|
|
362
|
+
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
361
363
|
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
362
364
|
stateLeafHash,
|
|
363
365
|
actualStateTreeDepth,
|
|
@@ -419,11 +421,10 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
419
421
|
newBallotVoRoot <== newBallotVoRootMux;
|
|
420
422
|
|
|
421
423
|
// 6. Generate a new state root.
|
|
422
|
-
var computedNewStateLeafhash = PoseidonHasher(
|
|
424
|
+
var computedNewStateLeafhash = PoseidonHasher(3)([
|
|
423
425
|
computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
|
|
424
426
|
computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
|
|
425
|
-
voiceCreditBalanceMux
|
|
426
|
-
stateLeaf[STATE_LEAF_TIMESTAMP_IDX]
|
|
427
|
+
voiceCreditBalanceMux
|
|
427
428
|
]);
|
|
428
429
|
|
|
429
430
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
@@ -445,3 +446,4 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
445
446
|
|
|
446
447
|
newBallotRoot <== computedNewBallotQip;
|
|
447
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/
|
|
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
|
-
|
|
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.
|
|
@@ -32,14 +37,13 @@ template ProcessMessages(
|
|
|
32
37
|
var STATE_TREE_ARITY = 2;
|
|
33
38
|
var MSG_LENGTH = 10;
|
|
34
39
|
var PACKED_CMD_LENGTH = 4;
|
|
35
|
-
var STATE_LEAF_LENGTH =
|
|
40
|
+
var STATE_LEAF_LENGTH = 3;
|
|
36
41
|
var BALLOT_LENGTH = 2;
|
|
37
42
|
var BALLOT_NONCE_IDX = 0;
|
|
38
43
|
var BALLOT_VO_ROOT_IDX = 1;
|
|
39
44
|
var STATE_LEAF_PUB_X_IDX = 0;
|
|
40
45
|
var STATE_LEAF_PUB_Y_IDX = 1;
|
|
41
46
|
var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
|
|
42
|
-
var STATE_LEAF_TIMESTAMP_IDX = 3;
|
|
43
47
|
var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
44
48
|
// Number of options for this poll.
|
|
45
49
|
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
@@ -260,7 +264,7 @@ template ProcessMessages(
|
|
|
260
264
|
*/
|
|
261
265
|
template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
262
266
|
// Constants defining the structure and size of state and ballots.
|
|
263
|
-
var STATE_LEAF_LENGTH =
|
|
267
|
+
var STATE_LEAF_LENGTH = 3;
|
|
264
268
|
var BALLOT_LENGTH = 2;
|
|
265
269
|
var MSG_LENGTH = 10;
|
|
266
270
|
var PACKED_CMD_LENGTH = 4;
|
|
@@ -276,8 +280,6 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
276
280
|
var STATE_LEAF_PUB_Y_IDX = 1;
|
|
277
281
|
// Voice Credit balance.
|
|
278
282
|
var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
|
|
279
|
-
// Timestamp.
|
|
280
|
-
var STATE_LEAF_TIMESTAMP_IDX = 3;
|
|
281
283
|
var N_BITS = 252;
|
|
282
284
|
|
|
283
285
|
// Number of users that have completed the sign up.
|
|
@@ -356,7 +358,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
356
358
|
var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
|
|
357
359
|
|
|
358
360
|
// 3. Verify that the original state leaf exists in the given state root.
|
|
359
|
-
var stateLeafHash = PoseidonHasher(
|
|
361
|
+
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
360
362
|
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
361
363
|
stateLeafHash,
|
|
362
364
|
actualStateTreeDepth,
|
|
@@ -421,11 +423,10 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
421
423
|
newBallotVoRoot <== newBallotVoRootMux;
|
|
422
424
|
|
|
423
425
|
// 6. Generate a new state root.
|
|
424
|
-
var computedNewStateLeafhash = PoseidonHasher(
|
|
426
|
+
var computedNewStateLeafhash = PoseidonHasher(3)([
|
|
425
427
|
computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
|
|
426
428
|
computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
|
|
427
|
-
voiceCreditBalanceMux
|
|
428
|
-
stateLeaf[STATE_LEAF_TIMESTAMP_IDX]
|
|
429
|
+
voiceCreditBalanceMux
|
|
429
430
|
]);
|
|
430
431
|
|
|
431
432
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
@@ -447,3 +448,4 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
447
448
|
|
|
448
449
|
newBallotRoot <== computedNewBallotQip;
|
|
449
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/
|
|
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
|
+
}
|
package/circom/voter/poll.circom
CHANGED
|
@@ -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/
|
|
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
|
|
@@ -65,8 +65,6 @@ template PollJoined(stateTreeDepth) {
|
|
|
65
65
|
signal input privKey;
|
|
66
66
|
// User's voice credits balance
|
|
67
67
|
signal input voiceCreditsBalance;
|
|
68
|
-
// Poll's joined timestamp
|
|
69
|
-
signal input joinTimestamp;
|
|
70
68
|
// Path elements
|
|
71
69
|
signal input pathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
72
70
|
// Path indices
|
|
@@ -79,7 +77,7 @@ template PollJoined(stateTreeDepth) {
|
|
|
79
77
|
// User private to public key
|
|
80
78
|
var derivedPubKey[2] = PrivToPubKey()(privKey);
|
|
81
79
|
|
|
82
|
-
var stateLeaf = PoseidonHasher(
|
|
80
|
+
var stateLeaf = PoseidonHasher(3)([derivedPubKey[0], derivedPubKey[1], voiceCreditsBalance]);
|
|
83
81
|
|
|
84
82
|
// Inclusion proof
|
|
85
83
|
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
@@ -91,3 +89,4 @@ template PollJoined(stateTreeDepth) {
|
|
|
91
89
|
|
|
92
90
|
stateLeafQip === stateRoot;
|
|
93
91
|
}
|
|
92
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maci-protocol/circuits",
|
|
3
|
-
"version": "0.0.0-ci.
|
|
3
|
+
"version": "0.0.0-ci.cf26211",
|
|
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.
|
|
47
|
-
"@maci-protocol/crypto": "0.0.0-ci.
|
|
48
|
-
"@maci-protocol/domainobjs": "0.0.0-ci.
|
|
49
|
-
"@maci-protocol/sdk": "0.0.0-ci.
|
|
46
|
+
"@maci-protocol/core": "0.0.0-ci.cf26211",
|
|
47
|
+
"@maci-protocol/crypto": "0.0.0-ci.cf26211",
|
|
48
|
+
"@maci-protocol/domainobjs": "0.0.0-ci.cf26211",
|
|
49
|
+
"@maci-protocol/sdk": "0.0.0-ci.cf26211",
|
|
50
50
|
"@zk-kit/circuits": "^0.4.0",
|
|
51
51
|
"circomkit": "^0.3.2",
|
|
52
52
|
"circomlib": "^2.0.5"
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"ts-node": "^10.9.1",
|
|
68
68
|
"typescript": "^5.8.3"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "cd4acb041dcbcc1245e699ff63dfdb585c723dee"
|
|
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
|
-
}
|