@maci-protocol/circuits 0.0.0-ci.2653bc0

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 (39) hide show
  1. package/CHANGELOG.md +673 -0
  2. package/LICENSE +22 -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/genZkeys.d.ts +9 -0
  9. package/build/ts/genZkeys.d.ts.map +1 -0
  10. package/build/ts/genZkeys.js +67 -0
  11. package/build/ts/genZkeys.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 +105 -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 +58 -0
  22. package/circom/coordinator/non-qv/processMessages.circom +447 -0
  23. package/circom/coordinator/non-qv/tallyVotes.circom +232 -0
  24. package/circom/coordinator/qv/processMessages.circom +449 -0
  25. package/circom/coordinator/qv/tallyVotes.circom +277 -0
  26. package/circom/utils/calculateTotal.circom +22 -0
  27. package/circom/utils/hashers.circom +78 -0
  28. package/circom/utils/messageToCommand.circom +78 -0
  29. package/circom/utils/non-qv/messageValidator.circom +89 -0
  30. package/circom/utils/non-qv/stateLeafAndBallotTransformer.circom +105 -0
  31. package/circom/utils/privToPubKey.circom +36 -0
  32. package/circom/utils/qv/messageValidator.circom +95 -0
  33. package/circom/utils/qv/stateLeafAndBallotTransformer.circom +105 -0
  34. package/circom/utils/trees/incrementalMerkleTree.circom +198 -0
  35. package/circom/utils/trees/incrementalQuinaryTree.circom +287 -0
  36. package/circom/utils/verifySignature.circom +117 -0
  37. package/circom/voter/poll.circom +93 -0
  38. package/circomkit.json +18 -0
  39. package/package.json +71 -0
@@ -0,0 +1,58 @@
1
+ {
2
+ "PollJoining_10_test": {
3
+ "file": "./voter/poll",
4
+ "template": "PollJoining",
5
+ "params": [10],
6
+ "pubs": ["nullifier", "stateRoot", "pollPubKey", "pollId"]
7
+ },
8
+ "PollJoined_10_test": {
9
+ "file": "./voter/poll",
10
+ "template": "PollJoined",
11
+ "params": [10],
12
+ "pubs": ["stateRoot"]
13
+ },
14
+ "ProcessMessages_10-20-2_test": {
15
+ "file": "./coordinator/qv/processMessages",
16
+ "template": "ProcessMessages",
17
+ "params": [10, 20, 2],
18
+ "pubs": [
19
+ "numSignUps",
20
+ "index",
21
+ "batchEndIndex",
22
+ "currentSbCommitment",
23
+ "newSbCommitment",
24
+ "outputBatchHash",
25
+ "actualStateTreeDepth",
26
+ "coordinatorPublicKeyHash",
27
+ "voteOptions"
28
+ ]
29
+ },
30
+ "ProcessMessagesNonQv_10-20-2_test": {
31
+ "file": "./coordinator/non-qv/processMessages",
32
+ "template": "ProcessMessagesNonQv",
33
+ "params": [10, 20, 2],
34
+ "pubs": [
35
+ "numSignUps",
36
+ "index",
37
+ "batchEndIndex",
38
+ "currentSbCommitment",
39
+ "newSbCommitment",
40
+ "outputBatchHash",
41
+ "actualStateTreeDepth",
42
+ "coordinatorPublicKeyHash",
43
+ "voteOptions"
44
+ ]
45
+ },
46
+ "TallyVotes_10-1-2_test": {
47
+ "file": "./coordinator/qv/tallyVotes",
48
+ "template": "TallyVotes",
49
+ "params": [10, 1, 2],
50
+ "pubs": ["index", "numSignUps", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
51
+ },
52
+ "TallyVotesNonQv_10-1-2_test": {
53
+ "file": "./coordinator/non-qv/tallyVotes",
54
+ "template": "TallyVotesNonQv",
55
+ "params": [10, 1, 2],
56
+ "pubs": ["index", "numSignUps", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
57
+ }
58
+ }
@@ -0,0 +1,447 @@
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/hashers.circom";
9
+ include "../../utils/messageToCommand.circom";
10
+ include "../../utils/privToPubKey.circom";
11
+ include "../../utils/non-qv/stateLeafAndBallotTransformer.circom";
12
+ include "../../utils/trees/incrementalMerkleTree.circom";
13
+ include "../../utils/trees/incrementalQuinaryTree.circom";
14
+
15
+ /**
16
+ * Proves the correctness of processing a batch of MACI messages.
17
+ * This template does not support Quadratic Voting (QV).
18
+ */
19
+ template ProcessMessagesNonQv(
20
+ stateTreeDepth,
21
+ batchSize,
22
+ voteOptionTreeDepth
23
+ ) {
24
+ // Must ensure that the trees have a valid structure.
25
+ assert(stateTreeDepth > 0);
26
+ assert(batchSize > 0);
27
+ assert(voteOptionTreeDepth > 0);
28
+
29
+ // Default for IQT (quinary trees).
30
+ var VOTE_OPTION_TREE_ARITY = 5;
31
+ // Default for Binary trees.
32
+ var STATE_TREE_ARITY = 2;
33
+ var MSG_LENGTH = 10;
34
+ var PACKED_CMD_LENGTH = 4;
35
+ var STATE_LEAF_LENGTH = 4;
36
+ var BALLOT_LENGTH = 2;
37
+ var BALLOT_NONCE_IDX = 0;
38
+ var BALLOT_VO_ROOT_IDX = 1;
39
+ var STATE_LEAF_PUB_X_IDX = 0;
40
+ var STATE_LEAF_PUB_Y_IDX = 1;
41
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
42
+ var STATE_LEAF_TIMESTAMP_IDX = 3;
43
+ var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
44
+ // Number of options for this poll.
45
+ var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
46
+
47
+ // Number of users that have completed the sign up.
48
+ signal input numSignUps;
49
+ // Value of chainHash at beginning of batch
50
+ signal input inputBatchHash;
51
+ // Value of chainHash at end of batch
52
+ signal input outputBatchHash;
53
+ // The messages.
54
+ signal input msgs[batchSize][MSG_LENGTH];
55
+ // The coordinator's private key.
56
+ signal input coordPrivKey;
57
+ // The ECDH public key per message.
58
+ signal input encPubKeys[batchSize][2];
59
+ // The current state root (before the processing).
60
+ signal input currentStateRoot;
61
+ // The actual tree depth (might be <= stateTreeDepth).
62
+ // @note it is a public input to ensure fair processing from
63
+ // the coordinator (no censoring)
64
+ signal input actualStateTreeDepth;
65
+ // The coordinator public key hash
66
+ signal input coordinatorPublicKeyHash;
67
+ // The number of valid vote options for the poll.
68
+ signal input voteOptions;
69
+
70
+ // The state leaves upon which messages are applied.
71
+ // transform(currentStateLeaf[4], message5) => newStateLeaf4
72
+ // transform(currentStateLeaf[3], message4) => newStateLeaf3
73
+ // transform(currentStateLeaf[2], message3) => newStateLeaf2
74
+ // transform(currentStateLeaf[1], message1) => newStateLeaf1
75
+ // transform(currentStateLeaf[0], message0) => newStateLeaf0
76
+ // ...
77
+
78
+ signal input currentStateLeaves[batchSize][STATE_LEAF_LENGTH];
79
+ // The Merkle path to each incremental new state root.
80
+ signal input currentStateLeavesPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
81
+ // The salted commitment to the state and ballot roots.
82
+ signal input currentSbCommitment;
83
+ signal input currentSbSalt;
84
+ // The salted commitment to the new state and ballot roots.
85
+ signal input newSbCommitment;
86
+ signal input newSbSalt;
87
+ // The current ballot root before batch processing.
88
+ signal input currentBallotRoot;
89
+ // Intermediate ballots.
90
+ signal input currentBallots[batchSize][BALLOT_LENGTH];
91
+ signal input currentBallotsPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
92
+ // Intermediate vote weights.
93
+ signal input currentVoteWeights[batchSize];
94
+ signal input currentVoteWeightsPathElements[batchSize][voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
95
+
96
+ // nb. The messages are processed in REVERSE order.
97
+ // Therefore, the index of the first message to process does not match the index of the
98
+ // first message (e.g., [msg1, msg2, msg3] => first message to process has index 3).
99
+
100
+ // The index of the first message in the batch, inclusive.
101
+ signal input index;
102
+
103
+ // The index of the last message in the batch to process, exclusive.
104
+ // This value may be less than index + batchSize if this batch is
105
+ // the last batch and the total number of messages is not a multiple of the batch size.
106
+ signal input batchEndIndex;
107
+
108
+ // The history of state and ballot roots and temporary intermediate
109
+ // signals (for processing purposes).
110
+ signal stateRoots[batchSize + 1];
111
+ signal ballotRoots[batchSize + 1];
112
+ signal tmpStateRoot1[batchSize];
113
+ signal tmpStateRoot2[batchSize];
114
+ signal tmpBallotRoot1[batchSize];
115
+ signal tmpBallotRoot2[batchSize];
116
+
117
+ // Must verify the current sb commitment.
118
+ var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
119
+ computedCurrentSbCommitment === currentSbCommitment;
120
+
121
+ // -----------------------------------------------------------------------
122
+ // 0. Ensure that the maximum vote options signal is valid and if
123
+ // the maximum users signal is valid
124
+ var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
125
+ voteOptionsValid === 1;
126
+
127
+ // Check numSignUps <= the max number of users (i.e., number of state leaves
128
+ // that can fit the state tree).
129
+ var numSignUpsValid = LessEqThan(32)([numSignUps, STATE_TREE_ARITY ** stateTreeDepth]);
130
+ numSignUpsValid === 1;
131
+
132
+ // Hash each Message to check their existence in the Message chain hash.
133
+ var computedMessageHashers[batchSize];
134
+ var computedChainHashes[batchSize];
135
+ var chainHash[batchSize + 1];
136
+ chainHash[0] = inputBatchHash;
137
+ for (var i = 0; i < batchSize; i++) {
138
+ // calculate message hash
139
+ computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[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
+ // Ensure that right output batch hash was sent to circuit
154
+ chainHash[batchSize] === outputBatchHash;
155
+
156
+ // Decrypt each Message to a Command.
157
+ // MessageToCommand derives the ECDH shared key from the coordinator's
158
+ // private key and the message's ephemeral public key. Next, it uses this
159
+ // shared key to decrypt a Message to a Command.
160
+
161
+ // Ensure that the coordinator's public key from the contract is correct
162
+ // based on the given private key - that is, the prover knows the
163
+ // coordinator's private key.
164
+ var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
165
+ var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
166
+ derivedPubKeyHash === coordinatorPublicKeyHash;
167
+
168
+ // Decrypt each Message into a Command.
169
+ // The command i-th is composed by the following fields.
170
+ // e.g., command 0 is made of commandsStateIndex[0],
171
+ // commandsNewPubKey[0], ..., commandsPackedCommandOut[0]
172
+ var computedCommandsStateIndex[batchSize];
173
+ var computedCommandsNewPubKey[batchSize][2];
174
+ var computedCommandsVoteOptionIndex[batchSize];
175
+ var computedCommandsNewVoteWeight[batchSize];
176
+ var computedCommandsNonce[batchSize];
177
+ var computedCommandsPollId[batchSize];
178
+ var computedCommandsSalt[batchSize];
179
+ var computedCommandsSigR8[batchSize][2];
180
+ var computedCommandsSigS[batchSize];
181
+ var computedCommandsPackedCommandOut[batchSize][PACKED_CMD_LENGTH];
182
+
183
+ for (var i = 0; i < batchSize; i++) {
184
+ (
185
+ computedCommandsStateIndex[i],
186
+ computedCommandsNewPubKey[i],
187
+ computedCommandsVoteOptionIndex[i],
188
+ computedCommandsNewVoteWeight[i],
189
+ computedCommandsNonce[i],
190
+ computedCommandsPollId[i],
191
+ computedCommandsSalt[i],
192
+ computedCommandsSigR8[i],
193
+ computedCommandsSigS[i],
194
+ computedCommandsPackedCommandOut[i]
195
+ ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
196
+ }
197
+
198
+ // Process messages in reverse order.
199
+ // Assign current state and ballot roots.
200
+ stateRoots[batchSize] <== currentStateRoot;
201
+ ballotRoots[batchSize] <== currentBallotRoot;
202
+
203
+ // Define vote type message processors.
204
+ var computedNewVoteStateRoot[batchSize];
205
+ var computedNewVoteBallotRoot[batchSize];
206
+
207
+ // Start from batchSize and decrement for process in reverse order.
208
+ for (var i = batchSize - 1; i >= 0; i--) {
209
+ // Process as vote type message.
210
+ var currentStateLeavesPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
211
+ var currentBallotPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
212
+ var currentVoteWeightsPathElement[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
213
+
214
+ for (var j = 0; j < stateTreeDepth; j++) {
215
+ for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
216
+ currentStateLeavesPathElement[j][k] = currentStateLeavesPathElements[i][j][k];
217
+ currentBallotPathElement[j][k] = currentBallotsPathElements[i][j][k];
218
+ }
219
+ }
220
+
221
+ for (var j = 0; j < voteOptionTreeDepth; j++) {
222
+ for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
223
+ currentVoteWeightsPathElement[j][k] = currentVoteWeightsPathElements[i][j][k];
224
+ }
225
+ }
226
+
227
+ (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
228
+ numSignUps,
229
+ stateRoots[i + 1],
230
+ ballotRoots[i + 1],
231
+ actualStateTreeDepth,
232
+ currentStateLeaves[i],
233
+ currentStateLeavesPathElement,
234
+ currentBallots[i],
235
+ currentBallotPathElement,
236
+ currentVoteWeights[i],
237
+ currentVoteWeightsPathElement,
238
+ computedCommandsStateIndex[i],
239
+ computedCommandsNewPubKey[i],
240
+ computedCommandsVoteOptionIndex[i],
241
+ computedCommandsNewVoteWeight[i],
242
+ computedCommandsNonce[i],
243
+ computedCommandsPollId[i],
244
+ computedCommandsSalt[i],
245
+ computedCommandsSigR8[i],
246
+ computedCommandsSigS[i],
247
+ computedCommandsPackedCommandOut[i],
248
+ voteOptions
249
+ );
250
+
251
+ stateRoots[i] <== computedNewVoteStateRoot[i];
252
+ ballotRoots[i] <== computedNewVoteBallotRoot[i];
253
+ }
254
+
255
+ var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
256
+ computedNewSbCommitment === newSbCommitment;
257
+ }
258
+
259
+ /**
260
+ * Processes one message and updates the state accordingly.
261
+ * This template involves complex interactions, including transformations based on message type,
262
+ * validations against current states like voice credit balances or vote weights,
263
+ * and updates to Merkle trees representing state and ballot information.
264
+ * This is a critical building block for ensuring the integrity and correctness of MACI state.
265
+ * This template does not support Quadratic Voting (QV).
266
+ */
267
+ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
268
+ // Constants defining the structure and size of state and ballots.
269
+ var STATE_LEAF_LENGTH = 4;
270
+ var BALLOT_LENGTH = 2;
271
+ var MSG_LENGTH = 10;
272
+ var PACKED_CMD_LENGTH = 4;
273
+ var VOTE_OPTION_TREE_ARITY = 5;
274
+ var STATE_TREE_ARITY = 2;
275
+ var BALLOT_NONCE_IDX = 0;
276
+ // Ballot vote option (VO) root index.
277
+ var BALLOT_VO_ROOT_IDX = 1;
278
+
279
+ // Indices for elements within a state leaf.
280
+ // Public key.
281
+ var STATE_LEAF_PUB_X_IDX = 0;
282
+ var STATE_LEAF_PUB_Y_IDX = 1;
283
+ // Voice Credit balance.
284
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
285
+ // Timestamp.
286
+ var STATE_LEAF_TIMESTAMP_IDX = 3;
287
+ var N_BITS = 252;
288
+
289
+ // Number of users that have completed the sign up.
290
+ signal input numSignUps;
291
+ // The current value of the state tree root.
292
+ signal input currentStateRoot;
293
+ // The current value of the ballot tree root.
294
+ signal input currentBallotRoot;
295
+ // The actual tree depth (might be <= stateTreeDepth).
296
+ signal input actualStateTreeDepth;
297
+
298
+ // The state leaf and related path elements.
299
+ signal input stateLeaf[STATE_LEAF_LENGTH];
300
+ // Sibling nodes at each level of the state tree to verify the specific state leaf.
301
+ signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
302
+
303
+ // The ballot and related path elements.
304
+ signal input ballot[BALLOT_LENGTH];
305
+ signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
306
+
307
+ // The current vote weight and related path elements.
308
+ signal input currentVoteWeight;
309
+ signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
310
+
311
+ // Inputs related to the command being processed.
312
+ signal input cmdStateIndex;
313
+ signal input cmdNewPubKey[2];
314
+ signal input cmdVoteOptionIndex;
315
+ signal input cmdNewVoteWeight;
316
+ signal input cmdNonce;
317
+ signal input cmdPollId;
318
+ signal input cmdSalt;
319
+ signal input cmdSigR8[2];
320
+ signal input cmdSigS;
321
+ signal input packedCmd[PACKED_CMD_LENGTH];
322
+
323
+ // The number of valid vote options for the poll.
324
+ signal input voteOptions;
325
+
326
+ signal output newStateRoot;
327
+ signal output newBallotRoot;
328
+
329
+ // equal to newBallotVoRootMux (Mux1).
330
+ signal newBallotVoRoot;
331
+
332
+ // 1. Transform a state leaf and a ballot with a command.
333
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
334
+ var computedNewSlPubKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
335
+ (computedNewSlPubKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
336
+ numSignUps,
337
+ voteOptions,
338
+ [stateLeaf[STATE_LEAF_PUB_X_IDX], stateLeaf[STATE_LEAF_PUB_Y_IDX]],
339
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
340
+ ballot[BALLOT_NONCE_IDX],
341
+ currentVoteWeight,
342
+ cmdStateIndex,
343
+ cmdNewPubKey,
344
+ cmdVoteOptionIndex,
345
+ cmdNewVoteWeight,
346
+ cmdNonce,
347
+ cmdPollId,
348
+ cmdSalt,
349
+ cmdSigR8,
350
+ cmdSigS,
351
+ packedCmd
352
+ );
353
+
354
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
355
+ // Otherwise, generate indices for command.stateIndex.
356
+ var stateIndexMux = Mux1()([0, cmdStateIndex], computedIsStateLeafIndexValid);
357
+ var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
358
+
359
+ // 3. Verify that the original state leaf exists in the given state root.
360
+ var stateLeafHash = PoseidonHasher(4)(stateLeaf);
361
+ var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
362
+ stateLeafHash,
363
+ actualStateTreeDepth,
364
+ computedStateLeafPathIndices,
365
+ stateLeafPathElements
366
+ );
367
+
368
+ stateLeafQip === currentStateRoot;
369
+
370
+ // 4. Verify that the original ballot exists in the given ballot root.
371
+ var computedBallot = PoseidonHasher(2)([
372
+ ballot[BALLOT_NONCE_IDX],
373
+ ballot[BALLOT_VO_ROOT_IDX]
374
+ ]);
375
+
376
+ var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
377
+ computedBallot,
378
+ computedStateLeafPathIndices,
379
+ ballotPathElements
380
+ );
381
+
382
+ computedBallotQip === currentBallotRoot;
383
+
384
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
385
+ // at cmdVoteOptionIndex.
386
+ var cmdVoteOptionIndexMux = Mux1()([0, cmdVoteOptionIndex], computedIsVoteOptionIndexValid);
387
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(cmdVoteOptionIndexMux);
388
+
389
+ var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
390
+ currentVoteWeight,
391
+ computedCurrentVoteWeightPathIndices,
392
+ currentVoteWeightsPathElements
393
+ );
394
+
395
+ computedCurrentVoteWeightQip === ballot[BALLOT_VO_ROOT_IDX];
396
+
397
+ var voteWeightMux = Mux1()([currentVoteWeight, cmdNewVoteWeight], computedIsValid);
398
+ var voiceCreditBalanceMux = Mux1()(
399
+ [
400
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
401
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] + currentVoteWeight - cmdNewVoteWeight
402
+ ],
403
+ computedIsValid
404
+ );
405
+
406
+ // 5.1. Update the ballot's vote option root with the new vote weight.
407
+ var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
408
+ voteWeightMux,
409
+ computedCurrentVoteWeightPathIndices,
410
+ currentVoteWeightsPathElements
411
+ );
412
+
413
+ // The new vote option root in the ballot
414
+ var newBallotVoRootMux = Mux1()(
415
+ [ballot[BALLOT_VO_ROOT_IDX], computedNewVoteOptionTreeQip],
416
+ computedIsValid
417
+ );
418
+
419
+ newBallotVoRoot <== newBallotVoRootMux;
420
+
421
+ // 6. Generate a new state root.
422
+ var computedNewStateLeafhash = PoseidonHasher(4)([
423
+ computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
424
+ computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
425
+ voiceCreditBalanceMux,
426
+ stateLeaf[STATE_LEAF_TIMESTAMP_IDX]
427
+ ]);
428
+
429
+ var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
430
+ computedNewStateLeafhash,
431
+ actualStateTreeDepth,
432
+ computedStateLeafPathIndices,
433
+ stateLeafPathElements
434
+ );
435
+
436
+ newStateRoot <== computedNewStateLeafQip;
437
+
438
+ // 7. Generate a new ballot root.
439
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoRoot]);
440
+ var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
441
+ computedNewBallot,
442
+ computedStateLeafPathIndices,
443
+ ballotPathElements
444
+ );
445
+
446
+ newBallotRoot <== computedNewBallotQip;
447
+ }