From 4997e640899ff8ecb65800b4b5084fd8e08aa8ef Mon Sep 17 00:00:00 2001 From: cheethas Date: Wed, 29 Mar 2023 11:30:44 +0000 Subject: [PATCH 1/4] feat(memory_tree): frontier paths --- .../stdlib/merkle_tree/hash_path.hpp | 1 + .../stdlib/merkle_tree/memory_tree.cpp | 18 ++++++++++++++++++ .../stdlib/merkle_tree/memory_tree.hpp | 2 ++ 3 files changed, 21 insertions(+) diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp index de8276bb1e..f0a74a4e40 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp @@ -11,6 +11,7 @@ namespace merkle_tree { using namespace barretenberg; typedef std::vector> fr_hash_path; +typedef std::vector fr_frontier_path; template using hash_path = std::vector, field_t>>; inline fr_hash_path get_new_hash_path(fr_hash_path const& old_path, uint128_t index, fr const& value) diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp index 685ee73aa2..71cf58d78e 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp @@ -40,6 +40,24 @@ fr_hash_path MemoryTree::get_hash_path(size_t index) return path; } +fr_frontier_path MemoryTree::get_frontier_path(size_t index) +{ + fr_frontier_path path(depth_); + size_t offset = 0; + size_t layer_size = total_size_; + for (size_t i = 0; i < depth_; i++) { + if (index % 2 == 0) { + path[i] = hashes_[offset + index + 1]; + } else { + path[i] = hashes_[offset + index - 1]; + } + offset += layer_size; + layer_size >>= 1; + index >>= 1; + } + return path; +} + fr MemoryTree::update_element(size_t index, fr const& value) { size_t offset = 0; diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp index ccec978120..a151c0e641 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp @@ -30,6 +30,8 @@ class MemoryTree { fr_hash_path get_hash_path(size_t index); + fr_frontier_path get_frontier_path(size_t index); + fr update_element(size_t index, fr const& value); fr root() const { return root_; } From f88738d711ddf20f00e989baa7f739279143a4cf Mon Sep 17 00:00:00 2001 From: cheethas Date: Fri, 31 Mar 2023 14:53:08 +0000 Subject: [PATCH 2/4] fix: sibling path rename, nullifier tree -> protected vars --- cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp | 2 +- cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp | 4 ++-- cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp | 2 +- .../stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp | 3 ++- .../merkle_tree/nullifier_tree/nullifier_memory_tree.cpp | 2 ++ .../merkle_tree/nullifier_tree/nullifier_memory_tree.hpp | 4 +++- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp index f0a74a4e40..a157e2e68b 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp @@ -11,7 +11,7 @@ namespace merkle_tree { using namespace barretenberg; typedef std::vector> fr_hash_path; -typedef std::vector fr_frontier_path; +typedef std::vector fr_sibling_path; template using hash_path = std::vector, field_t>>; inline fr_hash_path get_new_hash_path(fr_hash_path const& old_path, uint128_t index, fr const& value) diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp index 71cf58d78e..aa716503b2 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp @@ -40,9 +40,9 @@ fr_hash_path MemoryTree::get_hash_path(size_t index) return path; } -fr_frontier_path MemoryTree::get_frontier_path(size_t index) +fr_sibling_path MemoryTree::get_sibling_path(size_t index) { - fr_frontier_path path(depth_); + fr_sibling_path path(depth_); size_t offset = 0; size_t layer_size = total_size_; for (size_t i = 0; i < depth_; i++) { diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp index a151c0e641..1e2e1480dc 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp @@ -30,7 +30,7 @@ class MemoryTree { fr_hash_path get_hash_path(size_t index); - fr_frontier_path get_frontier_path(size_t index); + fr_sibling_path get_sibling_path(size_t index); fr update_element(size_t index, fr const& value); diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp index d94c6638a0..03a6a25d91 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp @@ -44,9 +44,10 @@ inline std::pair find_closest_leaf(std::vector con { std::vector diff; bool repeated = false; + auto new_value_ = uint256_t(new_value); + for (size_t i = 0; i < leaves_.size(); i++) { auto leaf_value_ = uint256_t(leaves_[i].value); - auto new_value_ = uint256_t(new_value); if (leaf_value_ > new_value_) { diff.push_back(leaf_value_); } else if (leaf_value_ == new_value_) { diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp index bbc713ed9e..2dfa928793 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp @@ -1,5 +1,7 @@ #include "nullifier_memory_tree.hpp" #include "../hash.hpp" +#include "barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp" +#include namespace plonk { namespace stdlib { diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp index 3b49fc19bd..4f71632c01 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp @@ -2,6 +2,7 @@ #include "../hash.hpp" #include "../memory_tree.hpp" #include "nullifier_leaf.hpp" +#include namespace plonk { namespace stdlib { @@ -79,8 +80,9 @@ class NullifierMemoryTree : public MemoryTree { const std::vector& get_hashes() { return hashes_; } const std::vector& get_leaves() { return leaves_; } + const nullifier_leaf& get_leaf(size_t index) { return leaves_[index]; } - private: + protected: using MemoryTree::depth_; using MemoryTree::hashes_; using MemoryTree::root_; From 8f2aba4a06b550313d6e64857b08d20e7e7a2820 Mon Sep 17 00:00:00 2001 From: cheethas Date: Fri, 31 Mar 2023 14:58:00 +0000 Subject: [PATCH 3/4] clean --- .../stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp | 2 -- .../stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp | 1 - 2 files changed, 3 deletions(-) diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp index 2dfa928793..bbc713ed9e 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.cpp @@ -1,7 +1,5 @@ #include "nullifier_memory_tree.hpp" #include "../hash.hpp" -#include "barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp" -#include namespace plonk { namespace stdlib { diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp index 4f71632c01..6fc273675b 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_memory_tree.hpp @@ -2,7 +2,6 @@ #include "../hash.hpp" #include "../memory_tree.hpp" #include "nullifier_leaf.hpp" -#include namespace plonk { namespace stdlib { From fd2b8c62e40cc948b861c43cf4a14de3a1d24e03 Mon Sep 17 00:00:00 2001 From: cheethas Date: Mon, 3 Apr 2023 10:07:53 +0000 Subject: [PATCH 4/4] test: sibling path test --- .../stdlib/merkle_tree/memory_tree.test.cpp | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.test.cpp b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.test.cpp index 7466eeb15b..6996c90f40 100644 --- a/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.test.cpp +++ b/cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.test.cpp @@ -1,6 +1,5 @@ #include "memory_tree.hpp" #include -#include "barretenberg/stdlib/types/types.hpp" using namespace barretenberg; using namespace plonk::stdlib::merkle_tree; @@ -43,3 +42,39 @@ TEST(stdlib_merkle_tree, test_memory_store) EXPECT_EQ(db.get_hash_path(3), expected); EXPECT_EQ(db.root(), root); } + +TEST(stdlib_merkle_tree, test_memory_store_sibling_path) +{ + fr e00 = 0; + fr e01 = VALUES[1]; + fr e02 = VALUES[2]; + fr e03 = VALUES[3]; + fr e10 = hash_pair_native(e00, e01); + fr e11 = hash_pair_native(e02, e03); + fr root = hash_pair_native(e10, e11); + + MemoryTree db(2); + for (size_t i = 0; i < 4; ++i) { + db.update_element(i, VALUES[i]); + } + + // Check correct paths are generated for each layer 0 element + fr_sibling_path expected00 = { + e01, + e11, + }; + fr_sibling_path expected01 = { e00, e11 }; + fr_sibling_path expected02 = { + e03, + e10, + }; + fr_sibling_path expected03 = { + e02, + e10, + }; + EXPECT_EQ(db.get_sibling_path(0), expected00); + EXPECT_EQ(db.get_sibling_path(1), expected01); + EXPECT_EQ(db.get_sibling_path(2), expected02); + EXPECT_EQ(db.get_sibling_path(3), expected03); + EXPECT_EQ(db.root(), root); +}