mirror of
https://github.com/tornadocash/tornado-nova
synced 2024-02-02 14:53:56 +01:00
refactor TreeUpdater.circom
This commit is contained in:
parent
bccf576f07
commit
75419e5cff
@ -124,7 +124,7 @@ template Transaction(levels, nIns, nOuts, zeroLeaf) {
|
|||||||
sumIns + extAmount === sumOuts + fee;
|
sumIns + extAmount === sumOuts + fee;
|
||||||
|
|
||||||
// Check merkle tree update with inserted transaction outputs
|
// 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.oldRoot <== root;
|
||||||
treeUpdater.newRoot <== newRoot;
|
treeUpdater.newRoot <== newRoot;
|
||||||
for (var i = 0; i < nOuts; i++) {
|
for (var i = 0; i < nOuts; i++) {
|
||||||
|
@ -1,29 +1,36 @@
|
|||||||
include "./merkleTree.circom";
|
include "./merkleTree.circom";
|
||||||
|
|
||||||
// inserts a pair of leaves into a tree
|
// inserts a subtree into a merkle tree
|
||||||
// checks that tree previously contained zeroes is same positions
|
// checks that tree previously contained zeroes is the same positions
|
||||||
// zeroLeaf is a second level leaf: `hash(0, 0)`
|
// zeroSubtreeRoot is a root of a subtree that contains only zeroes
|
||||||
template TreeUpdater(n, zeroLeaf) {
|
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 oldRoot;
|
||||||
signal input newRoot;
|
signal input newRoot;
|
||||||
signal input leaf[2];
|
signal input leaf[1 << subtreeLevels];
|
||||||
signal input pathIndices;
|
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();
|
component leafPair = HashLeftRight();
|
||||||
leafPair.left <== leaf[0];
|
leafPair.left <== leaf[0];
|
||||||
leafPair.right <== leaf[1];
|
leafPair.right <== leaf[1];
|
||||||
|
|
||||||
component treeBefore = MerkleTree(n - 1);
|
component treeBefore = MerkleTree(remainingLevels);
|
||||||
for(var i = 0; i < n - 1; i++) {
|
for(var i = 0; i < remainingLevels; i++) {
|
||||||
treeBefore.pathElements[i] <== pathElements[i];
|
treeBefore.pathElements[i] <== pathElements[i];
|
||||||
}
|
}
|
||||||
treeBefore.pathIndices <== pathIndices;
|
treeBefore.pathIndices <== pathIndices;
|
||||||
treeBefore.leaf <== zeroLeaf;
|
treeBefore.leaf <== zeroSubtreeRoot;
|
||||||
treeBefore.root === oldRoot;
|
treeBefore.root === oldRoot;
|
||||||
|
|
||||||
component treeAfter = MerkleTree(n - 1);
|
component treeAfter = MerkleTree(remainingLevels);
|
||||||
for(var i = 0; i < n - 1; i++) {
|
for(var i = 0; i < remainingLevels; i++) {
|
||||||
treeAfter.pathElements[i] <== pathElements[i];
|
treeAfter.pathElements[i] <== pathElements[i];
|
||||||
}
|
}
|
||||||
treeAfter.pathIndices <== pathIndices;
|
treeAfter.pathIndices <== pathIndices;
|
||||||
|
Loading…
Reference in New Issue
Block a user