@maci-protocol/circuits 0.0.0-ci.2d2f5fb → 0.0.0-ci.2d57ba7

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 (33) 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 +253 -0
  7. package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
  8. package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +12 -206
  9. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +199 -0
  10. package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +18 -95
  11. package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +13 -214
  12. package/circom/coordinator/qv/SingleMessageProcessor.circom +207 -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/full/MessageValidator.circom +2 -4
  17. package/circom/utils/non-qv/MessageValidator.circom +2 -2
  18. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  19. package/circom/utils/qv/MessageValidator.circom +2 -2
  20. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  21. package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
  22. package/circom/utils/trees/LeafExists.circom +2 -2
  23. package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
  24. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  25. package/circom/utils/trees/{MerklePathIndicesGenerator.circom → QuinaryGeneratePathIndices.circom} +18 -18
  26. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  27. package/circom/utils/trees/QuinarySelector.circom +42 -0
  28. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  29. package/circom/utils/trees/Splicer.circom +76 -0
  30. package/circom/voter/PollJoined.circom +3 -3
  31. package/circom/voter/PollJoining.circom +3 -3
  32. package/package.json +16 -16
  33. package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
@@ -9,19 +9,13 @@ include "../../utils/PoseidonHasher.circom";
9
9
  include "../../utils/MessageHasher.circom";
10
10
  include "../../utils/MessageToCommand.circom";
11
11
  include "../../utils/PrivateToPublicKey.circom";
12
- include "../../utils/non-qv/StateLeafAndBallotTransformer.circom";
13
- include "../../utils/trees/MerkleTreeInclusionProof.circom";
14
- include "../../utils/trees/LeafExists.circom";
15
- include "../../utils/trees/CheckRoot.circom";
16
- include "../../utils/trees/MerklePathIndicesGenerator.circom";
17
- include "../../utils/trees/BinaryMerkleRoot.circom";
18
- include "../../utils/trees/incrementalQuinaryTree.circom";
12
+ include "./SingleMessageProcessor.circom";
19
13
 
20
14
  /**
21
15
  * Proves the correctness of processing a batch of MACI messages.
22
16
  * This template does not support Quadratic Voting (QV).
23
17
  */
24
- template ProcessMessagesNonQv(
18
+ template MessageProcessorNonQv(
25
19
  stateTreeDepth,
26
20
  batchSize,
27
21
  voteOptionTreeDepth
@@ -208,34 +202,34 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
208
202
  // Start from batchSize and decrement for process in reverse order.
209
203
  for (var i = batchSize - 1; i >= 0; i--) {
210
204
  // Process as vote type message.
211
- var currentStateLeavesPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
212
- var currentBallotPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
213
- var currentVoteWeightsPathElement[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
205
+ var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
206
+ var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
207
+ var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
214
208
 
215
209
  for (var j = 0; j < stateTreeDepth; j++) {
216
210
  for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
217
- currentStateLeavesPathElement[j][k] = currentStateLeavesPathElements[i][j][k];
218
- currentBallotPathElement[j][k] = currentBallotsPathElements[i][j][k];
211
+ computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
212
+ computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
219
213
  }
220
214
  }
221
215
 
222
216
  for (var j = 0; j < voteOptionTreeDepth; j++) {
223
217
  for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
224
- currentVoteWeightsPathElement[j][k] = currentVoteWeightsPathElements[i][j][k];
218
+ computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
225
219
  }
226
220
  }
227
221
 
228
- (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
222
+ (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth)(
229
223
  totalSignups,
230
224
  stateRoots[i + 1],
231
225
  ballotRoots[i + 1],
232
226
  actualStateTreeDepth,
233
227
  currentStateLeaves[i],
234
- currentStateLeavesPathElement,
228
+ computedCurrentStateLeavesPathElements,
235
229
  currentBallots[i],
236
- currentBallotPathElement,
230
+ computedCurrentBallotPathElements,
237
231
  currentVoteWeights[i],
238
- currentVoteWeightsPathElement,
232
+ computedCurrentVoteWeightsPathElements,
239
233
  computedCommandsStateIndex[i],
240
234
  computedCommandsNewPublicKey[i],
241
235
  computedCommandsVoteOptionIndex[i],
@@ -256,191 +250,3 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
256
250
  var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
257
251
  computedNewSbCommitment === newSbCommitment;
258
252
  }
259
-
260
- /**
261
- * Processes one message and updates the state accordingly.
262
- * This template involves complex interactions, including transformations based on message type,
263
- * validations against current states like voice credit balances or vote weights,
264
- * and updates to Merkle trees representing state and ballot information.
265
- * This is a critical building block for ensuring the integrity and correctness of MACI state.
266
- * This template does not support Quadratic Voting (QV).
267
- */
268
- template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
269
- // Constants defining the structure and size of state and ballots.
270
- var STATE_LEAF_LENGTH = 3;
271
- var BALLOT_LENGTH = 2;
272
- var MESSAGE_LENGTH = 10;
273
- var PACKED_COMMAND_LENGTH = 4;
274
- var VOTE_OPTION_TREE_ARITY = 5;
275
- var STATE_TREE_ARITY = 2;
276
- var BALLOT_NONCE_INDEX = 0;
277
- // Ballot vote option (vote option) root index.
278
- var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
279
-
280
- // Indices for elements within a state leaf.
281
- // Public key.
282
- var STATE_LEAF_PUBLIC_X_INDEX = 0;
283
- var STATE_LEAF_PUBLIC_Y_INDEX = 1;
284
- // Voice Credit balance.
285
- var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
286
- var NUMBER_BITS = 252;
287
-
288
- // Number of users that have completed the sign up.
289
- signal input totalSignups;
290
- // The current value of the state tree root.
291
- signal input currentStateRoot;
292
- // The current value of the ballot tree root.
293
- signal input currentBallotRoot;
294
- // The actual tree depth (might be <= stateTreeDepth).
295
- signal input actualStateTreeDepth;
296
-
297
- // The state leaf and related path elements.
298
- signal input stateLeaf[STATE_LEAF_LENGTH];
299
- // Sibling nodes at each level of the state tree to verify the specific state leaf.
300
- signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
301
-
302
- // The ballot and related path elements.
303
- signal input ballot[BALLOT_LENGTH];
304
- signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
305
-
306
- // The current vote weight and related path elements.
307
- signal input currentVoteWeight;
308
- signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
309
-
310
- // Inputs related to the command being processed.
311
- signal input commandStateIndex;
312
- signal input commandPublicKey[2];
313
- signal input commandVoteOptionIndex;
314
- signal input commandNewVoteWeight;
315
- signal input commandNonce;
316
- signal input commandPollId;
317
- signal input commandSalt;
318
- signal input commandSignaturePoint[2];
319
- signal input commandSignatureScalar;
320
- signal input packedCommand[PACKED_COMMAND_LENGTH];
321
-
322
- // The number of valid vote options for the poll.
323
- signal input voteOptions;
324
-
325
- signal output newStateRoot;
326
- signal output newBallotRoot;
327
-
328
- // equal to newBallotVoteOptionRootMux (Mux1).
329
- signal newBallotVoteOptionRoot;
330
-
331
- // 1. Transform a state leaf and a ballot with a command.
332
- // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
333
- var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
334
- (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
335
- totalSignups,
336
- voteOptions,
337
- [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
338
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
339
- ballot[BALLOT_NONCE_INDEX],
340
- currentVoteWeight,
341
- commandStateIndex,
342
- commandPublicKey,
343
- commandVoteOptionIndex,
344
- commandNewVoteWeight,
345
- commandNonce,
346
- commandPollId,
347
- commandSalt,
348
- commandSignaturePoint,
349
- commandSignatureScalar,
350
- packedCommand
351
- );
352
-
353
- // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
354
- // Otherwise, generate indices for command.stateIndex.
355
- var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
356
- var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
357
-
358
- // 3. Verify that the original state leaf exists in the given state root.
359
- var stateLeafHash = PoseidonHasher(3)(stateLeaf);
360
- var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
361
- stateLeafHash,
362
- actualStateTreeDepth,
363
- computedStateLeafPathIndices,
364
- stateLeafPathElements
365
- );
366
-
367
- stateLeafQip === currentStateRoot;
368
-
369
- // 4. Verify that the original ballot exists in the given ballot root.
370
- var computedBallot = PoseidonHasher(2)([
371
- ballot[BALLOT_NONCE_INDEX],
372
- ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
373
- ]);
374
-
375
- var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
376
- computedBallot,
377
- computedStateLeafPathIndices,
378
- ballotPathElements
379
- );
380
-
381
- computedBallotQip === currentBallotRoot;
382
-
383
- // 5. Verify that currentVoteWeight exists in the ballot's vote option root
384
- // at commandVoteOptionIndex.
385
- var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
386
- var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
387
-
388
- var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
389
- currentVoteWeight,
390
- computedCurrentVoteWeightPathIndices,
391
- currentVoteWeightsPathElements
392
- );
393
-
394
- computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
395
-
396
- var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
397
- var voiceCreditBalanceMux = Mux1()(
398
- [
399
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
400
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
401
- ],
402
- computedIsValid
403
- );
404
-
405
- // 5.1. Update the ballot's vote option root with the new vote weight.
406
- var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
407
- voteWeightMux,
408
- computedCurrentVoteWeightPathIndices,
409
- currentVoteWeightsPathElements
410
- );
411
-
412
- // The new vote option root in the ballot
413
- var newBallotVoteOptionRootMux = Mux1()(
414
- [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
415
- computedIsValid
416
- );
417
-
418
- newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
419
-
420
- // 6. Generate a new state root.
421
- var computedNewStateLeafHash = PoseidonHasher(3)([
422
- computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
423
- computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
424
- voiceCreditBalanceMux
425
- ]);
426
-
427
- var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
428
- computedNewStateLeafHash,
429
- actualStateTreeDepth,
430
- computedStateLeafPathIndices,
431
- stateLeafPathElements
432
- );
433
-
434
- newStateRoot <== computedNewStateLeafQip;
435
-
436
- // 7. Generate a new ballot root.
437
- var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
438
- var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
439
- computedNewBallot,
440
- computedStateLeafPathIndices,
441
- ballotPathElements
442
- );
443
-
444
- newBallotRoot <== computedNewBallotQip;
445
- }
446
-
@@ -0,0 +1,199 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./mux1.circom";
5
+ // local imports
6
+ include "../../utils/PoseidonHasher.circom";
7
+ include "../../utils/trees/MerkleTreeInclusionProof.circom";
8
+ include "../../utils/trees/BinaryMerkleRoot.circom";
9
+ include "../../utils/trees/QuinaryTreeInclusionProof.circom";
10
+ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
11
+ include "../../utils/non-qv/StateLeafAndBallotTransformer.circom";
12
+
13
+
14
+ /**
15
+ * Processes one message and updates the state accordingly.
16
+ * This template involves complex interactions, including transformations based on message type,
17
+ * validations against current states like voice credit balances or vote weights,
18
+ * and updates to Merkle trees representing state and ballot information.
19
+ * This is a critical building block for ensuring the integrity and correctness of MACI state.
20
+ * This template does not support Quadratic Voting (QV).
21
+ */
22
+ template SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth) {
23
+ // Constants defining the structure and size of state and ballots.
24
+ var STATE_LEAF_LENGTH = 3;
25
+ var BALLOT_LENGTH = 2;
26
+ var MESSAGE_LENGTH = 10;
27
+ var PACKED_COMMAND_LENGTH = 4;
28
+ var VOTE_OPTION_TREE_ARITY = 5;
29
+ var STATE_TREE_ARITY = 2;
30
+ var BALLOT_NONCE_INDEX = 0;
31
+ // Ballot vote option (vote option) root index.
32
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
33
+
34
+ // Indices for elements within a state leaf.
35
+ // Public key.
36
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
37
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
38
+ // Voice Credit balance.
39
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
40
+ var NUMBER_BITS = 252;
41
+
42
+ // Number of users that have completed the sign up.
43
+ signal input totalSignups;
44
+ // The current value of the state tree root.
45
+ signal input currentStateRoot;
46
+ // The current value of the ballot tree root.
47
+ signal input currentBallotRoot;
48
+ // The actual tree depth (might be <= stateTreeDepth).
49
+ signal input actualStateTreeDepth;
50
+
51
+ // The state leaf and related path elements.
52
+ signal input stateLeaf[STATE_LEAF_LENGTH];
53
+ // Sibling nodes at each level of the state tree to verify the specific state leaf.
54
+ signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
55
+
56
+ // The ballot and related path elements.
57
+ signal input ballot[BALLOT_LENGTH];
58
+ signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
59
+
60
+ // The current vote weight and related path elements.
61
+ signal input currentVoteWeight;
62
+ signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
63
+
64
+ // Inputs related to the command being processed.
65
+ signal input commandStateIndex;
66
+ signal input commandPublicKey[2];
67
+ signal input commandVoteOptionIndex;
68
+ signal input commandNewVoteWeight;
69
+ signal input commandNonce;
70
+ signal input commandPollId;
71
+ signal input commandSalt;
72
+ signal input commandSignaturePoint[2];
73
+ signal input commandSignatureScalar;
74
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
75
+
76
+ // The number of valid vote options for the poll.
77
+ signal input voteOptions;
78
+
79
+ signal output newStateRoot;
80
+ signal output newBallotRoot;
81
+
82
+ // equal to newBallotVoteOptionRootMux (Mux1).
83
+ signal newBallotVoteOptionRoot;
84
+
85
+ // 1. Transform a state leaf and a ballot with a command.
86
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
87
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
88
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
89
+ totalSignups,
90
+ voteOptions,
91
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
92
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
93
+ ballot[BALLOT_NONCE_INDEX],
94
+ currentVoteWeight,
95
+ commandStateIndex,
96
+ commandPublicKey,
97
+ commandVoteOptionIndex,
98
+ commandNewVoteWeight,
99
+ commandNonce,
100
+ commandPollId,
101
+ commandSalt,
102
+ commandSignaturePoint,
103
+ commandSignatureScalar,
104
+ packedCommand
105
+ );
106
+
107
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
108
+ // Otherwise, generate indices for command.stateIndex.
109
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
110
+
111
+ // 3. Verify that the original state leaf exists in the given state root.
112
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
113
+ var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
114
+ stateLeafHash,
115
+ actualStateTreeDepth,
116
+ stateIndexMux,
117
+ stateLeafPathElements
118
+ );
119
+
120
+ stateLeafQip === currentStateRoot;
121
+
122
+ // 4. Verify that the original ballot exists in the given ballot root.
123
+ var computedBallot = PoseidonHasher(2)([
124
+ ballot[BALLOT_NONCE_INDEX],
125
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
126
+ ]);
127
+ var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
128
+
129
+ var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
130
+ computedBallot,
131
+ computedStateLeafPathIndices,
132
+ ballotPathElements
133
+ );
134
+
135
+ computedBallotQip === currentBallotRoot;
136
+
137
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
138
+ // at commandVoteOptionIndex.
139
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
140
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
141
+
142
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
143
+ currentVoteWeight,
144
+ computedCurrentVoteWeightPathIndices,
145
+ currentVoteWeightsPathElements
146
+ );
147
+
148
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
149
+
150
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
151
+ var voiceCreditBalanceMux = Mux1()(
152
+ [
153
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
154
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
155
+ ],
156
+ computedIsValid
157
+ );
158
+
159
+ // 5.1. Update the ballot's vote option root with the new vote weight.
160
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
161
+ voteWeightMux,
162
+ computedCurrentVoteWeightPathIndices,
163
+ currentVoteWeightsPathElements
164
+ );
165
+
166
+ // The new vote option root in the ballot
167
+ var newBallotVoteOptionRootMux = Mux1()(
168
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
169
+ computedIsValid
170
+ );
171
+
172
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
173
+
174
+ // 6. Generate a new state root.
175
+ var computedNewStateLeafHash = PoseidonHasher(3)([
176
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
177
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
178
+ voiceCreditBalanceMux
179
+ ]);
180
+
181
+ var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
182
+ computedNewStateLeafHash,
183
+ actualStateTreeDepth,
184
+ stateIndexMux,
185
+ stateLeafPathElements
186
+ );
187
+
188
+ newStateRoot <== computedNewStateLeafQip;
189
+
190
+ // 7. Generate a new ballot root.
191
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
192
+ var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
193
+ computedNewBallot,
194
+ computedStateLeafPathIndices,
195
+ ballotPathElements
196
+ );
197
+
198
+ newBallotRoot <== computedNewBallotQip;
199
+ }
@@ -5,10 +5,10 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
+ include "../../utils/non-qv/ResultCommitmentVerifier.circom";
8
9
  include "../../utils/trees/CheckRoot.circom";
9
- include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
10
  include "../../utils/trees/LeafExists.circom";
11
- include "../../utils/trees/incrementalQuinaryTree.circom";
11
+ include "../../utils/trees/QuinaryCheckRoot.circom";
12
12
  include "../../utils/CalculateTotal.circom";
13
13
  include "../../utils/PoseidonHasher.circom";
14
14
 
@@ -16,24 +16,24 @@ include "../../utils/PoseidonHasher.circom";
16
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
17
17
  * This template does not support Quadratic Voting (QV).
18
18
  */
19
- template TallyVotesNonQv(
19
+ template VoteTallyNonQv(
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
38
  var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
39
39
 
@@ -44,7 +44,7 @@ template TallyVotesNonQv(
44
44
  // Index for the voting option root in the ballot array.
45
45
  var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
46
46
  // Difference in tree depths, used in path calculations.
47
- var STATE_INT_TREE_DEPTH_DIFFERENCE = 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;
@@ -64,7 +64,7 @@ template TallyVotesNonQv(
64
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[STATE_INT_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
67
+ signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
68
  signal input votes[batchSize][totalVoteOptions];
69
69
  // Current results for each vote option.
70
70
  signal input currentResults[totalVoteOptions];
@@ -95,11 +95,11 @@ template TallyVotesNonQv(
95
95
  computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
96
96
  }
97
97
 
98
- var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
99
- var computedBallotPathIndices[STATE_INT_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_INT_TREE_DEPTH_DIFFERENCE)(index / batchSize);
98
+ var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
99
+ var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
100
100
 
101
101
  // Verifies each ballot's existence within the ballot tree.
102
- LeafExists(STATE_INT_TREE_DEPTH_DIFFERENCE)(
102
+ LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
103
103
  computedBallotSubroot,
104
104
  ballotPathElements,
105
105
  computedBallotPathIndices,
@@ -110,7 +110,7 @@ template TallyVotesNonQv(
110
110
  var computedVoteTree[batchSize];
111
111
 
112
112
  for (var i = 0; i < batchSize; i++) {
113
- computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
113
+ computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
114
114
  computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
115
115
  }
116
116
 
@@ -122,13 +122,14 @@ template TallyVotesNonQv(
122
122
  var computedCalculateTotalResult[totalVoteOptions];
123
123
 
124
124
  for (var i = 0; i < totalVoteOptions; i++) {
125
- var computedNumsRC[batchSize + 1];
126
- computedNumsRC[batchSize] = currentResults[i] * computedIsZero;
125
+ var computedVotes[batchSize + 1];
126
+ computedVotes[batchSize] = currentResults[i] * computedIsZero;
127
+
127
128
  for (var j = 0; j < batchSize; j++) {
128
- computedNumsRC[j] = votes[j][i];
129
+ computedVotes[j] = votes[j][i];
129
130
  }
130
131
 
131
- computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedNumsRC);
132
+ computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
132
133
  }
133
134
 
134
135
  // Tally the new spent voice credit total.
@@ -158,81 +159,3 @@ template TallyVotesNonQv(
158
159
  newSpentVoiceCreditSubtotalSalt
159
160
  );
160
161
  }
161
-
162
- /**
163
- * Performs verifications and computations related to current voting results.
164
- * Also, computes and outputs a commitment to the new results.
165
- * This template does not support Quadratic Voting (QV).
166
- */
167
- template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
168
- // Number of children per node in the tree, defining the tree's branching factor.
169
- var TREE_ARITY = 5;
170
- // Number of voting options available, determined by the depth of the vote option tree.
171
- var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
172
-
173
- // Equal to 1 if this is the first batch, otherwise 0.
174
- signal input isFirstBatch;
175
- // Commitment to the current tally before this batch.
176
- signal input currentTallyCommitment;
177
- // Commitment to the new tally after processing this batch.
178
- signal input newTallyCommitment;
179
-
180
- // Current results for each vote option.
181
- signal input currentResults[totalVoteOptions];
182
- // Salt for the root of the current results.
183
- signal input currentResultsRootSalt;
184
-
185
- // New results for each vote option.
186
- signal input newResults[totalVoteOptions];
187
- // Salt for the root of the new results.
188
- signal input newResultsRootSalt;
189
-
190
- // Total voice credits spent so far.
191
- signal input currentSpentVoiceCreditSubtotal;
192
- // Salt for the total spent voice credits.
193
- signal input currentSpentVoiceCreditSubtotalSalt;
194
-
195
- // Total new voice credits spent.
196
- signal input newSpentVoiceCreditSubtotal;
197
- // Salt for the new total spent voice credits.
198
- signal input newSpentVoiceCreditSubtotalSalt;
199
-
200
- // Compute the commitment to the current results.
201
- var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
202
-
203
- // Verify currentResultsCommitmentHash.
204
- var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
205
-
206
- // Compute the commitment to the current spent voice credits.
207
- var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
208
-
209
- // Commit to the current tally
210
- var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
211
-
212
- // Check if the current tally commitment is correct only if this is not the first batch.
213
- // computedIsZero.out is 1 if this is not the first batch.
214
- // computedIsZero.out is 0 if this is the first batch.
215
- var computedIsZero = IsZero()(isFirstBatch);
216
-
217
- // isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
218
- // isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
219
- signal isFirstCommitment;
220
- isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
221
- isFirstCommitment === currentTallyCommitment;
222
-
223
- // Compute the root of the new results.
224
- var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
225
- var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
226
-
227
- // Compute the commitment to the new spent voice credits value.
228
- var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
229
-
230
- // Commit to the new tally.
231
- var computedNewTallyCommitment = PoseidonHasher(2)([
232
- computedNewResultsCommitment,
233
- computedNewSpentVoiceCreditsCommitment
234
- ]);
235
-
236
- computedNewTallyCommitment === newTallyCommitment;
237
- }
238
-