tornado-nova/circuits/treeUpdater.circom

40 lines
1.4 KiB
Plaintext
Raw Normal View History

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
template TreeUpdater(levels, subtreeLevels, zeroSubtreeRoot) {
// currently it works only with 1-level subtrees
assert(subtreeLevels == 1);
var remainingLevels = levels - subtreeLevels;
2020-04-08 11:41:12 +02:00
signal input oldRoot;
signal input newRoot;
2021-08-13 17:43:47 +02:00
signal input leaf[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
// todo: make it work with arbitrary subtree levels
// currently it works only with 1-level subtrees
2021-08-24 18:19:26 +02:00
component leafPair = Poseidon(2);
leafPair.inputs[0] <== leaf[0];
leafPair.inputs[1] <== leaf[1];
2020-04-08 11:41:12 +02:00
2021-08-13 17:43:47 +02:00
component treeBefore = MerkleTree(remainingLevels);
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;
2021-08-13 17:43:47 +02:00
component treeAfter = MerkleTree(remainingLevels);
for(var i = 0; i < remainingLevels; i++) {
2020-04-08 11:41:12 +02:00
treeAfter.pathElements[i] <== pathElements[i];
}
treeAfter.pathIndices <== pathIndices;
2021-08-24 18:19:26 +02:00
treeAfter.leaf <== leafPair.out;
2020-04-08 11:41:12 +02:00
treeAfter.root === newRoot;
2021-08-13 17:43:47 +02:00
}