@maci-protocol/circuits 0.0.0-ci.2df0337 → 0.0.0-ci.2ea88a0
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/README.md +2 -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 +9 -9
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +32 -16
- package/circom/coordinator/full/MessageProcessor.circom +245 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
- package/circom/coordinator/non-qv/MessageProcessor.circom +244 -0
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +197 -0
- package/circom/coordinator/non-qv/VoteTally.circom +161 -0
- package/circom/coordinator/qv/MessageProcessor.circom +242 -0
- package/circom/coordinator/qv/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/qv/VoteTally.circom +179 -0
- package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/EdDSAPoseidonVerifier.circom +8 -3
- package/circom/utils/IsOnCurve.circom +40 -0
- package/circom/utils/PrivateToPublicKey.circom +3 -3
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +127 -0
- package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +39 -34
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +39 -34
- package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/{MerkleGeneratePathIndices.circom → QuinaryGeneratePathIndices.circom} +18 -18
- package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
- package/circom/utils/trees/QuinarySelector.circom +42 -0
- package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
- package/circom/utils/trees/Splicer.circom +76 -0
- package/circom/voter/PollJoined.circom +5 -5
- package/circom/voter/PollJoining.circom +3 -3
- package/package.json +20 -17
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
- package/circom/coordinator/non-qv/processMessages.circom +0 -449
- package/circom/coordinator/non-qv/tallyVotes.circom +0 -235
- package/circom/coordinator/qv/processMessages.circom +0 -451
- package/circom/coordinator/qv/tallyVotes.circom +0 -279
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
// zk-kit import
|
|
6
|
+
include "./unpack-element.circom";
|
|
7
|
+
// local imports
|
|
8
|
+
include "../../utils/trees/CheckRoot.circom";
|
|
9
|
+
include "../../utils/trees/LeafExists.circom";
|
|
10
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
11
|
+
include "../../utils/qv/ResultCommitmentVerifier.circom";
|
|
12
|
+
include "../../utils/CalculateTotal.circom";
|
|
13
|
+
include "../../utils/PoseidonHasher.circom";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Processes batches of votes and verifies their validity in a Merkle tree structure.
|
|
17
|
+
* This template supports Quadratic Voting (QV) with individual vote counting.
|
|
18
|
+
* Note: this circuit is not using right now, this is a part of individual vote counts functionality.
|
|
19
|
+
* Not finished yet, don't use it in production. It is kept here for future use.
|
|
20
|
+
*/
|
|
21
|
+
template VoteTallyWithIndividualCountsQv(
|
|
22
|
+
stateTreeDepth,
|
|
23
|
+
tallyProcessingStateTreeDepth,
|
|
24
|
+
voteOptionTreeDepth
|
|
25
|
+
) {
|
|
26
|
+
// Ensure there's at least one level in the vote option tree.
|
|
27
|
+
assert(voteOptionTreeDepth > 0);
|
|
28
|
+
// Ensure the intermediate state tree has at least one level.
|
|
29
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
30
|
+
// The intermediate state tree must be smaller than the full state tree.
|
|
31
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
32
|
+
|
|
33
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
34
|
+
var TREE_ARITY = 5;
|
|
35
|
+
var BALLOT_TREE_ARITY = 2;
|
|
36
|
+
var VOTE_COUNTS_TREE_ARITY = 2;
|
|
37
|
+
|
|
38
|
+
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
39
|
+
var ballotBatchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
40
|
+
var voteCountsBatchSize = VOTE_COUNTS_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
41
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
42
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
43
|
+
|
|
44
|
+
// Number of elements in each ballot.
|
|
45
|
+
var BALLOT_LENGTH = 2;
|
|
46
|
+
// Index for the nonce in the ballot array.
|
|
47
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
48
|
+
// Index for the voting option root in the ballot array.
|
|
49
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
50
|
+
// Difference in tree depths, used in path calculations.
|
|
51
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
52
|
+
// Number of elements in each vote count leaf.
|
|
53
|
+
var VOTE_COUNTS_LENGTH = 2;
|
|
54
|
+
// Index for the voting option index.
|
|
55
|
+
var VOTE_COUNTS_NONCE_INDEX = 0;
|
|
56
|
+
// Index for root of the vote count per option.
|
|
57
|
+
var VOTE_COUNTS_ROOT_INDEX = 1;
|
|
58
|
+
|
|
59
|
+
// Root of the state Merkle tree, representing the overall state before voting.
|
|
60
|
+
signal input stateRoot;
|
|
61
|
+
// Root of the ballot Merkle tree, representing the submitted ballots.
|
|
62
|
+
signal input ballotRoot;
|
|
63
|
+
// Root of the vote counts Merkle tree, representing the counts of votes for each option.
|
|
64
|
+
signal input voteCountsRoot;
|
|
65
|
+
// Salt used in commitment to secure the ballot data.
|
|
66
|
+
signal input sbSalt;
|
|
67
|
+
// Commitment to the state and ballots.
|
|
68
|
+
signal input sbCommitment;
|
|
69
|
+
// Commitment to the current tally before this batch.
|
|
70
|
+
signal input currentTallyCommitment;
|
|
71
|
+
// Commitment to the new tally after processing this batch.
|
|
72
|
+
signal input newTallyCommitment;
|
|
73
|
+
// Start index of given batch
|
|
74
|
+
signal input index;
|
|
75
|
+
// Number of users that signup
|
|
76
|
+
signal input totalSignups;
|
|
77
|
+
// Ballots and their corresponding path elements for verification in the tree.
|
|
78
|
+
signal input ballots[ballotBatchSize][BALLOT_LENGTH];
|
|
79
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
80
|
+
signal input votes[ballotBatchSize][totalVoteOptions];
|
|
81
|
+
// Individual vote count tree and their corresponding path elements for verification in the tree.
|
|
82
|
+
signal input voteCounts[voteCountsBatchSize][VOTE_COUNTS_LENGTH];
|
|
83
|
+
signal input voteCountsPathElements[STATE_TREE_DEPTH_DIFFERENCE][VOTE_COUNTS_TREE_ARITY - 1];
|
|
84
|
+
signal input voteCountsData[voteCountsBatchSize][totalVoteOptions];
|
|
85
|
+
// Current results for each vote option.
|
|
86
|
+
signal input currentResults[totalVoteOptions];
|
|
87
|
+
// Salt for the root of the current results.
|
|
88
|
+
signal input currentResultsRootSalt;
|
|
89
|
+
// Total voice credits spent so far.
|
|
90
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
91
|
+
// Salt for the total spent voice credits.
|
|
92
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
93
|
+
// Spent voice credits per vote option.
|
|
94
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
95
|
+
// Salt for the root of spent credits per option.
|
|
96
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
97
|
+
// Salt for the root of the new results.
|
|
98
|
+
signal input newResultsRootSalt;
|
|
99
|
+
// Salt for the new spent credits per vote option root.
|
|
100
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
101
|
+
// Salt for the new total spent voice credits root.
|
|
102
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
103
|
+
|
|
104
|
+
// Verify sbCommitment.
|
|
105
|
+
var computedSbCommitment = PoseidonHasher(3)([stateRoot, ballotRoot, sbSalt]);
|
|
106
|
+
computedSbCommitment === sbCommitment;
|
|
107
|
+
|
|
108
|
+
// Validates that the index is within the valid range of sign-ups.
|
|
109
|
+
var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
|
|
110
|
+
totalSignupsValid === 1;
|
|
111
|
+
|
|
112
|
+
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
113
|
+
var computedBallotHashers[ballotBatchSize];
|
|
114
|
+
var computedVoteCountsHashers[voteCountsBatchSize];
|
|
115
|
+
|
|
116
|
+
for (var i = 0; i < ballotBatchSize; i++) {
|
|
117
|
+
computedBallotHashers[i] = PoseidonHasher(2)([
|
|
118
|
+
ballots[i][BALLOT_NONCE_INDEX],
|
|
119
|
+
ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
120
|
+
]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (var i = 0; i < voteCountsBatchSize; i++) {
|
|
124
|
+
computedVoteCountsHashers[i] = PoseidonHasher(2)([
|
|
125
|
+
voteCounts[i][VOTE_COUNTS_NONCE_INDEX],
|
|
126
|
+
voteCounts[i][VOTE_COUNTS_ROOT_INDEX]
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
131
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / ballotBatchSize);
|
|
132
|
+
|
|
133
|
+
var computedVoteCountsSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedVoteCountsHashers);
|
|
134
|
+
var computedVoteCountsPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / voteCountsBatchSize);
|
|
135
|
+
|
|
136
|
+
// Verifies each ballot's existence within the ballot tree.
|
|
137
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
138
|
+
computedBallotSubroot,
|
|
139
|
+
ballotPathElements,
|
|
140
|
+
computedBallotPathIndices,
|
|
141
|
+
ballotRoot
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// Verifies each vote count's existence within the vote count tree.
|
|
145
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
146
|
+
computedVoteCountsSubroot,
|
|
147
|
+
voteCountsPathElements,
|
|
148
|
+
computedVoteCountsPathIndices,
|
|
149
|
+
voteCountsRoot
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
// Processes vote options, verifying each against its declared root.
|
|
153
|
+
var computedVoteTree[ballotBatchSize];
|
|
154
|
+
|
|
155
|
+
for (var i = 0; i < ballotBatchSize; i++) {
|
|
156
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
157
|
+
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Processes vote counts, verifying each against its declared root.
|
|
161
|
+
var computedVoteCountsTree[voteCountsBatchSize];
|
|
162
|
+
|
|
163
|
+
for (var i = 0; i < voteCountsBatchSize; i++) {
|
|
164
|
+
computedVoteCountsTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(voteCountsData[i]);
|
|
165
|
+
computedVoteCountsTree[i] === voteCounts[i][VOTE_COUNTS_ROOT_INDEX];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
169
|
+
var computedIsFirstBatch = IsZero()(index);
|
|
170
|
+
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
171
|
+
|
|
172
|
+
// Tally the new results.
|
|
173
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
174
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
175
|
+
var numsRC[ballotBatchSize + 1];
|
|
176
|
+
numsRC[ballotBatchSize] = currentResults[i] * computedIsZero;
|
|
177
|
+
for (var j = 0; j < ballotBatchSize; j++) {
|
|
178
|
+
numsRC[j] = votes[j][i];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
computedCalculateTotalResult[i] = CalculateTotal(ballotBatchSize + 1)(numsRC);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Tally the new spent voice credit total.
|
|
185
|
+
var numsSVC[ballotBatchSize * totalVoteOptions + 1];
|
|
186
|
+
numsSVC[ballotBatchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
187
|
+
for (var i = 0; i < ballotBatchSize; i++) {
|
|
188
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
189
|
+
numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(ballotBatchSize * totalVoteOptions + 1)(numsSVC);
|
|
194
|
+
|
|
195
|
+
// Tally the spent voice credits per vote option.
|
|
196
|
+
var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
|
|
197
|
+
|
|
198
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
199
|
+
var computedTotalVoiceCreditSpent[ballotBatchSize + 1];
|
|
200
|
+
computedTotalVoiceCreditSpent[ballotBatchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
|
|
201
|
+
for (var j = 0; j < ballotBatchSize; j++) {
|
|
202
|
+
computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(ballotBatchSize + 1)(computedTotalVoiceCreditSpent);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
209
|
+
ResultCommitmentVerifierQv(voteOptionTreeDepth)(
|
|
210
|
+
computedIsFirstBatch,
|
|
211
|
+
currentTallyCommitment,
|
|
212
|
+
newTallyCommitment,
|
|
213
|
+
currentResults,
|
|
214
|
+
currentResultsRootSalt,
|
|
215
|
+
computedCalculateTotalResult,
|
|
216
|
+
newResultsRootSalt,
|
|
217
|
+
currentSpentVoiceCreditSubtotal,
|
|
218
|
+
currentSpentVoiceCreditSubtotalSalt,
|
|
219
|
+
computedNewSpentVoiceCreditSubtotal,
|
|
220
|
+
newSpentVoiceCreditSubtotalSalt,
|
|
221
|
+
currentPerVoteOptionSpentVoiceCredits,
|
|
222
|
+
currentPerVoteOptionSpentVoiceCreditsRootSalt,
|
|
223
|
+
computedNewPerVOSpentVoiceCredits,
|
|
224
|
+
newPerVoteOptionSpentVoiceCreditsRootSalt
|
|
225
|
+
);
|
|
226
|
+
}
|
|
@@ -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
|
}
|
|
@@ -9,6 +9,7 @@ include "./escalarmulany.circom";
|
|
|
9
9
|
include "./escalarmulfix.circom";
|
|
10
10
|
// local imports
|
|
11
11
|
include "./PoseidonHasher.circom";
|
|
12
|
+
include "./IsOnCurve.circom";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Variant of the EdDSAPoseidonVerifier template from circomlib
|
|
@@ -37,9 +38,13 @@ template EdDSAPoseidonVerifier() {
|
|
|
37
38
|
// Output signal for the validity of the signature.
|
|
38
39
|
signal output isValid;
|
|
39
40
|
|
|
41
|
+
// Verify the public key and signature point are on the BabyJubJub curve.
|
|
42
|
+
var computedIsPkOnCurve = IsOnCurve()(publicKeyX, publicKeyY);
|
|
43
|
+
var computedIsSpOnCurve = IsOnCurve()(signaturePointX, signaturePointY);
|
|
44
|
+
|
|
40
45
|
// Ensure signatureScalar<Subgroup Order.
|
|
41
46
|
// convert the signature scalar signatureScalar into its binary representation.
|
|
42
|
-
var computedNum2Bits[254] =
|
|
47
|
+
var computedNum2Bits[254] = Num2Bits_strict()(signatureScalar);
|
|
43
48
|
|
|
44
49
|
var computedCompConstantIn[254] = computedNum2Bits;
|
|
45
50
|
computedCompConstantIn[253] = 0;
|
|
@@ -82,10 +87,10 @@ template EdDSAPoseidonVerifier() {
|
|
|
82
87
|
// Components to handle edge cases and ensure that all conditions
|
|
83
88
|
// for a valid signature are met, including the
|
|
84
89
|
// public key not being zero and other integrity checks.
|
|
85
|
-
var computedIsAxZero = IsZero()(
|
|
90
|
+
var computedIsAxZero = IsZero()(computedDouble3XOut);
|
|
86
91
|
var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
|
|
87
92
|
var computedIsCcZero = IsZero()(computedCompConstant);
|
|
88
|
-
var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero,
|
|
93
|
+
var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero + computedIsPkOnCurve + computedIsSpOnCurve, 5]);
|
|
89
94
|
|
|
90
95
|
isValid <== computedIsValid;
|
|
91
96
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns 0 or 1 depending on if the point is on the BabyJubJub curve or not. The point is on the
|
|
8
|
+
* BabyJubJub curve if the following equation is true: a*x^2 + y^2 == 1 + d*x^2*y^2
|
|
9
|
+
* This template is identical to the BabyCheck template from circomlib, but it returns 0 or 1
|
|
10
|
+
* instead of having a hard constraint.
|
|
11
|
+
* Based on: https://github.com/iden3/circomlib/blob/master/circuits/babyjub.circom
|
|
12
|
+
*/
|
|
13
|
+
template IsOnCurve() {
|
|
14
|
+
// x coordinate of the point on the BabyJubJub curve.
|
|
15
|
+
signal input x;
|
|
16
|
+
// y coordinate of the point on the BabyJubJub curve.
|
|
17
|
+
signal input y;
|
|
18
|
+
// True when the point (x, y) satisfies the BabyJubJub curve equation.
|
|
19
|
+
signal output isValid;
|
|
20
|
+
|
|
21
|
+
// x^2 and y^2 intermediate values.
|
|
22
|
+
signal x2;
|
|
23
|
+
signal y2;
|
|
24
|
+
// x^2 * y^2 intermediate value.
|
|
25
|
+
signal x2y2;
|
|
26
|
+
|
|
27
|
+
// BabyJubJub curve parameters.
|
|
28
|
+
var a = 168700;
|
|
29
|
+
var d = 168696;
|
|
30
|
+
|
|
31
|
+
// Compute x^2 and y^2.
|
|
32
|
+
x2 <== x * x;
|
|
33
|
+
y2 <== y * y;
|
|
34
|
+
|
|
35
|
+
// Compute x^2 * y^2.
|
|
36
|
+
x2y2 <== x2 * y2;
|
|
37
|
+
|
|
38
|
+
// Check if a*x^2 + y^2 == 1 + d*x^2*y^2.
|
|
39
|
+
isValid <== IsEqual()([a * x2 + y2, 1 + d * x2y2]);
|
|
40
|
+
}
|
|
@@ -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,127 @@
|
|
|
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
|
+
|
|
123
|
+
// Constrain commandPollId and commandSalt using dummy squares.
|
|
124
|
+
// This binds the proof to a specific poll and salt.
|
|
125
|
+
signal commandPollIdSquare <== commandPollId * commandPollId;
|
|
126
|
+
signal commandSaltSquared <== commandSalt * commandSalt;
|
|
127
|
+
}
|
|
@@ -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 +
|