2021-08-25 00:23:16 +02:00
|
|
|
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
|
|
|
|
template TreeUpdater(levels, subtreeLevels, zeroSubtreeRoot) {
|
|
|
|
var remainingLevels = levels - subtreeLevels;
|
|
|
|
|
2020-04-08 11:41:12 +02:00
|
|
|
signal input oldRoot;
|
|
|
|
signal input newRoot;
|
2021-08-25 00:23:16 +02:00
|
|
|
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
|
2021-08-25 00:23:16 +02:00
|
|
|
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
|
|
|
|
2021-08-25 00:23:16 +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;
|
|
|
|
|
2021-08-25 00:23:16 +02:00
|
|
|
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;
|
2021-08-25 00:23:16 +02:00
|
|
|
treeAfter.leaf <== subtree.root;
|
2020-04-08 11:41:12 +02:00
|
|
|
treeAfter.root === newRoot;
|
2021-08-13 17:43:47 +02:00
|
|
|
}
|