@maci-protocol/circuits 0.0.0-ci.044d30d

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 (43) hide show
  1. package/CHANGELOG.md +673 -0
  2. package/LICENSE +22 -0
  3. package/README.md +20 -0
  4. package/build/ts/compile.d.ts +10 -0
  5. package/build/ts/compile.d.ts.map +1 -0
  6. package/build/ts/compile.js +123 -0
  7. package/build/ts/compile.js.map +1 -0
  8. package/build/ts/genZkeys.d.ts +9 -0
  9. package/build/ts/genZkeys.d.ts.map +1 -0
  10. package/build/ts/genZkeys.js +67 -0
  11. package/build/ts/genZkeys.js.map +1 -0
  12. package/build/ts/info.d.ts +2 -0
  13. package/build/ts/info.d.ts.map +1 -0
  14. package/build/ts/info.js +72 -0
  15. package/build/ts/info.js.map +1 -0
  16. package/build/ts/types.d.ts +104 -0
  17. package/build/ts/types.d.ts.map +1 -0
  18. package/build/ts/types.js +3 -0
  19. package/build/ts/types.js.map +1 -0
  20. package/build/tsconfig.build.tsbuildinfo +1 -0
  21. package/circom/circuits.json +58 -0
  22. package/circom/coordinator/non-qv/processMessages.circom +449 -0
  23. package/circom/coordinator/non-qv/tallyVotes.circom +235 -0
  24. package/circom/coordinator/qv/processMessages.circom +451 -0
  25. package/circom/coordinator/qv/tallyVotes.circom +279 -0
  26. package/circom/utils/calculateTotal.circom +22 -0
  27. package/circom/utils/hashers.circom +78 -0
  28. package/circom/utils/messageToCommand.circom +78 -0
  29. package/circom/utils/non-qv/messageValidator.circom +89 -0
  30. package/circom/utils/non-qv/stateLeafAndBallotTransformer.circom +105 -0
  31. package/circom/utils/privToPubKey.circom +36 -0
  32. package/circom/utils/qv/messageValidator.circom +95 -0
  33. package/circom/utils/qv/stateLeafAndBallotTransformer.circom +105 -0
  34. package/circom/utils/trees/BinaryMerkleRoot.circom +54 -0
  35. package/circom/utils/trees/CheckRoot.circom +45 -0
  36. package/circom/utils/trees/LeafExists.circom +27 -0
  37. package/circom/utils/trees/MerkleGeneratePathIndices.circom +40 -0
  38. package/circom/utils/trees/MerkleTreeInclusionProof.circom +49 -0
  39. package/circom/utils/trees/incrementalQuinaryTree.circom +287 -0
  40. package/circom/utils/verifySignature.circom +117 -0
  41. package/circom/voter/poll.circom +92 -0
  42. package/circomkit.json +18 -0
  43. package/package.json +71 -0
@@ -0,0 +1,279 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./comparators.circom";
5
+ // zk-kit import
6
+ include "./unpack-element.circom";
7
+ // local imports
8
+ include "../../utils/trees/CheckRoot.circom";
9
+ include "../../utils/trees/MerkleGeneratePathIndices.circom";
10
+ include "../../utils/trees/LeafExists.circom";
11
+ include "../../utils/trees/incrementalQuinaryTree.circom";
12
+ include "../../utils/calculateTotal.circom";
13
+ include "../../utils/hashers.circom";
14
+
15
+ /**
16
+ * Processes batches of votes and verifies their validity in a Merkle tree structure.
17
+ * This template supports Quadratic Voting (QV).
18
+ */
19
+ template TallyVotes(
20
+ stateTreeDepth,
21
+ intStateTreeDepth,
22
+ voteOptionTreeDepth
23
+ ) {
24
+ // Ensure there's at least one level in the vote option tree.
25
+ assert(voteOptionTreeDepth > 0);
26
+ // Ensure the intermediate state tree has at least one level.
27
+ assert(intStateTreeDepth > 0);
28
+ // The intermediate state tree must be smaller than the full state tree.
29
+ assert(intStateTreeDepth < stateTreeDepth);
30
+
31
+ // Number of children per node in the tree, defining the tree's branching factor.
32
+ var TREE_ARITY = 5;
33
+ var BALLOT_TREE_ARITY = 2;
34
+
35
+ // The number of ballots processed at once, determined by the depth of the intermediate state tree.
36
+ var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
37
+ // Number of voting options available, determined by the depth of the vote option tree.
38
+ var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
39
+
40
+ // Number of elements in each ballot.
41
+ var BALLOT_LENGTH = 2;
42
+ // Index for the nonce in the ballot array.
43
+ var BALLOT_NONCE_IDX = 0;
44
+ // Index for the voting option root in the ballot array.
45
+ var BALLOT_VO_ROOT_IDX = 1;
46
+ // Difference in tree depths, used in path calculations.
47
+ var k = stateTreeDepth - intStateTreeDepth;
48
+
49
+ // Root of the state Merkle tree, representing the overall state before voting.
50
+ signal input stateRoot;
51
+ // Root of the ballot Merkle tree, representing the submitted ballots.
52
+ signal input ballotRoot;
53
+ // Salt used in commitment to secure the ballot data.
54
+ signal input sbSalt;
55
+ // Commitment to the state and ballots.
56
+ signal input sbCommitment;
57
+ // Commitment to the current tally before this batch.
58
+ signal input currentTallyCommitment;
59
+ // Commitment to the new tally after processing this batch.
60
+ signal input newTallyCommitment;
61
+ // Start index of given batch
62
+ signal input index;
63
+ // Number of users that signup
64
+ signal input numSignUps;
65
+ // Ballots and their corresponding path elements for verification in the tree.
66
+ signal input ballots[batchSize][BALLOT_LENGTH];
67
+ signal input ballotPathElements[k][BALLOT_TREE_ARITY - 1];
68
+ signal input votes[batchSize][numVoteOptions];
69
+ // Current results for each vote option.
70
+ signal input currentResults[numVoteOptions];
71
+ // Salt for the root of the current results.
72
+ signal input currentResultsRootSalt;
73
+ // Total voice credits spent so far.
74
+ signal input currentSpentVoiceCreditSubtotal;
75
+ // Salt for the total spent voice credits.
76
+ signal input currentSpentVoiceCreditSubtotalSalt;
77
+ // Spent voice credits per vote option.
78
+ signal input currentPerVOSpentVoiceCredits[numVoteOptions];
79
+ // Salt for the root of spent credits per option.
80
+ signal input currentPerVOSpentVoiceCreditsRootSalt;
81
+ // Salt for the root of the new results.
82
+ signal input newResultsRootSalt;
83
+ // Salt for the new spent credits per vote option root.
84
+ signal input newPerVOSpentVoiceCreditsRootSalt;
85
+ // Salt for the new total spent voice credits root.
86
+ signal input newSpentVoiceCreditSubtotalSalt;
87
+
88
+ // Verify sbCommitment.
89
+ var computedSbCommitment = PoseidonHasher(3)([stateRoot, ballotRoot, sbSalt]);
90
+ computedSbCommitment === sbCommitment;
91
+
92
+ // Validates that the index is within the valid range of sign-ups.
93
+ var numSignUpsValid = LessEqThan(50)([index, numSignUps]);
94
+ numSignUpsValid === 1;
95
+
96
+ // Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
97
+ var computedBallotHashers[batchSize];
98
+
99
+ for (var i = 0; i < batchSize; i++) {
100
+ computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_IDX], ballots[i][BALLOT_VO_ROOT_IDX]]);
101
+ }
102
+
103
+ var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
104
+ var computedBallotPathIndices[k] = MerkleGeneratePathIndices(k)(index / batchSize);
105
+
106
+ // Verifies each ballot's existence within the ballot tree.
107
+ LeafExists(k)(
108
+ computedBallotSubroot,
109
+ ballotPathElements,
110
+ computedBallotPathIndices,
111
+ ballotRoot
112
+ );
113
+
114
+ // Processes vote options, verifying each against its declared root.
115
+ var computedVoteTree[batchSize];
116
+ for (var i = 0; i < batchSize; i++) {
117
+ computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
118
+ computedVoteTree[i] === ballots[i][BALLOT_VO_ROOT_IDX];
119
+ }
120
+
121
+ // Calculates new results and spent voice credits based on the current and incoming votes.
122
+ var computedIsFirstBatch = IsZero()(index);
123
+ var computedIsZero = IsZero()(computedIsFirstBatch);
124
+
125
+ // Tally the new results.
126
+ var computedCalculateTotalResult[numVoteOptions];
127
+ for (var i = 0; i < numVoteOptions; i++) {
128
+ var numsRC[batchSize + 1];
129
+ numsRC[batchSize] = currentResults[i] * computedIsZero;
130
+ for (var j = 0; j < batchSize; j++) {
131
+ numsRC[j] = votes[j][i];
132
+ }
133
+
134
+ computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(numsRC);
135
+ }
136
+
137
+ // Tally the new spent voice credit total.
138
+ var numsSVC[batchSize * numVoteOptions + 1];
139
+ numsSVC[batchSize * numVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
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];
143
+ }
144
+ }
145
+
146
+ var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * numVoteOptions + 1)(numsSVC);
147
+
148
+ // Tally the spent voice credits per vote option.
149
+ var computedNewPerVOSpentVoiceCredits[numVoteOptions];
150
+
151
+ for (var i = 0; i < numVoteOptions; i++) {
152
+ var computedNumsSVC[batchSize + 1];
153
+ computedNumsSVC[batchSize] = currentPerVOSpentVoiceCredits[i] * computedIsZero;
154
+ for (var j = 0; j < batchSize; j++) {
155
+ computedNumsSVC[j] = votes[j][i] * votes[j][i];
156
+ }
157
+
158
+ computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedNumsSVC);
159
+ }
160
+
161
+ // Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
162
+ ResultCommitmentVerifier(voteOptionTreeDepth)(
163
+ computedIsFirstBatch,
164
+ currentTallyCommitment,
165
+ newTallyCommitment,
166
+ currentResults,
167
+ currentResultsRootSalt,
168
+ computedCalculateTotalResult,
169
+ newResultsRootSalt,
170
+ currentSpentVoiceCreditSubtotal,
171
+ currentSpentVoiceCreditSubtotalSalt,
172
+ computedNewSpentVoiceCreditSubtotal,
173
+ newSpentVoiceCreditSubtotalSalt,
174
+ currentPerVOSpentVoiceCredits,
175
+ currentPerVOSpentVoiceCreditsRootSalt,
176
+ computedNewPerVOSpentVoiceCredits,
177
+ newPerVOSpentVoiceCreditsRootSalt
178
+ );
179
+ }
180
+
181
+ /**
182
+ * Performs verifications and computations related to current voting results.
183
+ * Also, computes and outputs a commitment to the new results.
184
+ * This template supports the Quadratic Voting (QV).
185
+ */
186
+ template ResultCommitmentVerifier(voteOptionTreeDepth) {
187
+ // Number of children per node in the tree, defining the tree's branching factor.
188
+ var TREE_ARITY = 5;
189
+ // Number of voting options available, determined by the depth of the vote option tree.
190
+ var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
191
+
192
+ // Equal to 1 if this is the first batch, otherwise 0.
193
+ signal input isFirstBatch;
194
+ // Commitment to the current tally before this batch.
195
+ signal input currentTallyCommitment;
196
+ // Commitment to the new tally after processing this batch.
197
+ signal input newTallyCommitment;
198
+
199
+ // Current results for each vote option.
200
+ signal input currentResults[numVoteOptions];
201
+ // Salt for the root of the current results.
202
+ signal input currentResultsRootSalt;
203
+
204
+ // New results for each vote option.
205
+ signal input newResults[numVoteOptions];
206
+ // Salt for the root of the new results.
207
+ signal input newResultsRootSalt;
208
+
209
+ // Total voice credits spent so far.
210
+ signal input currentSpentVoiceCreditSubtotal;
211
+ // Salt for the total spent voice credits.
212
+ signal input currentSpentVoiceCreditSubtotalSalt;
213
+
214
+ // Total new voice credits spent.
215
+ signal input newSpentVoiceCreditSubtotal;
216
+ // Salt for the new total spent voice credits.
217
+ signal input newSpentVoiceCreditSubtotalSalt;
218
+
219
+ // Spent voice credits per vote option.
220
+ signal input currentPerVOSpentVoiceCredits[numVoteOptions];
221
+ // Salt for the root of spent credits per option.
222
+ signal input currentPerVOSpentVoiceCreditsRootSalt;
223
+
224
+ // New spent voice credits per vote option.
225
+ signal input newPerVOSpentVoiceCredits[numVoteOptions];
226
+ // Salt for the root of new spent credits per option.
227
+ signal input newPerVOSpentVoiceCreditsRootSalt;
228
+
229
+ // Compute the commitment to the current results.
230
+ var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
231
+
232
+ // Verify currentResultsCommitmentHash.
233
+ var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
234
+
235
+ // Compute the commitment to the current spent voice credits.
236
+ var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
237
+
238
+ // Compute the root of the spent voice credits per vote option.
239
+ var computedCurrentPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentPerVOSpentVoiceCredits);
240
+ var computedCurrentPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVOSpentVoiceCreditsRoot, currentPerVOSpentVoiceCreditsRootSalt]);
241
+
242
+ // Commit to the current tally.
243
+ var computedCurrentTallyCommitment = PoseidonHasher(3)([
244
+ computedCurrentResultsCommitment,
245
+ computedCurrentSpentVoiceCreditsCommitment,
246
+ computedCurrentPerVOSpentVoiceCreditsCommitment
247
+ ]);
248
+
249
+ // Check if the current tally commitment is correct only if this is not the first batch.
250
+ // computedIsZero.out is 1 if this is not the first batch.
251
+ // computedIsZero.out is 0 if this is the first batch.
252
+ var computedIsZero = IsZero()(isFirstBatch);
253
+
254
+ // 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;
259
+
260
+ // Compute the root of the new results.
261
+ var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
262
+ var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
263
+
264
+ // Compute the commitment to the new spent voice credits value.
265
+ var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
266
+
267
+ // Compute the root of the spent voice credits per vote option.
268
+ var computedNewPerVOSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(newPerVOSpentVoiceCredits);
269
+ var computedNewPerVOSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVOSpentVoiceCreditsRoot, newPerVOSpentVoiceCreditsRootSalt]);
270
+
271
+ // Commit to the new tally.
272
+ var computedNewTallyCommitment = PoseidonHasher(3)([
273
+ computedNewResultsCommitment,
274
+ computedNewSpentVoiceCreditsCommitment,
275
+ computedNewPerVOSpentVoiceCreditsCommitment
276
+ ]);
277
+
278
+ computedNewTallyCommitment === newTallyCommitment;
279
+ }
@@ -0,0 +1,22 @@
1
+ pragma circom 2.0.0;
2
+
3
+ /**
4
+ * Computes the cumulative sum of an array of n input signals.
5
+ * It iterates through each input, aggregating the sum up to that point,
6
+ * and outputs the total sum of all inputs. This template is useful for
7
+ * operations requiring the total sum of multiple signals, ensuring the
8
+ * final output reflects the cumulative total of the inputs provided.
9
+ */
10
+ template CalculateTotal(n) {
11
+ signal input nums[n];
12
+ signal output sum;
13
+
14
+ signal sums[n];
15
+ sums[0] <== nums[0];
16
+
17
+ for (var i = 1; i < n; i++) {
18
+ sums[i] <== sums[i - 1] + nums[i];
19
+ }
20
+
21
+ sum <== sums[n - 1];
22
+ }
@@ -0,0 +1,78 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // zk-kit imports
4
+ include "./poseidon-cipher.circom";
5
+
6
+ /**
7
+ * Computes the Poseidon hash for an array of n inputs, including a default initial state
8
+ * of zero not counted in n. First, extends the inputs by prepending a zero, creating an array [0, inputs].
9
+ * Then, the Poseidon hash of the extended inputs is calculated, with the first element of the
10
+ * result assigned as the output.
11
+ */
12
+ template PoseidonHasher(n) {
13
+ signal input inputs[n];
14
+ signal output out;
15
+
16
+ // [0, inputs].
17
+ var computedExtendedInputs[n + 1];
18
+ computedExtendedInputs[0] = 0;
19
+
20
+ for (var i = 0; i < n; i++) {
21
+ computedExtendedInputs[i + 1] = inputs[i];
22
+ }
23
+
24
+ // Compute the Poseidon hash of the extended inputs.
25
+ var computedPoseidonPerm[n + 1];
26
+ computedPoseidonPerm = PoseidonPerm(n + 1)(computedExtendedInputs);
27
+
28
+ out <== computedPoseidonPerm[0];
29
+ }
30
+
31
+ /**
32
+ * Hashes a MACI message and the public key used for message encryption.
33
+ * This template processes 10 message inputs and a 2-element public key
34
+ * combining them using the Poseidon hash function. The hashing process involves two stages:
35
+ * 1. hashing message parts in groups of five and,
36
+ * 2. hashing the grouped results alongside the first message input and
37
+ * the encryption public key to produce a final hash output.
38
+ */
39
+ template MessageHasher() {
40
+ // The MACI message is composed of 10 parts.
41
+ signal input in[10];
42
+ // the public key used to encrypt the message.
43
+ signal input encPubKey[2];
44
+ // we output an hash.
45
+ signal output hash;
46
+
47
+ // Hasher4(
48
+ // Hasher5_1(in[1], in[2], in[3], in[4], in[5]),
49
+ // Hasher5_2(in[6], in[7], in[8], in[9], in[10])
50
+ // in[11],
51
+ // in[12]
52
+ // )
53
+
54
+ var computedHasher5_1;
55
+ computedHasher5_1 = PoseidonHasher(5)([
56
+ in[0],
57
+ in[1],
58
+ in[2],
59
+ in[3],
60
+ in[4]
61
+ ]);
62
+
63
+ var computedHasher5_2;
64
+ computedHasher5_2 = PoseidonHasher(5)([
65
+ in[5],
66
+ in[6],
67
+ in[7],
68
+ in[8],
69
+ in[9]
70
+ ]);
71
+
72
+ hash <== PoseidonHasher(4)([
73
+ computedHasher5_1,
74
+ computedHasher5_2,
75
+ encPubKey[0],
76
+ encPubKey[1]
77
+ ]);
78
+ }
@@ -0,0 +1,78 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib import
4
+ include "./bitify.circom";
5
+ // zk-kit imports
6
+ include "./ecdh.circom";
7
+ include "./unpack-element.circom";
8
+ include "./poseidon-cipher.circom";
9
+ // local imports
10
+ include "./hashers.circom";
11
+
12
+ /**
13
+ * Converts a MACI message to a command by decrypting it.
14
+ * Processes encrypted MACI messages into structured MACI commands
15
+ * by decrypting using a shared key derived from ECDH. After decryption,
16
+ * unpacks and assigns decrypted values to specific command components.
17
+ */
18
+ template MessageToCommand() {
19
+ var MSG_LENGTH = 7;
20
+ var PACKED_CMD_LENGTH = 4;
21
+ var UNPACKED_CMD_LENGTH = 8;
22
+ var UNPACK_ELEM_LENGTH = 5;
23
+ var DECRYPTED_LENGTH = 9;
24
+ var MESSAGE_PARTS = 10;
25
+
26
+ // The message is an array of 10 parts.
27
+ signal input message[MESSAGE_PARTS];
28
+ signal input encPrivKey;
29
+ signal input encPubKey[2];
30
+
31
+ // Command parts.
32
+ signal output stateIndex;
33
+ signal output newPubKey[2];
34
+ signal output voteOptionIndex;
35
+ signal output newVoteWeight;
36
+ signal output nonce;
37
+ signal output pollId;
38
+ signal output salt;
39
+ signal output sigR8[2];
40
+ signal output sigS;
41
+ // Packed command.
42
+ signal output packedCommandOut[PACKED_CMD_LENGTH];
43
+
44
+ // Generate the shared key for decrypting the message.
45
+ var computedEcdh[2] = Ecdh()(encPrivKey, encPubKey);
46
+
47
+ // Decrypt the message using Poseidon decryption.
48
+ var computedDecryptor[DECRYPTED_LENGTH] = PoseidonDecryptWithoutCheck(MSG_LENGTH)(
49
+ message,
50
+ 0,
51
+ computedEcdh
52
+ );
53
+
54
+ // Save the decrypted message into a packed command signal.
55
+ signal packedCommand[PACKED_CMD_LENGTH];
56
+ for (var i = 0; i < PACKED_CMD_LENGTH; i++) {
57
+ packedCommand[i] <== computedDecryptor[i];
58
+ }
59
+
60
+ var computedUnpackElement[UNPACK_ELEM_LENGTH] = UnpackElement(UNPACK_ELEM_LENGTH)(packedCommand[0]);
61
+
62
+ // Everything below were packed into the first element.
63
+ stateIndex <== computedUnpackElement[4];
64
+ voteOptionIndex <== computedUnpackElement[3];
65
+ newVoteWeight <== computedUnpackElement[2];
66
+ nonce <== computedUnpackElement[1];
67
+ pollId <== computedUnpackElement[0];
68
+
69
+ newPubKey[0] <== packedCommand[1];
70
+ newPubKey[1] <== packedCommand[2];
71
+ salt <== packedCommand[3];
72
+
73
+ sigR8[0] <== computedDecryptor[4];
74
+ sigR8[1] <== computedDecryptor[5];
75
+ sigS <== computedDecryptor[6];
76
+
77
+ packedCommandOut <== packedCommand;
78
+ }
@@ -0,0 +1,89 @@
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 does not support Quadratic Voting (QV).
11
+ */
12
+ template MessageValidatorNonQv() {
13
+ // Length of the packed command.
14
+ var PACKED_CMD_LENGTH = 4;
15
+
16
+ // State index of the user.
17
+ signal input stateTreeIndex;
18
+ // Number of user sign-ups in the state tree.
19
+ signal input numSignUps;
20
+ // Vote option index.
21
+ signal input voteOptionIndex;
22
+ // Number of valid vote options for the poll.
23
+ signal input voteOptions;
24
+ // Ballot nonce.
25
+ signal input originalNonce;
26
+ // Command nonce.
27
+ signal input nonce;
28
+ // Packed command.
29
+ signal input cmd[PACKED_CMD_LENGTH];
30
+ // Public key of the state leaf (user).
31
+ signal input pubKey[2];
32
+ // ECDSA signature of the command (R part).
33
+ signal input sigR8[2];
34
+ // ECDSA signature of the command (S part).
35
+ signal input sigS;
36
+ // State leaf current voice credit balance.
37
+ signal input currentVoiceCreditBalance;
38
+ // Current number of votes for specific option.
39
+ signal input currentVotesForOption;
40
+ // Vote weight.
41
+ signal input voteWeight;
42
+
43
+ // True when the command is valid; otherwise false.
44
+ signal output isValid;
45
+ // True if the state leaf index is valid
46
+ signal output isStateLeafIndexValid;
47
+ // True if the vote option index is valid
48
+ signal output isVoteOptionIndexValid;
49
+
50
+ // Check (1) - The state leaf index must be valid.
51
+ // The check ensure that the stateTreeIndex < numSignUps as first validation.
52
+ // Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
53
+ // while 1 is for the first actual user.
54
+ var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, numSignUps]);
55
+
56
+ // Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
57
+ var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
58
+
59
+ // Check (3) - The nonce must be correct.
60
+ var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
61
+
62
+ // Check (4) - The signature must be correct.
63
+ var computedIsSignatureValid = VerifySignature()(pubKey, sigR8, sigS, cmd);
64
+
65
+ // Check (5) - There must be sufficient voice credits.
66
+ // The check ensure that currentVoiceCreditBalance + (currentVotesForOption) >= (voteWeight).
67
+ var computedAreVoiceCreditsSufficient = SafeGreaterEqThan(252)(
68
+ [
69
+ currentVotesForOption + currentVoiceCreditBalance,
70
+ voteWeight
71
+ ]
72
+ );
73
+
74
+ // When all five checks are correct, then isValid = 1.
75
+ var computedIsUpdateValid = IsEqual()(
76
+ [
77
+ 5,
78
+ computedIsSignatureValid +
79
+ computedAreVoiceCreditsSufficient +
80
+ computedIsNonceValid +
81
+ computedIsStateLeafIndexValid +
82
+ computedIsVoteOptionIndexValid
83
+ ]
84
+ );
85
+
86
+ isValid <== computedIsUpdateValid;
87
+ isStateLeafIndexValid <== computedIsStateLeafIndexValid;
88
+ isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
89
+ }
@@ -0,0 +1,105 @@
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 does not support Quadratic Voting (QV).
13
+ */
14
+ template StateLeafAndBallotTransformerNonQv() {
15
+ // Length of the packed command.
16
+ var PACKED_CMD_LENGTH = 4;
17
+
18
+ // Number of user sign-ups in the state tree.
19
+ signal input numSignUps;
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 slPubKey[2];
26
+ // Current voice credit balance.
27
+ signal input slVoiceCreditBalance;
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 cmdStateIndex;
38
+ // Public key of the user.
39
+ signal input cmdNewPubKey[2];
40
+ // Vote option index.
41
+ signal input cmdVoteOptionIndex;
42
+ // Vote weight.
43
+ signal input cmdNewVoteWeight;
44
+ // Nonce.
45
+ signal input cmdNonce;
46
+ // Poll identifier.
47
+ signal input cmdPollId;
48
+ // Salt.
49
+ signal input cmdSalt;
50
+ // ECDSA signature of the command (R part).
51
+ signal input cmdSigR8[2];
52
+ // ECDSA signature of the command (S part).
53
+ signal input cmdSigS;
54
+ // Packed command.
55
+ // nb. we are assuming that the packedCommand is always valid.
56
+ signal input packedCommand[PACKED_CMD_LENGTH];
57
+
58
+ // New state leaf (if the command is valid).
59
+ signal output newSlPubKey[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 (computedMessageValidator, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
72
+ cmdStateIndex,
73
+ numSignUps,
74
+ cmdVoteOptionIndex,
75
+ voteOptions,
76
+ ballotNonce,
77
+ cmdNonce,
78
+ packedCommand,
79
+ slPubKey,
80
+ cmdSigR8,
81
+ cmdSigS,
82
+ slVoiceCreditBalance,
83
+ ballotCurrentVotesForOption,
84
+ cmdNewVoteWeight
85
+ );
86
+
87
+ // If the message is valid then we swap out the public key.
88
+ // This means using a Mux1() for pubKey[0] and another one
89
+ // for pubKey[1].
90
+ var computedNewSlPubKey0Mux = Mux1()([slPubKey[0], cmdNewPubKey[0]], computedMessageValidator);
91
+ var computedNewSlPubKey1Mux = Mux1()([slPubKey[1], cmdNewPubKey[1]], computedMessageValidator);
92
+
93
+ newSlPubKey[0] <== computedNewSlPubKey0Mux;
94
+ newSlPubKey[1] <== computedNewSlPubKey1Mux;
95
+
96
+ // If the message is valid, then we swap out the ballot nonce
97
+ // using a Mux1().
98
+ var computedNewBallotNonceMux = Mux1()([ballotNonce, cmdNonce], computedMessageValidator);
99
+
100
+ newBallotNonce <== computedNewBallotNonceMux;
101
+
102
+ isValid <== computedMessageValidator;
103
+ isStateLeafIndexValid <== computedIsStateLeafIndexValid;
104
+ isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
105
+ }
@@ -0,0 +1,36 @@
1
+ pragma circom 2.0.0;
2
+
3
+ // circomlib imports
4
+ include "./bitify.circom";
5
+ include "./comparators.circom";
6
+ include "./escalarmulfix.circom";
7
+
8
+ /**
9
+ * Converts a private key to a public key on the BabyJubJub curve.
10
+ * The input private key needs to be hashed and then pruned before.
11
+ */
12
+ template PrivToPubKey() {
13
+ // The base point of the BabyJubJub curve.
14
+ var BASE8[2] = [
15
+ 5299619240641551281634865583518297030282874472190772894086521144482721001553,
16
+ 16950150798460657717958625567821834550301663161624707787222815936182638968203
17
+ ];
18
+
19
+ // Prime subgroup order 'l'.
20
+ var l = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
21
+
22
+ signal input privKey;
23
+ signal output pubKey[2];
24
+
25
+ // Check if private key is in the prime subgroup order 'l'
26
+ var isLessThan = LessThan(251)([privKey, l]);
27
+ isLessThan === 1;
28
+
29
+ // Convert the private key to bits.
30
+ var computedPrivBits[253] = Num2Bits(253)(privKey);
31
+
32
+ // Perform scalar multiplication with the basepoint.
33
+ var computedEscalarMulFix[2] = EscalarMulFix(253, BASE8)(computedPrivBits);
34
+
35
+ pubKey <== computedEscalarMulFix;
36
+ }