@maci-protocol/circuits 0.0.0-ci.fd5247e → 0.0.0-ci.ffabe48
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/build/tsconfig.build.tsbuildinfo +1 -1
- package/circom/circuits.json +16 -0
- package/circom/coordinator/full/MessageProcessor.circom +253 -0
- package/circom/coordinator/full/SingleMessageProcessor.circom +204 -0
- package/circom/coordinator/non-qv/processMessages.circom +14 -13
- package/circom/coordinator/non-qv/tallyVotes.circom +18 -18
- package/circom/coordinator/qv/processMessages.circom +14 -13
- package/circom/coordinator/qv/tallyVotes.circom +15 -15
- package/circom/utils/CalculateTotal.circom +6 -6
- package/circom/utils/full/MessageValidator.circom +2 -4
- package/circom/utils/non-qv/MessageValidator.circom +2 -2
- package/circom/utils/qv/MessageValidator.circom +2 -2
- package/circom/utils/trees/LeafExists.circom +2 -2
- package/circom/utils/trees/MerkleTreeInclusionProof.circom +4 -4
- 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/package.json +9 -9
- package/circom/utils/trees/incrementalQuinaryTree.circom +0 -287
|
@@ -10,7 +10,8 @@ include "../../utils/MessageHasher.circom";
|
|
|
10
10
|
include "../../utils/MessageToCommand.circom";
|
|
11
11
|
include "../../utils/PrivateToPublicKey.circom";
|
|
12
12
|
include "../../utils/qv/StateLeafAndBallotTransformer.circom";
|
|
13
|
-
include "../../utils/trees/
|
|
13
|
+
include "../../utils/trees/QuinaryTreeInclusionProof.circom";
|
|
14
|
+
include "../../utils/trees/QuinaryGeneratePathIndices.circom";
|
|
14
15
|
include "../../utils/trees/MerkleTreeInclusionProof.circom";
|
|
15
16
|
include "../../utils/trees/LeafExists.circom";
|
|
16
17
|
include "../../utils/trees/CheckRoot.circom";
|
|
@@ -205,20 +206,20 @@ template ProcessMessages(
|
|
|
205
206
|
// Start from batchSize and decrement for process in reverse order.
|
|
206
207
|
for (var i = batchSize - 1; i >= 0; i--) {
|
|
207
208
|
// Process as vote type message.
|
|
208
|
-
var
|
|
209
|
-
var
|
|
210
|
-
var
|
|
209
|
+
var computedCurrentStateLeavesPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
210
|
+
var computedCurrentBallotPathElements[stateTreeDepth][STATE_TREE_ARITY - 1];
|
|
211
|
+
var computedCurrentVoteWeightsPathElements[voteOptionTreeDepth][VOTE_OPTION_TREE_ARITY - 1];
|
|
211
212
|
|
|
212
213
|
for (var j = 0; j < stateTreeDepth; j++) {
|
|
213
214
|
for (var k = 0; k < STATE_TREE_ARITY - 1; k++) {
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
computedCurrentStateLeavesPathElements[j][k] = currentStateLeavesPathElements[i][j][k];
|
|
216
|
+
computedCurrentBallotPathElements[j][k] = currentBallotsPathElements[i][j][k];
|
|
216
217
|
}
|
|
217
218
|
}
|
|
218
219
|
|
|
219
220
|
for (var j = 0; j < voteOptionTreeDepth; j++) {
|
|
220
221
|
for (var k = 0; k < VOTE_OPTION_TREE_ARITY - 1; k++) {
|
|
221
|
-
|
|
222
|
+
computedCurrentVoteWeightsPathElements[j][k] = currentVoteWeightsPathElements[i][j][k];
|
|
222
223
|
}
|
|
223
224
|
}
|
|
224
225
|
|
|
@@ -228,11 +229,11 @@ template ProcessMessages(
|
|
|
228
229
|
ballotRoots[i + 1],
|
|
229
230
|
actualStateTreeDepth,
|
|
230
231
|
currentStateLeaves[i],
|
|
231
|
-
|
|
232
|
+
computedCurrentStateLeavesPathElements,
|
|
232
233
|
currentBallots[i],
|
|
233
|
-
|
|
234
|
+
computedCurrentBallotPathElements,
|
|
234
235
|
currentVoteWeights[i],
|
|
235
|
-
|
|
236
|
+
computedCurrentVoteWeightsPathElements,
|
|
236
237
|
computedCommandsStateIndex[i],
|
|
237
238
|
computedCommandsNewPublicKey[i],
|
|
238
239
|
computedCommandsVoteOptionIndex[i],
|
|
@@ -388,9 +389,9 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
388
389
|
commandNewVoteWeightSquare <== commandNewVoteWeight * commandNewVoteWeight;
|
|
389
390
|
|
|
390
391
|
var commandVoteOptionIndexMux = Mux1()([0, commandVoteOptionIndex], computedIsVoteOptionIndexValid);
|
|
391
|
-
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] =
|
|
392
|
+
var computedCurrentVoteWeightPathIndices[voteOptionTreeDepth] = QuinaryGeneratePathIndices(voteOptionTreeDepth)(commandVoteOptionIndexMux);
|
|
392
393
|
|
|
393
|
-
var computedCurrentVoteWeightQip =
|
|
394
|
+
var computedCurrentVoteWeightQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
394
395
|
currentVoteWeight,
|
|
395
396
|
computedCurrentVoteWeightPathIndices,
|
|
396
397
|
currentVoteWeightsPathElements
|
|
@@ -408,7 +409,7 @@ template ProcessOne(stateTreeDepth, voteOptionTreeDepth) {
|
|
|
408
409
|
);
|
|
409
410
|
|
|
410
411
|
// 5.1. Update the ballot's vote option root with the new vote weight.
|
|
411
|
-
var computedNewVoteOptionTreeQip =
|
|
412
|
+
var computedNewVoteOptionTreeQip = QuinaryTreeInclusionProof(voteOptionTreeDepth)(
|
|
412
413
|
voteWeightMux,
|
|
413
414
|
computedCurrentVoteWeightPathIndices,
|
|
414
415
|
currentVoteWeightsPathElements
|
|
@@ -8,7 +8,7 @@ include "./unpack-element.circom";
|
|
|
8
8
|
include "../../utils/trees/CheckRoot.circom";
|
|
9
9
|
include "../../utils/trees/MerklePathIndicesGenerator.circom";
|
|
10
10
|
include "../../utils/trees/LeafExists.circom";
|
|
11
|
-
include "../../utils/trees/
|
|
11
|
+
include "../../utils/trees/QuinaryCheckRoot.circom";
|
|
12
12
|
include "../../utils/CalculateTotal.circom";
|
|
13
13
|
include "../../utils/PoseidonHasher.circom";
|
|
14
14
|
|
|
@@ -18,22 +18,22 @@ include "../../utils/PoseidonHasher.circom";
|
|
|
18
18
|
*/
|
|
19
19
|
template TallyVotes(
|
|
20
20
|
stateTreeDepth,
|
|
21
|
-
|
|
21
|
+
tallyProcessingStateTreeDepth,
|
|
22
22
|
voteOptionTreeDepth
|
|
23
23
|
) {
|
|
24
24
|
// Ensure there's at least one level in the vote option tree.
|
|
25
25
|
assert(voteOptionTreeDepth > 0);
|
|
26
26
|
// Ensure the intermediate state tree has at least one level.
|
|
27
|
-
assert(
|
|
27
|
+
assert(tallyProcessingStateTreeDepth > 0);
|
|
28
28
|
// The intermediate state tree must be smaller than the full state tree.
|
|
29
|
-
assert(
|
|
29
|
+
assert(tallyProcessingStateTreeDepth < stateTreeDepth);
|
|
30
30
|
|
|
31
31
|
// Number of children per node in the tree, defining the tree's branching factor.
|
|
32
32
|
var TREE_ARITY = 5;
|
|
33
33
|
var BALLOT_TREE_ARITY = 2;
|
|
34
34
|
|
|
35
35
|
// The number of ballots processed at once, determined by the depth of the intermediate state tree.
|
|
36
|
-
var batchSize = BALLOT_TREE_ARITY **
|
|
36
|
+
var batchSize = BALLOT_TREE_ARITY ** tallyProcessingStateTreeDepth;
|
|
37
37
|
// Number of voting options available, determined by the depth of the vote option tree.
|
|
38
38
|
var totalVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
|
|
39
39
|
|
|
@@ -44,7 +44,7 @@ template TallyVotes(
|
|
|
44
44
|
// Index for the voting option root in the ballot array.
|
|
45
45
|
var BALLOT_VOTE_OPTION_ROOT_INDEX = 1;
|
|
46
46
|
// Difference in tree depths, used in path calculations.
|
|
47
|
-
var
|
|
47
|
+
var STATE_TREE_DEPTH_DIFFERENCE = stateTreeDepth - tallyProcessingStateTreeDepth;
|
|
48
48
|
|
|
49
49
|
// Root of the state Merkle tree, representing the overall state before voting.
|
|
50
50
|
signal input stateRoot;
|
|
@@ -64,7 +64,7 @@ template TallyVotes(
|
|
|
64
64
|
signal input totalSignups;
|
|
65
65
|
// Ballots and their corresponding path elements for verification in the tree.
|
|
66
66
|
signal input ballots[batchSize][BALLOT_LENGTH];
|
|
67
|
-
signal input ballotPathElements[
|
|
67
|
+
signal input ballotPathElements[STATE_TREE_DEPTH_DIFFERENCE][BALLOT_TREE_ARITY - 1];
|
|
68
68
|
signal input votes[batchSize][totalVoteOptions];
|
|
69
69
|
// Current results for each vote option.
|
|
70
70
|
signal input currentResults[totalVoteOptions];
|
|
@@ -100,11 +100,11 @@ template TallyVotes(
|
|
|
100
100
|
computedBallotHashers[i] = PoseidonHasher(2)([ballots[i][BALLOT_NONCE_INDEX], ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX]]);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
var computedBallotSubroot = CheckRoot(
|
|
104
|
-
var computedBallotPathIndices[
|
|
103
|
+
var computedBallotSubroot = CheckRoot(tallyProcessingStateTreeDepth)(computedBallotHashers);
|
|
104
|
+
var computedBallotPathIndices[STATE_TREE_DEPTH_DIFFERENCE] = MerklePathIndicesGenerator(STATE_TREE_DEPTH_DIFFERENCE)(index / batchSize);
|
|
105
105
|
|
|
106
106
|
// Verifies each ballot's existence within the ballot tree.
|
|
107
|
-
LeafExists(
|
|
107
|
+
LeafExists(STATE_TREE_DEPTH_DIFFERENCE)(
|
|
108
108
|
computedBallotSubroot,
|
|
109
109
|
ballotPathElements,
|
|
110
110
|
computedBallotPathIndices,
|
|
@@ -114,7 +114,7 @@ template TallyVotes(
|
|
|
114
114
|
// Processes vote options, verifying each against its declared root.
|
|
115
115
|
var computedVoteTree[batchSize];
|
|
116
116
|
for (var i = 0; i < batchSize; i++) {
|
|
117
|
-
computedVoteTree[i] =
|
|
117
|
+
computedVoteTree[i] = QuinaryCheckRoot(voteOptionTreeDepth)(votes[i]);
|
|
118
118
|
computedVoteTree[i] === ballots[i][BALLOT_VOTE_OPTION_ROOT_INDEX];
|
|
119
119
|
}
|
|
120
120
|
|
|
@@ -227,7 +227,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
227
227
|
signal input newPerVoteOptionSpentVoiceCreditsRootSalt;
|
|
228
228
|
|
|
229
229
|
// Compute the commitment to the current results.
|
|
230
|
-
var computedCurrentResultsRoot =
|
|
230
|
+
var computedCurrentResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentResults);
|
|
231
231
|
|
|
232
232
|
// Verify currentResultsCommitmentHash.
|
|
233
233
|
var computedCurrentResultsCommitment = PoseidonHasher(2)([computedCurrentResultsRoot, currentResultsRootSalt]);
|
|
@@ -236,7 +236,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
236
236
|
var computedCurrentSpentVoiceCreditsCommitment = PoseidonHasher(2)([currentSpentVoiceCreditSubtotal, currentSpentVoiceCreditSubtotalSalt]);
|
|
237
237
|
|
|
238
238
|
// Compute the root of the spent voice credits per vote option.
|
|
239
|
-
var computedCurrentPerVoteOptionSpentVoiceCreditsRoot =
|
|
239
|
+
var computedCurrentPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(currentPerVoteOptionSpentVoiceCredits);
|
|
240
240
|
var computedCurrentPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedCurrentPerVoteOptionSpentVoiceCreditsRoot, currentPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
241
241
|
|
|
242
242
|
// Commit to the current tally.
|
|
@@ -258,14 +258,14 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
|
|
|
258
258
|
isFirstCommitment === currentTallyCommitment;
|
|
259
259
|
|
|
260
260
|
// Compute the root of the new results.
|
|
261
|
-
var computedNewResultsRoot =
|
|
261
|
+
var computedNewResultsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newResults);
|
|
262
262
|
var computedNewResultsCommitment = PoseidonHasher(2)([computedNewResultsRoot, newResultsRootSalt]);
|
|
263
263
|
|
|
264
264
|
// Compute the commitment to the new spent voice credits value.
|
|
265
265
|
var computedNewSpentVoiceCreditsCommitment = PoseidonHasher(2)([newSpentVoiceCreditSubtotal, newSpentVoiceCreditSubtotalSalt]);
|
|
266
266
|
|
|
267
267
|
// Compute the root of the spent voice credits per vote option.
|
|
268
|
-
var computedNewPerVoteOptionSpentVoiceCreditsRoot =
|
|
268
|
+
var computedNewPerVoteOptionSpentVoiceCreditsRoot = QuinaryCheckRoot(voteOptionTreeDepth)(newPerVoteOptionSpentVoiceCredits);
|
|
269
269
|
var computedNewPerVoteOptionSpentVoiceCreditsCommitment = PoseidonHasher(2)([computedNewPerVoteOptionSpentVoiceCreditsRoot, newPerVoteOptionSpentVoiceCreditsRootSalt]);
|
|
270
270
|
|
|
271
271
|
// Commit to the new tally.
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
pragma circom 2.0.0;
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Computes the cumulative sum of an array of
|
|
4
|
+
* Computes the cumulative sum of an array of length input signals.
|
|
5
5
|
* It iterates through each input, aggregating the sum up to that point,
|
|
6
6
|
* and outputs the total sum of all inputs. This template is useful for
|
|
7
7
|
* operations requiring the total sum of multiple signals, ensuring the
|
|
8
8
|
* final output reflects the cumulative total of the inputs provided.
|
|
9
9
|
*/
|
|
10
|
-
template CalculateTotal(
|
|
10
|
+
template CalculateTotal(length) {
|
|
11
11
|
// Array of values.
|
|
12
|
-
signal input nums[
|
|
12
|
+
signal input nums[length];
|
|
13
13
|
// Total sum.
|
|
14
14
|
signal output sum;
|
|
15
15
|
|
|
16
|
-
signal sums[
|
|
16
|
+
signal sums[length];
|
|
17
17
|
sums[0] <== nums[0];
|
|
18
18
|
|
|
19
|
-
for (var i = 1; i <
|
|
19
|
+
for (var i = 1; i < length; i++) {
|
|
20
20
|
sums[i] <== sums[i - 1] + nums[i];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
sum <== sums[
|
|
23
|
+
sum <== sums[length - 1];
|
|
24
24
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
pragma circom 2.0.0;
|
|
2
2
|
|
|
3
|
-
// circomlib import
|
|
4
|
-
include "./mux1.circom";
|
|
5
3
|
// zk-kit imports
|
|
6
4
|
include "./safe-comparators.circom";
|
|
7
5
|
// local imports
|
|
@@ -28,7 +26,7 @@ template MessageValidatorFull() {
|
|
|
28
26
|
// Ballot nonce.
|
|
29
27
|
signal input originalNonce;
|
|
30
28
|
// Command nonce.
|
|
31
|
-
signal input
|
|
29
|
+
signal input commandNonce;
|
|
32
30
|
// Packed command.
|
|
33
31
|
signal input command[PACKED_COMMAND_LENGTH];
|
|
34
32
|
// Public key of the state leaf (user).
|
|
@@ -61,7 +59,7 @@ template MessageValidatorFull() {
|
|
|
61
59
|
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
62
60
|
|
|
63
61
|
// Check (3) - The nonce must be correct.
|
|
64
|
-
var computedIsNonceValid = IsEqual()([originalNonce + 1,
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
65
63
|
|
|
66
64
|
// Check (4) - The signature must be correct.
|
|
67
65
|
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
@@ -26,7 +26,7 @@ template MessageValidatorNonQv() {
|
|
|
26
26
|
// Ballot nonce.
|
|
27
27
|
signal input originalNonce;
|
|
28
28
|
// Command nonce.
|
|
29
|
-
signal input
|
|
29
|
+
signal input commandNonce;
|
|
30
30
|
// Packed command.
|
|
31
31
|
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
32
|
// Public key of the state leaf (user).
|
|
@@ -59,7 +59,7 @@ template MessageValidatorNonQv() {
|
|
|
59
59
|
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
60
|
|
|
61
61
|
// Check (3) - The nonce must be correct.
|
|
62
|
-
var computedIsNonceValid = IsEqual()([originalNonce + 1,
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
63
|
|
|
64
64
|
// Check (4) - The signature must be correct.
|
|
65
65
|
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
@@ -26,7 +26,7 @@ template MessageValidator() {
|
|
|
26
26
|
// Ballot nonce.
|
|
27
27
|
signal input originalNonce;
|
|
28
28
|
// Command nonce.
|
|
29
|
-
signal input
|
|
29
|
+
signal input commandNonce;
|
|
30
30
|
// Packed command.
|
|
31
31
|
signal input command[PACKED_COMMAND_LENGTH];
|
|
32
32
|
// Public key of the state leaf (user).
|
|
@@ -59,7 +59,7 @@ template MessageValidator() {
|
|
|
59
59
|
var computedIsVoteOptionIndexValid = SafeLessThan(252)([voteOptionIndex, voteOptions]);
|
|
60
60
|
|
|
61
61
|
// Check (3) - The nonce must be correct.
|
|
62
|
-
var computedIsNonceValid = IsEqual()([originalNonce + 1,
|
|
62
|
+
var computedIsNonceValid = IsEqual()([originalNonce + 1, commandNonce]);
|
|
63
63
|
|
|
64
64
|
// Check (4) - The signature must be correct.
|
|
65
65
|
var computedIsSignatureValid = VerifySignature()(publicKey, signaturePoint, signatureScalar, command);
|
|
@@ -13,13 +13,13 @@ template LeafExists(levels) {
|
|
|
13
13
|
// The elements along the path needed for the inclusion proof.
|
|
14
14
|
signal input path_elements[levels][1];
|
|
15
15
|
// The indices indicating the path taken through the tree for the leaf.
|
|
16
|
-
signal input
|
|
16
|
+
signal input path_indices[levels];
|
|
17
17
|
// The root of the Merkle tree, against which the inclusion is verified.
|
|
18
18
|
signal input root;
|
|
19
19
|
|
|
20
20
|
var computedMerkleRoot = MerkleTreeInclusionProof(levels)(
|
|
21
21
|
leaf,
|
|
22
|
-
|
|
22
|
+
path_indices,
|
|
23
23
|
path_elements
|
|
24
24
|
);
|
|
25
25
|
|
|
@@ -12,7 +12,7 @@ template MerkleTreeInclusionProof(n_levels) {
|
|
|
12
12
|
// The leaf node from which the Merkle root is calculated.
|
|
13
13
|
signal input leaf;
|
|
14
14
|
// Indices indicating left or right child for each level of the tree.
|
|
15
|
-
signal input
|
|
15
|
+
signal input path_indices[n_levels];
|
|
16
16
|
// Sibling node values required to compute the hash at each level.
|
|
17
17
|
signal input path_elements[n_levels][1];
|
|
18
18
|
|
|
@@ -25,8 +25,8 @@ template MerkleTreeInclusionProof(n_levels) {
|
|
|
25
25
|
levelHashes[0] <== leaf;
|
|
26
26
|
|
|
27
27
|
for (var i = 0; i < n_levels; i++) {
|
|
28
|
-
// Validate
|
|
29
|
-
|
|
28
|
+
// Validate path_indices to be either 0 or 1, ensuring no other values.
|
|
29
|
+
path_indices[i] * (1 - path_indices[i]) === 0;
|
|
30
30
|
|
|
31
31
|
// Configure the multiplexer based on the path index for the current level.
|
|
32
32
|
var multiplexer[2][2] = [
|
|
@@ -36,7 +36,7 @@ template MerkleTreeInclusionProof(n_levels) {
|
|
|
36
36
|
|
|
37
37
|
var multiplexerResult[2] = MultiMux1(2)(
|
|
38
38
|
multiplexer,
|
|
39
|
-
|
|
39
|
+
path_indices[i]
|
|
40
40
|
);
|
|
41
41
|
|
|
42
42
|
var computedLevelHash = PoseidonHasher(2)([multiplexerResult[0], multiplexerResult[1]]);
|
|
@@ -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
|
+
}
|