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