@maci-protocol/circuits 0.0.0-ci.044d30d → 0.0.0-ci.0bef05d

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 (37) hide show
  1. package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
  2. package/build/ts/generateZkeys.d.ts.map +1 -0
  3. package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
  4. package/build/ts/generateZkeys.js.map +1 -0
  5. package/build/ts/types.d.ts +8 -8
  6. package/build/ts/types.d.ts.map +1 -1
  7. package/build/tsconfig.build.tsbuildinfo +1 -1
  8. package/circom/circuits.json +7 -7
  9. package/circom/coordinator/non-qv/processMessages.circom +92 -95
  10. package/circom/coordinator/non-qv/tallyVotes.circom +35 -32
  11. package/circom/coordinator/qv/processMessages.circom +94 -94
  12. package/circom/coordinator/qv/tallyVotes.circom +37 -37
  13. package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +2 -0
  14. package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
  15. package/circom/utils/MessageHasher.circom +57 -0
  16. package/circom/utils/MessageToCommand.circom +107 -0
  17. package/circom/utils/PoseidonHasher.circom +29 -0
  18. package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +11 -9
  19. package/circom/utils/VerifySignature.circom +39 -0
  20. package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +13 -11
  21. package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +32 -32
  22. package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +13 -11
  23. package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +32 -32
  24. package/circom/utils/trees/BinaryMerkleRoot.circom +11 -3
  25. package/circom/utils/trees/CheckRoot.circom +18 -14
  26. package/circom/utils/trees/LeafExists.circom +1 -1
  27. package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +11 -7
  28. package/circom/utils/trees/MerkleTreeInclusionProof.circom +6 -5
  29. package/circom/utils/trees/incrementalQuinaryTree.circom +2 -2
  30. package/circom/voter/PollJoined.circom +43 -0
  31. package/circom/voter/PollJoining.circom +54 -0
  32. package/package.json +12 -11
  33. package/build/ts/genZkeys.d.ts.map +0 -1
  34. package/build/ts/genZkeys.js.map +0 -1
  35. package/circom/utils/hashers.circom +0 -78
  36. package/circom/utils/messageToCommand.circom +0 -78
  37. package/circom/voter/poll.circom +0 -92
@@ -5,16 +5,16 @@ 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/qv/stateLeafAndBallotTransformer.circom";
8
+ include "../../utils/PoseidonHasher.circom";
9
+ include "../../utils/MessageHasher.circom";
10
+ include "../../utils/MessageToCommand.circom";
11
+ include "../../utils/PrivateToPublicKey.circom";
12
+ include "../../utils/qv/StateLeafAndBallotTransformer.circom";
12
13
  include "../../utils/trees/incrementalQuinaryTree.circom";
13
- // Merkle tree utilities (split from incrementalMerkleTree.circom)
14
14
  include "../../utils/trees/MerkleTreeInclusionProof.circom";
15
15
  include "../../utils/trees/LeafExists.circom";
16
16
  include "../../utils/trees/CheckRoot.circom";
17
- include "../../utils/trees/MerkleGeneratePathIndices.circom";
17
+ include "../../utils/trees/MerklePathIndicesGenerator.circom";
18
18
  include "../../utils/trees/BinaryMerkleRoot.circom";
19
19
 
20
20
  /**
@@ -35,31 +35,31 @@ template ProcessMessages(
35
35
  var VOTE_OPTION_TREE_ARITY = 5;
36
36
  // Default for binary trees.
37
37
  var STATE_TREE_ARITY = 2;
38
- var MSG_LENGTH = 10;
39
- var PACKED_CMD_LENGTH = 4;
38
+ var MESSAGE_LENGTH = 10;
39
+ var PACKED_COMMAND_LENGTH = 4;
40
40
  var STATE_LEAF_LENGTH = 3;
41
41
  var BALLOT_LENGTH = 2;
42
- var BALLOT_NONCE_IDX = 0;
43
- var BALLOT_VO_ROOT_IDX = 1;
44
- var STATE_LEAF_PUB_X_IDX = 0;
45
- var STATE_LEAF_PUB_Y_IDX = 1;
46
- var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
47
- var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
42
+ var BALLOT_NONCE_INDEX = 0;
43
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
44
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
45
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
46
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
47
+ var MESSAGE_TREE_ZERO_VALUE = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
48
48
  // Number of options for this poll.
49
49
  var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
50
50
 
51
51
  // Number of users that have completed the sign up.
52
- signal input numSignUps;
52
+ signal input totalSignups;
53
53
  // Value of chainHash at beginning of batch
54
54
  signal input inputBatchHash;
55
55
  // Value of chainHash at end of batch
56
56
  signal input outputBatchHash;
57
57
  // The messages.
58
- signal input msgs[batchSize][MSG_LENGTH];
58
+ signal input messages[batchSize][MESSAGE_LENGTH];
59
59
  // The coordinator's private key.
60
- signal input coordPrivKey;
60
+ signal input coordinatorPrivateKey;
61
61
  // The ECDH public key per message.
62
- signal input encPubKeys[batchSize][2];
62
+ signal input encryptionPublicKeys[batchSize][2];
63
63
  // The current state root (before the processing).
64
64
  signal input currentStateRoot;
65
65
  // The actual tree depth (might be <= stateTreeDepth).
@@ -122,10 +122,10 @@ template ProcessMessages(
122
122
  var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
123
123
  voteOptionsValid === 1;
124
124
 
125
- // Check numSignUps <= the max number of users (i.e., number of state leaves
125
+ // Check totalSignups <= the max number of users (i.e., number of state leaves
126
126
  // that can fit the state tree).
127
- var numSignUpsValid = LessEqThan(32)([numSignUps, STATE_TREE_ARITY ** stateTreeDepth]);
128
- numSignUpsValid === 1;
127
+ var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
128
+ totalSignupsValid === 1;
129
129
 
130
130
  // Hash each Message to check their existence in the Message tree.
131
131
  var computedMessageHashers[batchSize];
@@ -135,7 +135,7 @@ template ProcessMessages(
135
135
 
136
136
  for (var i = 0; i < batchSize; i++) {
137
137
  // calculate message hash
138
- computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
138
+ computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
139
139
  // check if message is valid or not (if index of message is less than index of last valid message in batch)
140
140
  var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
141
141
  // calculate chain hash if message is valid
@@ -159,38 +159,38 @@ template ProcessMessages(
159
159
  // Ensure that the coordinator's public key from the contract is correct
160
160
  // based on the given private key - that is, the prover knows the
161
161
  // coordinator's private key.
162
- var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
163
- var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
164
- derivedPubKeyHash === coordinatorPublicKeyHash;
162
+ var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
163
+ var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
164
+ derivedPublicKeyHash === coordinatorPublicKeyHash;
165
165
 
166
166
  // Decrypt each Message into a Command.
167
167
  // The command i-th is composed by the following fields.
168
168
  // e.g., command 0 is made of commandsStateIndex[0],
169
- // commandsNewPubKey[0], ..., commandsPackedCommandOut[0]
169
+ // commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
170
170
  var computedCommandsStateIndex[batchSize];
171
- var computedCommandsNewPubKey[batchSize][2];
171
+ var computedCommandsNewPublicKey[batchSize][2];
172
172
  var computedCommandsVoteOptionIndex[batchSize];
173
173
  var computedCommandsNewVoteWeight[batchSize];
174
174
  var computedCommandsNonce[batchSize];
175
175
  var computedCommandsPollId[batchSize];
176
176
  var computedCommandsSalt[batchSize];
177
- var computedCommandsSigR8[batchSize][2];
178
- var computedCommandsSigS[batchSize];
179
- var computedCommandsPackedCommandOut[batchSize][PACKED_CMD_LENGTH];
177
+ var computedCommandsSignaturePoint[batchSize][2];
178
+ var computedCommandsSignatureScalar[batchSize];
179
+ var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
180
180
 
181
181
  for (var i = 0; i < batchSize; i++) {
182
182
  (
183
183
  computedCommandsStateIndex[i],
184
- computedCommandsNewPubKey[i],
184
+ computedCommandsNewPublicKey[i],
185
185
  computedCommandsVoteOptionIndex[i],
186
186
  computedCommandsNewVoteWeight[i],
187
187
  computedCommandsNonce[i],
188
188
  computedCommandsPollId[i],
189
189
  computedCommandsSalt[i],
190
- computedCommandsSigR8[i],
191
- computedCommandsSigS[i],
190
+ computedCommandsSignaturePoint[i],
191
+ computedCommandsSignatureScalar[i],
192
192
  computedCommandsPackedCommandOut[i]
193
- ) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
193
+ ) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
194
194
  }
195
195
 
196
196
  // Process messages in reverse order.
@@ -223,7 +223,7 @@ template ProcessMessages(
223
223
  }
224
224
 
225
225
  (computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOne(stateTreeDepth, voteOptionTreeDepth)(
226
- numSignUps,
226
+ totalSignups,
227
227
  stateRoots[i + 1],
228
228
  ballotRoots[i + 1],
229
229
  actualStateTreeDepth,
@@ -234,14 +234,14 @@ template ProcessMessages(
234
234
  currentVoteWeights[i],
235
235
  currentVoteWeightsPathElement,
236
236
  computedCommandsStateIndex[i],
237
- computedCommandsNewPubKey[i],
237
+ computedCommandsNewPublicKey[i],
238
238
  computedCommandsVoteOptionIndex[i],
239
239
  computedCommandsNewVoteWeight[i],
240
240
  computedCommandsNonce[i],
241
241
  computedCommandsPollId[i],
242
242
  computedCommandsSalt[i],
243
- computedCommandsSigR8[i],
244
- computedCommandsSigS[i],
243
+ computedCommandsSignaturePoint[i],
244
+ computedCommandsSignatureScalar[i],
245
245
  computedCommandsPackedCommandOut[i],
246
246
  voteOptions
247
247
  );
@@ -266,24 +266,24 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
266
266
  // Constants defining the structure and size of state and ballots.
267
267
  var STATE_LEAF_LENGTH = 3;
268
268
  var BALLOT_LENGTH = 2;
269
- var MSG_LENGTH = 10;
270
- var PACKED_CMD_LENGTH = 4;
269
+ var MESSAGE_LENGTH = 10;
270
+ var PACKED_COMMAND_LENGTH = 4;
271
271
  var VOTE_OPTION_TREE_ARITY = 5;
272
272
  var STATE_TREE_ARITY = 2;
273
- var BALLOT_NONCE_IDX = 0;
274
- // Ballot vote option (VO) root index.
275
- var BALLOT_VO_ROOT_IDX = 1;
273
+ var BALLOT_NONCE_INDEX = 0;
274
+ // Ballot vote option (vote option) root index.
275
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
276
276
 
277
277
  // Indices for elements within a state leaf.
278
278
  // Public key.
279
- var STATE_LEAF_PUB_X_IDX = 0;
280
- var STATE_LEAF_PUB_Y_IDX = 1;
279
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
280
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
281
281
  // Voice Credit balance.
282
- var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
283
- var N_BITS = 252;
282
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
283
+ var NUMBER_BITS = 252;
284
284
 
285
285
  // Number of users that have completed the sign up.
286
- signal input numSignUps;
286
+ signal input totalSignups;
287
287
  // The current value of the state tree root.
288
288
  signal input currentStateRoot;
289
289
  // The current value of the ballot tree root.
@@ -305,16 +305,16 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
305
305
  signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
306
306
 
307
307
  // Inputs related to the command being processed.
308
- signal input cmdStateIndex;
309
- signal input cmdNewPubKey[2];
310
- signal input cmdVoteOptionIndex;
311
- signal input cmdNewVoteWeight;
312
- signal input cmdNonce;
313
- signal input cmdPollId;
314
- signal input cmdSalt;
315
- signal input cmdSigR8[2];
316
- signal input cmdSigS;
317
- signal input packedCmd[PACKED_CMD_LENGTH];
308
+ signal input commandStateIndex;
309
+ signal input commandPublicKey[2];
310
+ signal input commandVoteOptionIndex;
311
+ signal input commandNewVoteWeight;
312
+ signal input commandNonce;
313
+ signal input commandPollId;
314
+ signal input commandSalt;
315
+ signal input commandSignaturePoint[2];
316
+ signal input commandSignatureScalar;
317
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
318
318
 
319
319
  // The number of valid vote options for the poll.
320
320
  signal input voteOptions;
@@ -325,37 +325,37 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
325
325
  // Intermediate signals.
326
326
  // currentVoteWeight * currentVoteWeight.
327
327
  signal currentVoteWeightSquare;
328
- // cmdNewVoteWeight * cmdNewVoteWeight.
329
- signal cmdNewVoteWeightSquare;
330
- // equal to newBallotVoRootMux (Mux1).
328
+ // commandNewVoteWeight * commandNewVoteWeight.
329
+ signal commandNewVoteWeightSquare;
330
+ // equal to newBallotVoteOptionRootMux (Mux1).
331
331
  signal newBallotVoRoot;
332
332
 
333
333
  // 1. Transform a state leaf and a ballot with a command.
334
334
  // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
335
- var computedNewSlPubKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
336
- (computedNewSlPubKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
337
- numSignUps,
335
+ var computedNewstateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
336
+ (computedNewstateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
337
+ totalSignups,
338
338
  voteOptions,
339
- [stateLeaf[STATE_LEAF_PUB_X_IDX], stateLeaf[STATE_LEAF_PUB_Y_IDX]],
340
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
341
- ballot[BALLOT_NONCE_IDX],
339
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
340
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
341
+ ballot[BALLOT_NONCE_INDEX],
342
342
  currentVoteWeight,
343
- cmdStateIndex,
344
- cmdNewPubKey,
345
- cmdVoteOptionIndex,
346
- cmdNewVoteWeight,
347
- cmdNonce,
348
- cmdPollId,
349
- cmdSalt,
350
- cmdSigR8,
351
- cmdSigS,
352
- packedCmd
343
+ commandStateIndex,
344
+ commandPublicKey,
345
+ commandVoteOptionIndex,
346
+ commandNewVoteWeight,
347
+ commandNonce,
348
+ commandPollId,
349
+ commandSalt,
350
+ commandSignaturePoint,
351
+ commandSignatureScalar,
352
+ packedCommand
353
353
  );
354
354
 
355
355
  // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
356
356
  // Otherwise, generate indices for command.stateIndex.
357
- var stateIndexMux = Mux1()([0, cmdStateIndex], computedIsStateLeafIndexValid);
358
- var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
357
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
358
+ var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
359
359
 
360
360
  // 3. Verify that the original state leaf exists in the given state root.
361
361
  var stateLeafHash = PoseidonHasher(3)(stateLeaf);
@@ -370,8 +370,8 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
370
370
 
371
371
  // 4. Verify that the original ballot exists in the given ballot root.
372
372
  var computedBallot = PoseidonHasher(2)([
373
- ballot[BALLOT_NONCE_IDX],
374
- ballot[BALLOT_VO_ROOT_IDX]
373
+ ballot[BALLOT_NONCE_INDEX],
374
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
375
375
  ]);
376
376
 
377
377
  var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
@@ -383,12 +383,12 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
383
383
  computedBallotQip === currentBallotRoot;
384
384
 
385
385
  // 5. Verify that currentVoteWeight exists in the ballot's vote option root
386
- // at cmdVoteOptionIndex.
386
+ // at commandVoteOptionIndex.
387
387
  currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
388
- cmdNewVoteWeightSquare <== cmdNewVoteWeight * cmdNewVoteWeight;
388
+ commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
389
389
 
390
- var cmdVoteOptionIndexMux = Mux1()([0, cmdVoteOptionIndex], computedIsVoteOptionIndexValid);
391
- var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(cmdVoteOptionIndexMux);
390
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
391
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
392
392
 
393
393
  var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
394
394
  currentVoteWeight,
@@ -396,13 +396,13 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
396
396
  currentVoteWeightsPathElements
397
397
  );
398
398
 
399
- computedCurrentVoteWeightQip === ballot[BALLOT_VO_ROOT_IDX];
399
+ computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
400
400
 
401
- var voteWeightMux = Mux1()([currentVoteWeight, cmdNewVoteWeight], computedIsValid);
401
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
402
402
  var voiceCreditBalanceMux = Mux1()(
403
403
  [
404
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
405
- stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] + currentVoteWeightSquare - cmdNewVoteWeightSquare
404
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
405
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeightSquare - commandNewVoteWeightSquare
406
406
  ],
407
407
  computedIsValid
408
408
  );
@@ -415,17 +415,17 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
415
415
  );
416
416
 
417
417
  // The new vote option root in the ballot
418
- var newBallotVoRootMux = Mux1()(
419
- [ballot[BALLOT_VO_ROOT_IDX], computedNewVoteOptionTreeQip],
418
+ var newBallotVoteOptionRootMux = Mux1()(
419
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
420
420
  computedIsValid
421
421
  );
422
422
 
423
- newBallotVoRoot <== newBallotVoRootMux;
423
+ newBallotVoRoot <== newBallotVoteOptionRootMux;
424
424
 
425
425
  // 6. Generate a new state root.
426
426
  var computedNewStateLeafhash = PoseidonHasher(3)([
427
- computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
428
- computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
427
+ computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
428
+ computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
429
429
  voiceCreditBalanceMux
430
430
  ]);
431
431
 
@@ -6,11 +6,11 @@ include "./comparators.circom";
6
6
  include "./unpack-element.circom";
7
7
  // local imports
8
8
  include "../../utils/trees/CheckRoot.circom";
9
- include "../../utils/trees/MerkleGeneratePathIndices.circom";
9
+ include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
10
  include "../../utils/trees/LeafExists.circom";
11
11
  include "../../utils/trees/incrementalQuinaryTree.circom";
12
- include "../../utils/calculateTotal.circom";
13
- include "../../utils/hashers.circom";
12
+ include "../../utils/CalculateTotal.circom";
13
+ include "../../utils/PoseidonHasher.circom";
14
14
 
15
15
  /**
16
16
  * Processes batches of votes and verifies their validity in a Merkle tree structure.
@@ -35,16 +35,16 @@ template TallyVotes(
35
35
  // The number of ballots processed at once, determined by the depth of the intermediate state tree.
36
36
  var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
37
37
  // Number of voting options available, determined by the depth of the vote option tree.
38
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
38
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
39
39
 
40
40
  // Number of elements in each ballot.
41
41
  var BALLOT_LENGTH = 2;
42
42
  // Index for the nonce in the ballot array.
43
- var BALLOT_NONCE_IDX = 0;
43
+ var BALLOT_NONCE_INDEX = 0;
44
44
  // Index for the voting option root in the ballot array.
45
- var BALLOT_VO_ROOT_IDX = 1;
45
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
46
46
  // Difference in tree depths, used in path calculations.
47
- var k = stateTreeDepth - intStateTreeDepth;
47
+ var STATE_INT_TREE_DEPTH_DIFFERENCE = stateTreeDepth - intStateTreeDepth;
48
48
 
49
49
  // Root of the state Merkle tree, representing the overall state before voting.
50
50
  signal input stateRoot;
@@ -61,13 +61,13 @@ template TallyVotes(
61
61
  // Start index of given batch
62
62
  signal input index;
63
63
  // Number of users that signup
64
- signal input numSignUps;
64
+ signal input totalSignups;
65
65
  // Ballots and their corresponding path elements for verification in the tree.
66
66
  signal input ballots[batchSize][BALLOT_LENGTH];
67
- signal input ballotPathElements[k][BALLOT_TREE_ARITY - 1];
68
- signal input votes[batchSize][numVoteOptions];
67
+ signal input ballotPathElements[STATE_INT_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
68
+ signal input votes[batchSize][totalVoteOptions];
69
69
  // Current results for each vote option.
70
- signal input currentResults[numVoteOptions];
70
+ signal input currentResults[totalVoteOptions];
71
71
  // Salt for the root of the current results.
72
72
  signal input currentResultsRootSalt;
73
73
  // Total voice credits spent so far.
@@ -75,7 +75,7 @@ template TallyVotes(
75
75
  // Salt for the total spent voice credits.
76
76
  signal input currentSpentVoiceCreditSubtotalSalt;
77
77
  // Spent voice credits per vote option.
78
- signal input currentPerVOSpentVoiceCredits[numVoteOptions];
78
+ signal input currentPerVOSpentVoiceCredits[totalVoteOptions];
79
79
  // Salt for the root of spent credits per option.
80
80
  signal input currentPerVOSpentVoiceCreditsRootSalt;
81
81
  // Salt for the root of the new results.
@@ -90,21 +90,21 @@ template TallyVotes(
90
90
  computedSbCommitment === sbCommitment;
91
91
 
92
92
  // Validates that the index is within the valid range of sign-ups.
93
- var numSignUpsValid = LessEqThan(50)([index, numSignUps]);
94
- numSignUpsValid === 1;
93
+ var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
94
+ totalSignupsValid === 1;
95
95
 
96
96
  // Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
97
97
  var computedBallotHashers[batchSize];
98
98
 
99
99
  for (var i = 0; i < batchSize; i++) {
100
- computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_IDX], ballots[i][BALLOT_VO_ROOT_IDX]]);
100
+ computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
101
101
  }
102
102
 
103
103
  var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
104
- var computedBallotPathIndices[k] = MerkleGeneratePathIndices(k)(index / batchSize);
104
+ var computedBallotPathIndices[STATE_INT_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_INT_TREE_DEPTH_DIFFERENCE)(index / batchSize);
105
105
 
106
106
  // Verifies each ballot's existence within the ballot tree.
107
- LeafExists(k)(
107
+ LeafExists(STATE_INT_TREE_DEPTH_DIFFERENCE)(
108
108
  computedBallotSubroot,
109
109
  ballotPathElements,
110
110
  computedBallotPathIndices,
@@ -115,7 +115,7 @@ template TallyVotes(
115
115
  var computedVoteTree[batchSize];
116
116
  for (var i = 0; i < batchSize; i++) {
117
117
  computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
118
- computedVoteTree[i] === ballots[i][BALLOT_VO_ROOT_IDX];
118
+ computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
119
119
  }
120
120
 
121
121
  // Calculates new results and spent voice credits based on the current and incoming votes.
@@ -123,8 +123,8 @@ template TallyVotes(
123
123
  var computedIsZero = IsZero()(computedIsFirstBatch);
124
124
 
125
125
  // Tally the new results.
126
- var computedCalculateTotalResult[numVoteOptions];
127
- for (var i = 0; i < numVoteOptions; i++) {
126
+ var computedCalculateTotalResult[totalVoteOptions];
127
+ for (var i = 0; i < totalVoteOptions; i++) {
128
128
  var numsRC[batchSize + 1];
129
129
  numsRC[batchSize] = currentResults[i] * computedIsZero;
130
130
  for (var j = 0; j < batchSize; j++) {
@@ -135,20 +135,20 @@ template TallyVotes(
135
135
  }
136
136
 
137
137
  // Tally the new spent voice credit total.
138
- var numsSVC[batchSize * numVoteOptions + 1];
139
- numsSVC[batchSize * numVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
138
+ var numsSVC[batchSize * totalVoteOptions + 1];
139
+ numsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
140
140
  for (var i = 0; i < batchSize; i++) {
141
- for (var j = 0; j < numVoteOptions; j++) {
142
- numsSVC[i * numVoteOptions + j] = votes[i][j] * votes[i][j];
141
+ for (var j = 0; j < totalVoteOptions; j++) {
142
+ numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
143
143
  }
144
144
  }
145
145
 
146
- var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * numVoteOptions + 1)(numsSVC);
146
+ var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(numsSVC);
147
147
 
148
148
  // Tally the spent voice credits per vote option.
149
- var computedNewPerVOSpentVoiceCredits[numVoteOptions];
149
+ var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
150
150
 
151
- for (var i = 0; i < numVoteOptions; i++) {
151
+ for (var i = 0; i < totalVoteOptions; i++) {
152
152
  var computedNumsSVC[batchSize + 1];
153
153
  computedNumsSVC[batchSize] = currentPerVOSpentVoiceCredits[i] * computedIsZero;
154
154
  for (var j = 0; j < batchSize; j++) {
@@ -187,7 +187,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
187
187
  // Number of children per node in the tree, defining the tree's branching factor.
188
188
  var TREE_ARITY = 5;
189
189
  // Number of voting options available, determined by the depth of the vote option tree.
190
- var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
190
+ var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
191
191
 
192
192
  // Equal to 1 if this is the first batch, otherwise 0.
193
193
  signal input isFirstBatch;
@@ -197,12 +197,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
197
197
  signal input newTallyCommitment;
198
198
 
199
199
  // Current results for each vote option.
200
- signal input currentResults[numVoteOptions];
200
+ signal input currentResults[totalVoteOptions];
201
201
  // Salt for the root of the current results.
202
202
  signal input currentResultsRootSalt;
203
203
 
204
204
  // New results for each vote option.
205
- signal input newResults[numVoteOptions];
205
+ signal input newResults[totalVoteOptions];
206
206
  // Salt for the root of the new results.
207
207
  signal input newResultsRootSalt;
208
208
 
@@ -217,12 +217,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
217
217
  signal input newSpentVoiceCreditSubtotalSalt;
218
218
 
219
219
  // Spent voice credits per vote option.
220
- signal input currentPerVOSpentVoiceCredits[numVoteOptions];
220
+ signal input currentPerVOSpentVoiceCredits[totalVoteOptions];
221
221
  // Salt for the root of spent credits per option.
222
222
  signal input currentPerVOSpentVoiceCreditsRootSalt;
223
223
 
224
224
  // New spent voice credits per vote option.
225
- signal input newPerVOSpentVoiceCredits[numVoteOptions];
225
+ signal input newPerVOSpentVoiceCredits[totalVoteOptions];
226
226
  // Salt for the root of new spent credits per option.
227
227
  signal input newPerVOSpentVoiceCreditsRootSalt;
228
228
 
@@ -251,11 +251,11 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
251
251
  // computedIsZero.out is 0 if this is the first batch.
252
252
  var computedIsZero = IsZero()(isFirstBatch);
253
253
 
254
- // hz is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
255
- // hz is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
256
- signal hz;
257
- hz <== computedIsZero * computedCurrentTallyCommitment;
258
- hz === currentTallyCommitment;
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
259
 
260
260
  // Compute the root of the new results.
261
261
  var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
@@ -8,7 +8,9 @@ pragma circom 2.0.0;
8
8
  * final output reflects the cumulative total of the inputs provided.
9
9
  */
10
10
  template CalculateTotal(n) {
11
+ // Array of values.
11
12
  signal input nums[n];
13
+ // Total sum.
12
14
  signal output sum;
13
15
 
14
16
  signal sums[n];