Abstract uncommitted and depth personalization for IncrementalMerkleTree.

This commit is contained in:
Sean Bowe
2018-04-12 17:10:04 -06:00
parent 265eca3f47
commit a7cbb8475f
2 changed files with 22 additions and 10 deletions

View File

@@ -8,7 +8,11 @@
namespace libzcash { 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(); SHA256Compress res = SHA256Compress();
@@ -111,7 +115,7 @@ void IncrementalMerkleTree<Depth, Hash>::append(Hash obj) {
right = obj; right = obj;
} else { } else {
// Combine the leaves and propagate it up the tree // Combine the leaves and propagate it up the tree
boost::optional<Hash> combined = Hash::combine(*left, *right); boost::optional<Hash> combined = Hash::combine(*left, *right, 0);
// Set the "left" leaf to the object and make the "right" leaf none // Set the "left" leaf to the object and make the "right" leaf none
left = obj; left = obj;
@@ -120,7 +124,7 @@ void IncrementalMerkleTree<Depth, Hash>::append(Hash obj) {
for (size_t i = 0; i < Depth; i++) { for (size_t i = 0; i < Depth; i++) {
if (i < parents.size()) { if (i < parents.size()) {
if (parents[i]) { if (parents[i]) {
combined = Hash::combine(*parents[i], *combined); combined = Hash::combine(*parents[i], *combined, i+1);
parents[i] = boost::none; parents[i] = boost::none;
} else { } else {
parents[i] = *combined; parents[i] = *combined;
@@ -202,15 +206,15 @@ Hash IncrementalMerkleTree<Depth, Hash>::root(size_t depth,
Hash combine_left = left ? *left : filler.next(0); Hash combine_left = left ? *left : filler.next(0);
Hash combine_right = right ? *right : 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; size_t d = 1;
BOOST_FOREACH(const boost::optional<Hash>& parent, parents) { BOOST_FOREACH(const boost::optional<Hash>& parent, parents) {
if (parent) { if (parent) {
root = Hash::combine(*parent, root); root = Hash::combine(*parent, root, d);
} else { } else {
root = Hash::combine(root, filler.next(d)); root = Hash::combine(root, filler.next(d), d);
} }
d++; d++;
@@ -219,7 +223,7 @@ Hash IncrementalMerkleTree<Depth, Hash>::root(size_t depth,
// We may not have parents for ancestor trees, so we fill // We may not have parents for ancestor trees, so we fill
// the rest in here. // the rest in here.
while (d < depth) { while (d < depth) {
root = Hash::combine(root, filler.next(d)); root = Hash::combine(root, filler.next(d), d);
d++; d++;
} }

View File

@@ -57,9 +57,9 @@ template<size_t Depth, typename Hash>
class EmptyMerkleRoots { class EmptyMerkleRoots {
public: public:
EmptyMerkleRoots() { EmptyMerkleRoots() {
empty_roots.at(0) = Hash(); empty_roots.at(0) = Hash::uncommitted();
for (size_t d = 1; d <= Depth; d++) { 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) { Hash empty_root(size_t depth) {
@@ -213,7 +213,15 @@ public:
SHA256Compress() : uint256() {} SHA256Compress() : uint256() {}
SHA256Compress(uint256 contents) : uint256(contents) { } 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<size_t Depth, typename Hash> template<size_t Depth, typename Hash>