@maci-protocol/circuits 0.0.0-ci.752ce71 → 0.0.0-ci.75ef321

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.
@@ -11,9 +11,9 @@
11
11
  "params": [10],
12
12
  "pubs": ["stateRoot"]
13
13
  },
14
- "ProcessMessages_10-20-2_test": {
15
- "file": "./coordinator/qv/processMessages",
16
- "template": "ProcessMessages",
14
+ "MessageProcessorQv_10-20-2_test": {
15
+ "file": "./coordinator/qv/MessageProcessor",
16
+ "template": "MessageProcessorQv",
17
17
  "params": [10, 20, 2],
18
18
  "pubs": [
19
19
  "totalSignups",
@@ -27,9 +27,9 @@
27
27
  "voteOptions"
28
28
  ]
29
29
  },
30
- "ProcessMessagesNonQv_10-20-2_test": {
31
- "file": "./coordinator/non-qv/processMessages",
32
- "template": "ProcessMessagesNonQv",
30
+ "MessageProcessorNonQv_10-20-2_test": {
31
+ "file": "./coordinator/non-qv/MessageProcessor",
32
+ "template": "MessageProcessorNonQv",
33
33
  "params": [10, 20, 2],
34
34
  "pubs": [
35
35
  "totalSignups",
@@ -59,15 +59,15 @@
59
59
  "voteOptions"
60
60
  ]
61
61
  },
62
- "TallyVotes_10-1-2_test": {
63
- "file": "./coordinator/qv/tallyVotes",
64
- "template": "TallyVotes",
62
+ "VoteTallyQv_10-1-2_test": {
63
+ "file": "./coordinator/qv/VoteTally",
64
+ "template": "VoteTallyQv",
65
65
  "params": [10, 1, 2],
66
66
  "pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
67
67
  },
68
- "TallyVotesNonQv_10-1-2_test": {
69
- "file": "./coordinator/non-qv/tallyVotes",
70
- "template": "TallyVotesNonQv",
68
+ "VoteTallyNonQv_10-1-2_test": {
69
+ "file": "./coordinator/non-qv/VoteTally",
70
+ "template": "VoteTallyNonQv",
71
71
  "params": [10, 1, 2],
72
72
  "pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
73
73
  }
@@ -5,7 +5,6 @@ include "./mux1.circom";
5
5
  // local imports
6
6
  include "../../utils/PoseidonHasher.circom";
7
7
  include "../../utils/trees/MerkleTreeInclusionProof.circom";
8
- include "../../utils/trees/MerklePathIndicesGenerator.circom";
9
8
  include "../../utils/trees/BinaryMerkleRoot.circom";
10
9
  include "../../utils/trees/QuinaryTreeInclusionProof.circom";
11
10
  include "../../utils/trees/QuinaryGeneratePathIndices.circom";
@@ -112,14 +111,13 @@ template SingleMessageProcessorFull(stateTreeDepth, voteOptionTreeDepth) {
112
111
  // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
113
112
  // Otherwise, generate indices for command.stateIndex.
114
113
  var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
115
- var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
116
114
 
117
115
  // 3. Verify that the original state leaf exists in the given state root.
118
116
  var stateLeafHash = PoseidonHasher(3)(stateLeaf);
119
117
  var computedStateRoot = BinaryMerkleRoot(stateTreeDepth)(
120
118
  stateLeafHash,
121
119
  actualStateTreeDepth,
122
- computedStateLeafPathIndices,
120
+ stateIndexMux,
123
121
  stateLeafPathElements
124
122
  );
125
123
 
@@ -130,6 +128,7 @@ template SingleMessageProcessorFull(stateTreeDepth, voteOptionTreeDepth) {
130
128
  ballot[BALLOT_NONCE_INDEX],
131
129
  ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
132
130
  ]);
131
+ var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
133
132
 
134
133
  var computedBallotRoot = MerkleTreeInclusionProof(stateTreeDepth)(
135
134
  computedBallot,
@@ -186,7 +185,7 @@ template SingleMessageProcessorFull(stateTreeDepth, voteOptionTreeDepth) {
186
185
  var computedNewStateRoot = BinaryMerkleRoot(stateTreeDepth)(
187
186
  computedNewStateLeafhash,
188
187
  actualStateTreeDepth,
189
- computedStateLeafPathIndices,
188
+ stateIndexMux,
190
189
  stateLeafPathElements
191
190
  );
192
191
 
@@ -9,20 +9,13 @@ include "../../utils/PoseidonHasher.circom";
9
9
  include "../../utils/MessageHasher.circom";
10
10
  include "../../utils/MessageToCommand.circom";
11
11
  include "../../utils/PrivateToPublicKey.circom";
12
- include "../../utils/non-qv/StateLeafAndBallotTransformer.circom";
13
- include "../../utils/trees/MerkleTreeInclusionProof.circom";
14
- include "../../utils/trees/LeafExists.circom";
15
- include "../../utils/trees/CheckRoot.circom";
16
- include "../../utils/trees/MerklePathIndicesGenerator.circom";
17
- include "../../utils/trees/BinaryMerkleRoot.circom";
18
- include "../../utils/trees/QuinaryTreeInclusionProof.circom";
19
- include "../../utils/trees/QuinaryGeneratePathIndices.circom";
12
+ include "./SingleMessageProcessor.circom";
20
13
 
21
14
  /**
22
15
  * Proves the correctness of processing a batch of MACI messages.
23
16
  * This template does not support Quadratic Voting (QV).
24
17
  */
25
- template ProcessMessagesNonQv(
18
+ template MessageProcessorNonQv(
26
19
  stateTreeDepth,
27
20
  batchSize,
28
21
  voteOptionTreeDepth
@@ -226,7 +219,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
226
219
  }
227
220
  }
228
221
 
229
- (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
222
+ (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth)(
230
223
  totalSignups,
231
224
  stateRoots[i + 1],
232
225
  ballotRoots[i + 1],
@@ -257,191 +250,3 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
257
250
  var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
258
251
  computedNewSbCommitment === newSbCommitment;
259
252
  }
260
-
261
- /**
262
- * Processes one message and updates the state accordingly.
263
- * This template involves complex interactions, including transformations based on message type,
264
- * validations against current states like voice credit balances or vote weights,
265
- * and updates to Merkle trees representing state and ballot information.
266
- * This is a critical building block for ensuring the integrity and correctness of MACI state.
267
- * This template does not support Quadratic Voting (QV).
268
- */
269
- template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
270
- // Constants defining the structure and size of state and ballots.
271
- var STATE_LEAF_LENGTH = 3;
272
- var BALLOT_LENGTH = 2;
273
- var MESSAGE_LENGTH = 10;
274
- var PACKED_COMMAND_LENGTH = 4;
275
- var VOTE_OPTION_TREE_ARITY = 5;
276
- var STATE_TREE_ARITY = 2;
277
- var BALLOT_NONCE_INDEX = 0;
278
- // Ballot vote option (vote option) root index.
279
- var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
280
-
281
- // Indices for elements within a state leaf.
282
- // Public key.
283
- var STATE_LEAF_PUBLIC_X_INDEX = 0;
284
- var STATE_LEAF_PUBLIC_Y_INDEX = 1;
285
- // Voice Credit balance.
286
- var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
287
- var NUMBER_BITS = 252;
288
-
289
- // Number of users that have completed the sign up.
290
- signal input totalSignups;
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 commandStateIndex;
313
- signal input commandPublicKey[2];
314
- signal input commandVoteOptionIndex;
315
- signal input commandNewVoteWeight;
316
- signal input commandNonce;
317
- signal input commandPollId;
318
- signal input commandSalt;
319
- signal input commandSignaturePoint[2];
320
- signal input commandSignatureScalar;
321
- signal input packedCommand[PACKED_COMMAND_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 newBallotVoteOptionRootMux (Mux1).
330
- signal newBallotVoteOptionRoot;
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 computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
335
- (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
336
- totalSignups,
337
- voteOptions,
338
- [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
339
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
340
- ballot[BALLOT_NONCE_INDEX],
341
- currentVoteWeight,
342
- commandStateIndex,
343
- commandPublicKey,
344
- commandVoteOptionIndex,
345
- commandNewVoteWeight,
346
- commandNonce,
347
- commandPollId,
348
- commandSalt,
349
- commandSignaturePoint,
350
- commandSignatureScalar,
351
- packedCommand
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, commandStateIndex], computedIsStateLeafIndexValid);
357
- var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
358
-
359
- // 3. Verify that the original state leaf exists in the given state root.
360
- var stateLeafHash = PoseidonHasher(3)(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_INDEX],
373
- ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
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 commandVoteOptionIndex.
386
- var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
387
- var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
388
-
389
- var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
390
- currentVoteWeight,
391
- computedCurrentVoteWeightPathIndices,
392
- currentVoteWeightsPathElements
393
- );
394
-
395
- computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
396
-
397
- var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
398
- var voiceCreditBalanceMux = Mux1()(
399
- [
400
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
401
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
402
- ],
403
- computedIsValid
404
- );
405
-
406
- // 5.1. Update the ballot's vote option root with the new vote weight.
407
- var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
408
- voteWeightMux,
409
- computedCurrentVoteWeightPathIndices,
410
- currentVoteWeightsPathElements
411
- );
412
-
413
- // The new vote option root in the ballot
414
- var newBallotVoteOptionRootMux = Mux1()(
415
- [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
416
- computedIsValid
417
- );
418
-
419
- newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
420
-
421
- // 6. Generate a new state root.
422
- var computedNewStateLeafHash = PoseidonHasher(3)([
423
- computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
424
- computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
425
- voiceCreditBalanceMux
426
- ]);
427
-
428
- var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
429
- computedNewStateLeafHash,
430
- actualStateTreeDepth,
431
- computedStateLeafPathIndices,
432
- stateLeafPathElements
433
- );
434
-
435
- newStateRoot <== computedNewStateLeafQip;
436
-
437
- // 7. Generate a new ballot root.
438
- var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
439
- var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
440
- computedNewBallot,
441
- computedStateLeafPathIndices,
442
- ballotPathElements
443
- );
444
-
445
- newBallotRoot <== computedNewBallotQip;
446
- }
447
-
@@ -0,0 +1,199 @@
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/non-qv/StateLeafAndBallotTransformer.circom";
12
+
13
+
14
+ /**
15
+ * Processes one message and updates the state accordingly.
16
+ * This template involves complex interactions, including transformations based on message type,
17
+ * validations against current states like voice credit balances or vote weights,
18
+ * and updates to Merkle trees representing state and ballot information.
19
+ * This is a critical building block for ensuring the integrity and correctness of MACI state.
20
+ * This template does not support Quadratic Voting (QV).
21
+ */
22
+ template SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth) {
23
+ // Constants defining the structure and size of state and ballots.
24
+ var STATE_LEAF_LENGTH = 3;
25
+ var BALLOT_LENGTH = 2;
26
+ var MESSAGE_LENGTH = 10;
27
+ var PACKED_COMMAND_LENGTH = 4;
28
+ var VOTE_OPTION_TREE_ARITY = 5;
29
+ var STATE_TREE_ARITY = 2;
30
+ var BALLOT_NONCE_INDEX = 0;
31
+ // Ballot vote option (vote option) root index.
32
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
33
+
34
+ // Indices for elements within a state leaf.
35
+ // Public key.
36
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
37
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
38
+ // Voice Credit balance.
39
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
40
+ var NUMBER_BITS = 252;
41
+
42
+ // Number of users that have completed the sign up.
43
+ signal input totalSignups;
44
+ // The current value of the state tree root.
45
+ signal input currentStateRoot;
46
+ // The current value of the ballot tree root.
47
+ signal input currentBallotRoot;
48
+ // The actual tree depth (might be <= stateTreeDepth).
49
+ signal input actualStateTreeDepth;
50
+
51
+ // The state leaf and related path elements.
52
+ signal input stateLeaf[STATE_LEAF_LENGTH];
53
+ // Sibling nodes at each level of the state tree to verify the specific state leaf.
54
+ signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
55
+
56
+ // The ballot and related path elements.
57
+ signal input ballot[BALLOT_LENGTH];
58
+ signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
59
+
60
+ // The current vote weight and related path elements.
61
+ signal input currentVoteWeight;
62
+ signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
63
+
64
+ // Inputs related to the command being processed.
65
+ signal input commandStateIndex;
66
+ signal input commandPublicKey[2];
67
+ signal input commandVoteOptionIndex;
68
+ signal input commandNewVoteWeight;
69
+ signal input commandNonce;
70
+ signal input commandPollId;
71
+ signal input commandSalt;
72
+ signal input commandSignaturePoint[2];
73
+ signal input commandSignatureScalar;
74
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
75
+
76
+ // The number of valid vote options for the poll.
77
+ signal input voteOptions;
78
+
79
+ signal output newStateRoot;
80
+ signal output newBallotRoot;
81
+
82
+ // equal to newBallotVoteOptionRootMux (Mux1).
83
+ signal newBallotVoteOptionRoot;
84
+
85
+ // 1. Transform a state leaf and a ballot with a command.
86
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
87
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
88
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
89
+ totalSignups,
90
+ voteOptions,
91
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
92
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
93
+ ballot[BALLOT_NONCE_INDEX],
94
+ currentVoteWeight,
95
+ commandStateIndex,
96
+ commandPublicKey,
97
+ commandVoteOptionIndex,
98
+ commandNewVoteWeight,
99
+ commandNonce,
100
+ commandPollId,
101
+ commandSalt,
102
+ commandSignaturePoint,
103
+ commandSignatureScalar,
104
+ packedCommand
105
+ );
106
+
107
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
108
+ // Otherwise, generate indices for command.stateIndex.
109
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
110
+
111
+ // 3. Verify that the original state leaf exists in the given state root.
112
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
113
+ var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
114
+ stateLeafHash,
115
+ actualStateTreeDepth,
116
+ stateIndexMux,
117
+ stateLeafPathElements
118
+ );
119
+
120
+ stateLeafQip === currentStateRoot;
121
+
122
+ // 4. Verify that the original ballot exists in the given ballot root.
123
+ var computedBallot = PoseidonHasher(2)([
124
+ ballot[BALLOT_NONCE_INDEX],
125
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
126
+ ]);
127
+ var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
128
+
129
+ var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
130
+ computedBallot,
131
+ computedStateLeafPathIndices,
132
+ ballotPathElements
133
+ );
134
+
135
+ computedBallotQip === currentBallotRoot;
136
+
137
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
138
+ // at commandVoteOptionIndex.
139
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
140
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
141
+
142
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
143
+ currentVoteWeight,
144
+ computedCurrentVoteWeightPathIndices,
145
+ currentVoteWeightsPathElements
146
+ );
147
+
148
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
149
+
150
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
151
+ var voiceCreditBalanceMux = Mux1()(
152
+ [
153
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
154
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
155
+ ],
156
+ computedIsValid
157
+ );
158
+
159
+ // 5.1. Update the ballot's vote option root with the new vote weight.
160
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
161
+ voteWeightMux,
162
+ computedCurrentVoteWeightPathIndices,
163
+ currentVoteWeightsPathElements
164
+ );
165
+
166
+ // The new vote option root in the ballot
167
+ var newBallotVoteOptionRootMux = Mux1()(
168
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
169
+ computedIsValid
170
+ );
171
+
172
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
173
+
174
+ // 6. Generate a new state root.
175
+ var computedNewStateLeafHash = PoseidonHasher(3)([
176
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
177
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
178
+ voiceCreditBalanceMux
179
+ ]);
180
+
181
+ var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
182
+ computedNewStateLeafHash,
183
+ actualStateTreeDepth,
184
+ stateIndexMux,
185
+ stateLeafPathElements
186
+ );
187
+
188
+ newStateRoot <== computedNewStateLeafQip;
189
+
190
+ // 7. Generate a new ballot root.
191
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
192
+ var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
193
+ computedNewBallot,
194
+ computedStateLeafPathIndices,
195
+ ballotPathElements
196
+ );
197
+
198
+ newBallotRoot <== computedNewBallotQip;
199
+ }
@@ -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
- }