@maci-protocol/circuits 0.0.0-ci.4b8eeec → 0.0.0-ci.4bfa285
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/README.md +2 -2
- package/build/ts/types.d.ts +4 -4
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +12 -12
- package/circom/coordinator/full/MessageProcessor.circom +9 -9
- package/circom/coordinator/full/SingleMessageProcessor.circom +3 -4
- package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +12 -207
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +199 -0
- package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +16 -93
- package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +13 -215
- package/circom/coordinator/qv/SingleMessageProcessor.circom +207 -0
- package/circom/coordinator/qv/{tallyVotes.circom → VoteTally.circom} +12 -112
- package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
- package/circom/voter/PollJoined.circom +3 -3
- package/circom/voter/PollJoining.circom +3 -3
- package/package.json +14 -14
- package/circom/utils/trees/MerklePathIndicesGenerator.circom +0 -44
|
@@ -5,8 +5,8 @@ include "./comparators.circom";
|
|
|
5
5
|
// zk-kit import
|
|
6
6
|
include "./unpack-element.circom";
|
|
7
7
|
// local imports
|
|
8
|
+
include "../../utils/non-qv/ResultCommitmentVerifier.circom";
|
|
8
9
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
|
-
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
10
10
|
include "../../utils/trees/LeafExists.circom";
|
|
11
11
|
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
12
12
|
include "../../utils/CalculateTotal.circom";
|
|
@@ -16,24 +16,24 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
16
16
|
* Processes batches of votes and verifies their validity in a Merkle tree structure.
|
|
17
17
|
* This template does not support Quadratic Voting (QV).
|
|
18
18
|
*/
|
|
19
|
-
template
|
|
19
|
+
template VoteTallyNonQv(
|
|
20
20
|
stateTreeDepth,
|
|
21
|
-
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
22
|
voteOptionTreeDepth
|
|
23
23
|
) {
|
|
24
24
|
// Ensure there's at least one level in the vote option tree.
|
|
25
25
|
assert(voteOptionTreeDepth > 0);
|
|
26
26
|
// Ensure the intermediate state tree has at least one level.
|
|
27
|
-
assert(
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
28
|
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
-
assert(
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
30
|
|
|
31
31
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
32
|
var TREE_ARITY = 5;
|
|
33
33
|
var BALLOT_TREE_ARITY = 2;
|
|
34
34
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
-
var batchSize = BALLOT_TREE_ARITY **
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
37
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
38
|
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
@@ -44,7 +44,7 @@ template TallyVotesNonQv(
|
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
45
|
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
46
|
// Difference in tree depths, used in path calculations.
|
|
47
|
-
var
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -64,7 +64,7 @@ template TallyVotesNonQv(
|
|
|
64
64
|
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
68
|
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
70
|
signal input currentResults[totalVoteOptions];
|
|
@@ -95,11 +95,11 @@ template TallyVotesNonQv(
|
|
|
95
95
|
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
var computedBallotSubroot = CheckRoot(
|
|
99
|
-
var computedBallotPathIndices[
|
|
98
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
99
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
100
100
|
|
|
101
101
|
// Verifies each ballot's existence within the ballot tree.
|
|
102
|
-
LeafExists(
|
|
102
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
103
103
|
computedBallotSubroot,
|
|
104
104
|
ballotPathElements,
|
|
105
105
|
computedBallotPathIndices,
|
|
@@ -122,13 +122,14 @@ template TallyVotesNonQv(
|
|
|
122
122
|
var computedCalculateTotalResult[totalVoteOptions];
|
|
123
123
|
|
|
124
124
|
for (var i = 0; i < totalVoteOptions; i++) {
|
|
125
|
-
var
|
|
126
|
-
|
|
125
|
+
var computedVotes[batchSize + 1];
|
|
126
|
+
computedVotes[batchSize] = currentResults[i] * computedIsZero;
|
|
127
|
+
|
|
127
128
|
for (var j = 0; j < batchSize; j++) {
|
|
128
|
-
|
|
129
|
+
computedVotes[j] = votes[j][i];
|
|
129
130
|
}
|
|
130
131
|
|
|
131
|
-
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(
|
|
132
|
+
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
// Tally the new spent voice credit total.
|
|
@@ -158,81 +159,3 @@ template TallyVotesNonQv(
|
|
|
158
159
|
newSpentVoiceCreditSubtotalSalt
|
|
159
160
|
);
|
|
160
161
|
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Performs verifications and computations related to current voting results.
|
|
164
|
-
* Also, computes and outputs a commitment to the new results.
|
|
165
|
-
* This template does not support Quadratic Voting (QV).
|
|
166
|
-
*/
|
|
167
|
-
template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
|
|
168
|
-
// Number of children per node in the tree, defining the tree's branching factor.
|
|
169
|
-
var TREE_ARITY = 5;
|
|
170
|
-
// Number of voting options available, determined by the depth of the vote option tree.
|
|
171
|
-
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
172
|
-
|
|
173
|
-
// Equal to 1 if this is the first batch, otherwise 0.
|
|
174
|
-
signal input isFirstBatch;
|
|
175
|
-
// Commitment to the current tally before this batch.
|
|
176
|
-
signal input currentTallyCommitment;
|
|
177
|
-
// Commitment to the new tally after processing this batch.
|
|
178
|
-
signal input newTallyCommitment;
|
|
179
|
-
|
|
180
|
-
// Current results for each vote option.
|
|
181
|
-
signal input currentResults[totalVoteOptions];
|
|
182
|
-
// Salt for the root of the current results.
|
|
183
|
-
signal input currentResultsRootSalt;
|
|
184
|
-
|
|
185
|
-
// New results for each vote option.
|
|
186
|
-
signal input newResults[totalVoteOptions];
|
|
187
|
-
// Salt for the root of the new results.
|
|
188
|
-
signal input newResultsRootSalt;
|
|
189
|
-
|
|
190
|
-
// Total voice credits spent so far.
|
|
191
|
-
signal input currentSpentVoiceCreditSubtotal;
|
|
192
|
-
// Salt for the total spent voice credits.
|
|
193
|
-
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
194
|
-
|
|
195
|
-
// Total new voice credits spent.
|
|
196
|
-
signal input newSpentVoiceCreditSubtotal;
|
|
197
|
-
// Salt for the new total spent voice credits.
|
|
198
|
-
signal input newSpentVoiceCreditSubtotalSalt;
|
|
199
|
-
|
|
200
|
-
// Compute the commitment to the current results.
|
|
201
|
-
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
202
|
-
|
|
203
|
-
// Verify currentResultsCommitmentHash.
|
|
204
|
-
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
205
|
-
|
|
206
|
-
// Compute the commitment to the current spent voice credits.
|
|
207
|
-
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
208
|
-
|
|
209
|
-
// Commit to the current tally
|
|
210
|
-
var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
|
|
211
|
-
|
|
212
|
-
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
213
|
-
// computedIsZero.out is 1 if this is not the first batch.
|
|
214
|
-
// computedIsZero.out is 0 if this is the first batch.
|
|
215
|
-
var computedIsZero = IsZero()(isFirstBatch);
|
|
216
|
-
|
|
217
|
-
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
218
|
-
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
219
|
-
signal isFirstCommitment;
|
|
220
|
-
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
221
|
-
isFirstCommitment === currentTallyCommitment;
|
|
222
|
-
|
|
223
|
-
// Compute the root of the new results.
|
|
224
|
-
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
225
|
-
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
226
|
-
|
|
227
|
-
// Compute the commitment to the new spent voice credits value.
|
|
228
|
-
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
229
|
-
|
|
230
|
-
// Commit to the new tally.
|
|
231
|
-
var computedNewTallyCommitment = PoseidonHasher(2)([
|
|
232
|
-
computedNewResultsCommitment,
|
|
233
|
-
computedNewSpentVoiceCreditsCommitment
|
|
234
|
-
]);
|
|
235
|
-
|
|
236
|
-
computedNewTallyCommitment === newTallyCommitment;
|
|
237
|
-
}
|
|
238
|
-
|
|
@@ -9,20 +9,14 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
9
9
|
include "../../utils/MessageHasher.circom";
|
|
10
10
|
include "../../utils/MessageToCommand.circom";
|
|
11
11
|
include "../../utils/PrivateToPublicKey.circom";
|
|
12
|
-
include "
|
|
13
|
-
|
|
14
|
-
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
15
|
-
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
16
|
-
include "../../utils/trees/LeafExists.circom";
|
|
17
|
-
include "../../utils/trees/CheckRoot.circom";
|
|
18
|
-
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
19
|
-
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
12
|
+
include "./SingleMessageProcessor.circom";
|
|
13
|
+
|
|
20
14
|
|
|
21
15
|
/**
|
|
22
16
|
* Proves the correctness of processing a batch of MACI messages.
|
|
23
17
|
* This template supports the Quadratic Voting (QV).
|
|
24
18
|
*/
|
|
25
|
-
template
|
|
19
|
+
template MessageProcessorQv(
|
|
26
20
|
stateTreeDepth,
|
|
27
21
|
batchSize,
|
|
28
22
|
voteOptionTreeDepth
|
|
@@ -206,34 +200,34 @@ template ProcessMessages(
|
|
|
206
200
|
// Start from batchSize and decrement for process in reverse order.
|
|
207
201
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
208
202
|
// Process as vote type message.
|
|
209
|
-
var
|
|
210
|
-
var
|
|
211
|
-
var
|
|
203
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
204
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
205
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
212
206
|
|
|
213
207
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
214
208
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
215
|
-
|
|
216
|
-
|
|
209
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
210
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
217
211
|
}
|
|
218
212
|
}
|
|
219
213
|
|
|
220
214
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
221
215
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
222
|
-
|
|
216
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
223
217
|
}
|
|
224
218
|
}
|
|
225
219
|
|
|
226
|
-
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) =
|
|
220
|
+
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorQv(stateTreeDepth, voteOptionTreeDepth)(
|
|
227
221
|
totalSignups,
|
|
228
222
|
stateRoots[i + 1],
|
|
229
223
|
ballotRoots[i + 1],
|
|
230
224
|
actualStateTreeDepth,
|
|
231
225
|
currentStateLeaves[i],
|
|
232
|
-
|
|
226
|
+
computedCurrentStateLeavesPathElements,
|
|
233
227
|
currentBallots[i],
|
|
234
|
-
|
|
228
|
+
computedCurrentBallotPathElements,
|
|
235
229
|
currentVoteWeights[i],
|
|
236
|
-
|
|
230
|
+
computedCurrentVoteWeightsPathElements,
|
|
237
231
|
computedCommandsStateIndex[i],
|
|
238
232
|
computedCommandsNewPublicKey[i],
|
|
239
233
|
computedCommandsVoteOptionIndex[i],
|
|
@@ -254,199 +248,3 @@ template ProcessMessages(
|
|
|
254
248
|
var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
|
|
255
249
|
computedNewSbCommitment === newSbCommitment;
|
|
256
250
|
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Processes one message and updates the state accordingly.
|
|
260
|
-
* This template involves complex interactions, including transformations based on message type,
|
|
261
|
-
* validations against current states like voice credit balances or vote weights,
|
|
262
|
-
* and updates to Merkle trees representing state and ballot information.
|
|
263
|
-
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
264
|
-
* This template supports the Quadratic Voting (QV).
|
|
265
|
-
*/
|
|
266
|
-
template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
267
|
-
// Constants defining the structure and size of state and ballots.
|
|
268
|
-
var STATE_LEAF_LENGTH = 3;
|
|
269
|
-
var BALLOT_LENGTH = 2;
|
|
270
|
-
var MESSAGE_LENGTH = 10;
|
|
271
|
-
var PACKED_COMMAND_LENGTH = 4;
|
|
272
|
-
var VOTE_OPTION_TREE_ARITY = 5;
|
|
273
|
-
var STATE_TREE_ARITY = 2;
|
|
274
|
-
var BALLOT_NONCE_INDEX = 0;
|
|
275
|
-
// Ballot vote option (vote option) root index.
|
|
276
|
-
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
277
|
-
|
|
278
|
-
// Indices for elements within a state leaf.
|
|
279
|
-
// Public key.
|
|
280
|
-
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
281
|
-
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
282
|
-
// Voice Credit balance.
|
|
283
|
-
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
284
|
-
var NUMBER_BITS = 252;
|
|
285
|
-
|
|
286
|
-
// Number of users that have completed the sign up.
|
|
287
|
-
signal input totalSignups;
|
|
288
|
-
// The current value of the state tree root.
|
|
289
|
-
signal input currentStateRoot;
|
|
290
|
-
// The current value of the ballot tree root.
|
|
291
|
-
signal input currentBallotRoot;
|
|
292
|
-
// The actual tree depth (might be <= stateTreeDepth).
|
|
293
|
-
signal input actualStateTreeDepth;
|
|
294
|
-
|
|
295
|
-
// The state leaf and related path elements.
|
|
296
|
-
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
297
|
-
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
298
|
-
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
299
|
-
|
|
300
|
-
// The ballot and related path elements.
|
|
301
|
-
signal input ballot[BALLOT_LENGTH];
|
|
302
|
-
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
303
|
-
|
|
304
|
-
// The current vote weight and related path elements.
|
|
305
|
-
signal input currentVoteWeight;
|
|
306
|
-
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
307
|
-
|
|
308
|
-
// Inputs related to the command being processed.
|
|
309
|
-
signal input commandStateIndex;
|
|
310
|
-
signal input commandPublicKey[2];
|
|
311
|
-
signal input commandVoteOptionIndex;
|
|
312
|
-
signal input commandNewVoteWeight;
|
|
313
|
-
signal input commandNonce;
|
|
314
|
-
signal input commandPollId;
|
|
315
|
-
signal input commandSalt;
|
|
316
|
-
signal input commandSignaturePoint[2];
|
|
317
|
-
signal input commandSignatureScalar;
|
|
318
|
-
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
319
|
-
|
|
320
|
-
// The number of valid vote options for the poll.
|
|
321
|
-
signal input voteOptions;
|
|
322
|
-
|
|
323
|
-
signal output newStateRoot;
|
|
324
|
-
signal output newBallotRoot;
|
|
325
|
-
|
|
326
|
-
// Intermediate signals.
|
|
327
|
-
// currentVoteWeight * currentVoteWeight.
|
|
328
|
-
signal currentVoteWeightSquare;
|
|
329
|
-
// commandNewVoteWeight * commandNewVoteWeight.
|
|
330
|
-
signal commandNewVoteWeightSquare;
|
|
331
|
-
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
332
|
-
signal newBallotVoteOptionRoot;
|
|
333
|
-
|
|
334
|
-
// 1. Transform a state leaf and a ballot with a command.
|
|
335
|
-
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
336
|
-
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
337
|
-
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
338
|
-
totalSignups,
|
|
339
|
-
voteOptions,
|
|
340
|
-
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
341
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
342
|
-
ballot[BALLOT_NONCE_INDEX],
|
|
343
|
-
currentVoteWeight,
|
|
344
|
-
commandStateIndex,
|
|
345
|
-
commandPublicKey,
|
|
346
|
-
commandVoteOptionIndex,
|
|
347
|
-
commandNewVoteWeight,
|
|
348
|
-
commandNonce,
|
|
349
|
-
commandPollId,
|
|
350
|
-
commandSalt,
|
|
351
|
-
commandSignaturePoint,
|
|
352
|
-
commandSignatureScalar,
|
|
353
|
-
packedCommand
|
|
354
|
-
);
|
|
355
|
-
|
|
356
|
-
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
357
|
-
// Otherwise, generate indices for command.stateIndex.
|
|
358
|
-
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
359
|
-
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
360
|
-
|
|
361
|
-
// 3. Verify that the original state leaf exists in the given state root.
|
|
362
|
-
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
363
|
-
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
364
|
-
stateLeafHash,
|
|
365
|
-
actualStateTreeDepth,
|
|
366
|
-
computedStateLeafPathIndices,
|
|
367
|
-
stateLeafPathElements
|
|
368
|
-
);
|
|
369
|
-
|
|
370
|
-
stateLeafQip === currentStateRoot;
|
|
371
|
-
|
|
372
|
-
// 4. Verify that the original ballot exists in the given ballot root.
|
|
373
|
-
var computedBallot = PoseidonHasher(2)([
|
|
374
|
-
ballot[BALLOT_NONCE_INDEX],
|
|
375
|
-
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
376
|
-
]);
|
|
377
|
-
|
|
378
|
-
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
379
|
-
computedBallot,
|
|
380
|
-
computedStateLeafPathIndices,
|
|
381
|
-
ballotPathElements
|
|
382
|
-
);
|
|
383
|
-
|
|
384
|
-
computedBallotQip === currentBallotRoot;
|
|
385
|
-
|
|
386
|
-
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
387
|
-
// at commandVoteOptionIndex.
|
|
388
|
-
currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
|
|
389
|
-
commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
|
|
390
|
-
|
|
391
|
-
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
392
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
393
|
-
|
|
394
|
-
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
395
|
-
currentVoteWeight,
|
|
396
|
-
computedCurrentVoteWeightPathIndices,
|
|
397
|
-
currentVoteWeightsPathElements
|
|
398
|
-
);
|
|
399
|
-
|
|
400
|
-
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
401
|
-
|
|
402
|
-
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
403
|
-
var voiceCreditBalanceMux = Mux1()(
|
|
404
|
-
[
|
|
405
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
406
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeightSquare - commandNewVoteWeightSquare
|
|
407
|
-
],
|
|
408
|
-
computedIsValid
|
|
409
|
-
);
|
|
410
|
-
|
|
411
|
-
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
412
|
-
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
413
|
-
voteWeightMux,
|
|
414
|
-
computedCurrentVoteWeightPathIndices,
|
|
415
|
-
currentVoteWeightsPathElements
|
|
416
|
-
);
|
|
417
|
-
|
|
418
|
-
// The new vote option root in the ballot
|
|
419
|
-
var newBallotVoteOptionRootMux = Mux1()(
|
|
420
|
-
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
421
|
-
computedIsValid
|
|
422
|
-
);
|
|
423
|
-
|
|
424
|
-
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
425
|
-
|
|
426
|
-
// 6. Generate a new state root.
|
|
427
|
-
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
428
|
-
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
429
|
-
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
430
|
-
voiceCreditBalanceMux
|
|
431
|
-
]);
|
|
432
|
-
|
|
433
|
-
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
434
|
-
computedNewStateLeafHash,
|
|
435
|
-
actualStateTreeDepth,
|
|
436
|
-
computedStateLeafPathIndices,
|
|
437
|
-
stateLeafPathElements
|
|
438
|
-
);
|
|
439
|
-
|
|
440
|
-
newStateRoot <== computedNewStateLeafQip;
|
|
441
|
-
|
|
442
|
-
// 7. Generate a new ballot root.
|
|
443
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
444
|
-
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
445
|
-
computedNewBallot,
|
|
446
|
-
computedStateLeafPathIndices,
|
|
447
|
-
ballotPathElements
|
|
448
|
-
);
|
|
449
|
-
|
|
450
|
-
newBallotRoot <== computedNewBallotQip;
|
|
451
|
-
}
|
|
452
|
-
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../../utils/PoseidonHasher.circom";
|
|
7
|
+
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
8
|
+
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
9
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
10
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
11
|
+
include "../../utils/qv/StateLeafAndBallotTransformer.circom";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Processes one message and updates the state accordingly.
|
|
15
|
+
* This template involves complex interactions, including transformations based on message type,
|
|
16
|
+
* validations against current states like voice credit balances or vote weights,
|
|
17
|
+
* and updates to Merkle trees representing state and ballot information.
|
|
18
|
+
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
19
|
+
* This template supports the Quadratic Voting (QV).
|
|
20
|
+
*/
|
|
21
|
+
template SingleMessageProcessorQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
22
|
+
// Constants defining the structure and size of state and ballots.
|
|
23
|
+
var STATE_LEAF_LENGTH = 3;
|
|
24
|
+
var BALLOT_LENGTH = 2;
|
|
25
|
+
var MESSAGE_LENGTH = 10;
|
|
26
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
27
|
+
var VOTE_OPTION_TREE_ARITY = 5;
|
|
28
|
+
var STATE_TREE_ARITY = 2;
|
|
29
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
30
|
+
// Ballot vote option (vote option) root index.
|
|
31
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
32
|
+
|
|
33
|
+
// Indices for elements within a state leaf.
|
|
34
|
+
// Public key.
|
|
35
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
36
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
37
|
+
// Voice Credit balance.
|
|
38
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
39
|
+
var NUMBER_BITS = 252;
|
|
40
|
+
|
|
41
|
+
// Number of users that have completed the sign up.
|
|
42
|
+
signal input totalSignups;
|
|
43
|
+
// The current value of the state tree root.
|
|
44
|
+
signal input currentStateRoot;
|
|
45
|
+
// The current value of the ballot tree root.
|
|
46
|
+
signal input currentBallotRoot;
|
|
47
|
+
// The actual tree depth (might be <= stateTreeDepth).
|
|
48
|
+
signal input actualStateTreeDepth;
|
|
49
|
+
|
|
50
|
+
// The state leaf and related path elements.
|
|
51
|
+
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
52
|
+
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
53
|
+
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
54
|
+
|
|
55
|
+
// The ballot and related path elements.
|
|
56
|
+
signal input ballot[BALLOT_LENGTH];
|
|
57
|
+
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
58
|
+
|
|
59
|
+
// The current vote weight and related path elements.
|
|
60
|
+
signal input currentVoteWeight;
|
|
61
|
+
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
62
|
+
|
|
63
|
+
// Inputs related to the command being processed.
|
|
64
|
+
signal input commandStateIndex;
|
|
65
|
+
signal input commandPublicKey[2];
|
|
66
|
+
signal input commandVoteOptionIndex;
|
|
67
|
+
signal input commandNewVoteWeight;
|
|
68
|
+
signal input commandNonce;
|
|
69
|
+
signal input commandPollId;
|
|
70
|
+
signal input commandSalt;
|
|
71
|
+
signal input commandSignaturePoint[2];
|
|
72
|
+
signal input commandSignatureScalar;
|
|
73
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
74
|
+
|
|
75
|
+
// The number of valid vote options for the poll.
|
|
76
|
+
signal input voteOptions;
|
|
77
|
+
|
|
78
|
+
signal output newStateRoot;
|
|
79
|
+
signal output newBallotRoot;
|
|
80
|
+
|
|
81
|
+
// Intermediate signals.
|
|
82
|
+
// currentVoteWeight * currentVoteWeight.
|
|
83
|
+
signal currentVoteWeightSquare;
|
|
84
|
+
// commandNewVoteWeight * commandNewVoteWeight.
|
|
85
|
+
signal commandNewVoteWeightSquare;
|
|
86
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
87
|
+
signal newBallotVoteOptionRoot;
|
|
88
|
+
|
|
89
|
+
// 1. Transform a state leaf and a ballot with a command.
|
|
90
|
+
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
91
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
92
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
93
|
+
totalSignups,
|
|
94
|
+
voteOptions,
|
|
95
|
+
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
96
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
97
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
98
|
+
currentVoteWeight,
|
|
99
|
+
commandStateIndex,
|
|
100
|
+
commandPublicKey,
|
|
101
|
+
commandVoteOptionIndex,
|
|
102
|
+
commandNewVoteWeight,
|
|
103
|
+
commandNonce,
|
|
104
|
+
commandPollId,
|
|
105
|
+
commandSalt,
|
|
106
|
+
commandSignaturePoint,
|
|
107
|
+
commandSignatureScalar,
|
|
108
|
+
packedCommand
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
112
|
+
// Otherwise, generate indices for command.stateIndex.
|
|
113
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
114
|
+
|
|
115
|
+
// 3. Verify that the original state leaf exists in the given state root.
|
|
116
|
+
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
117
|
+
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
118
|
+
stateLeafHash,
|
|
119
|
+
actualStateTreeDepth,
|
|
120
|
+
stateIndexMux,
|
|
121
|
+
stateLeafPathElements
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
stateLeafQip === currentStateRoot;
|
|
125
|
+
|
|
126
|
+
// 4. Verify that the original ballot exists in the given ballot root.
|
|
127
|
+
var computedBallot = PoseidonHasher(2)([
|
|
128
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
129
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
130
|
+
]);
|
|
131
|
+
var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
|
|
132
|
+
|
|
133
|
+
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
134
|
+
computedBallot,
|
|
135
|
+
computedStateLeafPathIndices,
|
|
136
|
+
ballotPathElements
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
computedBallotQip === currentBallotRoot;
|
|
140
|
+
|
|
141
|
+
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
142
|
+
// at commandVoteOptionIndex.
|
|
143
|
+
currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
|
|
144
|
+
commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
|
|
145
|
+
|
|
146
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
147
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
148
|
+
|
|
149
|
+
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
150
|
+
currentVoteWeight,
|
|
151
|
+
computedCurrentVoteWeightPathIndices,
|
|
152
|
+
currentVoteWeightsPathElements
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
156
|
+
|
|
157
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
158
|
+
var voiceCreditBalanceMux = Mux1()(
|
|
159
|
+
[
|
|
160
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
161
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeightSquare - commandNewVoteWeightSquare
|
|
162
|
+
],
|
|
163
|
+
computedIsValid
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
167
|
+
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
168
|
+
voteWeightMux,
|
|
169
|
+
computedCurrentVoteWeightPathIndices,
|
|
170
|
+
currentVoteWeightsPathElements
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// The new vote option root in the ballot
|
|
174
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
175
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
176
|
+
computedIsValid
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
180
|
+
|
|
181
|
+
// 6. Generate a new state root.
|
|
182
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
183
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
184
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
185
|
+
voiceCreditBalanceMux
|
|
186
|
+
]);
|
|
187
|
+
|
|
188
|
+
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
189
|
+
computedNewStateLeafHash,
|
|
190
|
+
actualStateTreeDepth,
|
|
191
|
+
stateIndexMux,
|
|
192
|
+
stateLeafPathElements
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
newStateRoot <== computedNewStateLeafQip;
|
|
196
|
+
|
|
197
|
+
// 7. Generate a new ballot root.
|
|
198
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
199
|
+
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
200
|
+
computedNewBallot,
|
|
201
|
+
computedStateLeafPathIndices,
|
|
202
|
+
ballotPathElements
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
newBallotRoot <== computedNewBallotQip;
|
|
206
|
+
}
|
|
207
|
+
|