tornado-nova/circuits/merkleTreeUpdater.circom

38 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

include "./merkleProof.circom";
2020-04-08 11:41:12 +02:00
include "./merkleTree.circom";
2021-08-13 17:43:47 +02:00
// inserts a subtree into a merkle tree
// checks that tree previously contained zeroes is the same positions
// zeroSubtreeRoot is a root of a subtree that contains only zeroes
2021-10-17 13:05:01 +02:00
template MerkleTreeUpdater(levels, subtreeLevels, zeroSubtreeRoot) {
2021-08-13 17:43:47 +02:00
var remainingLevels = levels - subtreeLevels;
2020-04-08 11:41:12 +02:00
signal input oldRoot;
signal input newRoot;
signal input leaves[1 << subtreeLevels];
2020-04-08 11:41:12 +02:00
signal input pathIndices;
2021-08-13 17:43:47 +02:00
signal private input pathElements[remainingLevels];
2020-04-08 11:41:12 +02:00
2021-08-13 17:43:47 +02:00
// calculate subtree root
component subtree = MerkleTree(subtreeLevels);
for(var i = 0; i < (1 << subtreeLevels); i++) {
subtree.leaves[i] <== leaves[i];
}
2020-04-08 11:41:12 +02:00
component treeBefore = MerkleProof(remainingLevels);
2021-08-13 17:43:47 +02:00
for(var i = 0; i < remainingLevels; i++) {
2020-04-08 11:41:12 +02:00
treeBefore.pathElements[i] <== pathElements[i];
}
treeBefore.pathIndices <== pathIndices;
2021-08-13 17:43:47 +02:00
treeBefore.leaf <== zeroSubtreeRoot;
2020-04-08 11:41:12 +02:00
treeBefore.root === oldRoot;
component treeAfter = MerkleProof(remainingLevels);
2021-08-13 17:43:47 +02:00
for(var i = 0; i < remainingLevels; i++) {
2020-04-08 11:41:12 +02:00
treeAfter.pathElements[i] <== pathElements[i];
}
treeAfter.pathIndices <== pathIndices;
treeAfter.leaf <== subtree.root;
2020-04-08 11:41:12 +02:00
treeAfter.root === newRoot;
2021-08-13 17:43:47 +02:00
}