@maci-protocol/circuits 0.0.0-ci.4c6d4e8 → 0.0.0-ci.52ce07e

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 (48) 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 -11
  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 +110 -112
  13. package/circom/coordinator/non-qv/tallyVotes.circom +49 -46
  14. package/circom/coordinator/qv/processMessages.circom +111 -110
  15. package/circom/coordinator/qv/tallyVotes.circom +63 -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 +11 -3
  30. package/circom/utils/trees/CheckRoot.circom +18 -14
  31. package/circom/utils/trees/LeafExists.circom +3 -3
  32. package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +11 -7
  33. package/circom/utils/trees/MerkleTreeInclusionProof.circom +10 -9
  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 +15 -12
  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/incrementalQuinaryTree.circom +0 -287
  48. package/circom/voter/poll.circom +0 -92
@@ -6,11 +6,11 @@ include "./comparators.circom";
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
8
  include "../../utils/trees/CheckRoot.circom";
9
- include "../../utils/trees/MerkleGeneratePathIndices.circom";
9
+ include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
10
  include "../../utils/trees/LeafExists.circom";
11
- include "../../utils/trees/incrementalQuinaryTree.circom";
12
- include "../../utils/calculateTotal.circom";
13
- include "../../utils/hashers.circom";
11
+ include "../../utils/trees/QuinaryCheckRoot.circom";
12
+ include "../../utils/CalculateTotal.circom";
13
+ include "../../utils/PoseidonHasher.circom";
14
14
 
15
15
  /**
16
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
@@ -18,33 +18,33 @@ include "../../utils/hashers.circom";
18
18
  */
19
19
  template TallyVotes(
20
20
  stateTreeDepth,
21
- intStateTreeDepth,
21
+ tallyProcessingStateTreeDepth,
22
22
  voteOptionTreeDepth
23
23
  ) {
24
24
  // Ensure there's at least one level in the vote option tree.
25
25
  assert(voteOptionTreeDepth > 0);
26
26
  // Ensure the intermediate state tree has at least one level.
27
- assert(intStateTreeDepth > 0);
27
+ assert(tallyProcessingStateTreeDepth > 0);
28
28
  // The intermediate state tree must be smaller than the full state tree.
29
- assert(intStateTreeDepth < stateTreeDepth);
29
+ assert(tallyProcessingStateTreeDepth < stateTreeDepth);
30
30
 
31
31
  // Number of children per node in the tree, defining the tree's branching factor.
32
32
  var TREE_ARITY = 5;
33
33
  var BALLOT_TREE_ARITY = 2;
34
34
 
35
35
  // The number of ballots processed at once, determined by the depth of the intermediate state tree.
36
- var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
36
+ var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
37
37
  // Number of voting options available, determined by the depth of the vote option tree.
38
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
38
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
39
39
 
40
40
  // Number of elements in each ballot.
41
41
  var BALLOT_LENGTH = 2;
42
42
  // Index for the nonce in the ballot array.
43
- var BALLOT_NONCE_IDX = 0;
43
+ var BALLOT_NONCE_INDEX = 0;
44
44
  // Index for the voting option root in the ballot array.
45
- var BALLOT_VO_ROOT_IDX = 1;
45
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
46
46
  // Difference in tree depths, used in path calculations.
47
- var k = stateTreeDepth - intStateTreeDepth;
47
+ var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
48
48
 
49
49
  // Root of the state Merkle tree, representing the overall state before voting.
50
50
  signal input stateRoot;
@@ -61,13 +61,13 @@ template TallyVotes(
61
61
  // Start index of given batch
62
62
  signal input index;
63
63
  // Number of users that signup
64
- signal input numSignUps;
64
+ signal input totalSignups;
65
65
  // Ballots and their corresponding path elements for verification in the tree.
66
66
  signal input ballots[batchSize][BALLOT_LENGTH];
67
- signal input ballotPathElements[k][BALLOT_TREE_ARITY - 1];
68
- signal input votes[batchSize][numVoteOptions];
67
+ signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
+ signal input votes[batchSize][totalVoteOptions];
69
69
  // Current results for each vote option.
70
- signal input currentResults[numVoteOptions];
70
+ signal input currentResults[totalVoteOptions];
71
71
  // Salt for the root of the current results.
72
72
  signal input currentResultsRootSalt;
73
73
  // Total voice credits spent so far.
@@ -75,13 +75,13 @@ template TallyVotes(
75
75
  // Salt for the total spent voice credits.
76
76
  signal input currentSpentVoiceCreditSubtotalSalt;
77
77
  // Spent voice credits per vote option.
78
- signal input currentPerVOSpentVoiceCredits[numVoteOptions];
78
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
79
79
  // Salt for the root of spent credits per option.
80
- signal input currentPerVOSpentVoiceCreditsRootSalt;
80
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
81
81
  // Salt for the root of the new results.
82
82
  signal input newResultsRootSalt;
83
83
  // Salt for the new spent credits per vote option root.
84
- signal input newPerVOSpentVoiceCreditsRootSalt;
84
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
85
85
  // Salt for the new total spent voice credits root.
86
86
  signal input newSpentVoiceCreditSubtotalSalt;
87
87
 
@@ -90,21 +90,21 @@ template TallyVotes(
90
90
  computedSbCommitment === sbCommitment;
91
91
 
92
92
  // Validates that the index is within the valid range of sign-ups.
93
- var numSignUpsValid = LessEqThan(50)([index, numSignUps]);
94
- numSignUpsValid === 1;
93
+ var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
94
+ totalSignupsValid === 1;
95
95
 
96
96
  // Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
97
97
  var computedBallotHashers[batchSize];
98
98
 
99
99
  for (var i = 0; i < batchSize; i++) {
100
- 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]]);
101
101
  }
102
102
 
103
- var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
104
- 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);
105
105
 
106
106
  // Verifies each ballot's existence within the ballot tree.
107
- LeafExists(k)(
107
+ LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
108
108
  computedBallotSubroot,
109
109
  ballotPathElements,
110
110
  computedBallotPathIndices,
@@ -114,8 +114,8 @@ template TallyVotes(
114
114
  // Processes vote options, verifying each against its declared root.
115
115
  var computedVoteTree[batchSize];
116
116
  for (var i = 0; i < batchSize; i++) {
117
- computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
118
- 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];
119
119
  }
120
120
 
121
121
  // Calculates new results and spent voice credits based on the current and incoming votes.
@@ -123,8 +123,8 @@ template TallyVotes(
123
123
  var computedIsZero = IsZero()(computedIsFirstBatch);
124
124
 
125
125
  // Tally the new results.
126
- var computedCalculateTotalResult[numVoteOptions];
127
- for (var i = 0; i < numVoteOptions; i++) {
126
+ var computedCalculateTotalResult[totalVoteOptions];
127
+ for (var i = 0; i < totalVoteOptions; i++) {
128
128
  var numsRC[batchSize + 1];
129
129
  numsRC[batchSize] = currentResults[i] * computedIsZero;
130
130
  for (var j = 0; j < batchSize; j++) {
@@ -135,27 +135,27 @@ template TallyVotes(
135
135
  }
136
136
 
137
137
  // Tally the new spent voice credit total.
138
- var numsSVC[batchSize * numVoteOptions + 1];
139
- numsSVC[batchSize * numVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
138
+ var numsSVC[batchSize * totalVoteOptions + 1];
139
+ numsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
140
140
  for (var i = 0; i < batchSize; i++) {
141
- for (var j = 0; j < numVoteOptions; j++) {
142
- 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];
143
143
  }
144
144
  }
145
145
 
146
- var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * numVoteOptions + 1)(numsSVC);
146
+ var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(numsSVC);
147
147
 
148
148
  // Tally the spent voice credits per vote option.
149
- var computedNewPerVOSpentVoiceCredits[numVoteOptions];
149
+ var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
150
150
 
151
- for (var i = 0; i < numVoteOptions; i++) {
152
- var computedNumsSVC[batchSize + 1];
153
- computedNumsSVC[batchSize] = currentPerVOSpentVoiceCredits[i] * computedIsZero;
151
+ for (var i = 0; i < totalVoteOptions; i++) {
152
+ var computedTotalVoiceCreditSpent[batchSize + 1];
153
+ computedTotalVoiceCreditSpent[batchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
154
154
  for (var j = 0; j < batchSize; j++) {
155
- computedNumsSVC[j] = votes[j][i] * votes[j][i];
155
+ computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
156
156
  }
157
157
 
158
- computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedNumsSVC);
158
+ computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedTotalVoiceCreditSpent);
159
159
  }
160
160
 
161
161
  // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
@@ -171,10 +171,10 @@ template TallyVotes(
171
171
  currentSpentVoiceCreditSubtotalSalt,
172
172
  computedNewSpentVoiceCreditSubtotal,
173
173
  newSpentVoiceCreditSubtotalSalt,
174
- currentPerVOSpentVoiceCredits,
175
- currentPerVOSpentVoiceCreditsRootSalt,
174
+ currentPerVoteOptionSpentVoiceCredits,
175
+ currentPerVoteOptionSpentVoiceCreditsRootSalt,
176
176
  computedNewPerVOSpentVoiceCredits,
177
- newPerVOSpentVoiceCreditsRootSalt
177
+ newPerVoteOptionSpentVoiceCreditsRootSalt
178
178
  );
179
179
  }
180
180
 
@@ -187,7 +187,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
187
187
  // Number of children per node in the tree, defining the tree's branching factor.
188
188
  var TREE_ARITY = 5;
189
189
  // Number of voting options available, determined by the depth of the vote option tree.
190
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
190
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
191
191
 
192
192
  // Equal to 1 if this is the first batch, otherwise 0.
193
193
  signal input isFirstBatch;
@@ -197,12 +197,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
197
197
  signal input newTallyCommitment;
198
198
 
199
199
  // Current results for each vote option.
200
- signal input currentResults[numVoteOptions];
200
+ signal input currentResults[totalVoteOptions];
201
201
  // Salt for the root of the current results.
202
202
  signal input currentResultsRootSalt;
203
203
 
204
204
  // New results for each vote option.
205
- signal input newResults[numVoteOptions];
205
+ signal input newResults[totalVoteOptions];
206
206
  // Salt for the root of the new results.
207
207
  signal input newResultsRootSalt;
208
208
 
@@ -217,17 +217,17 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
217
217
  signal input newSpentVoiceCreditSubtotalSalt;
218
218
 
219
219
  // Spent voice credits per vote option.
220
- signal input currentPerVOSpentVoiceCredits[numVoteOptions];
220
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
221
221
  // Salt for the root of spent credits per option.
222
- signal input currentPerVOSpentVoiceCreditsRootSalt;
222
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
223
223
 
224
224
  // New spent voice credits per vote option.
225
- signal input newPerVOSpentVoiceCredits[numVoteOptions];
225
+ signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
226
226
  // Salt for the root of new spent credits per option.
227
- signal input newPerVOSpentVoiceCreditsRootSalt;
227
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
228
228
 
229
229
  // Compute the commitment to the current results.
230
- var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
230
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
231
231
 
232
232
  // Verify currentResultsCommitmentHash.
233
233
  var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
@@ -236,14 +236,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
236
236
  var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
237
237
 
238
238
  // Compute the root of the spent voice credits per vote option.
239
- var computedCurrentPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentPerVOSpentVoiceCredits);
240
- var computedCurrentPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVOSpentVoiceCreditsRoot, currentPerVOSpentVoiceCreditsRootSalt]);
239
+ var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
240
+ var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
241
241
 
242
242
  // Commit to the current tally.
243
243
  var computedCurrentTallyCommitment = PoseidonHasher(3)([
244
244
  computedCurrentResultsCommitment,
245
245
  computedCurrentSpentVoiceCreditsCommitment,
246
- computedCurrentPerVOSpentVoiceCreditsCommitment
246
+ computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
247
247
  ]);
248
248
 
249
249
  // Check if the current tally commitment is correct only if this is not the first batch.
@@ -251,28 +251,28 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
251
251
  // computedIsZero.out is 0 if this is the first batch.
252
252
  var computedIsZero = IsZero()(isFirstBatch);
253
253
 
254
- // hz is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
255
- // hz is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
256
- signal hz;
257
- hz <== computedIsZero * computedCurrentTallyCommitment;
258
- 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;
259
259
 
260
260
  // Compute the root of the new results.
261
- var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
261
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
262
262
  var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
263
263
 
264
264
  // Compute the commitment to the new spent voice credits value.
265
265
  var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
266
266
 
267
267
  // Compute the root of the spent voice credits per vote option.
268
- var computedNewPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(newPerVOSpentVoiceCredits);
269
- var computedNewPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVOSpentVoiceCreditsRoot, newPerVOSpentVoiceCreditsRootSalt]);
268
+ var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
269
+ var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
270
270
 
271
271
  // Commit to the new tally.
272
272
  var computedNewTallyCommitment = PoseidonHasher(3)([
273
273
  computedNewResultsCommitment,
274
274
  computedNewSpentVoiceCreditsCommitment,
275
- computedNewPerVOSpentVoiceCreditsCommitment
275
+ computedNewPerVoteOptionSpentVoiceCreditsCommitment
276
276
  ]);
277
277
 
278
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
+ }