refactor TreeUpdater.circom

This commit is contained in:
poma 2021-08-13 18:43:47 +03:00
parent bccf576f07
commit 75419e5cff
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
2 changed files with 20 additions and 13 deletions

View File

@ -124,7 +124,7 @@ template Transaction(levels, nIns, nOuts, zeroLeaf) {
sumIns + extAmount === sumOuts + fee;
// Check merkle tree update with inserted transaction outputs
component treeUpdater = TreeUpdater(levels, zeroLeaf);
component treeUpdater = TreeUpdater(levels, 1 /* log2(nOuts) */, zeroLeaf);
treeUpdater.oldRoot <== root;
treeUpdater.newRoot <== newRoot;
for (var i = 0; i < nOuts; i++) {

View File

@ -1,32 +1,39 @@
include "./merkleTree.circom";
// inserts a pair of leaves into a tree
// checks that tree previously contained zeroes is same positions
// zeroLeaf is a second level leaf: `hash(0, 0)`
template TreeUpdater(n, zeroLeaf) {
// 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;
signal input oldRoot;
signal input newRoot;
signal input leaf[2];
signal input leaf[1 << subtreeLevels];
signal input pathIndices;
signal private input pathElements[n - 1];
signal private input pathElements[remainingLevels];
// calculate subtree root
// todo: make it work with arbitrary subtree levels
// currently it works only with 1-level subtrees
component leafPair = HashLeftRight();
leafPair.left <== leaf[0];
leafPair.right <== leaf[1];
component treeBefore = MerkleTree(n - 1);
for(var i = 0; i < n - 1; i++) {
component treeBefore = MerkleTree(remainingLevels);
for(var i = 0; i < remainingLevels; i++) {
treeBefore.pathElements[i] <== pathElements[i];
}
treeBefore.pathIndices <== pathIndices;
treeBefore.leaf <== zeroLeaf;
treeBefore.leaf <== zeroSubtreeRoot;
treeBefore.root === oldRoot;
component treeAfter = MerkleTree(n - 1);
for(var i = 0; i < n - 1; i++) {
component treeAfter = MerkleTree(remainingLevels);
for(var i = 0; i < remainingLevels; i++) {
treeAfter.pathElements[i] <== pathElements[i];
}
treeAfter.pathIndices <== pathIndices;
treeAfter.leaf <== leafPair.hash;
treeAfter.root === newRoot;
}
}