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