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