Skip to content

Commit

Permalink
Auto merge of #4136 - LarryRuane:empty-roots, r=Eirik0
Browse files Browse the repository at this point in the history
precompute EmptyMerklePath roots

This is an alternative implementation of #4131.
  • Loading branch information
zkbot committed Oct 17, 2019
2 parents f25d36b + ffb8e67 commit cfc5212
Show file tree
Hide file tree
Showing 6 changed files with 805 additions and 153 deletions.
2 changes: 0 additions & 2 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ JSON_TEST_FILES = \
test/data/tx_valid.json \
test/data/sighash.json \
test/data/merkle_roots.json \
test/data/merkle_roots_empty.json \
test/data/merkle_serialization.json \
test/data/merkle_witness_serialization.json \
test/data/merkle_path.json \
test/data/merkle_commitments.json \
test/data/merkle_roots_sapling.json \
test/data/merkle_roots_empty_sapling.json \
test/data/merkle_serialization_sapling.json \
test/data/merkle_witness_serialization_sapling.json \
test/data/merkle_path_sapling.json \
Expand Down
22 changes: 12 additions & 10 deletions src/gtest/test_merkletree.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include <gtest/gtest.h>

#include "test/data/merkle_roots.json.h"
#include "test/data/merkle_roots_empty.json.h"
#include "test/data/merkle_serialization.json.h"
#include "test/data/merkle_witness_serialization.json.h"
#include "test/data/merkle_path.json.h"
#include "test/data/merkle_commitments.json.h"

#include "test/data/merkle_roots_sapling.json.h"
#include "test/data/merkle_roots_empty_sapling.json.h"
#include "test/data/merkle_serialization_sapling.json.h"
#include "test/data/merkle_witness_serialization_sapling.json.h"
#include "test/data/merkle_path_sapling.json.h"
Expand Down Expand Up @@ -165,25 +163,29 @@ TEST(merkletree, SaplingVectors) {
}

TEST(merkletree, emptyroots) {
UniValue empty_roots = read_json(MAKE_STRING(json_tests::merkle_roots_empty));

libzcash::EmptyMerkleRoots<64, libzcash::SHA256Compress> emptyroots;
std::array<libzcash::SHA256Compress, 65> computed;

for (size_t depth = 0; depth <= 64; depth++) {
expect_test_vector(empty_roots[depth], emptyroots.empty_root(depth));
computed.at(0) = libzcash::SHA256Compress::uncommitted();
ASSERT_TRUE(emptyroots.empty_root(0) == computed.at(0));
for (size_t d = 1; d <= 64; d++) {
computed.at(d) = libzcash::SHA256Compress::combine(computed.at(d-1), computed.at(d-1), d-1);
ASSERT_TRUE(emptyroots.empty_root(d) == computed.at(d));
}

// Double check that we're testing (at least) all the empty roots we'll use.
ASSERT_TRUE(INCREMENTAL_MERKLE_TREE_DEPTH <= 64);
}

TEST(merkletree, EmptyrootsSapling) {
UniValue empty_roots = read_json(MAKE_STRING(json_tests::merkle_roots_empty_sapling));

libzcash::EmptyMerkleRoots<62, libzcash::PedersenHash> emptyroots;
std::array<libzcash::PedersenHash, 63> computed;

for (size_t depth = 0; depth <= 62; depth++) {
expect_test_vector(empty_roots[depth], emptyroots.empty_root(depth));
computed.at(0) = libzcash::PedersenHash::uncommitted();
ASSERT_TRUE(emptyroots.empty_root(0) == computed.at(0));
for (size_t d = 1; d <= 62; d++) {
computed.at(d) = libzcash::PedersenHash::combine(computed.at(d-1), computed.at(d-1), d-1);
ASSERT_TRUE(emptyroots.empty_root(d) == computed.at(d));
}

// Double check that we're testing (at least) all the empty roots we'll use.
Expand Down
68 changes: 0 additions & 68 deletions src/test/data/merkle_roots_empty.json

This file was deleted.

65 changes: 0 additions & 65 deletions src/test/data/merkle_roots_empty_sapling.json

This file was deleted.

0 comments on commit cfc5212

Please sign in to comment.