@maci-protocol/circuits 0.0.0-ci.01622be
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/CHANGELOG.md +673 -0
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/build/ts/compile.d.ts +10 -0
- package/build/ts/compile.d.ts.map +1 -0
- package/build/ts/compile.js +123 -0
- package/build/ts/compile.js.map +1 -0
- package/build/ts/generateZkeys.d.ts +9 -0
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/generateZkeys.js +67 -0
- package/build/ts/generateZkeys.js.map +1 -0
- package/build/ts/info.d.ts +2 -0
- package/build/ts/info.d.ts.map +1 -0
- package/build/ts/info.js +72 -0
- package/build/ts/info.js.map +1 -0
- package/build/ts/types.d.ts +104 -0
- package/build/ts/types.d.ts.map +1 -0
- package/build/ts/types.js +3 -0
- package/build/ts/types.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/circom/circuits.json +74 -0
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +447 -0
- package/circom/coordinator/non-qv/tallyVotes.circom +238 -0
- package/circom/coordinator/qv/processMessages.circom +452 -0
- package/circom/coordinator/qv/tallyVotes.circom +279 -0
- package/circom/utils/CalculateTotal.circom +24 -0
- package/circom/utils/EdDSAPoseidonVerifier.circom +91 -0
- package/circom/utils/MessageHasher.circom +57 -0
- package/circom/utils/MessageToCommand.circom +107 -0
- package/circom/utils/PoseidonHasher.circom +29 -0
- package/circom/utils/PrivateToPublicKey.circom +38 -0
- package/circom/utils/VerifySignature.circom +39 -0
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/MessageValidator.circom +91 -0
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/qv/MessageValidator.circom +97 -0
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
- package/circom/utils/trees/CheckRoot.circom +49 -0
- package/circom/utils/trees/LeafExists.circom +27 -0
- package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
- 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 +43 -0
- package/circom/voter/PollJoining.circom +54 -0
- package/circomkit.json +18 -0
- package/package.json +74 -0
|
@@ -0,0 +1,279 @@
|
|
|
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/MerklePathIndicesGenerator.circom";
|
|
10
|
+
include "../../utils/trees/LeafExists.circom";
|
|
11
|
+
include "../../utils/trees/QuinaryCheckRoot.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).
|
|
18
|
+
*/
|
|
19
|
+
template TallyVotes(
|
|
20
|
+
stateTreeDepth,
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
|
+
voteOptionTreeDepth
|
|
23
|
+
) {
|
|
24
|
+
// Ensure there's at least one level in the vote option tree.
|
|
25
|
+
assert(voteOptionTreeDepth > 0);
|
|
26
|
+
// Ensure the intermediate state tree has at least one level.
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
|
+
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
|
+
|
|
31
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
|
+
var TREE_ARITY = 5;
|
|
33
|
+
var BALLOT_TREE_ARITY = 2;
|
|
34
|
+
|
|
35
|
+
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
|
+
|
|
40
|
+
// Number of elements in each ballot.
|
|
41
|
+
var BALLOT_LENGTH = 2;
|
|
42
|
+
// Index for the nonce in the ballot array.
|
|
43
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
44
|
+
// Index for the voting option root in the ballot array.
|
|
45
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
|
+
// Difference in tree depths, used in path calculations.
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
|
+
|
|
49
|
+
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
|
+
signal input stateRoot;
|
|
51
|
+
// Root of the ballot Merkle tree, representing the submitted ballots.
|
|
52
|
+
signal input ballotRoot;
|
|
53
|
+
// Salt used in commitment to secure the ballot data.
|
|
54
|
+
signal input sbSalt;
|
|
55
|
+
// Commitment to the state and ballots.
|
|
56
|
+
signal input sbCommitment;
|
|
57
|
+
// Commitment to the current tally before this batch.
|
|
58
|
+
signal input currentTallyCommitment;
|
|
59
|
+
// Commitment to the new tally after processing this batch.
|
|
60
|
+
signal input newTallyCommitment;
|
|
61
|
+
// Start index of given batch
|
|
62
|
+
signal input index;
|
|
63
|
+
// Number of users that signup
|
|
64
|
+
signal input totalSignups;
|
|
65
|
+
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
|
+
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
|
+
signal input votes[batchSize][totalVoteOptions];
|
|
69
|
+
// Current results for each vote option.
|
|
70
|
+
signal input currentResults[totalVoteOptions];
|
|
71
|
+
// Salt for the root of the current results.
|
|
72
|
+
signal input currentResultsRootSalt;
|
|
73
|
+
// Total voice credits spent so far.
|
|
74
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
75
|
+
// Salt for the total spent voice credits.
|
|
76
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
77
|
+
// Spent voice credits per vote option.
|
|
78
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
79
|
+
// Salt for the root of spent credits per option.
|
|
80
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
81
|
+
// Salt for the root of the new results.
|
|
82
|
+
signal input newResultsRootSalt;
|
|
83
|
+
// Salt for the new spent credits per vote option root.
|
|
84
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
85
|
+
// Salt for the new total spent voice credits root.
|
|
86
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
87
|
+
|
|
88
|
+
// Verify sbCommitment.
|
|
89
|
+
var computedSbCommitment = PoseidonHasher(3)([stateRoot, ballotRoot, sbSalt]);
|
|
90
|
+
computedSbCommitment === sbCommitment;
|
|
91
|
+
|
|
92
|
+
// Validates that the index is within the valid range of sign-ups.
|
|
93
|
+
var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
|
|
94
|
+
totalSignupsValid === 1;
|
|
95
|
+
|
|
96
|
+
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
97
|
+
var computedBallotHashers[batchSize];
|
|
98
|
+
|
|
99
|
+
for (var i = 0; i < batchSize; i++) {
|
|
100
|
+
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
104
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
105
|
+
|
|
106
|
+
// Verifies each ballot's existence within the ballot tree.
|
|
107
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
108
|
+
computedBallotSubroot,
|
|
109
|
+
ballotPathElements,
|
|
110
|
+
computedBallotPathIndices,
|
|
111
|
+
ballotRoot
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
// Processes vote options, verifying each against its declared root.
|
|
115
|
+
var computedVoteTree[batchSize];
|
|
116
|
+
for (var i = 0; i < batchSize; i++) {
|
|
117
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
118
|
+
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
122
|
+
var computedIsFirstBatch = IsZero()(index);
|
|
123
|
+
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
124
|
+
|
|
125
|
+
// Tally the new results.
|
|
126
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
127
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
128
|
+
var numsRC[batchSize + 1];
|
|
129
|
+
numsRC[batchSize] = currentResults[i] * computedIsZero;
|
|
130
|
+
for (var j = 0; j < batchSize; j++) {
|
|
131
|
+
numsRC[j] = votes[j][i];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(numsRC);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Tally the new spent voice credit total.
|
|
138
|
+
var numsSVC[batchSize * totalVoteOptions + 1];
|
|
139
|
+
numsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
140
|
+
for (var i = 0; i < batchSize; i++) {
|
|
141
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
142
|
+
numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(numsSVC);
|
|
147
|
+
|
|
148
|
+
// Tally the spent voice credits per vote option.
|
|
149
|
+
var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
|
|
150
|
+
|
|
151
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
152
|
+
var computedTotalVoiceCreditSpent[batchSize + 1];
|
|
153
|
+
computedTotalVoiceCreditSpent[batchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
|
|
154
|
+
for (var j = 0; j < batchSize; j++) {
|
|
155
|
+
computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(batchSize + 1)(computedTotalVoiceCreditSpent);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
162
|
+
ResultCommitmentVerifier(voteOptionTreeDepth)(
|
|
163
|
+
computedIsFirstBatch,
|
|
164
|
+
currentTallyCommitment,
|
|
165
|
+
newTallyCommitment,
|
|
166
|
+
currentResults,
|
|
167
|
+
currentResultsRootSalt,
|
|
168
|
+
computedCalculateTotalResult,
|
|
169
|
+
newResultsRootSalt,
|
|
170
|
+
currentSpentVoiceCreditSubtotal,
|
|
171
|
+
currentSpentVoiceCreditSubtotalSalt,
|
|
172
|
+
computedNewSpentVoiceCreditSubtotal,
|
|
173
|
+
newSpentVoiceCreditSubtotalSalt,
|
|
174
|
+
currentPerVoteOptionSpentVoiceCredits,
|
|
175
|
+
currentPerVoteOptionSpentVoiceCreditsRootSalt,
|
|
176
|
+
computedNewPerVOSpentVoiceCredits,
|
|
177
|
+
newPerVoteOptionSpentVoiceCreditsRootSalt
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Performs verifications and computations related to current voting results.
|
|
183
|
+
* Also, computes and outputs a commitment to the new results.
|
|
184
|
+
* This template supports the Quadratic Voting (QV).
|
|
185
|
+
*/
|
|
186
|
+
template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
187
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
188
|
+
var TREE_ARITY = 5;
|
|
189
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
190
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
191
|
+
|
|
192
|
+
// Equal to 1 if this is the first batch, otherwise 0.
|
|
193
|
+
signal input isFirstBatch;
|
|
194
|
+
// Commitment to the current tally before this batch.
|
|
195
|
+
signal input currentTallyCommitment;
|
|
196
|
+
// Commitment to the new tally after processing this batch.
|
|
197
|
+
signal input newTallyCommitment;
|
|
198
|
+
|
|
199
|
+
// Current results for each vote option.
|
|
200
|
+
signal input currentResults[totalVoteOptions];
|
|
201
|
+
// Salt for the root of the current results.
|
|
202
|
+
signal input currentResultsRootSalt;
|
|
203
|
+
|
|
204
|
+
// New results for each vote option.
|
|
205
|
+
signal input newResults[totalVoteOptions];
|
|
206
|
+
// Salt for the root of the new results.
|
|
207
|
+
signal input newResultsRootSalt;
|
|
208
|
+
|
|
209
|
+
// Total voice credits spent so far.
|
|
210
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
211
|
+
// Salt for the total spent voice credits.
|
|
212
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
213
|
+
|
|
214
|
+
// Total new voice credits spent.
|
|
215
|
+
signal input newSpentVoiceCreditSubtotal;
|
|
216
|
+
// Salt for the new total spent voice credits.
|
|
217
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
218
|
+
|
|
219
|
+
// Spent voice credits per vote option.
|
|
220
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
221
|
+
// Salt for the root of spent credits per option.
|
|
222
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
223
|
+
|
|
224
|
+
// New spent voice credits per vote option.
|
|
225
|
+
signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
226
|
+
// Salt for the root of new spent credits per option.
|
|
227
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
228
|
+
|
|
229
|
+
// Compute the commitment to the current results.
|
|
230
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
231
|
+
|
|
232
|
+
// Verify currentResultsCommitmentHash.
|
|
233
|
+
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
234
|
+
|
|
235
|
+
// Compute the commitment to the current spent voice credits.
|
|
236
|
+
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
237
|
+
|
|
238
|
+
// Compute the root of the spent voice credits per vote option.
|
|
239
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
|
|
240
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
241
|
+
|
|
242
|
+
// Commit to the current tally.
|
|
243
|
+
var computedCurrentTallyCommitment = PoseidonHasher(3)([
|
|
244
|
+
computedCurrentResultsCommitment,
|
|
245
|
+
computedCurrentSpentVoiceCreditsCommitment,
|
|
246
|
+
computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
|
|
247
|
+
]);
|
|
248
|
+
|
|
249
|
+
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
250
|
+
// computedIsZero.out is 1 if this is not the first batch.
|
|
251
|
+
// computedIsZero.out is 0 if this is the first batch.
|
|
252
|
+
var computedIsZero = IsZero()(isFirstBatch);
|
|
253
|
+
|
|
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
|
+
|
|
260
|
+
// Compute the root of the new results.
|
|
261
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
262
|
+
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
263
|
+
|
|
264
|
+
// Compute the commitment to the new spent voice credits value.
|
|
265
|
+
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
266
|
+
|
|
267
|
+
// Compute the root of the spent voice credits per vote option.
|
|
268
|
+
var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
|
|
269
|
+
var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
270
|
+
|
|
271
|
+
// Commit to the new tally.
|
|
272
|
+
var computedNewTallyCommitment = PoseidonHasher(3)([
|
|
273
|
+
computedNewResultsCommitment,
|
|
274
|
+
computedNewSpentVoiceCreditsCommitment,
|
|
275
|
+
computedNewPerVoteOptionSpentVoiceCreditsCommitment
|
|
276
|
+
]);
|
|
277
|
+
|
|
278
|
+
computedNewTallyCommitment === newTallyCommitment;
|
|
279
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Computes the cumulative sum of an array of length input signals.
|
|
5
|
+
* It iterates through each input, aggregating the sum up to that point,
|
|
6
|
+
* and outputs the total sum of all inputs. This template is useful for
|
|
7
|
+
* operations requiring the total sum of multiple signals, ensuring the
|
|
8
|
+
* final output reflects the cumulative total of the inputs provided.
|
|
9
|
+
*/
|
|
10
|
+
template CalculateTotal(length) {
|
|
11
|
+
// Array of values.
|
|
12
|
+
signal input nums[length];
|
|
13
|
+
// Total sum.
|
|
14
|
+
signal output sum;
|
|
15
|
+
|
|
16
|
+
signal sums[length];
|
|
17
|
+
sums[0] <== nums[0];
|
|
18
|
+
|
|
19
|
+
for (var i = 1; i < length; i++) {
|
|
20
|
+
sums[i] <== sums[i - 1] + nums[i];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
sum <== sums[length - 1];
|
|
24
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib imports
|
|
4
|
+
include "./compconstant.circom";
|
|
5
|
+
include "./comparators.circom";
|
|
6
|
+
include "./pointbits.circom";
|
|
7
|
+
include "./bitify.circom";
|
|
8
|
+
include "./escalarmulany.circom";
|
|
9
|
+
include "./escalarmulfix.circom";
|
|
10
|
+
// local imports
|
|
11
|
+
include "./PoseidonHasher.circom";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Variant of the EdDSAPoseidonVerifier template from circomlib
|
|
15
|
+
* https://github.com/iden3/circomlib/blob/master/circuits/eddsa.circom
|
|
16
|
+
*/
|
|
17
|
+
template EdDSAPoseidonVerifier() {
|
|
18
|
+
// The prime subgroup order.
|
|
19
|
+
var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
|
|
20
|
+
|
|
21
|
+
// The base point of the BabyJubJub curve.
|
|
22
|
+
var BASE8[2] = [
|
|
23
|
+
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
24
|
+
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
// The x and y coordinates of the public key.
|
|
28
|
+
signal input publicKeyX;
|
|
29
|
+
signal input publicKeyY;
|
|
30
|
+
// Signature scalar.
|
|
31
|
+
signal input signatureScalar;
|
|
32
|
+
// The x and y coordinates of the signature point.
|
|
33
|
+
signal input signaturePointX;
|
|
34
|
+
signal input signaturePointY;
|
|
35
|
+
// Message hash.
|
|
36
|
+
signal input messageHash;
|
|
37
|
+
// Output signal for the validity of the signature.
|
|
38
|
+
signal output isValid;
|
|
39
|
+
|
|
40
|
+
// Ensure signatureScalar<Subgroup Order.
|
|
41
|
+
// convert the signature scalar signatureScalar into its binary representation.
|
|
42
|
+
var computedNum2Bits[254] = Num2Bits(254)(signatureScalar);
|
|
43
|
+
|
|
44
|
+
var computedCompConstantIn[254] = computedNum2Bits;
|
|
45
|
+
computedCompConstantIn[253] = 0;
|
|
46
|
+
|
|
47
|
+
// A component that ensures signatureScalar is within a valid range,
|
|
48
|
+
// comparing it against a constant representing the subgroup order.
|
|
49
|
+
var computedCompConstant = CompConstant(SUBGROUP_ORDER - 1)(computedCompConstantIn);
|
|
50
|
+
|
|
51
|
+
// Calculate the h = H(R, A, msg).
|
|
52
|
+
var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([
|
|
53
|
+
signaturePointX,
|
|
54
|
+
signaturePointY,
|
|
55
|
+
publicKeyX,
|
|
56
|
+
publicKeyY,
|
|
57
|
+
messageHash
|
|
58
|
+
]));
|
|
59
|
+
|
|
60
|
+
// These components perform point doubling operations on the public key
|
|
61
|
+
// to align it within the correct subgroup as part of the verification process.
|
|
62
|
+
var (computedDouble1XOut, computedDouble1YOut) = BabyDbl()(publicKeyX, publicKeyY);
|
|
63
|
+
var (computedDouble2XOut, computedDouble2YOut) = BabyDbl()(computedDouble1XOut, computedDouble1YOut);
|
|
64
|
+
var (computedDouble3XOut, computedDouble3YOut) = BabyDbl()(computedDouble2XOut, computedDouble2YOut);
|
|
65
|
+
|
|
66
|
+
// A component that performs scalar multiplication of the
|
|
67
|
+
// adjusted public key by the hash output, essential for the verification calculation.
|
|
68
|
+
var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDouble3XOut, computedDouble3YOut]);
|
|
69
|
+
|
|
70
|
+
// Compute the right side: right = R8 + right2.
|
|
71
|
+
var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(signaturePointX, signaturePointY, computedEscalarMulAny[0], computedEscalarMulAny[1]);
|
|
72
|
+
|
|
73
|
+
// Fixed-base scalar multiplication of a base point by signatureScalar.
|
|
74
|
+
var computedEscalarMulFix[2] = EscalarMulFix(254, BASE8)(computedNum2Bits);
|
|
75
|
+
|
|
76
|
+
// Components to check the equality of x and y coordinates
|
|
77
|
+
// between the computed and expected points of the signature.
|
|
78
|
+
var computedIsRightValid = IsEqual()([computedEscalarMulFix[0], computedAddRightXOut]);
|
|
79
|
+
var computedIsLeftValid = IsEqual()([computedEscalarMulFix[1], computedAddRightYOut]);
|
|
80
|
+
var computedIsLeftRightValid = IsEqual()([computedIsRightValid + computedIsLeftValid, 2]);
|
|
81
|
+
|
|
82
|
+
// Components to handle edge cases and ensure that all conditions
|
|
83
|
+
// for a valid signature are met, including the
|
|
84
|
+
// public key not being zero and other integrity checks.
|
|
85
|
+
var computedIsAxZero = IsZero()(publicKeyX);
|
|
86
|
+
var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
|
|
87
|
+
var computedIsCcZero = IsZero()(computedCompConstant);
|
|
88
|
+
var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero, 3]);
|
|
89
|
+
|
|
90
|
+
isValid <== computedIsValid;
|
|
91
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
include "./PoseidonHasher.circom";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hashes a MACI message and the public key used for message encryption.
|
|
7
|
+
* This template processes 10 message inputs and a 2-element public key
|
|
8
|
+
* combining them using the Poseidon hash function. The hashing process involves two stages:
|
|
9
|
+
* 1. hashing message parts data groups of five and,
|
|
10
|
+
* 2. hashing the grouped results alongside the first message input and
|
|
11
|
+
* the encryption public key to produce a final hash output.
|
|
12
|
+
*/
|
|
13
|
+
template MessageHasher() {
|
|
14
|
+
// Message parts
|
|
15
|
+
var MESSAGE_PARTS = 10;
|
|
16
|
+
var STATE_INDEX = 0;
|
|
17
|
+
var VOTE_OPTION_INDEX = 1;
|
|
18
|
+
var NEW_VOTE_WEIGHT = 2;
|
|
19
|
+
var NONCE = 3;
|
|
20
|
+
var POLL_ID = 4;
|
|
21
|
+
var SIGNATURE_POINT_X = 5;
|
|
22
|
+
var SIGNATURE_POINT_Y = 6;
|
|
23
|
+
var SIGNATURE_SCALAR = 7;
|
|
24
|
+
var ENCRYPTED_PUBLIC_KEY_X = 8;
|
|
25
|
+
var ENCRYPTED_PUBLIC_KEY_Y = 9;
|
|
26
|
+
|
|
27
|
+
// The MACI message is composed of 10 parts.
|
|
28
|
+
signal input data[MESSAGE_PARTS];
|
|
29
|
+
// the public key used to encrypt the message.
|
|
30
|
+
signal input encryptionPublicKey[2];
|
|
31
|
+
|
|
32
|
+
// we output an hash.
|
|
33
|
+
signal output hash;
|
|
34
|
+
|
|
35
|
+
var computedHasherPart1 = PoseidonHasher(5)([
|
|
36
|
+
data[STATE_INDEX],
|
|
37
|
+
data[VOTE_OPTION_INDEX],
|
|
38
|
+
data[NEW_VOTE_WEIGHT],
|
|
39
|
+
data[NONCE],
|
|
40
|
+
data[POLL_ID]
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
var computedHasherPart2 = PoseidonHasher(5)([
|
|
44
|
+
data[SIGNATURE_POINT_X],
|
|
45
|
+
data[SIGNATURE_POINT_Y],
|
|
46
|
+
data[SIGNATURE_SCALAR],
|
|
47
|
+
data[ENCRYPTED_PUBLIC_KEY_X],
|
|
48
|
+
data[ENCRYPTED_PUBLIC_KEY_Y]
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
hash <== PoseidonHasher(4)([
|
|
52
|
+
computedHasherPart1,
|
|
53
|
+
computedHasherPart2,
|
|
54
|
+
encryptionPublicKey[0],
|
|
55
|
+
encryptionPublicKey[1]
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./bitify.circom";
|
|
5
|
+
// zk-kit imports
|
|
6
|
+
include "./ecdh.circom";
|
|
7
|
+
include "./unpack-element.circom";
|
|
8
|
+
include "./poseidon-cipher.circom";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Converts a MACI message to a command by decrypting it.
|
|
12
|
+
* Processes encrypted MACI messages into structured MACI commands
|
|
13
|
+
* by decrypting using a shared key derived from ECDH. After decryption,
|
|
14
|
+
* unpacks and assigns decrypted values to specific command components.
|
|
15
|
+
*/
|
|
16
|
+
template MessageToCommand() {
|
|
17
|
+
var MESSAGE_LENGTH = 7;
|
|
18
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
19
|
+
var UNPACK_ELEMENT_LENGTH = 5;
|
|
20
|
+
var DECRYPTED_LENGTH = 9;
|
|
21
|
+
var MESSAGE_PARTS = 10;
|
|
22
|
+
|
|
23
|
+
// Element indices.
|
|
24
|
+
var ELEMENT_POLL_ID = 0;
|
|
25
|
+
var ELEMENT_NONCE = 1;
|
|
26
|
+
var ELEMENT_NEW_VOTE_WEIGHT = 2;
|
|
27
|
+
var ELEMENT_VOTE_OPTION_INDEX = 3;
|
|
28
|
+
var ELEMENT_STATE_INDEX = 4;
|
|
29
|
+
|
|
30
|
+
// Command indices.
|
|
31
|
+
var COMMAND_STATE_INDEX = 0;
|
|
32
|
+
var COMMAND_PUBLIC_KEY_X = 1;
|
|
33
|
+
var COMMAND_PUBLIC_KEY_Y = 2;
|
|
34
|
+
var COMMAND_SALT = 3;
|
|
35
|
+
|
|
36
|
+
// Decryptor indices.
|
|
37
|
+
var SIGNATURE_POINT_X = 4;
|
|
38
|
+
var SIGNATURE_POINT_Y = 5;
|
|
39
|
+
var SIGNATURE_SCALAR = 6;
|
|
40
|
+
|
|
41
|
+
// The message is an array of 10 parts.
|
|
42
|
+
signal input message[MESSAGE_PARTS];
|
|
43
|
+
// The encryption private key
|
|
44
|
+
signal input encryptionPrivateKey;
|
|
45
|
+
// The encryption public key
|
|
46
|
+
signal input encryptionPublicKey[2];
|
|
47
|
+
|
|
48
|
+
// Command parts.
|
|
49
|
+
signal output stateIndex;
|
|
50
|
+
// The new public key.
|
|
51
|
+
signal output newPublicKey[2];
|
|
52
|
+
// The vote option index.
|
|
53
|
+
signal output voteOptionIndex;
|
|
54
|
+
// The new vote weight.
|
|
55
|
+
signal output newVoteWeight;
|
|
56
|
+
// The nonce.
|
|
57
|
+
signal output nonce;
|
|
58
|
+
// The poll id.
|
|
59
|
+
signal output pollId;
|
|
60
|
+
// The salt.
|
|
61
|
+
signal output salt;
|
|
62
|
+
// The signature point.
|
|
63
|
+
signal output signaturePoint[2];
|
|
64
|
+
// The signature scalar.
|
|
65
|
+
signal output signatureScalar;
|
|
66
|
+
|
|
67
|
+
// Packed command.
|
|
68
|
+
signal output packedCommandOut[PACKED_COMMAND_LENGTH];
|
|
69
|
+
|
|
70
|
+
// Generate the shared key for decrypting the message.
|
|
71
|
+
var computedEcdh[2] = Ecdh()(encryptionPrivateKey, encryptionPublicKey);
|
|
72
|
+
|
|
73
|
+
// Decrypt the message using Poseidon decryption.
|
|
74
|
+
var computedDecryptor[DECRYPTED_LENGTH] = PoseidonDecryptWithoutCheck(MESSAGE_LENGTH)(
|
|
75
|
+
message,
|
|
76
|
+
0,
|
|
77
|
+
computedEcdh
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// Save the decrypted message into a packed command signal.
|
|
81
|
+
signal packedCommand[PACKED_COMMAND_LENGTH];
|
|
82
|
+
|
|
83
|
+
for (var i = 0; i < PACKED_COMMAND_LENGTH; i++) {
|
|
84
|
+
packedCommand[i] <== computedDecryptor[i];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
var computedUnpackElement[UNPACK_ELEMENT_LENGTH] = UnpackElement(UNPACK_ELEMENT_LENGTH)(
|
|
88
|
+
packedCommand[COMMAND_STATE_INDEX]
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Everything below were packed into the first element.
|
|
92
|
+
pollId <== computedUnpackElement[ELEMENT_POLL_ID];
|
|
93
|
+
nonce <== computedUnpackElement[ELEMENT_NONCE];
|
|
94
|
+
newVoteWeight <== computedUnpackElement[ELEMENT_NEW_VOTE_WEIGHT];
|
|
95
|
+
voteOptionIndex <== computedUnpackElement[ELEMENT_VOTE_OPTION_INDEX];
|
|
96
|
+
stateIndex <== computedUnpackElement[ELEMENT_STATE_INDEX];
|
|
97
|
+
|
|
98
|
+
newPublicKey[0] <== packedCommand[COMMAND_PUBLIC_KEY_X];
|
|
99
|
+
newPublicKey[1] <== packedCommand[COMMAND_PUBLIC_KEY_Y];
|
|
100
|
+
salt <== packedCommand[COMMAND_SALT];
|
|
101
|
+
|
|
102
|
+
signaturePoint[0] <== computedDecryptor[SIGNATURE_POINT_X];
|
|
103
|
+
signaturePoint[1] <== computedDecryptor[SIGNATURE_POINT_Y];
|
|
104
|
+
signatureScalar <== computedDecryptor[SIGNATURE_SCALAR];
|
|
105
|
+
|
|
106
|
+
packedCommandOut <== packedCommand;
|
|
107
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./poseidon-cipher.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Computes the Poseidon hash for an array of n inputs, including a default initial state
|
|
8
|
+
* of zero not counted in n. First, extends the inputs by prepending a zero, creating an array [0, inputs].
|
|
9
|
+
* Then, the Poseidon hash of the extended inputs is calculated, with the first element of the
|
|
10
|
+
* result assigned as the output.
|
|
11
|
+
*/
|
|
12
|
+
template PoseidonHasher(n) {
|
|
13
|
+
signal input inputs[n];
|
|
14
|
+
signal output out;
|
|
15
|
+
|
|
16
|
+
// [0, inputs].
|
|
17
|
+
var computedExtendedInputs[n + 1];
|
|
18
|
+
computedExtendedInputs[0] = 0;
|
|
19
|
+
|
|
20
|
+
for (var i = 0; i < n; i++) {
|
|
21
|
+
computedExtendedInputs[i + 1] = inputs[i];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Compute the Poseidon hash of the extended inputs.
|
|
25
|
+
var computedPoseidonPerm[n + 1];
|
|
26
|
+
computedPoseidonPerm = PoseidonPerm(n + 1)(computedExtendedInputs);
|
|
27
|
+
|
|
28
|
+
out <== computedPoseidonPerm[0];
|
|
29
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib imports
|
|
4
|
+
include "./bitify.circom";
|
|
5
|
+
include "./comparators.circom";
|
|
6
|
+
include "./escalarmulfix.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a private key to a public key on the BabyJubJub curve.
|
|
10
|
+
* The input private key needs to be hashed and then pruned before.
|
|
11
|
+
*/
|
|
12
|
+
template PrivateToPublicKey() {
|
|
13
|
+
// The base point of the BabyJubJub curve.
|
|
14
|
+
var BASE8[2] = [
|
|
15
|
+
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
16
|
+
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
// The prime subgroup order.
|
|
20
|
+
var SUBGROUP_ORDER = 2736030358979909402780800718157159386076813972158567259200215660948447373041;
|
|
21
|
+
|
|
22
|
+
// The private key
|
|
23
|
+
signal input privateKey;
|
|
24
|
+
// The public key
|
|
25
|
+
signal output publicKey[2];
|
|
26
|
+
|
|
27
|
+
// Check if private key is in the prime subgroup order
|
|
28
|
+
var isLessThan = LessThan(251)([privateKey, SUBGROUP_ORDER]);
|
|
29
|
+
isLessThan === 1;
|
|
30
|
+
|
|
31
|
+
// Convert the private key to bits.
|
|
32
|
+
var computedPrivateBits[253] = Num2Bits(253)(privateKey);
|
|
33
|
+
|
|
34
|
+
// Perform scalar multiplication with the basepoint.
|
|
35
|
+
var computedPublicKey[2] = EscalarMulFix(253, BASE8)(computedPrivateBits);
|
|
36
|
+
|
|
37
|
+
publicKey <== computedPublicKey;
|
|
38
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "./EdDSAPoseidonVerifier.circom";
|
|
5
|
+
include "./PoseidonHasher.circom";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
|
|
9
|
+
*/
|
|
10
|
+
template VerifySignature() {
|
|
11
|
+
// Number of elements in the hash preimage.
|
|
12
|
+
var PRE_IMAGE_LENGTH = 4;
|
|
13
|
+
|
|
14
|
+
// Public key of the signer, consisting of two coordinates [x, y].
|
|
15
|
+
signal input publicKey[2];
|
|
16
|
+
// signaturePoint point from the signature, consisting of two coordinates [x, y].
|
|
17
|
+
signal input signaturePoint[2];
|
|
18
|
+
// Scalar component of the signature.
|
|
19
|
+
signal input signatureScalar;
|
|
20
|
+
// The preimage data that was hashed, an array of four elements.
|
|
21
|
+
signal input preimage[PRE_IMAGE_LENGTH];
|
|
22
|
+
// The validity of the signature.
|
|
23
|
+
signal output isValid;
|
|
24
|
+
|
|
25
|
+
// Hash the preimage using the Poseidon hashing function configured for four inputs.
|
|
26
|
+
var computedPreimage = PoseidonHasher(4)(preimage);
|
|
27
|
+
|
|
28
|
+
// Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
|
|
29
|
+
var computedIsValid = EdDSAPoseidonVerifier()(
|
|
30
|
+
publicKey[0],
|
|
31
|
+
publicKey[1],
|
|
32
|
+
signatureScalar,
|
|
33
|
+
signaturePoint[0],
|
|
34
|
+
signaturePoint[1],
|
|
35
|
+
computedPreimage
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
isValid <== computedIsValid;
|
|
39
|
+
}
|