@maci-protocol/circuits 0.0.0-ci.00107eb

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.
Files changed (59) hide show
  1. package/CHANGELOG.md +673 -0
  2. package/LICENSE +21 -0
  3. package/README.md +20 -0
  4. package/build/ts/compile.d.ts +10 -0
  5. package/build/ts/compile.d.ts.map +1 -0
  6. package/build/ts/compile.js +123 -0
  7. package/build/ts/compile.js.map +1 -0
  8. package/build/ts/generateZkeys.d.ts +9 -0
  9. package/build/ts/generateZkeys.d.ts.map +1 -0
  10. package/build/ts/generateZkeys.js +67 -0
  11. package/build/ts/generateZkeys.js.map +1 -0
  12. package/build/ts/info.d.ts +2 -0
  13. package/build/ts/info.d.ts.map +1 -0
  14. package/build/ts/info.js +72 -0
  15. package/build/ts/info.js.map +1 -0
  16. package/build/ts/types.d.ts +104 -0
  17. package/build/ts/types.d.ts.map +1 -0
  18. package/build/ts/types.js +3 -0
  19. package/build/ts/types.js.map +1 -0
  20. package/build/tsconfig.build.tsbuildinfo +1 -0
  21. package/circom/circuits.json +74 -0
  22. package/circom/coordinator/full/MessageProcessor.circom +253 -0
  23. package/circom/coordinator/full/SingleMessageProcessor.circom +203 -0
  24. package/circom/coordinator/non-qv/MessageProcessor.circom +252 -0
  25. package/circom/coordinator/non-qv/SingleMessageProcessor.circom +199 -0
  26. package/circom/coordinator/non-qv/VoteTally.circom +161 -0
  27. package/circom/coordinator/qv/MessageProcessor.circom +250 -0
  28. package/circom/coordinator/qv/SingleMessageProcessor.circom +207 -0
  29. package/circom/coordinator/qv/VoteTally.circom +179 -0
  30. package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
  31. package/circom/utils/CalculateTotal.circom +24 -0
  32. package/circom/utils/EdDSAPoseidonVerifier.circom +91 -0
  33. package/circom/utils/MessageHasher.circom +57 -0
  34. package/circom/utils/MessageToCommand.circom +107 -0
  35. package/circom/utils/PoseidonHasher.circom +29 -0
  36. package/circom/utils/PrivateToPublicKey.circom +38 -0
  37. package/circom/utils/VerifySignature.circom +39 -0
  38. package/circom/utils/full/MessageValidator.circom +91 -0
  39. package/circom/utils/full/StateLeafAndBallotTransformer.circom +122 -0
  40. package/circom/utils/non-qv/MessageValidator.circom +91 -0
  41. package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
  42. package/circom/utils/non-qv/StateLeafAndBallotTransformer.circom +105 -0
  43. package/circom/utils/qv/MessageValidator.circom +97 -0
  44. package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
  45. package/circom/utils/qv/StateLeafAndBallotTransformer.circom +105 -0
  46. package/circom/utils/trees/BinaryMerkleRoot.circom +65 -0
  47. package/circom/utils/trees/CheckRoot.circom +49 -0
  48. package/circom/utils/trees/LeafExists.circom +27 -0
  49. package/circom/utils/trees/MerkleTreeInclusionProof.circom +50 -0
  50. package/circom/utils/trees/QuinaryCheckRoot.circom +54 -0
  51. package/circom/utils/trees/QuinaryGeneratePathIndices.circom +44 -0
  52. package/circom/utils/trees/QuinaryLeafExists.circom +30 -0
  53. package/circom/utils/trees/QuinarySelector.circom +42 -0
  54. package/circom/utils/trees/QuinaryTreeInclusionProof.circom +55 -0
  55. package/circom/utils/trees/Splicer.circom +76 -0
  56. package/circom/voter/PollJoined.circom +43 -0
  57. package/circom/voter/PollJoining.circom +54 -0
  58. package/circomkit.json +18 -0
  59. 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
+ "MessageProcessorQv_10-20-2_test": {
15
+ "file": "./coordinator/qv/MessageProcessor",
16
+ "template": "MessageProcessorQv",
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
+ "MessageProcessorNonQv_10-20-2_test": {
31
+ "file": "./coordinator/non-qv/MessageProcessor",
32
+ "template": "MessageProcessorNonQv",
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
+ "VoteTallyQv_10-1-2_test": {
63
+ "file": "./coordinator/qv/VoteTally",
64
+ "template": "VoteTallyQv",
65
+ "params": [10, 1, 2],
66
+ "pubs": ["index", "totalSignups", "sbCommitment", "currentTallyCommitment", "newTallyCommitment"]
67
+ },
68
+ "VoteTallyNonQv_10-1-2_test": {
69
+ "file": "./coordinator/non-qv/VoteTally",
70
+ "template": "VoteTallyNonQv",
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,203 @@
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/BinaryMerkleRoot.circom";
9
+ include "../../utils/trees/QuinaryTreeInclusionProof.circom";
10
+ include "../../utils/trees/QuinaryGeneratePathIndices.circom";
11
+ include "../../utils/full/StateLeafAndBallotTransformer.circom";
12
+
13
+ /**
14
+ * Processes one message and updates the state accordingly.
15
+ * This template involves complex interactions, including transformations based on message type,
16
+ * validations against current states like voice credit balances or vote weights,
17
+ * and updates to Merkle trees representing state and ballot information.
18
+ * This is a critical building block for ensuring the integrity and correctness of MACI state.
19
+ * This template supports fully spent voice credits mode.
20
+ */
21
+ template SingleMessageProcessorFull(stateTreeDepth, voteOptionTreeDepth) {
22
+ // The number of elements in the ballot.
23
+ var BALLOT_LENGTH = 2;
24
+ // The number of elements in the state leaf.
25
+ var STATE_LEAF_LENGTH = 3;
26
+ // The number of elements in the vote option tree node (tree arity).
27
+ var VOTE_OPTION_TREE_ARITY = 5;
28
+ // The number of elements in the state tree node (tree arity).
29
+ var STATE_TREE_ARITY = 2;
30
+ // The number of elements in the packed command.
31
+ var PACKED_COMMAND_LENGTH = 4;
32
+ // Constants defining the structure and size of state and ballots.
33
+ var BALLOT_NONCE_INDEX = 0;
34
+ // Ballot vote option (vote option) root index.
35
+ var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
36
+
37
+ // Indices for elements within a state leaf.
38
+ // Public key.
39
+ var STATE_LEAF_PUBLIC_X_INDEX = 0;
40
+ var STATE_LEAF_PUBLIC_Y_INDEX = 1;
41
+ // Voice Credit balance.
42
+ var STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX = 2;
43
+
44
+ // Number of users that have completed the sign up.
45
+ signal input totalSignups;
46
+ // The current value of the state tree root.
47
+ signal input currentStateRoot;
48
+ // The current value of the ballot tree root.
49
+ signal input currentBallotRoot;
50
+ // The actual tree depth (might be <= stateTreeDepth).
51
+ signal input actualStateTreeDepth;
52
+
53
+ // The state leaf and related path elements.
54
+ signal input stateLeaf[STATE_LEAF_LENGTH];
55
+ // Sibling nodes at each level of the state tree to verify the specific state leaf.
56
+ signal input stateLeafPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
57
+
58
+ // The ballot and related path elements.
59
+ signal input ballot[BALLOT_LENGTH];
60
+ signal input ballotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
61
+
62
+ // The current vote weight and related path elements.
63
+ signal input currentVoteWeight;
64
+ signal input currentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
65
+
66
+ // Inputs related to the command being processed.
67
+ signal input commandStateIndex;
68
+ signal input commandPublicKey[2];
69
+ signal input commandVoteOptionIndex;
70
+ signal input commandNewVoteWeight;
71
+ signal input commandNonce;
72
+ signal input commandPollId;
73
+ signal input commandSalt;
74
+ signal input commandSignaturePoint[2];
75
+ signal input commandSignatureScalar;
76
+ signal input packedCommand[PACKED_COMMAND_LENGTH];
77
+
78
+ // The number of valid vote options for the poll.
79
+ signal input voteOptions;
80
+
81
+ // The new state root.
82
+ signal output newStateRoot;
83
+ // The new ballot root.
84
+ signal output newBallotRoot;
85
+
86
+ // equal to newBallotVoteOptionRootMux (Mux1).
87
+ signal newBallotVoteOptionRoot;
88
+
89
+ // 1. Transform a state leaf and a ballot with a command.
90
+ // The result is a new state leaf, a new ballot, and an isValid signal (0 or 1).
91
+ var computedNewstateLeafPublicKey[2], computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid;
92
+ (computedNewstateLeafPublicKey, computedNewBallotNonce, computedIsValid, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = StateLeafAndBallotTransformerFull()(
93
+ totalSignups,
94
+ voteOptions,
95
+ [stateLeaf[STATE_LEAF_PUBLIC_X_INDEX], stateLeaf[STATE_LEAF_PUBLIC_Y_INDEX]],
96
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
97
+ ballot[BALLOT_NONCE_INDEX],
98
+ currentVoteWeight,
99
+ commandStateIndex,
100
+ commandPublicKey,
101
+ commandVoteOptionIndex,
102
+ commandNewVoteWeight,
103
+ commandNonce,
104
+ commandPollId,
105
+ commandSalt,
106
+ commandSignaturePoint,
107
+ commandSignatureScalar,
108
+ packedCommand
109
+ );
110
+
111
+ // 2. If computedIsStateLeafIndexValid is equal to zero, generate indices for leaf zero.
112
+ // Otherwise, generate indices for command.stateIndex.
113
+ var stateIndexMux = Mux1()([0, commandStateIndex], computedIsStateLeafIndexValid);
114
+
115
+ // 3. Verify that the original state leaf exists in the given state root.
116
+ var stateLeafHash = PoseidonHasher(3)(stateLeaf);
117
+ var computedStateRoot = BinaryMerkleRoot(stateTreeDepth)(
118
+ stateLeafHash,
119
+ actualStateTreeDepth,
120
+ stateIndexMux,
121
+ stateLeafPathElements
122
+ );
123
+
124
+ computedStateRoot === currentStateRoot;
125
+
126
+ // 4. Verify that the original ballot exists in the given ballot root.
127
+ var computedBallot = PoseidonHasher(2)([
128
+ ballot[BALLOT_NONCE_INDEX],
129
+ ballot[BALLOT_VOTE_OPTION_ROOT_INDEX]
130
+ ]);
131
+ var computedStateLeafPathIndices[stateTreeDepth] = Num2Bits(stateTreeDepth)(stateIndexMux);
132
+
133
+ var computedBallotRoot = MerkleTreeInclusionProof(stateTreeDepth)(
134
+ computedBallot,
135
+ computedStateLeafPathIndices,
136
+ ballotPathElements
137
+ );
138
+
139
+ computedBallotRoot === currentBallotRoot;
140
+
141
+ // 5. Verify that currentVoteWeight exists in the ballot's vote option root
142
+ // at commandVoteOptionIndex.
143
+ var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
144
+ var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
145
+
146
+ var computedCurrentVoteWeightRoot = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
147
+ currentVoteWeight,
148
+ computedCurrentVoteWeightPathIndices,
149
+ currentVoteWeightsPathElements
150
+ );
151
+
152
+ computedCurrentVoteWeightRoot === ballot[BALLOT_VOTE_OPTION_ROOT_INDEX];
153
+
154
+ var voteWeightMux = Mux1()([currentVoteWeight, commandNewVoteWeight], computedIsValid);
155
+ var voiceCreditBalanceMux = Mux1()(
156
+ [
157
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX],
158
+ stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_INDEX] + currentVoteWeight - commandNewVoteWeight
159
+ ],
160
+ computedIsValid
161
+ );
162
+
163
+ // 5.1. Update the ballot's vote option root with the new vote weight.
164
+ var computedNewVoteOptionTreeRoot = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
165
+ voteWeightMux,
166
+ computedCurrentVoteWeightPathIndices,
167
+ currentVoteWeightsPathElements
168
+ );
169
+
170
+ // The new vote option root in the ballot
171
+ var newBallotVoteOptionRootMux = Mux1()(
172
+ [ballot[BALLOT_VOTE_OPTION_ROOT_INDEX], computedNewVoteOptionTreeRoot],
173
+ computedIsValid
174
+ );
175
+
176
+ newBallotVoteOptionRoot <== newBallotVoteOptionRootMux;
177
+
178
+ // 6. Generate a new state root.
179
+ var computedNewStateLeafhash = PoseidonHasher(3)([
180
+ computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_X_INDEX],
181
+ computedNewstateLeafPublicKey[STATE_LEAF_PUBLIC_Y_INDEX],
182
+ voiceCreditBalanceMux
183
+ ]);
184
+
185
+ var computedNewStateRoot = BinaryMerkleRoot(stateTreeDepth)(
186
+ computedNewStateLeafhash,
187
+ actualStateTreeDepth,
188
+ stateIndexMux,
189
+ stateLeafPathElements
190
+ );
191
+
192
+ newStateRoot <== computedNewStateRoot;
193
+
194
+ // 7. Generate a new ballot root.
195
+ var computedNewBallot = PoseidonHasher(2)([computedNewBallotNonce, newBallotVoteOptionRoot]);
196
+ var computedNewBallotRoot = MerkleTreeInclusionProof(stateTreeDepth)(
197
+ computedNewBallot,
198
+ computedStateLeafPathIndices,
199
+ ballotPathElements
200
+ );
201
+
202
+ newBallotRoot <== computedNewBallotRoot;
203
+ }