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