@maci-protocol/circuits 0.0.0-ci.5209133 → 0.0.0-ci.52ce07e

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 (49) hide show
  1. package/LICENSE +1 -2
  2. package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
  3. package/build/ts/generateZkeys.d.ts.map +1 -0
  4. package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
  5. package/build/ts/generateZkeys.js.map +1 -0
  6. package/build/ts/types.d.ts +11 -11
  7. package/build/ts/types.d.ts.map +1 -1
  8. package/build/tsconfig.build.tsbuildinfo +1 -1
  9. package/circom/circuits.json +23 -7
  10. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  11. package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
  12. package/circom/coordinator/non-qv/processMessages.circom +115 -111
  13. package/circom/coordinator/non-qv/tallyVotes.circom +51 -45
  14. package/circom/coordinator/qv/processMessages.circom +116 -109
  15. package/circom/coordinator/qv/tallyVotes.circom +65 -63
  16. package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +8 -6
  17. package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
  18. package/circom/utils/MessageHasher.circom +57 -0
  19. package/circom/utils/MessageToCommand.circom +107 -0
  20. package/circom/utils/PoseidonHasher.circom +29 -0
  21. package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +12 -10
  22. package/circom/utils/VerifySignature.circom +39 -0
  23. package/circom/utils/full/MessageValidator.circom +91 -0
  24. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  25. package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +17 -15
  26. package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  27. package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +17 -15
  28. package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  29. package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
  30. package/circom/utils/trees/CheckRoot.circom +49 -0
  31. package/circom/utils/trees/LeafExists.circom +27 -0
  32. package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
  33. package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
  34. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  35. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  36. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  37. package/circom/utils/trees/QuinarySelector.circom +42 -0
  38. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  39. package/circom/utils/trees/Splicer.circom +76 -0
  40. package/circom/voter/PollJoined.circom +43 -0
  41. package/circom/voter/PollJoining.circom +54 -0
  42. package/package.json +16 -13
  43. package/build/ts/genZkeys.d.ts.map +0 -1
  44. package/build/ts/genZkeys.js.map +0 -1
  45. package/circom/utils/hashers.circom +0 -78
  46. package/circom/utils/messageToCommand.circom +0 -78
  47. package/circom/utils/trees/incrementalMerkleTree.circom +0 -198
  48. package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
  49. package/circom/voter/poll.circom +0 -91
@@ -5,12 +5,18 @@ include "./mux1.circom";
5
5
  // zk-kit imports
6
6
  include "./safe-comparators.circom";
7
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";
8
+ include "../../utils/PoseidonHasher.circom";
9
+ include "../../utils/MessageHasher.circom";
10
+ include "../../utils/MessageToCommand.circom";
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";
14
20
 
15
21
  /**
16
22
  * Proves the correctness of processing a batch of MACI messages.
@@ -30,31 +36,31 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
30
36
  var VOTE_OPTION_TREE_ARITY = 5;
31
37
  // Default for Binary trees.
32
38
  var STATE_TREE_ARITY = 2;
33
- var MSG_LENGTH = 10;
34
- var PACKED_CMD_LENGTH = 4;
39
+ var MESSAGE_LENGTH = 10;
40
+ var PACKED_COMMAND_LENGTH = 4;
35
41
  var STATE_LEAF_LENGTH = 3;
36
42
  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 msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
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;
43
49
  // Number of options for this poll.
44
50
  var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
45
51
 
46
52
  // Number of users that have completed the sign up.
47
- signal input numSignUps;
53
+ signal input totalSignups;
48
54
  // Value of chainHash at beginning of batch
49
55
  signal input inputBatchHash;
50
56
  // Value of chainHash at end of batch
51
57
  signal input outputBatchHash;
52
58
  // The messages.
53
- signal input msgs[batchSize][MSG_LENGTH];
59
+ signal input messages[batchSize][MESSAGE_LENGTH];
54
60
  // The coordinator's private key.
55
- signal input coordPrivKey;
61
+ signal input coordinatorPrivateKey;
56
62
  // The ECDH public key per message.
57
- signal input encPubKeys[batchSize][2];
63
+ signal input encryptionPublicKeys[batchSize][2];
58
64
  // The current state root (before the processing).
59
65
  signal input currentStateRoot;
60
66
  // The actual tree depth (might be <= stateTreeDepth).
@@ -108,34 +114,31 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
108
114
  // signals (for processing purposes).
109
115
  signal stateRoots[batchSize + 1];
110
116
  signal ballotRoots[batchSize + 1];
111
- signal tmpStateRoot1[batchSize];
112
- signal tmpStateRoot2[batchSize];
113
- signal tmpBallotRoot1[batchSize];
114
- signal tmpBallotRoot2[batchSize];
115
117
 
116
118
  // Must verify the current sb commitment.
117
119
  var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
118
120
  computedCurrentSbCommitment === currentSbCommitment;
119
121
 
120
122
  // -----------------------------------------------------------------------
121
- // 0. Ensure that the maximum vote options signal is valid and if
123
+ // 0. Ensure that the maximum vote options signal is valid and if
122
124
  // the maximum users signal is valid
123
125
  var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
124
126
  voteOptionsValid === 1;
125
127
 
126
- // Check numSignUps <= the max number of users (i.e., number of state leaves
128
+ // Check totalSignups <= the max number of users (i.e., number of state leaves
127
129
  // that can fit the state tree).
128
- var numSignUpsValid = LessEqThan(32)([numSignUps, STATE_TREE_ARITY ** stateTreeDepth]);
129
- numSignUpsValid === 1;
130
+ var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
131
+ totalSignupsValid === 1;
130
132
 
131
133
  // Hash each Message to check their existence in the Message chain hash.
132
134
  var computedMessageHashers[batchSize];
133
135
  var computedChainHashes[batchSize];
134
136
  var chainHash[batchSize + 1];
135
137
  chainHash[0] = inputBatchHash;
138
+
136
139
  for (var i = 0; i < batchSize; i++) {
137
140
  // calculate message hash
138
- computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
141
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
139
142
  // check if message is valid or not (if index of message is less than index of last valid message in batch)
140
143
  var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
141
144
  // calculate chain hash if message is valid
@@ -160,38 +163,38 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
160
163
  // Ensure that the coordinator's public key from the contract is correct
161
164
  // based on the given private key - that is, the prover knows the
162
165
  // coordinator's private key.
163
- var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
164
- var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
165
- derivedPubKeyHash === coordinatorPublicKeyHash;
166
+ var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
167
+ var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
168
+ derivedPublicKeyHash === coordinatorPublicKeyHash;
166
169
 
167
170
  // Decrypt each Message into a Command.
168
171
  // The command i-th is composed by the following fields.
169
172
  // e.g., command 0 is made of commandsStateIndex[0],
170
- // commandsNewPubKey[0], ..., commandsPackedCommandOut[0]
173
+ // commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
171
174
  var computedCommandsStateIndex[batchSize];
172
- var computedCommandsNewPubKey[batchSize][2];
175
+ var computedCommandsNewPublicKey[batchSize][2];
173
176
  var computedCommandsVoteOptionIndex[batchSize];
174
177
  var computedCommandsNewVoteWeight[batchSize];
175
178
  var computedCommandsNonce[batchSize];
176
179
  var computedCommandsPollId[batchSize];
177
180
  var computedCommandsSalt[batchSize];
178
- var computedCommandsSigR8[batchSize][2];
179
- var computedCommandsSigS[batchSize];
180
- var computedCommandsPackedCommandOut[batchSize][PACKED_CMD_LENGTH];
181
+ var computedCommandsSignaturePoint[batchSize][2];
182
+ var computedCommandsSignatureScalar[batchSize];
183
+ var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
181
184
 
182
185
  for (var i = 0; i < batchSize; i++) {
183
186
  (
184
187
  computedCommandsStateIndex[i],
185
- computedCommandsNewPubKey[i],
188
+ computedCommandsNewPublicKey[i],
186
189
  computedCommandsVoteOptionIndex[i],
187
190
  computedCommandsNewVoteWeight[i],
188
191
  computedCommandsNonce[i],
189
192
  computedCommandsPollId[i],
190
193
  computedCommandsSalt[i],
191
- computedCommandsSigR8[i],
192
- computedCommandsSigS[i],
194
+ computedCommandsSignaturePoint[i],
195
+ computedCommandsSignatureScalar[i],
193
196
  computedCommandsPackedCommandOut[i]
194
- ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
197
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
195
198
  }
196
199
 
197
200
  // Process messages in reverse order.
@@ -206,43 +209,43 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
206
209
  // Start from batchSize and decrement for process in reverse order.
207
210
  for (var i = batchSize - 1; i >= 0; i--) {
208
211
  // Process as vote type message.
209
- var currentStateLeavesPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
210
- var currentBallotPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
211
- var currentVoteWeightsPathElement[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
212
+ var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
213
+ var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
214
+ var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
212
215
 
213
216
  for (var j = 0; j < stateTreeDepth; j++) {
214
217
  for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
215
- currentStateLeavesPathElement[j][k] = currentStateLeavesPathElements[i][j][k];
216
- currentBallotPathElement[j][k] = currentBallotsPathElements[i][j][k];
218
+ computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
219
+ computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
217
220
  }
218
221
  }
219
222
 
220
223
  for (var j = 0; j < voteOptionTreeDepth; j++) {
221
224
  for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
222
- currentVoteWeightsPathElement[j][k] = currentVoteWeightsPathElements[i][j][k];
225
+ computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
223
226
  }
224
227
  }
225
228
 
226
229
  (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
227
- numSignUps,
230
+ totalSignups,
228
231
  stateRoots[i + 1],
229
232
  ballotRoots[i + 1],
230
233
  actualStateTreeDepth,
231
234
  currentStateLeaves[i],
232
- currentStateLeavesPathElement,
235
+ computedCurrentStateLeavesPathElements,
233
236
  currentBallots[i],
234
- currentBallotPathElement,
237
+ computedCurrentBallotPathElements,
235
238
  currentVoteWeights[i],
236
- currentVoteWeightsPathElement,
239
+ computedCurrentVoteWeightsPathElements,
237
240
  computedCommandsStateIndex[i],
238
- computedCommandsNewPubKey[i],
241
+ computedCommandsNewPublicKey[i],
239
242
  computedCommandsVoteOptionIndex[i],
240
243
  computedCommandsNewVoteWeight[i],
241
244
  computedCommandsNonce[i],
242
245
  computedCommandsPollId[i],
243
246
  computedCommandsSalt[i],
244
- computedCommandsSigR8[i],
245
- computedCommandsSigS[i],
247
+ computedCommandsSignaturePoint[i],
248
+ computedCommandsSignatureScalar[i],
246
249
  computedCommandsPackedCommandOut[i],
247
250
  voteOptions
248
251
  );
@@ -267,24 +270,24 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
267
270
  // Constants defining the structure and size of state and ballots.
268
271
  var STATE_LEAF_LENGTH = 3;
269
272
  var BALLOT_LENGTH = 2;
270
- var MSG_LENGTH = 10;
271
- var PACKED_CMD_LENGTH = 4;
273
+ var MESSAGE_LENGTH = 10;
274
+ var PACKED_COMMAND_LENGTH = 4;
272
275
  var VOTE_OPTION_TREE_ARITY = 5;
273
276
  var STATE_TREE_ARITY = 2;
274
- var BALLOT_NONCE_IDX = 0;
275
- // Ballot vote option (VO) root index.
276
- var BALLOT_VO_ROOT_IDX = 1;
277
+ var BALLOT_NONCE_INDEX = 0;
278
+ // Ballot vote option (vote option) root index.
279
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
277
280
 
278
281
  // Indices for elements within a state leaf.
279
282
  // Public key.
280
- var STATE_LEAF_PUB_X_IDX = 0;
281
- var STATE_LEAF_PUB_Y_IDX = 1;
283
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
284
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
282
285
  // Voice Credit balance.
283
- var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
284
- var N_BITS = 252;
286
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
287
+ var NUMBER_BITS = 252;
285
288
 
286
289
  // Number of users that have completed the sign up.
287
- signal input numSignUps;
290
+ signal input totalSignups;
288
291
  // The current value of the state tree root.
289
292
  signal input currentStateRoot;
290
293
  // The current value of the ballot tree root.
@@ -306,16 +309,16 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
306
309
  signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
307
310
 
308
311
  // Inputs related to the command being processed.
309
- signal input cmdStateIndex;
310
- signal input cmdNewPubKey[2];
311
- signal input cmdVoteOptionIndex;
312
- signal input cmdNewVoteWeight;
313
- signal input cmdNonce;
314
- signal input cmdPollId;
315
- signal input cmdSalt;
316
- signal input cmdSigR8[2];
317
- signal input cmdSigS;
318
- signal input packedCmd[PACKED_CMD_LENGTH];
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];
319
322
 
320
323
  // The number of valid vote options for the poll.
321
324
  signal input voteOptions;
@@ -323,35 +326,35 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
323
326
  signal output newStateRoot;
324
327
  signal output newBallotRoot;
325
328
 
326
- // equal to newBallotVoRootMux (Mux1).
327
- signal newBallotVoRoot;
329
+ // equal to newBallotVoteOptionRootMux (Mux1).
330
+ signal newBallotVoteOptionRoot;
328
331
 
329
332
  // 1. Transform a state leaf and a ballot with a command.
330
333
  // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
331
- var computedNewSlPubKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
332
- (computedNewSlPubKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
333
- numSignUps,
334
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
335
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
336
+ totalSignups,
334
337
  voteOptions,
335
- [stateLeaf[STATE_LEAF_PUB_X_IDX], stateLeaf[STATE_LEAF_PUB_Y_IDX]],
336
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
337
- ballot[BALLOT_NONCE_IDX],
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],
338
341
  currentVoteWeight,
339
- cmdStateIndex,
340
- cmdNewPubKey,
341
- cmdVoteOptionIndex,
342
- cmdNewVoteWeight,
343
- cmdNonce,
344
- cmdPollId,
345
- cmdSalt,
346
- cmdSigR8,
347
- cmdSigS,
348
- packedCmd
342
+ commandStateIndex,
343
+ commandPublicKey,
344
+ commandVoteOptionIndex,
345
+ commandNewVoteWeight,
346
+ commandNonce,
347
+ commandPollId,
348
+ commandSalt,
349
+ commandSignaturePoint,
350
+ commandSignatureScalar,
351
+ packedCommand
349
352
  );
350
353
 
351
354
  // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
352
355
  // Otherwise, generate indices for command.stateIndex.
353
- var stateIndexMux = Mux1()([0, cmdStateIndex], computedIsStateLeafIndexValid);
354
- var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
356
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
357
+ var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
355
358
 
356
359
  // 3. Verify that the original state leaf exists in the given state root.
357
360
  var stateLeafHash = PoseidonHasher(3)(stateLeaf);
@@ -366,8 +369,8 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
366
369
 
367
370
  // 4. Verify that the original ballot exists in the given ballot root.
368
371
  var computedBallot = PoseidonHasher(2)([
369
- ballot[BALLOT_NONCE_IDX],
370
- ballot[BALLOT_VO_ROOT_IDX]
372
+ ballot[BALLOT_NONCE_INDEX],
373
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
371
374
  ]);
372
375
 
373
376
  var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
@@ -379,51 +382,51 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
379
382
  computedBallotQip === currentBallotRoot;
380
383
 
381
384
  // 5. Verify that currentVoteWeight exists in the ballot's vote option root
382
- // at cmdVoteOptionIndex.
383
- var cmdVoteOptionIndexMux = Mux1()([0, cmdVoteOptionIndex], computedIsVoteOptionIndexValid);
384
- var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(cmdVoteOptionIndexMux);
385
+ // at commandVoteOptionIndex.
386
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
387
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
385
388
 
386
- var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
389
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
387
390
  currentVoteWeight,
388
391
  computedCurrentVoteWeightPathIndices,
389
392
  currentVoteWeightsPathElements
390
393
  );
391
394
 
392
- computedCurrentVoteWeightQip === ballot[BALLOT_VO_ROOT_IDX];
395
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
393
396
 
394
- var voteWeightMux = Mux1()([currentVoteWeight, cmdNewVoteWeight], computedIsValid);
397
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
395
398
  var voiceCreditBalanceMux = Mux1()(
396
399
  [
397
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
398
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] + currentVoteWeight - cmdNewVoteWeight
400
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
401
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
399
402
  ],
400
403
  computedIsValid
401
404
  );
402
405
 
403
406
  // 5.1. Update the ballot's vote option root with the new vote weight.
404
- var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
407
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
405
408
  voteWeightMux,
406
409
  computedCurrentVoteWeightPathIndices,
407
410
  currentVoteWeightsPathElements
408
411
  );
409
412
 
410
413
  // The new vote option root in the ballot
411
- var newBallotVoRootMux = Mux1()(
412
- [ballot[BALLOT_VO_ROOT_IDX], computedNewVoteOptionTreeQip],
414
+ var newBallotVoteOptionRootMux = Mux1()(
415
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
413
416
  computedIsValid
414
417
  );
415
418
 
416
- newBallotVoRoot <== newBallotVoRootMux;
419
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
417
420
 
418
421
  // 6. Generate a new state root.
419
- var computedNewStateLeafhash = PoseidonHasher(3)([
420
- computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
421
- computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
422
+ var computedNewStateLeafHash = PoseidonHasher(3)([
423
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
424
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
422
425
  voiceCreditBalanceMux
423
426
  ]);
424
427
 
425
428
  var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
426
- computedNewStateLeafhash,
429
+ computedNewStateLeafHash,
427
430
  actualStateTreeDepth,
428
431
  computedStateLeafPathIndices,
429
432
  stateLeafPathElements
@@ -432,7 +435,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
432
435
  newStateRoot <== computedNewStateLeafQip;
433
436
 
434
437
  // 7. Generate a new ballot root.
435
- var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoRoot]);
438
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
436
439
  var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
437
440
  computedNewBallot,
438
441
  computedStateLeafPathIndices,
@@ -441,3 +444,4 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
441
444
 
442
445
  newBallotRoot <== computedNewBallotQip;
443
446
  }
447
+
@@ -5,10 +5,12 @@ include "./comparators.circom";
5
5
  // zk-kit import
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
- include "../../utils/trees/incrementalMerkleTree.circom";
9
- include "../../utils/trees/incrementalQuinaryTree.circom";
10
- include "../../utils/calculateTotal.circom";
11
- include "../../utils/hashers.circom";
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
+ include "../../utils/trees/LeafExists.circom";
11
+ include "../../utils/trees/QuinaryCheckRoot.circom";
12
+ include "../../utils/CalculateTotal.circom";
13
+ include "../../utils/PoseidonHasher.circom";
12
14
 
13
15
  /**
14
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
@@ -16,33 +18,33 @@ include "../../utils/hashers.circom";
16
18
  */
17
19
  template TallyVotesNonQv(
18
20
  stateTreeDepth,
19
- intStateTreeDepth,
21
+ tallyProcessingStateTreeDepth,
20
22
  voteOptionTreeDepth
21
23
  ) {
22
24
  // Ensure there's at least one level in the vote option tree.
23
25
  assert(voteOptionTreeDepth > 0);
24
26
  // Ensure the intermediate state tree has at least one level.
25
- assert(intStateTreeDepth > 0);
27
+ assert(tallyProcessingStateTreeDepth > 0);
26
28
  // The intermediate state tree must be smaller than the full state tree.
27
- assert(intStateTreeDepth < stateTreeDepth);
29
+ assert(tallyProcessingStateTreeDepth < stateTreeDepth);
28
30
 
29
31
  // Number of children per node in the tree, defining the tree's branching factor.
30
32
  var TREE_ARITY = 5;
31
33
  var BALLOT_TREE_ARITY = 2;
32
34
 
33
35
  // The number of ballots processed at once, determined by the depth of the intermediate state tree.
34
- var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
36
+ var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
35
37
  // Number of voting options available, determined by the depth of the vote option tree.
36
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
38
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
37
39
 
38
40
  // Number of elements in each ballot.
39
41
  var BALLOT_LENGTH = 2;
40
42
  // Index for the nonce in the ballot array.
41
- var BALLOT_NONCE_IDX = 0;
43
+ var BALLOT_NONCE_INDEX = 0;
42
44
  // Index for the voting option root in the ballot array.
43
- var BALLOT_VO_ROOT_IDX = 1;
45
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
44
46
  // Difference in tree depths, used in path calculations.
45
- var k = stateTreeDepth - intStateTreeDepth;
47
+ var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
46
48
 
47
49
  // Root of the state Merkle tree, representing the overall state before voting.
48
50
  signal input stateRoot;
@@ -59,13 +61,13 @@ template TallyVotesNonQv(
59
61
  // Start index of given batch
60
62
  signal input index;
61
63
  // Number of users that signup
62
- signal input numSignUps;
64
+ signal input totalSignups;
63
65
  // Ballots and their corresponding path elements for verification in the tree.
64
66
  signal input ballots[batchSize][BALLOT_LENGTH];
65
- signal input ballotPathElements[k][BALLOT_TREE_ARITY - 1];
66
- signal input votes[batchSize][numVoteOptions];
67
+ signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
+ signal input votes[batchSize][totalVoteOptions];
67
69
  // Current results for each vote option.
68
- signal input currentResults[numVoteOptions];
70
+ signal input currentResults[totalVoteOptions];
69
71
  // Salt for the root of the current results.
70
72
  signal input currentResultsRootSalt;
71
73
  // Total voice credits spent so far.
@@ -83,21 +85,21 @@ template TallyVotesNonQv(
83
85
 
84
86
 
85
87
  // Validates that the index is within the valid range of sign-ups.
86
- var numSignUpsValid = LessEqThan(50)([index, numSignUps]);
87
- numSignUpsValid === 1;
88
+ var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
89
+ totalSignupsValid === 1;
88
90
 
89
91
  // Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
90
92
  var computedBallotHashers[batchSize];
91
93
 
92
94
  for (var i = 0; i < batchSize; i++) {
93
- computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_IDX], ballots[i][BALLOT_VO_ROOT_IDX]]);
95
+ computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
94
96
  }
95
97
 
96
- var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
97
- var computedBallotPathIndices[k] = MerkleGeneratePathIndices(k)(index / batchSize);
98
+ var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
99
+ var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
98
100
 
99
101
  // Verifies each ballot's existence within the ballot tree.
100
- LeafExists(k)(
102
+ LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
101
103
  computedBallotSubroot,
102
104
  ballotPathElements,
103
105
  computedBallotPathIndices,
@@ -106,9 +108,10 @@ template TallyVotesNonQv(
106
108
 
107
109
  // Processes vote options, verifying each against its declared root.
108
110
  var computedVoteTree[batchSize];
111
+
109
112
  for (var i = 0; i < batchSize; i++) {
110
- computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
111
- computedVoteTree[i] === ballots[i][BALLOT_VO_ROOT_IDX];
113
+ computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
114
+ computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
112
115
  }
113
116
 
114
117
  // Calculates new results and spent voice credits based on the current and incoming votes.
@@ -116,27 +119,30 @@ template TallyVotesNonQv(
116
119
  var computedIsZero = IsZero()(computedIsFirstBatch);
117
120
 
118
121
  // Tally the new results.
119
- var computedCalculateTotalResult[numVoteOptions];
120
- for (var i = 0; i < numVoteOptions; i++) {
121
- var computedNumsRC[batchSize + 1];
122
- computedNumsRC[batchSize] = currentResults[i] * computedIsZero;
122
+ var computedCalculateTotalResult[totalVoteOptions];
123
+
124
+ for (var i = 0; i < totalVoteOptions; i++) {
125
+ var computedVotes[batchSize + 1];
126
+ computedVotes[batchSize] = currentResults[i] * computedIsZero;
127
+
123
128
  for (var j = 0; j < batchSize; j++) {
124
- computedNumsRC[j] = votes[j][i];
129
+ computedVotes[j] = votes[j][i];
125
130
  }
126
131
 
127
- computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedNumsRC);
132
+ computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
128
133
  }
129
134
 
130
135
  // Tally the new spent voice credit total.
131
- var computedNumsSVC[batchSize * numVoteOptions + 1];
132
- computedNumsSVC[batchSize * numVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
136
+ var computedTotalVoiceCreditSpent[batchSize * totalVoteOptions + 1];
137
+ computedTotalVoiceCreditSpent[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
138
+
133
139
  for (var i = 0; i < batchSize; i++) {
134
- for (var j = 0; j < numVoteOptions; j++) {
135
- computedNumsSVC[i * numVoteOptions + j] = votes[i][j];
140
+ for (var j = 0; j < totalVoteOptions; j++) {
141
+ computedTotalVoiceCreditSpent[i * totalVoteOptions + j] = votes[i][j];
136
142
  }
137
143
  }
138
144
 
139
- var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * numVoteOptions + 1)(computedNumsSVC);
145
+ var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedTotalVoiceCreditSpent);
140
146
 
141
147
  // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
142
148
  ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
@@ -163,7 +169,7 @@ template TallyVotesNonQv(
163
169
  // Number of children per node in the tree, defining the tree's branching factor.
164
170
  var TREE_ARITY = 5;
165
171
  // Number of voting options available, determined by the depth of the vote option tree.
166
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
172
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
167
173
 
168
174
  // Equal to 1 if this is the first batch, otherwise 0.
169
175
  signal input isFirstBatch;
@@ -173,12 +179,12 @@ template TallyVotesNonQv(
173
179
  signal input newTallyCommitment;
174
180
 
175
181
  // Current results for each vote option.
176
- signal input currentResults[numVoteOptions];
182
+ signal input currentResults[totalVoteOptions];
177
183
  // Salt for the root of the current results.
178
184
  signal input currentResultsRootSalt;
179
185
 
180
186
  // New results for each vote option.
181
- signal input newResults[numVoteOptions];
187
+ signal input newResults[totalVoteOptions];
182
188
  // Salt for the root of the new results.
183
189
  signal input newResultsRootSalt;
184
190
 
@@ -193,7 +199,7 @@ template TallyVotesNonQv(
193
199
  signal input newSpentVoiceCreditSubtotalSalt;
194
200
 
195
201
  // Compute the commitment to the current results.
196
- var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
202
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
197
203
 
198
204
  // Verify currentResultsCommitmentHash.
199
205
  var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
@@ -209,14 +215,14 @@ template TallyVotesNonQv(
209
215
  // computedIsZero.out is 0 if this is the first batch.
210
216
  var computedIsZero = IsZero()(isFirstBatch);
211
217
 
212
- // hz is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
213
- // hz is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
214
- signal hz;
215
- hz <== computedIsZero * computedCurrentTallyCommitment;
216
- hz === currentTallyCommitment;
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;
217
223
 
218
224
  // Compute the root of the new results.
219
- var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
225
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
220
226
  var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
221
227
 
222
228
  // Compute the commitment to the new spent voice credits value.