@maci-protocol/circuits 0.0.0-ci.044d30d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +673 -0
- package/LICENSE +22 -0
- package/README.md +20 -0
- package/build/ts/compile.d.ts +10 -0
- package/build/ts/compile.d.ts.map +1 -0
- package/build/ts/compile.js +123 -0
- package/build/ts/compile.js.map +1 -0
- package/build/ts/genZkeys.d.ts +9 -0
- package/build/ts/genZkeys.d.ts.map +1 -0
- package/build/ts/genZkeys.js +67 -0
- package/build/ts/genZkeys.js.map +1 -0
- package/build/ts/info.d.ts +2 -0
- package/build/ts/info.d.ts.map +1 -0
- package/build/ts/info.js +72 -0
- package/build/ts/info.js.map +1 -0
- package/build/ts/types.d.ts +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 +58 -0
- package/circom/coordinator/non-qv/processMessages.circom +449 -0
- package/circom/coordinator/non-qv/tallyVotes.circom +235 -0
- package/circom/coordinator/qv/processMessages.circom +451 -0
- package/circom/coordinator/qv/tallyVotes.circom +279 -0
- package/circom/utils/calculateTotal.circom +22 -0
- package/circom/utils/hashers.circom +78 -0
- package/circom/utils/messageToCommand.circom +78 -0
- package/circom/utils/non-qv/messageValidator.circom +89 -0
- package/circom/utils/non-qv/stateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/privToPubKey.circom +36 -0
- package/circom/utils/qv/messageValidator.circom +95 -0
- package/circom/utils/qv/stateLeafAndBallotTransformer.circom +105 -0
- package/circom/utils/trees/BinaryMerkleRoot.circom +54 -0
- package/circom/utils/trees/CheckRoot.circom +45 -0
- package/circom/utils/trees/LeafExists.circom +27 -0
- package/circom/utils/trees/MerkleGeneratePathIndices.circom +40 -0
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +49 -0
- package/circom/utils/trees/incrementalQuinaryTree.circom +287 -0
- package/circom/utils/verifySignature.circom +117 -0
- package/circom/voter/poll.circom +92 -0
- package/circomkit.json +18 -0
- package/package.json +71 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib imports
|
|
4
|
+
include "./bitify.circom";
|
|
5
|
+
include "./mux1.circom";
|
|
6
|
+
// zk-kit import
|
|
7
|
+
include "./safe-comparators.circom";
|
|
8
|
+
// local imports
|
|
9
|
+
include "../calculateTotal.circom";
|
|
10
|
+
include "../hashers.circom";
|
|
11
|
+
|
|
12
|
+
// Incremental Quintary Merkle Tree (IQT) verification circuits.
|
|
13
|
+
// Since each node contains 5 leaves, we are using PoseidonT6 for hashing them.
|
|
14
|
+
//
|
|
15
|
+
// nb. circom has some particularities which limit the code patterns we can use:
|
|
16
|
+
// - You can only assign a value to a signal once.
|
|
17
|
+
// - A component's input signal must only be wired to another component's output signal.
|
|
18
|
+
// - Variables can store linear combinations, and can also be used for loops,
|
|
19
|
+
// declaring sizes of things, and anything that is not related to inputs of a circuit.
|
|
20
|
+
// - The compiler fails whenever you try to mix invalid elements.
|
|
21
|
+
// - You can't use a signal as a list index.
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Selects an item from a list based on the given index.
|
|
25
|
+
* It verifies the index is within the valid range and then iterates over the inputs to find the match.
|
|
26
|
+
* For each item, it checks if its position equals the given index and if so, multiplies the item
|
|
27
|
+
* by the result of the equality check, effectively selecting it.
|
|
28
|
+
* The sum of these results yields the selected item, ensuring only the item at the specified index be the output.
|
|
29
|
+
*
|
|
30
|
+
* nb. The number of items must be less than 8, and the index must be less than the number of items.
|
|
31
|
+
*/
|
|
32
|
+
template QuinSelector(choices) {
|
|
33
|
+
signal input in[choices];
|
|
34
|
+
signal input index;
|
|
35
|
+
signal output out;
|
|
36
|
+
|
|
37
|
+
// Ensure that index < choices.
|
|
38
|
+
var computedLtIndex = SafeLessThan(3)([index, choices]);
|
|
39
|
+
computedLtIndex === 1;
|
|
40
|
+
|
|
41
|
+
// Initialize an array to hold the results of equality checks.
|
|
42
|
+
var computedResults[choices];
|
|
43
|
+
|
|
44
|
+
// For each item, check whether its index equals the input index.
|
|
45
|
+
// The result is multiplied by the corresponding input value.
|
|
46
|
+
for (var i = 0; i < choices; i++) {
|
|
47
|
+
var computedIsIndexEqual = IsEqual()([i, index]);
|
|
48
|
+
|
|
49
|
+
computedResults[i] = computedIsIndexEqual * in[i];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Calculate the total sum of the results array.
|
|
53
|
+
out <== CalculateTotal(choices)(computedResults);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The output array contains the input items, with the leaf inserted at the
|
|
58
|
+
* specified index. For example, if input = [0, 20, 30, 40], index = 3, and
|
|
59
|
+
* leaf = 10, the output will be [0, 20, 30, 10, 40].
|
|
60
|
+
*/
|
|
61
|
+
template Splicer(numItems) {
|
|
62
|
+
// The number of output items (because only one item is inserted).
|
|
63
|
+
var NUM_OUTPUT_ITEMS = numItems + 1;
|
|
64
|
+
|
|
65
|
+
signal input in[numItems];
|
|
66
|
+
signal input leaf;
|
|
67
|
+
signal input index;
|
|
68
|
+
signal output out[NUM_OUTPUT_ITEMS];
|
|
69
|
+
|
|
70
|
+
// There is a loop where the goal is to assign values to the output signal.
|
|
71
|
+
//
|
|
72
|
+
// | output[0] | output[1] | output[2] | ...
|
|
73
|
+
//
|
|
74
|
+
// We can either assign the leaf, or an item from the `items` signal, to the output, using Mux1().
|
|
75
|
+
// The Mux1's selector is 0 or 1 depending on whether the index is equal to the loop counter.
|
|
76
|
+
//
|
|
77
|
+
// i --> [IsEqual] <-- index
|
|
78
|
+
// |
|
|
79
|
+
// v
|
|
80
|
+
// leaf --> [Mux1] <-- <item from in>
|
|
81
|
+
// |
|
|
82
|
+
// v
|
|
83
|
+
// output[m]
|
|
84
|
+
//
|
|
85
|
+
// To obtain the value from <item from in>, we need to compute an item
|
|
86
|
+
// index (let it be `s`).
|
|
87
|
+
// 1. if index = 2 and i = 0, then s = 0
|
|
88
|
+
// 2. if index = 2 and i = 1, then s = 1
|
|
89
|
+
// 3. if index = 2 and i = 2, then s = 2
|
|
90
|
+
// 4. if index = 2 and i = 3, then s = 2
|
|
91
|
+
// 5. if index = 2 and i = 4, then s = 3
|
|
92
|
+
// We then wire `s`, as well as each item in `in` to a QuinSelector.
|
|
93
|
+
// The output signal from the QuinSelector is <item from in> and gets
|
|
94
|
+
// wired to Mux1 (as above).
|
|
95
|
+
|
|
96
|
+
var inputs[NUM_OUTPUT_ITEMS];
|
|
97
|
+
|
|
98
|
+
for (var i = 0; i < numItems; i++) {
|
|
99
|
+
inputs[i] = in[i];
|
|
100
|
+
}
|
|
101
|
+
inputs[NUM_OUTPUT_ITEMS - 1] = 0;
|
|
102
|
+
|
|
103
|
+
for (var i = 0; i < NUM_OUTPUT_ITEMS; i++) {
|
|
104
|
+
// Determines if current index is greater than the insertion index.
|
|
105
|
+
var computedIsIndexAfterInsertPoint = SafeGreaterThan(3)([i, index]);
|
|
106
|
+
|
|
107
|
+
// Calculates correct index for original items, adjusting for leaf insertion.
|
|
108
|
+
var computedAdjustedIndex = i - computedIsIndexAfterInsertPoint;
|
|
109
|
+
|
|
110
|
+
// Selects item from the original array or the leaf for insertion.
|
|
111
|
+
var computedQuinSelected = QuinSelector(NUM_OUTPUT_ITEMS)(inputs, computedAdjustedIndex);
|
|
112
|
+
var computedIsIndexEqual = IsEqual()([index, i]);
|
|
113
|
+
var mux = Mux1()([computedQuinSelected, leaf], computedIsIndexEqual);
|
|
114
|
+
|
|
115
|
+
out[i] <== mux;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Computes the root of an IQT given a leaf, its path, and sibling nodes at each level of the tree.
|
|
121
|
+
* It iteratively incorporates the leaf or the hash from the previous level with sibling nodes using
|
|
122
|
+
* the Splicer to place the leaf or hash at the correct position based on path_index.
|
|
123
|
+
* Then, it hashes these values together with PoseidonHasher to move up the tree.
|
|
124
|
+
* This process repeats for each level (levels) of the tree, culminating in the computation of the tree's root.
|
|
125
|
+
*/
|
|
126
|
+
template QuinTreeInclusionProof(levels) {
|
|
127
|
+
var LEAVES_PER_NODE = 5;
|
|
128
|
+
var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;
|
|
129
|
+
|
|
130
|
+
signal input leaf;
|
|
131
|
+
signal input path_index[levels];
|
|
132
|
+
signal input path_elements[levels][LEAVES_PER_PATH_LEVEL];
|
|
133
|
+
signal output root;
|
|
134
|
+
|
|
135
|
+
var currentLeaf = leaf;
|
|
136
|
+
|
|
137
|
+
// Iteratively hash each level of path_elements with the leaf or previous hash
|
|
138
|
+
for (var i = 0; i < levels; i++) {
|
|
139
|
+
var elements[LEAVES_PER_PATH_LEVEL];
|
|
140
|
+
|
|
141
|
+
for (var j = 0; j < LEAVES_PER_PATH_LEVEL; j++) {
|
|
142
|
+
elements[j] = path_elements[i][j];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
var computedSplicedLeaf[LEAVES_PER_NODE] = Splicer(LEAVES_PER_PATH_LEVEL)(
|
|
146
|
+
elements,
|
|
147
|
+
currentLeaf,
|
|
148
|
+
path_index[i]
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
currentLeaf = PoseidonHasher(5)([
|
|
152
|
+
computedSplicedLeaf[0],
|
|
153
|
+
computedSplicedLeaf[1],
|
|
154
|
+
computedSplicedLeaf[2],
|
|
155
|
+
computedSplicedLeaf[3],
|
|
156
|
+
computedSplicedLeaf[4]
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
root <== currentLeaf;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Verifies if a given leaf exists within an IQT.
|
|
165
|
+
* Takes a leaf, its path to the root (specified by indices and path elements),
|
|
166
|
+
* and the root itself, to verify the leaf's inclusion within the tree.
|
|
167
|
+
*/
|
|
168
|
+
template QuinLeafExists(levels){
|
|
169
|
+
var LEAVES_PER_NODE = 5;
|
|
170
|
+
var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;
|
|
171
|
+
|
|
172
|
+
signal input leaf;
|
|
173
|
+
signal input path_index[levels];
|
|
174
|
+
signal input path_elements[levels][LEAVES_PER_PATH_LEVEL];
|
|
175
|
+
signal input root;
|
|
176
|
+
|
|
177
|
+
// Verify the Merkle path.
|
|
178
|
+
var computedRoot = QuinTreeInclusionProof(levels)(leaf, path_index, path_elements);
|
|
179
|
+
|
|
180
|
+
root === computedRoot;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Checks if a list of leaves exists within an IQT, leveraging the PoseidonT6
|
|
185
|
+
* circuit for hashing. This can be used to verify the presence of multiple leaves.
|
|
186
|
+
*/
|
|
187
|
+
template QuinBatchLeavesExists(levels, batchLevels) {
|
|
188
|
+
var LEAVES_PER_NODE = 5;
|
|
189
|
+
var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;
|
|
190
|
+
var LEAVES_PER_BATCH = LEAVES_PER_NODE ** batchLevels;
|
|
191
|
+
|
|
192
|
+
signal input root;
|
|
193
|
+
signal input leaves[LEAVES_PER_BATCH];
|
|
194
|
+
signal input path_index[levels - batchLevels];
|
|
195
|
+
signal input path_elements[levels - batchLevels][LEAVES_PER_PATH_LEVEL];
|
|
196
|
+
|
|
197
|
+
// Compute the subroot (= leaf).
|
|
198
|
+
var computedQuinSubroot = QuinCheckRoot(batchLevels)(leaves);
|
|
199
|
+
|
|
200
|
+
// Check if the Merkle path is valid
|
|
201
|
+
QuinLeafExists(levels - batchLevels)(computedQuinSubroot, path_index, path_elements, root);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Calculates the path indices required for Merkle proof verifications (e.g., QuinTreeInclusionProof, QuinLeafExists).
|
|
206
|
+
* Given a node index within an IQT and the total tree levels, it outputs the path indices leading to that node.
|
|
207
|
+
* The template handles the modulo and division operations to break down the tree index into its constituent path indices.
|
|
208
|
+
* e.g., if the index is 30 and the number of levels is 4, the output should be [0, 1, 1, 0].
|
|
209
|
+
*/
|
|
210
|
+
template QuinGeneratePathIndices(levels) {
|
|
211
|
+
var BASE = 5;
|
|
212
|
+
|
|
213
|
+
signal input in;
|
|
214
|
+
signal output out[levels];
|
|
215
|
+
|
|
216
|
+
var m = in;
|
|
217
|
+
var computedResults[levels];
|
|
218
|
+
|
|
219
|
+
for (var i = 0; i < levels; i++) {
|
|
220
|
+
// circom's best practices suggests to avoid using <-- unless you
|
|
221
|
+
// are aware of what's going on. This is the only way to do modulo operation.
|
|
222
|
+
out[i] <-- m % BASE;
|
|
223
|
+
m = m \ BASE;
|
|
224
|
+
|
|
225
|
+
// Check that each output element is less than the base.
|
|
226
|
+
var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
|
|
227
|
+
computedIsOutputElementLessThanBase === 1;
|
|
228
|
+
|
|
229
|
+
// Re-compute the total sum.
|
|
230
|
+
computedResults[i] = out[i] * (BASE ** i);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Check that the total sum matches the index.
|
|
234
|
+
var computedCalculateTotal = CalculateTotal(levels)(computedResults);
|
|
235
|
+
|
|
236
|
+
computedCalculateTotal === in;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Computes the root of a quintary Merkle tree given a list of leaves.
|
|
241
|
+
* This template constructs a Merkle tree with each node having 5 children (quintary)
|
|
242
|
+
* and computes the root by hashing with Poseidon the leaves and intermediate nodes in the given order.
|
|
243
|
+
* The computation is performed by first hashing groups of 5 leaves to form the bottom layer of nodes,
|
|
244
|
+
* then recursively hashing groups of these nodes to form the next layer, and so on, until the root is computed.
|
|
245
|
+
*/
|
|
246
|
+
template QuinCheckRoot(levels) {
|
|
247
|
+
var LEAVES_PER_NODE = 5;
|
|
248
|
+
var totalLeaves = LEAVES_PER_NODE ** levels;
|
|
249
|
+
var numLeafHashers = LEAVES_PER_NODE ** (levels - 1);
|
|
250
|
+
|
|
251
|
+
signal input leaves[totalLeaves];
|
|
252
|
+
signal output root;
|
|
253
|
+
|
|
254
|
+
// Determine the total number of hashers.
|
|
255
|
+
var numHashers = 0;
|
|
256
|
+
for (var i = 0; i < levels; i++) {
|
|
257
|
+
numHashers += LEAVES_PER_NODE ** i;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
var computedHashers[numHashers];
|
|
261
|
+
|
|
262
|
+
// Initialize hashers for the leaves.
|
|
263
|
+
for (var i = 0; i < numLeafHashers; i++) {
|
|
264
|
+
computedHashers[i] = PoseidonHasher(5)([
|
|
265
|
+
leaves[i * LEAVES_PER_NODE + 0],
|
|
266
|
+
leaves[i * LEAVES_PER_NODE + 1],
|
|
267
|
+
leaves[i * LEAVES_PER_NODE + 2],
|
|
268
|
+
leaves[i * LEAVES_PER_NODE + 3],
|
|
269
|
+
leaves[i * LEAVES_PER_NODE + 4]
|
|
270
|
+
]);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Initialize hashers for intermediate nodes and compute the root.
|
|
274
|
+
var k = 0;
|
|
275
|
+
for (var i = numLeafHashers; i < numHashers; i++) {
|
|
276
|
+
computedHashers[i] = PoseidonHasher(5)([
|
|
277
|
+
computedHashers[k * LEAVES_PER_NODE + 0],
|
|
278
|
+
computedHashers[k * LEAVES_PER_NODE + 1],
|
|
279
|
+
computedHashers[k * LEAVES_PER_NODE + 2],
|
|
280
|
+
computedHashers[k * LEAVES_PER_NODE + 3],
|
|
281
|
+
computedHashers[k * LEAVES_PER_NODE + 4]
|
|
282
|
+
]);
|
|
283
|
+
k++;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
root <== computedHashers[numHashers - 1];
|
|
287
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib imports
|
|
4
|
+
include "./compconstant.circom";
|
|
5
|
+
include "./comparators.circom";
|
|
6
|
+
include "./pointbits.circom";
|
|
7
|
+
include "./bitify.circom";
|
|
8
|
+
include "./escalarmulany.circom";
|
|
9
|
+
include "./escalarmulfix.circom";
|
|
10
|
+
// local imports
|
|
11
|
+
include "./hashers.circom";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Variant of the EdDSAPoseidonVerifier template from circomlib
|
|
15
|
+
* https://github.com/iden3/circomlib/blob/master/circuits/eddsa.circom
|
|
16
|
+
*/
|
|
17
|
+
template EdDSAPoseidonVerifier_patched() {
|
|
18
|
+
// The x and y coordinates of the public key.
|
|
19
|
+
signal input Ax;
|
|
20
|
+
signal input Ay;
|
|
21
|
+
// Signature scalar.
|
|
22
|
+
signal input S;
|
|
23
|
+
// The x and y coordinates of the signature point.
|
|
24
|
+
signal input R8x;
|
|
25
|
+
signal input R8y;
|
|
26
|
+
// Message hash.
|
|
27
|
+
signal input M;
|
|
28
|
+
|
|
29
|
+
signal output valid;
|
|
30
|
+
|
|
31
|
+
// Ensure S<Subgroup Order.
|
|
32
|
+
// convert the signature scalar S into its binary representation.
|
|
33
|
+
var computedNum2Bits[254] = Num2Bits(254)(S);
|
|
34
|
+
|
|
35
|
+
var computedCompConstantIn[254] = computedNum2Bits;
|
|
36
|
+
computedCompConstantIn[253] = 0;
|
|
37
|
+
|
|
38
|
+
// A component that ensures S is within a valid range,
|
|
39
|
+
// comparing it against a constant representing the subgroup order.
|
|
40
|
+
var computedCompConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040)(computedCompConstantIn);
|
|
41
|
+
|
|
42
|
+
// Calculate the h = H(R,A, msg).
|
|
43
|
+
var computedH2Bits[254] = Num2Bits_strict()(PoseidonHasher(5)([R8x, R8y, Ax, Ay, M]));
|
|
44
|
+
|
|
45
|
+
// These components perform point doubling operations on the public key
|
|
46
|
+
// to align it within the correct subgroup as part of the verification process.
|
|
47
|
+
var (computedDbl1XOut, computedDbl1YOut) = BabyDbl()(Ax, Ay);
|
|
48
|
+
var (computedDbl2XOut, computedDbl2YOut) = BabyDbl()(computedDbl1XOut, computedDbl1YOut);
|
|
49
|
+
var (computedDbl3XOut, computedDbl3YOut) = BabyDbl()(computedDbl2XOut, computedDbl2YOut);
|
|
50
|
+
|
|
51
|
+
// A component that performs scalar multiplication of the
|
|
52
|
+
// adjusted public key by the hash output, essential for the verification calculation.
|
|
53
|
+
var computedEscalarMulAny[2] = EscalarMulAny(254)(computedH2Bits, [computedDbl3XOut, computedDbl3YOut]);
|
|
54
|
+
|
|
55
|
+
// Compute the right side: right = R8 + right2.
|
|
56
|
+
var (computedAddRightXOut, computedAddRightYOut) = BabyAdd()(R8x, R8y, computedEscalarMulAny[0], computedEscalarMulAny[1]);
|
|
57
|
+
|
|
58
|
+
// Calculate the left side: left = S * B8.
|
|
59
|
+
var BASE8[2] = [
|
|
60
|
+
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
61
|
+
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
// Fixed-base scalar multiplication of a base point by S.
|
|
65
|
+
var computedEscalarMulFix[2] = EscalarMulFix(254, BASE8)(computedNum2Bits);
|
|
66
|
+
|
|
67
|
+
// Components to check the equality of x and y coordinates
|
|
68
|
+
// between the computed and expected points of the signature.
|
|
69
|
+
var computedIsRightValid = IsEqual()([computedEscalarMulFix[0], computedAddRightXOut]);
|
|
70
|
+
var computedIsLeftValid = IsEqual()([computedEscalarMulFix[1], computedAddRightYOut]);
|
|
71
|
+
var computedIsLeftRightValid = IsEqual()([computedIsRightValid + computedIsLeftValid, 2]);
|
|
72
|
+
|
|
73
|
+
// Components to handle edge cases and ensure that all conditions
|
|
74
|
+
// for a valid signature are met, including the
|
|
75
|
+
// public key not being zero and other integrity checks.
|
|
76
|
+
var computedIsAxZero = IsZero()(Ax);
|
|
77
|
+
var computedIsAxEqual = IsEqual()([computedIsAxZero, 0]);
|
|
78
|
+
var computedIsCcZero = IsZero()(computedCompConstant);
|
|
79
|
+
var computedIsValid = IsEqual()([computedIsLeftRightValid + computedIsAxEqual + computedIsCcZero, 3]);
|
|
80
|
+
|
|
81
|
+
valid <== computedIsValid;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Verifies the EdDSA signature for a given command, which has exactly four elements in the hash preimage.
|
|
86
|
+
*/
|
|
87
|
+
template VerifySignature() {
|
|
88
|
+
// Public key of the signer, consisting of two coordinates [x, y].
|
|
89
|
+
signal input pubKey[2];
|
|
90
|
+
// R8 point from the signature, consisting of two coordinates [x, y].
|
|
91
|
+
signal input R8[2];
|
|
92
|
+
// Scalar component of the signature.
|
|
93
|
+
signal input S;
|
|
94
|
+
|
|
95
|
+
// Number of elements in the hash preimage.
|
|
96
|
+
var k = 4;
|
|
97
|
+
|
|
98
|
+
// The preimage data that was hashed, an array of four elements.
|
|
99
|
+
signal input preimage[k];
|
|
100
|
+
|
|
101
|
+
signal output valid;
|
|
102
|
+
|
|
103
|
+
// Hash the preimage using the Poseidon hashing function configured for four inputs.
|
|
104
|
+
var computedM = PoseidonHasher(4)(preimage);
|
|
105
|
+
|
|
106
|
+
// Instantiate the patched EdDSA Poseidon verifier with the necessary inputs.
|
|
107
|
+
var computedVerifier = EdDSAPoseidonVerifier_patched()(
|
|
108
|
+
pubKey[0],
|
|
109
|
+
pubKey[1],
|
|
110
|
+
S,
|
|
111
|
+
R8[0],
|
|
112
|
+
R8[1],
|
|
113
|
+
computedM
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
valid <== computedVerifier;
|
|
117
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "../utils/hashers.circom";
|
|
5
|
+
include "../utils/privToPubKey.circom";
|
|
6
|
+
include "../utils/trees/BinaryMerkleRoot.circom";
|
|
7
|
+
|
|
8
|
+
// Poll Joining Circuit
|
|
9
|
+
// Allows a user to prove knowledge of a private key that is signed up to
|
|
10
|
+
// a MACI contract.
|
|
11
|
+
template PollJoining(stateTreeDepth) {
|
|
12
|
+
// Constants defining the tree structure
|
|
13
|
+
var STATE_TREE_ARITY = 2;
|
|
14
|
+
|
|
15
|
+
// User's private key
|
|
16
|
+
signal input privKey;
|
|
17
|
+
// Poll's public key
|
|
18
|
+
signal input pollPubKey[2];
|
|
19
|
+
// Siblings
|
|
20
|
+
signal input siblings[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
21
|
+
// Indices
|
|
22
|
+
signal input indices[stateTreeDepth];
|
|
23
|
+
// User's hashed private key
|
|
24
|
+
signal input nullifier;
|
|
25
|
+
// MACI State tree root which proves the user is signed up
|
|
26
|
+
signal input stateRoot;
|
|
27
|
+
// The actual tree depth (might be <= stateTreeDepth) used in BinaryMerkleRoot
|
|
28
|
+
signal input actualStateTreeDepth;
|
|
29
|
+
// The poll id
|
|
30
|
+
signal input pollId;
|
|
31
|
+
|
|
32
|
+
// Compute the nullifier (hash of private key and poll id)
|
|
33
|
+
var computedNullifier = PoseidonHasher(2)([privKey, pollId]);
|
|
34
|
+
nullifier === computedNullifier;
|
|
35
|
+
|
|
36
|
+
// User private to public key
|
|
37
|
+
var derivedPubKey[2] = PrivToPubKey()(privKey);
|
|
38
|
+
// Hash the public key
|
|
39
|
+
var pubKeyHash = PoseidonHasher(2)([derivedPubKey[0], derivedPubKey[1]]);
|
|
40
|
+
|
|
41
|
+
// Ensure the poll public key is the same as the maci one (public input)
|
|
42
|
+
derivedPubKey[0] === pollPubKey[0];
|
|
43
|
+
derivedPubKey[1] === pollPubKey[1];
|
|
44
|
+
|
|
45
|
+
// Inclusion proof
|
|
46
|
+
var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
47
|
+
pubKeyHash,
|
|
48
|
+
actualStateTreeDepth,
|
|
49
|
+
indices,
|
|
50
|
+
siblings
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
calculatedRoot === stateRoot;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Poll Joined Circuit
|
|
57
|
+
// Allows a user to prove that they have joined a MACI poll.
|
|
58
|
+
// This is to be used with the MACI offchain implementation to allow
|
|
59
|
+
// users to authenticate to the relayer service (to reduce spamming).
|
|
60
|
+
template PollJoined(stateTreeDepth) {
|
|
61
|
+
// Constants defining the tree structure
|
|
62
|
+
var STATE_TREE_ARITY = 2;
|
|
63
|
+
|
|
64
|
+
// User's private key
|
|
65
|
+
signal input privKey;
|
|
66
|
+
// User's voice credits balance
|
|
67
|
+
signal input voiceCreditsBalance;
|
|
68
|
+
// Path elements
|
|
69
|
+
signal input pathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
70
|
+
// Path indices
|
|
71
|
+
signal input pathIndices[stateTreeDepth];
|
|
72
|
+
// Poll State tree root which proves the user is joined
|
|
73
|
+
signal input stateRoot;
|
|
74
|
+
// The actual tree depth (might be <= stateTreeDepth) Used in BinaryMerkleRoot
|
|
75
|
+
signal input actualStateTreeDepth;
|
|
76
|
+
|
|
77
|
+
// User private to public key
|
|
78
|
+
var derivedPubKey[2] = PrivToPubKey()(privKey);
|
|
79
|
+
|
|
80
|
+
var stateLeaf = PoseidonHasher(3)([derivedPubKey[0], derivedPubKey[1], voiceCreditsBalance]);
|
|
81
|
+
|
|
82
|
+
// Inclusion proof
|
|
83
|
+
var stateLeafQip = BinaryMerkleRoot(stateTreeDepth)(
|
|
84
|
+
stateLeaf,
|
|
85
|
+
actualStateTreeDepth,
|
|
86
|
+
pathIndices,
|
|
87
|
+
pathElements
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
stateLeafQip === stateRoot;
|
|
91
|
+
}
|
|
92
|
+
|
package/circomkit.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"protocol": "groth16",
|
|
3
|
+
"prime": "bn128",
|
|
4
|
+
"version": "2.1.6",
|
|
5
|
+
"circuits": "./circom/circuits.json",
|
|
6
|
+
"dirPtau": "./ptau",
|
|
7
|
+
"dirCircuits": "./circom",
|
|
8
|
+
"dirInputs": "./inputs",
|
|
9
|
+
"dirBuild": "./build",
|
|
10
|
+
"optimization": 2,
|
|
11
|
+
"inspect": false,
|
|
12
|
+
"include": ["./node_modules/circomlib/circuits", "./node_modules/@zk-kit/circuits/circom"],
|
|
13
|
+
"groth16numContributions": 0,
|
|
14
|
+
"groth16askForEntropy": false,
|
|
15
|
+
"logLevel": "INFO",
|
|
16
|
+
"verbose": true,
|
|
17
|
+
"cWitness": true
|
|
18
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maci-protocol/circuits",
|
|
3
|
+
"version": "0.0.0-ci.044d30d",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "zk-SNARK circuits for MACI",
|
|
6
|
+
"main": "build/ts/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"build",
|
|
9
|
+
"circom",
|
|
10
|
+
"circomkit.json",
|
|
11
|
+
"LICENSE",
|
|
12
|
+
"README.md",
|
|
13
|
+
"CHANGELOG.md"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build-test-circuits-c": "ts-node ./ts/compile.ts --cWitness",
|
|
20
|
+
"build-test-circuits-wasm": "ts-node ./ts/compile.ts",
|
|
21
|
+
"gen-zkeys": "ts-node ./ts/genZkeys.ts",
|
|
22
|
+
"info": "NODE_OPTIONS=--max-old-space-size=4096 ts-node ./ts/info.ts",
|
|
23
|
+
"watch": "tsc --watch",
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"circom:build": "NODE_OPTIONS=--max-old-space-size=4096 circomkit compile",
|
|
26
|
+
"circom:setup": "NODE_OPTIONS=--max-old-space-size=4096 circomkit setup",
|
|
27
|
+
"types": "tsc -p tsconfig.json --noEmit",
|
|
28
|
+
"mocha-test": "NODE_OPTIONS=--max-old-space-size=4096 ts-mocha --exit -g '^(?!.*\\[fuzz\\]).*$'",
|
|
29
|
+
"test": "pnpm run mocha-test ts/__tests__/*.test.ts",
|
|
30
|
+
"test:fuzz": "NODE_OPTIONS=--max-old-space-size=4096 ts-mocha --exit -g '\\[fuzz\\]' ./ts/__tests__/*.test.ts",
|
|
31
|
+
"test:hasher": "pnpm run mocha-test ts/__tests__/Hasher.test.ts",
|
|
32
|
+
"test:slAndBallotTransformer": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformer.test.ts",
|
|
33
|
+
"test:messageToCommand": "pnpm run mocha-test ts/__tests__/MessageToCommand.test.ts",
|
|
34
|
+
"test:messageValidator": "pnpm run mocha-test ts/__tests__/MessageValidator.test.ts",
|
|
35
|
+
"test:verifySignature": "pnpm run mocha-test ts/__tests__/VerifySignature.test.ts",
|
|
36
|
+
"test:privToPubKey": "pnpm run mocha-test ts/__tests__/PrivToPubKey.test.ts",
|
|
37
|
+
"test:calculateTotal": "pnpm run mocha-test ts/__tests__/CalculateTotal.test.ts",
|
|
38
|
+
"test:processMessages": "pnpm run mocha-test ts/__tests__/ProcessMessages.test.ts",
|
|
39
|
+
"test:tallyVotes": "pnpm run mocha-test ts/__tests__/TallyVotes.test.ts",
|
|
40
|
+
"test:ceremonyParams": "pnpm run mocha-test ts/__tests__/CeremonyParams.test.ts",
|
|
41
|
+
"test:incrementalQuinaryTree": "pnpm run mocha-test ts/__tests__/IncrementalQuinaryTree.test.ts",
|
|
42
|
+
"test:pollJoining": "pnpm run mocha-test ts/__tests__/PollJoining.test.ts",
|
|
43
|
+
"test:pollJoined": "pnpm run mocha-test ts/__tests__/PollJoined.test.ts"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@maci-protocol/core": "0.0.0-ci.044d30d",
|
|
47
|
+
"@maci-protocol/crypto": "0.0.0-ci.044d30d",
|
|
48
|
+
"@maci-protocol/domainobjs": "0.0.0-ci.044d30d",
|
|
49
|
+
"@maci-protocol/sdk": "0.0.0-ci.044d30d",
|
|
50
|
+
"@zk-kit/circuits": "^0.4.0",
|
|
51
|
+
"circomkit": "^0.3.2",
|
|
52
|
+
"circomlib": "^2.0.5"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/chai": "^4.3.11",
|
|
56
|
+
"@types/chai-as-promised": "^7.1.8",
|
|
57
|
+
"@types/mocha": "^10.0.10",
|
|
58
|
+
"@types/node": "^22.14.0",
|
|
59
|
+
"@types/snarkjs": "^0.7.9",
|
|
60
|
+
"@zk-kit/baby-jubjub": "^1.0.3",
|
|
61
|
+
"chai": "^4.3.10",
|
|
62
|
+
"chai-as-promised": "^7.1.2",
|
|
63
|
+
"fast-check": "^4.1.0",
|
|
64
|
+
"glob": "^11.0.1",
|
|
65
|
+
"mocha": "^11.1.0",
|
|
66
|
+
"ts-mocha": "^11.1.0",
|
|
67
|
+
"ts-node": "^10.9.1",
|
|
68
|
+
"typescript": "^5.8.3"
|
|
69
|
+
},
|
|
70
|
+
"gitHead": "5a5bf7a30836932641c6da5e3236d0f2e89aea16"
|
|
71
|
+
}
|