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