@maci-protocol/circuits 0.0.0-ci.01622be
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 +21 -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/generateZkeys.d.ts +9 -0
- package/build/ts/generateZkeys.d.ts.map +1 -0
- package/build/ts/generateZkeys.js +67 -0
- package/build/ts/generateZkeys.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 +104 -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 +74 -0
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +447 -0
- package/circom/coordinator/non-qv/tallyVotes.circom +238 -0
- package/circom/coordinator/qv/processMessages.circom +452 -0
- package/circom/coordinator/qv/tallyVotes.circom +279 -0
- package/circom/utils/CalculateTotal.circom +24 -0
- package/circom/utils/EdDSAPoseidonVerifier.circom +91 -0
- package/circom/utils/MessageHasher.circom +57 -0
- package/circom/utils/MessageToCommand.circom +107 -0
- package/circom/utils/PoseidonHasher.circom +29 -0
- package/circom/utils/PrivateToPublicKey.circom +38 -0
- package/circom/utils/VerifySignature.circom +39 -0
- package/circom/utils/full/MessageValidator.circom +91 -0
- package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
- package/circom/utils/non-qv/MessageValidator.circom +91 -0
- package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/qv/MessageValidator.circom +97 -0
- package/circom/utils/qv/StateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +62 -0
- package/circom/utils/trees/CheckRoot.circom +49 -0
- package/circom/utils/trees/LeafExists.circom +27 -0
- package/circom/utils/trees/MerklePathIndicesGenerator.circom +44 -0
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
- package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
- package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
- package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
- package/circom/utils/trees/QuinarySelector.circom +42 -0
- package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
- package/circom/utils/trees/Splicer.circom +76 -0
- package/circom/voter/PollJoined.circom +43 -0
- package/circom/voter/PollJoining.circom +54 -0
- package/circomkit.json +18 -0
- package/package.json +74 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"PollJoining_10_test": {
|
|
3
|
+
"file": "./voter/PollJoining",
|
|
4
|
+
"template": "PollJoining",
|
|
5
|
+
"params": [10],
|
|
6
|
+
"pubs": ["nullifier", "stateRoot", "pollPublicKey", "pollId"]
|
|
7
|
+
},
|
|
8
|
+
"PollJoined_10_test": {
|
|
9
|
+
"file": "./voter/PollJoined",
|
|
10
|
+
"template": "PollJoined",
|
|
11
|
+
"params": [10],
|
|
12
|
+
"pubs": ["stateRoot"]
|
|
13
|
+
},
|
|
14
|
+
"ProcessMessages_10-20-2_test": {
|
|
15
|
+
"file": "./coordinator/qv/processMessages",
|
|
16
|
+
"template": "ProcessMessages",
|
|
17
|
+
"params": [10, 20, 2],
|
|
18
|
+
"pubs": [
|
|
19
|
+
"totalSignups",
|
|
20
|
+
"index",
|
|
21
|
+
"batchEndIndex",
|
|
22
|
+
"currentSbCommitment",
|
|
23
|
+
"newSbCommitment",
|
|
24
|
+
"outputBatchHash",
|
|
25
|
+
"actualStateTreeDepth",
|
|
26
|
+
"coordinatorPublicKeyHash",
|
|
27
|
+
"voteOptions"
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
"ProcessMessagesNonQv_10-20-2_test": {
|
|
31
|
+
"file": "./coordinator/non-qv/processMessages",
|
|
32
|
+
"template": "ProcessMessagesNonQv",
|
|
33
|
+
"params": [10, 20, 2],
|
|
34
|
+
"pubs": [
|
|
35
|
+
"totalSignups",
|
|
36
|
+
"index",
|
|
37
|
+
"batchEndIndex",
|
|
38
|
+
"currentSbCommitment",
|
|
39
|
+
"newSbCommitment",
|
|
40
|
+
"outputBatchHash",
|
|
41
|
+
"actualStateTreeDepth",
|
|
42
|
+
"coordinatorPublicKeyHash",
|
|
43
|
+
"voteOptions"
|
|
44
|
+
]
|
|
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
|
+
},
|
|
62
|
+
"TallyVotes_10-1-2_test": {
|
|
63
|
+
"file": "./coordinator/qv/tallyVotes",
|
|
64
|
+
"template": "TallyVotes",
|
|
65
|
+
"params": [10, 1, 2],
|
|
66
|
+
"pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
|
|
67
|
+
},
|
|
68
|
+
"TallyVotesNonQv_10-1-2_test": {
|
|
69
|
+
"file": "./coordinator/non-qv/tallyVotes",
|
|
70
|
+
"template": "TallyVotesNonQv",
|
|
71
|
+
"params": [10, 1, 2],
|
|
72
|
+
"pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -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
|
+
}
|