@maci-protocol/circuits 0.0.0-ci.a1bedc5 → 0.0.0-ci.a51dc13
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/LICENSE +1 -2
- package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
- package/build/ts/generateZkeys.js.map +1 -0
- package/build/ts/types.d.ts +5 -5
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +20 -4
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +102 -104
- package/circom/coordinator/non-qv/tallyVotes.circom +47 -44
- package/circom/coordinator/qv/processMessages.circom +103 -102
- package/circom/coordinator/qv/tallyVotes.circom +61 -61
- 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 → MessageValidator.circom} +15 -13
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +34 -34
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +34 -34
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +1 -1
- 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 +11 -9
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -6,9 +6,9 @@ include "./comparators.circom";
|
|
|
6
6
|
include "./unpack-element.circom";
|
|
7
7
|
// local imports
|
|
8
8
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
|
-
include "../../utils/trees/
|
|
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,33 +18,33 @@ 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
|
-
var
|
|
38
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
40
40
|
// Number of elements in each ballot.
|
|
41
41
|
var BALLOT_LENGTH = 2;
|
|
42
42
|
// Index for the nonce in the ballot array.
|
|
43
|
-
var
|
|
43
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
|
-
var
|
|
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;
|
|
@@ -61,13 +61,13 @@ template TallyVotes(
|
|
|
61
61
|
// Start index of given batch
|
|
62
62
|
signal input index;
|
|
63
63
|
// Number of users that signup
|
|
64
|
-
signal input
|
|
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[
|
|
68
|
-
signal input votes[batchSize][
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
|
+
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
|
-
signal input currentResults[
|
|
70
|
+
signal input currentResults[totalVoteOptions];
|
|
71
71
|
// Salt for the root of the current results.
|
|
72
72
|
signal input currentResultsRootSalt;
|
|
73
73
|
// Total voice credits spent so far.
|
|
@@ -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
|
|
|
@@ -90,21 +90,21 @@ template TallyVotes(
|
|
|
90
90
|
computedSbCommitment === sbCommitment;
|
|
91
91
|
|
|
92
92
|
// Validates that the index is within the valid range of sign-ups.
|
|
93
|
-
var
|
|
94
|
-
|
|
93
|
+
var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
|
|
94
|
+
totalSignupsValid === 1;
|
|
95
95
|
|
|
96
96
|
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
97
97
|
var computedBallotHashers[batchSize];
|
|
98
98
|
|
|
99
99
|
for (var i = 0; i < batchSize; i++) {
|
|
100
|
-
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][
|
|
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,8 +114,8 @@ 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] =
|
|
118
|
-
computedVoteTree[i] === ballots[i][
|
|
117
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
118
|
+
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
@@ -123,8 +123,8 @@ template TallyVotes(
|
|
|
123
123
|
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
124
124
|
|
|
125
125
|
// Tally the new results.
|
|
126
|
-
var computedCalculateTotalResult[
|
|
127
|
-
for (var i = 0; i <
|
|
126
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
127
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
128
128
|
var numsRC[batchSize + 1];
|
|
129
129
|
numsRC[batchSize] = currentResults[i] * computedIsZero;
|
|
130
130
|
for (var j = 0; j < batchSize; j++) {
|
|
@@ -135,27 +135,27 @@ template TallyVotes(
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
// Tally the new spent voice credit total.
|
|
138
|
-
var numsSVC[batchSize *
|
|
139
|
-
numsSVC[batchSize *
|
|
138
|
+
var numsSVC[batchSize * totalVoteOptions + 1];
|
|
139
|
+
numsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
140
140
|
for (var i = 0; i < batchSize; i++) {
|
|
141
|
-
for (var j = 0; j <
|
|
142
|
-
numsSVC[i *
|
|
141
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
142
|
+
numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize *
|
|
146
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(numsSVC);
|
|
147
147
|
|
|
148
148
|
// Tally the spent voice credits per vote option.
|
|
149
|
-
var computedNewPerVOSpentVoiceCredits[
|
|
149
|
+
var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
|
|
150
150
|
|
|
151
|
-
for (var i = 0; i <
|
|
152
|
-
var
|
|
153
|
-
|
|
151
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
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
|
|
|
@@ -187,7 +187,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
187
187
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
188
188
|
var TREE_ARITY = 5;
|
|
189
189
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
190
|
-
var
|
|
190
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
191
191
|
|
|
192
192
|
// Equal to 1 if this is the first batch, otherwise 0.
|
|
193
193
|
signal input isFirstBatch;
|
|
@@ -197,12 +197,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
197
197
|
signal input newTallyCommitment;
|
|
198
198
|
|
|
199
199
|
// Current results for each vote option.
|
|
200
|
-
signal input currentResults[
|
|
200
|
+
signal input currentResults[totalVoteOptions];
|
|
201
201
|
// Salt for the root of the current results.
|
|
202
202
|
signal input currentResultsRootSalt;
|
|
203
203
|
|
|
204
204
|
// New results for each vote option.
|
|
205
|
-
signal input newResults[
|
|
205
|
+
signal input newResults[totalVoteOptions];
|
|
206
206
|
// Salt for the root of the new results.
|
|
207
207
|
signal input newResultsRootSalt;
|
|
208
208
|
|
|
@@ -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.
|
|
@@ -251,28 +251,28 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
251
251
|
// computedIsZero.out is 0 if this is the first batch.
|
|
252
252
|
var computedIsZero = IsZero()(isFirstBatch);
|
|
253
253
|
|
|
254
|
-
//
|
|
255
|
-
//
|
|
256
|
-
signal
|
|
257
|
-
|
|
258
|
-
|
|
254
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
255
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
256
|
+
signal isFirstCommitment;
|
|
257
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
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
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -11,12 +11,14 @@ include "../VerifySignature.circom";
|
|
|
11
11
|
*/
|
|
12
12
|
template MessageValidatorNonQv() {
|
|
13
13
|
// Length of the packed command.
|
|
14
|
-
var
|
|
14
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
15
|
+
// Number of checks to be performed.
|
|
16
|
+
var TOTAL_CHECKS = 5;
|
|
15
17
|
|
|
16
18
|
// State index of the user.
|
|
17
19
|
signal input stateTreeIndex;
|
|
18
20
|
// Number of user sign-ups in the state tree.
|
|
19
|
-
signal input
|
|
21
|
+
signal input totalSignups;
|
|
20
22
|
// Vote option index.
|
|
21
23
|
signal input voteOptionIndex;
|
|
22
24
|
// Number of valid vote options for the poll.
|
|
@@ -24,15 +26,15 @@ template MessageValidatorNonQv() {
|
|
|
24
26
|
// Ballot nonce.
|
|
25
27
|
signal input originalNonce;
|
|
26
28
|
// Command nonce.
|
|
27
|
-
signal input
|
|
29
|
+
signal input commandNonce;
|
|
28
30
|
// Packed command.
|
|
29
|
-
signal input
|
|
31
|
+
signal input command[PACKED_COMMAND_LENGTH];
|
|
30
32
|
// Public key of the state leaf (user).
|
|
31
33
|
signal input publicKey[2];
|
|
32
|
-
//
|
|
33
|
-
signal input
|
|
34
|
-
//
|
|
35
|
-
signal input
|
|
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;
|
|
36
38
|
// State leaf current voice credit balance.
|
|
37
39
|
signal input currentVoiceCreditBalance;
|
|
38
40
|
// Current number of votes for specific option.
|
|
@@ -48,19 +50,19 @@ template MessageValidatorNonQv() {
|
|
|
48
50
|
signal output isVoteOptionIndexValid;
|
|
49
51
|
|
|
50
52
|
// Check (1) - The state leaf index must be valid.
|
|
51
|
-
// The check ensure that the stateTreeIndex <
|
|
53
|
+
// The check ensure that the stateTreeIndex < totalSignups as first validation.
|
|
52
54
|
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
53
55
|
// while 1 is for the first actual user.
|
|
54
|
-
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex,
|
|
56
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, totalSignups]);
|
|
55
57
|
|
|
56
58
|
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
57
59
|
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
58
60
|
|
|
59
61
|
// Check (3) - The nonce must be correct.
|
|
60
|
-
var computedIsNonceValid = IsEqual()([originalNonce + 1,
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
61
63
|
|
|
62
64
|
// Check (4) - The signature must be correct.
|
|
63
|
-
var computedIsSignatureValid = VerifySignature()(publicKey,
|
|
65
|
+
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
64
66
|
|
|
65
67
|
// Check (5) - There must be sufficient voice credits.
|
|
66
68
|
// The check ensure that currentVoiceCreditBalance + (currentVotesForOption) >= (voteWeight).
|
|
@@ -74,7 +76,7 @@ template MessageValidatorNonQv() {
|
|
|
74
76
|
// When all five checks are correct, then isValid = 1.
|
|
75
77
|
var computedIsUpdateValid = IsEqual()(
|
|
76
78
|
[
|
|
77
|
-
|
|
79
|
+
TOTAL_CHECKS,
|
|
78
80
|
computedIsSignatureValid +
|
|
79
81
|
computedAreVoiceCreditsSufficient +
|
|
80
82
|
computedIsNonceValid +
|