Skip to content

Commit

Permalink
Updating tree heights (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Jul 11, 2023
1 parent 72989cf commit 68b0fd8
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 100 deletions.
2 changes: 1 addition & 1 deletion circuits/cpp/barretenberg
Submodule barretenberg updated 506 files
3 changes: 2 additions & 1 deletion circuits/cpp/src/aztec3/circuits/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ using abis::FunctionData;
using abis::Point;
using aztec3::circuits::abis::ContractLeafPreimage;
using aztec3::circuits::abis::FunctionLeafPreimage;
using MerkleTree = stdlib::merkle_tree::MemoryTree;
using MemoryStore = stdlib::merkle_tree::MemoryStore;
using MerkleTree = stdlib::merkle_tree::MerkleTree<MemoryStore>;

template <typename NCT> typename NCT::fr compute_var_args_hash(std::vector<typename NCT::fr> args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ get_random_reads(NT::fr const& contract_address, int const num_read_requests)
// set -> vector without collisions
std::vector<NT::uint32> rr_leaf_indices(rr_leaf_indices_set.begin(), rr_leaf_indices_set.end());

MerkleTree private_data_tree = MerkleTree(PRIVATE_DATA_TREE_HEIGHT);
MemoryStore private_data_tree_store;
MerkleTree private_data_tree = MerkleTree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

// add the commitments to the private data tree for each read request
// add them at their corresponding index in the tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <barretenberg/barretenberg.hpp>

#include <array>
#include <cstdint>

namespace {

Expand Down Expand Up @@ -47,9 +48,9 @@ using aztec3::circuits::compute_empty_sibling_path;
constexpr size_t MAX_FUNCTION_LEAVES = 1 << aztec3::FUNCTION_TREE_HEIGHT; // 2^(height-1)
// NOTE: *DO NOT* call hashes in static initializers and assign them to constants. This will fail. Instead, use
// lazy initialization or functions. Lambdas were introduced here.
const auto EMPTY_FUNCTION_LEAF = [] { return FunctionLeafPreimage<NT>{}.hash(); }; // hash of empty/0 preimage
const auto EMPTY_CONTRACT_LEAF = [] { return NewContractData<NT>{}.hash(); }; // hash of empty/0 preimage
constexpr size_t PRIVATE_DATA_TREE_NUM_LEAVES = 1 << aztec3::PRIVATE_DATA_TREE_HEIGHT; // 2^(height-1)
const auto EMPTY_FUNCTION_LEAF = [] { return FunctionLeafPreimage<NT>{}.hash(); }; // hash of empty/0 preimage
const auto EMPTY_CONTRACT_LEAF = [] { return NewContractData<NT>{}.hash(); }; // hash of empty/0 preimage
constexpr uint64_t PRIVATE_DATA_TREE_NUM_LEAVES = 1ULL << aztec3::PRIVATE_DATA_TREE_HEIGHT; // 2^(height-1)

inline const auto& get_empty_function_siblings()
{
Expand Down
93 changes: 65 additions & 28 deletions circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ TEST_F(base_rollup_tests, native_no_new_contract_leafs)
// No contract leaves -> will insert empty tree -> i.e. end_contract_tree_root = start_contract_tree_root

BaseRollupInputs emptyInputs = base_rollup_inputs_from_kernels({ get_empty_kernel(), get_empty_kernel() });
auto empty_contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);
MemoryStore contract_tree_store;
auto empty_contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_HEIGHT);

BaseOrMergeRollupPublicInputs outputs =
aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(builder, emptyInputs);
Expand Down Expand Up @@ -168,14 +169,17 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted)
.function_tree_root = fr(2),
};

auto empty_contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);
MemoryStore empty_contract_tree_store;
auto empty_contract_tree = MerkleTree(empty_contract_tree_store, CONTRACT_TREE_HEIGHT);
AppendOnlyTreeSnapshot<NT> const expected_start_contracts_snapshot = {
.root = empty_contract_tree.root(),
.next_available_leaf_index = 0,
};

// create expected end contract tree snapshot
auto expected_end_contracts_snapshot_tree = stdlib::merkle_tree::MemoryTree(CONTRACT_TREE_HEIGHT);
MemoryStore contract_tree_store;
auto expected_end_contracts_snapshot_tree =
stdlib::merkle_tree::MerkleTree<MemoryStore>(contract_tree_store, CONTRACT_TREE_HEIGHT);
expected_end_contracts_snapshot_tree.update_element(0, new_contract.hash());

AppendOnlyTreeSnapshot<NT> const expected_end_contracts_snapshot = {
Expand Down Expand Up @@ -212,7 +216,8 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted_in_non_empty_snapshot_tr
kernel_data[0].public_inputs.end.new_contracts[0] = new_contract;
BaseRollupInputs inputs = base_rollup_inputs_from_kernels(kernel_data);

auto start_contract_tree_snapshot = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);
MemoryStore start_contract_tree_snapshot_store;
auto start_contract_tree_snapshot = MerkleTree(start_contract_tree_snapshot_store, CONTRACT_TREE_HEIGHT);
// insert 12 leaves to the tree (next available leaf index is 12)
for (size_t i = 0; i < 12; ++i) {
start_contract_tree_snapshot.update_element(i, fr(i));
Expand All @@ -232,7 +237,10 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted_in_non_empty_snapshot_tr
auto expected_contract_leaf = crypto::pedersen_commitment::compress_native(
{ new_contract.contract_address, new_contract.portal_contract_address, new_contract.function_tree_root },
GeneratorIndex::CONTRACT_LEAF);
auto expected_end_contracts_snapshot_tree = start_contract_tree_snapshot;

auto expected_end_contract_tree_snapshot_store = start_contract_tree_snapshot_store;
auto expected_end_contracts_snapshot_tree =
MerkleTree(expected_end_contract_tree_snapshot_store, CONTRACT_TREE_HEIGHT);
expected_end_contracts_snapshot_tree.update_element(12, expected_contract_leaf);

AppendOnlyTreeSnapshot<NT> const expected_end_contracts_snapshot = {
Expand Down Expand Up @@ -265,7 +273,8 @@ TEST_F(base_rollup_tests, native_new_commitments_tree)
}

// get sibling path
auto private_data_tree = native_base_rollup::MerkleTree(PRIVATE_DATA_TREE_HEIGHT);
MemoryStore private_data_tree_store;
auto private_data_tree = MerkleTree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
AppendOnlyTreeSnapshot<NT> const expected_start_commitments_snapshot = {
.root = private_data_tree.root(),
.next_available_leaf_index = 0,
Expand Down Expand Up @@ -631,7 +640,8 @@ TEST_F(base_rollup_tests, native_compute_membership_historic_private_data_negati
std::array<PreviousKernelData<NT>, 2> const kernel_data = { get_empty_kernel(), get_empty_kernel() };
BaseRollupInputs inputs = base_rollup_inputs_from_kernels(kernel_data);

auto private_data_tree = native_base_rollup::MerkleTree(PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT);
MemoryStore private_data_store;
auto private_data_tree = MerkleTree(private_data_store, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT);

// Create an INCORRECT sibling path for the private data tree root in the historic tree roots.
auto hash_path = private_data_tree.get_sibling_path(0);
Expand All @@ -658,7 +668,9 @@ TEST_F(base_rollup_tests, native_compute_membership_historic_contract_tree_negat
std::array<PreviousKernelData<NT>, 2> const kernel_data = { get_empty_kernel(), get_empty_kernel() };
BaseRollupInputs inputs = base_rollup_inputs_from_kernels(kernel_data);

auto contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_ROOTS_TREE_HEIGHT);
MemoryStore contract_tree_store;
auto contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_ROOTS_TREE_HEIGHT);


// Create an INCORRECT sibling path for contract tree root in the historic tree roots.
auto hash_path = contract_tree.get_sibling_path(0);
Expand Down Expand Up @@ -746,11 +758,17 @@ TEST_F(base_rollup_tests, native_cbind_0)
TEST_F(base_rollup_tests, native_single_public_state_read)
{
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_single_public_state_read");
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
stdlib::merkle_tree::MemoryStore public_data_tree_store;
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
MemoryStore private_data_tree_store;
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

MemoryStore contract_tree_store;
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);

MemoryStore public_data_tree_store;
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

MemoryStore l1_to_l2_messages_tree_store;
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);

auto data_read = abis::PublicDataRead<NT>{
.leaf_index = fr(1),
Expand All @@ -775,11 +793,18 @@ TEST_F(base_rollup_tests, native_single_public_state_read)
TEST_F(base_rollup_tests, native_single_public_state_write)
{
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_single_public_state_write");
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
stdlib::merkle_tree::MemoryStore public_data_tree_store;
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
MemoryStore private_data_tree_store;
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

MemoryStore contract_tree_store;
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);

MemoryStore public_data_tree_store;
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

MemoryStore l1_to_l2_messages_tree_store;
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);


auto data_write = abis::PublicDataUpdateRequest<NT>{
.leaf_index = fr(1),
Expand All @@ -806,11 +831,17 @@ TEST_F(base_rollup_tests, native_single_public_state_write)
TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
{
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_multiple_public_state_read_writes");
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
stdlib::merkle_tree::MemoryStore public_data_tree_store;
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
MemoryStore private_data_tree_store;
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

MemoryStore contract_tree_store;
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);

MemoryStore public_data_tree_store;
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

MemoryStore l1_to_l2_messages_tree_store;
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);

std::array<PreviousKernelData<NT>, 2> kernel_data = { get_empty_kernel(), get_empty_kernel() };

Expand Down Expand Up @@ -847,11 +878,17 @@ TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
TEST_F(base_rollup_tests, native_invalid_public_state_read)
{
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_invalid_public_state_read");
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
stdlib::merkle_tree::MemoryStore public_data_tree_store;
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
MemoryStore private_data_tree_store;
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);

MemoryStore contract_tree_store;
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);

MemoryStore public_data_tree_store;
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);

MemoryStore l1_to_l2_messages_tree_store;
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);

auto data_read = abis::PublicDataRead<NT>{
.leaf_index = fr(1),
Expand Down
6 changes: 3 additions & 3 deletions circuits/cpp/src/aztec3/circuits/rollup/base/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ using Aggregator = aztec3::circuits::recursion::Aggregator;
using AggregationObject = utils::types::NativeTypes::AggregationObject;
using AppendOnlySnapshot = abis::AppendOnlyTreeSnapshot<NT>;

// Nullifier Tree Alias
using MerkleTree = stdlib::merkle_tree::MemoryTree;
// Tree Aliases
using MemoryStore = stdlib::merkle_tree::MemoryStore;
using MerkleTree = stdlib::merkle_tree::MerkleTree<MemoryStore>;
using NullifierTree = stdlib::merkle_tree::NullifierMemoryTree;
using NullifierLeafPreimage = abis::NullifierLeafPreimage<NT>;
using SparseTree = stdlib::merkle_tree::MerkleTree<stdlib::merkle_tree::MemoryStore>;

} // namespace aztec3::circuits::rollup::native_base_rollup
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace aztec3::circuits::rollup::native_base_rollup {

NT::fr calculate_empty_tree_root(const size_t depth)
{
MerkleTree const empty_tree = MerkleTree(depth);
MemoryStore empty_tree_store;
MerkleTree const empty_tree = MerkleTree(empty_tree_store, depth);
return empty_tree.root();
}

Expand Down Expand Up @@ -84,7 +85,9 @@ std::vector<NT::fr> calculate_contract_leaves(BaseRollupInputs const& baseRollup

NT::fr calculate_contract_subtree(std::vector<NT::fr> contract_leaves)
{
MerkleTree contracts_tree = MerkleTree(CONTRACT_SUBTREE_HEIGHT);
MemoryStore contracts_tree_store;
MerkleTree contracts_tree(contracts_tree_store, CONTRACT_SUBTREE_HEIGHT);


// Compute the merkle root of a contract subtree
// Contracts subtree
Expand All @@ -96,7 +99,9 @@ NT::fr calculate_contract_subtree(std::vector<NT::fr> contract_leaves)

NT::fr calculate_commitments_subtree(DummyBuilder& builder, BaseRollupInputs const& baseRollupInputs)
{
MerkleTree commitments_tree = MerkleTree(PRIVATE_DATA_SUBTREE_HEIGHT);
MemoryStore commitments_tree_store;
MerkleTree commitments_tree(commitments_tree_store, PRIVATE_DATA_SUBTREE_HEIGHT);


for (size_t i = 0; i < 2; i++) {
auto new_commitments = baseRollupInputs.kernel_data[i].public_inputs.end.new_commitments;
Expand Down Expand Up @@ -191,7 +196,8 @@ NT::fr create_nullifier_subtree(
std::array<NullifierLeafPreimage, MAX_NEW_NULLIFIERS_PER_TX * 2> const& nullifier_leaves)
{
// Build a merkle tree of the nullifiers
MerkleTree nullifier_subtree = MerkleTree(NULLIFIER_SUBTREE_HEIGHT);
MemoryStore nullifier_subtree_store;
MerkleTree nullifier_subtree(nullifier_subtree_store, NULLIFIER_SUBTREE_HEIGHT);
for (size_t i = 0; i < nullifier_leaves.size(); i++) {
// hash() checks if nullifier is empty (and if so returns 0)
nullifier_subtree.update_element(i, nullifier_leaves[i].hash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace aztec3::circuits::rollup::components {
*/
NT::fr calculate_empty_tree_root(const size_t depth)
{
stdlib::merkle_tree::MemoryTree const empty_tree = stdlib::merkle_tree::MemoryTree(depth);
MemoryStore empty_tree_store;
MerkleTree const empty_tree = MerkleTree(empty_tree_store, depth);
return empty_tree.root();
}

Expand Down
Loading

0 comments on commit 68b0fd8

Please sign in to comment.