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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/CHANGELOG.md +432 -0
  2. package/README.md +2 -2
  3. package/build/ts/types.d.ts +4 -4
  4. package/build/ts/types.d.ts.map +1 -1
  5. package/build/tsconfig.build.tsbuildinfo +1 -1
  6. package/circom/circuits.json +12 -12
  7. package/circom/coordinator/full/MessageProcessor.circom +5 -13
  8. package/circom/coordinator/full/SingleMessageProcessor.circom +3 -4
  9. package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +8 -211
  10. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +197 -0
  11. package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +3 -80
  12. package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +9 -219
  13. package/circom/coordinator/qv/SingleMessageProcessor.circom +204 -0
  14. package/circom/coordinator/qv/{tallyVotes.circom → VoteTally.circom} +4 -104
  15. package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
  16. package/circom/utils/EdDSAPoseidonVerifier.circom +8 -3
  17. package/circom/utils/IsOnCurve.circom +40 -0
  18. package/circom/utils/full/StateLeafAndBallotTransformer.circom +5 -0
  19. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  20. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +5 -0
  21. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  22. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +5 -0
  23. package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
  24. package/circom/voter/PollJoined.circom +3 -3
  25. package/circom/voter/PollJoining.circom +3 -3
  26. package/package.json +17 -16
  27. package/circom/utils/trees/MerklePathIndicesGenerator.circom +0 -44
@@ -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
  }
@@ -33,14 +33,6 @@ include "./SingleMessageProcessor.circom";
33
33
  var PACKED_COMMAND_LENGTH = 4;
34
34
  var STATE_LEAF_LENGTH = 3;
35
35
  var BALLOT_LENGTH = 2;
36
- var BALLOT_NONCE_INDEX = 0;
37
- var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
38
- var STATE_LEAF_PUBLIC_X_INDEX = 0;
39
- var STATE_LEAF_PUBLIC_Y_INDEX = 1;
40
- var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
41
- var MESSAGE_TREE_ZERO_VALUE = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
42
- // Number of options for this poll.
43
- var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
44
36
 
45
37
  // Number of users that have completed the sign up.
46
38
  signal input totalSignups;
@@ -57,7 +49,7 @@ include "./SingleMessageProcessor.circom";
57
49
  // The current state root (before the processing).
58
50
  signal input currentStateRoot;
59
51
  // The actual tree depth (might be <= stateTreeDepth).
60
- // @note it is a public input to ensure fair processing from
52
+ // @note it is a public input to ensure fair processing from
61
53
  // the coordinator (no censoring)
62
54
  signal input actualStateTreeDepth;
63
55
  // The coordinator public key hash
@@ -97,7 +89,7 @@ include "./SingleMessageProcessor.circom";
97
89
 
98
90
  // The index of the first message in the batch, inclusive.
99
91
  signal input index;
100
-
92
+
101
93
  // The index of the last message in the batch to process, exclusive.
102
94
  // This value may be less than index + batchSize if this batch is
103
95
  // the last batch and the total number of messages is not a multiple of the batch size.
@@ -112,7 +104,7 @@ include "./SingleMessageProcessor.circom";
112
104
  var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
113
105
  computedCurrentSbCommitment === currentSbCommitment;
114
106
 
115
- // -----------------------------------------------------------------------
107
+ // -----------------------------------------------------------------------
116
108
  // 0. Ensure that the maximum vote options signal is valid and if
117
109
  // the maximum users signal is valid
118
110
  var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
@@ -162,7 +154,7 @@ include "./SingleMessageProcessor.circom";
162
154
 
163
155
  // Decrypt each Message into a Command.
164
156
  // The command i-th is composed by the following fields.
165
- // e.g., command 0 is made of commandsStateIndex[0],
157
+ // e.g., command 0 is made of commandsStateIndex[0],
166
158
  // commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
167
159
  var computedCommandsStateIndex[batchSize];
168
160
  var computedCommandsNewPublicKey[batchSize][2];
@@ -205,7 +197,7 @@ include "./SingleMessageProcessor.circom";
205
197
  var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
206
198
  var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
207
199
  var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
208
-
200
+
209
201
  for (var j = 0; j < stateTreeDepth; j++) {
210
202
  for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
211
203
  computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
@@ -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
@@ -40,14 +33,6 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
40
33
  var PACKED_COMMAND_LENGTH = 4;
41
34
  var STATE_LEAF_LENGTH = 3;
42
35
  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
36
 
52
37
  // Number of users that have completed the sign up.
53
38
  signal input totalSignups;
@@ -64,7 +49,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
64
49
  // The current state root (before the processing).
65
50
  signal input currentStateRoot;
66
51
  // The actual tree depth (might be <= stateTreeDepth).
67
- // @note it is a public input to ensure fair processing from
52
+ // @note it is a public input to ensure fair processing from
68
53
  // the coordinator (no censoring)
69
54
  signal input actualStateTreeDepth;
70
55
  // The coordinator public key hash
@@ -104,7 +89,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
104
89
 
105
90
  // The index of the first message in the batch, inclusive.
106
91
  signal input index;
107
-
92
+
108
93
  // The index of the last message in the batch to process, exclusive.
109
94
  // This value may be less than index + batchSize if this batch is
110
95
  // the last batch and the total number of messages is not a multiple of the batch size.
@@ -119,7 +104,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
119
104
  var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
120
105
  computedCurrentSbCommitment === currentSbCommitment;
121
106
 
122
- // -----------------------------------------------------------------------
107
+ // -----------------------------------------------------------------------
123
108
  // 0. Ensure that the maximum vote options signal is valid and if
124
109
  // the maximum users signal is valid
125
110
  var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
@@ -169,7 +154,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
169
154
 
170
155
  // Decrypt each Message into a Command.
171
156
  // The command i-th is composed by the following fields.
172
- // e.g., command 0 is made of commandsStateIndex[0],
157
+ // e.g., command 0 is made of commandsStateIndex[0],
173
158
  // commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
174
159
  var computedCommandsStateIndex[batchSize];
175
160
  var computedCommandsNewPublicKey[batchSize][2];
@@ -212,7 +197,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
212
197
  var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
213
198
  var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
214
199
  var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
215
-
200
+
216
201
  for (var j = 0; j < stateTreeDepth; j++) {
217
202
  for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
218
203
  computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
@@ -226,7 +211,7 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
226
211
  }
227
212
  }
228
213
 
229
- (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
214
+ (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth)(
230
215
  totalSignups,
231
216
  stateRoots[i + 1],
232
217
  ballotRoots[i + 1],
@@ -257,191 +242,3 @@ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
257
242
  var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
258
243
  computedNewSbCommitment === newSbCommitment;
259
244
  }
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,197 @@
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 PACKED_COMMAND_LENGTH = 4;
27
+ var VOTE_OPTION_TREE_ARITY = 5;
28
+ var STATE_TREE_ARITY = 2;
29
+ var BALLOT_NONCE_INDEX = 0;
30
+ // Ballot vote option (vote option) root index.
31
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
32
+
33
+ // Indices for elements within a state leaf.
34
+ // Public key.
35
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
36
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
37
+ // Voice Credit balance.
38
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
39
+
40
+ // Number of users that have completed the sign up.
41
+ signal input totalSignups;
42
+ // The current value of the state tree root.
43
+ signal input currentStateRoot;
44
+ // The current value of the ballot tree root.
45
+ signal input currentBallotRoot;
46
+ // The actual tree depth (might be <= stateTreeDepth).
47
+ signal input actualStateTreeDepth;
48
+
49
+ // The state leaf and related path elements.
50
+ signal input stateLeaf[STATE_LEAF_LENGTH];
51
+ // Sibling nodes at each level of the state tree to verify the specific state leaf.
52
+ signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
53
+
54
+ // The ballot and related path elements.
55
+ signal input ballot[BALLOT_LENGTH];
56
+ signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
57
+
58
+ // The current vote weight and related path elements.
59
+ signal input currentVoteWeight;
60
+ signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
61
+
62
+ // Inputs related to the command being processed.
63
+ signal input commandStateIndex;
64
+ signal input commandPublicKey[2];
65
+ signal input commandVoteOptionIndex;
66
+ signal input commandNewVoteWeight;
67
+ signal input commandNonce;
68
+ signal input commandPollId;
69
+ signal input commandSalt;
70
+ signal input commandSignaturePoint[2];
71
+ signal input commandSignatureScalar;
72
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
73
+
74
+ // The number of valid vote options for the poll.
75
+ signal input voteOptions;
76
+
77
+ signal output newStateRoot;
78
+ signal output newBallotRoot;
79
+
80
+ // equal to newBallotVoteOptionRootMux (Mux1).
81
+ signal newBallotVoteOptionRoot;
82
+
83
+ // 1. Transform a state leaf and a ballot with a command.
84
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
85
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
86
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
87
+ totalSignups,
88
+ voteOptions,
89
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
90
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
91
+ ballot[BALLOT_NONCE_INDEX],
92
+ currentVoteWeight,
93
+ commandStateIndex,
94
+ commandPublicKey,
95
+ commandVoteOptionIndex,
96
+ commandNewVoteWeight,
97
+ commandNonce,
98
+ commandPollId,
99
+ commandSalt,
100
+ commandSignaturePoint,
101
+ commandSignatureScalar,
102
+ packedCommand
103
+ );
104
+
105
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
106
+ // Otherwise, generate indices for command.stateIndex.
107
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
108
+
109
+ // 3. Verify that the original state leaf exists in the given state root.
110
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
111
+ var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
112
+ stateLeafHash,
113
+ actualStateTreeDepth,
114
+ stateIndexMux,
115
+ stateLeafPathElements
116
+ );
117
+
118
+ stateLeafQip === currentStateRoot;
119
+
120
+ // 4. Verify that the original ballot exists in the given ballot root.
121
+ var computedBallot = PoseidonHasher(2)([
122
+ ballot[BALLOT_NONCE_INDEX],
123
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
124
+ ]);
125
+ var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
126
+
127
+ var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
128
+ computedBallot,
129
+ computedStateLeafPathIndices,
130
+ ballotPathElements
131
+ );
132
+
133
+ computedBallotQip === currentBallotRoot;
134
+
135
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
136
+ // at commandVoteOptionIndex.
137
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
138
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
139
+
140
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
141
+ currentVoteWeight,
142
+ computedCurrentVoteWeightPathIndices,
143
+ currentVoteWeightsPathElements
144
+ );
145
+
146
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
147
+
148
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
149
+ var voiceCreditBalanceMux = Mux1()(
150
+ [
151
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
152
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
153
+ ],
154
+ computedIsValid
155
+ );
156
+
157
+ // 5.1. Update the ballot's vote option root with the new vote weight.
158
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
159
+ voteWeightMux,
160
+ computedCurrentVoteWeightPathIndices,
161
+ currentVoteWeightsPathElements
162
+ );
163
+
164
+ // The new vote option root in the ballot
165
+ var newBallotVoteOptionRootMux = Mux1()(
166
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
167
+ computedIsValid
168
+ );
169
+
170
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
171
+
172
+ // 6. Generate a new state root.
173
+ var computedNewStateLeafHash = PoseidonHasher(3)([
174
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
175
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
176
+ voiceCreditBalanceMux
177
+ ]);
178
+
179
+ var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
180
+ computedNewStateLeafHash,
181
+ actualStateTreeDepth,
182
+ stateIndexMux,
183
+ stateLeafPathElements
184
+ );
185
+
186
+ newStateRoot <== computedNewStateLeafQip;
187
+
188
+ // 7. Generate a new ballot root.
189
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
190
+ var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
191
+ computedNewBallot,
192
+ computedStateLeafPathIndices,
193
+ ballotPathElements
194
+ );
195
+
196
+ newBallotRoot <== computedNewBallotQip;
197
+ }