@maci-protocol/circuits 0.0.0-ci.f9da2fc → 0.0.0-ci.fc91dc9
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 +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 +98 -95
- package/circom/coordinator/non-qv/tallyVotes.circom +38 -32
- package/circom/coordinator/qv/processMessages.circom +99 -93
- package/circom/coordinator/qv/tallyVotes.circom +39 -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 +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 +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/utils/trees/incrementalMerkleTree.circom +0 -198
- package/circom/voter/poll.circom +0 -91
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,31 +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
|
|
38
|
+
var MESSAGE_LENGTH = 10;
|
|
39
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
35
40
|
var STATE_LEAF_LENGTH = 3;
|
|
36
41
|
var BALLOT_LENGTH = 2;
|
|
37
|
-
var
|
|
38
|
-
var
|
|
39
|
-
var
|
|
40
|
-
var
|
|
41
|
-
var
|
|
42
|
-
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;
|
|
43
48
|
// Number of options for this poll.
|
|
44
49
|
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
45
50
|
|
|
46
51
|
// Number of users that have completed the sign up.
|
|
47
|
-
signal input
|
|
52
|
+
signal input totalSignups;
|
|
48
53
|
// Value of chainHash at beginning of batch
|
|
49
54
|
signal input inputBatchHash;
|
|
50
55
|
// Value of chainHash at end of batch
|
|
51
56
|
signal input outputBatchHash;
|
|
52
57
|
// The messages.
|
|
53
|
-
signal input
|
|
58
|
+
signal input messages[batchSize][MESSAGE_LENGTH];
|
|
54
59
|
// The coordinator's private key.
|
|
55
|
-
signal input
|
|
60
|
+
signal input coordinatorPrivateKey;
|
|
56
61
|
// The ECDH public key per message.
|
|
57
|
-
signal input
|
|
62
|
+
signal input encryptionPublicKeys[batchSize][2];
|
|
58
63
|
// The current state root (before the processing).
|
|
59
64
|
signal input currentStateRoot;
|
|
60
65
|
// The actual tree depth (might be <= stateTreeDepth).
|
|
@@ -108,34 +113,31 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
108
113
|
// signals (for processing purposes).
|
|
109
114
|
signal stateRoots[batchSize + 1];
|
|
110
115
|
signal ballotRoots[batchSize + 1];
|
|
111
|
-
signal tmpStateRoot1[batchSize];
|
|
112
|
-
signal tmpStateRoot2[batchSize];
|
|
113
|
-
signal tmpBallotRoot1[batchSize];
|
|
114
|
-
signal tmpBallotRoot2[batchSize];
|
|
115
116
|
|
|
116
117
|
// Must verify the current sb commitment.
|
|
117
118
|
var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
|
|
118
119
|
computedCurrentSbCommitment === currentSbCommitment;
|
|
119
120
|
|
|
120
121
|
// -----------------------------------------------------------------------
|
|
121
|
-
|
|
122
|
+
// 0. Ensure that the maximum vote options signal is valid and if
|
|
122
123
|
// the maximum users signal is valid
|
|
123
124
|
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
124
125
|
voteOptionsValid === 1;
|
|
125
126
|
|
|
126
|
-
// Check
|
|
127
|
+
// Check totalSignups <= the max number of users (i.e., number of state leaves
|
|
127
128
|
// that can fit the state tree).
|
|
128
|
-
var
|
|
129
|
-
|
|
129
|
+
var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
|
|
130
|
+
totalSignupsValid === 1;
|
|
130
131
|
|
|
131
132
|
// Hash each Message to check their existence in the Message chain hash.
|
|
132
133
|
var computedMessageHashers[batchSize];
|
|
133
134
|
var computedChainHashes[batchSize];
|
|
134
135
|
var chainHash[batchSize + 1];
|
|
135
136
|
chainHash[0] = inputBatchHash;
|
|
137
|
+
|
|
136
138
|
for (var i = 0; i < batchSize; i++) {
|
|
137
139
|
// calculate message hash
|
|
138
|
-
computedMessageHashers[i] = MessageHasher()(
|
|
140
|
+
computedMessageHashers[i] = MessageHasher()(messages[i], encryptionPublicKeys[i]);
|
|
139
141
|
// check if message is valid or not (if index of message is less than index of last valid message in batch)
|
|
140
142
|
var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
|
|
141
143
|
// calculate chain hash if message is valid
|
|
@@ -160,38 +162,38 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
160
162
|
// Ensure that the coordinator's public key from the contract is correct
|
|
161
163
|
// based on the given private key - that is, the prover knows the
|
|
162
164
|
// coordinator's private key.
|
|
163
|
-
var
|
|
164
|
-
var
|
|
165
|
-
|
|
165
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
|
|
166
|
+
var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
|
|
167
|
+
derivedPublicKeyHash === coordinatorPublicKeyHash;
|
|
166
168
|
|
|
167
169
|
// Decrypt each Message into a Command.
|
|
168
170
|
// The command i-th is composed by the following fields.
|
|
169
171
|
// e.g., command 0 is made of commandsStateIndex[0],
|
|
170
|
-
//
|
|
172
|
+
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
171
173
|
var computedCommandsStateIndex[batchSize];
|
|
172
|
-
var
|
|
174
|
+
var computedCommandsNewPublicKey[batchSize][2];
|
|
173
175
|
var computedCommandsVoteOptionIndex[batchSize];
|
|
174
176
|
var computedCommandsNewVoteWeight[batchSize];
|
|
175
177
|
var computedCommandsNonce[batchSize];
|
|
176
178
|
var computedCommandsPollId[batchSize];
|
|
177
179
|
var computedCommandsSalt[batchSize];
|
|
178
|
-
var
|
|
179
|
-
var
|
|
180
|
-
var computedCommandsPackedCommandOut[batchSize][
|
|
180
|
+
var computedCommandsSignaturePoint[batchSize][2];
|
|
181
|
+
var computedCommandsSignatureScalar[batchSize];
|
|
182
|
+
var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
|
|
181
183
|
|
|
182
184
|
for (var i = 0; i < batchSize; i++) {
|
|
183
185
|
(
|
|
184
186
|
computedCommandsStateIndex[i],
|
|
185
|
-
|
|
187
|
+
computedCommandsNewPublicKey[i],
|
|
186
188
|
computedCommandsVoteOptionIndex[i],
|
|
187
189
|
computedCommandsNewVoteWeight[i],
|
|
188
190
|
computedCommandsNonce[i],
|
|
189
191
|
computedCommandsPollId[i],
|
|
190
192
|
computedCommandsSalt[i],
|
|
191
|
-
|
|
192
|
-
|
|
193
|
+
computedCommandsSignaturePoint[i],
|
|
194
|
+
computedCommandsSignatureScalar[i],
|
|
193
195
|
computedCommandsPackedCommandOut[i]
|
|
194
|
-
) = MessageToCommand()(
|
|
196
|
+
) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
|
|
195
197
|
}
|
|
196
198
|
|
|
197
199
|
// Process messages in reverse order.
|
|
@@ -224,7 +226,7 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
224
226
|
}
|
|
225
227
|
|
|
226
228
|
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth)(
|
|
227
|
-
|
|
229
|
+
totalSignups,
|
|
228
230
|
stateRoots[i + 1],
|
|
229
231
|
ballotRoots[i + 1],
|
|
230
232
|
actualStateTreeDepth,
|
|
@@ -235,14 +237,14 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
235
237
|
currentVoteWeights[i],
|
|
236
238
|
currentVoteWeightsPathElement,
|
|
237
239
|
computedCommandsStateIndex[i],
|
|
238
|
-
|
|
240
|
+
computedCommandsNewPublicKey[i],
|
|
239
241
|
computedCommandsVoteOptionIndex[i],
|
|
240
242
|
computedCommandsNewVoteWeight[i],
|
|
241
243
|
computedCommandsNonce[i],
|
|
242
244
|
computedCommandsPollId[i],
|
|
243
245
|
computedCommandsSalt[i],
|
|
244
|
-
|
|
245
|
-
|
|
246
|
+
computedCommandsSignaturePoint[i],
|
|
247
|
+
computedCommandsSignatureScalar[i],
|
|
246
248
|
computedCommandsPackedCommandOut[i],
|
|
247
249
|
voteOptions
|
|
248
250
|
);
|
|
@@ -267,24 +269,24 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
267
269
|
// Constants defining the structure and size of state and ballots.
|
|
268
270
|
var STATE_LEAF_LENGTH = 3;
|
|
269
271
|
var BALLOT_LENGTH = 2;
|
|
270
|
-
var
|
|
271
|
-
var
|
|
272
|
+
var MESSAGE_LENGTH = 10;
|
|
273
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
272
274
|
var VOTE_OPTION_TREE_ARITY = 5;
|
|
273
275
|
var STATE_TREE_ARITY = 2;
|
|
274
|
-
var
|
|
275
|
-
// Ballot vote option (
|
|
276
|
-
var
|
|
276
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
277
|
+
// Ballot vote option (vote option) root index.
|
|
278
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
277
279
|
|
|
278
280
|
// Indices for elements within a state leaf.
|
|
279
281
|
// Public key.
|
|
280
|
-
var
|
|
281
|
-
var
|
|
282
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
283
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
282
284
|
// Voice Credit balance.
|
|
283
|
-
var
|
|
284
|
-
var
|
|
285
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
286
|
+
var NUMBER_BITS = 252;
|
|
285
287
|
|
|
286
288
|
// Number of users that have completed the sign up.
|
|
287
|
-
signal input
|
|
289
|
+
signal input totalSignups;
|
|
288
290
|
// The current value of the state tree root.
|
|
289
291
|
signal input currentStateRoot;
|
|
290
292
|
// The current value of the ballot tree root.
|
|
@@ -306,16 +308,16 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
306
308
|
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
307
309
|
|
|
308
310
|
// Inputs related to the command being processed.
|
|
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
|
|
318
|
-
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];
|
|
319
321
|
|
|
320
322
|
// The number of valid vote options for the poll.
|
|
321
323
|
signal input voteOptions;
|
|
@@ -323,35 +325,35 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
323
325
|
signal output newStateRoot;
|
|
324
326
|
signal output newBallotRoot;
|
|
325
327
|
|
|
326
|
-
// equal to
|
|
328
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
327
329
|
signal newBallotVoRoot;
|
|
328
330
|
|
|
329
331
|
// 1. Transform a state leaf and a ballot with a command.
|
|
330
332
|
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
331
|
-
var
|
|
332
|
-
(
|
|
333
|
-
|
|
333
|
+
var computedNewstateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
334
|
+
(computedNewstateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerNonQv()(
|
|
335
|
+
totalSignups,
|
|
334
336
|
voteOptions,
|
|
335
|
-
[stateLeaf[
|
|
336
|
-
stateLeaf[
|
|
337
|
-
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],
|
|
338
340
|
currentVoteWeight,
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
341
|
+
commandStateIndex,
|
|
342
|
+
commandPublicKey,
|
|
343
|
+
commandVoteOptionIndex,
|
|
344
|
+
commandNewVoteWeight,
|
|
345
|
+
commandNonce,
|
|
346
|
+
commandPollId,
|
|
347
|
+
commandSalt,
|
|
348
|
+
commandSignaturePoint,
|
|
349
|
+
commandSignatureScalar,
|
|
350
|
+
packedCommand
|
|
349
351
|
);
|
|
350
352
|
|
|
351
353
|
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
352
354
|
// Otherwise, generate indices for command.stateIndex.
|
|
353
|
-
var stateIndexMux = Mux1()([0,
|
|
354
|
-
var computedStateLeafPathIndices[stateTreeDepth] =
|
|
355
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
356
|
+
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
355
357
|
|
|
356
358
|
// 3. Verify that the original state leaf exists in the given state root.
|
|
357
359
|
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
@@ -366,8 +368,8 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
366
368
|
|
|
367
369
|
// 4. Verify that the original ballot exists in the given ballot root.
|
|
368
370
|
var computedBallot = PoseidonHasher(2)([
|
|
369
|
-
ballot[
|
|
370
|
-
ballot[
|
|
371
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
372
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
371
373
|
]);
|
|
372
374
|
|
|
373
375
|
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
@@ -379,9 +381,9 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
379
381
|
computedBallotQip === currentBallotRoot;
|
|
380
382
|
|
|
381
383
|
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
382
|
-
// at
|
|
383
|
-
var
|
|
384
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(
|
|
384
|
+
// at commandVoteOptionIndex.
|
|
385
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
386
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
385
387
|
|
|
386
388
|
var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
387
389
|
currentVoteWeight,
|
|
@@ -389,13 +391,13 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
389
391
|
currentVoteWeightsPathElements
|
|
390
392
|
);
|
|
391
393
|
|
|
392
|
-
computedCurrentVoteWeightQip === ballot[
|
|
394
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
393
395
|
|
|
394
|
-
var voteWeightMux = Mux1()([currentVoteWeight,
|
|
396
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
395
397
|
var voiceCreditBalanceMux = Mux1()(
|
|
396
398
|
[
|
|
397
|
-
stateLeaf[
|
|
398
|
-
stateLeaf[
|
|
399
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
400
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
|
|
399
401
|
],
|
|
400
402
|
computedIsValid
|
|
401
403
|
);
|
|
@@ -408,17 +410,17 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
408
410
|
);
|
|
409
411
|
|
|
410
412
|
// The new vote option root in the ballot
|
|
411
|
-
var
|
|
412
|
-
[ballot[
|
|
413
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
414
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
413
415
|
computedIsValid
|
|
414
416
|
);
|
|
415
417
|
|
|
416
|
-
newBallotVoRoot <==
|
|
418
|
+
newBallotVoRoot <== newBallotVoteOptionRootMux;
|
|
417
419
|
|
|
418
420
|
// 6. Generate a new state root.
|
|
419
421
|
var computedNewStateLeafhash = PoseidonHasher(3)([
|
|
420
|
-
|
|
421
|
-
|
|
422
|
+
computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
423
|
+
computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
422
424
|
voiceCreditBalanceMux
|
|
423
425
|
]);
|
|
424
426
|
|
|
@@ -441,3 +443,4 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
441
443
|
|
|
442
444
|
newBallotRoot <== computedNewBallotQip;
|
|
443
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 computedNumsSVC[batchSize *
|
|
132
|
-
computedNumsSVC[batchSize *
|
|
135
|
+
var computedNumsSVC[batchSize * totalVoteOptions + 1];
|
|
136
|
+
computedNumsSVC[batchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
137
|
+
|
|
133
138
|
for (var i = 0; i < batchSize; i++) {
|
|
134
|
-
for (var j = 0; j <
|
|
135
|
-
computedNumsSVC[i *
|
|
139
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
140
|
+
computedNumsSVC[i * totalVoteOptions + j] = votes[i][j];
|
|
136
141
|
}
|
|
137
142
|
}
|
|
138
143
|
|
|
139
|
-
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize *
|
|
144
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * totalVoteOptions + 1)(computedNumsSVC);
|
|
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
|
+
|