From 0c062e74c4ef75999b71deb5380abd5b6b642cca Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Wed, 30 Dec 2015 17:44:31 -0700 Subject: [PATCH] Add helper functions to IncrementalMerkleTree. --- libzerocash/IncrementalMerkleTree.cpp | 2 +- libzerocash/IncrementalMerkleTree.h | 11 ++++++++++- tests/merkleTest.cpp | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libzerocash/IncrementalMerkleTree.cpp b/libzerocash/IncrementalMerkleTree.cpp index 2de3d74..8cdabed 100644 --- a/libzerocash/IncrementalMerkleTree.cpp +++ b/libzerocash/IncrementalMerkleTree.cpp @@ -63,7 +63,7 @@ namespace libzerocash { return serialized; } - IncrementalMerkleTreeCompact IncrementalMerkleTreeCompact::Deserialize(std::vector serialized) { + IncrementalMerkleTreeCompact IncrementalMerkleTreeCompact::deserialize(const std::vector& serialized) { IncrementalMerkleTreeCompact deserialized; size_t currentPos = 0; diff --git a/libzerocash/IncrementalMerkleTree.h b/libzerocash/IncrementalMerkleTree.h index 4362799..e185a55 100644 --- a/libzerocash/IncrementalMerkleTree.h +++ b/libzerocash/IncrementalMerkleTree.h @@ -46,7 +46,7 @@ class IncrementalMerkleTreeCompact { std::vector< bool > const& getHashList() { return hashList; } std::vector serialize(); - static IncrementalMerkleTreeCompact Deserialize(std::vector serialized); + static IncrementalMerkleTreeCompact deserialize(const std::vector& serialized); private: IncrementalMerkleTreeCompact() : treeHeight(0) {} @@ -115,6 +115,15 @@ class IncrementalMerkleTree { std::vectorgetRoot(); bool prune(); IncrementalMerkleTreeCompact getCompactRepresentation(); + std::vector serialize() { + auto compact = getCompactRepresentation(); + return compact.serialize(); + } + + static IncrementalMerkleTree deserialize(const std::vector& serialized) { + auto deserialized = IncrementalMerkleTreeCompact::deserialize(serialized); + return IncrementalMerkleTree(deserialized); + } bool fromCompactRepresentation(IncrementalMerkleTreeCompact &rep); diff --git a/tests/merkleTest.cpp b/tests/merkleTest.cpp index 5ebb4e1..f93d30c 100644 --- a/tests/merkleTest.cpp +++ b/tests/merkleTest.cpp @@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE( testCompactRepresentation ) { /* Test serializing and deserializing. */ std::vector serializedCompact = compact.serialize(); - IncrementalMerkleTreeCompact deserializedCompact = IncrementalMerkleTreeCompact::Deserialize(serializedCompact); + IncrementalMerkleTreeCompact deserializedCompact = IncrementalMerkleTreeCompact::deserialize(serializedCompact); BOOST_REQUIRE(compact.getTreeHeight() == deserializedCompact.getTreeHeight()); BOOST_REQUIRE(compact.getHashList() == deserializedCompact.getHashList()); BOOST_REQUIRE(compact.getHashVec() == deserializedCompact.getHashVec()); @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE( testCompactDeserializationFailures ) { for (size_t trunc_len = 0; trunc_len < serialized.size(); trunc_len++) { std::vector truncated(serialized.begin(), serialized.begin() + trunc_len); BOOST_CHECK_THROW( - IncrementalMerkleTreeCompact::Deserialize(truncated), + IncrementalMerkleTreeCompact::deserialize(truncated), std::out_of_range ); } @@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE( testCompactDeserializationFailures ) { std::vector extra_byte = serialized; extra_byte.push_back(0x00); BOOST_CHECK_THROW( - IncrementalMerkleTreeCompact::Deserialize(extra_byte), + IncrementalMerkleTreeCompact::deserialize(extra_byte), std::runtime_error ); }