Skip to content

Commit

Permalink
feat: l1 to l2 messaging (#385)
Browse files Browse the repository at this point in the history
* feat: initial decoder contact changes

* feat: add message snapshot to l2 block

* feat(world_state): add l2 merkle tree

* feat: add l1ToL2 roots tree to ts

* feat: historic l2 tree in l2 block

* feat: add l1->l2 messages types to root rollup

* feat: add to circuit powered block builder

* feat: add types to root rollup inputs

* feat: update root rollup circuit

* feat: add l1tol2 messages to the rollup calldata

* soldoc

* fix: pedersen segfault issue

temp commented out

fix: pedersen segfault issue

* clean: fix native root tests

* Add messages hash test

* feat: stub l1_to_l2_messages in block builder

* fix: update snaphshots in circuits.js

* fix: activate sim tree checking in rollup builder

* fix: update snapshot

* feat: add l2 block utilities

* fix: rebase cpp build issues

* feat: update l1 contract after rebase

* fix: update ts tests

* chore: update viem scripts

* chore: update ethjs helpers

* fix

* feat: update decoder.t.sol

* fix: re skip `l2-block-publisher.test.ts`

* chore: clang tidy fix

* fix: clang tidy

* run prettier

* fix: remove done todos

* fix: re introduce test initialiser lost in merge

* fix: move custom typing to its own file in the sequencer

* fix: add negative tests, cleanup

* chore: forge fmt

* fix: rename updateRootsTree -> updateHistoricRootsTree

* fix: update contract doc comment

* fix: update cpp test and naming nits

* fix: update trailing mock

---------

Co-authored-by: cheethas <[email protected]>
  • Loading branch information
Maddiaa0 and cheethas authored May 3, 2023
1 parent d1daa26 commit 6730a93
Show file tree
Hide file tree
Showing 37 changed files with 968 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ template <typename NCT> struct RootRollupInputs {
std::array<fr, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT> new_historic_private_data_tree_root_sibling_path;
std::array<fr, CONTRACT_TREE_ROOTS_TREE_HEIGHT> new_historic_contract_tree_root_sibling_path;

// inputs required to process l1 to l2 messages
std::array<fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP> l1_to_l2_messages;
std::array<fr, L1_TO_L2_MSG_SUBTREE_INCLUSION_CHECK_DEPTH> new_l1_to_l2_message_tree_root_sibling_path;
std::array<fr, L1_TO_L2_MSG_TREE_ROOTS_TREE_HEIGHT> new_historic_l1_to_l2_message_roots_tree_sibling_path;

AppendOnlyTreeSnapshot<NCT> start_l1_to_l2_message_tree_snapshot;
AppendOnlyTreeSnapshot<NCT> start_historic_tree_l1_to_l2_message_tree_roots_snapshot;

bool operator==(RootRollupInputs<NCT> const&) const = default;
};

Expand All @@ -37,6 +45,11 @@ template <typename NCT> void read(uint8_t const*& it, RootRollupInputs<NCT>& obj
read(it, obj.previous_rollup_data);
read(it, obj.new_historic_private_data_tree_root_sibling_path);
read(it, obj.new_historic_contract_tree_root_sibling_path);
read(it, obj.l1_to_l2_messages);
read(it, obj.new_l1_to_l2_message_tree_root_sibling_path);
read(it, obj.new_historic_l1_to_l2_message_roots_tree_sibling_path);
read(it, obj.start_l1_to_l2_message_tree_snapshot);
read(it, obj.start_historic_tree_l1_to_l2_message_tree_roots_snapshot);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, RootRollupInputs<NCT> const& obj)
Expand All @@ -46,14 +59,27 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, RootRollupInputs<N
write(buf, obj.previous_rollup_data);
write(buf, obj.new_historic_private_data_tree_root_sibling_path);
write(buf, obj.new_historic_contract_tree_root_sibling_path);
write(buf, obj.l1_to_l2_messages);
write(buf, obj.new_l1_to_l2_message_tree_root_sibling_path);
write(buf, obj.new_historic_l1_to_l2_message_roots_tree_sibling_path);
write(buf, obj.start_l1_to_l2_message_tree_snapshot);
write(buf, obj.start_historic_tree_l1_to_l2_message_tree_roots_snapshot);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, RootRollupInputs<NCT> const& obj)
{
return os << "previous_rollup_data: " << obj.previous_rollup_data << "\n"
<< "new_historic_private_data_tree_roots: " << obj.new_historic_private_data_tree_root_sibling_path
<< "\n"
<< "new_historic_contract_tree_roots: " << obj.new_historic_contract_tree_root_sibling_path << "\n";
<< "new_historic_contract_tree_roots: " << obj.new_historic_contract_tree_root_sibling_path << "\n"
<< "new_l1_to_l2_messages: " << obj.l1_to_l2_messages << "\n"
<< "new_l1_to_l2_message_tree_root_sibling_path: " << obj.new_l1_to_l2_message_tree_root_sibling_path
<< "\n"
<< "new_historic_l1_to_l2_message_roots_tree_sibling_path: "
<< obj.new_historic_l1_to_l2_message_roots_tree_sibling_path << "\n"
<< "start_l1_to_l2_message_tree_snapshot: " << obj.start_l1_to_l2_message_tree_snapshot << "\n"
<< "start_historic_tree_l1_to_l2_message_tree_roots_snapshot: "
<< obj.start_historic_tree_l1_to_l2_message_tree_roots_snapshot << "\n";
}

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace aztec3::circuits::abis {
using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;


template <typename NCT> struct RootRollupPublicInputs {
using fr = typename NCT::fr;
using AggregationObject = typename NCT::AggregationObject;
Expand All @@ -40,7 +41,14 @@ template <typename NCT> struct RootRollupPublicInputs {
AppendOnlyTreeSnapshot<NCT> start_tree_of_historic_contract_tree_roots_snapshot;
AppendOnlyTreeSnapshot<NCT> end_tree_of_historic_contract_tree_roots_snapshot;

AppendOnlyTreeSnapshot<NCT> start_l1_to_l2_messages_tree_snapshot;
AppendOnlyTreeSnapshot<NCT> end_l1_to_l2_messages_tree_snapshot;

AppendOnlyTreeSnapshot<NCT> start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot;
AppendOnlyTreeSnapshot<NCT> end_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot;

std::array<fr, 2> calldata_hash;
std::array<fr, 2> l1_to_l2_messages_hash;

bool operator==(RootRollupPublicInputs<NCT> const&) const = default;

Expand All @@ -53,11 +61,15 @@ template <typename NCT> struct RootRollupPublicInputs {
write(buf, start_contract_tree_snapshot);
write(buf, start_tree_of_historic_private_data_tree_roots_snapshot);
write(buf, start_tree_of_historic_contract_tree_roots_snapshot);
write(buf, start_l1_to_l2_messages_tree_snapshot);
write(buf, start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
write(buf, end_private_data_tree_snapshot);
write(buf, end_nullifier_tree_snapshot);
write(buf, end_contract_tree_snapshot);
write(buf, end_tree_of_historic_private_data_tree_roots_snapshot);
write(buf, end_tree_of_historic_contract_tree_roots_snapshot);
write(buf, end_l1_to_l2_messages_tree_snapshot);
write(buf, end_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);

// Stitching calldata hash together
auto high_buffer = calldata_hash[0].to_buffer();
Expand All @@ -70,6 +82,17 @@ template <typename NCT> struct RootRollupPublicInputs {
buf.push_back(low_buffer[16 + i]);
}

// Stitch l1_to_l2_messages_hash
auto high_buffer_m = l1_to_l2_messages_hash[0].to_buffer();
auto low_buffer_m = l1_to_l2_messages_hash[1].to_buffer();

for (uint8_t i = 0; i < 16; i++) {
buf.push_back(high_buffer_m[16 + i]);
}
for (uint8_t i = 0; i < 16; i++) {
buf.push_back(low_buffer_m[16 + i]);
}

return sha256::sha256_to_field(buf);
}
};
Expand All @@ -91,7 +114,12 @@ template <typename NCT> void read(uint8_t const*& it, RootRollupPublicInputs<NCT
read(it, obj.end_tree_of_historic_private_data_tree_roots_snapshot);
read(it, obj.start_tree_of_historic_contract_tree_roots_snapshot);
read(it, obj.end_tree_of_historic_contract_tree_roots_snapshot);
read(it, obj.start_l1_to_l2_messages_tree_snapshot);
read(it, obj.end_l1_to_l2_messages_tree_snapshot);
read(it, obj.start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
read(it, obj.end_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
read(it, obj.calldata_hash);
read(it, obj.l1_to_l2_messages_hash);
};

template <typename NCT> void write(std::vector<uint8_t>& buf, RootRollupPublicInputs<NCT> const& obj)
Expand All @@ -111,7 +139,12 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, RootRollupPublicIn
write(buf, obj.end_tree_of_historic_private_data_tree_roots_snapshot);
write(buf, obj.start_tree_of_historic_contract_tree_roots_snapshot);
write(buf, obj.end_tree_of_historic_contract_tree_roots_snapshot);
write(buf, obj.start_l1_to_l2_messages_tree_snapshot);
write(buf, obj.end_l1_to_l2_messages_tree_snapshot);
write(buf, obj.start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
write(buf, obj.end_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
write(buf, obj.calldata_hash);
write(buf, obj.l1_to_l2_messages_hash);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, RootRollupPublicInputs<NCT> const& obj)
Expand All @@ -133,7 +166,15 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, RootRollupPub
<< obj.start_tree_of_historic_contract_tree_roots_snapshot << "\n"
<< "end_tree_of_historic_contract_tree_roots_snapshot: "
<< obj.end_tree_of_historic_contract_tree_roots_snapshot << "\n"
<< "calldata_hash: " << obj.calldata_hash << "\n";
<< "start_l1_to_l2_messages_tree_snapshot: " << obj.start_l1_to_l2_messages_tree_snapshot << "\n"
<< "end_l1_tol2_messages_tree_snapshot: " << obj.end_l1_to_l2_messages_tree_snapshot << "\n"
<< "start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot: "
<< obj.start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot << "\n"
<< "end_tree_of_historic_l1_tol2_messages_tree_roots_snapshot: "
<< obj.end_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot << "\n"
<< "calldata_hash: " << obj.calldata_hash << "\n"
<< "l1_to_l2_messages_hash: " << obj.l1_to_l2_messages_hash << "\n";
;
};

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ AppendOnlySnapshot check_nullifier_tree_non_membership_and_insert_to_tree(DummyC
}

// Check that the new subtree is to be inserted at the next location, and is empty currently
const auto empty_nullifier_subtree_root = calculate_empty_tree_root(NULLIFIER_SUBTREE_DEPTH);
const auto empty_nullifier_subtree_root = components::calculate_empty_tree_root(NULLIFIER_SUBTREE_DEPTH);
auto leafIndexNullifierSubtreeDepth =
baseRollupInputs.start_nullifier_tree_snapshot.next_available_leaf_index >> NULLIFIER_SUBTREE_DEPTH;
check_membership<NT>(composer,
Expand Down Expand Up @@ -541,7 +541,7 @@ BaseOrMergeRollupPublicInputs base_rollup_circuit(DummyComposer& composer, BaseR
NT::fr const commitments_tree_subroot = calculate_commitments_subtree(composer, baseRollupInputs);

// Insert commitment subtrees:
const auto empty_commitments_subtree_root = calculate_empty_tree_root(PRIVATE_DATA_SUBTREE_DEPTH);
const auto empty_commitments_subtree_root = components::calculate_empty_tree_root(PRIVATE_DATA_SUBTREE_DEPTH);
auto end_private_data_tree_snapshot =
components::insert_subtree_to_snapshot_tree(composer,
baseRollupInputs.start_private_data_tree_snapshot,
Expand All @@ -552,7 +552,7 @@ BaseOrMergeRollupPublicInputs base_rollup_circuit(DummyComposer& composer, BaseR
"empty commitment subtree membership check");

// Insert contract subtrees:
const auto empty_contracts_subtree_root = calculate_empty_tree_root(CONTRACT_SUBTREE_DEPTH);
const auto empty_contracts_subtree_root = components::calculate_empty_tree_root(CONTRACT_SUBTREE_DEPTH);
auto end_contract_tree_snapshot =
components::insert_subtree_to_snapshot_tree(composer,
baseRollupInputs.start_contract_tree_snapshot,
Expand Down
13 changes: 13 additions & 0 deletions circuits/cpp/src/aztec3/circuits/rollup/components/components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "barretenberg/crypto/sha256/sha256.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/stdlib/hash/pedersen/pedersen.hpp"
#include "barretenberg/stdlib/merkle_tree/memory_tree.hpp"

#include <algorithm>
#include <array>
Expand All @@ -18,6 +19,18 @@

namespace aztec3::circuits::rollup::components {

/**
* @brief Get the root of an empty tree of a given depth
*
* @param depth
* @return NT::fr
*/
NT::fr calculate_empty_tree_root(const size_t depth)
{
stdlib::merkle_tree::MemoryTree const empty_tree = stdlib::merkle_tree::MemoryTree(depth);
return empty_tree.root();
}

/**
* @brief Create an aggregation object for the proofs that are provided
* - We add points P0 for each of our proofs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using aztec3::circuits::check_membership;
using aztec3::circuits::root_from_sibling_path;

namespace aztec3::circuits::rollup::components {
NT::fr calculate_empty_tree_root(size_t depth);
std::array<fr, 2> compute_calldata_hash(std::array<abis::PreviousRollupData<NT>, 2> previous_rollup_data);
void assert_prev_rollups_follow_on_from_each_other(DummyComposer& composer,
BaseOrMergeRollupPublicInputs const& left,
Expand Down
Loading

0 comments on commit 6730a93

Please sign in to comment.