@maci-protocol/circuits 0.0.0-ci.77d0530 → 0.0.0-ci.799d56f
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/README.md +2 -2
- package/build/ts/types.d.ts +4 -4
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +28 -12
- package/circom/coordinator/full/MessageProcessor.circom +245 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
- package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +17 -219
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +197 -0
- package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +18 -95
- package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +18 -227
- package/circom/coordinator/qv/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/qv/{tallyVotes.circom → VoteTally.circom} +14 -114
- package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/EdDSAPoseidonVerifier.circom +8 -3
- package/circom/utils/IsOnCurve.circom +40 -0
- package/circom/utils/full/MessageValidator.circom +2 -4
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +5 -0
- package/circom/utils/non-qv/MessageValidator.circom +2 -2
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +5 -0
- package/circom/utils/qv/MessageValidator.circom +2 -2
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +5 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/{MerklePathIndicesGenerator.circom → QuinaryGeneratePathIndices.circom} +18 -18
- 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 +3 -3
- package/circom/voter/PollJoining.circom +3 -3
- package/package.json +17 -16
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -5,10 +5,10 @@ include "./comparators.circom";
|
|
|
5
5
|
// zk-kit import
|
|
6
6
|
include "./unpack-element.circom";
|
|
7
7
|
// local imports
|
|
8
|
+
include "../../utils/non-qv/ResultCommitmentVerifier.circom";
|
|
8
9
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
|
-
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
10
10
|
include "../../utils/trees/LeafExists.circom";
|
|
11
|
-
include "../../utils/trees/
|
|
11
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
12
12
|
include "../../utils/CalculateTotal.circom";
|
|
13
13
|
include "../../utils/PoseidonHasher.circom";
|
|
14
14
|
|
|
@@ -16,24 +16,24 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
16
16
|
* Processes batches of votes and verifies their validity in a Merkle tree structure.
|
|
17
17
|
* This template does not support Quadratic Voting (QV).
|
|
18
18
|
*/
|
|
19
|
-
template
|
|
19
|
+
template VoteTallyNonQv(
|
|
20
20
|
stateTreeDepth,
|
|
21
|
-
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
22
|
voteOptionTreeDepth
|
|
23
23
|
) {
|
|
24
24
|
// Ensure there's at least one level in the vote option tree.
|
|
25
25
|
assert(voteOptionTreeDepth > 0);
|
|
26
26
|
// Ensure the intermediate state tree has at least one level.
|
|
27
|
-
assert(
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
28
|
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
-
assert(
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
30
|
|
|
31
31
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
32
|
var TREE_ARITY = 5;
|
|
33
33
|
var BALLOT_TREE_ARITY = 2;
|
|
34
34
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
-
var batchSize = BALLOT_TREE_ARITY **
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
37
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
38
|
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
@@ -44,7 +44,7 @@ template TallyVotesNonQv(
|
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
45
|
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
46
|
// Difference in tree depths, used in path calculations.
|
|
47
|
-
var
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -64,7 +64,7 @@ template TallyVotesNonQv(
|
|
|
64
64
|
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
68
|
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
70
|
signal input currentResults[totalVoteOptions];
|
|
@@ -95,11 +95,11 @@ template TallyVotesNonQv(
|
|
|
95
95
|
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
var computedBallotSubroot = CheckRoot(
|
|
99
|
-
var computedBallotPathIndices[
|
|
98
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
99
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
100
100
|
|
|
101
101
|
// Verifies each ballot's existence within the ballot tree.
|
|
102
|
-
LeafExists(
|
|
102
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
103
103
|
computedBallotSubroot,
|
|
104
104
|
ballotPathElements,
|
|
105
105
|
computedBallotPathIndices,
|
|
@@ -110,7 +110,7 @@ template TallyVotesNonQv(
|
|
|
110
110
|
var computedVoteTree[batchSize];
|
|
111
111
|
|
|
112
112
|
for (var i = 0; i < batchSize; i++) {
|
|
113
|
-
computedVoteTree[i] =
|
|
113
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
114
114
|
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
115
115
|
}
|
|
116
116
|
|
|
@@ -122,13 +122,14 @@ template TallyVotesNonQv(
|
|
|
122
122
|
var computedCalculateTotalResult[totalVoteOptions];
|
|
123
123
|
|
|
124
124
|
for (var i = 0; i < totalVoteOptions; i++) {
|
|
125
|
-
var
|
|
126
|
-
|
|
125
|
+
var computedVotes[batchSize + 1];
|
|
126
|
+
computedVotes[batchSize] = currentResults[i] * computedIsZero;
|
|
127
|
+
|
|
127
128
|
for (var j = 0; j < batchSize; j++) {
|
|
128
|
-
|
|
129
|
+
computedVotes[j] = votes[j][i];
|
|
129
130
|
}
|
|
130
131
|
|
|
131
|
-
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(
|
|
132
|
+
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedVotes);
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
// Tally the new spent voice credit total.
|
|
@@ -158,81 +159,3 @@ template TallyVotesNonQv(
|
|
|
158
159
|
newSpentVoiceCreditSubtotalSalt
|
|
159
160
|
);
|
|
160
161
|
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Performs verifications and computations related to current voting results.
|
|
164
|
-
* Also, computes and outputs a commitment to the new results.
|
|
165
|
-
* This template does not support Quadratic Voting (QV).
|
|
166
|
-
*/
|
|
167
|
-
template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
|
|
168
|
-
// Number of children per node in the tree, defining the tree's branching factor.
|
|
169
|
-
var TREE_ARITY = 5;
|
|
170
|
-
// Number of voting options available, determined by the depth of the vote option tree.
|
|
171
|
-
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
172
|
-
|
|
173
|
-
// Equal to 1 if this is the first batch, otherwise 0.
|
|
174
|
-
signal input isFirstBatch;
|
|
175
|
-
// Commitment to the current tally before this batch.
|
|
176
|
-
signal input currentTallyCommitment;
|
|
177
|
-
// Commitment to the new tally after processing this batch.
|
|
178
|
-
signal input newTallyCommitment;
|
|
179
|
-
|
|
180
|
-
// Current results for each vote option.
|
|
181
|
-
signal input currentResults[totalVoteOptions];
|
|
182
|
-
// Salt for the root of the current results.
|
|
183
|
-
signal input currentResultsRootSalt;
|
|
184
|
-
|
|
185
|
-
// New results for each vote option.
|
|
186
|
-
signal input newResults[totalVoteOptions];
|
|
187
|
-
// Salt for the root of the new results.
|
|
188
|
-
signal input newResultsRootSalt;
|
|
189
|
-
|
|
190
|
-
// Total voice credits spent so far.
|
|
191
|
-
signal input currentSpentVoiceCreditSubtotal;
|
|
192
|
-
// Salt for the total spent voice credits.
|
|
193
|
-
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
194
|
-
|
|
195
|
-
// Total new voice credits spent.
|
|
196
|
-
signal input newSpentVoiceCreditSubtotal;
|
|
197
|
-
// Salt for the new total spent voice credits.
|
|
198
|
-
signal input newSpentVoiceCreditSubtotalSalt;
|
|
199
|
-
|
|
200
|
-
// Compute the commitment to the current results.
|
|
201
|
-
var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
202
|
-
|
|
203
|
-
// Verify currentResultsCommitmentHash.
|
|
204
|
-
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
205
|
-
|
|
206
|
-
// Compute the commitment to the current spent voice credits.
|
|
207
|
-
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
208
|
-
|
|
209
|
-
// Commit to the current tally
|
|
210
|
-
var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
|
|
211
|
-
|
|
212
|
-
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
213
|
-
// computedIsZero.out is 1 if this is not the first batch.
|
|
214
|
-
// computedIsZero.out is 0 if this is the first batch.
|
|
215
|
-
var computedIsZero = IsZero()(isFirstBatch);
|
|
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;
|
|
222
|
-
|
|
223
|
-
// Compute the root of the new results.
|
|
224
|
-
var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
|
|
225
|
-
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
226
|
-
|
|
227
|
-
// Compute the commitment to the new spent voice credits value.
|
|
228
|
-
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
229
|
-
|
|
230
|
-
// Commit to the new tally.
|
|
231
|
-
var computedNewTallyCommitment = PoseidonHasher(2)([
|
|
232
|
-
computedNewResultsCommitment,
|
|
233
|
-
computedNewSpentVoiceCreditsCommitment
|
|
234
|
-
]);
|
|
235
|
-
|
|
236
|
-
computedNewTallyCommitment === newTallyCommitment;
|
|
237
|
-
}
|
|
238
|
-
|
|
@@ -9,19 +9,14 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
9
9
|
include "../../utils/MessageHasher.circom";
|
|
10
10
|
include "../../utils/MessageToCommand.circom";
|
|
11
11
|
include "../../utils/PrivateToPublicKey.circom";
|
|
12
|
-
include "
|
|
13
|
-
|
|
14
|
-
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
15
|
-
include "../../utils/trees/LeafExists.circom";
|
|
16
|
-
include "../../utils/trees/CheckRoot.circom";
|
|
17
|
-
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
18
|
-
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
12
|
+
include "./SingleMessageProcessor.circom";
|
|
13
|
+
|
|
19
14
|
|
|
20
15
|
/**
|
|
21
16
|
* Proves the correctness of processing a batch of MACI messages.
|
|
22
17
|
* This template supports the Quadratic Voting (QV).
|
|
23
18
|
*/
|
|
24
|
-
template
|
|
19
|
+
template MessageProcessorQv(
|
|
25
20
|
stateTreeDepth,
|
|
26
21
|
batchSize,
|
|
27
22
|
voteOptionTreeDepth
|
|
@@ -39,14 +34,6 @@ template ProcessMessages(
|
|
|
39
34
|
var PACKED_COMMAND_LENGTH = 4;
|
|
40
35
|
var STATE_LEAF_LENGTH = 3;
|
|
41
36
|
var BALLOT_LENGTH = 2;
|
|
42
|
-
var BALLOT_NONCE_INDEX = 0;
|
|
43
|
-
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
44
|
-
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
45
|
-
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
46
|
-
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
47
|
-
var MESSAGE_TREE_ZERO_VALUE = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
48
|
-
// Number of options for this poll.
|
|
49
|
-
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
50
37
|
|
|
51
38
|
// Number of users that have completed the sign up.
|
|
52
39
|
signal input totalSignups;
|
|
@@ -63,7 +50,7 @@ template ProcessMessages(
|
|
|
63
50
|
// The current state root (before the processing).
|
|
64
51
|
signal input currentStateRoot;
|
|
65
52
|
// The actual tree depth (might be <= stateTreeDepth).
|
|
66
|
-
// @note it is a public input to ensure fair processing from
|
|
53
|
+
// @note it is a public input to ensure fair processing from
|
|
67
54
|
// the coordinator (no censoring)
|
|
68
55
|
signal input actualStateTreeDepth;
|
|
69
56
|
// The coordinator public key hash
|
|
@@ -103,7 +90,7 @@ template ProcessMessages(
|
|
|
103
90
|
|
|
104
91
|
// The index of the first message in the batch, inclusive.
|
|
105
92
|
signal input index;
|
|
106
|
-
|
|
93
|
+
|
|
107
94
|
// The index of the last message in the batch to process, exclusive.
|
|
108
95
|
// This value may be less than batchSize if this batch is
|
|
109
96
|
// the last batch and the total number of messages is not a multiple of the batch size.
|
|
@@ -165,7 +152,7 @@ template ProcessMessages(
|
|
|
165
152
|
|
|
166
153
|
// Decrypt each Message into a Command.
|
|
167
154
|
// The command i-th is composed by the following fields.
|
|
168
|
-
// e.g., command 0 is made of commandsStateIndex[0],
|
|
155
|
+
// e.g., command 0 is made of commandsStateIndex[0],
|
|
169
156
|
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
170
157
|
var computedCommandsStateIndex[batchSize];
|
|
171
158
|
var computedCommandsNewPublicKey[batchSize][2];
|
|
@@ -205,34 +192,34 @@ template ProcessMessages(
|
|
|
205
192
|
// Start from batchSize and decrement for process in reverse order.
|
|
206
193
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
207
194
|
// Process as vote type message.
|
|
208
|
-
var
|
|
209
|
-
var
|
|
210
|
-
var
|
|
211
|
-
|
|
195
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
196
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
197
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
198
|
+
|
|
212
199
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
213
200
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
214
|
-
|
|
215
|
-
|
|
201
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
202
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
216
203
|
}
|
|
217
204
|
}
|
|
218
205
|
|
|
219
206
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
220
207
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
221
|
-
|
|
208
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
222
209
|
}
|
|
223
210
|
}
|
|
224
|
-
|
|
225
|
-
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) =
|
|
211
|
+
|
|
212
|
+
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorQv(stateTreeDepth, voteOptionTreeDepth)(
|
|
226
213
|
totalSignups,
|
|
227
214
|
stateRoots[i + 1],
|
|
228
215
|
ballotRoots[i + 1],
|
|
229
216
|
actualStateTreeDepth,
|
|
230
217
|
currentStateLeaves[i],
|
|
231
|
-
|
|
218
|
+
computedCurrentStateLeavesPathElements,
|
|
232
219
|
currentBallots[i],
|
|
233
|
-
|
|
220
|
+
computedCurrentBallotPathElements,
|
|
234
221
|
currentVoteWeights[i],
|
|
235
|
-
|
|
222
|
+
computedCurrentVoteWeightsPathElements,
|
|
236
223
|
computedCommandsStateIndex[i],
|
|
237
224
|
computedCommandsNewPublicKey[i],
|
|
238
225
|
computedCommandsVoteOptionIndex[i],
|
|
@@ -253,199 +240,3 @@ template ProcessMessages(
|
|
|
253
240
|
var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
|
|
254
241
|
computedNewSbCommitment === newSbCommitment;
|
|
255
242
|
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Processes one message and updates the state accordingly.
|
|
259
|
-
* This template involves complex interactions, including transformations based on message type,
|
|
260
|
-
* validations against current states like voice credit balances or vote weights,
|
|
261
|
-
* and updates to Merkle trees representing state and ballot information.
|
|
262
|
-
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
263
|
-
* This template supports the Quadratic Voting (QV).
|
|
264
|
-
*/
|
|
265
|
-
template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
266
|
-
// Constants defining the structure and size of state and ballots.
|
|
267
|
-
var STATE_LEAF_LENGTH = 3;
|
|
268
|
-
var BALLOT_LENGTH = 2;
|
|
269
|
-
var MESSAGE_LENGTH = 10;
|
|
270
|
-
var PACKED_COMMAND_LENGTH = 4;
|
|
271
|
-
var VOTE_OPTION_TREE_ARITY = 5;
|
|
272
|
-
var STATE_TREE_ARITY = 2;
|
|
273
|
-
var BALLOT_NONCE_INDEX = 0;
|
|
274
|
-
// Ballot vote option (vote option) root index.
|
|
275
|
-
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
276
|
-
|
|
277
|
-
// Indices for elements within a state leaf.
|
|
278
|
-
// Public key.
|
|
279
|
-
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
280
|
-
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
281
|
-
// Voice Credit balance.
|
|
282
|
-
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
283
|
-
var NUMBER_BITS = 252;
|
|
284
|
-
|
|
285
|
-
// Number of users that have completed the sign up.
|
|
286
|
-
signal input totalSignups;
|
|
287
|
-
// The current value of the state tree root.
|
|
288
|
-
signal input currentStateRoot;
|
|
289
|
-
// The current value of the ballot tree root.
|
|
290
|
-
signal input currentBallotRoot;
|
|
291
|
-
// The actual tree depth (might be <= stateTreeDepth).
|
|
292
|
-
signal input actualStateTreeDepth;
|
|
293
|
-
|
|
294
|
-
// The state leaf and related path elements.
|
|
295
|
-
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
296
|
-
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
297
|
-
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
298
|
-
|
|
299
|
-
// The ballot and related path elements.
|
|
300
|
-
signal input ballot[BALLOT_LENGTH];
|
|
301
|
-
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
302
|
-
|
|
303
|
-
// The current vote weight and related path elements.
|
|
304
|
-
signal input currentVoteWeight;
|
|
305
|
-
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
306
|
-
|
|
307
|
-
// Inputs related to the command being processed.
|
|
308
|
-
signal input commandStateIndex;
|
|
309
|
-
signal input commandPublicKey[2];
|
|
310
|
-
signal input commandVoteOptionIndex;
|
|
311
|
-
signal input commandNewVoteWeight;
|
|
312
|
-
signal input commandNonce;
|
|
313
|
-
signal input commandPollId;
|
|
314
|
-
signal input commandSalt;
|
|
315
|
-
signal input commandSignaturePoint[2];
|
|
316
|
-
signal input commandSignatureScalar;
|
|
317
|
-
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
318
|
-
|
|
319
|
-
// The number of valid vote options for the poll.
|
|
320
|
-
signal input voteOptions;
|
|
321
|
-
|
|
322
|
-
signal output newStateRoot;
|
|
323
|
-
signal output newBallotRoot;
|
|
324
|
-
|
|
325
|
-
// Intermediate signals.
|
|
326
|
-
// currentVoteWeight * currentVoteWeight.
|
|
327
|
-
signal currentVoteWeightSquare;
|
|
328
|
-
// commandNewVoteWeight * commandNewVoteWeight.
|
|
329
|
-
signal commandNewVoteWeightSquare;
|
|
330
|
-
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
331
|
-
signal newBallotVoteOptionRoot;
|
|
332
|
-
|
|
333
|
-
// 1. Transform a state leaf and a ballot with a command.
|
|
334
|
-
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
335
|
-
var computedNewStateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
336
|
-
(computedNewStateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
337
|
-
totalSignups,
|
|
338
|
-
voteOptions,
|
|
339
|
-
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
340
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
341
|
-
ballot[BALLOT_NONCE_INDEX],
|
|
342
|
-
currentVoteWeight,
|
|
343
|
-
commandStateIndex,
|
|
344
|
-
commandPublicKey,
|
|
345
|
-
commandVoteOptionIndex,
|
|
346
|
-
commandNewVoteWeight,
|
|
347
|
-
commandNonce,
|
|
348
|
-
commandPollId,
|
|
349
|
-
commandSalt,
|
|
350
|
-
commandSignaturePoint,
|
|
351
|
-
commandSignatureScalar,
|
|
352
|
-
packedCommand
|
|
353
|
-
);
|
|
354
|
-
|
|
355
|
-
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
356
|
-
// Otherwise, generate indices for command.stateIndex.
|
|
357
|
-
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
358
|
-
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
359
|
-
|
|
360
|
-
// 3. Verify that the original state leaf exists in the given state root.
|
|
361
|
-
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
362
|
-
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
363
|
-
stateLeafHash,
|
|
364
|
-
actualStateTreeDepth,
|
|
365
|
-
computedStateLeafPathIndices,
|
|
366
|
-
stateLeafPathElements
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
stateLeafQip === currentStateRoot;
|
|
370
|
-
|
|
371
|
-
// 4. Verify that the original ballot exists in the given ballot root.
|
|
372
|
-
var computedBallot = PoseidonHasher(2)([
|
|
373
|
-
ballot[BALLOT_NONCE_INDEX],
|
|
374
|
-
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
375
|
-
]);
|
|
376
|
-
|
|
377
|
-
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
378
|
-
computedBallot,
|
|
379
|
-
computedStateLeafPathIndices,
|
|
380
|
-
ballotPathElements
|
|
381
|
-
);
|
|
382
|
-
|
|
383
|
-
computedBallotQip === currentBallotRoot;
|
|
384
|
-
|
|
385
|
-
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
386
|
-
// at commandVoteOptionIndex.
|
|
387
|
-
currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
|
|
388
|
-
commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
|
|
389
|
-
|
|
390
|
-
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
391
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
392
|
-
|
|
393
|
-
var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
394
|
-
currentVoteWeight,
|
|
395
|
-
computedCurrentVoteWeightPathIndices,
|
|
396
|
-
currentVoteWeightsPathElements
|
|
397
|
-
);
|
|
398
|
-
|
|
399
|
-
computedCurrentVoteWeightQip === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
400
|
-
|
|
401
|
-
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
402
|
-
var voiceCreditBalanceMux = Mux1()(
|
|
403
|
-
[
|
|
404
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
405
|
-
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeightSquare - commandNewVoteWeightSquare
|
|
406
|
-
],
|
|
407
|
-
computedIsValid
|
|
408
|
-
);
|
|
409
|
-
|
|
410
|
-
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
411
|
-
var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
412
|
-
voteWeightMux,
|
|
413
|
-
computedCurrentVoteWeightPathIndices,
|
|
414
|
-
currentVoteWeightsPathElements
|
|
415
|
-
);
|
|
416
|
-
|
|
417
|
-
// The new vote option root in the ballot
|
|
418
|
-
var newBallotVoteOptionRootMux = Mux1()(
|
|
419
|
-
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeQip],
|
|
420
|
-
computedIsValid
|
|
421
|
-
);
|
|
422
|
-
|
|
423
|
-
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
424
|
-
|
|
425
|
-
// 6. Generate a new state root.
|
|
426
|
-
var computedNewStateLeafHash = PoseidonHasher(3)([
|
|
427
|
-
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
428
|
-
computedNewStateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
429
|
-
voiceCreditBalanceMux
|
|
430
|
-
]);
|
|
431
|
-
|
|
432
|
-
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
433
|
-
computedNewStateLeafHash,
|
|
434
|
-
actualStateTreeDepth,
|
|
435
|
-
computedStateLeafPathIndices,
|
|
436
|
-
stateLeafPathElements
|
|
437
|
-
);
|
|
438
|
-
|
|
439
|
-
newStateRoot <== computedNewStateLeafQip;
|
|
440
|
-
|
|
441
|
-
// 7. Generate a new ballot root.
|
|
442
|
-
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
443
|
-
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
444
|
-
computedNewBallot,
|
|
445
|
-
computedStateLeafPathIndices,
|
|
446
|
-
ballotPathElements
|
|
447
|
-
);
|
|
448
|
-
|
|
449
|
-
newBallotRoot <== computedNewBallotQip;
|
|
450
|
-
}
|
|
451
|
-
|