@maci-protocol/circuits 0.0.0-ci.ffabe48 → 3.0.0

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