@maci-protocol/circuits 0.0.0-ci.a577366 → 0.0.0-ci.a73cfa9

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 (57) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +2 -2
  3. package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
  4. package/build/ts/generateZkeys.d.ts.map +1 -0
  5. package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
  6. package/build/ts/generateZkeys.js.map +1 -0
  7. package/build/ts/types.d.ts +15 -15
  8. package/build/ts/types.d.ts.map +1 -1
  9. package/build/tsconfig.build.tsbuildinfo +1 -1
  10. package/circom/circuits.json +35 -19
  11. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  12. package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
  13. package/circom/coordinator/non-qv/MessageProcessor.circom +252 -0
  14. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +199 -0
  15. package/circom/coordinator/non-qv/VoteTally.circom +161 -0
  16. package/circom/coordinator/qv/MessageProcessor.circom +250 -0
  17. package/circom/coordinator/qv/SingleMessageProcessor.circom +207 -0
  18. package/circom/coordinator/qv/VoteTally.circom +179 -0
  19. package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
  20. package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +8 -6
  21. package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
  22. package/circom/utils/MessageHasher.circom +57 -0
  23. package/circom/utils/MessageToCommand.circom +107 -0
  24. package/circom/utils/PoseidonHasher.circom +29 -0
  25. package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +12 -10
  26. package/circom/utils/VerifySignature.circom +39 -0
  27. package/circom/utils/full/MessageValidator.circom +91 -0
  28. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  29. package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +17 -15
  30. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  31. package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  32. package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +17 -15
  33. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  34. package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +36 -36
  35. package/circom/utils/trees/BinaryMerkleRoot.circom +14 -3
  36. package/circom/utils/trees/CheckRoot.circom +18 -14
  37. package/circom/utils/trees/LeafExists.circom +3 -3
  38. package/circom/utils/trees/MerkleTreeInclusionProof.circom +10 -9
  39. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  40. package/circom/utils/trees/{MerkleGeneratePathIndices.circom → QuinaryGeneratePathIndices.circom} +19 -15
  41. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  42. package/circom/utils/trees/QuinarySelector.circom +42 -0
  43. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  44. package/circom/utils/trees/Splicer.circom +76 -0
  45. package/circom/voter/PollJoined.circom +43 -0
  46. package/circom/voter/PollJoining.circom +54 -0
  47. package/package.json +22 -19
  48. package/build/ts/genZkeys.d.ts.map +0 -1
  49. package/build/ts/genZkeys.js.map +0 -1
  50. package/circom/coordinator/non-qv/processMessages.circom +0 -449
  51. package/circom/coordinator/non-qv/tallyVotes.circom +0 -235
  52. package/circom/coordinator/qv/processMessages.circom +0 -451
  53. package/circom/coordinator/qv/tallyVotes.circom +0 -279
  54. package/circom/utils/hashers.circom +0 -78
  55. package/circom/utils/messageToCommand.circom +0 -78
  56. package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
  57. package/circom/voter/poll.circom +0 -92
@@ -1,279 +0,0 @@
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
- }
@@ -1,78 +0,0 @@
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
- }
@@ -1,78 +0,0 @@
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
- }