Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(memory_tree|a3): add sibling path calculations #301

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/src/barretenberg/stdlib/merkle_tree/hash_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace merkle_tree {
using namespace barretenberg;

typedef std::vector<std::pair<fr, fr>> fr_hash_path;
typedef std::vector<fr> fr_sibling_path;
template <typename Ctx> using hash_path = std::vector<std::pair<field_t<Ctx>, field_t<Ctx>>>;

inline fr_hash_path get_new_hash_path(fr_hash_path const& old_path, uint128_t index, fr const& value)
Expand Down
18 changes: 18 additions & 0 deletions cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ fr_hash_path MemoryTree::get_hash_path(size_t index)
return path;
}

fr_sibling_path MemoryTree::get_sibling_path(size_t index)
{
fr_sibling_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;
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MemoryTree {

fr_hash_path get_hash_path(size_t index);

fr_sibling_path get_sibling_path(size_t index);

fr update_element(size_t index, fr const& value);

fr root() const { return root_; }
Expand Down
37 changes: 36 additions & 1 deletion cpp/src/barretenberg/stdlib/merkle_tree/memory_tree.test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "memory_tree.hpp"
#include <gtest/gtest.h>
#include "barretenberg/stdlib/types/types.hpp"

using namespace barretenberg;
using namespace plonk::stdlib::merkle_tree;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ inline std::pair<size_t, bool> find_closest_leaf(std::vector<nullifier_leaf> con
{
std::vector<uint256_t> 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_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ class NullifierMemoryTree : public MemoryTree {

const std::vector<barretenberg::fr>& get_hashes() { return hashes_; }
const std::vector<nullifier_leaf>& 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_;
Expand Down