@maci-protocol/circuits 0.0.0-ci.77d0530 → 0.0.0-ci.799d56f
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.
- package/README.md +2 -2
- package/build/ts/types.d.ts +4 -4
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +28 -12
- package/circom/coordinator/full/MessageProcessor.circom +245 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
- package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +17 -219
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +197 -0
- package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +18 -95
- package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +18 -227
- package/circom/coordinator/qv/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/qv/{tallyVotes.circom → VoteTally.circom} +14 -114
- package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/EdDSAPoseidonVerifier.circom +8 -3
- package/circom/utils/IsOnCurve.circom +40 -0
- package/circom/utils/full/MessageValidator.circom +2 -4
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +5 -0
- package/circom/utils/non-qv/MessageValidator.circom +2 -2
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +5 -0
- package/circom/utils/qv/MessageValidator.circom +2 -2
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +5 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/{MerklePathIndicesGenerator.circom → QuinaryGeneratePathIndices.circom} +18 -18
- package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
- package/circom/utils/trees/QuinarySelector.circom +42 -0
- package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
- package/circom/utils/trees/Splicer.circom +76 -0
- package/circom/voter/PollJoined.circom +3 -3
- package/circom/voter/PollJoining.circom +3 -3
- package/package.json +17 -16
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -9,19 +9,13 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
9
9
|
include "../../utils/MessageHasher.circom";
|
|
10
10
|
include "../../utils/MessageToCommand.circom";
|
|
11
11
|
include "../../utils/PrivateToPublicKey.circom";
|
|
12
|
-
include "
|
|
13
|
-
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
14
|
-
include "../../utils/trees/LeafExists.circom";
|
|
15
|
-
include "../../utils/trees/CheckRoot.circom";
|
|
16
|
-
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
17
|
-
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
18
|
-
include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
12
|
+
include "./SingleMessageProcessor.circom";
|
|
19
13
|
|
|
20
14
|
/**
|
|
21
15
|
* Proves the correctness of processing a batch of MACI messages.
|
|
22
16
|
* This template does not support Quadratic Voting (QV).
|
|
23
17
|
*/
|
|
24
|
-
template
|
|
18
|
+
template MessageProcessorNonQv(
|
|
25
19
|
stateTreeDepth,
|
|
26
20
|
batchSize,
|
|
27
21
|
voteOptionTreeDepth
|
|
@@ -39,14 +33,6 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
39
33
|
var PACKED_COMMAND_LENGTH = 4;
|
|
40
34
|
var STATE_LEAF_LENGTH = 3;
|
|
41
35
|
var BALLOT_LENGTH = 2;
|
|
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
|
-
// Number of options for this poll.
|
|
49
|
-
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
50
36
|
|
|
51
37
|
// Number of users that have completed the sign up.
|
|
52
38
|
signal input totalSignups;
|
|
@@ -63,7 +49,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
63
49
|
// The current state root (before the processing).
|
|
64
50
|
signal input currentStateRoot;
|
|
65
51
|
// The actual tree depth (might be <= stateTreeDepth).
|
|
66
|
-
// @note it is a public input to ensure fair processing from
|
|
52
|
+
// @note it is a public input to ensure fair processing from
|
|
67
53
|
// the coordinator (no censoring)
|
|
68
54
|
signal input actualStateTreeDepth;
|
|
69
55
|
// The coordinator public key hash
|
|
@@ -103,7 +89,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
103
89
|
|
|
104
90
|
// The index of the first message in the batch, inclusive.
|
|
105
91
|
signal input index;
|
|
106
|
-
|
|
92
|
+
|
|
107
93
|
// The index of the last message in the batch to process, exclusive.
|
|
108
94
|
// This value may be less than index + batchSize if this batch is
|
|
109
95
|
// the last batch and the total number of messages is not a multiple of the batch size.
|
|
@@ -118,7 +104,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
118
104
|
var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
|
|
119
105
|
computedCurrentSbCommitment === currentSbCommitment;
|
|
120
106
|
|
|
121
|
-
// -----------------------------------------------------------------------
|
|
107
|
+
// -----------------------------------------------------------------------
|
|
122
108
|
// 0. Ensure that the maximum vote options signal is valid and if
|
|
123
109
|
// the maximum users signal is valid
|
|
124
110
|
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
@@ -168,7 +154,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
168
154
|
|
|
169
155
|
// Decrypt each Message into a Command.
|
|
170
156
|
// The command i-th is composed by the following fields.
|
|
171
|
-
// e.g., command 0 is made of commandsStateIndex[0],
|
|
157
|
+
// e.g., command 0 is made of commandsStateIndex[0],
|
|
172
158
|
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
173
159
|
var computedCommandsStateIndex[batchSize];
|
|
174
160
|
var computedCommandsNewPublicKey[batchSize][2];
|
|
@@ -208,34 +194,34 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
208
194
|
// Start from batchSize and decrement for process in reverse order.
|
|
209
195
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
210
196
|
// Process as vote type message.
|
|
211
|
-
var
|
|
212
|
-
var
|
|
213
|
-
var
|
|
214
|
-
|
|
197
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
198
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
199
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
200
|
+
|
|
215
201
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
216
202
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
217
|
-
|
|
218
|
-
|
|
203
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
204
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
219
205
|
}
|
|
220
206
|
}
|
|
221
207
|
|
|
222
208
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
223
209
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
224
|
-
|
|
210
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
225
211
|
}
|
|
226
212
|
}
|
|
227
213
|
|
|
228
|
-
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) =
|
|
214
|
+
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth)(
|
|
229
215
|
totalSignups,
|
|
230
216
|
stateRoots[i + 1],
|
|
231
217
|
ballotRoots[i + 1],
|
|
232
218
|
actualStateTreeDepth,
|
|
233
219
|
currentStateLeaves[i],
|
|
234
|
-
|
|
220
|
+
computedCurrentStateLeavesPathElements,
|
|
235
221
|
currentBallots[i],
|
|
236
|
-
|
|
222
|
+
computedCurrentBallotPathElements,
|
|
237
223
|
currentVoteWeights[i],
|
|
238
|
-
|
|
224
|
+
computedCurrentVoteWeightsPathElements,
|
|
239
225
|
computedCommandsStateIndex[i],
|
|
240
226
|
computedCommandsNewPublicKey[i],
|
|
241
227
|
computedCommandsVoteOptionIndex[i],
|
|
@@ -256,191 +242,3 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
256
242
|
var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
|
|
257
243
|
computedNewSbCommitment === newSbCommitment;
|
|
258
244
|
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Processes one message and updates the state accordingly.
|
|
262
|
-
* This template involves complex interactions, including transformations based on message type,
|
|
263
|
-
* validations against current states like voice credit balances or vote weights,
|
|
264
|
-
* and updates to Merkle trees representing state and ballot information.
|
|
265
|
-
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
266
|
-
* This template does not support Quadratic Voting (QV).
|
|
267
|
-
*/
|
|
268
|
-
template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
269
|
-
// Constants defining the structure and size of state and ballots.
|
|
270
|
-
var STATE_LEAF_LENGTH = 3;
|
|
271
|
-
var BALLOT_LENGTH = 2;
|
|
272
|
-
var MESSAGE_LENGTH = 10;
|
|
273
|
-
var PACKED_COMMAND_LENGTH = 4;
|
|
274
|
-
var VOTE_OPTION_TREE_ARITY = 5;
|
|
275
|
-
var STATE_TREE_ARITY = 2;
|
|
276
|
-
var BALLOT_NONCE_INDEX = 0;
|
|
277
|
-
// Ballot vote option (vote option) root index.
|
|
278
|
-
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
279
|
-
|
|
280
|
-
// Indices for elements within a state leaf.
|
|
281
|
-
// Public key.
|
|
282
|
-
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
283
|
-
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
284
|
-
// Voice Credit balance.
|
|
285
|
-
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
286
|
-
var NUMBER_BITS = 252;
|
|
287
|
-
|
|
288
|
-
// Number of users that have completed the sign up.
|
|
289
|
-
signal input totalSignups;
|
|
290
|
-
// The current value of the state tree root.
|
|
291
|
-
signal input currentStateRoot;
|
|
292
|
-
// The current value of the ballot tree root.
|
|
293
|
-
signal input currentBallotRoot;
|
|
294
|
-
// The actual tree depth (might be <= stateTreeDepth).
|
|
295
|
-
signal input actualStateTreeDepth;
|
|
296
|
-
|
|
297
|
-
// The state leaf and related path elements.
|
|
298
|
-
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
299
|
-
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
300
|
-
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
301
|
-
|
|
302
|
-
// The ballot and related path elements.
|
|
303
|
-
signal input ballot[BALLOT_LENGTH];
|
|
304
|
-
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
305
|
-
|
|
306
|
-
// The current vote weight and related path elements.
|
|
307
|
-
signal input currentVoteWeight;
|
|
308
|
-
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
309
|
-
|
|
310
|
-
// Inputs related to the command being processed.
|
|
311
|
-
signal input commandStateIndex;
|
|
312
|
-
signal input commandPublicKey[2];
|
|
313
|
-
signal input commandVoteOptionIndex;
|
|
314
|
-
signal input commandNewVoteWeight;
|
|
315
|
-
signal input commandNonce;
|
|
316
|
-
signal input commandPollId;
|
|
317
|
-
signal input commandSalt;
|
|
318
|
-
signal input commandSignaturePoint[2];
|
|
319
|
-
signal input commandSignatureScalar;
|
|
320
|
-
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
321
|
-
|
|
322
|
-
// The number of valid vote options for the poll.
|
|
323
|
-
signal input voteOptions;
|
|
324
|
-
|
|
325
|
-
signal output newStateRoot;
|
|
326
|
-
signal output newBallotRoot;
|
|
327
|
-
|
|
328
|
-
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
329
|
-
signal newBallotVoteOptionRoot;
|
|
330
|
-
|
|
331
|
-
// 1. Transform a state leaf and a ballot with a command.
|
|
332
|
-
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
333
|
-
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
334
|
-
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
|
|
335
|
-
totalSignups,
|
|
336
|
-
voteOptions,
|
|
337
|
-
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
338
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
339
|
-
ballot[BALLOT_NONCE_INDEX],
|
|
340
|
-
currentVoteWeight,
|
|
341
|
-
commandStateIndex,
|
|
342
|
-
commandPublicKey,
|
|
343
|
-
commandVoteOptionIndex,
|
|
344
|
-
commandNewVoteWeight,
|
|
345
|
-
commandNonce,
|
|
346
|
-
commandPollId,
|
|
347
|
-
commandSalt,
|
|
348
|
-
commandSignaturePoint,
|
|
349
|
-
commandSignatureScalar,
|
|
350
|
-
packedCommand
|
|
351
|
-
);
|
|
352
|
-
|
|
353
|
-
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
354
|
-
// Otherwise, generate indices for command.stateIndex.
|
|
355
|
-
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
356
|
-
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
357
|
-
|
|
358
|
-
// 3. Verify that the original state leaf exists in the given state root.
|
|
359
|
-
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
360
|
-
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
361
|
-
stateLeafHash,
|
|
362
|
-
actualStateTreeDepth,
|
|
363
|
-
computedStateLeafPathIndices,
|
|
364
|
-
stateLeafPathElements
|
|
365
|
-
);
|
|
366
|
-
|
|
367
|
-
stateLeafQip === currentStateRoot;
|
|
368
|
-
|
|
369
|
-
// 4. Verify that the original ballot exists in the given ballot root.
|
|
370
|
-
var computedBallot = PoseidonHasher(2)([
|
|
371
|
-
ballot[BALLOT_NONCE_INDEX],
|
|
372
|
-
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
373
|
-
]);
|
|
374
|
-
|
|
375
|
-
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
376
|
-
computedBallot,
|
|
377
|
-
computedStateLeafPathIndices,
|
|
378
|
-
ballotPathElements
|
|
379
|
-
);
|
|
380
|
-
|
|
381
|
-
computedBallotQip === currentBallotRoot;
|
|
382
|
-
|
|
383
|
-
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
384
|
-
// at commandVoteOptionIndex.
|
|
385
|
-
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
386
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
387
|
-
|
|
388
|
-
var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
389
|
-
currentVoteWeight,
|
|
390
|
-
computedCurrentVoteWeightPathIndices,
|
|
391
|
-
currentVoteWeightsPathElements
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
395
|
-
|
|
396
|
-
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
397
|
-
var voiceCreditBalanceMux = Mux1()(
|
|
398
|
-
[
|
|
399
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
400
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
|
|
401
|
-
],
|
|
402
|
-
computedIsValid
|
|
403
|
-
);
|
|
404
|
-
|
|
405
|
-
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
406
|
-
var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
407
|
-
voteWeightMux,
|
|
408
|
-
computedCurrentVoteWeightPathIndices,
|
|
409
|
-
currentVoteWeightsPathElements
|
|
410
|
-
);
|
|
411
|
-
|
|
412
|
-
// The new vote option root in the ballot
|
|
413
|
-
var newBallotVoteOptionRootMux = Mux1()(
|
|
414
|
-
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
415
|
-
computedIsValid
|
|
416
|
-
);
|
|
417
|
-
|
|
418
|
-
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
419
|
-
|
|
420
|
-
// 6. Generate a new state root.
|
|
421
|
-
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
422
|
-
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
423
|
-
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
424
|
-
voiceCreditBalanceMux
|
|
425
|
-
]);
|
|
426
|
-
|
|
427
|
-
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
428
|
-
computedNewStateLeafHash,
|
|
429
|
-
actualStateTreeDepth,
|
|
430
|
-
computedStateLeafPathIndices,
|
|
431
|
-
stateLeafPathElements
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
newStateRoot <== computedNewStateLeafQip;
|
|
435
|
-
|
|
436
|
-
// 7. Generate a new ballot root.
|
|
437
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
438
|
-
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
439
|
-
computedNewBallot,
|
|
440
|
-
computedStateLeafPathIndices,
|
|
441
|
-
ballotPathElements
|
|
442
|
-
);
|
|
443
|
-
|
|
444
|
-
newBallotRoot <== computedNewBallotQip;
|
|
445
|
-
}
|
|
446
|
-
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../../utils/PoseidonHasher.circom";
|
|
7
|
+
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
8
|
+
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
9
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
10
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
11
|
+
include "../../utils/non-qv/StateLeafAndBallotTransformer.circom";
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Processes one message and updates the state accordingly.
|
|
16
|
+
* This template involves complex interactions, including transformations based on message type,
|
|
17
|
+
* validations against current states like voice credit balances or vote weights,
|
|
18
|
+
* and updates to Merkle trees representing state and ballot information.
|
|
19
|
+
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
20
|
+
* This template does not support Quadratic Voting (QV).
|
|
21
|
+
*/
|
|
22
|
+
template SingleMessageProcessorNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
23
|
+
// Constants defining the structure and size of state and ballots.
|
|
24
|
+
var STATE_LEAF_LENGTH = 3;
|
|
25
|
+
var BALLOT_LENGTH = 2;
|
|
26
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
27
|
+
var VOTE_OPTION_TREE_ARITY = 5;
|
|
28
|
+
var STATE_TREE_ARITY = 2;
|
|
29
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
30
|
+
// Ballot vote option (vote option) root index.
|
|
31
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
32
|
+
|
|
33
|
+
// Indices for elements within a state leaf.
|
|
34
|
+
// Public key.
|
|
35
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
36
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
37
|
+
// Voice Credit balance.
|
|
38
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
39
|
+
|
|
40
|
+
// Number of users that have completed the sign up.
|
|
41
|
+
signal input totalSignups;
|
|
42
|
+
// The current value of the state tree root.
|
|
43
|
+
signal input currentStateRoot;
|
|
44
|
+
// The current value of the ballot tree root.
|
|
45
|
+
signal input currentBallotRoot;
|
|
46
|
+
// The actual tree depth (might be <= stateTreeDepth).
|
|
47
|
+
signal input actualStateTreeDepth;
|
|
48
|
+
|
|
49
|
+
// The state leaf and related path elements.
|
|
50
|
+
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
51
|
+
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
52
|
+
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
53
|
+
|
|
54
|
+
// The ballot and related path elements.
|
|
55
|
+
signal input ballot[BALLOT_LENGTH];
|
|
56
|
+
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
57
|
+
|
|
58
|
+
// The current vote weight and related path elements.
|
|
59
|
+
signal input currentVoteWeight;
|
|
60
|
+
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
61
|
+
|
|
62
|
+
// Inputs related to the command being processed.
|
|
63
|
+
signal input commandStateIndex;
|
|
64
|
+
signal input commandPublicKey[2];
|
|
65
|
+
signal input commandVoteOptionIndex;
|
|
66
|
+
signal input commandNewVoteWeight;
|
|
67
|
+
signal input commandNonce;
|
|
68
|
+
signal input commandPollId;
|
|
69
|
+
signal input commandSalt;
|
|
70
|
+
signal input commandSignaturePoint[2];
|
|
71
|
+
signal input commandSignatureScalar;
|
|
72
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
73
|
+
|
|
74
|
+
// The number of valid vote options for the poll.
|
|
75
|
+
signal input voteOptions;
|
|
76
|
+
|
|
77
|
+
signal output newStateRoot;
|
|
78
|
+
signal output newBallotRoot;
|
|
79
|
+
|
|
80
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
81
|
+
signal newBallotVoteOptionRoot;
|
|
82
|
+
|
|
83
|
+
// 1. Transform a state leaf and a ballot with a command.
|
|
84
|
+
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
85
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
86
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
|
|
87
|
+
totalSignups,
|
|
88
|
+
voteOptions,
|
|
89
|
+
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
90
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
91
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
92
|
+
currentVoteWeight,
|
|
93
|
+
commandStateIndex,
|
|
94
|
+
commandPublicKey,
|
|
95
|
+
commandVoteOptionIndex,
|
|
96
|
+
commandNewVoteWeight,
|
|
97
|
+
commandNonce,
|
|
98
|
+
commandPollId,
|
|
99
|
+
commandSalt,
|
|
100
|
+
commandSignaturePoint,
|
|
101
|
+
commandSignatureScalar,
|
|
102
|
+
packedCommand
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
106
|
+
// Otherwise, generate indices for command.stateIndex.
|
|
107
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
108
|
+
|
|
109
|
+
// 3. Verify that the original state leaf exists in the given state root.
|
|
110
|
+
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
111
|
+
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
112
|
+
stateLeafHash,
|
|
113
|
+
actualStateTreeDepth,
|
|
114
|
+
stateIndexMux,
|
|
115
|
+
stateLeafPathElements
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
stateLeafQip === currentStateRoot;
|
|
119
|
+
|
|
120
|
+
// 4. Verify that the original ballot exists in the given ballot root.
|
|
121
|
+
var computedBallot = PoseidonHasher(2)([
|
|
122
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
123
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
124
|
+
]);
|
|
125
|
+
var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
|
|
126
|
+
|
|
127
|
+
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
128
|
+
computedBallot,
|
|
129
|
+
computedStateLeafPathIndices,
|
|
130
|
+
ballotPathElements
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
computedBallotQip === currentBallotRoot;
|
|
134
|
+
|
|
135
|
+
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
136
|
+
// at commandVoteOptionIndex.
|
|
137
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
138
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
139
|
+
|
|
140
|
+
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
141
|
+
currentVoteWeight,
|
|
142
|
+
computedCurrentVoteWeightPathIndices,
|
|
143
|
+
currentVoteWeightsPathElements
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
147
|
+
|
|
148
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
149
|
+
var voiceCreditBalanceMux = Mux1()(
|
|
150
|
+
[
|
|
151
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
152
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
|
|
153
|
+
],
|
|
154
|
+
computedIsValid
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
158
|
+
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
159
|
+
voteWeightMux,
|
|
160
|
+
computedCurrentVoteWeightPathIndices,
|
|
161
|
+
currentVoteWeightsPathElements
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
// The new vote option root in the ballot
|
|
165
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
166
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
167
|
+
computedIsValid
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
171
|
+
|
|
172
|
+
// 6. Generate a new state root.
|
|
173
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
174
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
175
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
176
|
+
voiceCreditBalanceMux
|
|
177
|
+
]);
|
|
178
|
+
|
|
179
|
+
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
180
|
+
computedNewStateLeafHash,
|
|
181
|
+
actualStateTreeDepth,
|
|
182
|
+
stateIndexMux,
|
|
183
|
+
stateLeafPathElements
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
newStateRoot <== computedNewStateLeafQip;
|
|
187
|
+
|
|
188
|
+
// 7. Generate a new ballot root.
|
|
189
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
190
|
+
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
191
|
+
computedNewBallot,
|
|
192
|
+
computedStateLeafPathIndices,
|
|
193
|
+
ballotPathElements
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
newBallotRoot <== computedNewBallotQip;
|
|
197
|
+
}
|