@maci-protocol/circuits 0.0.0-ci.ca15230 → 0.0.0-ci.cd6ffc8

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.
@@ -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
@@ -223,7 +217,7 @@ template ProcessMessages(
223
217
  }
224
218
  }
225
219
 
226
- (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOne(stateTreeDepth, voteOptionTreeDepth)(
220
+ (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorQv(stateTreeDepth, voteOptionTreeDepth)(
227
221
  totalSignups,
228
222
  stateRoots[i + 1],
229
223
  ballotRoots[i + 1],
@@ -254,199 +248,3 @@ template ProcessMessages(
254
248
  var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
255
249
  computedNewSbCommitment === newSbCommitment;
256
250
  }
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,208 @@
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/MerklePathIndicesGenerator.circom";
9
+ include "../../utils/trees/BinaryMerkleRoot.circom";
10
+ include "../../utils/trees/QuinaryTreeInclusionProof.circom";
11
+ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
12
+ include "../../utils/qv/StateLeafAndBallotTransformer.circom";
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 supports the Quadratic Voting (QV).
21
+ */
22
+ template SingleMessageProcessorQv(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
+ // Intermediate signals.
83
+ // currentVoteWeight * currentVoteWeight.
84
+ signal currentVoteWeightSquare;
85
+ // commandNewVoteWeight * commandNewVoteWeight.
86
+ signal commandNewVoteWeightSquare;
87
+ // equal to newBallotVoteOptionRootMux (Mux1).
88
+ signal newBallotVoteOptionRoot;
89
+
90
+ // 1. Transform a state leaf and a ballot with a command.
91
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
92
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
93
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
94
+ totalSignups,
95
+ voteOptions,
96
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
97
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
98
+ ballot[BALLOT_NONCE_INDEX],
99
+ currentVoteWeight,
100
+ commandStateIndex,
101
+ commandPublicKey,
102
+ commandVoteOptionIndex,
103
+ commandNewVoteWeight,
104
+ commandNonce,
105
+ commandPollId,
106
+ commandSalt,
107
+ commandSignaturePoint,
108
+ commandSignatureScalar,
109
+ packedCommand
110
+ );
111
+
112
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
113
+ // Otherwise, generate indices for command.stateIndex.
114
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
115
+ var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
116
+
117
+ // 3. Verify that the original state leaf exists in the given state root.
118
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
119
+ var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
120
+ stateLeafHash,
121
+ actualStateTreeDepth,
122
+ computedStateLeafPathIndices,
123
+ stateLeafPathElements
124
+ );
125
+
126
+ stateLeafQip === currentStateRoot;
127
+
128
+ // 4. Verify that the original ballot exists in the given ballot root.
129
+ var computedBallot = PoseidonHasher(2)([
130
+ ballot[BALLOT_NONCE_INDEX],
131
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
132
+ ]);
133
+
134
+ var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
135
+ computedBallot,
136
+ computedStateLeafPathIndices,
137
+ ballotPathElements
138
+ );
139
+
140
+ computedBallotQip === currentBallotRoot;
141
+
142
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
143
+ // at commandVoteOptionIndex.
144
+ currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
145
+ commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
146
+
147
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
148
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
149
+
150
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
151
+ currentVoteWeight,
152
+ computedCurrentVoteWeightPathIndices,
153
+ currentVoteWeightsPathElements
154
+ );
155
+
156
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
157
+
158
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
159
+ var voiceCreditBalanceMux = Mux1()(
160
+ [
161
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
162
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeightSquare - commandNewVoteWeightSquare
163
+ ],
164
+ computedIsValid
165
+ );
166
+
167
+ // 5.1. Update the ballot's vote option root with the new vote weight.
168
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
169
+ voteWeightMux,
170
+ computedCurrentVoteWeightPathIndices,
171
+ currentVoteWeightsPathElements
172
+ );
173
+
174
+ // The new vote option root in the ballot
175
+ var newBallotVoteOptionRootMux = Mux1()(
176
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
177
+ computedIsValid
178
+ );
179
+
180
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
181
+
182
+ // 6. Generate a new state root.
183
+ var computedNewStateLeafHash = PoseidonHasher(3)([
184
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
185
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
186
+ voiceCreditBalanceMux
187
+ ]);
188
+
189
+ var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
190
+ computedNewStateLeafHash,
191
+ actualStateTreeDepth,
192
+ computedStateLeafPathIndices,
193
+ stateLeafPathElements
194
+ );
195
+
196
+ newStateRoot <== computedNewStateLeafQip;
197
+
198
+ // 7. Generate a new ballot root.
199
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
200
+ var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
201
+ computedNewBallot,
202
+ computedStateLeafPathIndices,
203
+ ballotPathElements
204
+ );
205
+
206
+ newBallotRoot <== computedNewBallotQip;
207
+ }
208
+
@@ -9,6 +9,7 @@ include "../../utils/trees/CheckRoot.circom";
9
9
  include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
10
  include "../../utils/trees/LeafExists.circom";
11
11
  include "../../utils/trees/QuinaryCheckRoot.circom";
12
+ include "../../utils/qv/ResultCommitmentVerifier.circom";
12
13
  include "../../utils/CalculateTotal.circom";
13
14
  include "../../utils/PoseidonHasher.circom";
14
15
 
@@ -16,7 +17,7 @@ include "../../utils/PoseidonHasher.circom";
16
17
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
17
18
  * This template supports Quadratic Voting (QV).
18
19
  */
19
- template TallyVotes(
20
+ template VoteTallyQv(
20
21
  stateTreeDepth,
21
22
  tallyProcessingStateTreeDepth,
22
23
  voteOptionTreeDepth
@@ -159,7 +160,7 @@ template TallyVotes(
159
160
  }
160
161
 
161
162
  // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
162
- ResultCommitmentVerifier(voteOptionTreeDepth)(
163
+ ResultCommitmentVerifierQv(voteOptionTreeDepth)(
163
164
  computedIsFirstBatch,
164
165
  currentTallyCommitment,
165
166
  newTallyCommitment,
@@ -177,103 +178,3 @@ template TallyVotes(
177
178
  newPerVoteOptionSpentVoiceCreditsRootSalt
178
179
  );
179
180
  }
180
-
181
- /**
182
- * Performs verifications and computations related to current voting results.
183
- * Also, computes and outputs a commitment to the new results.
184
- * This template supports the Quadratic Voting (QV).
185
- */
186
- template ResultCommitmentVerifier(voteOptionTreeDepth) {
187
- // Number of children per node in the tree, defining the tree's branching factor.
188
- var TREE_ARITY = 5;
189
- // Number of voting options available, determined by the depth of the vote option tree.
190
- var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
191
-
192
- // Equal to 1 if this is the first batch, otherwise 0.
193
- signal input isFirstBatch;
194
- // Commitment to the current tally before this batch.
195
- signal input currentTallyCommitment;
196
- // Commitment to the new tally after processing this batch.
197
- signal input newTallyCommitment;
198
-
199
- // Current results for each vote option.
200
- signal input currentResults[totalVoteOptions];
201
- // Salt for the root of the current results.
202
- signal input currentResultsRootSalt;
203
-
204
- // New results for each vote option.
205
- signal input newResults[totalVoteOptions];
206
- // Salt for the root of the new results.
207
- signal input newResultsRootSalt;
208
-
209
- // Total voice credits spent so far.
210
- signal input currentSpentVoiceCreditSubtotal;
211
- // Salt for the total spent voice credits.
212
- signal input currentSpentVoiceCreditSubtotalSalt;
213
-
214
- // Total new voice credits spent.
215
- signal input newSpentVoiceCreditSubtotal;
216
- // Salt for the new total spent voice credits.
217
- signal input newSpentVoiceCreditSubtotalSalt;
218
-
219
- // Spent voice credits per vote option.
220
- signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
221
- // Salt for the root of spent credits per option.
222
- signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
223
-
224
- // New spent voice credits per vote option.
225
- signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
226
- // Salt for the root of new spent credits per option.
227
- signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
228
-
229
- // Compute the commitment to the current results.
230
- var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
231
-
232
- // Verify currentResultsCommitmentHash.
233
- var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
234
-
235
- // Compute the commitment to the current spent voice credits.
236
- var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
237
-
238
- // Compute the root of the spent voice credits per vote option.
239
- var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
240
- var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
241
-
242
- // Commit to the current tally.
243
- var computedCurrentTallyCommitment = PoseidonHasher(3)([
244
- computedCurrentResultsCommitment,
245
- computedCurrentSpentVoiceCreditsCommitment,
246
- computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
247
- ]);
248
-
249
- // Check if the current tally commitment is correct only if this is not the first batch.
250
- // computedIsZero.out is 1 if this is not the first batch.
251
- // computedIsZero.out is 0 if this is the first batch.
252
- var computedIsZero = IsZero()(isFirstBatch);
253
-
254
- // isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
255
- // isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
256
- signal isFirstCommitment;
257
- isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
258
- isFirstCommitment === currentTallyCommitment;
259
-
260
- // Compute the root of the new results.
261
- var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
262
- var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
263
-
264
- // Compute the commitment to the new spent voice credits value.
265
- var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
266
-
267
- // Compute the root of the spent voice credits per vote option.
268
- var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
269
- var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
270
-
271
- // Commit to the new tally.
272
- var computedNewTallyCommitment = PoseidonHasher(3)([
273
- computedNewResultsCommitment,
274
- computedNewSpentVoiceCreditsCommitment,
275
- computedNewPerVoteOptionSpentVoiceCreditsCommitment
276
- ]);
277
-
278
- computedNewTallyCommitment === newTallyCommitment;
279
- }
@@ -0,0 +1,84 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./comparators.circom";
5
+ // local imports
6
+ include "../trees/QuinaryCheckRoot.circom";
7
+ include "../PoseidonHasher.circom";
8
+
9
+ /**
10
+ * Performs verifications and computations related to current voting results.
11
+ * Also, computes and outputs a commitment to the new results.
12
+ * This template does not support Quadratic Voting (QV).
13
+ */
14
+ template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
15
+ // Number of children per node in the tree, defining the tree's branching factor.
16
+ var TREE_ARITY = 5;
17
+ // Number of voting options available, determined by the depth of the vote option tree.
18
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
19
+
20
+ // Equal to 1 if this is the first batch, otherwise 0.
21
+ signal input isFirstBatch;
22
+ // Commitment to the current tally before this batch.
23
+ signal input currentTallyCommitment;
24
+ // Commitment to the new tally after processing this batch.
25
+ signal input newTallyCommitment;
26
+
27
+ // Current results for each vote option.
28
+ signal input currentResults[totalVoteOptions];
29
+ // Salt for the root of the current results.
30
+ signal input currentResultsRootSalt;
31
+
32
+ // New results for each vote option.
33
+ signal input newResults[totalVoteOptions];
34
+ // Salt for the root of the new results.
35
+ signal input newResultsRootSalt;
36
+
37
+ // Total voice credits spent so far.
38
+ signal input currentSpentVoiceCreditSubtotal;
39
+ // Salt for the total spent voice credits.
40
+ signal input currentSpentVoiceCreditSubtotalSalt;
41
+
42
+ // Total new voice credits spent.
43
+ signal input newSpentVoiceCreditSubtotal;
44
+ // Salt for the new total spent voice credits.
45
+ signal input newSpentVoiceCreditSubtotalSalt;
46
+
47
+ // Compute the commitment to the current results.
48
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
49
+
50
+ // Verify currentResultsCommitmentHash.
51
+ var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
52
+
53
+ // Compute the commitment to the current spent voice credits.
54
+ var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
55
+
56
+ // Commit to the current tally
57
+ var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
58
+
59
+ // Check if the current tally commitment is correct only if this is not the first batch.
60
+ // computedIsZero.out is 1 if this is not the first batch.
61
+ // computedIsZero.out is 0 if this is the first batch.
62
+ var computedIsZero = IsZero()(isFirstBatch);
63
+
64
+ // isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
65
+ // isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
66
+ signal isFirstCommitment;
67
+ isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
68
+ isFirstCommitment === currentTallyCommitment;
69
+
70
+ // Compute the root of the new results.
71
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
72
+ var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
73
+
74
+ // Compute the commitment to the new spent voice credits value.
75
+ var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
76
+
77
+ // Commit to the new tally.
78
+ var computedNewTallyCommitment = PoseidonHasher(2)([
79
+ computedNewResultsCommitment,
80
+ computedNewSpentVoiceCreditsCommitment
81
+ ]);
82
+
83
+ computedNewTallyCommitment === newTallyCommitment;
84
+ }