@maci-protocol/circuits 0.0.0-ci.26f28d6
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/CHANGELOG.md +673 -0
- package/LICENSE +22 -0
- package/README.md +20 -0
- package/build/ts/compile.d.ts +10 -0
- package/build/ts/compile.d.ts.map +1 -0
- package/build/ts/compile.js +123 -0
- package/build/ts/compile.js.map +1 -0
- package/build/ts/genZkeys.d.ts +9 -0
- package/build/ts/genZkeys.d.ts.map +1 -0
- package/build/ts/genZkeys.js +67 -0
- package/build/ts/genZkeys.js.map +1 -0
- package/build/ts/info.d.ts +2 -0
- package/build/ts/info.d.ts.map +1 -0
- package/build/ts/info.js +72 -0
- package/build/ts/info.js.map +1 -0
- package/build/ts/types.d.ts +105 -0
- package/build/ts/types.d.ts.map +1 -0
- package/build/ts/types.js +3 -0
- package/build/ts/types.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/circom/circuits.json +58 -0
- package/circom/coordinator/non-qv/processMessages.circom +447 -0
- package/circom/coordinator/non-qv/tallyVotes.circom +232 -0
- package/circom/coordinator/qv/processMessages.circom +449 -0
- package/circom/coordinator/qv/tallyVotes.circom +277 -0
- package/circom/utils/calculateTotal.circom +22 -0
- package/circom/utils/hashers.circom +78 -0
- package/circom/utils/messageToCommand.circom +78 -0
- package/circom/utils/non-qv/messageValidator.circom +89 -0
- package/circom/utils/non-qv/stateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/privToPubKey.circom +36 -0
- package/circom/utils/qv/messageValidator.circom +95 -0
- package/circom/utils/qv/stateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/trees/incrementalMerkleTree.circom +198 -0
- package/circom/utils/trees/incrementalQuinaryTree.circom +287 -0
- package/circom/utils/verifySignature.circom +117 -0
- package/circom/voter/poll.circom +93 -0
- package/circomkit.json +18 -0
- package/package.json +71 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
// zk-kit import
|
|
6
|
+
include "./unpack-element.circom";
|
|
7
|
+
// local imports
|
|
8
|
+
include "../../utils/trees/incrementalMerkleTree.circom";
|
|
9
|
+
include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
10
|
+
include "../../utils/calculateTotal.circom";
|
|
11
|
+
include "../../utils/hashers.circom";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Processes batches of votes and verifies their validity in a Merkle tree structure.
|
|
15
|
+
* This template does not support Quadratic Voting (QV).
|
|
16
|
+
*/
|
|
17
|
+
template TallyVotesNonQv(
|
|
18
|
+
stateTreeDepth,
|
|
19
|
+
intStateTreeDepth,
|
|
20
|
+
voteOptionTreeDepth
|
|
21
|
+
) {
|
|
22
|
+
// Ensure there's at least one level in the vote option tree.
|
|
23
|
+
assert(voteOptionTreeDepth > 0);
|
|
24
|
+
// Ensure the intermediate state tree has at least one level.
|
|
25
|
+
assert(intStateTreeDepth > 0);
|
|
26
|
+
// The intermediate state tree must be smaller than the full state tree.
|
|
27
|
+
assert(intStateTreeDepth < stateTreeDepth);
|
|
28
|
+
|
|
29
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
30
|
+
var TREE_ARITY = 5;
|
|
31
|
+
var BALLOT_TREE_ARITY = 2;
|
|
32
|
+
|
|
33
|
+
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
34
|
+
var batchSize = BALLOT_TREE_ARITY ** intStateTreeDepth;
|
|
35
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
36
|
+
var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
37
|
+
|
|
38
|
+
// Number of elements in each ballot.
|
|
39
|
+
var BALLOT_LENGTH = 2;
|
|
40
|
+
// Index for the nonce in the ballot array.
|
|
41
|
+
var BALLOT_NONCE_IDX = 0;
|
|
42
|
+
// Index for the voting option root in the ballot array.
|
|
43
|
+
var BALLOT_VO_ROOT_IDX = 1;
|
|
44
|
+
// Difference in tree depths, used in path calculations.
|
|
45
|
+
var k = stateTreeDepth - intStateTreeDepth;
|
|
46
|
+
|
|
47
|
+
// Root of the state Merkle tree, representing the overall state before voting.
|
|
48
|
+
signal input stateRoot;
|
|
49
|
+
// Root of the ballot Merkle tree, representing the submitted ballots.
|
|
50
|
+
signal input ballotRoot;
|
|
51
|
+
// Salt used in commitment to secure the ballot data.
|
|
52
|
+
signal input sbSalt;
|
|
53
|
+
// Commitment to the state and ballots.
|
|
54
|
+
signal input sbCommitment;
|
|
55
|
+
// Commitment to the current tally before this batch.
|
|
56
|
+
signal input currentTallyCommitment;
|
|
57
|
+
// Commitment to the new tally after processing this batch.
|
|
58
|
+
signal input newTallyCommitment;
|
|
59
|
+
// Start index of given batch
|
|
60
|
+
signal input index;
|
|
61
|
+
// Number of users that signup
|
|
62
|
+
signal input numSignUps;
|
|
63
|
+
// Ballots and their corresponding path elements for verification in the tree.
|
|
64
|
+
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
65
|
+
signal input ballotPathElements[k][BALLOT_TREE_ARITY - 1];
|
|
66
|
+
signal input votes[batchSize][numVoteOptions];
|
|
67
|
+
// Current results for each vote option.
|
|
68
|
+
signal input currentResults[numVoteOptions];
|
|
69
|
+
// Salt for the root of the current results.
|
|
70
|
+
signal input currentResultsRootSalt;
|
|
71
|
+
// Total voice credits spent so far.
|
|
72
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
73
|
+
// Salt for the total spent voice credits.
|
|
74
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
75
|
+
// Salt for the root of the new results.
|
|
76
|
+
signal input newResultsRootSalt;
|
|
77
|
+
// Salt for the new total spent voice credits root.
|
|
78
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
79
|
+
|
|
80
|
+
// Verify sbCommitment.
|
|
81
|
+
var computedSbCommitment = PoseidonHasher(3)([stateRoot, ballotRoot, sbSalt]);
|
|
82
|
+
computedSbCommitment === sbCommitment;
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
// Validates that the index is within the valid range of sign-ups.
|
|
86
|
+
var numSignUpsValid = LessEqThan(50)([index, numSignUps]);
|
|
87
|
+
numSignUpsValid === 1;
|
|
88
|
+
|
|
89
|
+
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
90
|
+
var computedBallotHashers[batchSize];
|
|
91
|
+
|
|
92
|
+
for (var i = 0; i < batchSize; i++) {
|
|
93
|
+
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_IDX], ballots[i][BALLOT_VO_ROOT_IDX]]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
var computedBallotSubroot = CheckRoot(intStateTreeDepth)(computedBallotHashers);
|
|
97
|
+
var computedBallotPathIndices[k] = MerkleGeneratePathIndices(k)(index / batchSize);
|
|
98
|
+
|
|
99
|
+
// Verifies each ballot's existence within the ballot tree.
|
|
100
|
+
LeafExists(k)(
|
|
101
|
+
computedBallotSubroot,
|
|
102
|
+
ballotPathElements,
|
|
103
|
+
computedBallotPathIndices,
|
|
104
|
+
ballotRoot
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Processes vote options, verifying each against its declared root.
|
|
108
|
+
var computedVoteTree[batchSize];
|
|
109
|
+
for (var i = 0; i < batchSize; i++) {
|
|
110
|
+
computedVoteTree[i] = QuinCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
111
|
+
computedVoteTree[i] === ballots[i][BALLOT_VO_ROOT_IDX];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
115
|
+
var computedIsFirstBatch = IsZero()(index);
|
|
116
|
+
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
117
|
+
|
|
118
|
+
// Tally the new results.
|
|
119
|
+
var computedCalculateTotalResult[numVoteOptions];
|
|
120
|
+
for (var i = 0; i < numVoteOptions; i++) {
|
|
121
|
+
var computedNumsRC[batchSize + 1];
|
|
122
|
+
computedNumsRC[batchSize] = currentResults[i] * computedIsZero;
|
|
123
|
+
for (var j = 0; j < batchSize; j++) {
|
|
124
|
+
computedNumsRC[j] = votes[j][i];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
computedCalculateTotalResult[i] = CalculateTotal(batchSize + 1)(computedNumsRC);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Tally the new spent voice credit total.
|
|
131
|
+
var computedNumsSVC[batchSize * numVoteOptions + 1];
|
|
132
|
+
computedNumsSVC[batchSize * numVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
133
|
+
for (var i = 0; i < batchSize; i++) {
|
|
134
|
+
for (var j = 0; j < numVoteOptions; j++) {
|
|
135
|
+
computedNumsSVC[i * numVoteOptions + j] = votes[i][j];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(batchSize * numVoteOptions + 1)(computedNumsSVC);
|
|
140
|
+
|
|
141
|
+
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
142
|
+
ResultCommitmentVerifierNonQv(voteOptionTreeDepth)(
|
|
143
|
+
computedIsFirstBatch,
|
|
144
|
+
currentTallyCommitment,
|
|
145
|
+
newTallyCommitment,
|
|
146
|
+
currentResults,
|
|
147
|
+
currentResultsRootSalt,
|
|
148
|
+
computedCalculateTotalResult,
|
|
149
|
+
newResultsRootSalt,
|
|
150
|
+
currentSpentVoiceCreditSubtotal,
|
|
151
|
+
currentSpentVoiceCreditSubtotalSalt,
|
|
152
|
+
computedNewSpentVoiceCreditSubtotal,
|
|
153
|
+
newSpentVoiceCreditSubtotalSalt
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Performs verifications and computations related to current voting results.
|
|
159
|
+
* Also, computes and outputs a commitment to the new results.
|
|
160
|
+
* This template does not support Quadratic Voting (QV).
|
|
161
|
+
*/
|
|
162
|
+
template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
|
|
163
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
164
|
+
var TREE_ARITY = 5;
|
|
165
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
166
|
+
var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
167
|
+
|
|
168
|
+
// Equal to 1 if this is the first batch, otherwise 0.
|
|
169
|
+
signal input isFirstBatch;
|
|
170
|
+
// Commitment to the current tally before this batch.
|
|
171
|
+
signal input currentTallyCommitment;
|
|
172
|
+
// Commitment to the new tally after processing this batch.
|
|
173
|
+
signal input newTallyCommitment;
|
|
174
|
+
|
|
175
|
+
// Current results for each vote option.
|
|
176
|
+
signal input currentResults[numVoteOptions];
|
|
177
|
+
// Salt for the root of the current results.
|
|
178
|
+
signal input currentResultsRootSalt;
|
|
179
|
+
|
|
180
|
+
// New results for each vote option.
|
|
181
|
+
signal input newResults[numVoteOptions];
|
|
182
|
+
// Salt for the root of the new results.
|
|
183
|
+
signal input newResultsRootSalt;
|
|
184
|
+
|
|
185
|
+
// Total voice credits spent so far.
|
|
186
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
187
|
+
// Salt for the total spent voice credits.
|
|
188
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
189
|
+
|
|
190
|
+
// Total new voice credits spent.
|
|
191
|
+
signal input newSpentVoiceCreditSubtotal;
|
|
192
|
+
// Salt for the new total spent voice credits.
|
|
193
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
194
|
+
|
|
195
|
+
// Compute the commitment to the current results.
|
|
196
|
+
var computedCurrentResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
197
|
+
|
|
198
|
+
// Verify currentResultsCommitmentHash.
|
|
199
|
+
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
200
|
+
|
|
201
|
+
// Compute the commitment to the current spent voice credits.
|
|
202
|
+
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
203
|
+
|
|
204
|
+
// Commit to the current tally
|
|
205
|
+
var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
|
|
206
|
+
|
|
207
|
+
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
208
|
+
// computedIsZero.out is 1 if this is not the first batch.
|
|
209
|
+
// computedIsZero.out is 0 if this is the first batch.
|
|
210
|
+
var computedIsZero = IsZero()(isFirstBatch);
|
|
211
|
+
|
|
212
|
+
// hz is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
213
|
+
// hz is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
214
|
+
signal hz;
|
|
215
|
+
hz <== computedIsZero * computedCurrentTallyCommitment;
|
|
216
|
+
hz === currentTallyCommitment;
|
|
217
|
+
|
|
218
|
+
// Compute the root of the new results.
|
|
219
|
+
var computedNewResultsRoot = QuinCheckRoot(voteOptionTreeDepth)(newResults);
|
|
220
|
+
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
221
|
+
|
|
222
|
+
// Compute the commitment to the new spent voice credits value.
|
|
223
|
+
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
224
|
+
|
|
225
|
+
// Commit to the new tally.
|
|
226
|
+
var computedNewTallyCommitment = PoseidonHasher(2)([
|
|
227
|
+
computedNewResultsCommitment,
|
|
228
|
+
computedNewSpentVoiceCreditsCommitment
|
|
229
|
+
]);
|
|
230
|
+
|
|
231
|
+
computedNewTallyCommitment === newTallyCommitment;
|
|
232
|
+
}
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// zk-kit imports
|
|
6
|
+
include "./safe-comparators.circom";
|
|
7
|
+
// local imports
|
|
8
|
+
include "../../utils/hashers.circom";
|
|
9
|
+
include "../../utils/messageToCommand.circom";
|
|
10
|
+
include "../../utils/privToPubKey.circom";
|
|
11
|
+
include "../../utils/qv/stateLeafAndBallotTransformer.circom";
|
|
12
|
+
include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
13
|
+
include "../../utils/trees/incrementalMerkleTree.circom";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Proves the correctness of processing a batch of MACI messages.
|
|
17
|
+
* This template supports the Quadratic Voting (QV).
|
|
18
|
+
*/
|
|
19
|
+
template ProcessMessages(
|
|
20
|
+
stateTreeDepth,
|
|
21
|
+
batchSize,
|
|
22
|
+
voteOptionTreeDepth
|
|
23
|
+
) {
|
|
24
|
+
// Must ensure that the trees have a valid structure.
|
|
25
|
+
assert(stateTreeDepth > 0);
|
|
26
|
+
assert(batchSize > 0);
|
|
27
|
+
assert(voteOptionTreeDepth > 0);
|
|
28
|
+
|
|
29
|
+
// Default for IQT (quinary trees).
|
|
30
|
+
var VOTE_OPTION_TREE_ARITY = 5;
|
|
31
|
+
// Default for binary trees.
|
|
32
|
+
var STATE_TREE_ARITY = 2;
|
|
33
|
+
var MSG_LENGTH = 10;
|
|
34
|
+
var PACKED_CMD_LENGTH = 4;
|
|
35
|
+
var STATE_LEAF_LENGTH = 4;
|
|
36
|
+
var BALLOT_LENGTH = 2;
|
|
37
|
+
var BALLOT_NONCE_IDX = 0;
|
|
38
|
+
var BALLOT_VO_ROOT_IDX = 1;
|
|
39
|
+
var STATE_LEAF_PUB_X_IDX = 0;
|
|
40
|
+
var STATE_LEAF_PUB_Y_IDX = 1;
|
|
41
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
|
|
42
|
+
var STATE_LEAF_TIMESTAMP_IDX = 3;
|
|
43
|
+
var msgTreeZeroValue = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
44
|
+
// Number of options for this poll.
|
|
45
|
+
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
46
|
+
|
|
47
|
+
// Number of users that have completed the sign up.
|
|
48
|
+
signal input numSignUps;
|
|
49
|
+
// Value of chainHash at beginning of batch
|
|
50
|
+
signal input inputBatchHash;
|
|
51
|
+
// Value of chainHash at end of batch
|
|
52
|
+
signal input outputBatchHash;
|
|
53
|
+
// The messages.
|
|
54
|
+
signal input msgs[batchSize][MSG_LENGTH];
|
|
55
|
+
// The coordinator's private key.
|
|
56
|
+
signal input coordPrivKey;
|
|
57
|
+
// The ECDH public key per message.
|
|
58
|
+
signal input encPubKeys[batchSize][2];
|
|
59
|
+
// The current state root (before the processing).
|
|
60
|
+
signal input currentStateRoot;
|
|
61
|
+
// The actual tree depth (might be <= stateTreeDepth).
|
|
62
|
+
// @note it is a public input to ensure fair processing from
|
|
63
|
+
// the coordinator (no censoring)
|
|
64
|
+
signal input actualStateTreeDepth;
|
|
65
|
+
// The coordinator public key hash
|
|
66
|
+
signal input coordinatorPublicKeyHash;
|
|
67
|
+
// The number of valid vote options for the poll.
|
|
68
|
+
signal input voteOptions;
|
|
69
|
+
|
|
70
|
+
// The state leaves upon which messages are applied.
|
|
71
|
+
// transform(currentStateLeaf[4], message5) => newStateLeaf4
|
|
72
|
+
// transform(currentStateLeaf[3], message4) => newStateLeaf3
|
|
73
|
+
// transform(currentStateLeaf[2], message3) => newStateLeaf2
|
|
74
|
+
// transform(currentStateLeaf[1], message1) => newStateLeaf1
|
|
75
|
+
// transform(currentStateLeaf[0], message0) => newStateLeaf0
|
|
76
|
+
// ...
|
|
77
|
+
|
|
78
|
+
signal input currentStateLeaves[batchSize][STATE_LEAF_LENGTH];
|
|
79
|
+
// The Merkle path to each incremental new state root.
|
|
80
|
+
signal input currentStateLeavesPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
81
|
+
// The salted commitment to the state and ballot roots.
|
|
82
|
+
signal input currentSbCommitment;
|
|
83
|
+
signal input currentSbSalt;
|
|
84
|
+
// The salted commitment to the new state and ballot roots.
|
|
85
|
+
signal input newSbCommitment;
|
|
86
|
+
signal input newSbSalt;
|
|
87
|
+
// The current ballot root before batch processing.
|
|
88
|
+
signal input currentBallotRoot;
|
|
89
|
+
// Intermediate ballots.
|
|
90
|
+
signal input currentBallots[batchSize][BALLOT_LENGTH];
|
|
91
|
+
signal input currentBallotsPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
92
|
+
// Intermediate vote weights.
|
|
93
|
+
signal input currentVoteWeights[batchSize];
|
|
94
|
+
signal input currentVoteWeightsPathElements[batchSize][voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
95
|
+
|
|
96
|
+
// nb. The messages are processed in REVERSE order.
|
|
97
|
+
// Therefore, the index of the first message to process does not match the index of the
|
|
98
|
+
// first message (e.g., [msg1, msg2, msg3] => first message to process has index 3).
|
|
99
|
+
|
|
100
|
+
// The index of the first message in the batch, inclusive.
|
|
101
|
+
signal input index;
|
|
102
|
+
|
|
103
|
+
// The index of the last message in the batch to process, exclusive.
|
|
104
|
+
// This value may be less than batchSize if this batch is
|
|
105
|
+
// the last batch and the total number of messages is not a multiple of the batch size.
|
|
106
|
+
signal input batchEndIndex;
|
|
107
|
+
|
|
108
|
+
// The history of state and ballot roots and temporary intermediate
|
|
109
|
+
// signals (for processing purposes).
|
|
110
|
+
signal stateRoots[batchSize + 1];
|
|
111
|
+
signal ballotRoots[batchSize + 1];
|
|
112
|
+
|
|
113
|
+
// Must verify the current sb commitment.
|
|
114
|
+
var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
|
|
115
|
+
computedCurrentSbCommitment === currentSbCommitment;
|
|
116
|
+
|
|
117
|
+
// Ensure that the vote options signal is valid
|
|
118
|
+
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
119
|
+
voteOptionsValid === 1;
|
|
120
|
+
|
|
121
|
+
// Check numSignUps <= the max number of users (i.e., number of state leaves
|
|
122
|
+
// that can fit the state tree).
|
|
123
|
+
var numSignUpsValid = LessEqThan(32)([numSignUps, STATE_TREE_ARITY ** stateTreeDepth]);
|
|
124
|
+
numSignUpsValid === 1;
|
|
125
|
+
|
|
126
|
+
// Hash each Message to check their existence in the Message tree.
|
|
127
|
+
var computedMessageHashers[batchSize];
|
|
128
|
+
var computedChainHashes[batchSize];
|
|
129
|
+
var chainHash[batchSize + 1];
|
|
130
|
+
chainHash[0] = inputBatchHash;
|
|
131
|
+
|
|
132
|
+
for (var i = 0; i < batchSize; i++) {
|
|
133
|
+
// calculate message hash
|
|
134
|
+
computedMessageHashers[i] = MessageHasher()(msgs[i], encPubKeys[i]);
|
|
135
|
+
// check if message is valid or not (if index of message is less than index of last valid message in batch)
|
|
136
|
+
var batchStartIndexValid = SafeLessThan(32)([index + i, batchEndIndex]);
|
|
137
|
+
// calculate chain hash if message is valid
|
|
138
|
+
computedChainHashes[i] = PoseidonHasher(2)([chainHash[i], computedMessageHashers[i]]);
|
|
139
|
+
// choose between old chain hash value and new chain hash value depending if message is valid or not
|
|
140
|
+
chainHash[i + 1] = Mux1()([chainHash[i], computedChainHashes[i]], batchStartIndexValid);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// If batchEndIndex < index + i, the remaining
|
|
144
|
+
// message hashes should be the zero value.
|
|
145
|
+
// e.g. [m, z, z, z, z] if there is only 1 real message in the batch
|
|
146
|
+
// This makes possible to have a batch of messages which is only partially full.
|
|
147
|
+
|
|
148
|
+
chainHash[batchSize] === outputBatchHash;
|
|
149
|
+
|
|
150
|
+
// Decrypt each Message to a Command.
|
|
151
|
+
// MessageToCommand derives the ECDH shared key from the coordinator's
|
|
152
|
+
// private key and the message's ephemeral public key. Next, it uses this
|
|
153
|
+
// shared key to decrypt a Message to a Command.
|
|
154
|
+
|
|
155
|
+
// Ensure that the coordinator's public key from the contract is correct
|
|
156
|
+
// based on the given private key - that is, the prover knows the
|
|
157
|
+
// coordinator's private key.
|
|
158
|
+
var derivedPubKey[2] = PrivToPubKey()(coordPrivKey);
|
|
159
|
+
var derivedPubKeyHash = PoseidonHasher(2)(derivedPubKey);
|
|
160
|
+
derivedPubKeyHash === coordinatorPublicKeyHash;
|
|
161
|
+
|
|
162
|
+
// Decrypt each Message into a Command.
|
|
163
|
+
// The command i-th is composed by the following fields.
|
|
164
|
+
// e.g., command 0 is made of commandsStateIndex[0],
|
|
165
|
+
// commandsNewPubKey[0], ..., commandsPackedCommandOut[0]
|
|
166
|
+
var computedCommandsStateIndex[batchSize];
|
|
167
|
+
var computedCommandsNewPubKey[batchSize][2];
|
|
168
|
+
var computedCommandsVoteOptionIndex[batchSize];
|
|
169
|
+
var computedCommandsNewVoteWeight[batchSize];
|
|
170
|
+
var computedCommandsNonce[batchSize];
|
|
171
|
+
var computedCommandsPollId[batchSize];
|
|
172
|
+
var computedCommandsSalt[batchSize];
|
|
173
|
+
var computedCommandsSigR8[batchSize][2];
|
|
174
|
+
var computedCommandsSigS[batchSize];
|
|
175
|
+
var computedCommandsPackedCommandOut[batchSize][PACKED_CMD_LENGTH];
|
|
176
|
+
|
|
177
|
+
for (var i = 0; i < batchSize; i++) {
|
|
178
|
+
(
|
|
179
|
+
computedCommandsStateIndex[i],
|
|
180
|
+
computedCommandsNewPubKey[i],
|
|
181
|
+
computedCommandsVoteOptionIndex[i],
|
|
182
|
+
computedCommandsNewVoteWeight[i],
|
|
183
|
+
computedCommandsNonce[i],
|
|
184
|
+
computedCommandsPollId[i],
|
|
185
|
+
computedCommandsSalt[i],
|
|
186
|
+
computedCommandsSigR8[i],
|
|
187
|
+
computedCommandsSigS[i],
|
|
188
|
+
computedCommandsPackedCommandOut[i]
|
|
189
|
+
) = MessageToCommand()(msgs[i], coordPrivKey, encPubKeys[i]);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Process messages in reverse order.
|
|
193
|
+
// Assign current state and ballot roots.
|
|
194
|
+
stateRoots[batchSize] <== currentStateRoot;
|
|
195
|
+
ballotRoots[batchSize] <== currentBallotRoot;
|
|
196
|
+
|
|
197
|
+
// Define vote type message processors.
|
|
198
|
+
var computedNewVoteStateRoot[batchSize];
|
|
199
|
+
var computedNewVoteBallotRoot[batchSize];
|
|
200
|
+
|
|
201
|
+
// Start from batchSize and decrement for process in reverse order.
|
|
202
|
+
for (var i = batchSize - 1; i >= 0; i--) {
|
|
203
|
+
// Process as vote type message.
|
|
204
|
+
var currentStateLeavesPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
205
|
+
var currentBallotPathElement[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
206
|
+
var currentVoteWeightsPathElement[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
207
|
+
|
|
208
|
+
for (var j = 0; j < stateTreeDepth; j++) {
|
|
209
|
+
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
210
|
+
currentStateLeavesPathElement[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
211
|
+
currentBallotPathElement[j][k] = currentBallotsPathElements[i][j][k];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
216
|
+
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
217
|
+
currentVoteWeightsPathElement[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = ProcessOne(stateTreeDepth, voteOptionTreeDepth)(
|
|
222
|
+
numSignUps,
|
|
223
|
+
stateRoots[i + 1],
|
|
224
|
+
ballotRoots[i + 1],
|
|
225
|
+
actualStateTreeDepth,
|
|
226
|
+
currentStateLeaves[i],
|
|
227
|
+
currentStateLeavesPathElement,
|
|
228
|
+
currentBallots[i],
|
|
229
|
+
currentBallotPathElement,
|
|
230
|
+
currentVoteWeights[i],
|
|
231
|
+
currentVoteWeightsPathElement,
|
|
232
|
+
computedCommandsStateIndex[i],
|
|
233
|
+
computedCommandsNewPubKey[i],
|
|
234
|
+
computedCommandsVoteOptionIndex[i],
|
|
235
|
+
computedCommandsNewVoteWeight[i],
|
|
236
|
+
computedCommandsNonce[i],
|
|
237
|
+
computedCommandsPollId[i],
|
|
238
|
+
computedCommandsSalt[i],
|
|
239
|
+
computedCommandsSigR8[i],
|
|
240
|
+
computedCommandsSigS[i],
|
|
241
|
+
computedCommandsPackedCommandOut[i],
|
|
242
|
+
voteOptions
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
stateRoots[i] <== computedNewVoteStateRoot[i];
|
|
246
|
+
ballotRoots[i] <== computedNewVoteBallotRoot[i];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
|
|
250
|
+
computedNewSbCommitment === newSbCommitment;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Processes one message and updates the state accordingly.
|
|
255
|
+
* This template involves complex interactions, including transformations based on message type,
|
|
256
|
+
* validations against current states like voice credit balances or vote weights,
|
|
257
|
+
* and updates to Merkle trees representing state and ballot information.
|
|
258
|
+
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
259
|
+
* This template supports the Quadratic Voting (QV).
|
|
260
|
+
*/
|
|
261
|
+
template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
262
|
+
// Constants defining the structure and size of state and ballots.
|
|
263
|
+
var STATE_LEAF_LENGTH = 4;
|
|
264
|
+
var BALLOT_LENGTH = 2;
|
|
265
|
+
var MSG_LENGTH = 10;
|
|
266
|
+
var PACKED_CMD_LENGTH = 4;
|
|
267
|
+
var VOTE_OPTION_TREE_ARITY = 5;
|
|
268
|
+
var STATE_TREE_ARITY = 2;
|
|
269
|
+
var BALLOT_NONCE_IDX = 0;
|
|
270
|
+
// Ballot vote option (VO) root index.
|
|
271
|
+
var BALLOT_VO_ROOT_IDX = 1;
|
|
272
|
+
|
|
273
|
+
// Indices for elements within a state leaf.
|
|
274
|
+
// Public key.
|
|
275
|
+
var STATE_LEAF_PUB_X_IDX = 0;
|
|
276
|
+
var STATE_LEAF_PUB_Y_IDX = 1;
|
|
277
|
+
// Voice Credit balance.
|
|
278
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_IDX = 2;
|
|
279
|
+
// Timestamp.
|
|
280
|
+
var STATE_LEAF_TIMESTAMP_IDX = 3;
|
|
281
|
+
var N_BITS = 252;
|
|
282
|
+
|
|
283
|
+
// Number of users that have completed the sign up.
|
|
284
|
+
signal input numSignUps;
|
|
285
|
+
// The current value of the state tree root.
|
|
286
|
+
signal input currentStateRoot;
|
|
287
|
+
// The current value of the ballot tree root.
|
|
288
|
+
signal input currentBallotRoot;
|
|
289
|
+
// The actual tree depth (might be <= stateTreeDepth).
|
|
290
|
+
signal input actualStateTreeDepth;
|
|
291
|
+
|
|
292
|
+
// The state leaf and related path elements.
|
|
293
|
+
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
294
|
+
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
295
|
+
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
296
|
+
|
|
297
|
+
// The ballot and related path elements.
|
|
298
|
+
signal input ballot[BALLOT_LENGTH];
|
|
299
|
+
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
300
|
+
|
|
301
|
+
// The current vote weight and related path elements.
|
|
302
|
+
signal input currentVoteWeight;
|
|
303
|
+
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
304
|
+
|
|
305
|
+
// Inputs related to the command being processed.
|
|
306
|
+
signal input cmdStateIndex;
|
|
307
|
+
signal input cmdNewPubKey[2];
|
|
308
|
+
signal input cmdVoteOptionIndex;
|
|
309
|
+
signal input cmdNewVoteWeight;
|
|
310
|
+
signal input cmdNonce;
|
|
311
|
+
signal input cmdPollId;
|
|
312
|
+
signal input cmdSalt;
|
|
313
|
+
signal input cmdSigR8[2];
|
|
314
|
+
signal input cmdSigS;
|
|
315
|
+
signal input packedCmd[PACKED_CMD_LENGTH];
|
|
316
|
+
|
|
317
|
+
// The number of valid vote options for the poll.
|
|
318
|
+
signal input voteOptions;
|
|
319
|
+
|
|
320
|
+
signal output newStateRoot;
|
|
321
|
+
signal output newBallotRoot;
|
|
322
|
+
|
|
323
|
+
// Intermediate signals.
|
|
324
|
+
// currentVoteWeight * currentVoteWeight.
|
|
325
|
+
signal currentVoteWeightSquare;
|
|
326
|
+
// cmdNewVoteWeight * cmdNewVoteWeight.
|
|
327
|
+
signal cmdNewVoteWeightSquare;
|
|
328
|
+
// equal to newBallotVoRootMux (Mux1).
|
|
329
|
+
signal newBallotVoRoot;
|
|
330
|
+
|
|
331
|
+
// 1. Transform a state leaf and a ballot with a command.
|
|
332
|
+
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
333
|
+
var computedNewSlPubKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
334
|
+
(computedNewSlPubKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformer()(
|
|
335
|
+
numSignUps,
|
|
336
|
+
voteOptions,
|
|
337
|
+
[stateLeaf[STATE_LEAF_PUB_X_IDX], stateLeaf[STATE_LEAF_PUB_Y_IDX]],
|
|
338
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
|
|
339
|
+
ballot[BALLOT_NONCE_IDX],
|
|
340
|
+
currentVoteWeight,
|
|
341
|
+
cmdStateIndex,
|
|
342
|
+
cmdNewPubKey,
|
|
343
|
+
cmdVoteOptionIndex,
|
|
344
|
+
cmdNewVoteWeight,
|
|
345
|
+
cmdNonce,
|
|
346
|
+
cmdPollId,
|
|
347
|
+
cmdSalt,
|
|
348
|
+
cmdSigR8,
|
|
349
|
+
cmdSigS,
|
|
350
|
+
packedCmd
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
354
|
+
// Otherwise, generate indices for command.stateIndex.
|
|
355
|
+
var stateIndexMux = Mux1()([0, cmdStateIndex], computedIsStateLeafIndexValid);
|
|
356
|
+
var computedStateLeafPathIndices[stateTreeDepth] = MerkleGeneratePathIndices(stateTreeDepth)(stateIndexMux);
|
|
357
|
+
|
|
358
|
+
// 3. Verify that the original state leaf exists in the given state root.
|
|
359
|
+
var stateLeafHash = PoseidonHasher(4)(stateLeaf);
|
|
360
|
+
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
361
|
+
stateLeafHash,
|
|
362
|
+
actualStateTreeDepth,
|
|
363
|
+
computedStateLeafPathIndices,
|
|
364
|
+
stateLeafPathElements
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
stateLeafQip === currentStateRoot;
|
|
368
|
+
|
|
369
|
+
// 4. Verify that the original ballot exists in the given ballot root.
|
|
370
|
+
var computedBallot = PoseidonHasher(2)([
|
|
371
|
+
ballot[BALLOT_NONCE_IDX],
|
|
372
|
+
ballot[BALLOT_VO_ROOT_IDX]
|
|
373
|
+
]);
|
|
374
|
+
|
|
375
|
+
var computedBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
376
|
+
computedBallot,
|
|
377
|
+
computedStateLeafPathIndices,
|
|
378
|
+
ballotPathElements
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
computedBallotQip === currentBallotRoot;
|
|
382
|
+
|
|
383
|
+
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
384
|
+
// at cmdVoteOptionIndex.
|
|
385
|
+
currentVoteWeightSquare <== currentVoteWeight * currentVoteWeight;
|
|
386
|
+
cmdNewVoteWeightSquare <== cmdNewVoteWeight * cmdNewVoteWeight;
|
|
387
|
+
|
|
388
|
+
var cmdVoteOptionIndexMux = Mux1()([0, cmdVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
389
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinGeneratePathIndices(voteOptionTreeDepth)(cmdVoteOptionIndexMux);
|
|
390
|
+
|
|
391
|
+
var computedCurrentVoteWeightQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
392
|
+
currentVoteWeight,
|
|
393
|
+
computedCurrentVoteWeightPathIndices,
|
|
394
|
+
currentVoteWeightsPathElements
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
computedCurrentVoteWeightQip === ballot[BALLOT_VO_ROOT_IDX];
|
|
398
|
+
|
|
399
|
+
var voteWeightMux = Mux1()([currentVoteWeight, cmdNewVoteWeight], computedIsValid);
|
|
400
|
+
var voiceCreditBalanceMux = Mux1()(
|
|
401
|
+
[
|
|
402
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX],
|
|
403
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] + currentVoteWeightSquare - cmdNewVoteWeightSquare
|
|
404
|
+
],
|
|
405
|
+
computedIsValid
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
409
|
+
var computedNewVoteOptionTreeQip = QuinTreeInclusionProof(voteOptionTreeDepth)(
|
|
410
|
+
voteWeightMux,
|
|
411
|
+
computedCurrentVoteWeightPathIndices,
|
|
412
|
+
currentVoteWeightsPathElements
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
// The new vote option root in the ballot
|
|
416
|
+
var newBallotVoRootMux = Mux1()(
|
|
417
|
+
[ballot[BALLOT_VO_ROOT_IDX], computedNewVoteOptionTreeQip],
|
|
418
|
+
computedIsValid
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
newBallotVoRoot <== newBallotVoRootMux;
|
|
422
|
+
|
|
423
|
+
// 6. Generate a new state root.
|
|
424
|
+
var computedNewStateLeafhash = PoseidonHasher(4)([
|
|
425
|
+
computedNewSlPubKey[STATE_LEAF_PUB_X_IDX],
|
|
426
|
+
computedNewSlPubKey[STATE_LEAF_PUB_Y_IDX],
|
|
427
|
+
voiceCreditBalanceMux,
|
|
428
|
+
stateLeaf[STATE_LEAF_TIMESTAMP_IDX]
|
|
429
|
+
]);
|
|
430
|
+
|
|
431
|
+
var computedNewStateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
432
|
+
computedNewStateLeafhash,
|
|
433
|
+
actualStateTreeDepth,
|
|
434
|
+
computedStateLeafPathIndices,
|
|
435
|
+
stateLeafPathElements
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
newStateRoot <== computedNewStateLeafQip;
|
|
439
|
+
|
|
440
|
+
// 7. Generate a new ballot root.
|
|
441
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoRoot]);
|
|
442
|
+
var computedNewBallotQip = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
443
|
+
computedNewBallot,
|
|
444
|
+
computedStateLeafPathIndices,
|
|
445
|
+
ballotPathElements
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
newBallotRoot <== computedNewBallotQip;
|
|
449
|
+
}
|