@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,49 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local import
|
|
4
|
+
include "../PoseidonHasher.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Verifies the correct construction of a Merkle tree from a set of leaves.
|
|
8
|
+
* Given a Merkle root and a list of leaves, check if the root is the
|
|
9
|
+
* correct result of inserting all the leaves into the tree (in the given order).
|
|
10
|
+
*/
|
|
11
|
+
template CheckRoot(levels) {
|
|
12
|
+
// The total number of leaves in the Merkle tree, calculated as 2 to the power of `levels`.
|
|
13
|
+
var TOTAL_LEVELS = 2 ** levels;
|
|
14
|
+
// The number of first-level hashers needed, equal to half the total leaves, as each hasher combines two leaves.
|
|
15
|
+
var LEAF_HASHERS = TOTAL_LEVELS / 2;
|
|
16
|
+
// The number of intermediate hashers, one less than the number of leaf hashers,
|
|
17
|
+
// as each level of hashing reduces the number of hash elements by about half.
|
|
18
|
+
var INTERMEDIATE_HASHERS = LEAF_HASHERS - 1;
|
|
19
|
+
|
|
20
|
+
// Array of leaf values input to the circuit.
|
|
21
|
+
signal input leaves[TOTAL_LEVELS];
|
|
22
|
+
|
|
23
|
+
// Output signal for the Merkle root that results from hashing all the input leaves.
|
|
24
|
+
signal output root;
|
|
25
|
+
|
|
26
|
+
// Total number of hashers used in constructing the tree, one less than the total number of leaves,
|
|
27
|
+
// since each level of the tree combines two elements into one.
|
|
28
|
+
var hashersLength = TOTAL_LEVELS - 1;
|
|
29
|
+
var computedLevelHashers[hashersLength];
|
|
30
|
+
|
|
31
|
+
// Initialize hashers for the leaves, each taking two adjacent leaves as inputs.
|
|
32
|
+
for (var i = 0; i < LEAF_HASHERS; i++){
|
|
33
|
+
computedLevelHashers[i] = PoseidonHasher(2)([leaves[i * 2], leaves[i * 2 + 1]]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Initialize hashers for intermediate levels, each taking the outputs of two hashers from the previous level.
|
|
37
|
+
var index = 0;
|
|
38
|
+
|
|
39
|
+
for (var i = LEAF_HASHERS; i < LEAF_HASHERS + INTERMEDIATE_HASHERS; i++) {
|
|
40
|
+
computedLevelHashers[i] = PoseidonHasher(2)([
|
|
41
|
+
computedLevelHashers[index * 2],
|
|
42
|
+
computedLevelHashers[index * 2 + 1]
|
|
43
|
+
]);
|
|
44
|
+
index++;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Connect the output of the final hasher in the array to the root output signal.
|
|
48
|
+
root <== computedLevelHashers[hashersLength - 1];
|
|
49
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local import
|
|
4
|
+
include "./MerkleTreeInclusionProof.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Ensures that a leaf exists within a Merkle tree with a given root.
|
|
8
|
+
*/
|
|
9
|
+
template LeafExists(levels) {
|
|
10
|
+
// The leaf whose existence within the tree is being verified.
|
|
11
|
+
signal input leaf;
|
|
12
|
+
|
|
13
|
+
// The elements along the path needed for the inclusion proof.
|
|
14
|
+
signal input path_elements[levels][1];
|
|
15
|
+
// The indices indicating the path taken through the tree for the leaf.
|
|
16
|
+
signal input path_indices[levels];
|
|
17
|
+
// The root of the Merkle tree, against which the inclusion is verified.
|
|
18
|
+
signal input root;
|
|
19
|
+
|
|
20
|
+
var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
|
|
21
|
+
leaf,
|
|
22
|
+
path_indices,
|
|
23
|
+
path_elements
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
root === computedMerkleRoot;
|
|
27
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "../PoseidonHasher.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Recomputes a Merkle root from a given leaf and its path in a Merkle tree.
|
|
10
|
+
*/
|
|
11
|
+
template MerkleTreeInclusionProof(n_levels) {
|
|
12
|
+
// The leaf node from which the Merkle root is calculated.
|
|
13
|
+
signal input leaf;
|
|
14
|
+
// Indices indicating left or right child for each level of the tree.
|
|
15
|
+
signal input path_indices[n_levels];
|
|
16
|
+
// Sibling node values required to compute the hash at each level.
|
|
17
|
+
signal input path_elements[n_levels][1];
|
|
18
|
+
|
|
19
|
+
// The merkle root.
|
|
20
|
+
signal output root;
|
|
21
|
+
|
|
22
|
+
// Stores the hash at each level starting from the leaf to the root.
|
|
23
|
+
signal levelHashes[n_levels + 1];
|
|
24
|
+
// Initialize the first level with the given leaf.
|
|
25
|
+
levelHashes[0] <== leaf;
|
|
26
|
+
|
|
27
|
+
for (var i = 0; i < n_levels; i++) {
|
|
28
|
+
// Validate path_indices to be either 0 or 1, ensuring no other values.
|
|
29
|
+
path_indices[i] * (1 - path_indices[i]) === 0;
|
|
30
|
+
|
|
31
|
+
// Configure the multiplexer based on the path index for the current level.
|
|
32
|
+
var multiplexer[2][2] = [
|
|
33
|
+
[levelHashes[i], path_elements[i][0]],
|
|
34
|
+
[path_elements[i][0], levelHashes[i]]
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
var multiplexerResult[2] = MultiMux1(2)(
|
|
38
|
+
multiplexer,
|
|
39
|
+
path_indices[i]
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
var computedLevelHash = PoseidonHasher(2)([multiplexerResult[0], multiplexerResult[1]]);
|
|
43
|
+
|
|
44
|
+
// Store the resulting hash as the next level's hash.
|
|
45
|
+
levelHashes[i + 1] <== computedLevelHash;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Set the final level hash as the root.
|
|
49
|
+
root <== levelHashes[n_levels];
|
|
50
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "../PoseidonHasher.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Computes the root of a quintary Merkle tree given a list of leaves.
|
|
8
|
+
* This template constructs a Merkle tree with each node having 5 children (quintary)
|
|
9
|
+
* and computes the root by hashing with Poseidon the leaves and intermediate nodes in the given order.
|
|
10
|
+
* The computation is performed by first hashing groups of 5 leaves to form the bottom layer of nodes,
|
|
11
|
+
* then recursively hashing groups of these nodes to form the next layer, and so on, until the root is computed.
|
|
12
|
+
*/
|
|
13
|
+
template QuinaryCheckRoot(levels) {
|
|
14
|
+
var LEAVES_PER_NODE = 5;
|
|
15
|
+
var totalLeaves = LEAVES_PER_NODE ** levels;
|
|
16
|
+
var numLeafHashers = LEAVES_PER_NODE ** (levels - 1);
|
|
17
|
+
|
|
18
|
+
signal input leaves[totalLeaves];
|
|
19
|
+
signal output root;
|
|
20
|
+
|
|
21
|
+
// Determine the total number of hashers.
|
|
22
|
+
var numHashers = 0;
|
|
23
|
+
for (var i = 0; i < levels; i++) {
|
|
24
|
+
numHashers += LEAVES_PER_NODE ** i;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var computedHashers[numHashers];
|
|
28
|
+
|
|
29
|
+
// Initialize hashers for the leaves.
|
|
30
|
+
for (var i = 0; i < numLeafHashers; i++) {
|
|
31
|
+
computedHashers[i] = PoseidonHasher(5)([
|
|
32
|
+
leaves[i * LEAVES_PER_NODE + 0],
|
|
33
|
+
leaves[i * LEAVES_PER_NODE + 1],
|
|
34
|
+
leaves[i * LEAVES_PER_NODE + 2],
|
|
35
|
+
leaves[i * LEAVES_PER_NODE + 3],
|
|
36
|
+
leaves[i * LEAVES_PER_NODE + 4]
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Initialize hashers for intermediate nodes and compute the root.
|
|
41
|
+
var k = 0;
|
|
42
|
+
for (var i = numLeafHashers; i < numHashers; i++) {
|
|
43
|
+
computedHashers[i] = PoseidonHasher(5)([
|
|
44
|
+
computedHashers[k * LEAVES_PER_NODE + 0],
|
|
45
|
+
computedHashers[k * LEAVES_PER_NODE + 1],
|
|
46
|
+
computedHashers[k * LEAVES_PER_NODE + 2],
|
|
47
|
+
computedHashers[k * LEAVES_PER_NODE + 3],
|
|
48
|
+
computedHashers[k * LEAVES_PER_NODE + 4]
|
|
49
|
+
]);
|
|
50
|
+
k++;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
root <== computedHashers[numHashers - 1];
|
|
54
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit import
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../CalculateTotal.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Calculates the path indices required for Merkle proof verifications (e.g., QuinaryTreeInclusionProof, QuinaryLeafExists).
|
|
10
|
+
* Given a node index within an IQT 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
|
+
* e.g., if the index is 30 and the number of levels is 4, the output should be [0, 1, 1, 0].
|
|
13
|
+
*/
|
|
14
|
+
template QuinaryGeneratePathIndices(levels) {
|
|
15
|
+
// The number of leaves per node (tree arity)
|
|
16
|
+
var LEAVES_PER_NODE = 5;
|
|
17
|
+
|
|
18
|
+
// The index within the tree
|
|
19
|
+
signal input index;
|
|
20
|
+
// The generated path indices leading to the node of the provided index
|
|
21
|
+
signal output out[levels];
|
|
22
|
+
|
|
23
|
+
var indexModulus = index;
|
|
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] <-- indexModulus % LEAVES_PER_NODE;
|
|
30
|
+
indexModulus = indexModulus \ LEAVES_PER_NODE;
|
|
31
|
+
|
|
32
|
+
// Check that each output element is less than the base.
|
|
33
|
+
var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], LEAVES_PER_NODE]);
|
|
34
|
+
computedIsOutputElementLessThanBase === 1;
|
|
35
|
+
|
|
36
|
+
// Re-compute the total sum.
|
|
37
|
+
computedResults[i] = out[i] * (LEAVES_PER_NODE ** i);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check that the total sum matches the index.
|
|
41
|
+
var computedCalculateTotal = CalculateTotal(levels)(computedResults);
|
|
42
|
+
|
|
43
|
+
computedCalculateTotal === index;
|
|
44
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "./QuinaryTreeInclusionProof.circom";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Verifies if a given leaf exists within an IQT.
|
|
8
|
+
* Takes a leaf, its path to the root (specified by indices and path elements),
|
|
9
|
+
* and the root itself, to verify the leaf's inclusion within the tree.
|
|
10
|
+
*/
|
|
11
|
+
template QuinaryLeafExists(levels){
|
|
12
|
+
// The number of leaves per node (tree arity)
|
|
13
|
+
var LEAVES_PER_NODE = 5;
|
|
14
|
+
// Number of leaves per path level (excluding the leaf itself)
|
|
15
|
+
var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;
|
|
16
|
+
|
|
17
|
+
// The leaf to check for inclusion
|
|
18
|
+
signal input leaf;
|
|
19
|
+
// The path indices at each level of the tree
|
|
20
|
+
signal input path_indices[levels];
|
|
21
|
+
// The sibling nodes at each level of the tree
|
|
22
|
+
signal input path_elements[levels][LEAVES_PER_PATH_LEVEL];
|
|
23
|
+
// The computed root of the tree
|
|
24
|
+
signal input root;
|
|
25
|
+
|
|
26
|
+
// Verify the Merkle path.
|
|
27
|
+
var computedRoot = QuinaryTreeInclusionProof(levels)(leaf, path_indices, path_elements);
|
|
28
|
+
|
|
29
|
+
root === computedRoot;
|
|
30
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit import
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../CalculateTotal.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Selects an item from a list based on the given index.
|
|
10
|
+
* It verifies the index is within the valid range and then iterates over the inputs to find the match.
|
|
11
|
+
* For each item, it checks if its position equals the given index and if so, multiplies the item
|
|
12
|
+
* by the result of the equality check, effectively selecting it.
|
|
13
|
+
* The sum of these results yields the selected item, ensuring only the item at the specified index be the output.
|
|
14
|
+
*
|
|
15
|
+
* nb. The number of items must be less than 8, and the index must be less than the number of items.
|
|
16
|
+
*/
|
|
17
|
+
template QuinarySelector(choices) {
|
|
18
|
+
// The input elements to select from.
|
|
19
|
+
signal input in[choices];
|
|
20
|
+
// The index of the element to select
|
|
21
|
+
signal input index;
|
|
22
|
+
// The selected total sum of the elements.
|
|
23
|
+
signal output out;
|
|
24
|
+
|
|
25
|
+
// Ensure that index < choices.
|
|
26
|
+
var computedIndex = SafeLessThan(3)([index, choices]);
|
|
27
|
+
computedIndex === 1;
|
|
28
|
+
|
|
29
|
+
// Initialize an array to hold the results of equality checks.
|
|
30
|
+
var computedResults[choices];
|
|
31
|
+
|
|
32
|
+
// For each item, check whether its index equals the input index.
|
|
33
|
+
// The result is multiplied by the corresponding input value.
|
|
34
|
+
for (var i = 0; i < choices; i++) {
|
|
35
|
+
var computedIsIndexEqual = IsEqual()([i, index]);
|
|
36
|
+
|
|
37
|
+
computedResults[i] = computedIsIndexEqual * in[i];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Calculate the total sum of the results array.
|
|
41
|
+
out <== CalculateTotal(choices)(computedResults);
|
|
42
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "../PoseidonHasher.circom";
|
|
5
|
+
include "./Splicer.circom";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Computes the root of an IQT given a leaf, its path, and sibling nodes at each level of the tree.
|
|
9
|
+
* It iteratively incorporates the leaf or the hash from the previous level with sibling nodes using
|
|
10
|
+
* the Splicer to place the leaf or hash at the correct position based on path_indices.
|
|
11
|
+
* Then, it hashes these values together with PoseidonHasher to move up the tree.
|
|
12
|
+
* This process repeats for each level (levels) of the tree, culminating in the computation of the tree's root.
|
|
13
|
+
*/
|
|
14
|
+
template QuinaryTreeInclusionProof(levels) {
|
|
15
|
+
// The number of leaves per node (tree arity)
|
|
16
|
+
var LEAVES_PER_NODE = 5;
|
|
17
|
+
// Number of leaves per path level (excluding the leaf itself)
|
|
18
|
+
var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;
|
|
19
|
+
|
|
20
|
+
// The leaf to check for inclusion
|
|
21
|
+
signal input leaf;
|
|
22
|
+
// The path indices at each level of the tree
|
|
23
|
+
signal input path_indices[levels];
|
|
24
|
+
// The sibling nodes at each level of the tree
|
|
25
|
+
signal input path_elements[levels][LEAVES_PER_PATH_LEVEL];
|
|
26
|
+
// The computed root of the tree
|
|
27
|
+
signal output root;
|
|
28
|
+
|
|
29
|
+
var currentLeaf = leaf;
|
|
30
|
+
|
|
31
|
+
// Iteratively hash each level of path_elements with the leaf or previous hash
|
|
32
|
+
for (var i = 0; i < levels; i++) {
|
|
33
|
+
var elements[LEAVES_PER_PATH_LEVEL];
|
|
34
|
+
|
|
35
|
+
for (var j = 0; j < LEAVES_PER_PATH_LEVEL; j++) {
|
|
36
|
+
elements[j] = path_elements[i][j];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
var computedSplicedLeaf[LEAVES_PER_NODE] = Splicer(LEAVES_PER_PATH_LEVEL)(
|
|
40
|
+
elements,
|
|
41
|
+
currentLeaf,
|
|
42
|
+
path_indices[i]
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
currentLeaf = PoseidonHasher(5)([
|
|
46
|
+
computedSplicedLeaf[0],
|
|
47
|
+
computedSplicedLeaf[1],
|
|
48
|
+
computedSplicedLeaf[2],
|
|
49
|
+
computedSplicedLeaf[3],
|
|
50
|
+
computedSplicedLeaf[4]
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
root <== currentLeaf;
|
|
55
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib imports
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// zk-kit import
|
|
6
|
+
include "./safe-comparators.circom";
|
|
7
|
+
// local imports
|
|
8
|
+
include "./QuinarySelector.circom";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The output array contains the input items, with the leaf inserted at the
|
|
12
|
+
* specified index. For example, if input = [0, 20, 30, 40], index = 3, and
|
|
13
|
+
* leaf = 10, the output will be [0, 20, 30, 10, 40].
|
|
14
|
+
*/
|
|
15
|
+
template Splicer(numItems) {
|
|
16
|
+
// The number of output items (because only one item is inserted).
|
|
17
|
+
var NUM_OUTPUT_ITEMS = numItems + 1;
|
|
18
|
+
|
|
19
|
+
// The input items to splice.
|
|
20
|
+
signal input in[numItems];
|
|
21
|
+
// The leaf to insert.
|
|
22
|
+
signal input leaf;
|
|
23
|
+
// The index at which to insert the leaf.
|
|
24
|
+
signal input index;
|
|
25
|
+
// The output array containing the spliced items.
|
|
26
|
+
signal output out[NUM_OUTPUT_ITEMS];
|
|
27
|
+
|
|
28
|
+
// There is a loop where the goal is to assign values to the output signal.
|
|
29
|
+
//
|
|
30
|
+
// | output[0] | output[1] | output[2] | ...
|
|
31
|
+
//
|
|
32
|
+
// We can either assign the leaf, or an item from the `items` signal, to the output, using Mux1().
|
|
33
|
+
// The Mux1's selector is 0 or 1 depending on whether the index is equal to the loop counter.
|
|
34
|
+
//
|
|
35
|
+
// i --> [IsEqual] <-- index
|
|
36
|
+
// |
|
|
37
|
+
// v
|
|
38
|
+
// leaf --> [Mux1] <-- <item from in>
|
|
39
|
+
// |
|
|
40
|
+
// v
|
|
41
|
+
// output[m]
|
|
42
|
+
//
|
|
43
|
+
// To obtain the value from <item from in>, we need to compute an item
|
|
44
|
+
// index (let it be `s`).
|
|
45
|
+
// 1. if index = 2 and i = 0, then s = 0
|
|
46
|
+
// 2. if index = 2 and i = 1, then s = 1
|
|
47
|
+
// 3. if index = 2 and i = 2, then s = 2
|
|
48
|
+
// 4. if index = 2 and i = 3, then s = 2
|
|
49
|
+
// 5. if index = 2 and i = 4, then s = 3
|
|
50
|
+
// We then wire `s`, as well as each item in `in` to a QuinarySelector.
|
|
51
|
+
// The output signal from the QuinarySelector is <item from in> and gets
|
|
52
|
+
// wired to Mux1 (as above).
|
|
53
|
+
|
|
54
|
+
var inputs[NUM_OUTPUT_ITEMS];
|
|
55
|
+
|
|
56
|
+
for (var i = 0; i < numItems; i++) {
|
|
57
|
+
inputs[i] = in[i];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
inputs[NUM_OUTPUT_ITEMS - 1] = 0;
|
|
61
|
+
|
|
62
|
+
for (var i = 0; i < NUM_OUTPUT_ITEMS; i++) {
|
|
63
|
+
// Determines if current index is greater than the insertion index.
|
|
64
|
+
var computedIsIndexAfterInsertPoint = SafeGreaterThan(3)([i, index]);
|
|
65
|
+
|
|
66
|
+
// Calculates correct index for original items, adjusting for leaf insertion.
|
|
67
|
+
var computedAdjustedIndex = i - computedIsIndexAfterInsertPoint;
|
|
68
|
+
|
|
69
|
+
// Selects item from the original array or the leaf for insertion.
|
|
70
|
+
var computedQuinarySelected = QuinarySelector(NUM_OUTPUT_ITEMS)(inputs, computedAdjustedIndex);
|
|
71
|
+
var computedIsIndexEqual = IsEqual()([index, i]);
|
|
72
|
+
var mux = Mux1()([computedQuinarySelected, leaf], computedIsIndexEqual);
|
|
73
|
+
|
|
74
|
+
out[i] <== mux;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "../utils/PoseidonHasher.circom";
|
|
5
|
+
include "../utils/PrivateToPublicKey.circom";
|
|
6
|
+
include "../utils/trees/BinaryMerkleRoot.circom";
|
|
7
|
+
|
|
8
|
+
// Poll Joined Circuit
|
|
9
|
+
// Allows a user to prove that they have joined a MACI poll.
|
|
10
|
+
// This is to be used with the MACI offchain implementation to allow
|
|
11
|
+
// users to authenticate to the relayer service (to reduce spamming).
|
|
12
|
+
template PollJoined(stateTreeDepth) {
|
|
13
|
+
// Constants defining the tree structure
|
|
14
|
+
var STATE_TREE_ARITY = 2;
|
|
15
|
+
|
|
16
|
+
// User's private key
|
|
17
|
+
signal input privateKey;
|
|
18
|
+
// User's voice credits balance
|
|
19
|
+
signal input voiceCreditsBalance;
|
|
20
|
+
// Path elements
|
|
21
|
+
signal input pathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
22
|
+
// Path indices
|
|
23
|
+
signal input pathIndices[stateTreeDepth];
|
|
24
|
+
// Poll State tree root which proves the user is joined
|
|
25
|
+
signal input stateRoot;
|
|
26
|
+
// The actual tree depth (might be <= stateTreeDepth) Used in BinaryMerkleRoot
|
|
27
|
+
signal input actualStateTreeDepth;
|
|
28
|
+
|
|
29
|
+
// User private to public key
|
|
30
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(privateKey);
|
|
31
|
+
|
|
32
|
+
var stateLeaf = PoseidonHasher(3)([derivedPublicKey[0], derivedPublicKey[1], voiceCreditsBalance]);
|
|
33
|
+
|
|
34
|
+
// Inclusion proof
|
|
35
|
+
var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
36
|
+
stateLeaf,
|
|
37
|
+
actualStateTreeDepth,
|
|
38
|
+
pathIndices,
|
|
39
|
+
pathElements
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
calculatedRoot === stateRoot;
|
|
43
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// local imports
|
|
4
|
+
include "../utils/PoseidonHasher.circom";
|
|
5
|
+
include "../utils/PrivateToPublicKey.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 privateKey;
|
|
17
|
+
// Poll's public key
|
|
18
|
+
signal input pollPublicKey[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)([privateKey, pollId]);
|
|
34
|
+
nullifier === computedNullifier;
|
|
35
|
+
|
|
36
|
+
// User private to public key
|
|
37
|
+
var derivedPublicKey[2] = PrivateToPublicKey()(privateKey);
|
|
38
|
+
// Hash the public key
|
|
39
|
+
var publicKeyHash = PoseidonHasher(2)([derivedPublicKey[0], derivedPublicKey[1]]);
|
|
40
|
+
|
|
41
|
+
// Ensure the poll public key is the same as the maci one (public input)
|
|
42
|
+
derivedPublicKey[0] === pollPublicKey[0];
|
|
43
|
+
derivedPublicKey[1] === pollPublicKey[1];
|
|
44
|
+
|
|
45
|
+
// Inclusion proof
|
|
46
|
+
var calculatedRoot = BinaryMerkleRoot(stateTreeDepth)(
|
|
47
|
+
publicKeyHash,
|
|
48
|
+
actualStateTreeDepth,
|
|
49
|
+
indices,
|
|
50
|
+
siblings
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
calculatedRoot === stateRoot;
|
|
54
|
+
}
|
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,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maci-protocol/circuits",
|
|
3
|
+
"version": "0.0.0-ci.01622be",
|
|
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
|
+
"generate-zkeys": "ts-node ./ts/generateZkeys.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:poseidonHasher": "pnpm run mocha-test ts/__tests__/PoseidonHasher.test.ts",
|
|
32
|
+
"test:messageHasher": "pnpm run mocha-test ts/__tests__/MessageHasher.test.ts",
|
|
33
|
+
"test:slAndBallotTransformer": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformer.test.ts",
|
|
34
|
+
"test:slAndBallotTransformerFull": "pnpm run mocha-test ts/__tests__/StateLeafAndBallotTransformerFull.test.ts",
|
|
35
|
+
"test:messageToCommand": "pnpm run mocha-test ts/__tests__/MessageToCommand.test.ts",
|
|
36
|
+
"test:messageValidator": "pnpm run mocha-test ts/__tests__/MessageValidator.test.ts",
|
|
37
|
+
"test:messageValidatorFull": "pnpm run mocha-test ts/__tests__/MessageValidatorFull.test.ts",
|
|
38
|
+
"test:verifySignature": "pnpm run mocha-test ts/__tests__/VerifySignature.test.ts",
|
|
39
|
+
"test:privateToPublicKey": "pnpm run mocha-test ts/__tests__/PrivateToPublicKey.test.ts",
|
|
40
|
+
"test:calculateTotal": "pnpm run mocha-test ts/__tests__/CalculateTotal.test.ts",
|
|
41
|
+
"test:processMessages": "pnpm run mocha-test ts/__tests__/ProcessMessages.test.ts",
|
|
42
|
+
"test:tallyVotes": "pnpm run mocha-test ts/__tests__/TallyVotes.test.ts",
|
|
43
|
+
"test:ceremonyParams": "pnpm run mocha-test ts/__tests__/CeremonyParams.test.ts",
|
|
44
|
+
"test:incrementalQuinaryTree": "pnpm run mocha-test ts/__tests__/IncrementalQuinaryTree.test.ts",
|
|
45
|
+
"test:pollJoining": "pnpm run mocha-test ts/__tests__/PollJoining.test.ts",
|
|
46
|
+
"test:pollJoined": "pnpm run mocha-test ts/__tests__/PollJoined.test.ts"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@maci-protocol/core": "0.0.0-ci.01622be",
|
|
50
|
+
"@maci-protocol/crypto": "0.0.0-ci.01622be",
|
|
51
|
+
"@maci-protocol/domainobjs": "0.0.0-ci.01622be",
|
|
52
|
+
"@maci-protocol/sdk": "0.0.0-ci.01622be",
|
|
53
|
+
"@zk-kit/circuits": "^0.4.0",
|
|
54
|
+
"circomkit": "^0.3.2",
|
|
55
|
+
"circomlib": "^2.0.5"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@types/chai": "^4.3.11",
|
|
59
|
+
"@types/chai-as-promised": "^7.1.8",
|
|
60
|
+
"@types/mocha": "^10.0.10",
|
|
61
|
+
"@types/node": "^22.15.8",
|
|
62
|
+
"@types/snarkjs": "^0.7.9",
|
|
63
|
+
"@zk-kit/baby-jubjub": "^1.0.3",
|
|
64
|
+
"chai": "^4.3.10",
|
|
65
|
+
"chai-as-promised": "^7.1.2",
|
|
66
|
+
"fast-check": "^4.1.1",
|
|
67
|
+
"glob": "^11.0.1",
|
|
68
|
+
"mocha": "^11.1.0",
|
|
69
|
+
"ts-mocha": "^11.1.0",
|
|
70
|
+
"ts-node": "^10.9.1",
|
|
71
|
+
"typescript": "^5.8.3"
|
|
72
|
+
},
|
|
73
|
+
"gitHead": "c031cdea188c218096e7299863354c0891c1f3a6"
|
|
74
|
+
}
|