@maci-protocol/circuits 0.0.0-ci.0bef05d → 0.0.0-ci.0c8f2ee

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 (37) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +2 -2
  3. package/build/ts/types.d.ts +5 -5
  4. package/build/ts/types.d.ts.map +1 -1
  5. package/build/tsconfig.build.tsbuildinfo +1 -1
  6. package/circom/circuits.json +28 -12
  7. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  8. package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
  9. package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +13 -207
  10. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +200 -0
  11. package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +22 -98
  12. package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +13 -214
  13. package/circom/coordinator/qv/SingleMessageProcessor.circom +208 -0
  14. package/circom/coordinator/qv/VoteTally.circom +180 -0
  15. package/circom/utils/CalculateTotal.circom +6 -6
  16. package/circom/utils/PrivateToPublicKey.circom +3 -3
  17. package/circom/utils/full/MessageValidator.circom +91 -0
  18. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  19. package/circom/utils/non-qv/MessageValidator.circom +4 -4
  20. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  21. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +7 -7
  22. package/circom/utils/qv/MessageValidator.circom +4 -4
  23. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  24. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +7 -7
  25. package/circom/utils/trees/BinaryMerkleRoot.circom +1 -1
  26. package/circom/utils/trees/LeafExists.circom +2 -2
  27. package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
  28. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  29. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  30. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  31. package/circom/utils/trees/QuinarySelector.circom +42 -0
  32. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  33. package/circom/utils/trees/Splicer.circom +76 -0
  34. package/circom/voter/PollJoined.circom +2 -2
  35. package/package.json +14 -12
  36. package/circom/coordinator/qv/tallyVotes.circom +0 -279
  37. 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
@@ -119,7 +113,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
119
113
  computedCurrentSbCommitment === currentSbCommitment;
120
114
 
121
115
  // -----------------------------------------------------------------------
122
- // 0. Ensure that the maximum vote options signal is valid and if
116
+ // 0. Ensure that the maximum vote options signal is valid and if
123
117
  // the maximum users signal is valid
124
118
  var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
125
119
  voteOptionsValid === 1;
@@ -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 newBallotVoRoot;
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
- newBallotVoRoot <== 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, newBallotVoRoot]);
438
- var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
439
- computedNewBallot,
440
- computedStateLeafPathIndices,
441
- ballotPathElements
442
- );
443
-
444
- newBallotRoot <== computedNewBallotQip;
445
- }
446
-
@@ -0,0 +1,200 @@
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/MerklePathIndicesGenerator.circom";
9
+ include "../../utils/trees/BinaryMerkleRoot.circom";
10
+ include "../../utils/trees/QuinaryTreeInclusionProof.circom";
11
+ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
12
+ include "../../utils/non-qv/StateLeafAndBallotTransformer.circom";
13
+
14
+
15
+ /**
16
+ * Processes one message and updates the state accordingly.
17
+ * This template involves complex interactions, including transformations based on message type,
18
+ * validations against current states like voice credit balances or vote weights,
19
+ * and updates to Merkle trees representing state and ballot information.
20
+ * This is a critical building block for ensuring the integrity and correctness of MACI state.
21
+ * This template does not support Quadratic Voting (QV).
22
+ */
23
+ template SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth) {
24
+ // Constants defining the structure and size of state and ballots.
25
+ var STATE_LEAF_LENGTH = 3;
26
+ var BALLOT_LENGTH = 2;
27
+ var MESSAGE_LENGTH = 10;
28
+ var PACKED_COMMAND_LENGTH = 4;
29
+ var VOTE_OPTION_TREE_ARITY = 5;
30
+ var STATE_TREE_ARITY = 2;
31
+ var BALLOT_NONCE_INDEX = 0;
32
+ // Ballot vote option (vote option) root index.
33
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
34
+
35
+ // Indices for elements within a state leaf.
36
+ // Public key.
37
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
38
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
39
+ // Voice Credit balance.
40
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
41
+ var NUMBER_BITS = 252;
42
+
43
+ // Number of users that have completed the sign up.
44
+ signal input totalSignups;
45
+ // The current value of the state tree root.
46
+ signal input currentStateRoot;
47
+ // The current value of the ballot tree root.
48
+ signal input currentBallotRoot;
49
+ // The actual tree depth (might be <= stateTreeDepth).
50
+ signal input actualStateTreeDepth;
51
+
52
+ // The state leaf and related path elements.
53
+ signal input stateLeaf[STATE_LEAF_LENGTH];
54
+ // Sibling nodes at each level of the state tree to verify the specific state leaf.
55
+ signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
56
+
57
+ // The ballot and related path elements.
58
+ signal input ballot[BALLOT_LENGTH];
59
+ signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
60
+
61
+ // The current vote weight and related path elements.
62
+ signal input currentVoteWeight;
63
+ signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
64
+
65
+ // Inputs related to the command being processed.
66
+ signal input commandStateIndex;
67
+ signal input commandPublicKey[2];
68
+ signal input commandVoteOptionIndex;
69
+ signal input commandNewVoteWeight;
70
+ signal input commandNonce;
71
+ signal input commandPollId;
72
+ signal input commandSalt;
73
+ signal input commandSignaturePoint[2];
74
+ signal input commandSignatureScalar;
75
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
76
+
77
+ // The number of valid vote options for the poll.
78
+ signal input voteOptions;
79
+
80
+ signal output newStateRoot;
81
+ signal output newBallotRoot;
82
+
83
+ // equal to newBallotVoteOptionRootMux (Mux1).
84
+ signal newBallotVoteOptionRoot;
85
+
86
+ // 1. Transform a state leaf and a ballot with a command.
87
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
88
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
89
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
90
+ totalSignups,
91
+ voteOptions,
92
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
93
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
94
+ ballot[BALLOT_NONCE_INDEX],
95
+ currentVoteWeight,
96
+ commandStateIndex,
97
+ commandPublicKey,
98
+ commandVoteOptionIndex,
99
+ commandNewVoteWeight,
100
+ commandNonce,
101
+ commandPollId,
102
+ commandSalt,
103
+ commandSignaturePoint,
104
+ commandSignatureScalar,
105
+ packedCommand
106
+ );
107
+
108
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
109
+ // Otherwise, generate indices for command.stateIndex.
110
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
111
+ var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
112
+
113
+ // 3. Verify that the original state leaf exists in the given state root.
114
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
115
+ var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
116
+ stateLeafHash,
117
+ actualStateTreeDepth,
118
+ computedStateLeafPathIndices,
119
+ stateLeafPathElements
120
+ );
121
+
122
+ stateLeafQip === currentStateRoot;
123
+
124
+ // 4. Verify that the original ballot exists in the given ballot root.
125
+ var computedBallot = PoseidonHasher(2)([
126
+ ballot[BALLOT_NONCE_INDEX],
127
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
128
+ ]);
129
+
130
+ var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
131
+ computedBallot,
132
+ computedStateLeafPathIndices,
133
+ ballotPathElements
134
+ );
135
+
136
+ computedBallotQip === currentBallotRoot;
137
+
138
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
139
+ // at commandVoteOptionIndex.
140
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
141
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
142
+
143
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
144
+ currentVoteWeight,
145
+ computedCurrentVoteWeightPathIndices,
146
+ currentVoteWeightsPathElements
147
+ );
148
+
149
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
150
+
151
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
152
+ var voiceCreditBalanceMux = Mux1()(
153
+ [
154
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
155
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
156
+ ],
157
+ computedIsValid
158
+ );
159
+
160
+ // 5.1. Update the ballot's vote option root with the new vote weight.
161
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
162
+ voteWeightMux,
163
+ computedCurrentVoteWeightPathIndices,
164
+ currentVoteWeightsPathElements
165
+ );
166
+
167
+ // The new vote option root in the ballot
168
+ var newBallotVoteOptionRootMux = Mux1()(
169
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
170
+ computedIsValid
171
+ );
172
+
173
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
174
+
175
+ // 6. Generate a new state root.
176
+ var computedNewStateLeafHash = PoseidonHasher(3)([
177
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
178
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
179
+ voiceCreditBalanceMux
180
+ ]);
181
+
182
+ var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
183
+ computedNewStateLeafHash,
184
+ actualStateTreeDepth,
185
+ computedStateLeafPathIndices,
186
+ stateLeafPathElements
187
+ );
188
+
189
+ newStateRoot <== computedNewStateLeafQip;
190
+
191
+ // 7. Generate a new ballot root.
192
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
193
+ var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
194
+ computedNewBallot,
195
+ computedStateLeafPathIndices,
196
+ ballotPathElements
197
+ );
198
+
199
+ newBallotRoot <== computedNewBallotQip;
200
+ }
@@ -5,10 +5,11 @@ 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
10
  include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
11
  include "../../utils/trees/LeafExists.circom";
11
- include "../../utils/trees/incrementalQuinaryTree.circom";
12
+ include "../../utils/trees/QuinaryCheckRoot.circom";
12
13
  include "../../utils/CalculateTotal.circom";
13
14
  include "../../utils/PoseidonHasher.circom";
14
15
 
@@ -16,24 +17,24 @@ include "../../utils/PoseidonHasher.circom";
16
17
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
17
18
  * This template does not support Quadratic Voting (QV).
18
19
  */
19
- template TallyVotesNonQv(
20
+ template VoteTallyNonQv(
20
21
  stateTreeDepth,
21
- intStateTreeDepth,
22
+ tallyProcessingStateTreeDepth,
22
23
  voteOptionTreeDepth
23
24
  ) {
24
25
  // Ensure there's at least one level in the vote option tree.
25
26
  assert(voteOptionTreeDepth > 0);
26
27
  // Ensure the intermediate state tree has at least one level.
27
- assert(intStateTreeDepth > 0);
28
+ assert(tallyProcessingStateTreeDepth > 0);
28
29
  // The intermediate state tree must be smaller than the full state tree.
29
- assert(intStateTreeDepth < stateTreeDepth);
30
+ assert(tallyProcessingStateTreeDepth < stateTreeDepth);
30
31
 
31
32
  // Number of children per node in the tree, defining the tree's branching factor.
32
33
  var TREE_ARITY = 5;
33
34
  var BALLOT_TREE_ARITY = 2;
34
35
 
35
36
  // The number of ballots processed at once, determined by the depth of the intermediate state tree.
36
- var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
37
+ var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
37
38
  // Number of voting options available, determined by the depth of the vote option tree.
38
39
  var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
39
40
 
@@ -44,7 +45,7 @@ template TallyVotesNonQv(
44
45
  // Index for the voting option root in the ballot array.
45
46
  var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
46
47
  // Difference in tree depths, used in path calculations.
47
- var STATE_INT_TREE_DEPTH_DIFFERENCE = stateTreeDepth - intStateTreeDepth;
48
+ var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
48
49
 
49
50
  // Root of the state Merkle tree, representing the overall state before voting.
50
51
  signal input stateRoot;
@@ -64,7 +65,7 @@ template TallyVotesNonQv(
64
65
  signal input totalSignups;
65
66
  // Ballots and their corresponding path elements for verification in the tree.
66
67
  signal input ballots[batchSize][BALLOT_LENGTH];
67
- signal input ballotPathElements[STATE_INT_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
+ signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
69
  signal input votes[batchSize][totalVoteOptions];
69
70
  // Current results for each vote option.
70
71
  signal input currentResults[totalVoteOptions];
@@ -95,11 +96,11 @@ template TallyVotesNonQv(
95
96
  computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
96
97
  }
97
98
 
98
- var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
99
- var computedBallotPathIndices[STATE_INT_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_INT_TREE_DEPTH_DIFFERENCE)(index / batchSize);
99
+ var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
100
+ var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
100
101
 
101
102
  // Verifies each ballot's existence within the ballot tree.
102
- LeafExists(STATE_INT_TREE_DEPTH_DIFFERENCE)(
103
+ LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
103
104
  computedBallotSubroot,
104
105
  ballotPathElements,
105
106
  computedBallotPathIndices,
@@ -110,7 +111,7 @@ template TallyVotesNonQv(
110
111
  var computedVoteTree[batchSize];
111
112
 
112
113
  for (var i = 0; i < batchSize; i++) {
113
- computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
114
+ computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
114
115
  computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
115
116
  }
116
117
 
@@ -122,26 +123,27 @@ template TallyVotesNonQv(
122
123
  var computedCalculateTotalResult[totalVoteOptions];
123
124
 
124
125
  for (var i = 0; i < totalVoteOptions; i++) {
125
- var computedNumsRC[batchSize + 1];
126
- computedNumsRC[batchSize] = currentResults[i] * computedIsZero;
126
+ var computedVotes[batchSize + 1];
127
+ computedVotes[batchSize] = currentResults[i] * computedIsZero;
128
+
127
129
  for (var j = 0; j < batchSize; j++) {
128
- computedNumsRC[j] = votes[j][i];
130
+ computedVotes[j] = votes[j][i];
129
131
  }
130
132
 
131
- computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedNumsRC);
133
+ computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
132
134
  }
133
135
 
134
136
  // Tally the new spent voice credit total.
135
- var computedNumsSVC[batchSize * totalVoteOptions + 1];
136
- computedNumsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
137
+ var computedTotalVoiceCreditSpent[batchSize * totalVoteOptions + 1];
138
+ computedTotalVoiceCreditSpent[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
137
139
 
138
140
  for (var i = 0; i < batchSize; i++) {
139
141
  for (var j = 0; j < totalVoteOptions; j++) {
140
- computedNumsSVC[i * totalVoteOptions + j] = votes[i][j];
142
+ computedTotalVoiceCreditSpent[i * totalVoteOptions + j] = votes[i][j];
141
143
  }
142
144
  }
143
145
 
144
- var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedNumsSVC);
146
+ var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedTotalVoiceCreditSpent);
145
147
 
146
148
  // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
147
149
  ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
@@ -158,81 +160,3 @@ template TallyVotesNonQv(
158
160
  newSpentVoiceCreditSubtotalSalt
159
161
  );
160
162
  }
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
-