@maci-protocol/circuits 0.0.0-ci.77d0530 → 0.0.0-ci.799d56f

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.
Files changed (38) hide show
  1. package/README.md +2 -2
  2. package/build/ts/types.d.ts +4 -4
  3. package/build/ts/types.d.ts.map +1 -1
  4. package/build/tsconfig.build.tsbuildinfo +1 -1
  5. package/circom/circuits.json +28 -12
  6. package/circom/coordinator/full/MessageProcessor.circom +245 -0
  7. package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
  8. package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +17 -219
  9. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +197 -0
  10. package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +18 -95
  11. package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +18 -227
  12. package/circom/coordinator/qv/SingleMessageProcessor.circom +204 -0
  13. package/circom/coordinator/qv/{tallyVotes.circom → VoteTally.circom} +14 -114
  14. package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
  15. package/circom/utils/CalculateTotal.circom +6 -6
  16. package/circom/utils/EdDSAPoseidonVerifier.circom +8 -3
  17. package/circom/utils/IsOnCurve.circom +40 -0
  18. package/circom/utils/full/MessageValidator.circom +2 -4
  19. package/circom/utils/full/StateLeafAndBallotTransformer.circom +5 -0
  20. package/circom/utils/non-qv/MessageValidator.circom +2 -2
  21. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  22. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +5 -0
  23. package/circom/utils/qv/MessageValidator.circom +2 -2
  24. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  25. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +5 -0
  26. package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
  27. package/circom/utils/trees/LeafExists.circom +2 -2
  28. package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
  29. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  30. package/circom/utils/trees/{MerklePathIndicesGenerator.circom → QuinaryGeneratePathIndices.circom} +18 -18
  31. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  32. package/circom/utils/trees/QuinarySelector.circom +42 -0
  33. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  34. package/circom/utils/trees/Splicer.circom +76 -0
  35. package/circom/voter/PollJoined.circom +3 -3
  36. package/circom/voter/PollJoining.circom +3 -3
  37. package/package.json +17 -16
  38. package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
@@ -9,6 +9,7 @@ include "./escalarmulany.circom";
9
9
  include "./escalarmulfix.circom";
10
10
  // local imports
11
11
  include "./PoseidonHasher.circom";
12
+ include "./IsOnCurve.circom";
12
13
 
13
14
  /**
14
15
  * Variant of the EdDSAPoseidonVerifier template from circomlib
@@ -37,9 +38,13 @@ template EdDSAPoseidonVerifier() {
37
38
  // Output signal for the validity of the signature.
38
39
  signal output isValid;
39
40
 
41
+ // Verify the public key and signature point are on the BabyJubJub curve.
42
+ var computedIsPkOnCurve = IsOnCurve()(publicKeyX, publicKeyY);
43
+ var computedIsSpOnCurve = IsOnCurve()(signaturePointX, signaturePointY);
44
+
40
45
  // Ensure signatureScalar<Subgroup Order.
41
46
  // convert the signature scalar signatureScalar into its binary representation.
42
- var computedNum2Bits[254] = Num2Bits(254)(signatureScalar);
47
+ var computedNum2Bits[254] = Num2Bits_strict()(signatureScalar);
43
48
 
44
49
  var computedCompConstantIn[254] = computedNum2Bits;
45
50
  computedCompConstantIn[253] = 0;
@@ -82,10 +87,10 @@ template EdDSAPoseidonVerifier() {
82
87
  // Components to handle edge cases and ensure that all conditions
83
88
  // for a valid signature are met, including the
84
89
  // public key not being zero and other integrity checks.
85
- var computedIsAxZero = IsZero()(publicKeyX);
90
+ var computedIsAxZero = IsZero()(computedDouble3XOut);
86
91
  var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
87
92
  var computedIsCcZero = IsZero()(computedCompConstant);
88
- var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero, 3]);
93
+ var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero + computedIsPkOnCurve + computedIsSpOnCurve, 5]);
89
94
 
90
95
  isValid <== computedIsValid;
91
96
  }
@@ -0,0 +1,40 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit imports
4
+ include "./comparators.circom";
5
+
6
+ /**
7
+ * Returns 0 or 1 depending on if the point is on the BabyJubJub curve or not. The point is on the
8
+ * BabyJubJub curve if the following equation is true: a*x^2 + y^2 == 1 + d*x^2*y^2
9
+ * This template is identical to the BabyCheck template from circomlib, but it returns 0 or 1
10
+ * instead of having a hard constraint.
11
+ * Based on: https://github.com/iden3/circomlib/blob/master/circuits/babyjub.circom
12
+ */
13
+ template IsOnCurve() {
14
+ // x coordinate of the point on the BabyJubJub curve.
15
+ signal input x;
16
+ // y coordinate of the point on the BabyJubJub curve.
17
+ signal input y;
18
+ // True when the point (x, y) satisfies the BabyJubJub curve equation.
19
+ signal output isValid;
20
+
21
+ // x^2 and y^2 intermediate values.
22
+ signal x2;
23
+ signal y2;
24
+ // x^2 * y^2 intermediate value.
25
+ signal x2y2;
26
+
27
+ // BabyJubJub curve parameters.
28
+ var a = 168700;
29
+ var d = 168696;
30
+
31
+ // Compute x^2 and y^2.
32
+ x2 <== x * x;
33
+ y2 <== y * y;
34
+
35
+ // Compute x^2 * y^2.
36
+ x2y2 <== x2 * y2;
37
+
38
+ // Check if a*x^2 + y^2 == 1 + d*x^2*y^2.
39
+ isValid <== IsEqual()([a * x2 + y2, 1 + d * x2y2]);
40
+ }
@@ -1,7 +1,5 @@
1
1
  pragma circom 2.0.0;
2
2
 
3
- // circomlib import
4
- include "./mux1.circom";
5
3
  // zk-kit imports
6
4
  include "./safe-comparators.circom";
7
5
  // local imports
@@ -28,7 +26,7 @@ template MessageValidatorFull() {
28
26
  // Ballot nonce.
29
27
  signal input originalNonce;
30
28
  // Command nonce.
31
- signal input nonce;
29
+ signal input commandNonce;
32
30
  // Packed command.
33
31
  signal input command[PACKED_COMMAND_LENGTH];
34
32
  // Public key of the state leaf (user).
@@ -61,7 +59,7 @@ template MessageValidatorFull() {
61
59
  var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
62
60
 
63
61
  // Check (3) - The nonce must be correct.
64
- var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
62
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
65
63
 
66
64
  // Check (4) - The signature must be correct.
67
65
  var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
@@ -119,4 +119,9 @@ template StateLeafAndBallotTransformerFull() {
119
119
  isValid <== computedIsValid;
120
120
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
121
121
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
122
+
123
+ // Constrain commandPollId and commandSalt using dummy squares.
124
+ // This binds the proof to a specific poll and salt.
125
+ signal commandPollIdSquare <== commandPollId * commandPollId;
126
+ signal commandSaltSquared <== commandSalt * commandSalt;
122
127
  }
@@ -26,7 +26,7 @@ template MessageValidatorNonQv() {
26
26
  // Ballot nonce.
27
27
  signal input originalNonce;
28
28
  // Command nonce.
29
- signal input nonce;
29
+ signal input commandNonce;
30
30
  // Packed command.
31
31
  signal input command[PACKED_COMMAND_LENGTH];
32
32
  // Public key of the state leaf (user).
@@ -59,7 +59,7 @@ template MessageValidatorNonQv() {
59
59
  var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
60
60
 
61
61
  // Check (3) - The nonce must be correct.
62
- var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
62
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
63
63
 
64
64
  // Check (4) - The signature must be correct.
65
65
  var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
@@ -0,0 +1,84 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./comparators.circom";
5
+ // local imports
6
+ include "../trees/QuinaryCheckRoot.circom";
7
+ include "../PoseidonHasher.circom";
8
+
9
+ /**
10
+ * Performs verifications and computations related to current voting results.
11
+ * Also, computes and outputs a commitment to the new results.
12
+ * This template does not support Quadratic Voting (QV).
13
+ */
14
+ template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
15
+ // Number of children per node in the tree, defining the tree's branching factor.
16
+ var TREE_ARITY = 5;
17
+ // Number of voting options available, determined by the depth of the vote option tree.
18
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
19
+
20
+ // Equal to 1 if this is the first batch, otherwise 0.
21
+ signal input isFirstBatch;
22
+ // Commitment to the current tally before this batch.
23
+ signal input currentTallyCommitment;
24
+ // Commitment to the new tally after processing this batch.
25
+ signal input newTallyCommitment;
26
+
27
+ // Current results for each vote option.
28
+ signal input currentResults[totalVoteOptions];
29
+ // Salt for the root of the current results.
30
+ signal input currentResultsRootSalt;
31
+
32
+ // New results for each vote option.
33
+ signal input newResults[totalVoteOptions];
34
+ // Salt for the root of the new results.
35
+ signal input newResultsRootSalt;
36
+
37
+ // Total voice credits spent so far.
38
+ signal input currentSpentVoiceCreditSubtotal;
39
+ // Salt for the total spent voice credits.
40
+ signal input currentSpentVoiceCreditSubtotalSalt;
41
+
42
+ // Total new voice credits spent.
43
+ signal input newSpentVoiceCreditSubtotal;
44
+ // Salt for the new total spent voice credits.
45
+ signal input newSpentVoiceCreditSubtotalSalt;
46
+
47
+ // Compute the commitment to the current results.
48
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
49
+
50
+ // Verify currentResultsCommitmentHash.
51
+ var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
52
+
53
+ // Compute the commitment to the current spent voice credits.
54
+ var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
55
+
56
+ // Commit to the current tally
57
+ var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
58
+
59
+ // Check if the current tally commitment is correct only if this is not the first batch.
60
+ // computedIsZero.out is 1 if this is not the first batch.
61
+ // computedIsZero.out is 0 if this is the first batch.
62
+ var computedIsZero = IsZero()(isFirstBatch);
63
+
64
+ // isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
65
+ // isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
66
+ signal isFirstCommitment;
67
+ isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
68
+ isFirstCommitment === currentTallyCommitment;
69
+
70
+ // Compute the root of the new results.
71
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
72
+ var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
73
+
74
+ // Compute the commitment to the new spent voice credits value.
75
+ var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
76
+
77
+ // Commit to the new tally.
78
+ var computedNewTallyCommitment = PoseidonHasher(2)([
79
+ computedNewResultsCommitment,
80
+ computedNewSpentVoiceCreditsCommitment
81
+ ]);
82
+
83
+ computedNewTallyCommitment === newTallyCommitment;
84
+ }
@@ -102,4 +102,9 @@ template StateLeafAndBallotTransformerNonQv() {
102
102
  isValid <== computedIsValid;
103
103
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
104
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
+
106
+ // Constrain commandPollId and commandSalt using dummy squares.
107
+ // This binds the proof to a specific poll and salt.
108
+ signal commandPollIdSquare <== commandPollId * commandPollId;
109
+ signal commandSaltSquared <== commandSalt * commandSalt;
105
110
  }
@@ -26,7 +26,7 @@ template MessageValidator() {
26
26
  // Ballot nonce.
27
27
  signal input originalNonce;
28
28
  // Command nonce.
29
- signal input nonce;
29
+ signal input commandNonce;
30
30
  // Packed command.
31
31
  signal input command[PACKED_COMMAND_LENGTH];
32
32
  // Public key of the state leaf (user).
@@ -59,7 +59,7 @@ template MessageValidator() {
59
59
  var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
60
60
 
61
61
  // Check (3) - The nonce must be correct.
62
- var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
62
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
63
63
 
64
64
  // Check (4) - The signature must be correct.
65
65
  var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
@@ -0,0 +1,107 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./comparators.circom";
5
+ // local imports
6
+ include "../trees/QuinaryCheckRoot.circom";
7
+ include "../PoseidonHasher.circom";
8
+
9
+ /**
10
+ * Performs verifications and computations related to current voting results.
11
+ * Also, computes and outputs a commitment to the new results.
12
+ * This template supports the Quadratic Voting (QV).
13
+ */
14
+ template ResultCommitmentVerifierQv(voteOptionTreeDepth) {
15
+ // Number of children per node in the tree, defining the tree's branching factor.
16
+ var TREE_ARITY = 5;
17
+ // Number of voting options available, determined by the depth of the vote option tree.
18
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
19
+
20
+ // Equal to 1 if this is the first batch, otherwise 0.
21
+ signal input isFirstBatch;
22
+ // Commitment to the current tally before this batch.
23
+ signal input currentTallyCommitment;
24
+ // Commitment to the new tally after processing this batch.
25
+ signal input newTallyCommitment;
26
+
27
+ // Current results for each vote option.
28
+ signal input currentResults[totalVoteOptions];
29
+ // Salt for the root of the current results.
30
+ signal input currentResultsRootSalt;
31
+
32
+ // New results for each vote option.
33
+ signal input newResults[totalVoteOptions];
34
+ // Salt for the root of the new results.
35
+ signal input newResultsRootSalt;
36
+
37
+ // Total voice credits spent so far.
38
+ signal input currentSpentVoiceCreditSubtotal;
39
+ // Salt for the total spent voice credits.
40
+ signal input currentSpentVoiceCreditSubtotalSalt;
41
+
42
+ // Total new voice credits spent.
43
+ signal input newSpentVoiceCreditSubtotal;
44
+ // Salt for the new total spent voice credits.
45
+ signal input newSpentVoiceCreditSubtotalSalt;
46
+
47
+ // Spent voice credits per vote option.
48
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
49
+ // Salt for the root of spent credits per option.
50
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
51
+
52
+ // New spent voice credits per vote option.
53
+ signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
54
+ // Salt for the root of new spent credits per option.
55
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
56
+
57
+ // Compute the commitment to the current results.
58
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
59
+
60
+ // Verify currentResultsCommitmentHash.
61
+ var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
62
+
63
+ // Compute the commitment to the current spent voice credits.
64
+ var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
65
+
66
+ // Compute the root of the spent voice credits per vote option.
67
+ var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
68
+ var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
69
+
70
+ // Commit to the current tally.
71
+ var computedCurrentTallyCommitment = PoseidonHasher(3)([
72
+ computedCurrentResultsCommitment,
73
+ computedCurrentSpentVoiceCreditsCommitment,
74
+ computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
75
+ ]);
76
+
77
+ // Check if the current tally commitment is correct only if this is not the first batch.
78
+ // computedIsZero.out is 1 if this is not the first batch.
79
+ // computedIsZero.out is 0 if this is the first batch.
80
+ var computedIsZero = IsZero()(isFirstBatch);
81
+
82
+ // isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
83
+ // isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
84
+ signal isFirstCommitment;
85
+ isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
86
+ isFirstCommitment === currentTallyCommitment;
87
+
88
+ // Compute the root of the new results.
89
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
90
+ var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
91
+
92
+ // Compute the commitment to the new spent voice credits value.
93
+ var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
94
+
95
+ // Compute the root of the spent voice credits per vote option.
96
+ var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
97
+ var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
98
+
99
+ // Commit to the new tally.
100
+ var computedNewTallyCommitment = PoseidonHasher(3)([
101
+ computedNewResultsCommitment,
102
+ computedNewSpentVoiceCreditsCommitment,
103
+ computedNewPerVoteOptionSpentVoiceCreditsCommitment
104
+ ]);
105
+
106
+ computedNewTallyCommitment === newTallyCommitment;
107
+ }
@@ -102,4 +102,9 @@ template StateLeafAndBallotTransformer() {
102
102
  isValid <== computedIsValid;
103
103
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
104
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
+
106
+ // Constrain commandPollId and commandSalt using dummy squares.
107
+ // This binds the proof to a specific poll and salt.
108
+ signal commandPollIdSquare <== commandPollId * commandPollId;
109
+ signal commandSaltSquared <== commandSalt * commandSalt;
105
110
  }
@@ -7,7 +7,7 @@ include "./comparators.circom";
7
7
  include "../PoseidonHasher.circom";
8
8
 
9
9
  // @note taken from @zk-kit/circuits
10
- // if used directly in processMessages circom complains about duplicated
10
+ // if used directly in MessageProcessor circom complains about duplicated
11
11
  // templates (Ark, Poseidon, etc.)
12
12
  // This circuit is designed to calculate the root of a binary Merkle
13
13
  // tree given a leaf, its depth, and the necessary sibling
@@ -25,14 +25,17 @@ template BinaryMerkleRoot(MAX_DEPTH) {
25
25
  signal input leaf;
26
26
  // The depth of the Merkle tree.
27
27
  signal input depth;
28
- // The indices of the leaf node in the Merkle tree.
29
- signal input indices[MAX_DEPTH];
28
+ // The index of the leaf node in the Merkle tree.
29
+ signal input index;
30
30
  // The sibling nodes of the leaf node in the Merkle tree.
31
31
  signal input siblings[MAX_DEPTH][1];
32
32
 
33
33
  // The output of the Merkle tree root.
34
34
  signal output out;
35
35
 
36
+ // The indices of the leaf node in the Merkle tree.
37
+ signal indices[MAX_DEPTH] <== Num2Bits(MAX_DEPTH)(index);
38
+
36
39
  signal nodes[MAX_DEPTH + 1];
37
40
  nodes[0] <== leaf;
38
41
 
@@ -13,13 +13,13 @@ template LeafExists(levels) {
13
13
  // The elements along the path needed for the inclusion proof.
14
14
  signal input path_elements[levels][1];
15
15
  // The indices indicating the path taken through the tree for the leaf.
16
- signal input path_index[levels];
16
+ signal input path_indices[levels];
17
17
  // The root of the Merkle tree, against which the inclusion is verified.
18
18
  signal input root;
19
19
 
20
20
  var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
21
21
  leaf,
22
- path_index,
22
+ path_indices,
23
23
  path_elements
24
24
  );
25
25
 
@@ -12,7 +12,7 @@ template MerkleTreeInclusionProof(n_levels) {
12
12
  // The leaf node from which the Merkle root is calculated.
13
13
  signal input leaf;
14
14
  // Indices indicating left or right child for each level of the tree.
15
- signal input path_index[n_levels];
15
+ signal input path_indices[n_levels];
16
16
  // Sibling node values required to compute the hash at each level.
17
17
  signal input path_elements[n_levels][1];
18
18
 
@@ -25,8 +25,8 @@ template MerkleTreeInclusionProof(n_levels) {
25
25
  levelHashes[0] <== leaf;
26
26
 
27
27
  for (var i = 0; i < n_levels; i++) {
28
- // Validate path_index to be either 0 or 1, ensuring no other values.
29
- path_index[i] * (1 - path_index[i]) === 0;
28
+ // Validate path_indices to be either 0 or 1, ensuring no other values.
29
+ path_indices[i] * (1 - path_indices[i]) === 0;
30
30
 
31
31
  // Configure the multiplexer based on the path index for the current level.
32
32
  var multiplexer[2][2] = [
@@ -36,7 +36,7 @@ template MerkleTreeInclusionProof(n_levels) {
36
36
 
37
37
  var multiplexerResult[2] = MultiMux1(2)(
38
38
  multiplexer,
39
- path_index[i]
39
+ path_indices[i]
40
40
  );
41
41
 
42
42
  var computedLevelHash = PoseidonHasher(2)([multiplexerResult[0], multiplexerResult[1]]);
@@ -0,0 +1,54 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // local imports
4
+ include "../PoseidonHasher.circom";
5
+
6
+ /**
7
+ * Computes the root of a quintary Merkle tree given a list of leaves.
8
+ * This template constructs a Merkle tree with each node having 5 children (quintary)
9
+ * and computes the root by hashing with Poseidon the leaves and intermediate nodes in the given order.
10
+ * The computation is performed by first hashing groups of 5 leaves to form the bottom layer of nodes,
11
+ * then recursively hashing groups of these nodes to form the next layer, and so on, until the root is computed.
12
+ */
13
+ template QuinaryCheckRoot(levels) {
14
+ var LEAVES_PER_NODE = 5;
15
+ var totalLeaves = LEAVES_PER_NODE ** levels;
16
+ var numLeafHashers = LEAVES_PER_NODE ** (levels - 1);
17
+
18
+ signal input leaves[totalLeaves];
19
+ signal output root;
20
+
21
+ // Determine the total number of hashers.
22
+ var numHashers = 0;
23
+ for (var i = 0; i < levels; i++) {
24
+ numHashers += LEAVES_PER_NODE ** i;
25
+ }
26
+
27
+ var computedHashers[numHashers];
28
+
29
+ // Initialize hashers for the leaves.
30
+ for (var i = 0; i < numLeafHashers; i++) {
31
+ computedHashers[i] = PoseidonHasher(5)([
32
+ leaves[i * LEAVES_PER_NODE + 0],
33
+ leaves[i * LEAVES_PER_NODE + 1],
34
+ leaves[i * LEAVES_PER_NODE + 2],
35
+ leaves[i * LEAVES_PER_NODE + 3],
36
+ leaves[i * LEAVES_PER_NODE + 4]
37
+ ]);
38
+ }
39
+
40
+ // Initialize hashers for intermediate nodes and compute the root.
41
+ var k = 0;
42
+ for (var i = numLeafHashers; i < numHashers; i++) {
43
+ computedHashers[i] = PoseidonHasher(5)([
44
+ computedHashers[k * LEAVES_PER_NODE + 0],
45
+ computedHashers[k * LEAVES_PER_NODE + 1],
46
+ computedHashers[k * LEAVES_PER_NODE + 2],
47
+ computedHashers[k * LEAVES_PER_NODE + 3],
48
+ computedHashers[k * LEAVES_PER_NODE + 4]
49
+ ]);
50
+ k++;
51
+ }
52
+
53
+ root <== computedHashers[numHashers - 1];
54
+ }
@@ -1,44 +1,44 @@
1
1
  pragma circom 2.0.0;
2
2
 
3
- // zk-kit imports
3
+ // zk-kit import
4
4
  include "./safe-comparators.circom";
5
- // local import
5
+ // local imports
6
6
  include "../CalculateTotal.circom";
7
7
 
8
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.
9
+ * Calculates the path indices required for Merkle proof verifications (e.g., QuinaryTreeInclusionProof, QuinaryLeafExists).
10
+ * Given a node index within an IQT and the total tree levels, it outputs the path indices leading to that node.
11
11
  * The template handles the modulo and division operations to break down the tree index into its constituent path indices.
12
+ * e.g., if the index is 30 and the number of levels is 4, the output should be [0, 1, 1, 0].
12
13
  */
13
- template MerklePathIndicesGenerator(levels) {
14
- // The base used for the modulo and division operations, set to 2 for binary trees.
15
- var BASE = 2;
14
+ template QuinaryGeneratePathIndices(levels) {
15
+ // The number of leaves per node (tree arity)
16
+ var LEAVES_PER_NODE = 5;
16
17
 
17
- // The total sum of the path indices.
18
- signal input indices;
19
-
20
- // The generated path indices.
18
+ // The index within the tree
19
+ signal input index;
20
+ // The generated path indices leading to the node of the provided index
21
21
  signal output out[levels];
22
22
 
23
- var computedIndices = indices;
23
+ var indexModulus = index;
24
24
  var computedResults[levels];
25
25
 
26
26
  for (var i = 0; i < levels; i++) {
27
27
  // circom's best practices suggests to avoid using <-- unless you
28
28
  // are aware of what's going on. This is the only way to do modulo operation.
29
- out[i] <-- computedIndices % BASE;
30
- computedIndices = computedIndices \ BASE;
29
+ out[i] <-- indexModulus % LEAVES_PER_NODE;
30
+ indexModulus = indexModulus \ LEAVES_PER_NODE;
31
31
 
32
32
  // Check that each output element is less than the base.
33
- var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
33
+ var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], LEAVES_PER_NODE]);
34
34
  computedIsOutputElementLessThanBase === 1;
35
35
 
36
36
  // Re-compute the total sum.
37
- computedResults[i] = out[i] * (BASE ** i);
37
+ computedResults[i] = out[i] * (LEAVES_PER_NODE ** i);
38
38
  }
39
39
 
40
40
  // Check that the total sum matches the index.
41
41
  var computedCalculateTotal = CalculateTotal(levels)(computedResults);
42
42
 
43
- computedCalculateTotal === indices;
44
- }
43
+ computedCalculateTotal === index;
44
+ }
@@ -0,0 +1,30 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // local imports
4
+ include "./QuinaryTreeInclusionProof.circom";
5
+
6
+ /**
7
+ * Verifies if a given leaf exists within an IQT.
8
+ * Takes a leaf, its path to the root (specified by indices and path elements),
9
+ * and the root itself, to verify the leaf's inclusion within the tree.
10
+ */
11
+ template QuinaryLeafExists(levels){
12
+ // The number of leaves per node (tree arity)
13
+ var LEAVES_PER_NODE = 5;
14
+ // Number of leaves per path level (excluding the leaf itself)
15
+ var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;
16
+
17
+ // The leaf to check for inclusion
18
+ signal input leaf;
19
+ // The path indices at each level of the tree
20
+ signal input path_indices[levels];
21
+ // The sibling nodes at each level of the tree
22
+ signal input path_elements[levels][LEAVES_PER_PATH_LEVEL];
23
+ // The computed root of the tree
24
+ signal input root;
25
+
26
+ // Verify the Merkle path.
27
+ var computedRoot = QuinaryTreeInclusionProof(levels)(leaf, path_indices, path_elements);
28
+
29
+ root === computedRoot;
30
+ }
@@ -0,0 +1,42 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit import
4
+ include "./safe-comparators.circom";
5
+ // local imports
6
+ include "../CalculateTotal.circom";
7
+
8
+ /**
9
+ * Selects an item from a list based on the given index.
10
+ * It verifies the index is within the valid range and then iterates over the inputs to find the match.
11
+ * For each item, it checks if its position equals the given index and if so, multiplies the item
12
+ * by the result of the equality check, effectively selecting it.
13
+ * The sum of these results yields the selected item, ensuring only the item at the specified index be the output.
14
+ *
15
+ * nb. The number of items must be less than 8, and the index must be less than the number of items.
16
+ */
17
+ template QuinarySelector(choices) {
18
+ // The input elements to select from.
19
+ signal input in[choices];
20
+ // The index of the element to select
21
+ signal input index;
22
+ // The selected total sum of the elements.
23
+ signal output out;
24
+
25
+ // Ensure that index < choices.
26
+ var computedIndex = SafeLessThan(3)([index, choices]);
27
+ computedIndex === 1;
28
+
29
+ // Initialize an array to hold the results of equality checks.
30
+ var computedResults[choices];
31
+
32
+ // For each item, check whether its index equals the input index.
33
+ // The result is multiplied by the corresponding input value.
34
+ for (var i = 0; i < choices; i++) {
35
+ var computedIsIndexEqual = IsEqual()([i, index]);
36
+
37
+ computedResults[i] = computedIsIndexEqual * in[i];
38
+ }
39
+
40
+ // Calculate the total sum of the results array.
41
+ out <== CalculateTotal(choices)(computedResults);
42
+ }