@maci-protocol/circuits 0.0.0-ci.26f28d6
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 +105 -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 +447 -0
- package/circom/coordinator/non-qv/tallyVotes.circom +232 -0
- package/circom/coordinator/qv/processMessages.circom +449 -0
- package/circom/coordinator/qv/tallyVotes.circom +277 -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/incrementalMerkleTree.circom +198 -0
- package/circom/utils/trees/incrementalQuinaryTree.circom +287 -0
- package/circom/utils/verifySignature.circom +117 -0
- package/circom/voter/poll.circom +93 -0
- package/circomkit.json +18 -0
- package/package.json +71 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// local imports
|
|
6
|
+
include "../verifySignature.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a MACI message is valid or not.
|
|
10
|
+
* This template supports the Quadratic Voting (QV).
|
|
11
|
+
*/
|
|
12
|
+
template MessageValidator() {
|
|
13
|
+
// Length of the packed command.
|
|
14
|
+
var PACKED_CMD_LENGTH = 4;
|
|
15
|
+
|
|
16
|
+
// State index of the user.
|
|
17
|
+
signal input stateTreeIndex;
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input numSignUps;
|
|
20
|
+
// Vote option index.
|
|
21
|
+
signal input voteOptionIndex;
|
|
22
|
+
// Number of valid vote options for the poll.
|
|
23
|
+
signal input voteOptions;
|
|
24
|
+
// Ballot nonce.
|
|
25
|
+
signal input originalNonce;
|
|
26
|
+
// Command nonce.
|
|
27
|
+
signal input nonce;
|
|
28
|
+
// Packed command.
|
|
29
|
+
signal input cmd[PACKED_CMD_LENGTH];
|
|
30
|
+
// Public key of the state leaf (user).
|
|
31
|
+
signal input pubKey[2];
|
|
32
|
+
// ECDSA signature of the command (R part).
|
|
33
|
+
signal input sigR8[2];
|
|
34
|
+
// ECDSA signature of the command (S part).
|
|
35
|
+
signal input sigS;
|
|
36
|
+
// State leaf current voice credit balance.
|
|
37
|
+
signal input currentVoiceCreditBalance;
|
|
38
|
+
// Current number of votes for specific option.
|
|
39
|
+
signal input currentVotesForOption;
|
|
40
|
+
// Vote weight.
|
|
41
|
+
signal input voteWeight;
|
|
42
|
+
|
|
43
|
+
// True when the command is valid; otherwise false.
|
|
44
|
+
signal output isValid;
|
|
45
|
+
// True if the state leaf index is valid
|
|
46
|
+
signal output isStateLeafIndexValid;
|
|
47
|
+
// True if the vote option index is valid
|
|
48
|
+
signal output isVoteOptionIndexValid;
|
|
49
|
+
|
|
50
|
+
// Check (1) - The state leaf index must be valid.
|
|
51
|
+
// The check ensure that the stateTreeIndex < numSignUps as first validation.
|
|
52
|
+
// Must be < because the stateTreeIndex is 0-based. Zero is for blank state leaf
|
|
53
|
+
// while 1 is for the first actual user.
|
|
54
|
+
var computedIsStateLeafIndexValid = SafeLessThan(252)([stateTreeIndex, numSignUps]);
|
|
55
|
+
|
|
56
|
+
// Check (2) - The vote option index must be less than the number of valid vote options (0 indexed).
|
|
57
|
+
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
58
|
+
|
|
59
|
+
// Check (3) - The nonce must be correct.
|
|
60
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, nonce]);
|
|
61
|
+
|
|
62
|
+
// Check (4) - The signature must be correct.
|
|
63
|
+
var computedIsSignatureValid = VerifySignature()(pubKey, sigR8, sigS, cmd);
|
|
64
|
+
|
|
65
|
+
// Check (5) - There must be sufficient voice credits.
|
|
66
|
+
// The check ensure that the voteWeight is < sqrt(field size)
|
|
67
|
+
// so that voteWeight ^ 2 will not overflow.
|
|
68
|
+
var computedIsVoteWeightValid = SafeLessEqThan(252)([voteWeight, 147946756881789319005730692170996259609]);
|
|
69
|
+
|
|
70
|
+
// Check (6) - Check the current voice credit balance.
|
|
71
|
+
// The check ensure that currentVoiceCreditBalance + (currentVotesForOption ** 2) >= (voteWeight ** 2)
|
|
72
|
+
var computedAreVoiceCreditsSufficient = SafeGreaterEqThan(252)(
|
|
73
|
+
[
|
|
74
|
+
(currentVotesForOption * currentVotesForOption) + currentVoiceCreditBalance,
|
|
75
|
+
voteWeight * voteWeight
|
|
76
|
+
]
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// When all six checks are correct, then isValid = 1.
|
|
80
|
+
var computedIsUpdateValid = IsEqual()(
|
|
81
|
+
[
|
|
82
|
+
6,
|
|
83
|
+
computedIsSignatureValid +
|
|
84
|
+
computedAreVoiceCreditsSufficient +
|
|
85
|
+
computedIsVoteWeightValid +
|
|
86
|
+
computedIsNonceValid +
|
|
87
|
+
computedIsStateLeafIndexValid +
|
|
88
|
+
computedIsVoteOptionIndexValid
|
|
89
|
+
]
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
isValid <== computedIsUpdateValid;
|
|
93
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
94
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
95
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// circomlib import
|
|
4
|
+
include "./mux1.circom";
|
|
5
|
+
// local import
|
|
6
|
+
include "./messageValidator.circom";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Processes a command by verifying its validity and updates the state leaf and ballot accordingly.
|
|
10
|
+
* If the message is correct, updates the public key in the state leaf and the nonce
|
|
11
|
+
* in the ballot using multiplexer components.
|
|
12
|
+
* This template supports the Quadratic Voting (QV).
|
|
13
|
+
*/
|
|
14
|
+
template StateLeafAndBallotTransformer() {
|
|
15
|
+
// Length of the packed command.
|
|
16
|
+
var PACKED_CMD_LENGTH = 4;
|
|
17
|
+
|
|
18
|
+
// Number of user sign-ups in the state tree.
|
|
19
|
+
signal input numSignUps;
|
|
20
|
+
// Number of valid vote options for the poll.
|
|
21
|
+
signal input voteOptions;
|
|
22
|
+
|
|
23
|
+
// The following signals represents a state leaf (signed up user).
|
|
24
|
+
// Public key.
|
|
25
|
+
signal input slPubKey[2];
|
|
26
|
+
// Current voice credit balance.
|
|
27
|
+
signal input slVoiceCreditBalance;
|
|
28
|
+
|
|
29
|
+
// The following signals represents a ballot.
|
|
30
|
+
// Nonce.
|
|
31
|
+
signal input ballotNonce;
|
|
32
|
+
// Current number of votes for specific option.
|
|
33
|
+
signal input ballotCurrentVotesForOption;
|
|
34
|
+
|
|
35
|
+
// The following signals represents a command.
|
|
36
|
+
// State index of the user.
|
|
37
|
+
signal input cmdStateIndex;
|
|
38
|
+
// Public key of the user.
|
|
39
|
+
signal input cmdNewPubKey[2];
|
|
40
|
+
// Vote option index.
|
|
41
|
+
signal input cmdVoteOptionIndex;
|
|
42
|
+
// Vote weight.
|
|
43
|
+
signal input cmdNewVoteWeight;
|
|
44
|
+
// Nonce.
|
|
45
|
+
signal input cmdNonce;
|
|
46
|
+
// Poll identifier.
|
|
47
|
+
signal input cmdPollId;
|
|
48
|
+
// Salt.
|
|
49
|
+
signal input cmdSalt;
|
|
50
|
+
// ECDSA signature of the command (R part).
|
|
51
|
+
signal input cmdSigR8[2];
|
|
52
|
+
// ECDSA signature of the command (S part).
|
|
53
|
+
signal input cmdSigS;
|
|
54
|
+
// Packed command.
|
|
55
|
+
// nb. we are assuming that the packedCommand is always valid.
|
|
56
|
+
signal input packedCommand[PACKED_CMD_LENGTH];
|
|
57
|
+
|
|
58
|
+
// New state leaf (if the command is valid).
|
|
59
|
+
signal output newSlPubKey[2];
|
|
60
|
+
// New ballot (if the command is valid).
|
|
61
|
+
signal output newBallotNonce;
|
|
62
|
+
|
|
63
|
+
// True when the command is valid; otherwise false.
|
|
64
|
+
signal output isValid;
|
|
65
|
+
// True if the state leaf index is valid
|
|
66
|
+
signal output isStateLeafIndexValid;
|
|
67
|
+
// True if the vote option index is valid
|
|
68
|
+
signal output isVoteOptionIndexValid;
|
|
69
|
+
|
|
70
|
+
// Check if the command / message is valid.
|
|
71
|
+
var (computedMessageValidator, computedIsStateLeafIndexValid, computedIsVoteOptionIndexValid) = MessageValidator()(
|
|
72
|
+
cmdStateIndex,
|
|
73
|
+
numSignUps,
|
|
74
|
+
cmdVoteOptionIndex,
|
|
75
|
+
voteOptions,
|
|
76
|
+
ballotNonce,
|
|
77
|
+
cmdNonce,
|
|
78
|
+
packedCommand,
|
|
79
|
+
slPubKey,
|
|
80
|
+
cmdSigR8,
|
|
81
|
+
cmdSigS,
|
|
82
|
+
slVoiceCreditBalance,
|
|
83
|
+
ballotCurrentVotesForOption,
|
|
84
|
+
cmdNewVoteWeight
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// If the message is valid then we swap out the public key.
|
|
88
|
+
// This means using a Mux1() for pubKey[0] and another one
|
|
89
|
+
// for pubKey[1].
|
|
90
|
+
var computedNewSlPubKey0Mux = Mux1()([slPubKey[0], cmdNewPubKey[0]], computedMessageValidator);
|
|
91
|
+
var computedNewSlPubKey1Mux = Mux1()([slPubKey[1], cmdNewPubKey[1]], computedMessageValidator);
|
|
92
|
+
|
|
93
|
+
newSlPubKey[0] <== computedNewSlPubKey0Mux;
|
|
94
|
+
newSlPubKey[1] <== computedNewSlPubKey1Mux;
|
|
95
|
+
|
|
96
|
+
// If the message is valid, then we swap out the ballot nonce
|
|
97
|
+
// using a Mux1().
|
|
98
|
+
var computedNewBallotNonceMux = Mux1()([ballotNonce, cmdNonce], computedMessageValidator);
|
|
99
|
+
|
|
100
|
+
newBallotNonce <== computedNewBallotNonceMux;
|
|
101
|
+
|
|
102
|
+
isValid <== computedMessageValidator;
|
|
103
|
+
isStateLeafIndexValid <== computedIsStateLeafIndexValid;
|
|
104
|
+
isVoteOptionIndexValid <== computedIsVoteOptionIndexValid;
|
|
105
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
pragma circom 2.0.0;
|
|
2
|
+
|
|
3
|
+
// zk-kit imports
|
|
4
|
+
include "./safe-comparators.circom";
|
|
5
|
+
// circomlib import
|
|
6
|
+
include "./mux1.circom";
|
|
7
|
+
include "./comparators.circom";
|
|
8
|
+
// local import
|
|
9
|
+
include "../hashers.circom";
|
|
10
|
+
include "../calculateTotal.circom";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Recomputes a Merkle root from a given leaf and its path in a Merkle tree.
|
|
14
|
+
*/
|
|
15
|
+
template MerkleTreeInclusionProof(n_levels) {
|
|
16
|
+
// The leaf node from which the Merkle root is calculated.
|
|
17
|
+
signal input leaf;
|
|
18
|
+
// Indices indicating left or right child for each level of the tree.
|
|
19
|
+
signal input path_index[n_levels];
|
|
20
|
+
// Sibling node values required to compute the hash at each level.
|
|
21
|
+
signal input path_elements[n_levels][1];
|
|
22
|
+
|
|
23
|
+
signal output root;
|
|
24
|
+
|
|
25
|
+
// Stores the hash at each level starting from the leaf to the root.
|
|
26
|
+
signal levelHashes[n_levels + 1];
|
|
27
|
+
// Initialize the first level with the given leaf.
|
|
28
|
+
levelHashes[0] <== leaf;
|
|
29
|
+
|
|
30
|
+
for (var i = 0; i < n_levels; i++) {
|
|
31
|
+
// Validate path_index to be either 0 or 1, ensuring no other values.
|
|
32
|
+
path_index[i] * (1 - path_index[i]) === 0;
|
|
33
|
+
|
|
34
|
+
// Configure the multiplexer based on the path index for the current level.
|
|
35
|
+
var c[2][2] = [
|
|
36
|
+
[levelHashes[i], path_elements[i][0]],
|
|
37
|
+
[path_elements[i][0], levelHashes[i]]
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
var mux[2] = MultiMux1(2)(
|
|
41
|
+
c,
|
|
42
|
+
path_index[i]
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
var computedLevelHash = PoseidonHasher(2)([mux[0], mux[1]]);
|
|
46
|
+
|
|
47
|
+
// Store the resulting hash as the next level's hash.
|
|
48
|
+
levelHashes[i + 1] <== computedLevelHash;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Set the final level hash as the root.
|
|
52
|
+
root <== levelHashes[n_levels];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Ensures that a leaf exists within a Merkle tree with a given root.
|
|
57
|
+
*/
|
|
58
|
+
template LeafExists(levels){
|
|
59
|
+
// The leaf whose existence within the tree is being verified.
|
|
60
|
+
signal input leaf;
|
|
61
|
+
|
|
62
|
+
// The elements along the path needed for the inclusion proof.
|
|
63
|
+
signal input path_elements[levels][1];
|
|
64
|
+
// The indices indicating the path taken through the tree for the leaf.
|
|
65
|
+
signal input path_index[levels];
|
|
66
|
+
// The root of the Merkle tree, against which the inclusion is verified.
|
|
67
|
+
signal input root;
|
|
68
|
+
|
|
69
|
+
var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
|
|
70
|
+
leaf,
|
|
71
|
+
path_index,
|
|
72
|
+
path_elements
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
root === computedMerkleRoot;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Verifies the correct construction of a Merkle tree from a set of leaves.
|
|
80
|
+
* Given a Merkle root and a list of leaves, check if the root is the
|
|
81
|
+
* correct result of inserting all the leaves into the tree (in the given order).
|
|
82
|
+
*/
|
|
83
|
+
template CheckRoot(levels) {
|
|
84
|
+
// The total number of leaves in the Merkle tree, calculated as 2 to the power of `levels`.
|
|
85
|
+
var totalLeaves = 2 ** levels;
|
|
86
|
+
// The number of first-level hashers needed, equal to half the total leaves, as each hasher combines two leaves.
|
|
87
|
+
var numLeafHashers = totalLeaves / 2;
|
|
88
|
+
// The number of intermediate hashers, one less than the number of leaf hashers,
|
|
89
|
+
// as each level of hashing reduces the number of hash elements by about half.
|
|
90
|
+
var numIntermediateHashers = numLeafHashers - 1;
|
|
91
|
+
|
|
92
|
+
// Array of leaf values input to the circuit.
|
|
93
|
+
signal input leaves[totalLeaves];
|
|
94
|
+
|
|
95
|
+
// Output signal for the Merkle root that results from hashing all the input leaves.
|
|
96
|
+
signal output root;
|
|
97
|
+
|
|
98
|
+
// Total number of hashers used in constructing the tree, one less than the total number of leaves,
|
|
99
|
+
// since each level of the tree combines two elements into one.
|
|
100
|
+
var numHashers = totalLeaves - 1;
|
|
101
|
+
var computedLevelHashers[numHashers];
|
|
102
|
+
|
|
103
|
+
// Initialize hashers for the leaves, each taking two adjacent leaves as inputs.
|
|
104
|
+
for (var i = 0; i < numLeafHashers; i++){
|
|
105
|
+
computedLevelHashers[i] = PoseidonHasher(2)([leaves[i*2], leaves[i*2+1]]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Initialize hashers for intermediate levels, each taking the outputs of two hashers from the previous level.
|
|
109
|
+
var k = 0;
|
|
110
|
+
for (var i = numLeafHashers; i < numLeafHashers + numIntermediateHashers; i++) {
|
|
111
|
+
computedLevelHashers[i] = PoseidonHasher(2)([computedLevelHashers[k*2], computedLevelHashers[k*2+1]]);
|
|
112
|
+
k++;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Connect the output of the final hasher in the array to the root output signal.
|
|
116
|
+
root <== computedLevelHashers[numHashers-1];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Calculates the path indices required for Merkle proof verifications.
|
|
121
|
+
* Given a node index within an IMT and the total tree levels, it outputs the path indices leading to that node.
|
|
122
|
+
* The template handles the modulo and division operations to break down the tree index into its constituent path indices.
|
|
123
|
+
*/
|
|
124
|
+
template MerkleGeneratePathIndices(levels) {
|
|
125
|
+
var BASE = 2;
|
|
126
|
+
|
|
127
|
+
signal input in;
|
|
128
|
+
signal output out[levels];
|
|
129
|
+
|
|
130
|
+
var m = in;
|
|
131
|
+
var computedResults[levels];
|
|
132
|
+
|
|
133
|
+
for (var i = 0; i < levels; i++) {
|
|
134
|
+
// circom's best practices suggests to avoid using <-- unless you
|
|
135
|
+
// are aware of what's going on. This is the only way to do modulo operation.
|
|
136
|
+
out[i] <-- m % BASE;
|
|
137
|
+
m = m \ BASE;
|
|
138
|
+
|
|
139
|
+
// Check that each output element is less than the base.
|
|
140
|
+
var computedIsOutputElementLessThanBase = SafeLessThan(3)([out[i], BASE]);
|
|
141
|
+
computedIsOutputElementLessThanBase === 1;
|
|
142
|
+
|
|
143
|
+
// Re-compute the total sum.
|
|
144
|
+
computedResults[i] = out[i] * (BASE ** i);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Check that the total sum matches the index.
|
|
148
|
+
var computedCalculateTotal = CalculateTotal(levels)(computedResults);
|
|
149
|
+
|
|
150
|
+
computedCalculateTotal === in;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// @note taken from @zk-kit/circuits
|
|
154
|
+
// if used directly in processMessages circom complains about duplicated
|
|
155
|
+
// templates (Ark, Poseidon, etc.)
|
|
156
|
+
// This circuit is designed to calculate the root of a binary Merkle
|
|
157
|
+
// tree given a leaf, its depth, and the necessary sibling
|
|
158
|
+
// information (aka proof of membership).
|
|
159
|
+
// A circuit is designed without the capability to iterate through
|
|
160
|
+
// a dynamic array. To address this, a parameter with the static maximum
|
|
161
|
+
// tree depth is defined (i.e. 'MAX_DEPTH'). And additionally, the circuit
|
|
162
|
+
// receives a dynamic depth as an input, which is utilized in calculating the
|
|
163
|
+
// true root of the Merkle tree. The actual depth of the Merkle tree
|
|
164
|
+
// may be equal to or less than the static maximum depth.
|
|
165
|
+
// NOTE: This circuit will successfully verify `out = 0` for `depth > MAX_DEPTH`.
|
|
166
|
+
// Make sure to enforce `depth <= MAX_DEPTH` outside the circuit.
|
|
167
|
+
template BinaryMerkleRoot(MAX_DEPTH) {
|
|
168
|
+
signal input leaf, depth, indices[MAX_DEPTH], siblings[MAX_DEPTH][1];
|
|
169
|
+
|
|
170
|
+
signal output out;
|
|
171
|
+
|
|
172
|
+
signal nodes[MAX_DEPTH + 1];
|
|
173
|
+
nodes[0] <== leaf;
|
|
174
|
+
|
|
175
|
+
signal roots[MAX_DEPTH];
|
|
176
|
+
var root = 0;
|
|
177
|
+
|
|
178
|
+
for (var i = 0; i < MAX_DEPTH; i++) {
|
|
179
|
+
var isDepth = IsEqual()([depth, i]);
|
|
180
|
+
|
|
181
|
+
roots[i] <== isDepth * nodes[i];
|
|
182
|
+
|
|
183
|
+
root += roots[i];
|
|
184
|
+
|
|
185
|
+
var c[2][2] = [
|
|
186
|
+
[nodes[i], siblings[i][0]],
|
|
187
|
+
[siblings[i][0], nodes[i]]
|
|
188
|
+
];
|
|
189
|
+
|
|
190
|
+
var childNodes[2] = MultiMux1(2)(c, indices[i]);
|
|
191
|
+
|
|
192
|
+
nodes[i + 1] <== PoseidonHasher(2)(childNodes);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
var isDepth = IsEqual()([depth, MAX_DEPTH]);
|
|
196
|
+
|
|
197
|
+
out <== root + isDepth * nodes[MAX_DEPTH];
|
|
198
|
+
}
|
|
@@ -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
|
+
}
|