@maci-protocol/circuits 0.0.0-ci.a1bedc5 → 0.0.0-ci.a51dc13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -2
- package/build/ts/{genZkeys.d.ts → generateZkeys.d.ts} +1 -1
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/{genZkeys.js → generateZkeys.js} +1 -1
- package/build/ts/generateZkeys.js.map +1 -0
- package/build/ts/types.d.ts +5 -5
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +20 -4
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +102 -104
- package/circom/coordinator/non-qv/tallyVotes.circom +47 -44
- package/circom/coordinator/qv/processMessages.circom +103 -102
- package/circom/coordinator/qv/tallyVotes.circom +61 -61
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/PrivateToPublicKey.circom +3 -3
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +34 -34
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +15 -13
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +34 -34
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +1 -1
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
- package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
- package/circom/utils/trees/QuinarySelector.circom +42 -0
- package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
- package/circom/utils/trees/Splicer.circom +76 -0
- package/circom/voter/PollJoined.circom +2 -2
- package/package.json +11 -9
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -9,13 +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 "../../utils/non-qv/
|
|
12
|
+
include "../../utils/non-qv/StateLeafAndBallotTransformer.circom";
|
|
13
13
|
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
14
14
|
include "../../utils/trees/LeafExists.circom";
|
|
15
15
|
include "../../utils/trees/CheckRoot.circom";
|
|
16
|
-
include "../../utils/trees/
|
|
16
|
+
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
17
17
|
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
18
|
-
include "../../utils/trees/
|
|
18
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
19
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* Proves the correctness of processing a batch of MACI messages.
|
|
@@ -35,27 +36,27 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
35
36
|
var VOTE_OPTION_TREE_ARITY = 5;
|
|
36
37
|
// Default for Binary trees.
|
|
37
38
|
var STATE_TREE_ARITY = 2;
|
|
38
|
-
var
|
|
39
|
-
var
|
|
39
|
+
var MESSAGE_LENGTH = 10;
|
|
40
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
40
41
|
var STATE_LEAF_LENGTH = 3;
|
|
41
42
|
var BALLOT_LENGTH = 2;
|
|
42
|
-
var
|
|
43
|
-
var
|
|
44
|
-
var
|
|
45
|
-
var
|
|
46
|
-
var
|
|
47
|
-
var
|
|
43
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
44
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
45
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
46
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
47
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
48
|
+
var MESSAGE_TREE_ZERO_VALUE = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
48
49
|
// Number of options for this poll.
|
|
49
50
|
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
50
51
|
|
|
51
52
|
// Number of users that have completed the sign up.
|
|
52
|
-
signal input
|
|
53
|
+
signal input totalSignups;
|
|
53
54
|
// Value of chainHash at beginning of batch
|
|
54
55
|
signal input inputBatchHash;
|
|
55
56
|
// Value of chainHash at end of batch
|
|
56
57
|
signal input outputBatchHash;
|
|
57
58
|
// The messages.
|
|
58
|
-
signal input messages[batchSize][
|
|
59
|
+
signal input messages[batchSize][MESSAGE_LENGTH];
|
|
59
60
|
// The coordinator's private key.
|
|
60
61
|
signal input coordinatorPrivateKey;
|
|
61
62
|
// The ECDH public key per message.
|
|
@@ -113,31 +114,28 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
113
114
|
// signals (for processing purposes).
|
|
114
115
|
signal stateRoots[batchSize + 1];
|
|
115
116
|
signal ballotRoots[batchSize + 1];
|
|
116
|
-
signal tmpStateRoot1[batchSize];
|
|
117
|
-
signal tmpStateRoot2[batchSize];
|
|
118
|
-
signal tmpBallotRoot1[batchSize];
|
|
119
|
-
signal tmpBallotRoot2[batchSize];
|
|
120
117
|
|
|
121
118
|
// Must verify the current sb commitment.
|
|
122
119
|
var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
|
|
123
120
|
computedCurrentSbCommitment === currentSbCommitment;
|
|
124
121
|
|
|
125
122
|
// -----------------------------------------------------------------------
|
|
126
|
-
|
|
123
|
+
// 0. Ensure that the maximum vote options signal is valid and if
|
|
127
124
|
// the maximum users signal is valid
|
|
128
125
|
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
129
126
|
voteOptionsValid === 1;
|
|
130
127
|
|
|
131
|
-
// Check
|
|
128
|
+
// Check totalSignups <= the max number of users (i.e., number of state leaves
|
|
132
129
|
// that can fit the state tree).
|
|
133
|
-
var
|
|
134
|
-
|
|
130
|
+
var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
|
|
131
|
+
totalSignupsValid === 1;
|
|
135
132
|
|
|
136
133
|
// Hash each Message to check their existence in the Message chain hash.
|
|
137
134
|
var computedMessageHashers[batchSize];
|
|
138
135
|
var computedChainHashes[batchSize];
|
|
139
136
|
var chainHash[batchSize + 1];
|
|
140
137
|
chainHash[0] = inputBatchHash;
|
|
138
|
+
|
|
141
139
|
for (var i = 0; i < batchSize; i++) {
|
|
142
140
|
// calculate message hash
|
|
143
141
|
computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
|
|
@@ -165,36 +163,36 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
165
163
|
// Ensure that the coordinator's public key from the contract is correct
|
|
166
164
|
// based on the given private key - that is, the prover knows the
|
|
167
165
|
// coordinator's private key.
|
|
168
|
-
var
|
|
169
|
-
var
|
|
170
|
-
|
|
166
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
|
|
167
|
+
var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
|
|
168
|
+
derivedPublicKeyHash === coordinatorPublicKeyHash;
|
|
171
169
|
|
|
172
170
|
// Decrypt each Message into a Command.
|
|
173
171
|
// The command i-th is composed by the following fields.
|
|
174
172
|
// e.g., command 0 is made of commandsStateIndex[0],
|
|
175
|
-
//
|
|
173
|
+
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
176
174
|
var computedCommandsStateIndex[batchSize];
|
|
177
|
-
var
|
|
175
|
+
var computedCommandsNewPublicKey[batchSize][2];
|
|
178
176
|
var computedCommandsVoteOptionIndex[batchSize];
|
|
179
177
|
var computedCommandsNewVoteWeight[batchSize];
|
|
180
178
|
var computedCommandsNonce[batchSize];
|
|
181
179
|
var computedCommandsPollId[batchSize];
|
|
182
180
|
var computedCommandsSalt[batchSize];
|
|
183
|
-
var
|
|
184
|
-
var
|
|
185
|
-
var computedCommandsPackedCommandOut[batchSize][
|
|
181
|
+
var computedCommandsSignaturePoint[batchSize][2];
|
|
182
|
+
var computedCommandsSignatureScalar[batchSize];
|
|
183
|
+
var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
|
|
186
184
|
|
|
187
185
|
for (var i = 0; i < batchSize; i++) {
|
|
188
186
|
(
|
|
189
187
|
computedCommandsStateIndex[i],
|
|
190
|
-
|
|
188
|
+
computedCommandsNewPublicKey[i],
|
|
191
189
|
computedCommandsVoteOptionIndex[i],
|
|
192
190
|
computedCommandsNewVoteWeight[i],
|
|
193
191
|
computedCommandsNonce[i],
|
|
194
192
|
computedCommandsPollId[i],
|
|
195
193
|
computedCommandsSalt[i],
|
|
196
|
-
|
|
197
|
-
|
|
194
|
+
computedCommandsSignaturePoint[i],
|
|
195
|
+
computedCommandsSignatureScalar[i],
|
|
198
196
|
computedCommandsPackedCommandOut[i]
|
|
199
197
|
) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
|
|
200
198
|
}
|
|
@@ -211,43 +209,43 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
211
209
|
// Start from batchSize and decrement for process in reverse order.
|
|
212
210
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
213
211
|
// Process as vote type message.
|
|
214
|
-
var
|
|
215
|
-
var
|
|
216
|
-
var
|
|
212
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
213
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
214
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
217
215
|
|
|
218
216
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
219
217
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
220
|
-
|
|
221
|
-
|
|
218
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
219
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
222
220
|
}
|
|
223
221
|
}
|
|
224
222
|
|
|
225
223
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
226
224
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
227
|
-
|
|
225
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
228
226
|
}
|
|
229
227
|
}
|
|
230
228
|
|
|
231
229
|
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
|
|
232
|
-
|
|
230
|
+
totalSignups,
|
|
233
231
|
stateRoots[i + 1],
|
|
234
232
|
ballotRoots[i + 1],
|
|
235
233
|
actualStateTreeDepth,
|
|
236
234
|
currentStateLeaves[i],
|
|
237
|
-
|
|
235
|
+
computedCurrentStateLeavesPathElements,
|
|
238
236
|
currentBallots[i],
|
|
239
|
-
|
|
237
|
+
computedCurrentBallotPathElements,
|
|
240
238
|
currentVoteWeights[i],
|
|
241
|
-
|
|
239
|
+
computedCurrentVoteWeightsPathElements,
|
|
242
240
|
computedCommandsStateIndex[i],
|
|
243
|
-
|
|
241
|
+
computedCommandsNewPublicKey[i],
|
|
244
242
|
computedCommandsVoteOptionIndex[i],
|
|
245
243
|
computedCommandsNewVoteWeight[i],
|
|
246
244
|
computedCommandsNonce[i],
|
|
247
245
|
computedCommandsPollId[i],
|
|
248
246
|
computedCommandsSalt[i],
|
|
249
|
-
|
|
250
|
-
|
|
247
|
+
computedCommandsSignaturePoint[i],
|
|
248
|
+
computedCommandsSignatureScalar[i],
|
|
251
249
|
computedCommandsPackedCommandOut[i],
|
|
252
250
|
voteOptions
|
|
253
251
|
);
|
|
@@ -272,24 +270,24 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
272
270
|
// Constants defining the structure and size of state and ballots.
|
|
273
271
|
var STATE_LEAF_LENGTH = 3;
|
|
274
272
|
var BALLOT_LENGTH = 2;
|
|
275
|
-
var
|
|
276
|
-
var
|
|
273
|
+
var MESSAGE_LENGTH = 10;
|
|
274
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
277
275
|
var VOTE_OPTION_TREE_ARITY = 5;
|
|
278
276
|
var STATE_TREE_ARITY = 2;
|
|
279
|
-
var
|
|
280
|
-
// Ballot vote option (
|
|
281
|
-
var
|
|
277
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
278
|
+
// Ballot vote option (vote option) root index.
|
|
279
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
282
280
|
|
|
283
281
|
// Indices for elements within a state leaf.
|
|
284
282
|
// Public key.
|
|
285
|
-
var
|
|
286
|
-
var
|
|
283
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
284
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
287
285
|
// Voice Credit balance.
|
|
288
|
-
var
|
|
289
|
-
var
|
|
286
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
287
|
+
var NUMBER_BITS = 252;
|
|
290
288
|
|
|
291
289
|
// Number of users that have completed the sign up.
|
|
292
|
-
signal input
|
|
290
|
+
signal input totalSignups;
|
|
293
291
|
// The current value of the state tree root.
|
|
294
292
|
signal input currentStateRoot;
|
|
295
293
|
// The current value of the ballot tree root.
|
|
@@ -311,16 +309,16 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
311
309
|
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
312
310
|
|
|
313
311
|
// Inputs related to the command being processed.
|
|
314
|
-
signal input
|
|
315
|
-
signal input
|
|
316
|
-
signal input
|
|
317
|
-
signal input
|
|
318
|
-
signal input
|
|
319
|
-
signal input
|
|
320
|
-
signal input
|
|
321
|
-
signal input
|
|
322
|
-
signal input
|
|
323
|
-
signal input
|
|
312
|
+
signal input commandStateIndex;
|
|
313
|
+
signal input commandPublicKey[2];
|
|
314
|
+
signal input commandVoteOptionIndex;
|
|
315
|
+
signal input commandNewVoteWeight;
|
|
316
|
+
signal input commandNonce;
|
|
317
|
+
signal input commandPollId;
|
|
318
|
+
signal input commandSalt;
|
|
319
|
+
signal input commandSignaturePoint[2];
|
|
320
|
+
signal input commandSignatureScalar;
|
|
321
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
324
322
|
|
|
325
323
|
// The number of valid vote options for the poll.
|
|
326
324
|
signal input voteOptions;
|
|
@@ -328,35 +326,35 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
328
326
|
signal output newStateRoot;
|
|
329
327
|
signal output newBallotRoot;
|
|
330
328
|
|
|
331
|
-
// equal to
|
|
332
|
-
signal
|
|
329
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
330
|
+
signal newBallotVoteOptionRoot;
|
|
333
331
|
|
|
334
332
|
// 1. Transform a state leaf and a ballot with a command.
|
|
335
333
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
336
|
-
var
|
|
337
|
-
(
|
|
338
|
-
|
|
334
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
335
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
|
|
336
|
+
totalSignups,
|
|
339
337
|
voteOptions,
|
|
340
|
-
[stateLeaf[
|
|
341
|
-
stateLeaf[
|
|
342
|
-
ballot[
|
|
338
|
+
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
339
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
340
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
343
341
|
currentVoteWeight,
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
342
|
+
commandStateIndex,
|
|
343
|
+
commandPublicKey,
|
|
344
|
+
commandVoteOptionIndex,
|
|
345
|
+
commandNewVoteWeight,
|
|
346
|
+
commandNonce,
|
|
347
|
+
commandPollId,
|
|
348
|
+
commandSalt,
|
|
349
|
+
commandSignaturePoint,
|
|
350
|
+
commandSignatureScalar,
|
|
351
|
+
packedCommand
|
|
354
352
|
);
|
|
355
353
|
|
|
356
354
|
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
357
355
|
// Otherwise, generate indices for command.stateIndex.
|
|
358
|
-
var stateIndexMux = Mux1()([0,
|
|
359
|
-
var computedStateLeafPathIndices[stateTreeDepth] =
|
|
356
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
357
|
+
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
360
358
|
|
|
361
359
|
// 3. Verify that the original state leaf exists in the given state root.
|
|
362
360
|
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
@@ -371,8 +369,8 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
371
369
|
|
|
372
370
|
// 4. Verify that the original ballot exists in the given ballot root.
|
|
373
371
|
var computedBallot = PoseidonHasher(2)([
|
|
374
|
-
ballot[
|
|
375
|
-
ballot[
|
|
372
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
373
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
376
374
|
]);
|
|
377
375
|
|
|
378
376
|
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
@@ -384,51 +382,51 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
384
382
|
computedBallotQip === currentBallotRoot;
|
|
385
383
|
|
|
386
384
|
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
387
|
-
// at
|
|
388
|
-
var
|
|
389
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] =
|
|
385
|
+
// at commandVoteOptionIndex.
|
|
386
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
387
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
390
388
|
|
|
391
|
-
var computedCurrentVoteWeightQip =
|
|
389
|
+
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
392
390
|
currentVoteWeight,
|
|
393
391
|
computedCurrentVoteWeightPathIndices,
|
|
394
392
|
currentVoteWeightsPathElements
|
|
395
393
|
);
|
|
396
394
|
|
|
397
|
-
computedCurrentVoteWeightQip === ballot[
|
|
395
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
398
396
|
|
|
399
|
-
var voteWeightMux = Mux1()([currentVoteWeight,
|
|
397
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
400
398
|
var voiceCreditBalanceMux = Mux1()(
|
|
401
399
|
[
|
|
402
|
-
stateLeaf[
|
|
403
|
-
stateLeaf[
|
|
400
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
401
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
|
|
404
402
|
],
|
|
405
403
|
computedIsValid
|
|
406
404
|
);
|
|
407
405
|
|
|
408
406
|
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
409
|
-
var computedNewVoteOptionTreeQip =
|
|
407
|
+
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
410
408
|
voteWeightMux,
|
|
411
409
|
computedCurrentVoteWeightPathIndices,
|
|
412
410
|
currentVoteWeightsPathElements
|
|
413
411
|
);
|
|
414
412
|
|
|
415
413
|
// The new vote option root in the ballot
|
|
416
|
-
var
|
|
417
|
-
[ballot[
|
|
414
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
415
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
418
416
|
computedIsValid
|
|
419
417
|
);
|
|
420
418
|
|
|
421
|
-
|
|
419
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
422
420
|
|
|
423
421
|
// 6. Generate a new state root.
|
|
424
|
-
var
|
|
425
|
-
|
|
426
|
-
|
|
422
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
423
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
424
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
427
425
|
voiceCreditBalanceMux
|
|
428
426
|
]);
|
|
429
427
|
|
|
430
428
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
431
|
-
|
|
429
|
+
computedNewStateLeafHash,
|
|
432
430
|
actualStateTreeDepth,
|
|
433
431
|
computedStateLeafPathIndices,
|
|
434
432
|
stateLeafPathElements
|
|
@@ -437,7 +435,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
437
435
|
newStateRoot <== computedNewStateLeafQip;
|
|
438
436
|
|
|
439
437
|
// 7. Generate a new ballot root.
|
|
440
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce,
|
|
438
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
441
439
|
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
442
440
|
computedNewBallot,
|
|
443
441
|
computedStateLeafPathIndices,
|
|
@@ -6,9 +6,9 @@ include "./comparators.circom";
|
|
|
6
6
|
include "./unpack-element.circom";
|
|
7
7
|
// local imports
|
|
8
8
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
|
-
include "../../utils/trees/
|
|
9
|
+
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
10
10
|
include "../../utils/trees/LeafExists.circom";
|
|
11
|
-
include "../../utils/trees/
|
|
11
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
12
12
|
include "../../utils/CalculateTotal.circom";
|
|
13
13
|
include "../../utils/PoseidonHasher.circom";
|
|
14
14
|
|
|
@@ -18,33 +18,33 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
18
18
|
*/
|
|
19
19
|
template TallyVotesNonQv(
|
|
20
20
|
stateTreeDepth,
|
|
21
|
-
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
22
|
voteOptionTreeDepth
|
|
23
23
|
) {
|
|
24
24
|
// Ensure there's at least one level in the vote option tree.
|
|
25
25
|
assert(voteOptionTreeDepth > 0);
|
|
26
26
|
// Ensure the intermediate state tree has at least one level.
|
|
27
|
-
assert(
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
28
|
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
-
assert(
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
30
|
|
|
31
31
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
32
|
var TREE_ARITY = 5;
|
|
33
33
|
var BALLOT_TREE_ARITY = 2;
|
|
34
34
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
-
var batchSize = BALLOT_TREE_ARITY **
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
37
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
|
-
var
|
|
38
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
40
40
|
// Number of elements in each ballot.
|
|
41
41
|
var BALLOT_LENGTH = 2;
|
|
42
42
|
// Index for the nonce in the ballot array.
|
|
43
|
-
var
|
|
43
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
|
-
var
|
|
45
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
46
|
// Difference in tree depths, used in path calculations.
|
|
47
|
-
var
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -61,13 +61,13 @@ template TallyVotesNonQv(
|
|
|
61
61
|
// Start index of given batch
|
|
62
62
|
signal input index;
|
|
63
63
|
// Number of users that signup
|
|
64
|
-
signal input
|
|
64
|
+
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
68
|
-
signal input votes[batchSize][
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
|
+
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
|
-
signal input currentResults[
|
|
70
|
+
signal input currentResults[totalVoteOptions];
|
|
71
71
|
// Salt for the root of the current results.
|
|
72
72
|
signal input currentResultsRootSalt;
|
|
73
73
|
// Total voice credits spent so far.
|
|
@@ -85,21 +85,21 @@ template TallyVotesNonQv(
|
|
|
85
85
|
|
|
86
86
|
|
|
87
87
|
// Validates that the index is within the valid range of sign-ups.
|
|
88
|
-
var
|
|
89
|
-
|
|
88
|
+
var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
|
|
89
|
+
totalSignupsValid === 1;
|
|
90
90
|
|
|
91
91
|
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
92
92
|
var computedBallotHashers[batchSize];
|
|
93
93
|
|
|
94
94
|
for (var i = 0; i < batchSize; i++) {
|
|
95
|
-
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][
|
|
95
|
+
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
var computedBallotSubroot = CheckRoot(
|
|
99
|
-
var computedBallotPathIndices[
|
|
98
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
99
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
100
100
|
|
|
101
101
|
// Verifies each ballot's existence within the ballot tree.
|
|
102
|
-
LeafExists(
|
|
102
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
103
103
|
computedBallotSubroot,
|
|
104
104
|
ballotPathElements,
|
|
105
105
|
computedBallotPathIndices,
|
|
@@ -108,9 +108,10 @@ template TallyVotesNonQv(
|
|
|
108
108
|
|
|
109
109
|
// Processes vote options, verifying each against its declared root.
|
|
110
110
|
var computedVoteTree[batchSize];
|
|
111
|
+
|
|
111
112
|
for (var i = 0; i < batchSize; i++) {
|
|
112
|
-
computedVoteTree[i] =
|
|
113
|
-
computedVoteTree[i] === ballots[i][
|
|
113
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
114
|
+
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
@@ -118,27 +119,30 @@ template TallyVotesNonQv(
|
|
|
118
119
|
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
119
120
|
|
|
120
121
|
// Tally the new results.
|
|
121
|
-
var computedCalculateTotalResult[
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
123
|
+
|
|
124
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
125
|
+
var computedVotes[batchSize + 1];
|
|
126
|
+
computedVotes[batchSize] = currentResults[i] * computedIsZero;
|
|
127
|
+
|
|
125
128
|
for (var j = 0; j < batchSize; j++) {
|
|
126
|
-
|
|
129
|
+
computedVotes[j] = votes[j][i];
|
|
127
130
|
}
|
|
128
131
|
|
|
129
|
-
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(
|
|
132
|
+
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
|
|
130
133
|
}
|
|
131
134
|
|
|
132
135
|
// Tally the new spent voice credit total.
|
|
133
|
-
var
|
|
134
|
-
|
|
136
|
+
var computedTotalVoiceCreditSpent[batchSize * totalVoteOptions + 1];
|
|
137
|
+
computedTotalVoiceCreditSpent[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
138
|
+
|
|
135
139
|
for (var i = 0; i < batchSize; i++) {
|
|
136
|
-
for (var j = 0; j <
|
|
137
|
-
|
|
140
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
141
|
+
computedTotalVoiceCreditSpent[i * totalVoteOptions + j] = votes[i][j];
|
|
138
142
|
}
|
|
139
143
|
}
|
|
140
144
|
|
|
141
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize *
|
|
145
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedTotalVoiceCreditSpent);
|
|
142
146
|
|
|
143
147
|
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
144
148
|
ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
|
|
@@ -165,7 +169,7 @@ template TallyVotesNonQv(
|
|
|
165
169
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
166
170
|
var TREE_ARITY = 5;
|
|
167
171
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
168
|
-
var
|
|
172
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
169
173
|
|
|
170
174
|
// Equal to 1 if this is the first batch, otherwise 0.
|
|
171
175
|
signal input isFirstBatch;
|
|
@@ -175,12 +179,12 @@ template TallyVotesNonQv(
|
|
|
175
179
|
signal input newTallyCommitment;
|
|
176
180
|
|
|
177
181
|
// Current results for each vote option.
|
|
178
|
-
signal input currentResults[
|
|
182
|
+
signal input currentResults[totalVoteOptions];
|
|
179
183
|
// Salt for the root of the current results.
|
|
180
184
|
signal input currentResultsRootSalt;
|
|
181
185
|
|
|
182
186
|
// New results for each vote option.
|
|
183
|
-
signal input newResults[
|
|
187
|
+
signal input newResults[totalVoteOptions];
|
|
184
188
|
// Salt for the root of the new results.
|
|
185
189
|
signal input newResultsRootSalt;
|
|
186
190
|
|
|
@@ -195,7 +199,7 @@ template TallyVotesNonQv(
|
|
|
195
199
|
signal input newSpentVoiceCreditSubtotalSalt;
|
|
196
200
|
|
|
197
201
|
// Compute the commitment to the current results.
|
|
198
|
-
var computedCurrentResultsRoot =
|
|
202
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
199
203
|
|
|
200
204
|
// Verify currentResultsCommitmentHash.
|
|
201
205
|
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
@@ -211,14 +215,14 @@ template TallyVotesNonQv(
|
|
|
211
215
|
// computedIsZero.out is 0 if this is the first batch.
|
|
212
216
|
var computedIsZero = IsZero()(isFirstBatch);
|
|
213
217
|
|
|
214
|
-
//
|
|
215
|
-
//
|
|
216
|
-
signal
|
|
217
|
-
|
|
218
|
-
|
|
218
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
219
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
220
|
+
signal isFirstCommitment;
|
|
221
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
222
|
+
isFirstCommitment === currentTallyCommitment;
|
|
219
223
|
|
|
220
224
|
// Compute the root of the new results.
|
|
221
|
-
var computedNewResultsRoot =
|
|
225
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
222
226
|
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
223
227
|
|
|
224
228
|
// Compute the commitment to the new spent voice credits value.
|
|
@@ -232,4 +236,3 @@ template TallyVotesNonQv(
|
|
|
232
236
|
|
|
233
237
|
computedNewTallyCommitment === newTallyCommitment;
|
|
234
238
|
}
|
|
235
|
-
|