From 68b0fd844533f5cad46698f12193e0c97e39977d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Tue, 11 Jul 2023 17:44:01 +0200 Subject: [PATCH] Updating tree heights (#975) --- circuits/cpp/barretenberg | 2 +- circuits/cpp/src/aztec3/circuits/hash.hpp | 3 +- .../kernel/private/testing_harness.cpp | 3 +- .../kernel/private/testing_harness.hpp | 7 +- .../src/aztec3/circuits/rollup/base/.test.cpp | 93 +++++++++++++------ .../src/aztec3/circuits/rollup/base/init.hpp | 6 +- .../base/native_base_rollup_circuit.cpp | 14 ++- .../circuits/rollup/components/components.cpp | 3 +- .../src/aztec3/circuits/rollup/root/.test.cpp | 40 +++++--- .../src/aztec3/circuits/rollup/root/init.hpp | 3 +- .../root/native_root_rollup_circuit.cpp | 3 +- .../circuits/rollup/test_utils/init.hpp | 5 +- .../nullifier_tree_testing_harness.hpp | 2 +- .../circuits/rollup/test_utils/utils.cpp | 73 +++++++++------ .../circuits/rollup/test_utils/utils.hpp | 12 +-- circuits/cpp/src/aztec3/constants.hpp | 12 +-- 16 files changed, 181 insertions(+), 100 deletions(-) diff --git a/circuits/cpp/barretenberg b/circuits/cpp/barretenberg index d60b16a1421..b3db9f8944e 160000 --- a/circuits/cpp/barretenberg +++ b/circuits/cpp/barretenberg @@ -1 +1 @@ -Subproject commit d60b16a14219fd4bd130ce4537c3e94bfa10128f +Subproject commit b3db9f8944e546cd9da9a1529e2562ee75e62369 diff --git a/circuits/cpp/src/aztec3/circuits/hash.hpp b/circuits/cpp/src/aztec3/circuits/hash.hpp index 29986cc3dee..8beddaf61b5 100644 --- a/circuits/cpp/src/aztec3/circuits/hash.hpp +++ b/circuits/cpp/src/aztec3/circuits/hash.hpp @@ -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; template typename NCT::fr compute_var_args_hash(std::vector args) { diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.cpp index 7a93358a906..6b107bfed91 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.cpp @@ -82,7 +82,8 @@ get_random_reads(NT::fr const& contract_address, int const num_read_requests) // set -> vector without collisions std::vector 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 diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.hpp b/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.hpp index fed6000dac7..bc15e1d6763 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.hpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.hpp @@ -15,6 +15,7 @@ #include #include +#include namespace { @@ -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{}.hash(); }; // hash of empty/0 preimage -const auto EMPTY_CONTRACT_LEAF = [] { return NewContractData{}.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{}.hash(); }; // hash of empty/0 preimage +const auto EMPTY_CONTRACT_LEAF = [] { return NewContractData{}.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() { diff --git a/circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp b/circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp index 30dd57971d4..8ed3c2d188f 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp @@ -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); @@ -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 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(contract_tree_store, CONTRACT_TREE_HEIGHT); expected_end_contracts_snapshot_tree.update_element(0, new_contract.hash()); AppendOnlyTreeSnapshot const expected_end_contracts_snapshot = { @@ -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)); @@ -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 const expected_end_contracts_snapshot = { @@ -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 const expected_start_commitments_snapshot = { .root = private_data_tree.root(), .next_available_leaf_index = 0, @@ -631,7 +640,8 @@ TEST_F(base_rollup_tests, native_compute_membership_historic_private_data_negati std::array, 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); @@ -658,7 +668,9 @@ TEST_F(base_rollup_tests, native_compute_membership_historic_contract_tree_negat std::array, 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); @@ -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{ .leaf_index = fr(1), @@ -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{ .leaf_index = fr(1), @@ -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, 2> kernel_data = { get_empty_kernel(), get_empty_kernel() }; @@ -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{ .leaf_index = fr(1), diff --git a/circuits/cpp/src/aztec3/circuits/rollup/base/init.hpp b/circuits/cpp/src/aztec3/circuits/rollup/base/init.hpp index ab57158c43c..ad75c10258b 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/base/init.hpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/base/init.hpp @@ -32,10 +32,10 @@ using Aggregator = aztec3::circuits::recursion::Aggregator; using AggregationObject = utils::types::NativeTypes::AggregationObject; using AppendOnlySnapshot = abis::AppendOnlyTreeSnapshot; -// Nullifier Tree Alias -using MerkleTree = stdlib::merkle_tree::MemoryTree; +// Tree Aliases +using MemoryStore = stdlib::merkle_tree::MemoryStore; +using MerkleTree = stdlib::merkle_tree::MerkleTree; using NullifierTree = stdlib::merkle_tree::NullifierMemoryTree; using NullifierLeafPreimage = abis::NullifierLeafPreimage; -using SparseTree = stdlib::merkle_tree::MerkleTree; } // namespace aztec3::circuits::rollup::native_base_rollup \ No newline at end of file diff --git a/circuits/cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.cpp b/circuits/cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.cpp index 27f857499ae..0fbdbc240b2 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.cpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.cpp @@ -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(); } @@ -84,7 +85,9 @@ std::vector calculate_contract_leaves(BaseRollupInputs const& baseRollup NT::fr calculate_contract_subtree(std::vector 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 @@ -96,7 +99,9 @@ NT::fr calculate_contract_subtree(std::vector 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; @@ -191,7 +196,8 @@ NT::fr create_nullifier_subtree( std::array 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()); diff --git a/circuits/cpp/src/aztec3/circuits/rollup/components/components.cpp b/circuits/cpp/src/aztec3/circuits/rollup/components/components.cpp index da3b4c2149b..54ab4c1b3ea 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/components/components.cpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/components/components.cpp @@ -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(); } diff --git a/circuits/cpp/src/aztec3/circuits/rollup/root/.test.cpp b/circuits/cpp/src/aztec3/circuits/rollup/root/.test.cpp index 678bed199ce..f1dbb5f5b08 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/root/.test.cpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/root/.test.cpp @@ -49,7 +49,8 @@ using aztec3::circuits::rollup::native_root_rollup::RootRollupPublicInputs; using aztec3::circuits::abis::NewContractData; -using MemoryTree = stdlib::merkle_tree::MemoryTree; +using MemoryStore = stdlib::merkle_tree::MemoryStore; +using MerkleTree = stdlib::merkle_tree::MerkleTree; using KernelData = aztec3::circuits::abis::PreviousKernelData; } // namespace @@ -164,15 +165,28 @@ TEST_F(root_rollup_tests, native_root_missing_nullifier_logic) utils::DummyCircuitBuilder builder = utils::DummyCircuitBuilder("root_rollup_tests__native_root_missing_nullifier_logic"); - MemoryTree data_tree = MemoryTree(PRIVATE_DATA_TREE_HEIGHT); - MemoryTree contract_tree = MemoryTree(CONTRACT_TREE_HEIGHT); - MemoryTree historic_data_tree = MemoryTree(PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT); - MemoryTree historic_contract_tree = MemoryTree(CONTRACT_TREE_ROOTS_TREE_HEIGHT); - MemoryTree l1_to_l2_messages_tree = MemoryTree(L1_TO_L2_MSG_TREE_HEIGHT); - MemoryTree historic_l1_to_l2_tree = MemoryTree(L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT); + MemoryStore private_data_tree_store; + MerkleTree private_data_tree = MerkleTree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT); + + MemoryStore contract_tree_store; + MerkleTree contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_HEIGHT); + + MemoryStore historic_private_data_tree_store; + MerkleTree historic_private_data_tree = + MerkleTree(historic_private_data_tree_store, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT); + + MemoryStore historic_contract_tree_store; + MerkleTree historic_contract_tree = MerkleTree(historic_contract_tree_store, CONTRACT_TREE_ROOTS_TREE_HEIGHT); + + MemoryStore l1_to_l2_messages_tree_store; + MerkleTree l1_to_l2_messages_tree = MerkleTree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT); + + MemoryStore historic_l1_to_l2_messages_tree_store; + MerkleTree historic_l1_to_l2_tree = + MerkleTree(historic_l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT); // Historic trees are initialised with an empty root at position 0. - historic_data_tree.update_element(0, data_tree.root()); + historic_private_data_tree.update_element(0, private_data_tree.root()); historic_contract_tree.update_element(0, contract_tree.root()); historic_l1_to_l2_tree.update_element(0, l1_to_l2_messages_tree.root()); @@ -187,7 +201,7 @@ TEST_F(root_rollup_tests, native_root_missing_nullifier_logic) for (uint8_t commitment_k = 0; commitment_k < MAX_NEW_COMMITMENTS_PER_TX; commitment_k++) { auto val = fr(kernel_j * MAX_NEW_COMMITMENTS_PER_TX + commitment_k + 1); new_commitments[commitment_k] = val; - data_tree.update_element(kernel_j * MAX_NEW_COMMITMENTS_PER_TX + commitment_k, val); + private_data_tree.update_element(kernel_j * MAX_NEW_COMMITMENTS_PER_TX + commitment_k, val); } kernels[kernel_j].public_inputs.end.new_commitments = new_commitments; @@ -217,7 +231,7 @@ TEST_F(root_rollup_tests, native_root_missing_nullifier_logic) .next_available_leaf_index = 0 }; // The start historic data snapshots - AppendOnlyTreeSnapshot const start_historic_data_tree_snapshot = { .root = historic_data_tree.root(), + AppendOnlyTreeSnapshot const start_historic_data_tree_snapshot = { .root = historic_private_data_tree.root(), .next_available_leaf_index = 1 }; AppendOnlyTreeSnapshot const start_historic_contract_tree_snapshot = { .root = historic_contract_tree.root(), .next_available_leaf_index = 1 }; @@ -230,12 +244,12 @@ TEST_F(root_rollup_tests, native_root_missing_nullifier_logic) } // Insert the newest data root into the historic tree - historic_data_tree.update_element(1, data_tree.root()); + historic_private_data_tree.update_element(1, private_data_tree.root()); historic_contract_tree.update_element(1, contract_tree.root()); historic_l1_to_l2_tree.update_element(1, l1_to_l2_messages_tree.root()); // Compute the end snapshot - AppendOnlyTreeSnapshot const end_historic_data_tree_snapshot = { .root = historic_data_tree.root(), + AppendOnlyTreeSnapshot const end_historic_data_tree_snapshot = { .root = historic_private_data_tree.root(), .next_available_leaf_index = 2 }; AppendOnlyTreeSnapshot const end_historic_contract_tree_snapshot = { .root = historic_contract_tree.root(), .next_available_leaf_index = 2 }; @@ -256,7 +270,7 @@ TEST_F(root_rollup_tests, native_root_missing_nullifier_logic) ASSERT_EQ( outputs.end_private_data_tree_snapshot, rootRollupInputs.previous_rollup_data[1].base_or_merge_rollup_public_inputs.end_private_data_tree_snapshot); - AppendOnlyTreeSnapshot const expected_private_data_tree_snapshot = { .root = data_tree.root(), + AppendOnlyTreeSnapshot const expected_private_data_tree_snapshot = { .root = private_data_tree.root(), .next_available_leaf_index = 4 * MAX_NEW_COMMITMENTS_PER_TX }; ASSERT_EQ(outputs.end_private_data_tree_snapshot, expected_private_data_tree_snapshot); diff --git a/circuits/cpp/src/aztec3/circuits/rollup/root/init.hpp b/circuits/cpp/src/aztec3/circuits/rollup/root/init.hpp index 5b4428733f3..f24e7c719f3 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/root/init.hpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/root/init.hpp @@ -25,6 +25,7 @@ using RootRollupPublicInputs = abis::RootRollupPublicInputs; using Aggregator = aztec3::circuits::recursion::Aggregator; -using MerkleTree = stdlib::merkle_tree::MemoryTree; +using MemoryStore = stdlib::merkle_tree::MemoryStore; +using MerkleTree = stdlib::merkle_tree::MerkleTree; } // namespace aztec3::circuits::rollup::native_root_rollup diff --git a/circuits/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp b/circuits/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp index f6f7f9a2c6a..dc2802ebc0b 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp @@ -26,7 +26,8 @@ namespace aztec3::circuits::rollup::native_root_rollup { */ NT::fr calculate_subtree(std::array leaves) { - MerkleTree merkle_tree = MerkleTree(L1_TO_L2_MSG_SUBTREE_HEIGHT); + MemoryStore merkle_tree_store; + MerkleTree merkle_tree(merkle_tree_store, L1_TO_L2_MSG_SUBTREE_HEIGHT); for (size_t i = 0; i < NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP; i++) { merkle_tree.update_element(i, leaves[i]); diff --git a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/init.hpp b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/init.hpp index 1ebf7ace3e7..4d95d8b7c5d 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/init.hpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/init.hpp @@ -35,8 +35,9 @@ using AppendOnlySnapshot = abis::AppendOnlyTreeSnapshot; using NullifierLeafPreimage = aztec3::circuits::abis::NullifierLeafPreimage; -// Nullifier Tree Alias -using MerkleTree = stdlib::merkle_tree::MemoryTree; +// Tree Aliases +using MemoryStore = stdlib::merkle_tree::MemoryStore; +using MerkleTree = stdlib::merkle_tree::MerkleTree; using NullifierTree = stdlib::merkle_tree::NullifierMemoryTree; using NullifierLeaf = stdlib::merkle_tree::nullifier_leaf; diff --git a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/nullifier_tree_testing_harness.hpp b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/nullifier_tree_testing_harness.hpp index 9b4a39512f5..fc8384a3622 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/nullifier_tree_testing_harness.hpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/nullifier_tree_testing_harness.hpp @@ -55,4 +55,4 @@ class NullifierMemoryTreeTestingHarness : public proof_system::plonk::stdlib::me using MemoryTree::root_; using MemoryTree::total_size_; using NullifierMemoryTree::leaves_; -}; +}; \ No newline at end of file diff --git a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.cpp b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.cpp index c9f4821122f..8f0d71efe57 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.cpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.cpp @@ -30,11 +30,10 @@ using KernelData = aztec3::circuits::abis::PreviousKernelData; using NullifierLeafPreimage = aztec3::circuits::abis::NullifierLeafPreimage; -using MerkleTree = stdlib::merkle_tree::MemoryTree; +using MemoryStore = stdlib::merkle_tree::MemoryStore; +using MerkleTree = stdlib::merkle_tree::MerkleTree; using NullifierTree = stdlib::merkle_tree::NullifierMemoryTree; using NullifierLeaf = stdlib::merkle_tree::nullifier_leaf; -using MemoryStore = stdlib::merkle_tree::MemoryStore; -using SparseTree = stdlib::merkle_tree::MerkleTree; using aztec3::circuits::abis::MembershipWitness; using MergeRollupInputs = aztec3::circuits::abis::MergeRollupInputs; @@ -78,19 +77,26 @@ std::array get_empty_l1_to_l2_messages( BaseRollupInputs base_rollup_inputs_from_kernels(std::array kernel_data, MerkleTree& private_data_tree, MerkleTree& contract_tree, - SparseTree& public_data_tree, + MerkleTree& public_data_tree, MerkleTree& l1_to_l2_msg_tree) { // @todo Look at the starting points for all of these. // By supporting as inputs we can make very generic tests, where it is trivial to try new setups. - MerkleTree historic_private_data_tree = MerkleTree(PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT); - MerkleTree historic_contract_tree = MerkleTree(CONTRACT_TREE_ROOTS_TREE_HEIGHT); - MerkleTree historic_l1_to_l2_msg_tree = MerkleTree(L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT); + MemoryStore historic_data_tree_store; + MerkleTree historic_private_data_tree = MerkleTree(historic_data_tree_store, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT); + + MemoryStore historic_contract_tree_store; + MerkleTree historic_contract_tree = MerkleTree(historic_contract_tree_store, CONTRACT_TREE_ROOTS_TREE_HEIGHT); + + MemoryStore historic_l1_to_l2_msg_tree_store; + MerkleTree historic_l1_to_l2_msg_tree = + MerkleTree(historic_l1_to_l2_msg_tree_store, L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT); // Historic trees are initialised with an empty root at position 0. historic_private_data_tree.update_element(0, private_data_tree.root()); historic_contract_tree.update_element(0, contract_tree.root()); - historic_l1_to_l2_msg_tree.update_element(0, MerkleTree(L1_TO_L2_MSG_TREE_HEIGHT).root()); + MemoryStore tmp_store; + historic_l1_to_l2_msg_tree.update_element(0, MerkleTree(tmp_store, L1_TO_L2_MSG_TREE_HEIGHT).root()); ConstantRollupData const constantRollupData = { .start_tree_of_historic_private_data_tree_roots_snapshot = { @@ -232,12 +238,15 @@ BaseRollupInputs base_rollup_inputs_from_kernels(std::array kerne BaseRollupInputs base_rollup_inputs_from_kernels(std::array kernel_data) { - MerkleTree private_data_tree = MerkleTree(PRIVATE_DATA_TREE_HEIGHT); - MerkleTree contract_tree = MerkleTree(CONTRACT_TREE_HEIGHT); - MerkleTree l1_to_l2_messages_tree = MerkleTree(L1_TO_L2_MSG_TREE_HEIGHT); + MemoryStore private_data_store; + MerkleTree private_data_tree = MerkleTree(private_data_store, PRIVATE_DATA_TREE_HEIGHT); + MemoryStore contract_tree_store; + MerkleTree contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_HEIGHT); + MemoryStore l1_to_l2_messages_store; + MerkleTree l1_to_l2_messages_tree = MerkleTree(l1_to_l2_messages_store, L1_TO_L2_MSG_TREE_HEIGHT); MemoryStore public_data_tree_store; - SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT); + MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT); return base_rollup_inputs_from_kernels( std::move(kernel_data), private_data_tree, contract_tree, public_data_tree, l1_to_l2_messages_tree); @@ -252,8 +261,10 @@ std::array, 2> get_previous_rollup_data(DummyBuilder& bui aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(builder, base_rollup_input_1); // Build the trees based on inputs in base_rollup_input_1. - MerkleTree private_data_tree = MerkleTree(PRIVATE_DATA_TREE_HEIGHT); - MerkleTree contract_tree = MerkleTree(CONTRACT_TREE_HEIGHT); + MemoryStore private_data_store; + MerkleTree private_data_tree = MerkleTree(private_data_store, PRIVATE_DATA_TREE_HEIGHT); + MemoryStore contract_tree_store; + MerkleTree contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_HEIGHT); std::vector initial_values(2 * MAX_NEW_NULLIFIERS_PER_TX - 1); for (size_t i = 0; i < initial_values.size(); i++) { @@ -321,13 +332,25 @@ RootRollupInputs get_root_rollup_inputs(utils::DummyBuilder& builder, std::array kernel_data, std::array l1_to_l2_messages) { - MerkleTree historic_private_data_tree = MerkleTree(PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT); - MerkleTree historic_contract_tree = MerkleTree(CONTRACT_TREE_ROOTS_TREE_HEIGHT); - MerkleTree historic_l1_to_l2_msg_tree = MerkleTree(L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT); + MemoryStore historic_private_data_store; + MerkleTree historic_private_data_tree(historic_private_data_store, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT); + + MemoryStore historic_contract_tree_store; + MerkleTree historic_contract_tree(historic_contract_tree_store, CONTRACT_TREE_ROOTS_TREE_HEIGHT); + + MemoryStore historic_l1_to_l2_msg_tree_store; + MerkleTree historic_l1_to_l2_msg_tree(historic_l1_to_l2_msg_tree_store, L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT); + + + MemoryStore private_data_store; + const MerkleTree private_data_tree(private_data_store, PRIVATE_DATA_TREE_HEIGHT); + + MemoryStore contract_tree_store; + const MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT); + + MemoryStore l1_to_l2_msg_tree_store; + MerkleTree l1_to_l2_msg_tree(l1_to_l2_msg_tree_store, L1_TO_L2_MSG_TREE_HEIGHT); - MerkleTree const private_data_tree = MerkleTree(PRIVATE_DATA_TREE_HEIGHT); - MerkleTree const contract_tree = MerkleTree(CONTRACT_TREE_HEIGHT); - MerkleTree l1_to_l2_msg_tree = MerkleTree(L1_TO_L2_MSG_TREE_HEIGHT); // Historic trees are initialised with an empty root at position 0. historic_private_data_tree.update_element(0, private_data_tree.root()); @@ -430,10 +453,7 @@ nullifier_tree_testing_values generate_nullifier_tree_testing_values_explicit( NullifierMemoryTreeTestingHarness nullifier_tree = get_initial_nullifier_tree(initial_values); NullifierMemoryTreeTestingHarness reference_tree = get_initial_nullifier_tree(initial_values); - AppendOnlyTreeSnapshot const nullifier_tree_start_snapshot = { - .root = nullifier_tree.root(), - .next_available_leaf_index = static_cast(start_tree_size), - }; + AppendOnlyTreeSnapshot const nullifier_tree_start_snapshot = nullifier_tree.get_snapshot(); const size_t NUMBER_OF_NULLIFIERS = MAX_NEW_NULLIFIERS_PER_TX * 2; std::array new_nullifier_leaves{}; @@ -488,10 +508,7 @@ nullifier_tree_testing_values generate_nullifier_tree_testing_values_explicit( // Get expected root with subtrees inserted correctly // Expected end state - AppendOnlyTreeSnapshot const nullifier_tree_end_snapshot = { - .root = reference_tree.root(), - .next_available_leaf_index = uint32_t(reference_tree.size()), - }; + AppendOnlyTreeSnapshot const nullifier_tree_end_snapshot = reference_tree.get_snapshot(); std::vector sibling_path = reference_tree.get_sibling_path(start_tree_size); std::array sibling_path_array; diff --git a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.hpp b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.hpp index 14fcd2461e5..4b9a314c938 100644 --- a/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.hpp +++ b/circuits/cpp/src/aztec3/circuits/rollup/test_utils/utils.hpp @@ -30,13 +30,13 @@ using AppendOnlyTreeSnapshot = aztec3::circuits::abis::AppendOnlyTreeSnapshot; -// Nullifier Tree Alias -using MerkleTree = stdlib::merkle_tree::MemoryTree; +// Tree Aliases +using MemoryStore = stdlib::merkle_tree::MemoryStore; +using MerkleTree = stdlib::merkle_tree::MerkleTree; using NullifierTree = stdlib::merkle_tree::NullifierMemoryTree; using NullifierLeaf = stdlib::merkle_tree::nullifier_leaf; + using KernelData = aztec3::circuits::abis::PreviousKernelData; -using MemoryStore = stdlib::merkle_tree::MemoryStore; -using SparseTree = stdlib::merkle_tree::MerkleTree; using aztec3::circuits::abis::MembershipWitness; using aztec3::circuits::abis::PreviousRollupData; @@ -49,11 +49,11 @@ BaseRollupInputs base_rollup_inputs_from_kernels(std::array kerne BaseRollupInputs base_rollup_inputs_from_kernels(std::array kernel_data, MerkleTree& private_data_tree, MerkleTree& contract_tree, - SparseTree& public_data_tree, + MerkleTree& public_data_tree, MerkleTree& l1_to_l2_msg_tree); -template std::array get_sibling_path(SparseTree& tree, uint256_t leafIndex) +template std::array get_sibling_path(MerkleTree& tree, uint256_t leafIndex) { std::array siblingPath; auto path = tree.get_hash_path(leafIndex); diff --git a/circuits/cpp/src/aztec3/constants.hpp b/circuits/cpp/src/aztec3/constants.hpp index 113fc12d480..a4e086e8217 100644 --- a/circuits/cpp/src/aztec3/constants.hpp +++ b/circuits/cpp/src/aztec3/constants.hpp @@ -70,14 +70,14 @@ constexpr size_t KERNELS_PER_ROLLUP = 2; // TREES RELATED CONSTANTS constexpr size_t VK_TREE_HEIGHT = 3; constexpr size_t FUNCTION_TREE_HEIGHT = 4; -constexpr size_t CONTRACT_TREE_HEIGHT = 8; -constexpr size_t PRIVATE_DATA_TREE_HEIGHT = 16; +constexpr size_t CONTRACT_TREE_HEIGHT = 16; +constexpr size_t PRIVATE_DATA_TREE_HEIGHT = 32; constexpr size_t PUBLIC_DATA_TREE_HEIGHT = 254; constexpr size_t NULLIFIER_TREE_HEIGHT = 16; -constexpr size_t L1_TO_L2_MSG_TREE_HEIGHT = 8; -constexpr size_t PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT = 8; -constexpr size_t CONTRACT_TREE_ROOTS_TREE_HEIGHT = 8; -constexpr size_t L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT = 8; +constexpr size_t L1_TO_L2_MSG_TREE_HEIGHT = 16; +constexpr size_t PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT = 16; +constexpr size_t CONTRACT_TREE_ROOTS_TREE_HEIGHT = 16; +constexpr size_t L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT = 16; constexpr size_t ROLLUP_VK_TREE_HEIGHT = 8; // TODO: update