@maci-protocol/circuits 0.0.0-ci.752ce71 → 0.0.0-ci.75ef321
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/build/ts/types.d.ts +4 -4
- package/build/ts/types.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +12 -12
- package/circom/coordinator/full/SingleMessageProcessor.circom +3 -4
- package/circom/coordinator/non-qv/{processMessages.circom → MessageProcessor.circom} +3 -198
- package/circom/coordinator/non-qv/SingleMessageProcessor.circom +199 -0
- package/circom/coordinator/non-qv/{tallyVotes.circom → VoteTally.circom} +3 -80
- package/circom/coordinator/qv/{processMessages.circom → MessageProcessor.circom} +4 -206
- package/circom/coordinator/qv/SingleMessageProcessor.circom +207 -0
- package/circom/coordinator/qv/{tallyVotes.circom → VoteTally.circom} +4 -104
- package/circom/coordinator/qv/VoteTallyWithIndividualCounts.circom +226 -0
- package/circom/utils/non-qv/ResultCommitmentVerifier.circom +84 -0
- package/circom/utils/qv/ResultCommitmentVerifier.circom +107 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +6 -3
- package/circom/voter/PollJoined.circom +3 -3
- package/circom/voter/PollJoining.circom +3 -3
- package/package.json +13 -13
- package/circom/utils/trees/MerklePathIndicesGenerator.circom +0 -44
|
@@ -0,0 +1,226 @@
|
|
|
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/CheckRoot.circom";
|
|
9
|
+
include "../../utils/trees/LeafExists.circom";
|
|
10
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
11
|
+
include "../../utils/qv/ResultCommitmentVerifier.circom";
|
|
12
|
+
include "../../utils/CalculateTotal.circom";
|
|
13
|
+
include "../../utils/PoseidonHasher.circom";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Processes batches of votes and verifies their validity in a Merkle tree structure.
|
|
17
|
+
* This template supports Quadratic Voting (QV) with individual vote counting.
|
|
18
|
+
* Note: this circuit is not using right now, this is a part of individual vote counts functionality.
|
|
19
|
+
* Not finished yet, don't use it in production. It is kept here for future use.
|
|
20
|
+
*/
|
|
21
|
+
template VoteTallyWithIndividualCountsQv(
|
|
22
|
+
stateTreeDepth,
|
|
23
|
+
tallyProcessingStateTreeDepth,
|
|
24
|
+
voteOptionTreeDepth
|
|
25
|
+
) {
|
|
26
|
+
// Ensure there's at least one level in the vote option tree.
|
|
27
|
+
assert(voteOptionTreeDepth > 0);
|
|
28
|
+
// Ensure the intermediate state tree has at least one level.
|
|
29
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
30
|
+
// The intermediate state tree must be smaller than the full state tree.
|
|
31
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
32
|
+
|
|
33
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
34
|
+
var TREE_ARITY = 5;
|
|
35
|
+
var BALLOT_TREE_ARITY = 2;
|
|
36
|
+
var VOTE_COUNTS_TREE_ARITY = 2;
|
|
37
|
+
|
|
38
|
+
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
39
|
+
var ballotBatchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
40
|
+
var voteCountsBatchSize = VOTE_COUNTS_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
41
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
42
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
43
|
+
|
|
44
|
+
// Number of elements in each ballot.
|
|
45
|
+
var BALLOT_LENGTH = 2;
|
|
46
|
+
// Index for the nonce in the ballot array.
|
|
47
|
+
var BALLOT_NONCE_INDEX = 0;
|
|
48
|
+
// Index for the voting option root in the ballot array.
|
|
49
|
+
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
50
|
+
// Difference in tree depths, used in path calculations.
|
|
51
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
52
|
+
// Number of elements in each vote count leaf.
|
|
53
|
+
var VOTE_COUNTS_LENGTH = 2;
|
|
54
|
+
// Index for the voting option index.
|
|
55
|
+
var VOTE_COUNTS_NONCE_INDEX = 0;
|
|
56
|
+
// Index for root of the vote count per option.
|
|
57
|
+
var VOTE_COUNTS_ROOT_INDEX = 1;
|
|
58
|
+
|
|
59
|
+
// Root of the state Merkle tree, representing the overall state before voting.
|
|
60
|
+
signal input stateRoot;
|
|
61
|
+
// Root of the ballot Merkle tree, representing the submitted ballots.
|
|
62
|
+
signal input ballotRoot;
|
|
63
|
+
// Root of the vote counts Merkle tree, representing the counts of votes for each option.
|
|
64
|
+
signal input voteCountsRoot;
|
|
65
|
+
// Salt used in commitment to secure the ballot data.
|
|
66
|
+
signal input sbSalt;
|
|
67
|
+
// Commitment to the state and ballots.
|
|
68
|
+
signal input sbCommitment;
|
|
69
|
+
// Commitment to the current tally before this batch.
|
|
70
|
+
signal input currentTallyCommitment;
|
|
71
|
+
// Commitment to the new tally after processing this batch.
|
|
72
|
+
signal input newTallyCommitment;
|
|
73
|
+
// Start index of given batch
|
|
74
|
+
signal input index;
|
|
75
|
+
// Number of users that signup
|
|
76
|
+
signal input totalSignups;
|
|
77
|
+
// Ballots and their corresponding path elements for verification in the tree.
|
|
78
|
+
signal input ballots[ballotBatchSize][BALLOT_LENGTH];
|
|
79
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
80
|
+
signal input votes[ballotBatchSize][totalVoteOptions];
|
|
81
|
+
// Individual vote count tree and their corresponding path elements for verification in the tree.
|
|
82
|
+
signal input voteCounts[voteCountsBatchSize][VOTE_COUNTS_LENGTH];
|
|
83
|
+
signal input voteCountsPathElements[STATE_TREE_DEPTH_DIFFERENCE][VOTE_COUNTS_TREE_ARITY - 1];
|
|
84
|
+
signal input voteCountsData[voteCountsBatchSize][totalVoteOptions];
|
|
85
|
+
// Current results for each vote option.
|
|
86
|
+
signal input currentResults[totalVoteOptions];
|
|
87
|
+
// Salt for the root of the current results.
|
|
88
|
+
signal input currentResultsRootSalt;
|
|
89
|
+
// Total voice credits spent so far.
|
|
90
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
91
|
+
// Salt for the total spent voice credits.
|
|
92
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
93
|
+
// Spent voice credits per vote option.
|
|
94
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
95
|
+
// Salt for the root of spent credits per option.
|
|
96
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
97
|
+
// Salt for the root of the new results.
|
|
98
|
+
signal input newResultsRootSalt;
|
|
99
|
+
// Salt for the new spent credits per vote option root.
|
|
100
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
101
|
+
// Salt for the new total spent voice credits root.
|
|
102
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
103
|
+
|
|
104
|
+
// Verify sbCommitment.
|
|
105
|
+
var computedSbCommitment = PoseidonHasher(3)([stateRoot, ballotRoot, sbSalt]);
|
|
106
|
+
computedSbCommitment === sbCommitment;
|
|
107
|
+
|
|
108
|
+
// Validates that the index is within the valid range of sign-ups.
|
|
109
|
+
var totalSignupsValid = LessEqThan(50)([index, totalSignups]);
|
|
110
|
+
totalSignupsValid === 1;
|
|
111
|
+
|
|
112
|
+
// Hashes each ballot for subroot generation, and checks the existence of the leaf in the Merkle tree.
|
|
113
|
+
var computedBallotHashers[ballotBatchSize];
|
|
114
|
+
var computedVoteCountsHashers[voteCountsBatchSize];
|
|
115
|
+
|
|
116
|
+
for (var i = 0; i < ballotBatchSize; i++) {
|
|
117
|
+
computedBallotHashers[i] = PoseidonHasher(2)([
|
|
118
|
+
ballots[i][BALLOT_NONCE_INDEX],
|
|
119
|
+
ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]
|
|
120
|
+
]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (var i = 0; i < voteCountsBatchSize; i++) {
|
|
124
|
+
computedVoteCountsHashers[i] = PoseidonHasher(2)([
|
|
125
|
+
voteCounts[i][VOTE_COUNTS_NONCE_INDEX],
|
|
126
|
+
voteCounts[i][VOTE_COUNTS_ROOT_INDEX]
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
131
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / ballotBatchSize);
|
|
132
|
+
|
|
133
|
+
var computedVoteCountsSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedVoteCountsHashers);
|
|
134
|
+
var computedVoteCountsPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = Num2Bits(STATE_TREE_DEPTH_DIFFERENCE)(index / voteCountsBatchSize);
|
|
135
|
+
|
|
136
|
+
// Verifies each ballot's existence within the ballot tree.
|
|
137
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
138
|
+
computedBallotSubroot,
|
|
139
|
+
ballotPathElements,
|
|
140
|
+
computedBallotPathIndices,
|
|
141
|
+
ballotRoot
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// Verifies each vote count's existence within the vote count tree.
|
|
145
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
146
|
+
computedVoteCountsSubroot,
|
|
147
|
+
voteCountsPathElements,
|
|
148
|
+
computedVoteCountsPathIndices,
|
|
149
|
+
voteCountsRoot
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
// Processes vote options, verifying each against its declared root.
|
|
153
|
+
var computedVoteTree[ballotBatchSize];
|
|
154
|
+
|
|
155
|
+
for (var i = 0; i < ballotBatchSize; i++) {
|
|
156
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
157
|
+
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Processes vote counts, verifying each against its declared root.
|
|
161
|
+
var computedVoteCountsTree[voteCountsBatchSize];
|
|
162
|
+
|
|
163
|
+
for (var i = 0; i < voteCountsBatchSize; i++) {
|
|
164
|
+
computedVoteCountsTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(voteCountsData[i]);
|
|
165
|
+
computedVoteCountsTree[i] === voteCounts[i][VOTE_COUNTS_ROOT_INDEX];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Calculates new results and spent voice credits based on the current and incoming votes.
|
|
169
|
+
var computedIsFirstBatch = IsZero()(index);
|
|
170
|
+
var computedIsZero = IsZero()(computedIsFirstBatch);
|
|
171
|
+
|
|
172
|
+
// Tally the new results.
|
|
173
|
+
var computedCalculateTotalResult[totalVoteOptions];
|
|
174
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
175
|
+
var numsRC[ballotBatchSize + 1];
|
|
176
|
+
numsRC[ballotBatchSize] = currentResults[i] * computedIsZero;
|
|
177
|
+
for (var j = 0; j < ballotBatchSize; j++) {
|
|
178
|
+
numsRC[j] = votes[j][i];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
computedCalculateTotalResult[i] = CalculateTotal(ballotBatchSize + 1)(numsRC);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Tally the new spent voice credit total.
|
|
185
|
+
var numsSVC[ballotBatchSize * totalVoteOptions + 1];
|
|
186
|
+
numsSVC[ballotBatchSize * totalVoteOptions] = currentSpentVoiceCreditSubtotal * computedIsZero;
|
|
187
|
+
for (var i = 0; i < ballotBatchSize; i++) {
|
|
188
|
+
for (var j = 0; j < totalVoteOptions; j++) {
|
|
189
|
+
numsSVC[i * totalVoteOptions + j] = votes[i][j] * votes[i][j];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
var computedNewSpentVoiceCreditSubtotal = CalculateTotal(ballotBatchSize * totalVoteOptions + 1)(numsSVC);
|
|
194
|
+
|
|
195
|
+
// Tally the spent voice credits per vote option.
|
|
196
|
+
var computedNewPerVOSpentVoiceCredits[totalVoteOptions];
|
|
197
|
+
|
|
198
|
+
for (var i = 0; i < totalVoteOptions; i++) {
|
|
199
|
+
var computedTotalVoiceCreditSpent[ballotBatchSize + 1];
|
|
200
|
+
computedTotalVoiceCreditSpent[ballotBatchSize] = currentPerVoteOptionSpentVoiceCredits[i] * computedIsZero;
|
|
201
|
+
for (var j = 0; j < ballotBatchSize; j++) {
|
|
202
|
+
computedTotalVoiceCreditSpent[j] = votes[j][i] * votes[j][i];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
computedNewPerVOSpentVoiceCredits[i] = CalculateTotal(ballotBatchSize + 1)(computedTotalVoiceCreditSpent);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Verifies the updated results and spent credits, ensuring consistency and correctness of tally updates.
|
|
209
|
+
ResultCommitmentVerifierQv(voteOptionTreeDepth)(
|
|
210
|
+
computedIsFirstBatch,
|
|
211
|
+
currentTallyCommitment,
|
|
212
|
+
newTallyCommitment,
|
|
213
|
+
currentResults,
|
|
214
|
+
currentResultsRootSalt,
|
|
215
|
+
computedCalculateTotalResult,
|
|
216
|
+
newResultsRootSalt,
|
|
217
|
+
currentSpentVoiceCreditSubtotal,
|
|
218
|
+
currentSpentVoiceCreditSubtotalSalt,
|
|
219
|
+
computedNewSpentVoiceCreditSubtotal,
|
|
220
|
+
newSpentVoiceCreditSubtotalSalt,
|
|
221
|
+
currentPerVoteOptionSpentVoiceCredits,
|
|
222
|
+
currentPerVoteOptionSpentVoiceCreditsRootSalt,
|
|
223
|
+
computedNewPerVOSpentVoiceCredits,
|
|
224
|
+
newPerVoteOptionSpentVoiceCreditsRootSalt
|
|
225
|
+
);
|
|
226
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../trees/QuinaryCheckRoot.circom";
|
|
7
|
+
include "../PoseidonHasher.circom";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Performs verifications and computations related to current voting results.
|
|
11
|
+
* Also, computes and outputs a commitment to the new results.
|
|
12
|
+
* This template does not support Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template ResultCommitmentVerifierNonQv(voteOptionTreeDepth) {
|
|
15
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
16
|
+
var TREE_ARITY = 5;
|
|
17
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
18
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
19
|
+
|
|
20
|
+
// Equal to 1 if this is the first batch, otherwise 0.
|
|
21
|
+
signal input isFirstBatch;
|
|
22
|
+
// Commitment to the current tally before this batch.
|
|
23
|
+
signal input currentTallyCommitment;
|
|
24
|
+
// Commitment to the new tally after processing this batch.
|
|
25
|
+
signal input newTallyCommitment;
|
|
26
|
+
|
|
27
|
+
// Current results for each vote option.
|
|
28
|
+
signal input currentResults[totalVoteOptions];
|
|
29
|
+
// Salt for the root of the current results.
|
|
30
|
+
signal input currentResultsRootSalt;
|
|
31
|
+
|
|
32
|
+
// New results for each vote option.
|
|
33
|
+
signal input newResults[totalVoteOptions];
|
|
34
|
+
// Salt for the root of the new results.
|
|
35
|
+
signal input newResultsRootSalt;
|
|
36
|
+
|
|
37
|
+
// Total voice credits spent so far.
|
|
38
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
39
|
+
// Salt for the total spent voice credits.
|
|
40
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
41
|
+
|
|
42
|
+
// Total new voice credits spent.
|
|
43
|
+
signal input newSpentVoiceCreditSubtotal;
|
|
44
|
+
// Salt for the new total spent voice credits.
|
|
45
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
46
|
+
|
|
47
|
+
// Compute the commitment to the current results.
|
|
48
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
49
|
+
|
|
50
|
+
// Verify currentResultsCommitmentHash.
|
|
51
|
+
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
52
|
+
|
|
53
|
+
// Compute the commitment to the current spent voice credits.
|
|
54
|
+
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
55
|
+
|
|
56
|
+
// Commit to the current tally
|
|
57
|
+
var computedCurrentTallyCommitment = PoseidonHasher(2)([computedCurrentResultsCommitment, computedCurrentSpentVoiceCreditsCommitment]);
|
|
58
|
+
|
|
59
|
+
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
60
|
+
// computedIsZero.out is 1 if this is not the first batch.
|
|
61
|
+
// computedIsZero.out is 0 if this is the first batch.
|
|
62
|
+
var computedIsZero = IsZero()(isFirstBatch);
|
|
63
|
+
|
|
64
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
65
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
66
|
+
signal isFirstCommitment;
|
|
67
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
68
|
+
isFirstCommitment === currentTallyCommitment;
|
|
69
|
+
|
|
70
|
+
// Compute the root of the new results.
|
|
71
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
72
|
+
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
73
|
+
|
|
74
|
+
// Compute the commitment to the new spent voice credits value.
|
|
75
|
+
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
76
|
+
|
|
77
|
+
// Commit to the new tally.
|
|
78
|
+
var computedNewTallyCommitment = PoseidonHasher(2)([
|
|
79
|
+
computedNewResultsCommitment,
|
|
80
|
+
computedNewSpentVoiceCreditsCommitment
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
computedNewTallyCommitment === newTallyCommitment;
|
|
84
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../trees/QuinaryCheckRoot.circom";
|
|
7
|
+
include "../PoseidonHasher.circom";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Performs verifications and computations related to current voting results.
|
|
11
|
+
* Also, computes and outputs a commitment to the new results.
|
|
12
|
+
* This template supports the Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template ResultCommitmentVerifierQv(voteOptionTreeDepth) {
|
|
15
|
+
// Number of children per node in the tree, defining the tree's branching factor.
|
|
16
|
+
var TREE_ARITY = 5;
|
|
17
|
+
// Number of voting options available, determined by the depth of the vote option tree.
|
|
18
|
+
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
19
|
+
|
|
20
|
+
// Equal to 1 if this is the first batch, otherwise 0.
|
|
21
|
+
signal input isFirstBatch;
|
|
22
|
+
// Commitment to the current tally before this batch.
|
|
23
|
+
signal input currentTallyCommitment;
|
|
24
|
+
// Commitment to the new tally after processing this batch.
|
|
25
|
+
signal input newTallyCommitment;
|
|
26
|
+
|
|
27
|
+
// Current results for each vote option.
|
|
28
|
+
signal input currentResults[totalVoteOptions];
|
|
29
|
+
// Salt for the root of the current results.
|
|
30
|
+
signal input currentResultsRootSalt;
|
|
31
|
+
|
|
32
|
+
// New results for each vote option.
|
|
33
|
+
signal input newResults[totalVoteOptions];
|
|
34
|
+
// Salt for the root of the new results.
|
|
35
|
+
signal input newResultsRootSalt;
|
|
36
|
+
|
|
37
|
+
// Total voice credits spent so far.
|
|
38
|
+
signal input currentSpentVoiceCreditSubtotal;
|
|
39
|
+
// Salt for the total spent voice credits.
|
|
40
|
+
signal input currentSpentVoiceCreditSubtotalSalt;
|
|
41
|
+
|
|
42
|
+
// Total new voice credits spent.
|
|
43
|
+
signal input newSpentVoiceCreditSubtotal;
|
|
44
|
+
// Salt for the new total spent voice credits.
|
|
45
|
+
signal input newSpentVoiceCreditSubtotalSalt;
|
|
46
|
+
|
|
47
|
+
// Spent voice credits per vote option.
|
|
48
|
+
signal input currentPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
49
|
+
// Salt for the root of spent credits per option.
|
|
50
|
+
signal input currentPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
51
|
+
|
|
52
|
+
// New spent voice credits per vote option.
|
|
53
|
+
signal input newPerVoteOptionSpentVoiceCredits[totalVoteOptions];
|
|
54
|
+
// Salt for the root of new spent credits per option.
|
|
55
|
+
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
56
|
+
|
|
57
|
+
// Compute the commitment to the current results.
|
|
58
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
59
|
+
|
|
60
|
+
// Verify currentResultsCommitmentHash.
|
|
61
|
+
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
62
|
+
|
|
63
|
+
// Compute the commitment to the current spent voice credits.
|
|
64
|
+
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
65
|
+
|
|
66
|
+
// Compute the root of the spent voice credits per vote option.
|
|
67
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
|
|
68
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
69
|
+
|
|
70
|
+
// Commit to the current tally.
|
|
71
|
+
var computedCurrentTallyCommitment = PoseidonHasher(3)([
|
|
72
|
+
computedCurrentResultsCommitment,
|
|
73
|
+
computedCurrentSpentVoiceCreditsCommitment,
|
|
74
|
+
computedCurrentPerVoteOptionSpentVoiceCreditsCommitment
|
|
75
|
+
]);
|
|
76
|
+
|
|
77
|
+
// Check if the current tally commitment is correct only if this is not the first batch.
|
|
78
|
+
// computedIsZero.out is 1 if this is not the first batch.
|
|
79
|
+
// computedIsZero.out is 0 if this is the first batch.
|
|
80
|
+
var computedIsZero = IsZero()(isFirstBatch);
|
|
81
|
+
|
|
82
|
+
// isFirstCommitment is 0 if this is the first batch, currentTallyCommitment should be 0 if this is the first batch.
|
|
83
|
+
// isFirstCommitment is 1 if this is not the first batch, currentTallyCommitment should not be 0 if this is the first batch.
|
|
84
|
+
signal isFirstCommitment;
|
|
85
|
+
isFirstCommitment <== computedIsZero * computedCurrentTallyCommitment;
|
|
86
|
+
isFirstCommitment === currentTallyCommitment;
|
|
87
|
+
|
|
88
|
+
// Compute the root of the new results.
|
|
89
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
90
|
+
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
91
|
+
|
|
92
|
+
// Compute the commitment to the new spent voice credits value.
|
|
93
|
+
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
94
|
+
|
|
95
|
+
// Compute the root of the spent voice credits per vote option.
|
|
96
|
+
var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
|
|
97
|
+
var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
98
|
+
|
|
99
|
+
// Commit to the new tally.
|
|
100
|
+
var computedNewTallyCommitment = PoseidonHasher(3)([
|
|
101
|
+
computedNewResultsCommitment,
|
|
102
|
+
computedNewSpentVoiceCreditsCommitment,
|
|
103
|
+
computedNewPerVoteOptionSpentVoiceCreditsCommitment
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
computedNewTallyCommitment === newTallyCommitment;
|
|
107
|
+
}
|
|
@@ -7,7 +7,7 @@ include "./comparators.circom";
|
|
|
7
7
|
include "../PoseidonHasher.circom";
|
|
8
8
|
|
|
9
9
|
// @note taken from @zk-kit/circuits
|
|
10
|
-
// if used directly in
|
|
10
|
+
// if used directly in MessageProcessor circom complains about duplicated
|
|
11
11
|
// templates (Ark, Poseidon, etc.)
|
|
12
12
|
// This circuit is designed to calculate the root of a binary Merkle
|
|
13
13
|
// tree given a leaf, its depth, and the necessary sibling
|
|
@@ -25,14 +25,17 @@ template BinaryMerkleRoot(MAX_DEPTH) {
|
|
|
25
25
|
signal input leaf;
|
|
26
26
|
// The depth of the Merkle tree.
|
|
27
27
|
signal input depth;
|
|
28
|
-
// The
|
|
29
|
-
signal input
|
|
28
|
+
// The index of the leaf node in the Merkle tree.
|
|
29
|
+
signal input index;
|
|
30
30
|
// The sibling nodes of the leaf node in the Merkle tree.
|
|
31
31
|
signal input siblings[MAX_DEPTH][1];
|
|
32
32
|
|
|
33
33
|
// The output of the Merkle tree root.
|
|
34
34
|
signal output out;
|
|
35
35
|
|
|
36
|
+
// The indices of the leaf node in the Merkle tree.
|
|
37
|
+
signal indices[MAX_DEPTH] <== Num2Bits(MAX_DEPTH)(index);
|
|
38
|
+
|
|
36
39
|
signal nodes[MAX_DEPTH + 1];
|
|
37
40
|
nodes[0] <== leaf;
|
|
38
41
|
|
|
@@ -19,8 +19,8 @@ template PollJoined(stateTreeDepth) {
|
|
|
19
19
|
signal input voiceCreditsBalance;
|
|
20
20
|
// Path elements
|
|
21
21
|
signal input pathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
22
|
-
//
|
|
23
|
-
signal input
|
|
22
|
+
// Index
|
|
23
|
+
signal input index;
|
|
24
24
|
// Poll State tree root which proves the user is joined
|
|
25
25
|
signal input stateRoot;
|
|
26
26
|
// The actual tree depth (might be <= stateTreeDepth) Used in BinaryMerkleRoot
|
|
@@ -35,7 +35,7 @@ template PollJoined(stateTreeDepth) {
|
|
|
35
35
|
var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
36
36
|
stateLeaf,
|
|
37
37
|
actualStateTreeDepth,
|
|
38
|
-
|
|
38
|
+
index,
|
|
39
39
|
pathElements
|
|
40
40
|
);
|
|
41
41
|
|
|
@@ -18,8 +18,8 @@ template PollJoining(stateTreeDepth) {
|
|
|
18
18
|
signal input pollPublicKey[2];
|
|
19
19
|
// Siblings
|
|
20
20
|
signal input siblings[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
21
|
-
//
|
|
22
|
-
signal input
|
|
21
|
+
// Index
|
|
22
|
+
signal input index;
|
|
23
23
|
// User's hashed private key
|
|
24
24
|
signal input nullifier;
|
|
25
25
|
// MACI State tree root which proves the user is signed up
|
|
@@ -46,7 +46,7 @@ template PollJoining(stateTreeDepth) {
|
|
|
46
46
|
var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
47
47
|
publicKeyHash,
|
|
48
48
|
actualStateTreeDepth,
|
|
49
|
-
|
|
49
|
+
index,
|
|
50
50
|
siblings
|
|
51
51
|
);
|
|
52
52
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maci-protocol/circuits",
|
|
3
|
-
"version": "0.0.0-ci.
|
|
3
|
+
"version": "0.0.0-ci.75ef321",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "zk-SNARK circuits for MACI",
|
|
6
6
|
"main": "build/ts/index.js",
|
|
@@ -38,37 +38,37 @@
|
|
|
38
38
|
"test:verifySignature": "pnpm run mocha-test ts/__tests__/VerifySignature.test.ts",
|
|
39
39
|
"test:privateToPublicKey": "pnpm run mocha-test ts/__tests__/PrivateToPublicKey.test.ts",
|
|
40
40
|
"test:calculateTotal": "pnpm run mocha-test ts/__tests__/CalculateTotal.test.ts",
|
|
41
|
-
"test:
|
|
42
|
-
"test:
|
|
41
|
+
"test:messageProcessor": "pnpm run mocha-test ts/__tests__/MessageProcessor.test.ts",
|
|
42
|
+
"test:voteTally": "pnpm run mocha-test ts/__tests__/VoteTally.test.ts",
|
|
43
43
|
"test:ceremonyParams": "pnpm run mocha-test ts/__tests__/CeremonyParams.test.ts",
|
|
44
44
|
"test:incrementalQuinaryTree": "pnpm run mocha-test ts/__tests__/IncrementalQuinaryTree.test.ts",
|
|
45
45
|
"test:pollJoining": "pnpm run mocha-test ts/__tests__/PollJoining.test.ts",
|
|
46
46
|
"test:pollJoined": "pnpm run mocha-test ts/__tests__/PollJoined.test.ts"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@maci-protocol/core": "0.0.0-ci.
|
|
50
|
-
"@maci-protocol/crypto": "0.0.0-ci.
|
|
51
|
-
"@maci-protocol/domainobjs": "0.0.0-ci.
|
|
52
|
-
"@maci-protocol/sdk": "0.0.0-ci.
|
|
49
|
+
"@maci-protocol/core": "0.0.0-ci.75ef321",
|
|
50
|
+
"@maci-protocol/crypto": "0.0.0-ci.75ef321",
|
|
51
|
+
"@maci-protocol/domainobjs": "0.0.0-ci.75ef321",
|
|
52
|
+
"@maci-protocol/sdk": "0.0.0-ci.75ef321",
|
|
53
53
|
"@zk-kit/circuits": "^0.4.0",
|
|
54
|
-
"circomkit": "^0.3.
|
|
54
|
+
"circomkit": "^0.3.4",
|
|
55
55
|
"circomlib": "^2.0.5"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@types/chai": "^4.3.11",
|
|
59
59
|
"@types/chai-as-promised": "^7.1.8",
|
|
60
60
|
"@types/mocha": "^10.0.10",
|
|
61
|
-
"@types/node": "^
|
|
61
|
+
"@types/node": "^24.0.13",
|
|
62
62
|
"@types/snarkjs": "^0.7.9",
|
|
63
63
|
"@zk-kit/baby-jubjub": "^1.0.3",
|
|
64
64
|
"chai": "^4.3.10",
|
|
65
65
|
"chai-as-promised": "^7.1.2",
|
|
66
|
-
"fast-check": "^4.
|
|
67
|
-
"glob": "^11.0.
|
|
68
|
-
"mocha": "^11.
|
|
66
|
+
"fast-check": "^4.2.0",
|
|
67
|
+
"glob": "^11.0.3",
|
|
68
|
+
"mocha": "^11.7.1",
|
|
69
69
|
"ts-mocha": "^11.1.0",
|
|
70
70
|
"ts-node": "^10.9.1",
|
|
71
71
|
"typescript": "^5.8.3"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "bf5073ee655484a0df3ea49675976a0f2788faeb"
|
|
74
74
|
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
pragma circom 2.0.0;
|
|
2
|
-
|
|
3
|
-
// zk-kit imports
|
|
4
|
-
include "./safe-comparators.circom";
|
|
5
|
-
// local import
|
|
6
|
-
include "../CalculateTotal.circom";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Calculates the path indices required for Merkle proof verifications.
|
|
10
|
-
* Given a node index within an IMT and the total tree levels, it outputs the path indices leading to that node.
|
|
11
|
-
* The template handles the modulo and division operations to break down the tree index into its constituent path indices.
|
|
12
|
-
*/
|
|
13
|
-
template MerklePathIndicesGenerator(levels) {
|
|
14
|
-
// The base used for the modulo and division operations, set to 2 for binary trees.
|
|
15
|
-
var BASE = 2;
|
|
16
|
-
|
|
17
|
-
// The total sum of the path indices.
|
|
18
|
-
signal input indices;
|
|
19
|
-
|
|
20
|
-
// The generated path indices.
|
|
21
|
-
signal output out[levels];
|
|
22
|
-
|
|
23
|
-
var computedIndices = indices;
|
|
24
|
-
var computedResults[levels];
|
|
25
|
-
|
|
26
|
-
for (var i = 0; i < levels; i++) {
|
|
27
|
-
// circom's best practices suggests to avoid using <-- unless you
|
|
28
|
-
// are aware of what's going on. This is the only way to do modulo operation.
|
|
29
|
-
out[i] <-- computedIndices % BASE;
|
|
30
|
-
computedIndices = computedIndices \ BASE;
|
|
31
|
-
|
|
32
|
-
// Check that each output element is less than the base.
|
|
33
|
-
var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
|
|
34
|
-
computedIsOutputElementLessThanBase === 1;
|
|
35
|
-
|
|
36
|
-
// Re-compute the total sum.
|
|
37
|
-
computedResults[i] = out[i] * (BASE ** i);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Check that the total sum matches the index.
|
|
41
|
-
var computedCalculateTotal = CalculateTotal(levels)(computedResults);
|
|
42
|
-
|
|
43
|
-
computedCalculateTotal === indices;
|
|
44
|
-
}
|