@maci-protocol/circuits 0.0.0-ci.01622be

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 (55) hide show
  1. package/CHANGELOG.md +673 -0
  2. package/LICENSE +21 -0
  3. package/README.md +20 -0
  4. package/build/ts/compile.d.ts +10 -0
  5. package/build/ts/compile.d.ts.map +1 -0
  6. package/build/ts/compile.js +123 -0
  7. package/build/ts/compile.js.map +1 -0
  8. package/build/ts/generateZkeys.d.ts +9 -0
  9. package/build/ts/generateZkeys.d.ts.map +1 -0
  10. package/build/ts/generateZkeys.js +67 -0
  11. package/build/ts/generateZkeys.js.map +1 -0
  12. package/build/ts/info.d.ts +2 -0
  13. package/build/ts/info.d.ts.map +1 -0
  14. package/build/ts/info.js +72 -0
  15. package/build/ts/info.js.map +1 -0
  16. package/build/ts/types.d.ts +104 -0
  17. package/build/ts/types.d.ts.map +1 -0
  18. package/build/ts/types.js +3 -0
  19. package/build/ts/types.js.map +1 -0
  20. package/build/tsconfig.build.tsbuildinfo +1 -0
  21. package/circom/circuits.json +74 -0
  22. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  23. package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
  24. package/circom/coordinator/non-qv/processMessages.circom +447 -0
  25. package/circom/coordinator/non-qv/tallyVotes.circom +238 -0
  26. package/circom/coordinator/qv/processMessages.circom +452 -0
  27. package/circom/coordinator/qv/tallyVotes.circom +279 -0
  28. package/circom/utils/CalculateTotal.circom +24 -0
  29. package/circom/utils/EdDSAPoseidonVerifier.circom +91 -0
  30. package/circom/utils/MessageHasher.circom +57 -0
  31. package/circom/utils/MessageToCommand.circom +107 -0
  32. package/circom/utils/PoseidonHasher.circom +29 -0
  33. package/circom/utils/PrivateToPublicKey.circom +38 -0
  34. package/circom/utils/VerifySignature.circom +39 -0
  35. package/circom/utils/full/MessageValidator.circom +91 -0
  36. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  37. package/circom/utils/non-qv/MessageValidator.circom +91 -0
  38. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +105 -0
  39. package/circom/utils/qv/MessageValidator.circom +97 -0
  40. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +105 -0
  41. package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
  42. package/circom/utils/trees/CheckRoot.circom +49 -0
  43. package/circom/utils/trees/LeafExists.circom +27 -0
  44. package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
  45. package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
  46. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  47. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  48. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  49. package/circom/utils/trees/QuinarySelector.circom +42 -0
  50. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  51. package/circom/utils/trees/Splicer.circom +76 -0
  52. package/circom/voter/PollJoined.circom +43 -0
  53. package/circom/voter/PollJoining.circom +54 -0
  54. package/circomkit.json +18 -0
  55. package/package.json +74 -0
@@ -0,0 +1,452 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./mux1.circom";
5
+ // zk-kit imports
6
+ include "./safe-comparators.circom";
7
+ // local imports
8
+ include "../../utils/PoseidonHasher.circom";
9
+ include "../../utils/MessageHasher.circom";
10
+ include "../../utils/MessageToCommand.circom";
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";
20
+
21
+ /**
22
+ * Proves the correctness of processing a batch of MACI messages.
23
+ * This template supports the Quadratic Voting (QV).
24
+ */
25
+ template ProcessMessages(
26
+ stateTreeDepth,
27
+ batchSize,
28
+ voteOptionTreeDepth
29
+ ) {
30
+ // Must ensure that the trees have a valid structure.
31
+ assert(stateTreeDepth > 0);
32
+ assert(batchSize > 0);
33
+ assert(voteOptionTreeDepth > 0);
34
+
35
+ // Default for IQT (quinary trees).
36
+ var VOTE_OPTION_TREE_ARITY = 5;
37
+ // Default for binary trees.
38
+ var STATE_TREE_ARITY = 2;
39
+ var MESSAGE_LENGTH = 10;
40
+ var PACKED_COMMAND_LENGTH = 4;
41
+ var STATE_LEAF_LENGTH = 3;
42
+ 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
+
52
+ // Number of users that have completed the sign up.
53
+ signal input totalSignups;
54
+ // Value of chainHash at beginning of batch
55
+ signal input inputBatchHash;
56
+ // Value of chainHash at end of batch
57
+ signal input outputBatchHash;
58
+ // The messages.
59
+ signal input messages[batchSize][MESSAGE_LENGTH];
60
+ // The coordinator's private key.
61
+ signal input coordinatorPrivateKey;
62
+ // The ECDH public key per message.
63
+ signal input encryptionPublicKeys[batchSize][2];
64
+ // The current state root (before the processing).
65
+ signal input currentStateRoot;
66
+ // The actual tree depth (might be <= stateTreeDepth).
67
+ // @note it is a public input to ensure fair processing from
68
+ // the coordinator (no censoring)
69
+ signal input actualStateTreeDepth;
70
+ // The coordinator public key hash
71
+ signal input coordinatorPublicKeyHash;
72
+ // The number of valid vote options for the poll.
73
+ signal input voteOptions;
74
+
75
+ // The state leaves upon which messages are applied.
76
+ // transform(currentStateLeaf[4], message5) => newStateLeaf4
77
+ // transform(currentStateLeaf[3], message4) => newStateLeaf3
78
+ // transform(currentStateLeaf[2], message3) => newStateLeaf2
79
+ // transform(currentStateLeaf[1], message1) => newStateLeaf1
80
+ // transform(currentStateLeaf[0], message0) => newStateLeaf0
81
+ // ...
82
+
83
+ signal input currentStateLeaves[batchSize][STATE_LEAF_LENGTH];
84
+ // The Merkle path to each incremental new state root.
85
+ signal input currentStateLeavesPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
86
+ // The salted commitment to the state and ballot roots.
87
+ signal input currentSbCommitment;
88
+ signal input currentSbSalt;
89
+ // The salted commitment to the new state and ballot roots.
90
+ signal input newSbCommitment;
91
+ signal input newSbSalt;
92
+ // The current ballot root before batch processing.
93
+ signal input currentBallotRoot;
94
+ // Intermediate ballots.
95
+ signal input currentBallots[batchSize][BALLOT_LENGTH];
96
+ signal input currentBallotsPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
97
+ // Intermediate vote weights.
98
+ signal input currentVoteWeights[batchSize];
99
+ signal input currentVoteWeightsPathElements[batchSize][voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
100
+
101
+ // nb. The messages are processed in REVERSE order.
102
+ // Therefore, the index of the first message to process does not match the index of the
103
+ // first message (e.g., [msg1, msg2, msg3] => first message to process has index 3).
104
+
105
+ // The index of the first message in the batch, inclusive.
106
+ signal input index;
107
+
108
+ // The index of the last message in the batch to process, exclusive.
109
+ // This value may be less than batchSize if this batch is
110
+ // the last batch and the total number of messages is not a multiple of the batch size.
111
+ signal input batchEndIndex;
112
+
113
+ // The history of state and ballot roots and temporary intermediate
114
+ // signals (for processing purposes).
115
+ signal stateRoots[batchSize + 1];
116
+ signal ballotRoots[batchSize + 1];
117
+
118
+ // Must verify the current sb commitment.
119
+ var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
120
+ computedCurrentSbCommitment === currentSbCommitment;
121
+
122
+ // Ensure that the vote options signal is valid
123
+ var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
124
+ voteOptionsValid === 1;
125
+
126
+ // Check totalSignups <= the max number of users (i.e., number of state leaves
127
+ // that can fit the state tree).
128
+ var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
129
+ totalSignupsValid === 1;
130
+
131
+ // Hash each Message to check their existence in the Message tree.
132
+ var computedMessageHashers[batchSize];
133
+ var computedChainHashes[batchSize];
134
+ var chainHash[batchSize + 1];
135
+ chainHash[0] = inputBatchHash;
136
+
137
+ for (var i = 0; i < batchSize; i++) {
138
+ // calculate message hash
139
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
140
+ // check if message is valid or not (if index of message is less than index of last valid message in batch)
141
+ var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
142
+ // calculate chain hash if message is valid
143
+ computedChainHashes[i] = PoseidonHasher(2)([chainHash[i], computedMessageHashers[i]]);
144
+ // choose between old chain hash value and new chain hash value depending if message is valid or not
145
+ chainHash[i + 1] = Mux1()([chainHash[i], computedChainHashes[i]], batchStartIndexValid);
146
+ }
147
+
148
+ // If batchEndIndex < index + i, the remaining
149
+ // message hashes should be the zero value.
150
+ // e.g. [m, z, z, z, z] if there is only 1 real message in the batch
151
+ // This makes possible to have a batch of messages which is only partially full.
152
+
153
+ chainHash[batchSize] === outputBatchHash;
154
+
155
+ // Decrypt each Message to a Command.
156
+ // MessageToCommand derives the ECDH shared key from the coordinator's
157
+ // private key and the message's ephemeral public key. Next, it uses this
158
+ // shared key to decrypt a Message to a Command.
159
+
160
+ // Ensure that the coordinator's public key from the contract is correct
161
+ // based on the given private key - that is, the prover knows the
162
+ // coordinator's private key.
163
+ var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
164
+ var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
165
+ derivedPublicKeyHash === coordinatorPublicKeyHash;
166
+
167
+ // Decrypt each Message into a Command.
168
+ // The command i-th is composed by the following fields.
169
+ // e.g., command 0 is made of commandsStateIndex[0],
170
+ // commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
171
+ var computedCommandsStateIndex[batchSize];
172
+ var computedCommandsNewPublicKey[batchSize][2];
173
+ var computedCommandsVoteOptionIndex[batchSize];
174
+ var computedCommandsNewVoteWeight[batchSize];
175
+ var computedCommandsNonce[batchSize];
176
+ var computedCommandsPollId[batchSize];
177
+ var computedCommandsSalt[batchSize];
178
+ var computedCommandsSignaturePoint[batchSize][2];
179
+ var computedCommandsSignatureScalar[batchSize];
180
+ var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
181
+
182
+ for (var i = 0; i < batchSize; i++) {
183
+ (
184
+ computedCommandsStateIndex[i],
185
+ computedCommandsNewPublicKey[i],
186
+ computedCommandsVoteOptionIndex[i],
187
+ computedCommandsNewVoteWeight[i],
188
+ computedCommandsNonce[i],
189
+ computedCommandsPollId[i],
190
+ computedCommandsSalt[i],
191
+ computedCommandsSignaturePoint[i],
192
+ computedCommandsSignatureScalar[i],
193
+ computedCommandsPackedCommandOut[i]
194
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
195
+ }
196
+
197
+ // Process messages in reverse order.
198
+ // Assign current state and ballot roots.
199
+ stateRoots[batchSize] <== currentStateRoot;
200
+ ballotRoots[batchSize] <== currentBallotRoot;
201
+
202
+ // Define vote type message processors.
203
+ var computedNewVoteStateRoot[batchSize];
204
+ var computedNewVoteBallotRoot[batchSize];
205
+
206
+ // Start from batchSize and decrement for process in reverse order.
207
+ for (var i = batchSize - 1; i >= 0; i--) {
208
+ // Process as vote type message.
209
+ var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
210
+ var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
211
+ var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
212
+
213
+ for (var j = 0; j < stateTreeDepth; j++) {
214
+ for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
215
+ computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
216
+ computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
217
+ }
218
+ }
219
+
220
+ for (var j = 0; j < voteOptionTreeDepth; j++) {
221
+ for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
222
+ computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
223
+ }
224
+ }
225
+
226
+ (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOne(stateTreeDepth, voteOptionTreeDepth)(
227
+ totalSignups,
228
+ stateRoots[i + 1],
229
+ ballotRoots[i + 1],
230
+ actualStateTreeDepth,
231
+ currentStateLeaves[i],
232
+ computedCurrentStateLeavesPathElements,
233
+ currentBallots[i],
234
+ computedCurrentBallotPathElements,
235
+ currentVoteWeights[i],
236
+ computedCurrentVoteWeightsPathElements,
237
+ computedCommandsStateIndex[i],
238
+ computedCommandsNewPublicKey[i],
239
+ computedCommandsVoteOptionIndex[i],
240
+ computedCommandsNewVoteWeight[i],
241
+ computedCommandsNonce[i],
242
+ computedCommandsPollId[i],
243
+ computedCommandsSalt[i],
244
+ computedCommandsSignaturePoint[i],
245
+ computedCommandsSignatureScalar[i],
246
+ computedCommandsPackedCommandOut[i],
247
+ voteOptions
248
+ );
249
+
250
+ stateRoots[i] <== computedNewVoteStateRoot[i];
251
+ ballotRoots[i] <== computedNewVoteBallotRoot[i];
252
+ }
253
+
254
+ var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
255
+ computedNewSbCommitment === newSbCommitment;
256
+ }
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
+