Skip to content

Commit 68b0fd8

Browse files
authored
Updating tree heights (#975)
1 parent 72989cf commit 68b0fd8

File tree

16 files changed

+181
-100
lines changed

16 files changed

+181
-100
lines changed

circuits/cpp/barretenberg

Submodule barretenberg updated 506 files

circuits/cpp/src/aztec3/circuits/hash.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ using abis::FunctionData;
1818
using abis::Point;
1919
using aztec3::circuits::abis::ContractLeafPreimage;
2020
using aztec3::circuits::abis::FunctionLeafPreimage;
21-
using MerkleTree = stdlib::merkle_tree::MemoryTree;
21+
using MemoryStore = stdlib::merkle_tree::MemoryStore;
22+
using MerkleTree = stdlib::merkle_tree::MerkleTree<MemoryStore>;
2223

2324
template <typename NCT> typename NCT::fr compute_var_args_hash(std::vector<typename NCT::fr> args)
2425
{

circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ get_random_reads(NT::fr const& contract_address, int const num_read_requests)
8282
// set -> vector without collisions
8383
std::vector<NT::uint32> rr_leaf_indices(rr_leaf_indices_set.begin(), rr_leaf_indices_set.end());
8484

85-
MerkleTree private_data_tree = MerkleTree(PRIVATE_DATA_TREE_HEIGHT);
85+
MemoryStore private_data_tree_store;
86+
MerkleTree private_data_tree = MerkleTree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
8687

8788
// add the commitments to the private data tree for each read request
8889
// add them at their corresponding index in the tree

circuits/cpp/src/aztec3/circuits/kernel/private/testing_harness.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <barretenberg/barretenberg.hpp>
1616

1717
#include <array>
18+
#include <cstdint>
1819

1920
namespace {
2021

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

5455
inline const auto& get_empty_function_siblings()
5556
{

circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ TEST_F(base_rollup_tests, native_no_new_contract_leafs)
135135
// No contract leaves -> will insert empty tree -> i.e. end_contract_tree_root = start_contract_tree_root
136136

137137
BaseRollupInputs emptyInputs = base_rollup_inputs_from_kernels({ get_empty_kernel(), get_empty_kernel() });
138-
auto empty_contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);
138+
MemoryStore contract_tree_store;
139+
auto empty_contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_HEIGHT);
139140

140141
BaseOrMergeRollupPublicInputs outputs =
141142
aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(builder, emptyInputs);
@@ -168,14 +169,17 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted)
168169
.function_tree_root = fr(2),
169170
};
170171

171-
auto empty_contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);
172+
MemoryStore empty_contract_tree_store;
173+
auto empty_contract_tree = MerkleTree(empty_contract_tree_store, CONTRACT_TREE_HEIGHT);
172174
AppendOnlyTreeSnapshot<NT> const expected_start_contracts_snapshot = {
173175
.root = empty_contract_tree.root(),
174176
.next_available_leaf_index = 0,
175177
};
176178

177179
// create expected end contract tree snapshot
178-
auto expected_end_contracts_snapshot_tree = stdlib::merkle_tree::MemoryTree(CONTRACT_TREE_HEIGHT);
180+
MemoryStore contract_tree_store;
181+
auto expected_end_contracts_snapshot_tree =
182+
stdlib::merkle_tree::MerkleTree<MemoryStore>(contract_tree_store, CONTRACT_TREE_HEIGHT);
179183
expected_end_contracts_snapshot_tree.update_element(0, new_contract.hash());
180184

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

215-
auto start_contract_tree_snapshot = native_base_rollup::MerkleTree(CONTRACT_TREE_HEIGHT);
219+
MemoryStore start_contract_tree_snapshot_store;
220+
auto start_contract_tree_snapshot = MerkleTree(start_contract_tree_snapshot_store, CONTRACT_TREE_HEIGHT);
216221
// insert 12 leaves to the tree (next available leaf index is 12)
217222
for (size_t i = 0; i < 12; ++i) {
218223
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
232237
auto expected_contract_leaf = crypto::pedersen_commitment::compress_native(
233238
{ new_contract.contract_address, new_contract.portal_contract_address, new_contract.function_tree_root },
234239
GeneratorIndex::CONTRACT_LEAF);
235-
auto expected_end_contracts_snapshot_tree = start_contract_tree_snapshot;
240+
241+
auto expected_end_contract_tree_snapshot_store = start_contract_tree_snapshot_store;
242+
auto expected_end_contracts_snapshot_tree =
243+
MerkleTree(expected_end_contract_tree_snapshot_store, CONTRACT_TREE_HEIGHT);
236244
expected_end_contracts_snapshot_tree.update_element(12, expected_contract_leaf);
237245

238246
AppendOnlyTreeSnapshot<NT> const expected_end_contracts_snapshot = {
@@ -265,7 +273,8 @@ TEST_F(base_rollup_tests, native_new_commitments_tree)
265273
}
266274

267275
// get sibling path
268-
auto private_data_tree = native_base_rollup::MerkleTree(PRIVATE_DATA_TREE_HEIGHT);
276+
MemoryStore private_data_tree_store;
277+
auto private_data_tree = MerkleTree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
269278
AppendOnlyTreeSnapshot<NT> const expected_start_commitments_snapshot = {
270279
.root = private_data_tree.root(),
271280
.next_available_leaf_index = 0,
@@ -631,7 +640,8 @@ TEST_F(base_rollup_tests, native_compute_membership_historic_private_data_negati
631640
std::array<PreviousKernelData<NT>, 2> const kernel_data = { get_empty_kernel(), get_empty_kernel() };
632641
BaseRollupInputs inputs = base_rollup_inputs_from_kernels(kernel_data);
633642

634-
auto private_data_tree = native_base_rollup::MerkleTree(PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT);
643+
MemoryStore private_data_store;
644+
auto private_data_tree = MerkleTree(private_data_store, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT);
635645

636646
// Create an INCORRECT sibling path for the private data tree root in the historic tree roots.
637647
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
658668
std::array<PreviousKernelData<NT>, 2> const kernel_data = { get_empty_kernel(), get_empty_kernel() };
659669
BaseRollupInputs inputs = base_rollup_inputs_from_kernels(kernel_data);
660670

661-
auto contract_tree = native_base_rollup::MerkleTree(CONTRACT_TREE_ROOTS_TREE_HEIGHT);
671+
MemoryStore contract_tree_store;
672+
auto contract_tree = MerkleTree(contract_tree_store, CONTRACT_TREE_ROOTS_TREE_HEIGHT);
673+
662674

663675
// Create an INCORRECT sibling path for contract tree root in the historic tree roots.
664676
auto hash_path = contract_tree.get_sibling_path(0);
@@ -746,11 +758,17 @@ TEST_F(base_rollup_tests, native_cbind_0)
746758
TEST_F(base_rollup_tests, native_single_public_state_read)
747759
{
748760
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_single_public_state_read");
749-
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
750-
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
751-
stdlib::merkle_tree::MemoryStore public_data_tree_store;
752-
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
753-
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
761+
MemoryStore private_data_tree_store;
762+
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
763+
764+
MemoryStore contract_tree_store;
765+
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);
766+
767+
MemoryStore public_data_tree_store;
768+
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
769+
770+
MemoryStore l1_to_l2_messages_tree_store;
771+
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);
754772

755773
auto data_read = abis::PublicDataRead<NT>{
756774
.leaf_index = fr(1),
@@ -775,11 +793,18 @@ TEST_F(base_rollup_tests, native_single_public_state_read)
775793
TEST_F(base_rollup_tests, native_single_public_state_write)
776794
{
777795
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_single_public_state_write");
778-
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
779-
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
780-
stdlib::merkle_tree::MemoryStore public_data_tree_store;
781-
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
782-
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
796+
MemoryStore private_data_tree_store;
797+
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
798+
799+
MemoryStore contract_tree_store;
800+
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);
801+
802+
MemoryStore public_data_tree_store;
803+
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
804+
805+
MemoryStore l1_to_l2_messages_tree_store;
806+
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);
807+
783808

784809
auto data_write = abis::PublicDataUpdateRequest<NT>{
785810
.leaf_index = fr(1),
@@ -806,11 +831,17 @@ TEST_F(base_rollup_tests, native_single_public_state_write)
806831
TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
807832
{
808833
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_multiple_public_state_read_writes");
809-
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
810-
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
811-
stdlib::merkle_tree::MemoryStore public_data_tree_store;
812-
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
813-
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
834+
MemoryStore private_data_tree_store;
835+
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
836+
837+
MemoryStore contract_tree_store;
838+
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);
839+
840+
MemoryStore public_data_tree_store;
841+
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
842+
843+
MemoryStore l1_to_l2_messages_tree_store;
844+
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);
814845

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

@@ -847,11 +878,17 @@ TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
847878
TEST_F(base_rollup_tests, native_invalid_public_state_read)
848879
{
849880
DummyBuilder builder = DummyBuilder("base_rollup_tests__native_invalid_public_state_read");
850-
native_base_rollup::MerkleTree private_data_tree(PRIVATE_DATA_TREE_HEIGHT);
851-
native_base_rollup::MerkleTree contract_tree(CONTRACT_TREE_HEIGHT);
852-
stdlib::merkle_tree::MemoryStore public_data_tree_store;
853-
native_base_rollup::SparseTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
854-
native_base_rollup::MerkleTree l1_to_l2_messages_tree(L1_TO_L2_MSG_TREE_HEIGHT);
881+
MemoryStore private_data_tree_store;
882+
MerkleTree private_data_tree(private_data_tree_store, PRIVATE_DATA_TREE_HEIGHT);
883+
884+
MemoryStore contract_tree_store;
885+
MerkleTree contract_tree(contract_tree_store, CONTRACT_TREE_HEIGHT);
886+
887+
MemoryStore public_data_tree_store;
888+
MerkleTree public_data_tree(public_data_tree_store, PUBLIC_DATA_TREE_HEIGHT);
889+
890+
MemoryStore l1_to_l2_messages_tree_store;
891+
MerkleTree l1_to_l2_messages_tree(l1_to_l2_messages_tree_store, L1_TO_L2_MSG_TREE_HEIGHT);
855892

856893
auto data_read = abis::PublicDataRead<NT>{
857894
.leaf_index = fr(1),

circuits/cpp/src/aztec3/circuits/rollup/base/init.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ using Aggregator = aztec3::circuits::recursion::Aggregator;
3232
using AggregationObject = utils::types::NativeTypes::AggregationObject;
3333
using AppendOnlySnapshot = abis::AppendOnlyTreeSnapshot<NT>;
3434

35-
// Nullifier Tree Alias
36-
using MerkleTree = stdlib::merkle_tree::MemoryTree;
35+
// Tree Aliases
36+
using MemoryStore = stdlib::merkle_tree::MemoryStore;
37+
using MerkleTree = stdlib::merkle_tree::MerkleTree<MemoryStore>;
3738
using NullifierTree = stdlib::merkle_tree::NullifierMemoryTree;
3839
using NullifierLeafPreimage = abis::NullifierLeafPreimage<NT>;
39-
using SparseTree = stdlib::merkle_tree::MerkleTree<stdlib::merkle_tree::MemoryStore>;
4040

4141
} // namespace aztec3::circuits::rollup::native_base_rollup

circuits/cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ namespace aztec3::circuits::rollup::native_base_rollup {
2424

2525
NT::fr calculate_empty_tree_root(const size_t depth)
2626
{
27-
MerkleTree const empty_tree = MerkleTree(depth);
27+
MemoryStore empty_tree_store;
28+
MerkleTree const empty_tree = MerkleTree(empty_tree_store, depth);
2829
return empty_tree.root();
2930
}
3031

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

8586
NT::fr calculate_contract_subtree(std::vector<NT::fr> contract_leaves)
8687
{
87-
MerkleTree contracts_tree = MerkleTree(CONTRACT_SUBTREE_HEIGHT);
88+
MemoryStore contracts_tree_store;
89+
MerkleTree contracts_tree(contracts_tree_store, CONTRACT_SUBTREE_HEIGHT);
90+
8891

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

97100
NT::fr calculate_commitments_subtree(DummyBuilder& builder, BaseRollupInputs const& baseRollupInputs)
98101
{
99-
MerkleTree commitments_tree = MerkleTree(PRIVATE_DATA_SUBTREE_HEIGHT);
102+
MemoryStore commitments_tree_store;
103+
MerkleTree commitments_tree(commitments_tree_store, PRIVATE_DATA_SUBTREE_HEIGHT);
104+
100105

101106
for (size_t i = 0; i < 2; i++) {
102107
auto new_commitments = baseRollupInputs.kernel_data[i].public_inputs.end.new_commitments;
@@ -191,7 +196,8 @@ NT::fr create_nullifier_subtree(
191196
std::array<NullifierLeafPreimage, MAX_NEW_NULLIFIERS_PER_TX * 2> const& nullifier_leaves)
192197
{
193198
// Build a merkle tree of the nullifiers
194-
MerkleTree nullifier_subtree = MerkleTree(NULLIFIER_SUBTREE_HEIGHT);
199+
MemoryStore nullifier_subtree_store;
200+
MerkleTree nullifier_subtree(nullifier_subtree_store, NULLIFIER_SUBTREE_HEIGHT);
195201
for (size_t i = 0; i < nullifier_leaves.size(); i++) {
196202
// hash() checks if nullifier is empty (and if so returns 0)
197203
nullifier_subtree.update_element(i, nullifier_leaves[i].hash());

circuits/cpp/src/aztec3/circuits/rollup/components/components.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ namespace aztec3::circuits::rollup::components {
2424
*/
2525
NT::fr calculate_empty_tree_root(const size_t depth)
2626
{
27-
stdlib::merkle_tree::MemoryTree const empty_tree = stdlib::merkle_tree::MemoryTree(depth);
27+
MemoryStore empty_tree_store;
28+
MerkleTree const empty_tree = MerkleTree(empty_tree_store, depth);
2829
return empty_tree.root();
2930
}
3031

0 commit comments

Comments
 (0)