@maci-protocol/circuits 0.0.0-ci.ba71b1e → 0.0.0-ci.bd42e06
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/circuits.json +16 -0
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +23 -22
- package/circom/coordinator/non-qv/tallyVotes.circom +22 -22
- package/circom/coordinator/qv/processMessages.circom +23 -22
- package/circom/coordinator/qv/tallyVotes.circom +33 -33
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/PrivateToPublicKey.circom +3 -3
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/MessageValidator.circom +4 -4
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +7 -7
- package/circom/utils/qv/MessageValidator.circom +4 -4
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +7 -7
- 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/QuinaryGeneratePathIndices.circom +44 -0
- 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 +2 -2
- package/package.json +9 -7
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -8,7 +8,7 @@ include "./unpack-element.circom";
|
|
|
8
8
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
9
|
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
10
10
|
include "../../utils/trees/LeafExists.circom";
|
|
11
|
-
include "../../utils/trees/
|
|
11
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
12
12
|
include "../../utils/CalculateTotal.circom";
|
|
13
13
|
include "../../utils/PoseidonHasher.circom";
|
|
14
14
|
|
|
@@ -18,22 +18,22 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
18
18
|
*/
|
|
19
19
|
template TallyVotesNonQv(
|
|
20
20
|
stateTreeDepth,
|
|
21
|
-
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
22
|
voteOptionTreeDepth
|
|
23
23
|
) {
|
|
24
24
|
// Ensure there's at least one level in the vote option tree.
|
|
25
25
|
assert(voteOptionTreeDepth > 0);
|
|
26
26
|
// Ensure the intermediate state tree has at least one level.
|
|
27
|
-
assert(
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
28
|
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
-
assert(
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
30
|
|
|
31
31
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
32
|
var TREE_ARITY = 5;
|
|
33
33
|
var BALLOT_TREE_ARITY = 2;
|
|
34
34
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
-
var batchSize = BALLOT_TREE_ARITY **
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
37
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
38
|
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
@@ -44,7 +44,7 @@ template TallyVotesNonQv(
|
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
45
|
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
46
|
// Difference in tree depths, used in path calculations.
|
|
47
|
-
var
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -64,7 +64,7 @@ template TallyVotesNonQv(
|
|
|
64
64
|
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
68
|
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
70
|
signal input currentResults[totalVoteOptions];
|
|
@@ -95,11 +95,11 @@ template TallyVotesNonQv(
|
|
|
95
95
|
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
var computedBallotSubroot = CheckRoot(
|
|
99
|
-
var computedBallotPathIndices[
|
|
98
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
99
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
100
100
|
|
|
101
101
|
// Verifies each ballot's existence within the ballot tree.
|
|
102
|
-
LeafExists(
|
|
102
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
103
103
|
computedBallotSubroot,
|
|
104
104
|
ballotPathElements,
|
|
105
105
|
computedBallotPathIndices,
|
|
@@ -110,7 +110,7 @@ template TallyVotesNonQv(
|
|
|
110
110
|
var computedVoteTree[batchSize];
|
|
111
111
|
|
|
112
112
|
for (var i = 0; i < batchSize; i++) {
|
|
113
|
-
computedVoteTree[i] =
|
|
113
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
114
114
|
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
115
115
|
}
|
|
116
116
|
|
|
@@ -122,26 +122,27 @@ template TallyVotesNonQv(
|
|
|
122
122
|
var computedCalculateTotalResult[totalVoteOptions];
|
|
123
123
|
|
|
124
124
|
for (var i = 0; i < totalVoteOptions; i++) {
|
|
125
|
-
var
|
|
126
|
-
|
|
125
|
+
var computedVotes[batchSize + 1];
|
|
126
|
+
computedVotes[batchSize] = currentResults[i] * computedIsZero;
|
|
127
|
+
|
|
127
128
|
for (var j = 0; j < batchSize; j++) {
|
|
128
|
-
|
|
129
|
+
computedVotes[j] = votes[j][i];
|
|
129
130
|
}
|
|
130
131
|
|
|
131
|
-
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(
|
|
132
|
+
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
// Tally the new spent voice credit total.
|
|
135
|
-
var
|
|
136
|
-
|
|
136
|
+
var computedTotalVoiceCreditSpent[batchSize * totalVoteOptions + 1];
|
|
137
|
+
computedTotalVoiceCreditSpent[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
137
138
|
|
|
138
139
|
for (var i = 0; i < batchSize; i++) {
|
|
139
140
|
for (var j = 0; j < totalVoteOptions; j++) {
|
|
140
|
-
|
|
141
|
+
computedTotalVoiceCreditSpent[i * totalVoteOptions + j] = votes[i][j];
|
|
141
142
|
}
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(
|
|
145
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedTotalVoiceCreditSpent);
|
|
145
146
|
|
|
146
147
|
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
147
148
|
ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
|
|
@@ -198,7 +199,7 @@ template TallyVotesNonQv(
|
|
|
198
199
|
signal input newSpentVoiceCreditSubtotalSalt;
|
|
199
200
|
|
|
200
201
|
// Compute the commitment to the current results.
|
|
201
|
-
var computedCurrentResultsRoot =
|
|
202
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
202
203
|
|
|
203
204
|
// Verify currentResultsCommitmentHash.
|
|
204
205
|
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
@@ -221,7 +222,7 @@ template TallyVotesNonQv(
|
|
|
221
222
|
isFirstCommitment === currentTallyCommitment;
|
|
222
223
|
|
|
223
224
|
// Compute the root of the new results.
|
|
224
|
-
var computedNewResultsRoot =
|
|
225
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
225
226
|
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
226
227
|
|
|
227
228
|
// Compute the commitment to the new spent voice credits value.
|
|
@@ -235,4 +236,3 @@ template TallyVotesNonQv(
|
|
|
235
236
|
|
|
236
237
|
computedNewTallyCommitment === newTallyCommitment;
|
|
237
238
|
}
|
|
238
|
-
|
|
@@ -10,7 +10,8 @@ include "../../utils/MessageHasher.circom";
|
|
|
10
10
|
include "../../utils/MessageToCommand.circom";
|
|
11
11
|
include "../../utils/PrivateToPublicKey.circom";
|
|
12
12
|
include "../../utils/qv/StateLeafAndBallotTransformer.circom";
|
|
13
|
-
include "../../utils/trees/
|
|
13
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
14
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
14
15
|
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
15
16
|
include "../../utils/trees/LeafExists.circom";
|
|
16
17
|
include "../../utils/trees/CheckRoot.circom";
|
|
@@ -205,20 +206,20 @@ template ProcessMessages(
|
|
|
205
206
|
// Start from batchSize and decrement for process in reverse order.
|
|
206
207
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
207
208
|
// Process as vote type message.
|
|
208
|
-
var
|
|
209
|
-
var
|
|
210
|
-
var
|
|
209
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
210
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
211
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
211
212
|
|
|
212
213
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
213
214
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
216
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
216
217
|
}
|
|
217
218
|
}
|
|
218
219
|
|
|
219
220
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
220
221
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
221
|
-
|
|
222
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
222
223
|
}
|
|
223
224
|
}
|
|
224
225
|
|
|
@@ -228,11 +229,11 @@ template ProcessMessages(
|
|
|
228
229
|
ballotRoots[i + 1],
|
|
229
230
|
actualStateTreeDepth,
|
|
230
231
|
currentStateLeaves[i],
|
|
231
|
-
|
|
232
|
+
computedCurrentStateLeavesPathElements,
|
|
232
233
|
currentBallots[i],
|
|
233
|
-
|
|
234
|
+
computedCurrentBallotPathElements,
|
|
234
235
|
currentVoteWeights[i],
|
|
235
|
-
|
|
236
|
+
computedCurrentVoteWeightsPathElements,
|
|
236
237
|
computedCommandsStateIndex[i],
|
|
237
238
|
computedCommandsNewPublicKey[i],
|
|
238
239
|
computedCommandsVoteOptionIndex[i],
|
|
@@ -328,12 +329,12 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
328
329
|
// commandNewVoteWeight * commandNewVoteWeight.
|
|
329
330
|
signal commandNewVoteWeightSquare;
|
|
330
331
|
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
331
|
-
signal
|
|
332
|
+
signal newBallotVoteOptionRoot;
|
|
332
333
|
|
|
333
334
|
// 1. Transform a state leaf and a ballot with a command.
|
|
334
335
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
335
|
-
var
|
|
336
|
-
(
|
|
336
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
337
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
337
338
|
totalSignups,
|
|
338
339
|
voteOptions,
|
|
339
340
|
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
@@ -388,9 +389,9 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
388
389
|
commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
|
|
389
390
|
|
|
390
391
|
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
391
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] =
|
|
392
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
392
393
|
|
|
393
|
-
var computedCurrentVoteWeightQip =
|
|
394
|
+
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
394
395
|
currentVoteWeight,
|
|
395
396
|
computedCurrentVoteWeightPathIndices,
|
|
396
397
|
currentVoteWeightsPathElements
|
|
@@ -408,7 +409,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
408
409
|
);
|
|
409
410
|
|
|
410
411
|
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
411
|
-
var computedNewVoteOptionTreeQip =
|
|
412
|
+
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
412
413
|
voteWeightMux,
|
|
413
414
|
computedCurrentVoteWeightPathIndices,
|
|
414
415
|
currentVoteWeightsPathElements
|
|
@@ -420,17 +421,17 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
420
421
|
computedIsValid
|
|
421
422
|
);
|
|
422
423
|
|
|
423
|
-
|
|
424
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
424
425
|
|
|
425
426
|
// 6. Generate a new state root.
|
|
426
|
-
var
|
|
427
|
-
|
|
428
|
-
|
|
427
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
428
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
429
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
429
430
|
voiceCreditBalanceMux
|
|
430
431
|
]);
|
|
431
432
|
|
|
432
433
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
433
|
-
|
|
434
|
+
computedNewStateLeafHash,
|
|
434
435
|
actualStateTreeDepth,
|
|
435
436
|
computedStateLeafPathIndices,
|
|
436
437
|
stateLeafPathElements
|
|
@@ -439,7 +440,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
439
440
|
newStateRoot <== computedNewStateLeafQip;
|
|
440
441
|
|
|
441
442
|
// 7. Generate a new ballot root.
|
|
442
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce,
|
|
443
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
443
444
|
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
444
445
|
computedNewBallot,
|
|
445
446
|
computedStateLeafPathIndices,
|
|
@@ -8,7 +8,7 @@ include "./unpack-element.circom";
|
|
|
8
8
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
9
|
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
10
10
|
include "../../utils/trees/LeafExists.circom";
|
|
11
|
-
include "../../utils/trees/
|
|
11
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
12
12
|
include "../../utils/CalculateTotal.circom";
|
|
13
13
|
include "../../utils/PoseidonHasher.circom";
|
|
14
14
|
|
|
@@ -18,22 +18,22 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
18
18
|
*/
|
|
19
19
|
template TallyVotes(
|
|
20
20
|
stateTreeDepth,
|
|
21
|
-
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
22
|
voteOptionTreeDepth
|
|
23
23
|
) {
|
|
24
24
|
// Ensure there's at least one level in the vote option tree.
|
|
25
25
|
assert(voteOptionTreeDepth > 0);
|
|
26
26
|
// Ensure the intermediate state tree has at least one level.
|
|
27
|
-
assert(
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
28
|
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
-
assert(
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
30
|
|
|
31
31
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
32
|
var TREE_ARITY = 5;
|
|
33
33
|
var BALLOT_TREE_ARITY = 2;
|
|
34
34
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
-
var batchSize = BALLOT_TREE_ARITY **
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
37
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
38
|
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
@@ -44,7 +44,7 @@ template TallyVotes(
|
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
45
|
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
46
|
// Difference in tree depths, used in path calculations.
|
|
47
|
-
var
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -64,7 +64,7 @@ template TallyVotes(
|
|
|
64
64
|
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
68
|
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
70
|
signal input currentResults[totalVoteOptions];
|
|
@@ -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
|
|
|
@@ -100,11 +100,11 @@ template TallyVotes(
|
|
|
100
100
|
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
var computedBallotSubroot = CheckRoot(
|
|
104
|
-
var computedBallotPathIndices[
|
|
103
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
104
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
105
105
|
|
|
106
106
|
// Verifies each ballot's existence within the ballot tree.
|
|
107
|
-
LeafExists(
|
|
107
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
108
108
|
computedBallotSubroot,
|
|
109
109
|
ballotPathElements,
|
|
110
110
|
computedBallotPathIndices,
|
|
@@ -114,7 +114,7 @@ template TallyVotes(
|
|
|
114
114
|
// Processes vote options, verifying each against its declared root.
|
|
115
115
|
var computedVoteTree[batchSize];
|
|
116
116
|
for (var i = 0; i < batchSize; i++) {
|
|
117
|
-
computedVoteTree[i] =
|
|
117
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
118
118
|
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
119
119
|
}
|
|
120
120
|
|
|
@@ -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,17 +217,17 @@ 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
|
-
var computedCurrentResultsRoot =
|
|
230
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
231
231
|
|
|
232
232
|
// Verify currentResultsCommitmentHash.
|
|
233
233
|
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
@@ -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 = QuinaryCheckRoot(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.
|
|
@@ -258,21 +258,21 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
258
258
|
isFirstCommitment === currentTallyCommitment;
|
|
259
259
|
|
|
260
260
|
// Compute the root of the new results.
|
|
261
|
-
var computedNewResultsRoot =
|
|
261
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
262
262
|
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
263
263
|
|
|
264
264
|
// Compute the commitment to the new spent voice credits value.
|
|
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 = QuinaryCheckRoot(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;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
pragma circom 2.0.0;
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Computes the cumulative sum of an array of
|
|
4
|
+
* Computes the cumulative sum of an array of length input signals.
|
|
5
5
|
* It iterates through each input, aggregating the sum up to that point,
|
|
6
6
|
* and outputs the total sum of all inputs. This template is useful for
|
|
7
7
|
* operations requiring the total sum of multiple signals, ensuring the
|
|
8
8
|
* final output reflects the cumulative total of the inputs provided.
|
|
9
9
|
*/
|
|
10
|
-
template CalculateTotal(
|
|
10
|
+
template CalculateTotal(length) {
|
|
11
11
|
// Array of values.
|
|
12
|
-
signal input nums[
|
|
12
|
+
signal input nums[length];
|
|
13
13
|
// Total sum.
|
|
14
14
|
signal output sum;
|
|
15
15
|
|
|
16
|
-
signal sums[
|
|
16
|
+
signal sums[length];
|
|
17
17
|
sums[0] <== nums[0];
|
|
18
18
|
|
|
19
|
-
for (var i = 1; i <
|
|
19
|
+
for (var i = 1; i < length; i++) {
|
|
20
20
|
sums[i] <== sums[i - 1] + nums[i];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
sum <== sums[
|
|
23
|
+
sum <== sums[length - 1];
|
|
24
24
|
}
|
|
@@ -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,91 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../VerifySignature.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a MACI message is valid or not.
|
|
10
|
+
* This template supports the full mode (all credits are spent on one option)
|
|
11
|
+
*/
|
|
12
|
+
template MessageValidatorFull() {
|
|
13
|
+
// Length of the packed command.
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 5;
|
|
17
|
+
|
|
18
|
+
// State index of the user.
|
|
19
|
+
signal input stateTreeIndex;
|
|
20
|
+
// Number of user sign-ups in the state tree.
|
|
21
|
+
signal input totalSignups;
|
|
22
|
+
// Vote option index.
|
|
23
|
+
signal input voteOptionIndex;
|
|
24
|
+
// Number of valid vote options for the poll.
|
|
25
|
+
signal input voteOptions;
|
|
26
|
+
// Ballot nonce.
|
|
27
|
+
signal input originalNonce;
|
|
28
|
+
// Command nonce.
|
|
29
|
+
signal input commandNonce;
|
|
30
|
+
// Packed command.
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
|
+
// Public key of the state leaf (user).
|
|
33
|
+
signal input publicKey[2];
|
|
34
|
+
// EdDSA signature of the command (R part).
|
|
35
|
+
signal input signaturePoint[2];
|
|
36
|
+
// EdDSA signature of the command (S part).
|
|
37
|
+
signal input signatureScalar;
|
|
38
|
+
// State leaf current voice credit balance.
|
|
39
|
+
signal input currentVoiceCreditBalance;
|
|
40
|
+
// Current number of votes for specific option.
|
|
41
|
+
signal input currentVotesForOption;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input voteWeight;
|
|
44
|
+
|
|
45
|
+
// True when the command is valid; otherwise false.
|
|
46
|
+
signal output isValid;
|
|
47
|
+
// True if the state leaf index is valid
|
|
48
|
+
signal output isStateLeafIndexValid;
|
|
49
|
+
// True if the vote option index is valid
|
|
50
|
+
signal output isVoteOptionIndexValid;
|
|
51
|
+
|
|
52
|
+
// Check (1) - The state leaf index must be valid.
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
54
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
55
|
+
// while 1 is for the first actual user.
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
57
|
+
|
|
58
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
59
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
|
+
|
|
61
|
+
// Check (3) - The nonce must be correct.
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
|
+
|
|
64
|
+
// Check (4) - The signature must be correct.
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
66
|
+
|
|
67
|
+
// Check (5) - There must be sufficient voice credits.
|
|
68
|
+
// The check ensure that currentVotesForOption + currentVoiceCreditBalance is equal to voteWeight.
|
|
69
|
+
var computedAreVoiceCreditsSpent = IsEqual()(
|
|
70
|
+
[
|
|
71
|
+
currentVotesForOption + currentVoiceCreditBalance,
|
|
72
|
+
voteWeight
|
|
73
|
+
]
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// When all five checks are correct, then isValid = 1.
|
|
77
|
+
var computedIsUpdateValid = IsEqual()(
|
|
78
|
+
[
|
|
79
|
+
TOTAL_CHECKS,
|
|
80
|
+
computedIsSignatureValid +
|
|
81
|
+
computedAreVoiceCreditsSpent +
|
|
82
|
+
computedIsNonceValid +
|
|
83
|
+
computedIsStateLeafIndexValid +
|
|
84
|
+
computedIsVoteOptionIndexValid
|
|
85
|
+
]
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
isValid <== computedIsUpdateValid;
|
|
89
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
90
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
91
|
+
}
|