@maci-protocol/circuits 0.0.0-ci.f4bc8a6 → 0.0.0-ci.f5db322

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 (49) hide show
  1. package/LICENSE +1 -2
  2. package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
  3. package/build/ts/generateZkeys.d.ts.map +1 -0
  4. package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
  5. package/build/ts/generateZkeys.js.map +1 -0
  6. package/build/ts/types.d.ts +11 -12
  7. package/build/ts/types.d.ts.map +1 -1
  8. package/build/tsconfig.build.tsbuildinfo +1 -1
  9. package/circom/circuits.json +23 -7
  10. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  11. package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
  12. package/circom/coordinator/non-qv/processMessages.circom +119 -119
  13. package/circom/coordinator/non-qv/tallyVotes.circom +51 -45
  14. package/circom/coordinator/qv/processMessages.circom +120 -117
  15. package/circom/coordinator/qv/tallyVotes.circom +65 -63
  16. package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +8 -6
  17. package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
  18. package/circom/utils/MessageHasher.circom +57 -0
  19. package/circom/utils/MessageToCommand.circom +107 -0
  20. package/circom/utils/PoseidonHasher.circom +29 -0
  21. package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +12 -10
  22. package/circom/utils/VerifySignature.circom +39 -0
  23. package/circom/utils/full/MessageValidator.circom +91 -0
  24. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  25. package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +17 -15
  26. package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  27. package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +17 -15
  28. package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  29. package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
  30. package/circom/utils/trees/CheckRoot.circom +49 -0
  31. package/circom/utils/trees/LeafExists.circom +27 -0
  32. package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
  33. package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
  34. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  35. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  36. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  37. package/circom/utils/trees/QuinarySelector.circom +42 -0
  38. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  39. package/circom/utils/trees/Splicer.circom +76 -0
  40. package/circom/voter/PollJoined.circom +43 -0
  41. package/circom/voter/PollJoining.circom +54 -0
  42. package/package.json +17 -14
  43. package/build/ts/genZkeys.d.ts.map +0 -1
  44. package/build/ts/genZkeys.js.map +0 -1
  45. package/circom/utils/hashers.circom +0 -78
  46. package/circom/utils/messageToCommand.circom +0 -78
  47. package/circom/utils/trees/incrementalMerkleTree.circom +0 -198
  48. package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
  49. package/circom/voter/poll.circom +0 -93
@@ -5,10 +5,12 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
- include "../../utils/trees/incrementalMerkleTree.circom";
9
- include "../../utils/trees/incrementalQuinaryTree.circom";
10
- include "../../utils/calculateTotal.circom";
11
- include "../../utils/hashers.circom";
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
+ include "../../utils/trees/LeafExists.circom";
11
+ include "../../utils/trees/QuinaryCheckRoot.circom";
12
+ include "../../utils/CalculateTotal.circom";
13
+ include "../../utils/PoseidonHasher.circom";
12
14
 
13
15
  /**
14
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
@@ -16,33 +18,33 @@ include "../../utils/hashers.circom";
16
18
  */
17
19
  template TallyVotes(
18
20
  stateTreeDepth,
19
- intStateTreeDepth,
21
+ tallyProcessingStateTreeDepth,
20
22
  voteOptionTreeDepth
21
23
  ) {
22
24
  // Ensure there's at least one level in the vote option tree.
23
25
  assert(voteOptionTreeDepth > 0);
24
26
  // Ensure the intermediate state tree has at least one level.
25
- assert(intStateTreeDepth > 0);
27
+ assert(tallyProcessingStateTreeDepth > 0);
26
28
  // The intermediate state tree must be smaller than the full state tree.
27
- assert(intStateTreeDepth < stateTreeDepth);
29
+ assert(tallyProcessingStateTreeDepth < stateTreeDepth);
28
30
 
29
31
  // Number of children per node in the tree, defining the tree's branching factor.
30
32
  var TREE_ARITY = 5;
31
33
  var BALLOT_TREE_ARITY = 2;
32
34
 
33
35
  // The number of ballots processed at once, determined by the depth of the intermediate state tree.
34
- var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
36
+ var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
35
37
  // Number of voting options available, determined by the depth of the vote option tree.
36
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
38
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
37
39
 
38
40
  // Number of elements in each ballot.
39
41
  var BALLOT_LENGTH = 2;
40
42
  // Index for the nonce in the ballot array.
41
- var BALLOT_NONCE_IDX = 0;
43
+ var BALLOT_NONCE_INDEX = 0;
42
44
  // Index for the voting option root in the ballot array.
43
- var BALLOT_VO_ROOT_IDX = 1;
45
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
44
46
  // Difference in tree depths, used in path calculations.
45
- var k = stateTreeDepth - intStateTreeDepth;
47
+ var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
46
48
 
47
49
  // Root of the state Merkle tree, representing the overall state before voting.
48
50
  signal input stateRoot;
@@ -59,13 +61,13 @@ template TallyVotes(
59
61
  // Start index of given batch
60
62
  signal input index;
61
63
  // Number of users that signup
62
- signal input numSignUps;
64
+ signal input totalSignups;
63
65
  // Ballots and their corresponding path elements for verification in the tree.
64
66
  signal input ballots[batchSize][BALLOT_LENGTH];
65
- signal input ballotPathElements[k][BALLOT_TREE_ARITY - 1];
66
- signal input votes[batchSize][numVoteOptions];
67
+ signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
+ signal input votes[batchSize][totalVoteOptions];
67
69
  // Current results for each vote option.
68
- signal input currentResults[numVoteOptions];
70
+ signal input currentResults[totalVoteOptions];
69
71
  // Salt for the root of the current results.
70
72
  signal input currentResultsRootSalt;
71
73
  // Total voice credits spent so far.
@@ -73,13 +75,13 @@ template TallyVotes(
73
75
  // Salt for the total spent voice credits.
74
76
  signal input currentSpentVoiceCreditSubtotalSalt;
75
77
  // Spent voice credits per vote option.
76
- signal input currentPerVOSpentVoiceCredits[numVoteOptions];
78
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
77
79
  // Salt for the root of spent credits per option.
78
- signal input currentPerVOSpentVoiceCreditsRootSalt;
80
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
79
81
  // Salt for the root of the new results.
80
82
  signal input newResultsRootSalt;
81
83
  // Salt for the new spent credits per vote option root.
82
- signal input newPerVOSpentVoiceCreditsRootSalt;
84
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
83
85
  // Salt for the new total spent voice credits root.
84
86
  signal input newSpentVoiceCreditSubtotalSalt;
85
87
 
@@ -88,21 +90,21 @@ template TallyVotes(
88
90
  computedSbCommitment === sbCommitment;
89
91
 
90
92
  // Validates that the index is within the valid range of sign-ups.
91
- var numSignUpsValid = LessEqThan(50)([index, numSignUps]);
92
- numSignUpsValid === 1;
93
+ var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
94
+ totalSignupsValid === 1;
93
95
 
94
96
  // Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
95
97
  var computedBallotHashers[batchSize];
96
98
 
97
99
  for (var i = 0; i < batchSize; i++) {
98
- computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_IDX], ballots[i][BALLOT_VO_ROOT_IDX]]);
100
+ computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
99
101
  }
100
102
 
101
- var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
102
- var computedBallotPathIndices[k] = MerkleGeneratePathIndices(k)(index / batchSize);
103
+ var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
104
+ var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
103
105
 
104
106
  // Verifies each ballot's existence within the ballot tree.
105
- LeafExists(k)(
107
+ LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
106
108
  computedBallotSubroot,
107
109
  ballotPathElements,
108
110
  computedBallotPathIndices,
@@ -112,8 +114,8 @@ template TallyVotes(
112
114
  // Processes vote options, verifying each against its declared root.
113
115
  var computedVoteTree[batchSize];
114
116
  for (var i = 0; i < batchSize; i++) {
115
- computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
116
- computedVoteTree[i] === ballots[i][BALLOT_VO_ROOT_IDX];
117
+ computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
118
+ computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
117
119
  }
118
120
 
119
121
  // Calculates new results and spent voice credits based on the current and incoming votes.
@@ -121,8 +123,8 @@ template TallyVotes(
121
123
  var computedIsZero = IsZero()(computedIsFirstBatch);
122
124
 
123
125
  // Tally the new results.
124
- var computedCalculateTotalResult[numVoteOptions];
125
- for (var i = 0; i < numVoteOptions; i++) {
126
+ var computedCalculateTotalResult[totalVoteOptions];
127
+ for (var i = 0; i < totalVoteOptions; i++) {
126
128
  var numsRC[batchSize + 1];
127
129
  numsRC[batchSize] = currentResults[i] * computedIsZero;
128
130
  for (var j = 0; j < batchSize; j++) {
@@ -133,27 +135,27 @@ template TallyVotes(
133
135
  }
134
136
 
135
137
  // Tally the new spent voice credit total.
136
- var numsSVC[batchSize * numVoteOptions + 1];
137
- numsSVC[batchSize * numVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
138
+ var numsSVC[batchSize * totalVoteOptions + 1];
139
+ numsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
138
140
  for (var i = 0; i < batchSize; i++) {
139
- for (var j = 0; j < numVoteOptions; j++) {
140
- numsSVC[i * numVoteOptions + j] = votes[i][j] * votes[i][j];
141
+ for (var j = 0; j < totalVoteOptions; j++) {
142
+ numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
141
143
  }
142
144
  }
143
145
 
144
- var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * numVoteOptions + 1)(numsSVC);
146
+ var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(numsSVC);
145
147
 
146
148
  // Tally the spent voice credits per vote option.
147
- var computedNewPerVOSpentVoiceCredits[numVoteOptions];
149
+ var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
148
150
 
149
- for (var i = 0; i < numVoteOptions; i++) {
150
- var computedNumsSVC[batchSize + 1];
151
- computedNumsSVC[batchSize] = currentPerVOSpentVoiceCredits[i] * computedIsZero;
151
+ for (var i = 0; i < totalVoteOptions; i++) {
152
+ var computedTotalVoiceCreditSpent[batchSize + 1];
153
+ computedTotalVoiceCreditSpent[batchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
152
154
  for (var j = 0; j < batchSize; j++) {
153
- computedNumsSVC[j] = votes[j][i] * votes[j][i];
155
+ computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
154
156
  }
155
157
 
156
- computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedNumsSVC);
158
+ computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedTotalVoiceCreditSpent);
157
159
  }
158
160
 
159
161
  // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
@@ -169,10 +171,10 @@ template TallyVotes(
169
171
  currentSpentVoiceCreditSubtotalSalt,
170
172
  computedNewSpentVoiceCreditSubtotal,
171
173
  newSpentVoiceCreditSubtotalSalt,
172
- currentPerVOSpentVoiceCredits,
173
- currentPerVOSpentVoiceCreditsRootSalt,
174
+ currentPerVoteOptionSpentVoiceCredits,
175
+ currentPerVoteOptionSpentVoiceCreditsRootSalt,
174
176
  computedNewPerVOSpentVoiceCredits,
175
- newPerVOSpentVoiceCreditsRootSalt
177
+ newPerVoteOptionSpentVoiceCreditsRootSalt
176
178
  );
177
179
  }
178
180
 
@@ -185,7 +187,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
185
187
  // Number of children per node in the tree, defining the tree's branching factor.
186
188
  var TREE_ARITY = 5;
187
189
  // Number of voting options available, determined by the depth of the vote option tree.
188
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
190
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
189
191
 
190
192
  // Equal to 1 if this is the first batch, otherwise 0.
191
193
  signal input isFirstBatch;
@@ -195,12 +197,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
195
197
  signal input newTallyCommitment;
196
198
 
197
199
  // Current results for each vote option.
198
- signal input currentResults[numVoteOptions];
200
+ signal input currentResults[totalVoteOptions];
199
201
  // Salt for the root of the current results.
200
202
  signal input currentResultsRootSalt;
201
203
 
202
204
  // New results for each vote option.
203
- signal input newResults[numVoteOptions];
205
+ signal input newResults[totalVoteOptions];
204
206
  // Salt for the root of the new results.
205
207
  signal input newResultsRootSalt;
206
208
 
@@ -215,17 +217,17 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
215
217
  signal input newSpentVoiceCreditSubtotalSalt;
216
218
 
217
219
  // Spent voice credits per vote option.
218
- signal input currentPerVOSpentVoiceCredits[numVoteOptions];
220
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
219
221
  // Salt for the root of spent credits per option.
220
- signal input currentPerVOSpentVoiceCreditsRootSalt;
222
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
221
223
 
222
224
  // New spent voice credits per vote option.
223
- signal input newPerVOSpentVoiceCredits[numVoteOptions];
225
+ signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
224
226
  // Salt for the root of new spent credits per option.
225
- signal input newPerVOSpentVoiceCreditsRootSalt;
227
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
226
228
 
227
229
  // Compute the commitment to the current results.
228
- var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
230
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
229
231
 
230
232
  // Verify currentResultsCommitmentHash.
231
233
  var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
@@ -234,14 +236,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
234
236
  var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
235
237
 
236
238
  // Compute the root of the spent voice credits per vote option.
237
- var computedCurrentPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentPerVOSpentVoiceCredits);
238
- var computedCurrentPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVOSpentVoiceCreditsRoot, currentPerVOSpentVoiceCreditsRootSalt]);
239
+ var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
240
+ var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
239
241
 
240
242
  // Commit to the current tally.
241
243
  var computedCurrentTallyCommitment = PoseidonHasher(3)([
242
244
  computedCurrentResultsCommitment,
243
245
  computedCurrentSpentVoiceCreditsCommitment,
244
- computedCurrentPerVOSpentVoiceCreditsCommitment
246
+ computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
245
247
  ]);
246
248
 
247
249
  // Check if the current tally commitment is correct only if this is not the first batch.
@@ -249,28 +251,28 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
249
251
  // computedIsZero.out is 0 if this is the first batch.
250
252
  var computedIsZero = IsZero()(isFirstBatch);
251
253
 
252
- // hz is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
253
- // hz is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
254
- signal hz;
255
- hz <== computedIsZero * computedCurrentTallyCommitment;
256
- hz === currentTallyCommitment;
254
+ // isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
255
+ // isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
256
+ signal isFirstCommitment;
257
+ isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
258
+ isFirstCommitment === currentTallyCommitment;
257
259
 
258
260
  // Compute the root of the new results.
259
- var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
261
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
260
262
  var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
261
263
 
262
264
  // Compute the commitment to the new spent voice credits value.
263
265
  var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
264
266
 
265
267
  // Compute the root of the spent voice credits per vote option.
266
- var computedNewPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(newPerVOSpentVoiceCredits);
267
- var computedNewPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVOSpentVoiceCreditsRoot, newPerVOSpentVoiceCreditsRootSalt]);
268
+ var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
269
+ var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
268
270
 
269
271
  // Commit to the new tally.
270
272
  var computedNewTallyCommitment = PoseidonHasher(3)([
271
273
  computedNewResultsCommitment,
272
274
  computedNewSpentVoiceCreditsCommitment,
273
- computedNewPerVOSpentVoiceCreditsCommitment
275
+ computedNewPerVoteOptionSpentVoiceCreditsCommitment
274
276
  ]);
275
277
 
276
278
  computedNewTallyCommitment === newTallyCommitment;
@@ -1,22 +1,24 @@
1
1
  pragma circom 2.0.0;
2
2
 
3
3
  /**
4
- * Computes the cumulative sum of an array of n input signals.
4
+ * Computes the cumulative sum of an array of length input signals.
5
5
  * It iterates through each input, aggregating the sum up to that point,
6
6
  * and outputs the total sum of all inputs. This template is useful for
7
7
  * operations requiring the total sum of multiple signals, ensuring the
8
8
  * final output reflects the cumulative total of the inputs provided.
9
9
  */
10
- template CalculateTotal(n) {
11
- signal input nums[n];
10
+ template CalculateTotal(length) {
11
+ // Array of values.
12
+ signal input nums[length];
13
+ // Total sum.
12
14
  signal output sum;
13
15
 
14
- signal sums[n];
16
+ signal sums[length];
15
17
  sums[0] <== nums[0];
16
18
 
17
- for (var i = 1; i < n; i++) {
19
+ for (var i = 1; i < length; i++) {
18
20
  sums[i] <== sums[i - 1] + nums[i];
19
21
  }
20
22
 
21
- sum <== sums[n - 1];
23
+ sum <== sums[length - 1];
22
24
  }
@@ -8,60 +8,69 @@ include "./bitify.circom";
8
8
  include "./escalarmulany.circom";
9
9
  include "./escalarmulfix.circom";
10
10
  // local imports
11
- include "./hashers.circom";
11
+ include "./PoseidonHasher.circom";
12
12
 
13
13
  /**
14
14
  * Variant of the EdDSAPoseidonVerifier template from circomlib
15
15
  * https://github.com/iden3/circomlib/blob/master/circuits/eddsa.circom
16
16
  */
17
- template EdDSAPoseidonVerifier_patched() {
17
+ template EdDSAPoseidonVerifier() {
18
+ // The prime subgroup order.
19
+ var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
20
+
21
+ // The base point of the BabyJubJub curve.
22
+ var BASE8[2] = [
23
+ 5299619240641551281634865583518297030282874472190772894086521144482721001553,
24
+ 16950150798460657717958625567821834550301663161624707787222815936182638968203
25
+ ];
26
+
18
27
  // The x and y coordinates of the public key.
19
- signal input Ax;
20
- signal input Ay;
28
+ signal input publicKeyX;
29
+ signal input publicKeyY;
21
30
  // Signature scalar.
22
- signal input S;
31
+ signal input signatureScalar;
23
32
  // The x and y coordinates of the signature point.
24
- signal input R8x;
25
- signal input R8y;
33
+ signal input signaturePointX;
34
+ signal input signaturePointY;
26
35
  // Message hash.
27
- signal input M;
36
+ signal input messageHash;
37
+ // Output signal for the validity of the signature.
38
+ signal output isValid;
28
39
 
29
- signal output valid;
30
-
31
- // Ensure S<Subgroup Order.
32
- // convert the signature scalar S into its binary representation.
33
- var computedNum2Bits[254] = Num2Bits(254)(S);
40
+ // Ensure signatureScalar<Subgroup Order.
41
+ // convert the signature scalar signatureScalar into its binary representation.
42
+ var computedNum2Bits[254] = Num2Bits(254)(signatureScalar);
34
43
 
35
44
  var computedCompConstantIn[254] = computedNum2Bits;
36
45
  computedCompConstantIn[253] = 0;
37
46
 
38
- // A component that ensures S is within a valid range,
47
+ // A component that ensures signatureScalar is within a valid range,
39
48
  // comparing it against a constant representing the subgroup order.
40
- var computedCompConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040)(computedCompConstantIn);
49
+ var computedCompConstant = CompConstant(SUBGROUP_ORDER - 1)(computedCompConstantIn);
41
50
 
42
- // Calculate the h = H(R,A, msg).
43
- var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([R8x, R8y, Ax, Ay, M]));
51
+ // Calculate the h = H(R, A, msg).
52
+ var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([
53
+ signaturePointX,
54
+ signaturePointY,
55
+ publicKeyX,
56
+ publicKeyY,
57
+ messageHash
58
+ ]));
44
59
 
45
60
  // These components perform point doubling operations on the public key
46
61
  // to align it within the correct subgroup as part of the verification process.
47
- var (computedDbl1XOut, computedDbl1YOut) = BabyDbl()(Ax, Ay);
48
- var (computedDbl2XOut, computedDbl2YOut) = BabyDbl()(computedDbl1XOut, computedDbl1YOut);
49
- var (computedDbl3XOut, computedDbl3YOut) = BabyDbl()(computedDbl2XOut, computedDbl2YOut);
62
+ var (computedDouble1XOut, computedDouble1YOut) = BabyDbl()(publicKeyX, publicKeyY);
63
+ var (computedDouble2XOut, computedDouble2YOut) = BabyDbl()(computedDouble1XOut, computedDouble1YOut);
64
+ var (computedDouble3XOut, computedDouble3YOut) = BabyDbl()(computedDouble2XOut, computedDouble2YOut);
50
65
 
51
66
  // A component that performs scalar multiplication of the
52
67
  // adjusted public key by the hash output, essential for the verification calculation.
53
- var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDbl3XOut, computedDbl3YOut]);
68
+ var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDouble3XOut, computedDouble3YOut]);
54
69
 
55
70
  // Compute the right side: right = R8 + right2.
56
- var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(R8x, R8y, computedEscalarMulAny[0], computedEscalarMulAny[1]);
57
-
58
- // Calculate the left side: left = S * B8.
59
- var BASE8[2] = [
60
- 5299619240641551281634865583518297030282874472190772894086521144482721001553,
61
- 16950150798460657717958625567821834550301663161624707787222815936182638968203
62
- ];
71
+ var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(signaturePointX, signaturePointY, computedEscalarMulAny[0], computedEscalarMulAny[1]);
63
72
 
64
- // Fixed-base scalar multiplication of a base point by S.
73
+ // Fixed-base scalar multiplication of a base point by signatureScalar.
65
74
  var computedEscalarMulFix[2] = EscalarMulFix(254, BASE8)(computedNum2Bits);
66
75
 
67
76
  // Components to check the equality of x and y coordinates
@@ -73,45 +82,10 @@ template EdDSAPoseidonVerifier_patched() {
73
82
  // Components to handle edge cases and ensure that all conditions
74
83
  // for a valid signature are met, including the
75
84
  // public key not being zero and other integrity checks.
76
- var computedIsAxZero = IsZero()(Ax);
85
+ var computedIsAxZero = IsZero()(publicKeyX);
77
86
  var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
78
87
  var computedIsCcZero = IsZero()(computedCompConstant);
79
88
  var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero, 3]);
80
89
 
81
- valid <== computedIsValid;
82
- }
83
-
84
- /**
85
- * Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
86
- */
87
- template VerifySignature() {
88
- // Public key of the signer, consisting of two coordinates [x, y].
89
- signal input pubKey[2];
90
- // R8 point from the signature, consisting of two coordinates [x, y].
91
- signal input R8[2];
92
- // Scalar component of the signature.
93
- signal input S;
94
-
95
- // Number of elements in the hash preimage.
96
- var k = 4;
97
-
98
- // The preimage data that was hashed, an array of four elements.
99
- signal input preimage[k];
100
-
101
- signal output valid;
102
-
103
- // Hash the preimage using the Poseidon hashing function configured for four inputs.
104
- var computedM = PoseidonHasher(4)(preimage);
105
-
106
- // Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
107
- var computedVerifier = EdDSAPoseidonVerifier_patched()(
108
- pubKey[0],
109
- pubKey[1],
110
- S,
111
- R8[0],
112
- R8[1],
113
- computedM
114
- );
115
-
116
- valid <== computedVerifier;
90
+ isValid <== computedIsValid;
117
91
  }
@@ -0,0 +1,57 @@
1
+ pragma circom 2.0.0;
2
+
3
+ include "./PoseidonHasher.circom";
4
+
5
+ /**
6
+ * Hashes a MACI message and the public key used for message encryption.
7
+ * This template processes 10 message inputs and a 2-element public key
8
+ * combining them using the Poseidon hash function. The hashing process involves two stages:
9
+ * 1. hashing message parts data groups of five and,
10
+ * 2. hashing the grouped results alongside the first message input and
11
+ * the encryption public key to produce a final hash output.
12
+ */
13
+ template MessageHasher() {
14
+ // Message parts
15
+ var MESSAGE_PARTS = 10;
16
+ var STATE_INDEX = 0;
17
+ var VOTE_OPTION_INDEX = 1;
18
+ var NEW_VOTE_WEIGHT = 2;
19
+ var NONCE = 3;
20
+ var POLL_ID = 4;
21
+ var SIGNATURE_POINT_X = 5;
22
+ var SIGNATURE_POINT_Y = 6;
23
+ var SIGNATURE_SCALAR = 7;
24
+ var ENCRYPTED_PUBLIC_KEY_X = 8;
25
+ var ENCRYPTED_PUBLIC_KEY_Y = 9;
26
+
27
+ // The MACI message is composed of 10 parts.
28
+ signal input data[MESSAGE_PARTS];
29
+ // the public key used to encrypt the message.
30
+ signal input encryptionPublicKey[2];
31
+
32
+ // we output an hash.
33
+ signal output hash;
34
+
35
+ var computedHasherPart1 = PoseidonHasher(5)([
36
+ data[STATE_INDEX],
37
+ data[VOTE_OPTION_INDEX],
38
+ data[NEW_VOTE_WEIGHT],
39
+ data[NONCE],
40
+ data[POLL_ID]
41
+ ]);
42
+
43
+ var computedHasherPart2 = PoseidonHasher(5)([
44
+ data[SIGNATURE_POINT_X],
45
+ data[SIGNATURE_POINT_Y],
46
+ data[SIGNATURE_SCALAR],
47
+ data[ENCRYPTED_PUBLIC_KEY_X],
48
+ data[ENCRYPTED_PUBLIC_KEY_Y]
49
+ ]);
50
+
51
+ hash <== PoseidonHasher(4)([
52
+ computedHasherPart1,
53
+ computedHasherPart2,
54
+ encryptionPublicKey[0],
55
+ encryptionPublicKey[1]
56
+ ]);
57
+ }
@@ -0,0 +1,107 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./bitify.circom";
5
+ // zk-kit imports
6
+ include "./ecdh.circom";
7
+ include "./unpack-element.circom";
8
+ include "./poseidon-cipher.circom";
9
+
10
+ /**
11
+ * Converts a MACI message to a command by decrypting it.
12
+ * Processes encrypted MACI messages into structured MACI commands
13
+ * by decrypting using a shared key derived from ECDH. After decryption,
14
+ * unpacks and assigns decrypted values to specific command components.
15
+ */
16
+ template MessageToCommand() {
17
+ var MESSAGE_LENGTH = 7;
18
+ var PACKED_COMMAND_LENGTH = 4;
19
+ var UNPACK_ELEMENT_LENGTH = 5;
20
+ var DECRYPTED_LENGTH = 9;
21
+ var MESSAGE_PARTS = 10;
22
+
23
+ // Element indices.
24
+ var ELEMENT_POLL_ID = 0;
25
+ var ELEMENT_NONCE = 1;
26
+ var ELEMENT_NEW_VOTE_WEIGHT = 2;
27
+ var ELEMENT_VOTE_OPTION_INDEX = 3;
28
+ var ELEMENT_STATE_INDEX = 4;
29
+
30
+ // Command indices.
31
+ var COMMAND_STATE_INDEX = 0;
32
+ var COMMAND_PUBLIC_KEY_X = 1;
33
+ var COMMAND_PUBLIC_KEY_Y = 2;
34
+ var COMMAND_SALT = 3;
35
+
36
+ // Decryptor indices.
37
+ var SIGNATURE_POINT_X = 4;
38
+ var SIGNATURE_POINT_Y = 5;
39
+ var SIGNATURE_SCALAR = 6;
40
+
41
+ // The message is an array of 10 parts.
42
+ signal input message[MESSAGE_PARTS];
43
+ // The encryption private key
44
+ signal input encryptionPrivateKey;
45
+ // The encryption public key
46
+ signal input encryptionPublicKey[2];
47
+
48
+ // Command parts.
49
+ signal output stateIndex;
50
+ // The new public key.
51
+ signal output newPublicKey[2];
52
+ // The vote option index.
53
+ signal output voteOptionIndex;
54
+ // The new vote weight.
55
+ signal output newVoteWeight;
56
+ // The nonce.
57
+ signal output nonce;
58
+ // The poll id.
59
+ signal output pollId;
60
+ // The salt.
61
+ signal output salt;
62
+ // The signature point.
63
+ signal output signaturePoint[2];
64
+ // The signature scalar.
65
+ signal output signatureScalar;
66
+
67
+ // Packed command.
68
+ signal output packedCommandOut[PACKED_COMMAND_LENGTH];
69
+
70
+ // Generate the shared key for decrypting the message.
71
+ var computedEcdh[2] = Ecdh()(encryptionPrivateKey, encryptionPublicKey);
72
+
73
+ // Decrypt the message using Poseidon decryption.
74
+ var computedDecryptor[DECRYPTED_LENGTH] = PoseidonDecryptWithoutCheck(MESSAGE_LENGTH)(
75
+ message,
76
+ 0,
77
+ computedEcdh
78
+ );
79
+
80
+ // Save the decrypted message into a packed command signal.
81
+ signal packedCommand[PACKED_COMMAND_LENGTH];
82
+
83
+ for (var i = 0; i < PACKED_COMMAND_LENGTH; i++) {
84
+ packedCommand[i] <== computedDecryptor[i];
85
+ }
86
+
87
+ var computedUnpackElement[UNPACK_ELEMENT_LENGTH] = UnpackElement(UNPACK_ELEMENT_LENGTH)(
88
+ packedCommand[COMMAND_STATE_INDEX]
89
+ );
90
+
91
+ // Everything below were packed into the first element.
92
+ pollId <== computedUnpackElement[ELEMENT_POLL_ID];
93
+ nonce <== computedUnpackElement[ELEMENT_NONCE];
94
+ newVoteWeight <== computedUnpackElement[ELEMENT_NEW_VOTE_WEIGHT];
95
+ voteOptionIndex <== computedUnpackElement[ELEMENT_VOTE_OPTION_INDEX];
96
+ stateIndex <== computedUnpackElement[ELEMENT_STATE_INDEX];
97
+
98
+ newPublicKey[0] <== packedCommand[COMMAND_PUBLIC_KEY_X];
99
+ newPublicKey[1] <== packedCommand[COMMAND_PUBLIC_KEY_Y];
100
+ salt <== packedCommand[COMMAND_SALT];
101
+
102
+ signaturePoint[0] <== computedDecryptor[SIGNATURE_POINT_X];
103
+ signaturePoint[1] <== computedDecryptor[SIGNATURE_POINT_Y];
104
+ signatureScalar <== computedDecryptor[SIGNATURE_SCALAR];
105
+
106
+ packedCommandOut <== packedCommand;
107
+ }
@@ -0,0 +1,29 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit imports
4
+ include "./poseidon-cipher.circom";
5
+
6
+ /**
7
+ * Computes the Poseidon hash for an array of n inputs, including a default initial state
8
+ * of zero not counted in n. First, extends the inputs by prepending a zero, creating an array [0, inputs].
9
+ * Then, the Poseidon hash of the extended inputs is calculated, with the first element of the
10
+ * result assigned as the output.
11
+ */
12
+ template PoseidonHasher(n) {
13
+ signal input inputs[n];
14
+ signal output out;
15
+
16
+ // [0, inputs].
17
+ var computedExtendedInputs[n + 1];
18
+ computedExtendedInputs[0] = 0;
19
+
20
+ for (var i = 0; i < n; i++) {
21
+ computedExtendedInputs[i + 1] = inputs[i];
22
+ }
23
+
24
+ // Compute the Poseidon hash of the extended inputs.
25
+ var computedPoseidonPerm[n + 1];
26
+ computedPoseidonPerm = PoseidonPerm(n + 1)(computedExtendedInputs);
27
+
28
+ out <== computedPoseidonPerm[0];
29
+ }