From a7cbb8475f96621d554df600fdf244ca2464c6fa Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 12 Apr 2018 17:10:04 -0600 Subject: [PATCH] Abstract `uncommitted` and depth personalization for IncrementalMerkleTree. --- src/zcash/IncrementalMerkleTree.cpp | 18 +++++++++++------- src/zcash/IncrementalMerkleTree.hpp | 14 +++++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/zcash/IncrementalMerkleTree.cpp b/src/zcash/IncrementalMerkleTree.cpp index f8fafb31a..3101cf3f3 100644 --- a/src/zcash/IncrementalMerkleTree.cpp +++ b/src/zcash/IncrementalMerkleTree.cpp @@ -8,7 +8,11 @@ namespace libzcash { -SHA256Compress SHA256Compress::combine(const SHA256Compress& a, const SHA256Compress& b) +SHA256Compress SHA256Compress::combine( + const SHA256Compress& a, + const SHA256Compress& b, + size_t depth +) { SHA256Compress res = SHA256Compress(); @@ -111,7 +115,7 @@ void IncrementalMerkleTree::append(Hash obj) { right = obj; } else { // Combine the leaves and propagate it up the tree - boost::optional combined = Hash::combine(*left, *right); + boost::optional combined = Hash::combine(*left, *right, 0); // Set the "left" leaf to the object and make the "right" leaf none left = obj; @@ -120,7 +124,7 @@ void IncrementalMerkleTree::append(Hash obj) { for (size_t i = 0; i < Depth; i++) { if (i < parents.size()) { if (parents[i]) { - combined = Hash::combine(*parents[i], *combined); + combined = Hash::combine(*parents[i], *combined, i+1); parents[i] = boost::none; } else { parents[i] = *combined; @@ -202,15 +206,15 @@ Hash IncrementalMerkleTree::root(size_t depth, Hash combine_left = left ? *left : filler.next(0); Hash combine_right = right ? *right : filler.next(0); - Hash root = Hash::combine(combine_left, combine_right); + Hash root = Hash::combine(combine_left, combine_right, 0); size_t d = 1; BOOST_FOREACH(const boost::optional& parent, parents) { if (parent) { - root = Hash::combine(*parent, root); + root = Hash::combine(*parent, root, d); } else { - root = Hash::combine(root, filler.next(d)); + root = Hash::combine(root, filler.next(d), d); } d++; @@ -219,7 +223,7 @@ Hash IncrementalMerkleTree::root(size_t depth, // We may not have parents for ancestor trees, so we fill // the rest in here. while (d < depth) { - root = Hash::combine(root, filler.next(d)); + root = Hash::combine(root, filler.next(d), d); d++; } diff --git a/src/zcash/IncrementalMerkleTree.hpp b/src/zcash/IncrementalMerkleTree.hpp index c4cf9d675..ce536fec4 100644 --- a/src/zcash/IncrementalMerkleTree.hpp +++ b/src/zcash/IncrementalMerkleTree.hpp @@ -57,9 +57,9 @@ template class EmptyMerkleRoots { public: EmptyMerkleRoots() { - empty_roots.at(0) = Hash(); + empty_roots.at(0) = Hash::uncommitted(); for (size_t d = 1; d <= Depth; d++) { - empty_roots.at(d) = Hash::combine(empty_roots.at(d-1), empty_roots.at(d-1)); + empty_roots.at(d) = Hash::combine(empty_roots.at(d-1), empty_roots.at(d-1), d-1); } } Hash empty_root(size_t depth) { @@ -213,7 +213,15 @@ public: SHA256Compress() : uint256() {} SHA256Compress(uint256 contents) : uint256(contents) { } - static SHA256Compress combine(const SHA256Compress& a, const SHA256Compress& b); + static SHA256Compress combine( + const SHA256Compress& a, + const SHA256Compress& b, + size_t depth + ); + + static SHA256Compress uncommitted() { + return SHA256Compress(); + } }; template