@maci-protocol/circuits 0.0.0-ci.fc91dc9 → 0.0.0-ci.fd5247e
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/build/ts/types.d.ts +3 -3
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/coordinator/non-qv/processMessages.circom +9 -9
- package/circom/coordinator/non-qv/tallyVotes.circom +4 -4
- package/circom/coordinator/qv/processMessages.circom +9 -9
- package/circom/coordinator/qv/tallyVotes.circom +20 -20
- package/circom/utils/PrivateToPublicKey.circom +3 -3
- package/circom/utils/full/MessageValidator.circom +93 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/MessageValidator.circom +2 -2
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +7 -7
- package/circom/utils/qv/MessageValidator.circom +2 -2
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +7 -7
- package/circom/voter/PollJoined.circom +2 -2
- package/package.json +8 -6
|
@@ -326,12 +326,12 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
326
326
|
signal output newBallotRoot;
|
|
327
327
|
|
|
328
328
|
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
329
|
-
signal
|
|
329
|
+
signal newBallotVoteOptionRoot;
|
|
330
330
|
|
|
331
331
|
// 1. Transform a state leaf and a ballot with a command.
|
|
332
332
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
333
|
-
var
|
|
334
|
-
(
|
|
333
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
334
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
|
|
335
335
|
totalSignups,
|
|
336
336
|
voteOptions,
|
|
337
337
|
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
@@ -415,17 +415,17 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
415
415
|
computedIsValid
|
|
416
416
|
);
|
|
417
417
|
|
|
418
|
-
|
|
418
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
419
419
|
|
|
420
420
|
// 6. Generate a new state root.
|
|
421
|
-
var
|
|
422
|
-
|
|
423
|
-
|
|
421
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
422
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
423
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
424
424
|
voiceCreditBalanceMux
|
|
425
425
|
]);
|
|
426
426
|
|
|
427
427
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
428
|
-
|
|
428
|
+
computedNewStateLeafHash,
|
|
429
429
|
actualStateTreeDepth,
|
|
430
430
|
computedStateLeafPathIndices,
|
|
431
431
|
stateLeafPathElements
|
|
@@ -434,7 +434,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
434
434
|
newStateRoot <== computedNewStateLeafQip;
|
|
435
435
|
|
|
436
436
|
// 7. Generate a new ballot root.
|
|
437
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce,
|
|
437
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
438
438
|
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
439
439
|
computedNewBallot,
|
|
440
440
|
computedStateLeafPathIndices,
|
|
@@ -132,16 +132,16 @@ template TallyVotesNonQv(
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
// Tally the new spent voice credit total.
|
|
135
|
-
var
|
|
136
|
-
|
|
135
|
+
var computedTotalVoiceCreditSpent[batchSize * totalVoteOptions + 1];
|
|
136
|
+
computedTotalVoiceCreditSpent[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
137
137
|
|
|
138
138
|
for (var i = 0; i < batchSize; i++) {
|
|
139
139
|
for (var j = 0; j < totalVoteOptions; j++) {
|
|
140
|
-
|
|
140
|
+
computedTotalVoiceCreditSpent[i * totalVoteOptions + j] = votes[i][j];
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(
|
|
144
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedTotalVoiceCreditSpent);
|
|
145
145
|
|
|
146
146
|
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
147
147
|
ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
|
|
@@ -328,12 +328,12 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
328
328
|
// commandNewVoteWeight * commandNewVoteWeight.
|
|
329
329
|
signal commandNewVoteWeightSquare;
|
|
330
330
|
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
331
|
-
signal
|
|
331
|
+
signal newBallotVoteOptionRoot;
|
|
332
332
|
|
|
333
333
|
// 1. Transform a state leaf and a ballot with a command.
|
|
334
334
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
335
|
-
var
|
|
336
|
-
(
|
|
335
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
336
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
337
337
|
totalSignups,
|
|
338
338
|
voteOptions,
|
|
339
339
|
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
@@ -420,17 +420,17 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
420
420
|
computedIsValid
|
|
421
421
|
);
|
|
422
422
|
|
|
423
|
-
|
|
423
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
424
424
|
|
|
425
425
|
// 6. Generate a new state root.
|
|
426
|
-
var
|
|
427
|
-
|
|
428
|
-
|
|
426
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
427
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
428
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
429
429
|
voiceCreditBalanceMux
|
|
430
430
|
]);
|
|
431
431
|
|
|
432
432
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
433
|
-
|
|
433
|
+
computedNewStateLeafHash,
|
|
434
434
|
actualStateTreeDepth,
|
|
435
435
|
computedStateLeafPathIndices,
|
|
436
436
|
stateLeafPathElements
|
|
@@ -439,7 +439,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
439
439
|
newStateRoot <== computedNewStateLeafQip;
|
|
440
440
|
|
|
441
441
|
// 7. Generate a new ballot root.
|
|
442
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce,
|
|
442
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
443
443
|
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
444
444
|
computedNewBallot,
|
|
445
445
|
computedStateLeafPathIndices,
|
|
@@ -75,13 +75,13 @@ template TallyVotes(
|
|
|
75
75
|
// Salt for the total spent voice credits.
|
|
76
76
|
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
77
77
|
// Spent voice credits per vote option.
|
|
78
|
-
signal input
|
|
78
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
79
79
|
// Salt for the root of spent credits per option.
|
|
80
|
-
signal input
|
|
80
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
81
81
|
// Salt for the root of the new results.
|
|
82
82
|
signal input newResultsRootSalt;
|
|
83
83
|
// Salt for the new spent credits per vote option root.
|
|
84
|
-
signal input
|
|
84
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
85
85
|
// Salt for the new total spent voice credits root.
|
|
86
86
|
signal input newSpentVoiceCreditSubtotalSalt;
|
|
87
87
|
|
|
@@ -149,13 +149,13 @@ template TallyVotes(
|
|
|
149
149
|
var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
|
|
150
150
|
|
|
151
151
|
for (var i = 0; i < totalVoteOptions; i++) {
|
|
152
|
-
var
|
|
153
|
-
|
|
152
|
+
var computedTotalVoiceCreditSpent[batchSize + 1];
|
|
153
|
+
computedTotalVoiceCreditSpent[batchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
|
|
154
154
|
for (var j = 0; j < batchSize; j++) {
|
|
155
|
-
|
|
155
|
+
computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(
|
|
158
|
+
computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedTotalVoiceCreditSpent);
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
@@ -171,10 +171,10 @@ template TallyVotes(
|
|
|
171
171
|
currentSpentVoiceCreditSubtotalSalt,
|
|
172
172
|
computedNewSpentVoiceCreditSubtotal,
|
|
173
173
|
newSpentVoiceCreditSubtotalSalt,
|
|
174
|
-
|
|
175
|
-
|
|
174
|
+
currentPerVoteOptionSpentVoiceCredits,
|
|
175
|
+
currentPerVoteOptionSpentVoiceCreditsRootSalt,
|
|
176
176
|
computedNewPerVOSpentVoiceCredits,
|
|
177
|
-
|
|
177
|
+
newPerVoteOptionSpentVoiceCreditsRootSalt
|
|
178
178
|
);
|
|
179
179
|
}
|
|
180
180
|
|
|
@@ -217,14 +217,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
217
217
|
signal input newSpentVoiceCreditSubtotalSalt;
|
|
218
218
|
|
|
219
219
|
// Spent voice credits per vote option.
|
|
220
|
-
signal input
|
|
220
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
221
221
|
// Salt for the root of spent credits per option.
|
|
222
|
-
signal input
|
|
222
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
223
223
|
|
|
224
224
|
// New spent voice credits per vote option.
|
|
225
|
-
signal input
|
|
225
|
+
signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
226
226
|
// Salt for the root of new spent credits per option.
|
|
227
|
-
signal input
|
|
227
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
228
228
|
|
|
229
229
|
// Compute the commitment to the current results.
|
|
230
230
|
var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
@@ -236,14 +236,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
236
236
|
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
237
237
|
|
|
238
238
|
// Compute the root of the spent voice credits per vote option.
|
|
239
|
-
var
|
|
240
|
-
var
|
|
239
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
|
|
240
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
241
241
|
|
|
242
242
|
// Commit to the current tally.
|
|
243
243
|
var computedCurrentTallyCommitment = PoseidonHasher(3)([
|
|
244
244
|
computedCurrentResultsCommitment,
|
|
245
245
|
computedCurrentSpentVoiceCreditsCommitment,
|
|
246
|
-
|
|
246
|
+
computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
|
|
247
247
|
]);
|
|
248
248
|
|
|
249
249
|
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
@@ -265,14 +265,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
265
265
|
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
266
266
|
|
|
267
267
|
// Compute the root of the spent voice credits per vote option.
|
|
268
|
-
var
|
|
269
|
-
var
|
|
268
|
+
var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
|
|
269
|
+
var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
270
270
|
|
|
271
271
|
// Commit to the new tally.
|
|
272
272
|
var computedNewTallyCommitment = PoseidonHasher(3)([
|
|
273
273
|
computedNewResultsCommitment,
|
|
274
274
|
computedNewSpentVoiceCreditsCommitment,
|
|
275
|
-
|
|
275
|
+
computedNewPerVoteOptionSpentVoiceCreditsCommitment
|
|
276
276
|
]);
|
|
277
277
|
|
|
278
278
|
computedNewTallyCommitment === newTallyCommitment;
|
|
@@ -29,10 +29,10 @@ template PrivateToPublicKey() {
|
|
|
29
29
|
isLessThan === 1;
|
|
30
30
|
|
|
31
31
|
// Convert the private key to bits.
|
|
32
|
-
var
|
|
32
|
+
var computedPrivateBits[253] = Num2Bits(253)(privateKey);
|
|
33
33
|
|
|
34
34
|
// Perform scalar multiplication with the basepoint.
|
|
35
|
-
var
|
|
35
|
+
var computedPublicKey[2] = EscalarMulFix(253, BASE8)(computedPrivateBits);
|
|
36
36
|
|
|
37
|
-
publicKey <==
|
|
37
|
+
publicKey <== computedPublicKey;
|
|
38
38
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// zk-kit imports
|
|
6
|
+
include "./safe-comparators.circom";
|
|
7
|
+
// local imports
|
|
8
|
+
include "../VerifySignature.circom";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Checks if a MACI message is valid or not.
|
|
12
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
13
|
+
*/
|
|
14
|
+
template MessageValidatorFull() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
|
+
// Number of checks to be performed.
|
|
18
|
+
var TOTAL_CHECKS = 5;
|
|
19
|
+
|
|
20
|
+
// State index of the user.
|
|
21
|
+
signal input stateTreeIndex;
|
|
22
|
+
// Number of user sign-ups in the state tree.
|
|
23
|
+
signal input totalSignups;
|
|
24
|
+
// Vote option index.
|
|
25
|
+
signal input voteOptionIndex;
|
|
26
|
+
// Number of valid vote options for the poll.
|
|
27
|
+
signal input voteOptions;
|
|
28
|
+
// Ballot nonce.
|
|
29
|
+
signal input originalNonce;
|
|
30
|
+
// Command nonce.
|
|
31
|
+
signal input nonce;
|
|
32
|
+
// Packed command.
|
|
33
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
34
|
+
// Public key of the state leaf (user).
|
|
35
|
+
signal input publicKey[2];
|
|
36
|
+
// EdDSA signature of the command (R part).
|
|
37
|
+
signal input signaturePoint[2];
|
|
38
|
+
// EdDSA signature of the command (S part).
|
|
39
|
+
signal input signatureScalar;
|
|
40
|
+
// State leaf current voice credit balance.
|
|
41
|
+
signal input currentVoiceCreditBalance;
|
|
42
|
+
// Current number of votes for specific option.
|
|
43
|
+
signal input currentVotesForOption;
|
|
44
|
+
// Vote weight.
|
|
45
|
+
signal input voteWeight;
|
|
46
|
+
|
|
47
|
+
// True when the command is valid; otherwise false.
|
|
48
|
+
signal output isValid;
|
|
49
|
+
// True if the state leaf index is valid
|
|
50
|
+
signal output isStateLeafIndexValid;
|
|
51
|
+
// True if the vote option index is valid
|
|
52
|
+
signal output isVoteOptionIndexValid;
|
|
53
|
+
|
|
54
|
+
// Check (1) - The state leaf index must be valid.
|
|
55
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
56
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
57
|
+
// while 1 is for the first actual user.
|
|
58
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
59
|
+
|
|
60
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
61
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
62
|
+
|
|
63
|
+
// Check (3) - The nonce must be correct.
|
|
64
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
|
|
65
|
+
|
|
66
|
+
// Check (4) - The signature must be correct.
|
|
67
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
68
|
+
|
|
69
|
+
// Check (5) - There must be sufficient voice credits.
|
|
70
|
+
// The check ensure that currentVotesForOption + currentVoiceCreditBalance is equal to voteWeight.
|
|
71
|
+
var computedAreVoiceCreditsSpent = IsEqual()(
|
|
72
|
+
[
|
|
73
|
+
currentVotesForOption + currentVoiceCreditBalance,
|
|
74
|
+
voteWeight
|
|
75
|
+
]
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// When all five checks are correct, then isValid = 1.
|
|
79
|
+
var computedIsUpdateValid = IsEqual()(
|
|
80
|
+
[
|
|
81
|
+
TOTAL_CHECKS,
|
|
82
|
+
computedIsSignatureValid +
|
|
83
|
+
computedAreVoiceCreditsSpent +
|
|
84
|
+
computedIsNonceValid +
|
|
85
|
+
computedIsStateLeafIndexValid +
|
|
86
|
+
computedIsVoteOptionIndexValid
|
|
87
|
+
]
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
isValid <== computedIsUpdateValid;
|
|
91
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
92
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
93
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "./MessageValidator.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
10
|
+
* If the message is correct, updates the public key in the state leaf and the nonce
|
|
11
|
+
* in the ballot using multiplexer components.
|
|
12
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
13
|
+
*/
|
|
14
|
+
template StateLeafAndBallotTransformerFull() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
17
|
+
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input totalSignups;
|
|
20
|
+
// Number of valid vote options for the poll.
|
|
21
|
+
signal input voteOptions;
|
|
22
|
+
|
|
23
|
+
// The following signals represents a state leaf (signed up user).
|
|
24
|
+
// Public key.
|
|
25
|
+
signal input stateLeafPublicKey[2];
|
|
26
|
+
// Current voice credit balance.
|
|
27
|
+
signal input stateLeafVoiceCreditBalance;
|
|
28
|
+
|
|
29
|
+
// The following signals represents a ballot.
|
|
30
|
+
// Nonce.
|
|
31
|
+
signal input ballotNonce;
|
|
32
|
+
// Current number of votes for specific option.
|
|
33
|
+
signal input ballotCurrentVotesForOption;
|
|
34
|
+
|
|
35
|
+
// The following signals represents a command.
|
|
36
|
+
// State index of the user.
|
|
37
|
+
signal input commandStateIndex;
|
|
38
|
+
// Public key of the user.
|
|
39
|
+
signal input commandPublicKey[2];
|
|
40
|
+
// Vote option index.
|
|
41
|
+
signal input commandVoteOptionIndex;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input commandNewVoteWeight;
|
|
44
|
+
// Nonce.
|
|
45
|
+
signal input commandNonce;
|
|
46
|
+
// Poll identifier.
|
|
47
|
+
signal input commandPollId;
|
|
48
|
+
// Salt.
|
|
49
|
+
signal input commandSalt;
|
|
50
|
+
// EdDSA signature of the command (R part).
|
|
51
|
+
signal input commandSignaturePoint[2];
|
|
52
|
+
// EdDSA signature of the command (S part).
|
|
53
|
+
signal input commandSignatureScalar;
|
|
54
|
+
// Packed command.
|
|
55
|
+
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
57
|
+
|
|
58
|
+
// New state leaf (if the command is valid).
|
|
59
|
+
signal output newStateLeafPublicKey[2];
|
|
60
|
+
// New ballot (if the command is valid).
|
|
61
|
+
signal output newBallotNonce;
|
|
62
|
+
|
|
63
|
+
// True when the command is valid; otherwise false.
|
|
64
|
+
signal output isValid;
|
|
65
|
+
// True if the state leaf index is valid
|
|
66
|
+
signal output isStateLeafIndexValid;
|
|
67
|
+
// True if the vote option index is valid
|
|
68
|
+
signal output isVoteOptionIndexValid;
|
|
69
|
+
|
|
70
|
+
// Check if the command / message is valid.
|
|
71
|
+
var (
|
|
72
|
+
computedIsValid,
|
|
73
|
+
computedIsStateLeafIndexValid,
|
|
74
|
+
computedIsVoteOptionIndexValid
|
|
75
|
+
) = MessageValidatorFull()(
|
|
76
|
+
commandStateIndex,
|
|
77
|
+
totalSignups,
|
|
78
|
+
commandVoteOptionIndex,
|
|
79
|
+
voteOptions,
|
|
80
|
+
ballotNonce,
|
|
81
|
+
commandNonce,
|
|
82
|
+
packedCommand,
|
|
83
|
+
stateLeafPublicKey,
|
|
84
|
+
commandSignaturePoint,
|
|
85
|
+
commandSignatureScalar,
|
|
86
|
+
stateLeafVoiceCreditBalance,
|
|
87
|
+
ballotCurrentVotesForOption,
|
|
88
|
+
commandNewVoteWeight
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// If the message is valid then we swap out the public key.
|
|
92
|
+
// This means using a Mux1() for publicKey[0] and another one
|
|
93
|
+
// for publicKey[1].
|
|
94
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()(
|
|
95
|
+
[
|
|
96
|
+
stateLeafPublicKey[0],
|
|
97
|
+
commandPublicKey[0]
|
|
98
|
+
],
|
|
99
|
+
computedIsValid
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()(
|
|
103
|
+
[
|
|
104
|
+
stateLeafPublicKey[1],
|
|
105
|
+
commandPublicKey[1]
|
|
106
|
+
],
|
|
107
|
+
computedIsValid
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
111
|
+
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
112
|
+
|
|
113
|
+
// If the message is valid, then we swap out the ballot nonce
|
|
114
|
+
// using a Mux1().
|
|
115
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
116
|
+
|
|
117
|
+
newBallotNonce <== computedNewBallotNonceMux;
|
|
118
|
+
|
|
119
|
+
isValid <== computedIsValid;
|
|
120
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
121
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
122
|
+
}
|
|
@@ -31,9 +31,9 @@ template MessageValidatorNonQv() {
|
|
|
31
31
|
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
32
|
// Public key of the state leaf (user).
|
|
33
33
|
signal input publicKey[2];
|
|
34
|
-
//
|
|
34
|
+
// EdDSA signature of the command (R part).
|
|
35
35
|
signal input signaturePoint[2];
|
|
36
|
-
//
|
|
36
|
+
// EdDSA signature of the command (S part).
|
|
37
37
|
signal input signatureScalar;
|
|
38
38
|
// State leaf current voice credit balance.
|
|
39
39
|
signal input currentVoiceCreditBalance;
|
|
@@ -47,9 +47,9 @@ template StateLeafAndBallotTransformerNonQv() {
|
|
|
47
47
|
signal input commandPollId;
|
|
48
48
|
// Salt.
|
|
49
49
|
signal input commandSalt;
|
|
50
|
-
//
|
|
50
|
+
// EdDSA signature of the command (R part).
|
|
51
51
|
signal input commandSignaturePoint[2];
|
|
52
|
-
//
|
|
52
|
+
// EdDSA signature of the command (S part).
|
|
53
53
|
signal input commandSignatureScalar;
|
|
54
54
|
// Packed command.
|
|
55
55
|
// nb. we are assuming that the packedCommand is always valid.
|
|
@@ -68,7 +68,7 @@ template StateLeafAndBallotTransformerNonQv() {
|
|
|
68
68
|
signal output isVoteOptionIndexValid;
|
|
69
69
|
|
|
70
70
|
// Check if the command / message is valid.
|
|
71
|
-
var (
|
|
71
|
+
var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidatorNonQv()(
|
|
72
72
|
commandStateIndex,
|
|
73
73
|
totalSignups,
|
|
74
74
|
commandVoteOptionIndex,
|
|
@@ -87,19 +87,19 @@ template StateLeafAndBallotTransformerNonQv() {
|
|
|
87
87
|
// If the message is valid then we swap out the public key.
|
|
88
88
|
// This means using a Mux1() for publicKey[0] and another one
|
|
89
89
|
// for publicKey[1].
|
|
90
|
-
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]],
|
|
91
|
-
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]],
|
|
90
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
|
|
91
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
|
|
92
92
|
|
|
93
93
|
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
94
94
|
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
95
95
|
|
|
96
96
|
// If the message is valid, then we swap out the ballot nonce
|
|
97
97
|
// using a Mux1().
|
|
98
|
-
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce],
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
99
99
|
|
|
100
100
|
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
101
|
|
|
102
|
-
isValid <==
|
|
102
|
+
isValid <== computedIsValid;
|
|
103
103
|
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
104
|
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
105
|
}
|
|
@@ -31,9 +31,9 @@ template MessageValidator() {
|
|
|
31
31
|
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
32
|
// Public key of the state leaf (user).
|
|
33
33
|
signal input publicKey[2];
|
|
34
|
-
//
|
|
34
|
+
// EdDSA signature of the command (R part).
|
|
35
35
|
signal input signaturePoint[2];
|
|
36
|
-
//
|
|
36
|
+
// EdDSA signature of the command (S part).
|
|
37
37
|
signal input signatureScalar;
|
|
38
38
|
// State leaf current voice credit balance.
|
|
39
39
|
signal input currentVoiceCreditBalance;
|
|
@@ -47,9 +47,9 @@ template StateLeafAndBallotTransformer() {
|
|
|
47
47
|
signal input commandPollId;
|
|
48
48
|
// Salt.
|
|
49
49
|
signal input commandSalt;
|
|
50
|
-
//
|
|
50
|
+
// EdDSA signature of the command (R part).
|
|
51
51
|
signal input commandSignaturePoint[2];
|
|
52
|
-
//
|
|
52
|
+
// EdDSA signature of the command (S part).
|
|
53
53
|
signal input commandSignatureScalar;
|
|
54
54
|
// Packed command.
|
|
55
55
|
// nb. we are assuming that the packedCommand is always valid.
|
|
@@ -68,7 +68,7 @@ template StateLeafAndBallotTransformer() {
|
|
|
68
68
|
signal output isVoteOptionIndexValid;
|
|
69
69
|
|
|
70
70
|
// Check if the command / message is valid.
|
|
71
|
-
var (
|
|
71
|
+
var (computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
|
|
72
72
|
commandStateIndex,
|
|
73
73
|
totalSignups,
|
|
74
74
|
commandVoteOptionIndex,
|
|
@@ -87,19 +87,19 @@ template StateLeafAndBallotTransformer() {
|
|
|
87
87
|
// If the message is valid then we swap out the public key.
|
|
88
88
|
// This means using a Mux1() for publicKey[0] and another one
|
|
89
89
|
// for publicKey[1].
|
|
90
|
-
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]],
|
|
91
|
-
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]],
|
|
90
|
+
var computedNewstateLeafPublicKey0Mux = Mux1()([stateLeafPublicKey[0], commandPublicKey[0]], computedIsValid);
|
|
91
|
+
var computedNewstateLeafPublicKey1Mux = Mux1()([stateLeafPublicKey[1], commandPublicKey[1]], computedIsValid);
|
|
92
92
|
|
|
93
93
|
newStateLeafPublicKey[0] <== computedNewstateLeafPublicKey0Mux;
|
|
94
94
|
newStateLeafPublicKey[1] <== computedNewstateLeafPublicKey1Mux;
|
|
95
95
|
|
|
96
96
|
// If the message is valid, then we swap out the ballot nonce
|
|
97
97
|
// using a Mux1().
|
|
98
|
-
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce],
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, commandNonce], computedIsValid);
|
|
99
99
|
|
|
100
100
|
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
101
|
|
|
102
|
-
isValid <==
|
|
102
|
+
isValid <== computedIsValid;
|
|
103
103
|
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
104
|
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
105
|
}
|
|
@@ -32,12 +32,12 @@ template PollJoined(stateTreeDepth) {
|
|
|
32
32
|
var stateLeaf = PoseidonHasher(3)([derivedPublicKey[0], derivedPublicKey[1], voiceCreditsBalance]);
|
|
33
33
|
|
|
34
34
|
// Inclusion proof
|
|
35
|
-
var
|
|
35
|
+
var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
36
36
|
stateLeaf,
|
|
37
37
|
actualStateTreeDepth,
|
|
38
38
|
pathIndices,
|
|
39
39
|
pathElements
|
|
40
40
|
);
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
calculatedRoot === stateRoot;
|
|
43
43
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maci-protocol/circuits",
|
|
3
|
-
"version": "0.0.0-ci.
|
|
3
|
+
"version": "0.0.0-ci.fd5247e",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "zk-SNARK circuits for MACI",
|
|
6
6
|
"main": "build/ts/index.js",
|
|
@@ -31,8 +31,10 @@
|
|
|
31
31
|
"test:poseidonHasher": "pnpm run mocha-test ts/__tests__/PoseidonHasher.test.ts",
|
|
32
32
|
"test:messageHasher": "pnpm run mocha-test ts/__tests__/MessageHasher.test.ts",
|
|
33
33
|
"test:slAndBallotTransformer": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformer.test.ts",
|
|
34
|
+
"test:slAndBallotTransformerFull": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformerFull.test.ts",
|
|
34
35
|
"test:messageToCommand": "pnpm run mocha-test ts/__tests__/MessageToCommand.test.ts",
|
|
35
36
|
"test:messageValidator": "pnpm run mocha-test ts/__tests__/MessageValidator.test.ts",
|
|
37
|
+
"test:messageValidatorFull": "pnpm run mocha-test ts/__tests__/MessageValidatorFull.test.ts",
|
|
36
38
|
"test:verifySignature": "pnpm run mocha-test ts/__tests__/VerifySignature.test.ts",
|
|
37
39
|
"test:privateToPublicKey": "pnpm run mocha-test ts/__tests__/PrivateToPublicKey.test.ts",
|
|
38
40
|
"test:calculateTotal": "pnpm run mocha-test ts/__tests__/CalculateTotal.test.ts",
|
|
@@ -44,10 +46,10 @@
|
|
|
44
46
|
"test:pollJoined": "pnpm run mocha-test ts/__tests__/PollJoined.test.ts"
|
|
45
47
|
},
|
|
46
48
|
"dependencies": {
|
|
47
|
-
"@maci-protocol/core": "0.0.0-ci.
|
|
48
|
-
"@maci-protocol/crypto": "0.0.0-ci.
|
|
49
|
-
"@maci-protocol/domainobjs": "0.0.0-ci.
|
|
50
|
-
"@maci-protocol/sdk": "0.0.0-ci.
|
|
49
|
+
"@maci-protocol/core": "0.0.0-ci.fd5247e",
|
|
50
|
+
"@maci-protocol/crypto": "0.0.0-ci.fd5247e",
|
|
51
|
+
"@maci-protocol/domainobjs": "0.0.0-ci.fd5247e",
|
|
52
|
+
"@maci-protocol/sdk": "0.0.0-ci.fd5247e",
|
|
51
53
|
"@zk-kit/circuits": "^0.4.0",
|
|
52
54
|
"circomkit": "^0.3.2",
|
|
53
55
|
"circomlib": "^2.0.5"
|
|
@@ -68,5 +70,5 @@
|
|
|
68
70
|
"ts-node": "^10.9.1",
|
|
69
71
|
"typescript": "^5.8.3"
|
|
70
72
|
},
|
|
71
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "82107dafe2dcdb9d11a30402ca81db9605accdec"
|
|
72
74
|
}
|