@maci-protocol/circuits 0.0.0-ci.4a1da43 → 0.0.0-ci.4b8eeec

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 (29) hide show
  1. package/LICENSE +1 -2
  2. package/build/ts/types.d.ts +3 -3
  3. package/build/ts/types.d.ts.map +1 -1
  4. package/build/tsconfig.build.tsbuildinfo +1 -1
  5. package/circom/circuits.json +16 -0
  6. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  7. package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
  8. package/circom/coordinator/non-qv/processMessages.circom +15 -14
  9. package/circom/coordinator/non-qv/tallyVotes.circom +8 -8
  10. package/circom/coordinator/qv/processMessages.circom +14 -13
  11. package/circom/coordinator/qv/tallyVotes.circom +24 -24
  12. package/circom/utils/PrivateToPublicKey.circom +3 -3
  13. package/circom/utils/full/MessageValidator.circom +91 -0
  14. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  15. package/circom/utils/non-qv/MessageValidator.circom +4 -4
  16. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +7 -7
  17. package/circom/utils/qv/MessageValidator.circom +4 -4
  18. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +7 -7
  19. package/circom/utils/trees/LeafExists.circom +2 -2
  20. package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
  21. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  22. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  23. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  24. package/circom/utils/trees/QuinarySelector.circom +42 -0
  25. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  26. package/circom/utils/trees/Splicer.circom +76 -0
  27. package/circom/voter/PollJoined.circom +2 -2
  28. package/package.json +9 -7
  29. package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
@@ -10,7 +10,8 @@ include "../../utils/MessageHasher.circom";
10
10
  include "../../utils/MessageToCommand.circom";
11
11
  include "../../utils/PrivateToPublicKey.circom";
12
12
  include "../../utils/qv/StateLeafAndBallotTransformer.circom";
13
- include "../../utils/trees/incrementalQuinaryTree.circom";
13
+ include "../../utils/trees/QuinaryTreeInclusionProof.circom";
14
+ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
14
15
  include "../../utils/trees/MerkleTreeInclusionProof.circom";
15
16
  include "../../utils/trees/LeafExists.circom";
16
17
  include "../../utils/trees/CheckRoot.circom";
@@ -328,12 +329,12 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
328
329
  // commandNewVoteWeight * commandNewVoteWeight.
329
330
  signal commandNewVoteWeightSquare;
330
331
  // equal to newBallotVoteOptionRootMux (Mux1).
331
- signal newBallotVoRoot;
332
+ signal newBallotVoteOptionRoot;
332
333
 
333
334
  // 1. Transform a state leaf and a ballot with a command.
334
335
  // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
335
- var computedNewstateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
336
- (computedNewstateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
336
+ var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
337
+ (computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
337
338
  totalSignups,
338
339
  voteOptions,
339
340
  [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
@@ -388,9 +389,9 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
388
389
  commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
389
390
 
390
391
  var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
391
- var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
392
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
392
393
 
393
- var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
394
+ var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
394
395
  currentVoteWeight,
395
396
  computedCurrentVoteWeightPathIndices,
396
397
  currentVoteWeightsPathElements
@@ -408,7 +409,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
408
409
  );
409
410
 
410
411
  // 5.1. Update the ballot's vote option root with the new vote weight.
411
- var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
412
+ var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
412
413
  voteWeightMux,
413
414
  computedCurrentVoteWeightPathIndices,
414
415
  currentVoteWeightsPathElements
@@ -420,17 +421,17 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
420
421
  computedIsValid
421
422
  );
422
423
 
423
- newBallotVoRoot <== newBallotVoteOptionRootMux;
424
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
424
425
 
425
426
  // 6. Generate a new state root.
426
- var computedNewStateLeafhash = PoseidonHasher(3)([
427
- computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
428
- computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
427
+ var computedNewStateLeafHash = PoseidonHasher(3)([
428
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
429
+ computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
429
430
  voiceCreditBalanceMux
430
431
  ]);
431
432
 
432
433
  var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
433
- computedNewStateLeafhash,
434
+ computedNewStateLeafHash,
434
435
  actualStateTreeDepth,
435
436
  computedStateLeafPathIndices,
436
437
  stateLeafPathElements
@@ -439,7 +440,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
439
440
  newStateRoot <== computedNewStateLeafQip;
440
441
 
441
442
  // 7. Generate a new ballot root.
442
- var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoRoot]);
443
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
443
444
  var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
444
445
  computedNewBallot,
445
446
  computedStateLeafPathIndices,
@@ -8,7 +8,7 @@ include "./unpack-element.circom";
8
8
  include "../../utils/trees/CheckRoot.circom";
9
9
  include "../../utils/trees/MerklePathIndicesGenerator.circom";
10
10
  include "../../utils/trees/LeafExists.circom";
11
- include "../../utils/trees/incrementalQuinaryTree.circom";
11
+ include "../../utils/trees/QuinaryCheckRoot.circom";
12
12
  include "../../utils/CalculateTotal.circom";
13
13
  include "../../utils/PoseidonHasher.circom";
14
14
 
@@ -75,13 +75,13 @@ 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[totalVoteOptions];
78
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
79
79
  // Salt for the root of spent credits per option.
80
- signal input currentPerVOSpentVoiceCreditsRootSalt;
80
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
81
81
  // Salt for the root of the new results.
82
82
  signal input newResultsRootSalt;
83
83
  // Salt for the new spent credits per vote option root.
84
- signal input newPerVOSpentVoiceCreditsRootSalt;
84
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
85
85
  // Salt for the new total spent voice credits root.
86
86
  signal input newSpentVoiceCreditSubtotalSalt;
87
87
 
@@ -114,7 +114,7 @@ template TallyVotes(
114
114
  // Processes vote options, verifying each against its declared root.
115
115
  var computedVoteTree[batchSize];
116
116
  for (var i = 0; i < batchSize; i++) {
117
- computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
117
+ computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
118
118
  computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
119
119
  }
120
120
 
@@ -149,13 +149,13 @@ template TallyVotes(
149
149
  var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
150
150
 
151
151
  for (var i = 0; i < totalVoteOptions; i++) {
152
- var computedNumsSVC[batchSize + 1];
153
- computedNumsSVC[batchSize] = currentPerVOSpentVoiceCredits[i] * computedIsZero;
152
+ var computedTotalVoiceCreditSpent[batchSize + 1];
153
+ computedTotalVoiceCreditSpent[batchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
154
154
  for (var j = 0; j < batchSize; j++) {
155
- computedNumsSVC[j] = votes[j][i] * votes[j][i];
155
+ computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
156
156
  }
157
157
 
158
- computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedNumsSVC);
158
+ computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedTotalVoiceCreditSpent);
159
159
  }
160
160
 
161
161
  // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
@@ -171,10 +171,10 @@ template TallyVotes(
171
171
  currentSpentVoiceCreditSubtotalSalt,
172
172
  computedNewSpentVoiceCreditSubtotal,
173
173
  newSpentVoiceCreditSubtotalSalt,
174
- currentPerVOSpentVoiceCredits,
175
- currentPerVOSpentVoiceCreditsRootSalt,
174
+ currentPerVoteOptionSpentVoiceCredits,
175
+ currentPerVoteOptionSpentVoiceCreditsRootSalt,
176
176
  computedNewPerVOSpentVoiceCredits,
177
- newPerVOSpentVoiceCreditsRootSalt
177
+ newPerVoteOptionSpentVoiceCreditsRootSalt
178
178
  );
179
179
  }
180
180
 
@@ -217,17 +217,17 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
217
217
  signal input newSpentVoiceCreditSubtotalSalt;
218
218
 
219
219
  // Spent voice credits per vote option.
220
- signal input currentPerVOSpentVoiceCredits[totalVoteOptions];
220
+ signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
221
221
  // Salt for the root of spent credits per option.
222
- signal input currentPerVOSpentVoiceCreditsRootSalt;
222
+ signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
223
223
 
224
224
  // New spent voice credits per vote option.
225
- signal input newPerVOSpentVoiceCredits[totalVoteOptions];
225
+ signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
226
226
  // Salt for the root of new spent credits per option.
227
- signal input newPerVOSpentVoiceCreditsRootSalt;
227
+ signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
228
228
 
229
229
  // Compute the commitment to the current results.
230
- var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
230
+ var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
231
231
 
232
232
  // Verify currentResultsCommitmentHash.
233
233
  var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
@@ -236,14 +236,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
236
236
  var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
237
237
 
238
238
  // Compute the root of the spent voice credits per vote option.
239
- var computedCurrentPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentPerVOSpentVoiceCredits);
240
- var computedCurrentPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVOSpentVoiceCreditsRoot, currentPerVOSpentVoiceCreditsRootSalt]);
239
+ var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
240
+ var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
241
241
 
242
242
  // Commit to the current tally.
243
243
  var computedCurrentTallyCommitment = PoseidonHasher(3)([
244
244
  computedCurrentResultsCommitment,
245
245
  computedCurrentSpentVoiceCreditsCommitment,
246
- computedCurrentPerVOSpentVoiceCreditsCommitment
246
+ computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
247
247
  ]);
248
248
 
249
249
  // Check if the current tally commitment is correct only if this is not the first batch.
@@ -258,21 +258,21 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
258
258
  isFirstCommitment === currentTallyCommitment;
259
259
 
260
260
  // Compute the root of the new results.
261
- var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
261
+ var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
262
262
  var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
263
263
 
264
264
  // Compute the commitment to the new spent voice credits value.
265
265
  var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
266
266
 
267
267
  // Compute the root of the spent voice credits per vote option.
268
- var computedNewPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(newPerVOSpentVoiceCredits);
269
- var computedNewPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVOSpentVoiceCreditsRoot, newPerVOSpentVoiceCreditsRootSalt]);
268
+ var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
269
+ var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
270
270
 
271
271
  // Commit to the new tally.
272
272
  var computedNewTallyCommitment = PoseidonHasher(3)([
273
273
  computedNewResultsCommitment,
274
274
  computedNewSpentVoiceCreditsCommitment,
275
- computedNewPerVOSpentVoiceCreditsCommitment
275
+ computedNewPerVoteOptionSpentVoiceCreditsCommitment
276
276
  ]);
277
277
 
278
278
  computedNewTallyCommitment === newTallyCommitment;
@@ -29,10 +29,10 @@ template PrivateToPublicKey() {
29
29
  isLessThan === 1;
30
30
 
31
31
  // Convert the private key to bits.
32
- var computedPrivBits[253] = Num2Bits(253)(privateKey);
32
+ var computedPrivateBits[253] = Num2Bits(253)(privateKey);
33
33
 
34
34
  // Perform scalar multiplication with the basepoint.
35
- var computedEscalarMulFix[2] = EscalarMulFix(253, BASE8)(computedPrivBits);
35
+ var computedPublicKey[2] = EscalarMulFix(253, BASE8)(computedPrivateBits);
36
36
 
37
- publicKey <== computedEscalarMulFix;
37
+ publicKey <== computedPublicKey;
38
38
  }
@@ -0,0 +1,91 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit imports
4
+ include "./safe-comparators.circom";
5
+ // local imports
6
+ include "../VerifySignature.circom";
7
+
8
+ /**
9
+ * Checks if a MACI message is valid or not.
10
+ * This template supports the full mode (all credits are spent on one option)
11
+ */
12
+ template MessageValidatorFull() {
13
+ // Length of the packed command.
14
+ var PACKED_COMMAND_LENGTH = 4;
15
+ // Number of checks to be performed.
16
+ var TOTAL_CHECKS = 5;
17
+
18
+ // State index of the user.
19
+ signal input stateTreeIndex;
20
+ // Number of user sign-ups in the state tree.
21
+ signal input totalSignups;
22
+ // Vote option index.
23
+ signal input voteOptionIndex;
24
+ // Number of valid vote options for the poll.
25
+ signal input voteOptions;
26
+ // Ballot nonce.
27
+ signal input originalNonce;
28
+ // Command nonce.
29
+ signal input commandNonce;
30
+ // Packed command.
31
+ signal input command[PACKED_COMMAND_LENGTH];
32
+ // Public key of the state leaf (user).
33
+ signal input publicKey[2];
34
+ // EdDSA signature of the command (R part).
35
+ signal input signaturePoint[2];
36
+ // EdDSA signature of the command (S part).
37
+ signal input signatureScalar;
38
+ // State leaf current voice credit balance.
39
+ signal input currentVoiceCreditBalance;
40
+ // Current number of votes for specific option.
41
+ signal input currentVotesForOption;
42
+ // Vote weight.
43
+ signal input voteWeight;
44
+
45
+ // True when the command is valid; otherwise false.
46
+ signal output isValid;
47
+ // True if the state leaf index is valid
48
+ signal output isStateLeafIndexValid;
49
+ // True if the vote option index is valid
50
+ signal output isVoteOptionIndexValid;
51
+
52
+ // Check (1) - The state leaf index must be valid.
53
+ // The check ensure that the stateTreeIndex < totalSignups as first validation.
54
+ // Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
55
+ // while 1 is for the first actual user.
56
+ var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
57
+
58
+ // Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
59
+ var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
60
+
61
+ // Check (3) - The nonce must be correct.
62
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
63
+
64
+ // Check (4) - The signature must be correct.
65
+ var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
66
+
67
+ // Check (5) - There must be sufficient voice credits.
68
+ // The check ensure that currentVotesForOption + currentVoiceCreditBalance is equal to voteWeight.
69
+ var computedAreVoiceCreditsSpent = IsEqual()(
70
+ [
71
+ currentVotesForOption + currentVoiceCreditBalance,
72
+ voteWeight
73
+ ]
74
+ );
75
+
76
+ // When all five checks are correct, then isValid = 1.
77
+ var computedIsUpdateValid = IsEqual()(
78
+ [
79
+ TOTAL_CHECKS,
80
+ computedIsSignatureValid +
81
+ computedAreVoiceCreditsSpent +
82
+ computedIsNonceValid +
83
+ computedIsStateLeafIndexValid +
84
+ computedIsVoteOptionIndexValid
85
+ ]
86
+ );
87
+
88
+ isValid <== computedIsUpdateValid;
89
+ isStateLeafIndexValid <== computedIsStateLeafIndexValid;
90
+ isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
91
+ }
@@ -0,0 +1,122 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./mux1.circom";
5
+ // local import
6
+ include "./MessageValidator.circom";
7
+
8
+ /**
9
+ * Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
10
+ * If the message is correct, updates the public key in the state leaf and the nonce
11
+ * in the ballot using multiplexer components.
12
+ * This template supports the full mode (all credits are spent on one option)
13
+ */
14
+ template StateLeafAndBallotTransformerFull() {
15
+ // Length of the packed command.
16
+ var PACKED_COMMAND_LENGTH = 4;
17
+
18
+ // Number of user sign-ups in the state tree.
19
+ signal input totalSignups;
20
+ // Number of valid vote options for the poll.
21
+ signal input voteOptions;
22
+
23
+ // The following signals represents a state leaf (signed up user).
24
+ // Public key.
25
+ signal input stateLeafPublicKey[2];
26
+ // Current voice credit balance.
27
+ signal input stateLeafVoiceCreditBalance;
28
+
29
+ // The following signals represents a ballot.
30
+ // Nonce.
31
+ signal input ballotNonce;
32
+ // Current number of votes for specific option.
33
+ signal input ballotCurrentVotesForOption;
34
+
35
+ // The following signals represents a command.
36
+ // State index of the user.
37
+ signal input commandStateIndex;
38
+ // Public key of the user.
39
+ signal input commandPublicKey[2];
40
+ // Vote option index.
41
+ signal input commandVoteOptionIndex;
42
+ // Vote weight.
43
+ signal input commandNewVoteWeight;
44
+ // Nonce.
45
+ signal input commandNonce;
46
+ // Poll identifier.
47
+ signal input commandPollId;
48
+ // Salt.
49
+ signal input commandSalt;
50
+ // EdDSA signature of the command (R part).
51
+ signal input commandSignaturePoint[2];
52
+ // EdDSA signature of the command (S part).
53
+ signal input commandSignatureScalar;
54
+ // Packed command.
55
+ // nb. we are assuming that the packedCommand is always valid.
56
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
57
+
58
+ // New state leaf (if the command is valid).
59
+ signal output newStateLeafPublicKey[2];
60
+ // New ballot (if the command is valid).
61
+ signal output newBallotNonce;
62
+
63
+ // True when the command is valid; otherwise false.
64
+ signal output isValid;
65
+ // True if the state leaf index is valid
66
+ signal output isStateLeafIndexValid;
67
+ // True if the vote option index is valid
68
+ signal output isVoteOptionIndexValid;
69
+
70
+ // Check if the command / message is valid.
71
+ var (
72
+ computedIsValid,
73
+ computedIsStateLeafIndexValid,
74
+ computedIsVoteOptionIndexValid
75
+ ) = MessageValidatorFull()(
76
+ commandStateIndex,
77
+ totalSignups,
78
+ commandVoteOptionIndex,
79
+ voteOptions,
80
+ ballotNonce,
81
+ commandNonce,
82
+ packedCommand,
83
+ stateLeafPublicKey,
84
+ commandSignaturePoint,
85
+ commandSignatureScalar,
86
+ stateLeafVoiceCreditBalance,
87
+ ballotCurrentVotesForOption,
88
+ commandNewVoteWeight
89
+ );
90
+
91
+ // If the message is valid then we swap out the public key.
92
+ // This means using a Mux1() for publicKey[0] and another one
93
+ // for publicKey[1].
94
+ var computedNewstateLeafPublicKey0Mux = Mux1()(
95
+ [
96
+ stateLeafPublicKey[0],
97
+ commandPublicKey[0]
98
+ ],
99
+ computedIsValid
100
+ );
101
+
102
+ var computedNewstateLeafPublicKey1Mux = Mux1()(
103
+ [
104
+ stateLeafPublicKey[1],
105
+ commandPublicKey[1]
106
+ ],
107
+ computedIsValid
108
+ );
109
+
110
+ newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
111
+ newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
112
+
113
+ // If the message is valid, then we swap out the ballot nonce
114
+ // using a Mux1().
115
+ var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
116
+
117
+ newBallotNonce <== computedNewBallotNonceMux;
118
+
119
+ isValid <== computedIsValid;
120
+ isStateLeafIndexValid <== computedIsStateLeafIndexValid;
121
+ isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
122
+ }
@@ -26,14 +26,14 @@ template MessageValidatorNonQv() {
26
26
  // Ballot nonce.
27
27
  signal input originalNonce;
28
28
  // Command nonce.
29
- signal input nonce;
29
+ signal input commandNonce;
30
30
  // Packed command.
31
31
  signal input command[PACKED_COMMAND_LENGTH];
32
32
  // Public key of the state leaf (user).
33
33
  signal input publicKey[2];
34
- // ECDSA signature of the command (R part).
34
+ // EdDSA signature of the command (R part).
35
35
  signal input signaturePoint[2];
36
- // ECDSA signature of the command (S part).
36
+ // EdDSA signature of the command (S part).
37
37
  signal input signatureScalar;
38
38
  // State leaf current voice credit balance.
39
39
  signal input currentVoiceCreditBalance;
@@ -59,7 +59,7 @@ template MessageValidatorNonQv() {
59
59
  var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
60
60
 
61
61
  // Check (3) - The nonce must be correct.
62
- var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
62
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
63
63
 
64
64
  // Check (4) - The signature must be correct.
65
65
  var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
@@ -47,9 +47,9 @@ template StateLeafAndBallotTransformerNonQv() {
47
47
  signal input commandPollId;
48
48
  // Salt.
49
49
  signal input commandSalt;
50
- // ECDSA signature of the command (R part).
50
+ // EdDSA signature of the command (R part).
51
51
  signal input commandSignaturePoint[2];
52
- // ECDSA signature of the command (S part).
52
+ // EdDSA signature of the command (S part).
53
53
  signal input commandSignatureScalar;
54
54
  // Packed command.
55
55
  // nb. we are assuming that the packedCommand is always valid.
@@ -68,7 +68,7 @@ template StateLeafAndBallotTransformerNonQv() {
68
68
  signal output isVoteOptionIndexValid;
69
69
 
70
70
  // Check if the command / message is valid.
71
- var (computedMessageValidator, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
71
+ var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
72
72
  commandStateIndex,
73
73
  totalSignups,
74
74
  commandVoteOptionIndex,
@@ -87,19 +87,19 @@ template StateLeafAndBallotTransformerNonQv() {
87
87
  // If the message is valid then we swap out the public key.
88
88
  // This means using a Mux1() for publicKey[0] and another one
89
89
  // for publicKey[1].
90
- var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedMessageValidator);
91
- var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedMessageValidator);
90
+ var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
91
+ var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
92
92
 
93
93
  newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
94
94
  newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
95
95
 
96
96
  // If the message is valid, then we swap out the ballot nonce
97
97
  // using a Mux1().
98
- var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedMessageValidator);
98
+ var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
99
99
 
100
100
  newBallotNonce <== computedNewBallotNonceMux;
101
101
 
102
- isValid <== computedMessageValidator;
102
+ isValid <== computedIsValid;
103
103
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
104
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
105
  }
@@ -26,14 +26,14 @@ template MessageValidator() {
26
26
  // Ballot nonce.
27
27
  signal input originalNonce;
28
28
  // Command nonce.
29
- signal input nonce;
29
+ signal input commandNonce;
30
30
  // Packed command.
31
31
  signal input command[PACKED_COMMAND_LENGTH];
32
32
  // Public key of the state leaf (user).
33
33
  signal input publicKey[2];
34
- // ECDSA signature of the command (R part).
34
+ // EdDSA signature of the command (R part).
35
35
  signal input signaturePoint[2];
36
- // ECDSA signature of the command (S part).
36
+ // EdDSA signature of the command (S part).
37
37
  signal input signatureScalar;
38
38
  // State leaf current voice credit balance.
39
39
  signal input currentVoiceCreditBalance;
@@ -59,7 +59,7 @@ template MessageValidator() {
59
59
  var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
60
60
 
61
61
  // Check (3) - The nonce must be correct.
62
- var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
62
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
63
63
 
64
64
  // Check (4) - The signature must be correct.
65
65
  var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
@@ -47,9 +47,9 @@ template StateLeafAndBallotTransformer() {
47
47
  signal input commandPollId;
48
48
  // Salt.
49
49
  signal input commandSalt;
50
- // ECDSA signature of the command (R part).
50
+ // EdDSA signature of the command (R part).
51
51
  signal input commandSignaturePoint[2];
52
- // ECDSA signature of the command (S part).
52
+ // EdDSA signature of the command (S part).
53
53
  signal input commandSignatureScalar;
54
54
  // Packed command.
55
55
  // nb. we are assuming that the packedCommand is always valid.
@@ -68,7 +68,7 @@ template StateLeafAndBallotTransformer() {
68
68
  signal output isVoteOptionIndexValid;
69
69
 
70
70
  // Check if the command / message is valid.
71
- var (computedMessageValidator, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
71
+ var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
72
72
  commandStateIndex,
73
73
  totalSignups,
74
74
  commandVoteOptionIndex,
@@ -87,19 +87,19 @@ template StateLeafAndBallotTransformer() {
87
87
  // If the message is valid then we swap out the public key.
88
88
  // This means using a Mux1() for publicKey[0] and another one
89
89
  // for publicKey[1].
90
- var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedMessageValidator);
91
- var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedMessageValidator);
90
+ var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
91
+ var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
92
92
 
93
93
  newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
94
94
  newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
95
95
 
96
96
  // If the message is valid, then we swap out the ballot nonce
97
97
  // using a Mux1().
98
- var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedMessageValidator);
98
+ var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
99
99
 
100
100
  newBallotNonce <== computedNewBallotNonceMux;
101
101
 
102
- isValid <== computedMessageValidator;
102
+ isValid <== computedIsValid;
103
103
  isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
104
  isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
105
  }
@@ -13,13 +13,13 @@ template LeafExists(levels) {
13
13
  // The elements along the path needed for the inclusion proof.
14
14
  signal input path_elements[levels][1];
15
15
  // The indices indicating the path taken through the tree for the leaf.
16
- signal input path_index[levels];
16
+ signal input path_indices[levels];
17
17
  // The root of the Merkle tree, against which the inclusion is verified.
18
18
  signal input root;
19
19
 
20
20
  var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
21
21
  leaf,
22
- path_index,
22
+ path_indices,
23
23
  path_elements
24
24
  );
25
25
 
@@ -12,7 +12,7 @@ template MerkleTreeInclusionProof(n_levels) {
12
12
  // The leaf node from which the Merkle root is calculated.
13
13
  signal input leaf;
14
14
  // Indices indicating left or right child for each level of the tree.
15
- signal input path_index[n_levels];
15
+ signal input path_indices[n_levels];
16
16
  // Sibling node values required to compute the hash at each level.
17
17
  signal input path_elements[n_levels][1];
18
18
 
@@ -25,8 +25,8 @@ template MerkleTreeInclusionProof(n_levels) {
25
25
  levelHashes[0] <== leaf;
26
26
 
27
27
  for (var i = 0; i < n_levels; i++) {
28
- // Validate path_index to be either 0 or 1, ensuring no other values.
29
- path_index[i] * (1 - path_index[i]) === 0;
28
+ // Validate path_indices to be either 0 or 1, ensuring no other values.
29
+ path_indices[i] * (1 - path_indices[i]) === 0;
30
30
 
31
31
  // Configure the multiplexer based on the path index for the current level.
32
32
  var multiplexer[2][2] = [
@@ -36,7 +36,7 @@ template MerkleTreeInclusionProof(n_levels) {
36
36
 
37
37
  var multiplexerResult[2] = MultiMux1(2)(
38
38
  multiplexer,
39
- path_index[i]
39
+ path_indices[i]
40
40
  );
41
41
 
42
42
  var computedLevelHash = PoseidonHasher(2)([multiplexerResult[0], multiplexerResult[1]]);