From d60128e168a98c105fa564de50bd04baa0abc838 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 7 Jan 2016 07:50:20 -0700 Subject: [PATCH] Fix padding truncation bug in deserialization --- libzerocash/IncrementalMerkleTree.cpp | 4 +++- tests/merkleTest.cpp | 22 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libzerocash/IncrementalMerkleTree.cpp b/libzerocash/IncrementalMerkleTree.cpp index 17084a6..ac898b6 100644 --- a/libzerocash/IncrementalMerkleTree.cpp +++ b/libzerocash/IncrementalMerkleTree.cpp @@ -80,7 +80,9 @@ namespace libzerocash { currentPos += hashListBytesLength; convertBytesVectorToVector(hashListBytes, deserialized.hashList); /* Remove the multiple-of-8-bits padding. */ - deserialized.hashList.resize(deserialized.treeHeight); + deserialized.hashList.erase(deserialized.hashList.begin(), + deserialized.hashList.end() - deserialized.treeHeight + ); /* hashVec */ size_t hashVecSize = countOnes(deserialized.hashList); diff --git a/tests/merkleTest.cpp b/tests/merkleTest.cpp index 554768f..6df3509 100644 --- a/tests/merkleTest.cpp +++ b/tests/merkleTest.cpp @@ -113,11 +113,23 @@ BOOST_AUTO_TEST_CASE( testRootOfTreeOfNonZeroIsNonZero ) { BOOST_CHECK( expected_root != actual_root ); } +BOOST_AUTO_TEST_CASE( testSerializationEdgeCase ) { + +} + BOOST_AUTO_TEST_CASE( testCompactRepresentation ) { for (uint32_t num_entries = 0; num_entries < 100; num_entries++) { + size_t test_depth = 64; + + if (num_entries == 2) { + // This is a particular failure I'm testing with weird + // padding caused by this depth. + test_depth = 20; + } + std::vector< std::vector > values; std::vector root1, root2; - IncrementalMerkleTree incTree(64); + IncrementalMerkleTree incTree(test_depth); constructNonzeroTestVector(values, num_entries); @@ -126,7 +138,7 @@ BOOST_AUTO_TEST_CASE( testCompactRepresentation ) { IncrementalMerkleTreeCompact compact = incTree.getCompactRepresentation(); - BOOST_REQUIRE( compact.getTreeHeight() == 64 ); + BOOST_REQUIRE( compact.getTreeHeight() == test_depth ); // Calculate what the path to the next-added element should be. std::vector path_bytes(8); @@ -134,8 +146,10 @@ BOOST_AUTO_TEST_CASE( testCompactRepresentation ) { libzerocash::convertIntToBytesVector(num_entries, path_bytes); libzerocash::convertBytesVectorToVector(path_bytes, path_bits); - // Make sure the paths match. - BOOST_REQUIRE( compact.getHashList() == path_bits ); + if (test_depth == 64) { + // Make sure the paths match. + BOOST_REQUIRE( compact.getHashList() == path_bits ); + } // Make sure there's a hash for every '1' bit down the path. BOOST_REQUIRE( compact.getHashVec().size() == libzerocash::countOnes(path_bits) );