@maci-protocol/circuits 0.0.0-ci.fb960ed → 0.0.0-ci.fd5247e
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 +4 -4
- package/circom/coordinator/non-qv/processMessages.circom +89 -92
- package/circom/coordinator/non-qv/tallyVotes.circom +33 -30
- package/circom/coordinator/qv/processMessages.circom +90 -90
- package/circom/coordinator/qv/tallyVotes.circom +52 -52
- package/circom/utils/PrivateToPublicKey.circom +3 -3
- package/circom/utils/full/MessageValidator.circom +93 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/{messageValidator.circom → MessageValidator.circom} +13 -11
- package/circom/utils/non-qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +34 -34
- package/circom/utils/qv/{messageValidator.circom → MessageValidator.circom} +13 -11
- package/circom/utils/qv/{stateLeafAndBallotTransformer.circom → StateLeafAndBallotTransformer.circom} +34 -34
- package/circom/utils/trees/{MerkleGeneratePathIndices.circom → MerklePathIndicesGenerator.circom} +1 -1
- package/circom/voter/PollJoined.circom +2 -2
- package/package.json +10 -8
- package/build/ts/genZkeys.d.ts.map +0 -1
- package/build/ts/genZkeys.js.map +0 -1
package/circom/circuits.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"template": "ProcessMessages",
|
|
17
17
|
"params": [10, 20, 2],
|
|
18
18
|
"pubs": [
|
|
19
|
-
"
|
|
19
|
+
"totalSignups",
|
|
20
20
|
"index",
|
|
21
21
|
"batchEndIndex",
|
|
22
22
|
"currentSbCommitment",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"template": "ProcessMessagesNonQv",
|
|
33
33
|
"params": [10, 20, 2],
|
|
34
34
|
"pubs": [
|
|
35
|
-
"
|
|
35
|
+
"totalSignups",
|
|
36
36
|
"index",
|
|
37
37
|
"batchEndIndex",
|
|
38
38
|
"currentSbCommitment",
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"file": "./coordinator/qv/tallyVotes",
|
|
48
48
|
"template": "TallyVotes",
|
|
49
49
|
"params": [10, 1, 2],
|
|
50
|
-
"pubs": ["index", "
|
|
50
|
+
"pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
|
|
51
51
|
},
|
|
52
52
|
"TallyVotesNonQv_10-1-2_test": {
|
|
53
53
|
"file": "./coordinator/non-qv/tallyVotes",
|
|
54
54
|
"template": "TallyVotesNonQv",
|
|
55
55
|
"params": [10, 1, 2],
|
|
56
|
-
"pubs": ["index", "
|
|
56
|
+
"pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -9,11 +9,11 @@ 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
18
|
include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
19
19
|
|
|
@@ -35,27 +35,27 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
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 messages[batchSize][
|
|
58
|
+
signal input messages[batchSize][MESSAGE_LENGTH];
|
|
59
59
|
// The coordinator's private key.
|
|
60
60
|
signal input coordinatorPrivateKey;
|
|
61
61
|
// The ECDH public key per message.
|
|
@@ -113,31 +113,28 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
113
113
|
// signals (for processing purposes).
|
|
114
114
|
signal stateRoots[batchSize + 1];
|
|
115
115
|
signal ballotRoots[batchSize + 1];
|
|
116
|
-
signal tmpStateRoot1[batchSize];
|
|
117
|
-
signal tmpStateRoot2[batchSize];
|
|
118
|
-
signal tmpBallotRoot1[batchSize];
|
|
119
|
-
signal tmpBallotRoot2[batchSize];
|
|
120
116
|
|
|
121
117
|
// Must verify the current sb commitment.
|
|
122
118
|
var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
|
|
123
119
|
computedCurrentSbCommitment === currentSbCommitment;
|
|
124
120
|
|
|
125
121
|
// -----------------------------------------------------------------------
|
|
126
|
-
|
|
122
|
+
// 0. Ensure that the maximum vote options signal is valid and if
|
|
127
123
|
// the maximum users signal is valid
|
|
128
124
|
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
129
125
|
voteOptionsValid === 1;
|
|
130
126
|
|
|
131
|
-
// Check
|
|
127
|
+
// Check totalSignups <= the max number of users (i.e., number of state leaves
|
|
132
128
|
// that can fit the state tree).
|
|
133
|
-
var
|
|
134
|
-
|
|
129
|
+
var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
|
|
130
|
+
totalSignupsValid === 1;
|
|
135
131
|
|
|
136
132
|
// Hash each Message to check their existence in the Message chain hash.
|
|
137
133
|
var computedMessageHashers[batchSize];
|
|
138
134
|
var computedChainHashes[batchSize];
|
|
139
135
|
var chainHash[batchSize + 1];
|
|
140
136
|
chainHash[0] = inputBatchHash;
|
|
137
|
+
|
|
141
138
|
for (var i = 0; i < batchSize; i++) {
|
|
142
139
|
// calculate message hash
|
|
143
140
|
computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
|
|
@@ -165,36 +162,36 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
165
162
|
// Ensure that the coordinator's public key from the contract is correct
|
|
166
163
|
// based on the given private key - that is, the prover knows the
|
|
167
164
|
// coordinator's private key.
|
|
168
|
-
var
|
|
169
|
-
var
|
|
170
|
-
|
|
165
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
|
|
166
|
+
var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
|
|
167
|
+
derivedPublicKeyHash === coordinatorPublicKeyHash;
|
|
171
168
|
|
|
172
169
|
// Decrypt each Message into a Command.
|
|
173
170
|
// The command i-th is composed by the following fields.
|
|
174
171
|
// e.g., command 0 is made of commandsStateIndex[0],
|
|
175
|
-
//
|
|
172
|
+
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
176
173
|
var computedCommandsStateIndex[batchSize];
|
|
177
|
-
var
|
|
174
|
+
var computedCommandsNewPublicKey[batchSize][2];
|
|
178
175
|
var computedCommandsVoteOptionIndex[batchSize];
|
|
179
176
|
var computedCommandsNewVoteWeight[batchSize];
|
|
180
177
|
var computedCommandsNonce[batchSize];
|
|
181
178
|
var computedCommandsPollId[batchSize];
|
|
182
179
|
var computedCommandsSalt[batchSize];
|
|
183
|
-
var
|
|
184
|
-
var
|
|
185
|
-
var computedCommandsPackedCommandOut[batchSize][
|
|
180
|
+
var computedCommandsSignaturePoint[batchSize][2];
|
|
181
|
+
var computedCommandsSignatureScalar[batchSize];
|
|
182
|
+
var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
|
|
186
183
|
|
|
187
184
|
for (var i = 0; i < batchSize; i++) {
|
|
188
185
|
(
|
|
189
186
|
computedCommandsStateIndex[i],
|
|
190
|
-
|
|
187
|
+
computedCommandsNewPublicKey[i],
|
|
191
188
|
computedCommandsVoteOptionIndex[i],
|
|
192
189
|
computedCommandsNewVoteWeight[i],
|
|
193
190
|
computedCommandsNonce[i],
|
|
194
191
|
computedCommandsPollId[i],
|
|
195
192
|
computedCommandsSalt[i],
|
|
196
|
-
|
|
197
|
-
|
|
193
|
+
computedCommandsSignaturePoint[i],
|
|
194
|
+
computedCommandsSignatureScalar[i],
|
|
198
195
|
computedCommandsPackedCommandOut[i]
|
|
199
196
|
) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
|
|
200
197
|
}
|
|
@@ -229,7 +226,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
229
226
|
}
|
|
230
227
|
|
|
231
228
|
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
|
|
232
|
-
|
|
229
|
+
totalSignups,
|
|
233
230
|
stateRoots[i + 1],
|
|
234
231
|
ballotRoots[i + 1],
|
|
235
232
|
actualStateTreeDepth,
|
|
@@ -240,14 +237,14 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
240
237
|
currentVoteWeights[i],
|
|
241
238
|
currentVoteWeightsPathElement,
|
|
242
239
|
computedCommandsStateIndex[i],
|
|
243
|
-
|
|
240
|
+
computedCommandsNewPublicKey[i],
|
|
244
241
|
computedCommandsVoteOptionIndex[i],
|
|
245
242
|
computedCommandsNewVoteWeight[i],
|
|
246
243
|
computedCommandsNonce[i],
|
|
247
244
|
computedCommandsPollId[i],
|
|
248
245
|
computedCommandsSalt[i],
|
|
249
|
-
|
|
250
|
-
|
|
246
|
+
computedCommandsSignaturePoint[i],
|
|
247
|
+
computedCommandsSignatureScalar[i],
|
|
251
248
|
computedCommandsPackedCommandOut[i],
|
|
252
249
|
voteOptions
|
|
253
250
|
);
|
|
@@ -272,24 +269,24 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
272
269
|
// Constants defining the structure and size of state and ballots.
|
|
273
270
|
var STATE_LEAF_LENGTH = 3;
|
|
274
271
|
var BALLOT_LENGTH = 2;
|
|
275
|
-
var
|
|
276
|
-
var
|
|
272
|
+
var MESSAGE_LENGTH = 10;
|
|
273
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
277
274
|
var VOTE_OPTION_TREE_ARITY = 5;
|
|
278
275
|
var STATE_TREE_ARITY = 2;
|
|
279
|
-
var
|
|
280
|
-
// Ballot vote option (
|
|
281
|
-
var
|
|
276
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
277
|
+
// Ballot vote option (vote option) root index.
|
|
278
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
282
279
|
|
|
283
280
|
// Indices for elements within a state leaf.
|
|
284
281
|
// Public key.
|
|
285
|
-
var
|
|
286
|
-
var
|
|
282
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
283
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
287
284
|
// Voice Credit balance.
|
|
288
|
-
var
|
|
289
|
-
var
|
|
285
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
286
|
+
var NUMBER_BITS = 252;
|
|
290
287
|
|
|
291
288
|
// Number of users that have completed the sign up.
|
|
292
|
-
signal input
|
|
289
|
+
signal input totalSignups;
|
|
293
290
|
// The current value of the state tree root.
|
|
294
291
|
signal input currentStateRoot;
|
|
295
292
|
// The current value of the ballot tree root.
|
|
@@ -311,16 +308,16 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
311
308
|
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
312
309
|
|
|
313
310
|
// 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
|
|
311
|
+
signal input commandStateIndex;
|
|
312
|
+
signal input commandPublicKey[2];
|
|
313
|
+
signal input commandVoteOptionIndex;
|
|
314
|
+
signal input commandNewVoteWeight;
|
|
315
|
+
signal input commandNonce;
|
|
316
|
+
signal input commandPollId;
|
|
317
|
+
signal input commandSalt;
|
|
318
|
+
signal input commandSignaturePoint[2];
|
|
319
|
+
signal input commandSignatureScalar;
|
|
320
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
324
321
|
|
|
325
322
|
// The number of valid vote options for the poll.
|
|
326
323
|
signal input voteOptions;
|
|
@@ -328,35 +325,35 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
328
325
|
signal output newStateRoot;
|
|
329
326
|
signal output newBallotRoot;
|
|
330
327
|
|
|
331
|
-
// equal to
|
|
332
|
-
signal
|
|
328
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
329
|
+
signal newBallotVoteOptionRoot;
|
|
333
330
|
|
|
334
331
|
// 1. Transform a state leaf and a ballot with a command.
|
|
335
332
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
336
|
-
var
|
|
337
|
-
(
|
|
338
|
-
|
|
333
|
+
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
334
|
+
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
|
|
335
|
+
totalSignups,
|
|
339
336
|
voteOptions,
|
|
340
|
-
[stateLeaf[
|
|
341
|
-
stateLeaf[
|
|
342
|
-
ballot[
|
|
337
|
+
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
338
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
339
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
343
340
|
currentVoteWeight,
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
341
|
+
commandStateIndex,
|
|
342
|
+
commandPublicKey,
|
|
343
|
+
commandVoteOptionIndex,
|
|
344
|
+
commandNewVoteWeight,
|
|
345
|
+
commandNonce,
|
|
346
|
+
commandPollId,
|
|
347
|
+
commandSalt,
|
|
348
|
+
commandSignaturePoint,
|
|
349
|
+
commandSignatureScalar,
|
|
350
|
+
packedCommand
|
|
354
351
|
);
|
|
355
352
|
|
|
356
353
|
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
357
354
|
// Otherwise, generate indices for command.stateIndex.
|
|
358
|
-
var stateIndexMux = Mux1()([0,
|
|
359
|
-
var computedStateLeafPathIndices[stateTreeDepth] =
|
|
355
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
356
|
+
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
360
357
|
|
|
361
358
|
// 3. Verify that the original state leaf exists in the given state root.
|
|
362
359
|
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
@@ -371,8 +368,8 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
371
368
|
|
|
372
369
|
// 4. Verify that the original ballot exists in the given ballot root.
|
|
373
370
|
var computedBallot = PoseidonHasher(2)([
|
|
374
|
-
ballot[
|
|
375
|
-
ballot[
|
|
371
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
372
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
376
373
|
]);
|
|
377
374
|
|
|
378
375
|
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
@@ -384,9 +381,9 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
384
381
|
computedBallotQip === currentBallotRoot;
|
|
385
382
|
|
|
386
383
|
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
387
|
-
// at
|
|
388
|
-
var
|
|
389
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(
|
|
384
|
+
// at commandVoteOptionIndex.
|
|
385
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
386
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
390
387
|
|
|
391
388
|
var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
392
389
|
currentVoteWeight,
|
|
@@ -394,13 +391,13 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
394
391
|
currentVoteWeightsPathElements
|
|
395
392
|
);
|
|
396
393
|
|
|
397
|
-
computedCurrentVoteWeightQip === ballot[
|
|
394
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
398
395
|
|
|
399
|
-
var voteWeightMux = Mux1()([currentVoteWeight,
|
|
396
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
400
397
|
var voiceCreditBalanceMux = Mux1()(
|
|
401
398
|
[
|
|
402
|
-
stateLeaf[
|
|
403
|
-
stateLeaf[
|
|
399
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
400
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
|
|
404
401
|
],
|
|
405
402
|
computedIsValid
|
|
406
403
|
);
|
|
@@ -413,22 +410,22 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
413
410
|
);
|
|
414
411
|
|
|
415
412
|
// The new vote option root in the ballot
|
|
416
|
-
var
|
|
417
|
-
[ballot[
|
|
413
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
414
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
418
415
|
computedIsValid
|
|
419
416
|
);
|
|
420
417
|
|
|
421
|
-
|
|
418
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
422
419
|
|
|
423
420
|
// 6. Generate a new state root.
|
|
424
|
-
var
|
|
425
|
-
|
|
426
|
-
|
|
421
|
+
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
422
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
423
|
+
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
427
424
|
voiceCreditBalanceMux
|
|
428
425
|
]);
|
|
429
426
|
|
|
430
427
|
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
431
|
-
|
|
428
|
+
computedNewStateLeafHash,
|
|
432
429
|
actualStateTreeDepth,
|
|
433
430
|
computedStateLeafPathIndices,
|
|
434
431
|
stateLeafPathElements
|
|
@@ -437,7 +434,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
437
434
|
newStateRoot <== computedNewStateLeafQip;
|
|
438
435
|
|
|
439
436
|
// 7. Generate a new ballot root.
|
|
440
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce,
|
|
437
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
441
438
|
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
442
439
|
computedNewBallot,
|
|
443
440
|
computedStateLeafPathIndices,
|
|
@@ -6,7 +6,7 @@ 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
12
|
include "../../utils/CalculateTotal.circom";
|
|
@@ -35,16 +35,16 @@ template TallyVotesNonQv(
|
|
|
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 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_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.
|
|
@@ -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
98
|
var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
|
|
99
|
-
var computedBallotPathIndices[
|
|
99
|
+
var computedBallotPathIndices[STATE_INT_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_INT_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
100
100
|
|
|
101
101
|
// Verifies each ballot's existence within the ballot tree.
|
|
102
|
-
LeafExists(
|
|
102
|
+
LeafExists(STATE_INT_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
113
|
computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
113
|
-
computedVoteTree[i] === ballots[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,8 +119,9 @@ template TallyVotesNonQv(
|
|
|
118
119
|
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
119
120
|
|
|
120
121
|
// Tally the new results.
|
|
121
|
-
var computedCalculateTotalResult[
|
|
122
|
-
|
|
122
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
123
|
+
|
|
124
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
123
125
|
var computedNumsRC[batchSize + 1];
|
|
124
126
|
computedNumsRC[batchSize] = currentResults[i] * computedIsZero;
|
|
125
127
|
for (var j = 0; j < batchSize; j++) {
|
|
@@ -130,15 +132,16 @@ template TallyVotesNonQv(
|
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
// Tally the new spent voice credit total.
|
|
133
|
-
var
|
|
134
|
-
|
|
135
|
+
var computedTotalVoiceCreditSpent[batchSize * totalVoteOptions + 1];
|
|
136
|
+
computedTotalVoiceCreditSpent[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
137
|
+
|
|
135
138
|
for (var i = 0; i < batchSize; i++) {
|
|
136
|
-
for (var j = 0; j <
|
|
137
|
-
|
|
139
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
140
|
+
computedTotalVoiceCreditSpent[i * totalVoteOptions + j] = votes[i][j];
|
|
138
141
|
}
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize *
|
|
144
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedTotalVoiceCreditSpent);
|
|
142
145
|
|
|
143
146
|
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
144
147
|
ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
|
|
@@ -165,7 +168,7 @@ template TallyVotesNonQv(
|
|
|
165
168
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
166
169
|
var TREE_ARITY = 5;
|
|
167
170
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
168
|
-
var
|
|
171
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
169
172
|
|
|
170
173
|
// Equal to 1 if this is the first batch, otherwise 0.
|
|
171
174
|
signal input isFirstBatch;
|
|
@@ -175,12 +178,12 @@ template TallyVotesNonQv(
|
|
|
175
178
|
signal input newTallyCommitment;
|
|
176
179
|
|
|
177
180
|
// Current results for each vote option.
|
|
178
|
-
signal input currentResults[
|
|
181
|
+
signal input currentResults[totalVoteOptions];
|
|
179
182
|
// Salt for the root of the current results.
|
|
180
183
|
signal input currentResultsRootSalt;
|
|
181
184
|
|
|
182
185
|
// New results for each vote option.
|
|
183
|
-
signal input newResults[
|
|
186
|
+
signal input newResults[totalVoteOptions];
|
|
184
187
|
// Salt for the root of the new results.
|
|
185
188
|
signal input newResultsRootSalt;
|
|
186
189
|
|
|
@@ -211,11 +214,11 @@ template TallyVotesNonQv(
|
|
|
211
214
|
// computedIsZero.out is 0 if this is the first batch.
|
|
212
215
|
var computedIsZero = IsZero()(isFirstBatch);
|
|
213
216
|
|
|
214
|
-
//
|
|
215
|
-
//
|
|
216
|
-
signal
|
|
217
|
-
|
|
218
|
-
|
|
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;
|
|
219
222
|
|
|
220
223
|
// Compute the root of the new results.
|
|
221
224
|
var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
|