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

refactor: moving RootRollupInputs impl #5087

Merged
merged 2 commits into from
Mar 8, 2024
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
80 changes: 7 additions & 73 deletions noir-projects/noir-protocol-circuits/crates/rollup-lib/src/root.nr
Original file line number Diff line number Diff line change
@@ -1,81 +1,15 @@
mod root_rollup_inputs;
use root_rollup_inputs::RootRollupInputs;
mod root_rollup_public_inputs;

// Re-exports
use root_rollup_inputs::RootRollupInputs;
use root_rollup_public_inputs::RootRollupPublicInputs;

// TODO: Move all the following code to different files
use dep::types::{
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot,
constants::{NUM_FIELDS_PER_SHA256, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, L1_TO_L2_MSG_SUBTREE_HEIGHT},
header::Header, content_commitment::ContentCommitment,
merkle_tree::{append_only_tree, calculate_subtree, calculate_empty_tree_root},
state_reference::StateReference, utils::uint256::U256
constants::{NUM_FIELDS_PER_SHA256, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP},
utils::uint256::U256
};
use crate::components;

impl RootRollupInputs {
pub fn root_rollup_circuit(self) -> RootRollupPublicInputs {
let left = self.previous_rollup_data[0].base_or_merge_rollup_public_inputs;
let right = self.previous_rollup_data[1].base_or_merge_rollup_public_inputs;

let aggregation_object = components::aggregate_proofs(left, right);
components::assert_both_input_proofs_of_same_rollup_type(left, right);
let _ = components::assert_both_input_proofs_of_same_height_and_return(left, right);
components::assert_equal_constants(left, right);
components::assert_prev_rollups_follow_on_from_each_other(left, right);

// Check correct l1 to l2 tree given
// Compute subtree inserting l1 to l2 messages
let l1_to_l2_subtree_root = calculate_subtree(self.new_l1_to_l2_messages);

// Insert subtree into the l1 to l2 data tree
let empty_l1_to_l2_subtree_root = calculate_empty_tree_root(L1_TO_L2_MSG_SUBTREE_HEIGHT);
let new_l1_to_l2_message_tree_snapshot = append_only_tree::insert_subtree_to_snapshot_tree(
self.start_l1_to_l2_message_tree_snapshot,
self.new_l1_to_l2_message_tree_root_sibling_path,
empty_l1_to_l2_subtree_root,
l1_to_l2_subtree_root,
// TODO(Kev): For now we can add a test that this fits inside of
// a u8.
L1_TO_L2_MSG_SUBTREE_HEIGHT as u8
);

let state = StateReference { l1_to_l2_message_tree: new_l1_to_l2_message_tree_snapshot, partial: right.end };

// TODO: in_hash: #4633 and out_hash: #4561
let content_commitment = ContentCommitment {
tx_tree_height: right.height_in_block_tree + 1,
txs_effects_hash: components::compute_txs_effects_hash(self.previous_rollup_data),
in_hash: [0, 0],
out_hash: components::compute_out_hash(self.previous_rollup_data)
};

let header = Header {
last_archive: left.constants.last_archive,
content_commitment,
state,
global_variables: left.constants.global_variables
};

// Build the block hash for this by hashing the header and then insert the new leaf to archive tree.
let block_hash = header.hash();

// Update the archive
let archive = append_only_tree::insert_subtree_to_snapshot_tree(
self.start_archive_snapshot,
self.new_archive_sibling_path,
0,
block_hash,
0
);

RootRollupPublicInputs {
aggregation_object,
archive,
header,
// TODO(#4492): update this when implementing the new message model
l1_to_l2_messages_hash: compute_messages_hash(self.new_l1_to_l2_messages)
}
}
}

// See `test_message_input_flattened_length` on keeping this in sync,
// why its here and how this constant was computed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
use crate::abis::previous_rollup_data::PreviousRollupData;
use crate::abis::constant_rollup_data::ConstantRollupData;
use crate::{
abis::{
previous_rollup_data::PreviousRollupData,
constant_rollup_data::ConstantRollupData,
},
components,
root::{
compute_messages_hash,
root_rollup_public_inputs::RootRollupPublicInputs,
}
};
use dep::types::{
abis::{append_only_tree_snapshot::AppendOnlyTreeSnapshot, nullifier_leaf_preimage::NullifierLeafPreimage},
constants::{NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH, ARCHIVE_HEIGHT}
constants::{NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, L1_TO_L2_MSG_SUBTREE_HEIGHT, L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH, ARCHIVE_HEIGHT},
header::Header, content_commitment::ContentCommitment,
merkle_tree::{append_only_tree, calculate_subtree, calculate_empty_tree_root},
state_reference::StateReference
};

struct RootRollupInputs {
Expand All @@ -19,3 +31,69 @@ struct RootRollupInputs {
start_archive_snapshot : AppendOnlyTreeSnapshot,
new_archive_sibling_path : [Field; ARCHIVE_HEIGHT],
}

impl RootRollupInputs {
pub fn root_rollup_circuit(self) -> RootRollupPublicInputs {
let left = self.previous_rollup_data[0].base_or_merge_rollup_public_inputs;
let right = self.previous_rollup_data[1].base_or_merge_rollup_public_inputs;

let aggregation_object = components::aggregate_proofs(left, right);
components::assert_both_input_proofs_of_same_rollup_type(left, right);
let _ = components::assert_both_input_proofs_of_same_height_and_return(left, right);
components::assert_equal_constants(left, right);
components::assert_prev_rollups_follow_on_from_each_other(left, right);

// Check correct l1 to l2 tree given
// Compute subtree inserting l1 to l2 messages
let l1_to_l2_subtree_root = calculate_subtree(self.new_l1_to_l2_messages);

// Insert subtree into the l1 to l2 data tree
let empty_l1_to_l2_subtree_root = calculate_empty_tree_root(L1_TO_L2_MSG_SUBTREE_HEIGHT);
let new_l1_to_l2_message_tree_snapshot = append_only_tree::insert_subtree_to_snapshot_tree(
self.start_l1_to_l2_message_tree_snapshot,
self.new_l1_to_l2_message_tree_root_sibling_path,
empty_l1_to_l2_subtree_root,
l1_to_l2_subtree_root,
// TODO(Kev): For now we can add a test that this fits inside of
// a u8.
L1_TO_L2_MSG_SUBTREE_HEIGHT as u8
);

let state = StateReference { l1_to_l2_message_tree: new_l1_to_l2_message_tree_snapshot, partial: right.end };

// TODO: in_hash: #4633 and out_hash: #4561
let content_commitment = ContentCommitment {
tx_tree_height: right.height_in_block_tree + 1,
txs_effects_hash: components::compute_txs_effects_hash(self.previous_rollup_data),
in_hash: [0, 0],
out_hash: components::compute_out_hash(self.previous_rollup_data)
};

let header = Header {
last_archive: left.constants.last_archive,
content_commitment,
state,
global_variables: left.constants.global_variables
};

// Build the block hash for this by hashing the header and then insert the new leaf to archive tree.
let block_hash = header.hash();

// Update the archive
let archive = append_only_tree::insert_subtree_to_snapshot_tree(
self.start_archive_snapshot,
self.new_archive_sibling_path,
0,
block_hash,
0
);

RootRollupPublicInputs {
aggregation_object,
archive,
header,
// TODO(#4492): update this when implementing the new message model
l1_to_l2_messages_hash: compute_messages_hash(self.new_l1_to_l2_messages)
}
}
}
Loading