@maci-protocol/circuits 0.0.0-ci.fd5247e → 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/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +16 -0
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +14 -13
- package/circom/coordinator/non-qv/tallyVotes.circom +18 -18
- package/circom/coordinator/qv/processMessages.circom +14 -13
- package/circom/coordinator/qv/tallyVotes.circom +15 -15
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/full/MessageValidator.circom +2 -4
- package/circom/utils/non-qv/MessageValidator.circom +2 -2
- package/circom/utils/qv/MessageValidator.circom +2 -2
- 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/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/package.json +9 -9
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
package/circom/circuits.json
CHANGED
|
@@ -43,6 +43,22 @@
|
|
|
43
43
|
"voteOptions"
|
|
44
44
|
]
|
|
45
45
|
},
|
|
46
|
+
"MessageProcessorFull_10-20-2_test": {
|
|
47
|
+
"file": "./coordinator/full/MessageProcessor",
|
|
48
|
+
"template": "MessageProcessorFull",
|
|
49
|
+
"params": [10, 20, 2],
|
|
50
|
+
"pubs": [
|
|
51
|
+
"totalSignups",
|
|
52
|
+
"index",
|
|
53
|
+
"batchEndIndex",
|
|
54
|
+
"currentSbCommitment",
|
|
55
|
+
"newSbCommitment",
|
|
56
|
+
"outputBatchHash",
|
|
57
|
+
"actualStateTreeDepth",
|
|
58
|
+
"coordinatorPublicKeyHash",
|
|
59
|
+
"voteOptions"
|
|
60
|
+
]
|
|
61
|
+
},
|
|
46
62
|
"TallyVotes_10-1-2_test": {
|
|
47
63
|
"file": "./coordinator/qv/tallyVotes",
|
|
48
64
|
"template": "TallyVotes",
|
|
@@ -0,0 +1,253 @@
|
|
|
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/PoseidonHasher.circom";
|
|
9
|
+
include "../../utils/MessageHasher.circom";
|
|
10
|
+
include "../../utils/MessageToCommand.circom";
|
|
11
|
+
include "../../utils/PrivateToPublicKey.circom";
|
|
12
|
+
include "./SingleMessageProcessor.circom";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Proves the correctness of processing a batch of MACI messages.
|
|
16
|
+
* This template supports fully spent voice credits mode.
|
|
17
|
+
*/
|
|
18
|
+
template MessageProcessorFull(
|
|
19
|
+
stateTreeDepth,
|
|
20
|
+
batchSize,
|
|
21
|
+
voteOptionTreeDepth
|
|
22
|
+
) {
|
|
23
|
+
// Must ensure that the trees have a valid structure.
|
|
24
|
+
assert(stateTreeDepth > 0);
|
|
25
|
+
assert(batchSize > 0);
|
|
26
|
+
assert(voteOptionTreeDepth > 0);
|
|
27
|
+
|
|
28
|
+
// Default for IQT (quinary trees).
|
|
29
|
+
var VOTE_OPTION_TREE_ARITY = 5;
|
|
30
|
+
// Default for Binary trees.
|
|
31
|
+
var STATE_TREE_ARITY = 2;
|
|
32
|
+
var MESSAGE_LENGTH = 10;
|
|
33
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
34
|
+
var STATE_LEAF_LENGTH = 3;
|
|
35
|
+
var BALLOT_LENGTH = 2;
|
|
36
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
37
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
38
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
39
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
40
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
41
|
+
var MESSAGE_TREE_ZERO_VALUE = 8370432830353022751713833565135785980866757267633941821328460903436894336785;
|
|
42
|
+
// Number of options for this poll.
|
|
43
|
+
var maxVoteOptions = VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth;
|
|
44
|
+
|
|
45
|
+
// Number of users that have completed the sign up.
|
|
46
|
+
signal input totalSignups;
|
|
47
|
+
// Value of chainHash at beginning of batch
|
|
48
|
+
signal input inputBatchHash;
|
|
49
|
+
// Value of chainHash at end of batch
|
|
50
|
+
signal input outputBatchHash;
|
|
51
|
+
// The messages.
|
|
52
|
+
signal input messages[batchSize][MESSAGE_LENGTH];
|
|
53
|
+
// The coordinator's private key.
|
|
54
|
+
signal input coordinatorPrivateKey;
|
|
55
|
+
// The ECDH public key per message.
|
|
56
|
+
signal input encryptionPublicKeys[batchSize][2];
|
|
57
|
+
// The current state root (before the processing).
|
|
58
|
+
signal input currentStateRoot;
|
|
59
|
+
// The actual tree depth (might be <= stateTreeDepth).
|
|
60
|
+
// @note it is a public input to ensure fair processing from
|
|
61
|
+
// the coordinator (no censoring)
|
|
62
|
+
signal input actualStateTreeDepth;
|
|
63
|
+
// The coordinator public key hash
|
|
64
|
+
signal input coordinatorPublicKeyHash;
|
|
65
|
+
// The number of valid vote options for the poll.
|
|
66
|
+
signal input voteOptions;
|
|
67
|
+
|
|
68
|
+
// The state leaves upon which messages are applied.
|
|
69
|
+
// transform(currentStateLeaf[4], message5) => newStateLeaf4
|
|
70
|
+
// transform(currentStateLeaf[3], message4) => newStateLeaf3
|
|
71
|
+
// transform(currentStateLeaf[2], message3) => newStateLeaf2
|
|
72
|
+
// transform(currentStateLeaf[1], message1) => newStateLeaf1
|
|
73
|
+
// transform(currentStateLeaf[0], message0) => newStateLeaf0
|
|
74
|
+
// ...
|
|
75
|
+
|
|
76
|
+
signal input currentStateLeaves[batchSize][STATE_LEAF_LENGTH];
|
|
77
|
+
// The Merkle path to each incremental new state root.
|
|
78
|
+
signal input currentStateLeavesPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
79
|
+
// The salted commitment to the state and ballot roots.
|
|
80
|
+
signal input currentSbCommitment;
|
|
81
|
+
signal input currentSbSalt;
|
|
82
|
+
// The salted commitment to the new state and ballot roots.
|
|
83
|
+
signal input newSbCommitment;
|
|
84
|
+
signal input newSbSalt;
|
|
85
|
+
// The current ballot root before batch processing.
|
|
86
|
+
signal input currentBallotRoot;
|
|
87
|
+
// Intermediate ballots.
|
|
88
|
+
signal input currentBallots[batchSize][BALLOT_LENGTH];
|
|
89
|
+
signal input currentBallotsPathElements[batchSize][stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
90
|
+
// Intermediate vote weights.
|
|
91
|
+
signal input currentVoteWeights[batchSize];
|
|
92
|
+
signal input currentVoteWeightsPathElements[batchSize][voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
93
|
+
|
|
94
|
+
// nb. The messages are processed in REVERSE order.
|
|
95
|
+
// Therefore, the index of the first message to process does not match the index of the
|
|
96
|
+
// first message (e.g., [msg1, msg2, msg3] => first message to process has index 3).
|
|
97
|
+
|
|
98
|
+
// The index of the first message in the batch, inclusive.
|
|
99
|
+
signal input index;
|
|
100
|
+
|
|
101
|
+
// The index of the last message in the batch to process, exclusive.
|
|
102
|
+
// This value may be less than index + batchSize if this batch is
|
|
103
|
+
// the last batch and the total number of messages is not a multiple of the batch size.
|
|
104
|
+
signal input batchEndIndex;
|
|
105
|
+
|
|
106
|
+
// The history of state and ballot roots and temporary intermediate
|
|
107
|
+
// signals (for processing purposes).
|
|
108
|
+
signal stateRoots[batchSize + 1];
|
|
109
|
+
signal ballotRoots[batchSize + 1];
|
|
110
|
+
|
|
111
|
+
// Must verify the current sb commitment.
|
|
112
|
+
var computedCurrentSbCommitment = PoseidonHasher(3)([currentStateRoot, currentBallotRoot, currentSbSalt]);
|
|
113
|
+
computedCurrentSbCommitment === currentSbCommitment;
|
|
114
|
+
|
|
115
|
+
// -----------------------------------------------------------------------
|
|
116
|
+
// 0. Ensure that the maximum vote options signal is valid and if
|
|
117
|
+
// the maximum users signal is valid
|
|
118
|
+
var voteOptionsValid = LessEqThan(32)([voteOptions, VOTE_OPTION_TREE_ARITY ** voteOptionTreeDepth]);
|
|
119
|
+
voteOptionsValid === 1;
|
|
120
|
+
|
|
121
|
+
// Check totalSignups <= the max number of users (i.e., number of state leaves
|
|
122
|
+
// that can fit the state tree).
|
|
123
|
+
var totalSignupsValid = LessEqThan(32)([totalSignups, STATE_TREE_ARITY ** stateTreeDepth]);
|
|
124
|
+
totalSignupsValid === 1;
|
|
125
|
+
|
|
126
|
+
// Hash each Message to check their existence in the Message chain hash.
|
|
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()(messages[i], encryptionPublicKeys[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
|
+
// Ensure that right output batch hash was sent to circuit
|
|
149
|
+
chainHash[batchSize] === outputBatchHash;
|
|
150
|
+
|
|
151
|
+
// Decrypt each Message to a Command.
|
|
152
|
+
// MessageToCommand derives the ECDH shared key from the coordinator's
|
|
153
|
+
// private key and the message's ephemeral public key. Next, it uses this
|
|
154
|
+
// shared key to decrypt a Message to a Command.
|
|
155
|
+
|
|
156
|
+
// Ensure that the coordinator's public key from the contract is correct
|
|
157
|
+
// based on the given private key - that is, the prover knows the
|
|
158
|
+
// coordinator's private key.
|
|
159
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(coordinatorPrivateKey);
|
|
160
|
+
var derivedPublicKeyHash = PoseidonHasher(2)(derivedPublicKey);
|
|
161
|
+
derivedPublicKeyHash === coordinatorPublicKeyHash;
|
|
162
|
+
|
|
163
|
+
// Decrypt each Message into a Command.
|
|
164
|
+
// The command i-th is composed by the following fields.
|
|
165
|
+
// e.g., command 0 is made of commandsStateIndex[0],
|
|
166
|
+
// commandsNewPublicKey[0], ..., commandsPackedCommandOut[0]
|
|
167
|
+
var computedCommandsStateIndex[batchSize];
|
|
168
|
+
var computedCommandsNewPublicKey[batchSize][2];
|
|
169
|
+
var computedCommandsVoteOptionIndex[batchSize];
|
|
170
|
+
var computedCommandsNewVoteWeight[batchSize];
|
|
171
|
+
var computedCommandsNonce[batchSize];
|
|
172
|
+
var computedCommandsPollId[batchSize];
|
|
173
|
+
var computedCommandsSalt[batchSize];
|
|
174
|
+
var computedCommandsSignaturePoint[batchSize][2];
|
|
175
|
+
var computedCommandsSignatureScalar[batchSize];
|
|
176
|
+
var computedCommandsPackedCommandOut[batchSize][PACKED_COMMAND_LENGTH];
|
|
177
|
+
|
|
178
|
+
for (var i = 0; i < batchSize; i++) {
|
|
179
|
+
(
|
|
180
|
+
computedCommandsStateIndex[i],
|
|
181
|
+
computedCommandsNewPublicKey[i],
|
|
182
|
+
computedCommandsVoteOptionIndex[i],
|
|
183
|
+
computedCommandsNewVoteWeight[i],
|
|
184
|
+
computedCommandsNonce[i],
|
|
185
|
+
computedCommandsPollId[i],
|
|
186
|
+
computedCommandsSalt[i],
|
|
187
|
+
computedCommandsSignaturePoint[i],
|
|
188
|
+
computedCommandsSignatureScalar[i],
|
|
189
|
+
computedCommandsPackedCommandOut[i]
|
|
190
|
+
) = MessageToCommand()(messages[i], coordinatorPrivateKey, encryptionPublicKeys[i]);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Process messages in reverse order.
|
|
194
|
+
// Assign current state and ballot roots.
|
|
195
|
+
stateRoots[batchSize] <== currentStateRoot;
|
|
196
|
+
ballotRoots[batchSize] <== currentBallotRoot;
|
|
197
|
+
|
|
198
|
+
// Define vote type message processors.
|
|
199
|
+
var computedNewVoteStateRoot[batchSize];
|
|
200
|
+
var computedNewVoteBallotRoot[batchSize];
|
|
201
|
+
|
|
202
|
+
// Start from batchSize and decrement for process in reverse order.
|
|
203
|
+
for (var i = batchSize - 1; i >= 0; i--) {
|
|
204
|
+
// Process as vote type message.
|
|
205
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
206
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
207
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
208
|
+
|
|
209
|
+
for (var j = 0; j < stateTreeDepth; j++) {
|
|
210
|
+
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
211
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
212
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
217
|
+
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
218
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
(computedNewVoteStateRoot[i], computedNewVoteBallotRoot[i]) = SingleMessageProcessorFull(stateTreeDepth, voteOptionTreeDepth)(
|
|
223
|
+
totalSignups,
|
|
224
|
+
stateRoots[i + 1],
|
|
225
|
+
ballotRoots[i + 1],
|
|
226
|
+
actualStateTreeDepth,
|
|
227
|
+
currentStateLeaves[i],
|
|
228
|
+
computedCurrentStateLeavesPathElements,
|
|
229
|
+
currentBallots[i],
|
|
230
|
+
computedCurrentBallotPathElements,
|
|
231
|
+
currentVoteWeights[i],
|
|
232
|
+
computedCurrentVoteWeightsPathElements,
|
|
233
|
+
computedCommandsStateIndex[i],
|
|
234
|
+
computedCommandsNewPublicKey[i],
|
|
235
|
+
computedCommandsVoteOptionIndex[i],
|
|
236
|
+
computedCommandsNewVoteWeight[i],
|
|
237
|
+
computedCommandsNonce[i],
|
|
238
|
+
computedCommandsPollId[i],
|
|
239
|
+
computedCommandsSalt[i],
|
|
240
|
+
computedCommandsSignaturePoint[i],
|
|
241
|
+
computedCommandsSignatureScalar[i],
|
|
242
|
+
computedCommandsPackedCommandOut[i],
|
|
243
|
+
voteOptions
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
stateRoots[i] <== computedNewVoteStateRoot[i];
|
|
247
|
+
ballotRoots[i] <== computedNewVoteBallotRoot[i];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
var computedNewSbCommitment = PoseidonHasher(3)([stateRoots[0], ballotRoots[0], newSbSalt]);
|
|
251
|
+
|
|
252
|
+
computedNewSbCommitment === newSbCommitment;
|
|
253
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../../utils/PoseidonHasher.circom";
|
|
7
|
+
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
8
|
+
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
9
|
+
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
10
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
11
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
12
|
+
include "../../utils/full/StateLeafAndBallotTransformer.circom";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Processes one message and updates the state accordingly.
|
|
16
|
+
* This template involves complex interactions, including transformations based on message type,
|
|
17
|
+
* validations against current states like voice credit balances or vote weights,
|
|
18
|
+
* and updates to Merkle trees representing state and ballot information.
|
|
19
|
+
* This is a critical building block for ensuring the integrity and correctness of MACI state.
|
|
20
|
+
* This template supports fully spent voice credits mode.
|
|
21
|
+
*/
|
|
22
|
+
template SingleMessageProcessorFull(stateTreeDepth, voteOptionTreeDepth) {
|
|
23
|
+
// The number of elements in the ballot.
|
|
24
|
+
var BALLOT_LENGTH = 2;
|
|
25
|
+
// The number of elements in the state leaf.
|
|
26
|
+
var STATE_LEAF_LENGTH = 3;
|
|
27
|
+
// The number of elements in the vote option tree node (tree arity).
|
|
28
|
+
var VOTE_OPTION_TREE_ARITY = 5;
|
|
29
|
+
// The number of elements in the state tree node (tree arity).
|
|
30
|
+
var STATE_TREE_ARITY = 2;
|
|
31
|
+
// The number of elements in the packed command.
|
|
32
|
+
var PACKED_COMMAND_LENGTH = 4;
|
|
33
|
+
// Constants defining the structure and size of state and ballots.
|
|
34
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
35
|
+
// Ballot vote option (vote option) root index.
|
|
36
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
37
|
+
|
|
38
|
+
// Indices for elements within a state leaf.
|
|
39
|
+
// Public key.
|
|
40
|
+
var STATE_LEAF_PUBLIC_X_INDEX = 0;
|
|
41
|
+
var STATE_LEAF_PUBLIC_Y_INDEX = 1;
|
|
42
|
+
// Voice Credit balance.
|
|
43
|
+
var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
|
|
44
|
+
|
|
45
|
+
// Number of users that have completed the sign up.
|
|
46
|
+
signal input totalSignups;
|
|
47
|
+
// The current value of the state tree root.
|
|
48
|
+
signal input currentStateRoot;
|
|
49
|
+
// The current value of the ballot tree root.
|
|
50
|
+
signal input currentBallotRoot;
|
|
51
|
+
// The actual tree depth (might be <= stateTreeDepth).
|
|
52
|
+
signal input actualStateTreeDepth;
|
|
53
|
+
|
|
54
|
+
// The state leaf and related path elements.
|
|
55
|
+
signal input stateLeaf[STATE_LEAF_LENGTH];
|
|
56
|
+
// Sibling nodes at each level of the state tree to verify the specific state leaf.
|
|
57
|
+
signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
58
|
+
|
|
59
|
+
// The ballot and related path elements.
|
|
60
|
+
signal input ballot[BALLOT_LENGTH];
|
|
61
|
+
signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
62
|
+
|
|
63
|
+
// The current vote weight and related path elements.
|
|
64
|
+
signal input currentVoteWeight;
|
|
65
|
+
signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
66
|
+
|
|
67
|
+
// Inputs related to the command being processed.
|
|
68
|
+
signal input commandStateIndex;
|
|
69
|
+
signal input commandPublicKey[2];
|
|
70
|
+
signal input commandVoteOptionIndex;
|
|
71
|
+
signal input commandNewVoteWeight;
|
|
72
|
+
signal input commandNonce;
|
|
73
|
+
signal input commandPollId;
|
|
74
|
+
signal input commandSalt;
|
|
75
|
+
signal input commandSignaturePoint[2];
|
|
76
|
+
signal input commandSignatureScalar;
|
|
77
|
+
signal input packedCommand[PACKED_COMMAND_LENGTH];
|
|
78
|
+
|
|
79
|
+
// The number of valid vote options for the poll.
|
|
80
|
+
signal input voteOptions;
|
|
81
|
+
|
|
82
|
+
// The new state root.
|
|
83
|
+
signal output newStateRoot;
|
|
84
|
+
// The new ballot root.
|
|
85
|
+
signal output newBallotRoot;
|
|
86
|
+
|
|
87
|
+
// equal to newBallotVoteOptionRootMux (Mux1).
|
|
88
|
+
signal newBallotVoteOptionRoot;
|
|
89
|
+
|
|
90
|
+
// 1. Transform a state leaf and a ballot with a command.
|
|
91
|
+
// The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
|
|
92
|
+
var computedNewstateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
|
|
93
|
+
(computedNewstateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerFull()(
|
|
94
|
+
totalSignups,
|
|
95
|
+
voteOptions,
|
|
96
|
+
[stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
|
|
97
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
98
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
99
|
+
currentVoteWeight,
|
|
100
|
+
commandStateIndex,
|
|
101
|
+
commandPublicKey,
|
|
102
|
+
commandVoteOptionIndex,
|
|
103
|
+
commandNewVoteWeight,
|
|
104
|
+
commandNonce,
|
|
105
|
+
commandPollId,
|
|
106
|
+
commandSalt,
|
|
107
|
+
commandSignaturePoint,
|
|
108
|
+
commandSignatureScalar,
|
|
109
|
+
packedCommand
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
// 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
|
|
113
|
+
// Otherwise, generate indices for command.stateIndex.
|
|
114
|
+
var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
|
|
115
|
+
var computedStateLeafPathIndices[stateTreeDepth] = MerklePathIndicesGenerator(stateTreeDepth)(stateIndexMux);
|
|
116
|
+
|
|
117
|
+
// 3. Verify that the original state leaf exists in the given state root.
|
|
118
|
+
var stateLeafHash = PoseidonHasher(3)(stateLeaf);
|
|
119
|
+
var computedStateRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
120
|
+
stateLeafHash,
|
|
121
|
+
actualStateTreeDepth,
|
|
122
|
+
computedStateLeafPathIndices,
|
|
123
|
+
stateLeafPathElements
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
computedStateRoot === currentStateRoot;
|
|
127
|
+
|
|
128
|
+
// 4. Verify that the original ballot exists in the given ballot root.
|
|
129
|
+
var computedBallot = PoseidonHasher(2)([
|
|
130
|
+
ballot[BALLOT_NONCE_INDEX],
|
|
131
|
+
ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
132
|
+
]);
|
|
133
|
+
|
|
134
|
+
var computedBallotRoot = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
135
|
+
computedBallot,
|
|
136
|
+
computedStateLeafPathIndices,
|
|
137
|
+
ballotPathElements
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
computedBallotRoot === currentBallotRoot;
|
|
141
|
+
|
|
142
|
+
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
143
|
+
// at commandVoteOptionIndex.
|
|
144
|
+
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
145
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
146
|
+
|
|
147
|
+
var computedCurrentVoteWeightRoot = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
148
|
+
currentVoteWeight,
|
|
149
|
+
computedCurrentVoteWeightPathIndices,
|
|
150
|
+
currentVoteWeightsPathElements
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
computedCurrentVoteWeightRoot === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
154
|
+
|
|
155
|
+
var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
|
|
156
|
+
var voiceCreditBalanceMux = Mux1()(
|
|
157
|
+
[
|
|
158
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
|
|
159
|
+
stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
|
|
160
|
+
],
|
|
161
|
+
computedIsValid
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
165
|
+
var computedNewVoteOptionTreeRoot = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
166
|
+
voteWeightMux,
|
|
167
|
+
computedCurrentVoteWeightPathIndices,
|
|
168
|
+
currentVoteWeightsPathElements
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
// The new vote option root in the ballot
|
|
172
|
+
var newBallotVoteOptionRootMux = Mux1()(
|
|
173
|
+
[ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeRoot],
|
|
174
|
+
computedIsValid
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
|
|
178
|
+
|
|
179
|
+
// 6. Generate a new state root.
|
|
180
|
+
var computedNewStateLeafhash = PoseidonHasher(3)([
|
|
181
|
+
computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
|
|
182
|
+
computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
|
|
183
|
+
voiceCreditBalanceMux
|
|
184
|
+
]);
|
|
185
|
+
|
|
186
|
+
var computedNewStateRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
187
|
+
computedNewStateLeafhash,
|
|
188
|
+
actualStateTreeDepth,
|
|
189
|
+
computedStateLeafPathIndices,
|
|
190
|
+
stateLeafPathElements
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
newStateRoot <== computedNewStateRoot;
|
|
194
|
+
|
|
195
|
+
// 7. Generate a new ballot root.
|
|
196
|
+
var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
|
|
197
|
+
var computedNewBallotRoot = MerkleTreeInclusionProof(stateTreeDepth)(
|
|
198
|
+
computedNewBallot,
|
|
199
|
+
computedStateLeafPathIndices,
|
|
200
|
+
ballotPathElements
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
newBallotRoot <== computedNewBallotRoot;
|
|
204
|
+
}
|
|
@@ -15,7 +15,8 @@ include "../../utils/trees/LeafExists.circom";
|
|
|
15
15
|
include "../../utils/trees/CheckRoot.circom";
|
|
16
16
|
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
17
17
|
include "../../utils/trees/BinaryMerkleRoot.circom";
|
|
18
|
-
include "../../utils/trees/
|
|
18
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
19
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* Proves the correctness of processing a batch of MACI messages.
|
|
@@ -208,20 +209,20 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
208
209
|
// Start from batchSize and decrement for process in reverse order.
|
|
209
210
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
210
211
|
// Process as vote type message.
|
|
211
|
-
var
|
|
212
|
-
var
|
|
213
|
-
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];
|
|
214
215
|
|
|
215
216
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
216
217
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
217
|
-
|
|
218
|
-
|
|
218
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
219
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
219
220
|
}
|
|
220
221
|
}
|
|
221
222
|
|
|
222
223
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
223
224
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
224
|
-
|
|
225
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
225
226
|
}
|
|
226
227
|
}
|
|
227
228
|
|
|
@@ -231,11 +232,11 @@ include "../../utils/trees/incrementalQuinaryTree.circom";
|
|
|
231
232
|
ballotRoots[i + 1],
|
|
232
233
|
actualStateTreeDepth,
|
|
233
234
|
currentStateLeaves[i],
|
|
234
|
-
|
|
235
|
+
computedCurrentStateLeavesPathElements,
|
|
235
236
|
currentBallots[i],
|
|
236
|
-
|
|
237
|
+
computedCurrentBallotPathElements,
|
|
237
238
|
currentVoteWeights[i],
|
|
238
|
-
|
|
239
|
+
computedCurrentVoteWeightsPathElements,
|
|
239
240
|
computedCommandsStateIndex[i],
|
|
240
241
|
computedCommandsNewPublicKey[i],
|
|
241
242
|
computedCommandsVoteOptionIndex[i],
|
|
@@ -383,9 +384,9 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
383
384
|
// 5. Verify that currentVoteWeight exists in the ballot's vote option root
|
|
384
385
|
// at commandVoteOptionIndex.
|
|
385
386
|
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
386
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] =
|
|
387
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
387
388
|
|
|
388
|
-
var computedCurrentVoteWeightQip =
|
|
389
|
+
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
389
390
|
currentVoteWeight,
|
|
390
391
|
computedCurrentVoteWeightPathIndices,
|
|
391
392
|
currentVoteWeightsPathElements
|
|
@@ -403,7 +404,7 @@ template ProcessOneNonQv(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
403
404
|
);
|
|
404
405
|
|
|
405
406
|
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
406
|
-
var computedNewVoteOptionTreeQip =
|
|
407
|
+
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
407
408
|
voteWeightMux,
|
|
408
409
|
computedCurrentVoteWeightPathIndices,
|
|
409
410
|
currentVoteWeightsPathElements
|
|
@@ -8,7 +8,7 @@ include "./unpack-element.circom";
|
|
|
8
8
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
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
|
|
|
@@ -18,22 +18,22 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
18
18
|
*/
|
|
19
19
|
template TallyVotesNonQv(
|
|
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] = MerklePathIndicesGenerator(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.
|
|
@@ -198,7 +199,7 @@ template TallyVotesNonQv(
|
|
|
198
199
|
signal input newSpentVoiceCreditSubtotalSalt;
|
|
199
200
|
|
|
200
201
|
// Compute the commitment to the current results.
|
|
201
|
-
var computedCurrentResultsRoot =
|
|
202
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
202
203
|
|
|
203
204
|
// Verify currentResultsCommitmentHash.
|
|
204
205
|
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
@@ -221,7 +222,7 @@ template TallyVotesNonQv(
|
|
|
221
222
|
isFirstCommitment === currentTallyCommitment;
|
|
222
223
|
|
|
223
224
|
// Compute the root of the new results.
|
|
224
|
-
var computedNewResultsRoot =
|
|
225
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
225
226
|
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
226
227
|
|
|
227
228
|
// Compute the commitment to the new spent voice credits value.
|
|
@@ -235,4 +236,3 @@ template TallyVotesNonQv(
|
|
|
235
236
|
|
|
236
237
|
computedNewTallyCommitment === newTallyCommitment;
|
|
237
238
|
}
|
|
238
|
-
|