@maci-protocol/circuits 0.0.0-ci.044d30d → 0.0.0-ci.0bef05d
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/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 +8 -8
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +7 -7
- package/circom/coordinator/non-qv/processMessages.circom +92 -95
- package/circom/coordinator/non-qv/tallyVotes.circom +35 -32
- package/circom/coordinator/qv/processMessages.circom +94 -94
- package/circom/coordinator/qv/tallyVotes.circom +37 -37
- package/circom/utils/{calculateTotal.circom → CalculateTotal.circom} +2 -0
- package/circom/utils/{verifySignature.circom → EdDSAPoseidonVerifier.circom} +40 -66
- package/circom/utils/MessageHasher.circom +57 -0
- package/circom/utils/MessageToCommand.circom +107 -0
- package/circom/utils/PoseidonHasher.circom +29 -0
- package/circom/utils/{privToPubKey.circom → PrivateToPublicKey.circom} +11 -9
- package/circom/utils/VerifySignature.circom +39 -0
- package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +13 -11
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +32 -32
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +13 -11
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +32 -32
- package/circom/utils/trees/BinaryMerkleRoot.circom +11 -3
- package/circom/utils/trees/CheckRoot.circom +18 -14
- package/circom/utils/trees/LeafExists.circom +1 -1
- package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +11 -7
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +6 -5
- package/circom/utils/trees/incrementalQuinaryTree.circom +2 -2
- package/circom/voter/PollJoined.circom +43 -0
- package/circom/voter/PollJoining.circom +54 -0
- package/package.json +12 -11
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
- package/circom/utils/hashers.circom +0 -78
- package/circom/utils/messageToCommand.circom +0 -78
- package/circom/voter/poll.circom +0 -92
|
@@ -5,16 +5,16 @@ include "./mux1.circom";
|
|
|
5
5
|
// zk-kit imports
|
|
6
6
|
include "./safe-comparators.circom";
|
|
7
7
|
// local imports
|
|
8
|
-
include "../../utils/
|
|
9
|
-
include "../../utils/
|
|
10
|
-
include "../../utils/
|
|
11
|
-
include "../../utils/
|
|
8
|
+
include "../../utils/PoseidonHasher.circom";
|
|
9
|
+
include "../../utils/MessageHasher.circom";
|
|
10
|
+
include "../../utils/MessageToCommand.circom";
|
|
11
|
+
include "../../utils/PrivateToPublicKey.circom";
|
|
12
|
+
include "../../utils/qv/StateLeafAndBallotTransformer.circom";
|
|
12
13
|
include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
13
|
-
// Merkle tree utilities (split from incrementalMerkleTree.circom)
|
|
14
14
|
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
15
15
|
include "../../utils/trees/LeafExists.circom";
|
|
16
16
|
include "../../utils/trees/CheckRoot.circom";
|
|
17
|
-
include "../../utils/trees/
|
|
17
|
+
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
18
18
|
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -35,31 +35,31 @@ template ProcessMessages(
|
|
|
35
35
|
var VOTE_OPTION_TREE_ARITY = 5;
|
|
36
36
|
// Default for binary trees.
|
|
37
37
|
var STATE_TREE_ARITY = 2;
|
|
38
|
-
var
|
|
39
|
-
var
|
|
38
|
+
var MESSAGE_LENGTH = 10;
|
|
39
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
40
40
|
var STATE_LEAF_LENGTH = 3;
|
|
41
41
|
var BALLOT_LENGTH = 2;
|
|
42
|
-
var
|
|
43
|
-
var
|
|
44
|
-
var
|
|
45
|
-
var
|
|
46
|
-
var
|
|
47
|
-
var
|
|
42
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
43
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
44
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
45
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
46
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
47
|
+
var MESSAGE_TREE_ZERO_VALUE = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
48
48
|
// Number of options for this poll.
|
|
49
49
|
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
50
50
|
|
|
51
51
|
// Number of users that have completed the sign up.
|
|
52
|
-
signal input
|
|
52
|
+
signal input totalSignups;
|
|
53
53
|
// Value of chainHash at beginning of batch
|
|
54
54
|
signal input inputBatchHash;
|
|
55
55
|
// Value of chainHash at end of batch
|
|
56
56
|
signal input outputBatchHash;
|
|
57
57
|
// The messages.
|
|
58
|
-
signal input
|
|
58
|
+
signal input messages[batchSize][MESSAGE_LENGTH];
|
|
59
59
|
// The coordinator's private key.
|
|
60
|
-
signal input
|
|
60
|
+
signal input coordinatorPrivateKey;
|
|
61
61
|
// The ECDH public key per message.
|
|
62
|
-
signal input
|
|
62
|
+
signal input encryptionPublicKeys[batchSize][2];
|
|
63
63
|
// The current state root (before the processing).
|
|
64
64
|
signal input currentStateRoot;
|
|
65
65
|
// The actual tree depth (might be <= stateTreeDepth).
|
|
@@ -122,10 +122,10 @@ template ProcessMessages(
|
|
|
122
122
|
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
123
123
|
voteOptionsValid === 1;
|
|
124
124
|
|
|
125
|
-
// Check
|
|
125
|
+
// Check totalSignups <= the max number of users (i.e., number of state leaves
|
|
126
126
|
// that can fit the state tree).
|
|
127
|
-
var
|
|
128
|
-
|
|
127
|
+
var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
|
|
128
|
+
totalSignupsValid === 1;
|
|
129
129
|
|
|
130
130
|
// Hash each Message to check their existence in the Message tree.
|
|
131
131
|
var computedMessageHashers[batchSize];
|
|
@@ -135,7 +135,7 @@ template ProcessMessages(
|
|
|
135
135
|
|
|
136
136
|
for (var i = 0; i < batchSize; i++) {
|
|
137
137
|
// calculate message hash
|
|
138
|
-
computedMessageHashers[i] = MessageHasher()(
|
|
138
|
+
computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
|
|
139
139
|
// check if message is valid or not (if index of message is less than index of last valid message in batch)
|
|
140
140
|
var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
|
|
141
141
|
// calculate chain hash if message is valid
|
|
@@ -159,38 +159,38 @@ template ProcessMessages(
|
|
|
159
159
|
// Ensure that the coordinator's public key from the contract is correct
|
|
160
160
|
// based on the given private key - that is, the prover knows the
|
|
161
161
|
// coordinator's private key.
|
|
162
|
-
var
|
|
163
|
-
var
|
|
164
|
-
|
|
162
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
|
|
163
|
+
var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
|
|
164
|
+
derivedPublicKeyHash === coordinatorPublicKeyHash;
|
|
165
165
|
|
|
166
166
|
// Decrypt each Message into a Command.
|
|
167
167
|
// The command i-th is composed by the following fields.
|
|
168
168
|
// e.g., command 0 is made of commandsStateIndex[0],
|
|
169
|
-
//
|
|
169
|
+
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
170
170
|
var computedCommandsStateIndex[batchSize];
|
|
171
|
-
var
|
|
171
|
+
var computedCommandsNewPublicKey[batchSize][2];
|
|
172
172
|
var computedCommandsVoteOptionIndex[batchSize];
|
|
173
173
|
var computedCommandsNewVoteWeight[batchSize];
|
|
174
174
|
var computedCommandsNonce[batchSize];
|
|
175
175
|
var computedCommandsPollId[batchSize];
|
|
176
176
|
var computedCommandsSalt[batchSize];
|
|
177
|
-
var
|
|
178
|
-
var
|
|
179
|
-
var computedCommandsPackedCommandOut[batchSize][
|
|
177
|
+
var computedCommandsSignaturePoint[batchSize][2];
|
|
178
|
+
var computedCommandsSignatureScalar[batchSize];
|
|
179
|
+
var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
|
|
180
180
|
|
|
181
181
|
for (var i = 0; i < batchSize; i++) {
|
|
182
182
|
(
|
|
183
183
|
computedCommandsStateIndex[i],
|
|
184
|
-
|
|
184
|
+
computedCommandsNewPublicKey[i],
|
|
185
185
|
computedCommandsVoteOptionIndex[i],
|
|
186
186
|
computedCommandsNewVoteWeight[i],
|
|
187
187
|
computedCommandsNonce[i],
|
|
188
188
|
computedCommandsPollId[i],
|
|
189
189
|
computedCommandsSalt[i],
|
|
190
|
-
|
|
191
|
-
|
|
190
|
+
computedCommandsSignaturePoint[i],
|
|
191
|
+
computedCommandsSignatureScalar[i],
|
|
192
192
|
computedCommandsPackedCommandOut[i]
|
|
193
|
-
) = MessageToCommand()(
|
|
193
|
+
) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
|
|
194
194
|
}
|
|
195
195
|
|
|
196
196
|
// Process messages in reverse order.
|
|
@@ -223,7 +223,7 @@ template ProcessMessages(
|
|
|
223
223
|
}
|
|
224
224
|
|
|
225
225
|
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOne(stateTreeDepth, voteOptionTreeDepth)(
|
|
226
|
-
|
|
226
|
+
totalSignups,
|
|
227
227
|
stateRoots[i + 1],
|
|
228
228
|
ballotRoots[i + 1],
|
|
229
229
|
actualStateTreeDepth,
|
|
@@ -234,14 +234,14 @@ template ProcessMessages(
|
|
|
234
234
|
currentVoteWeights[i],
|
|
235
235
|
currentVoteWeightsPathElement,
|
|
236
236
|
computedCommandsStateIndex[i],
|
|
237
|
-
|
|
237
|
+
computedCommandsNewPublicKey[i],
|
|
238
238
|
computedCommandsVoteOptionIndex[i],
|
|
239
239
|
computedCommandsNewVoteWeight[i],
|
|
240
240
|
computedCommandsNonce[i],
|
|
241
241
|
computedCommandsPollId[i],
|
|
242
242
|
computedCommandsSalt[i],
|
|
243
|
-
|
|
244
|
-
|
|
243
|
+
computedCommandsSignaturePoint[i],
|
|
244
|
+
computedCommandsSignatureScalar[i],
|
|
245
245
|
computedCommandsPackedCommandOut[i],
|
|
246
246
|
voteOptions
|
|
247
247
|
);
|
|
@@ -266,24 +266,24 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
266
266
|
// Constants defining the structure and size of state and ballots.
|
|
267
267
|
var STATE_LEAF_LENGTH = 3;
|
|
268
268
|
var BALLOT_LENGTH = 2;
|
|
269
|
-
var
|
|
270
|
-
var
|
|
269
|
+
var MESSAGE_LENGTH = 10;
|
|
270
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
271
271
|
var VOTE_OPTION_TREE_ARITY = 5;
|
|
272
272
|
var STATE_TREE_ARITY = 2;
|
|
273
|
-
var
|
|
274
|
-
// Ballot vote option (
|
|
275
|
-
var
|
|
273
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
274
|
+
// Ballot vote option (vote option) root index.
|
|
275
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
276
276
|
|
|
277
277
|
// Indices for elements within a state leaf.
|
|
278
278
|
// Public key.
|
|
279
|
-
var
|
|
280
|
-
var
|
|
279
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
280
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
281
281
|
// Voice Credit balance.
|
|
282
|
-
var
|
|
283
|
-
var
|
|
282
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
283
|
+
var NUMBER_BITS = 252;
|
|
284
284
|
|
|
285
285
|
// Number of users that have completed the sign up.
|
|
286
|
-
signal input
|
|
286
|
+
signal input totalSignups;
|
|
287
287
|
// The current value of the state tree root.
|
|
288
288
|
signal input currentStateRoot;
|
|
289
289
|
// The current value of the ballot tree root.
|
|
@@ -305,16 +305,16 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
305
305
|
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
306
306
|
|
|
307
307
|
// Inputs related to the command being processed.
|
|
308
|
-
signal input
|
|
309
|
-
signal input
|
|
310
|
-
signal input
|
|
311
|
-
signal input
|
|
312
|
-
signal input
|
|
313
|
-
signal input
|
|
314
|
-
signal input
|
|
315
|
-
signal input
|
|
316
|
-
signal input
|
|
317
|
-
signal input
|
|
308
|
+
signal input commandStateIndex;
|
|
309
|
+
signal input commandPublicKey[2];
|
|
310
|
+
signal input commandVoteOptionIndex;
|
|
311
|
+
signal input commandNewVoteWeight;
|
|
312
|
+
signal input commandNonce;
|
|
313
|
+
signal input commandPollId;
|
|
314
|
+
signal input commandSalt;
|
|
315
|
+
signal input commandSignaturePoint[2];
|
|
316
|
+
signal input commandSignatureScalar;
|
|
317
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
318
318
|
|
|
319
319
|
// The number of valid vote options for the poll.
|
|
320
320
|
signal input voteOptions;
|
|
@@ -325,37 +325,37 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
325
325
|
// Intermediate signals.
|
|
326
326
|
// currentVoteWeight * currentVoteWeight.
|
|
327
327
|
signal currentVoteWeightSquare;
|
|
328
|
-
//
|
|
329
|
-
signal
|
|
330
|
-
// equal to
|
|
328
|
+
// commandNewVoteWeight * commandNewVoteWeight.
|
|
329
|
+
signal commandNewVoteWeightSquare;
|
|
330
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
331
331
|
signal newBallotVoRoot;
|
|
332
332
|
|
|
333
333
|
// 1. Transform a state leaf and a ballot with a command.
|
|
334
334
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
335
|
-
var
|
|
336
|
-
(
|
|
337
|
-
|
|
335
|
+
var computedNewstateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
336
|
+
(computedNewstateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
337
|
+
totalSignups,
|
|
338
338
|
voteOptions,
|
|
339
|
-
[stateLeaf[
|
|
340
|
-
stateLeaf[
|
|
341
|
-
ballot[
|
|
339
|
+
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
340
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
341
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
342
342
|
currentVoteWeight,
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
343
|
+
commandStateIndex,
|
|
344
|
+
commandPublicKey,
|
|
345
|
+
commandVoteOptionIndex,
|
|
346
|
+
commandNewVoteWeight,
|
|
347
|
+
commandNonce,
|
|
348
|
+
commandPollId,
|
|
349
|
+
commandSalt,
|
|
350
|
+
commandSignaturePoint,
|
|
351
|
+
commandSignatureScalar,
|
|
352
|
+
packedCommand
|
|
353
353
|
);
|
|
354
354
|
|
|
355
355
|
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
356
356
|
// Otherwise, generate indices for command.stateIndex.
|
|
357
|
-
var stateIndexMux = Mux1()([0,
|
|
358
|
-
var computedStateLeafPathIndices[stateTreeDepth] =
|
|
357
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
358
|
+
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
359
359
|
|
|
360
360
|
// 3. Verify that the original state leaf exists in the given state root.
|
|
361
361
|
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
@@ -370,8 +370,8 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
370
370
|
|
|
371
371
|
// 4. Verify that the original ballot exists in the given ballot root.
|
|
372
372
|
var computedBallot = PoseidonHasher(2)([
|
|
373
|
-
ballot[
|
|
374
|
-
ballot[
|
|
373
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
374
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
375
375
|
]);
|
|
376
376
|
|
|
377
377
|
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
@@ -383,12 +383,12 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
383
383
|
computedBallotQip === currentBallotRoot;
|
|
384
384
|
|
|
385
385
|
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
386
|
-
// at
|
|
386
|
+
// at commandVoteOptionIndex.
|
|
387
387
|
currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
|
|
388
|
-
|
|
388
|
+
commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
|
|
389
389
|
|
|
390
|
-
var
|
|
391
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(
|
|
390
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
391
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
392
392
|
|
|
393
393
|
var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
394
394
|
currentVoteWeight,
|
|
@@ -396,13 +396,13 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
396
396
|
currentVoteWeightsPathElements
|
|
397
397
|
);
|
|
398
398
|
|
|
399
|
-
computedCurrentVoteWeightQip === ballot[
|
|
399
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
400
400
|
|
|
401
|
-
var voteWeightMux = Mux1()([currentVoteWeight,
|
|
401
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
402
402
|
var voiceCreditBalanceMux = Mux1()(
|
|
403
403
|
[
|
|
404
|
-
stateLeaf[
|
|
405
|
-
stateLeaf[
|
|
404
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
405
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeightSquare - commandNewVoteWeightSquare
|
|
406
406
|
],
|
|
407
407
|
computedIsValid
|
|
408
408
|
);
|
|
@@ -415,17 +415,17 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
415
415
|
);
|
|
416
416
|
|
|
417
417
|
// The new vote option root in the ballot
|
|
418
|
-
var
|
|
419
|
-
[ballot[
|
|
418
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
419
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
420
420
|
computedIsValid
|
|
421
421
|
);
|
|
422
422
|
|
|
423
|
-
newBallotVoRoot <==
|
|
423
|
+
newBallotVoRoot <== newBallotVoteOptionRootMux;
|
|
424
424
|
|
|
425
425
|
// 6. Generate a new state root.
|
|
426
426
|
var computedNewStateLeafhash = PoseidonHasher(3)([
|
|
427
|
-
|
|
428
|
-
|
|
427
|
+
computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
428
|
+
computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
429
429
|
voiceCreditBalanceMux
|
|
430
430
|
]);
|
|
431
431
|
|
|
@@ -6,11 +6,11 @@ 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
11
|
include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
12
|
-
include "../../utils/
|
|
13
|
-
include "../../utils/
|
|
12
|
+
include "../../utils/CalculateTotal.circom";
|
|
13
|
+
include "../../utils/PoseidonHasher.circom";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Processes batches of votes and verifies their validity in a Merkle tree structure.
|
|
@@ -35,16 +35,16 @@ template TallyVotes(
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
36
|
var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
|
|
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_INT_TREE_DEPTH_DIFFERENCE = stateTreeDepth - intStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -61,13 +61,13 @@ template TallyVotes(
|
|
|
61
61
|
// Start index of given batch
|
|
62
62
|
signal input index;
|
|
63
63
|
// Number of users that signup
|
|
64
|
-
signal input
|
|
64
|
+
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
68
|
-
signal input votes[batchSize][
|
|
67
|
+
signal input ballotPathElements[STATE_INT_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
|
+
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
|
-
signal input currentResults[
|
|
70
|
+
signal input currentResults[totalVoteOptions];
|
|
71
71
|
// Salt for the root of the current results.
|
|
72
72
|
signal input currentResultsRootSalt;
|
|
73
73
|
// Total voice credits spent so far.
|
|
@@ -75,7 +75,7 @@ template TallyVotes(
|
|
|
75
75
|
// Salt for the total spent voice credits.
|
|
76
76
|
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
77
77
|
// Spent voice credits per vote option.
|
|
78
|
-
signal input currentPerVOSpentVoiceCredits[
|
|
78
|
+
signal input currentPerVOSpentVoiceCredits[totalVoteOptions];
|
|
79
79
|
// Salt for the root of spent credits per option.
|
|
80
80
|
signal input currentPerVOSpentVoiceCreditsRootSalt;
|
|
81
81
|
// Salt for the root of the new results.
|
|
@@ -90,21 +90,21 @@ template TallyVotes(
|
|
|
90
90
|
computedSbCommitment === sbCommitment;
|
|
91
91
|
|
|
92
92
|
// Validates that the index is within the valid range of sign-ups.
|
|
93
|
-
var
|
|
94
|
-
|
|
93
|
+
var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
|
|
94
|
+
totalSignupsValid === 1;
|
|
95
95
|
|
|
96
96
|
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
97
97
|
var computedBallotHashers[batchSize];
|
|
98
98
|
|
|
99
99
|
for (var i = 0; i < batchSize; i++) {
|
|
100
|
-
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][
|
|
100
|
+
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
|
|
104
|
-
var computedBallotPathIndices[
|
|
104
|
+
var computedBallotPathIndices[STATE_INT_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_INT_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
105
105
|
|
|
106
106
|
// Verifies each ballot's existence within the ballot tree.
|
|
107
|
-
LeafExists(
|
|
107
|
+
LeafExists(STATE_INT_TREE_DEPTH_DIFFERENCE)(
|
|
108
108
|
computedBallotSubroot,
|
|
109
109
|
ballotPathElements,
|
|
110
110
|
computedBallotPathIndices,
|
|
@@ -115,7 +115,7 @@ template TallyVotes(
|
|
|
115
115
|
var computedVoteTree[batchSize];
|
|
116
116
|
for (var i = 0; i < batchSize; i++) {
|
|
117
117
|
computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
118
|
-
computedVoteTree[i] === ballots[i][
|
|
118
|
+
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
@@ -123,8 +123,8 @@ template TallyVotes(
|
|
|
123
123
|
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
124
124
|
|
|
125
125
|
// Tally the new results.
|
|
126
|
-
var computedCalculateTotalResult[
|
|
127
|
-
for (var i = 0; i <
|
|
126
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
127
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
128
128
|
var numsRC[batchSize + 1];
|
|
129
129
|
numsRC[batchSize] = currentResults[i] * computedIsZero;
|
|
130
130
|
for (var j = 0; j < batchSize; j++) {
|
|
@@ -135,20 +135,20 @@ template TallyVotes(
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
// Tally the new spent voice credit total.
|
|
138
|
-
var numsSVC[batchSize *
|
|
139
|
-
numsSVC[batchSize *
|
|
138
|
+
var numsSVC[batchSize * totalVoteOptions + 1];
|
|
139
|
+
numsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
140
140
|
for (var i = 0; i < batchSize; i++) {
|
|
141
|
-
for (var j = 0; j <
|
|
142
|
-
numsSVC[i *
|
|
141
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
142
|
+
numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize *
|
|
146
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(numsSVC);
|
|
147
147
|
|
|
148
148
|
// Tally the spent voice credits per vote option.
|
|
149
|
-
var computedNewPerVOSpentVoiceCredits[
|
|
149
|
+
var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
|
|
150
150
|
|
|
151
|
-
for (var i = 0; i <
|
|
151
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
152
152
|
var computedNumsSVC[batchSize + 1];
|
|
153
153
|
computedNumsSVC[batchSize] = currentPerVOSpentVoiceCredits[i] * computedIsZero;
|
|
154
154
|
for (var j = 0; j < batchSize; j++) {
|
|
@@ -187,7 +187,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
187
187
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
188
188
|
var TREE_ARITY = 5;
|
|
189
189
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
190
|
-
var
|
|
190
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
191
191
|
|
|
192
192
|
// Equal to 1 if this is the first batch, otherwise 0.
|
|
193
193
|
signal input isFirstBatch;
|
|
@@ -197,12 +197,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
197
197
|
signal input newTallyCommitment;
|
|
198
198
|
|
|
199
199
|
// Current results for each vote option.
|
|
200
|
-
signal input currentResults[
|
|
200
|
+
signal input currentResults[totalVoteOptions];
|
|
201
201
|
// Salt for the root of the current results.
|
|
202
202
|
signal input currentResultsRootSalt;
|
|
203
203
|
|
|
204
204
|
// New results for each vote option.
|
|
205
|
-
signal input newResults[
|
|
205
|
+
signal input newResults[totalVoteOptions];
|
|
206
206
|
// Salt for the root of the new results.
|
|
207
207
|
signal input newResultsRootSalt;
|
|
208
208
|
|
|
@@ -217,12 +217,12 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
217
217
|
signal input newSpentVoiceCreditSubtotalSalt;
|
|
218
218
|
|
|
219
219
|
// Spent voice credits per vote option.
|
|
220
|
-
signal input currentPerVOSpentVoiceCredits[
|
|
220
|
+
signal input currentPerVOSpentVoiceCredits[totalVoteOptions];
|
|
221
221
|
// Salt for the root of spent credits per option.
|
|
222
222
|
signal input currentPerVOSpentVoiceCreditsRootSalt;
|
|
223
223
|
|
|
224
224
|
// New spent voice credits per vote option.
|
|
225
|
-
signal input newPerVOSpentVoiceCredits[
|
|
225
|
+
signal input newPerVOSpentVoiceCredits[totalVoteOptions];
|
|
226
226
|
// Salt for the root of new spent credits per option.
|
|
227
227
|
signal input newPerVOSpentVoiceCreditsRootSalt;
|
|
228
228
|
|
|
@@ -251,11 +251,11 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
251
251
|
// computedIsZero.out is 0 if this is the first batch.
|
|
252
252
|
var computedIsZero = IsZero()(isFirstBatch);
|
|
253
253
|
|
|
254
|
-
//
|
|
255
|
-
//
|
|
256
|
-
signal
|
|
257
|
-
|
|
258
|
-
|
|
254
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
255
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
256
|
+
signal isFirstCommitment;
|
|
257
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
258
|
+
isFirstCommitment === currentTallyCommitment;
|
|
259
259
|
|
|
260
260
|
// Compute the root of the new results.
|
|
261
261
|
var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
|