Skip to content

Commit

Permalink
feat: update root rollup circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
cheethas committed Apr 26, 2023
1 parent 1261a75 commit 0dd1ca7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ template <typename NCT> struct RootRollupInputs {
std::array<fr, CONTRACT_TREE_ROOTS_TREE_HEIGHT> new_historic_contract_tree_root_sibling_path;

// TODO(sean): is this going to be here?
std::array<fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP> new_l1_to_l2_messages;
std::array<fr, L2_MSG_TREE_HEIGHT> new_l1_to_l2_message_tree_root_sibling_path;
std::array<fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP> l1_to_l2_messages;
std::array<fr, L1_TO_L2_MSG_TREE_HEIGHT> new_l1_to_l2_message_tree_root_sibling_path;
std::array<fr, L1_TO_L2_MSG_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 @@ -41,7 +44,7 @@ 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.new_l1_to_l2_messages);
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);
};
Expand All @@ -53,7 +56,7 @@ 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.new_l1_to_l2_messages);
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);
};
Expand All @@ -64,7 +67,7 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, RootRollupInp
<< "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_l1_to_l2_messages: " << obj.new_l1_to_l2_messages << "\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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ template <typename NCT> struct RootRollupPublicInputs {
AppendOnlyTreeSnapshot<NCT> end_tree_of_historic_l1_tol2_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 @@ -64,6 +65,7 @@ template <typename NCT> void read(uint8_t const*& it, RootRollupPublicInputs<NCT
read(it, obj.start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
read(it, obj.end_tree_of_historic_l1_tol2_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 @@ -86,6 +88,7 @@ template <typename NCT> void write(std::vector<uint8_t>& buf, RootRollupPublicIn
write(buf, obj.start_tree_of_historic_l1_to_l2_messages_tree_roots_snapshot);
write(buf, obj.end_tree_of_historic_l1_tol2_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 @@ -111,7 +114,9 @@ template <typename NCT> std::ostream& operator<<(std::ostream& os, RootRollupPub
<< 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_tol2_messages_tree_roots_snapshot << "\n"
<< "calldata_hash: " << obj.calldata_hash << "\n";
<< "calldata_hash: " << obj.calldata_hash << "\n"
<< "l1_to_l2_messages_hash: " << obj.l1_to_l2_messages_hash << "\n";
;
};

} // namespace aztec3::circuits::abis
2 changes: 2 additions & 0 deletions circuits/cpp/src/aztec3/circuits/rollup/root/init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ using RootRollupPublicInputs = abis::RootRollupPublicInputs<NT>;

using Aggregator = aztec3::circuits::recursion::Aggregator;

using MerkleTree = stdlib::merkle_tree::MemoryTree;

} // namespace aztec3::circuits::rollup::native_root_rollup
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,40 @@ namespace aztec3::circuits::rollup::native_root_rollup {

// Access Native types through NT namespace

const NT::fr EMPTY_L1_TO_L2_MESSAGES_SUBTREE_ROOT = MerkleTree(L1_TO_L2_MSG_SUBTREE_DEPTH).root();

// TODO: turn this into generic function that can be used by other rollups
NT::fr calculate_subtree(std::array<NT::fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP> leaves)
{
MerkleTree merkle_tree = MerkleTree(L1_TO_L2_MSG_SUBTREE_DEPTH);

// Compute the merkle root of a contract subtree
// Contracts subtree
for (size_t i = 0; i < NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP; i++) {
merkle_tree.update_element(i, leaves[i]);
}
return merkle_tree.root();
}

// TODO: move helper functions here to components
std::array<NT::fr, 2> compute_messages_hash(std::array<NT::fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP> leaves)
{
std::vector<uint8_t> messages_hash_input_bytes_vec(leaves.begin(), leaves.end());
auto h = sha256::sha256(messages_hash_input_bytes_vec);

std::array<uint8_t, 32> buf_1, buf_2;
for (uint8_t i = 0; i < 16; i++) {
buf_1[i] = 0;
buf_1[16 + i] = h[i];
buf_2[i] = 0;
buf_2[16 + i] = h[i + 16];
}
auto high = fr::serialize_from_buffer(buf_1.data());
auto low = fr::serialize_from_buffer(buf_2.data());

return { high, low };
}

RootRollupPublicInputs root_rollup_circuit(DummyComposer& composer, RootRollupInputs const& rootRollupInputs)
{
// TODO: Verify the previous rollup proofs
Expand Down Expand Up @@ -59,6 +93,29 @@ RootRollupPublicInputs root_rollup_circuit(DummyComposer& composer, RootRollupIn
right.end_contract_tree_snapshot.root,
0);

// Check correct l1 to l2 tree given

// Compute subtree inserting l1 to l2 messages
auto l1_to_l2_subtree_root = calculate_subtree(rootRollupInputs.l1_to_l2_messages);

// Insert subtree into the l1 to l2 data tree
auto new_root =
components::insert_subtree_to_snapshot_tree(composer,
rootRollupInputs.start_l1_to_l2_message_tree_snapshot,
rootRollupInputs.new_l1_to_l2_message_tree_root_sibling_path,
EMPTY_L1_TO_L2_MESSAGES_SUBTREE_ROOT,
l1_to_l2_subtree_root,
L1_TO_L2_MSG_SUBTREE_INCLUSION_CHECK_DEPTH);

// Update the historic l1 to l2 data tree
auto end_l1_to_l2_data_tree_snapshot = components::insert_subtree_to_snapshot_tree(
composer,
rootRollupInputs.start_historic_tree_l1_to_l2_message_tree_roots_snapshot,
rootRollupInputs.new_historic_private_data_tree_root_sibling_path,
fr::zero(),
new_root.root,
0);

RootRollupPublicInputs public_inputs = {
.end_aggregation_object = aggregation_object,
.start_private_data_tree_snapshot = left.start_private_data_tree_snapshot,
Expand All @@ -74,6 +131,7 @@ RootRollupPublicInputs root_rollup_circuit(DummyComposer& composer, RootRollupIn
left.constants.start_tree_of_historic_contract_tree_roots_snapshot,
.end_tree_of_historic_contract_tree_roots_snapshot = end_tree_of_historic_contract_tree_roots_snapshot,
.calldata_hash = components::compute_calldata_hash(rootRollupInputs.previous_rollup_data),
.l1_to_l2_messages_hash = compute_messages_hash(rootRollupInputs.l1_to_l2_messages)
};

return public_inputs;
Expand Down
4 changes: 2 additions & 2 deletions circuits/cpp/src/aztec3/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ constexpr size_t PRIVATE_DATA_SUBTREE_INCLUSION_CHECK_DEPTH = NULLIFIER_TREE_HEI
constexpr size_t NULLIFIER_SUBTREE_DEPTH = 3;
constexpr size_t NULLIFIER_SUBTREE_INCLUSION_CHECK_DEPTH = NULLIFIER_TREE_HEIGHT - NULLIFIER_SUBTREE_DEPTH;

constexpr size_t L2_MSG_SUBTREE_DEPTH = 3;
constexpr size_t L2_MSG_SUBTREE_INCLUSION_CHECK_DEPTH = L1_TO_L2_MSG_TREE_HEIGHT - L2_MSG_SUBTREE_DEPTH;
constexpr size_t L1_TO_L2_MSG_SUBTREE_DEPTH = 4;
constexpr size_t L1_TO_L2_MSG_SUBTREE_INCLUSION_CHECK_DEPTH = L1_TO_L2_MSG_TREE_HEIGHT - L1_TO_L2_MSG_SUBTREE_DEPTH;

constexpr size_t PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT = 8;
constexpr size_t CONTRACT_TREE_ROOTS_TREE_HEIGHT = 8;
Expand Down
15 changes: 12 additions & 3 deletions yarn-project/circuits.js/src/structs/rollup/root_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { serializeToBuffer } from '../../utils/serialize.js';
import { AppendOnlyTreeSnapshot } from './append_only_tree_snapshot.js';
import {
CONTRACT_TREE_ROOTS_TREE_HEIGHT,
L1_TO_L2_MESSAGES_ROOTS_TREE_HEIGHT,
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
} from '../constants.js';
Expand All @@ -16,13 +17,15 @@ export class RootRollupInputs {

public newHistoricPrivateDataTreeRootSiblingPath: Fr[],
public newHistoricContractDataTreeRootSiblingPath: Fr[],

// TODO: Work out when writing the circuits if i will need to include both the normal and historic paths here
public newL1ToL2MessageTreeRootSiblingPath: Fr[],
public newHistoricL1ToL2MessageTreeRootSiblingPath: Fr[],
public newL1ToL2Messages: Fr[],
) {
assertLength(this, 'newHistoricPrivateDataTreeRootSiblingPath', PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT);
assertLength(this, 'newHistoricContractDataTreeRootSiblingPath', CONTRACT_TREE_ROOTS_TREE_HEIGHT);
// TODO: the height of this could be wrong
assertLength(this, 'newL1ToL2MessageTreeRootSiblingPath', L1_TO_L2_MESSAGES_ROOTS_TREE_HEIGHT);
assertLength(this, 'newHistoricL1ToL2MessageTreeRootSiblingPath', L1_TO_L2_MESSAGES_ROOTS_TREE_HEIGHT);
assertLength(this, 'newL1ToL2Messages', NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
}

Expand All @@ -31,6 +34,8 @@ export class RootRollupInputs {
this.previousRollupData,
this.newHistoricPrivateDataTreeRootSiblingPath,
this.newHistoricContractDataTreeRootSiblingPath,
this.newL1ToL2MessageTreeRootSiblingPath,
this.newHistoricL1ToL2MessageTreeRootSiblingPath,
this.newL1ToL2Messages,
);
}
Expand All @@ -44,7 +49,8 @@ export class RootRollupInputs {
fields.previousRollupData,
fields.newHistoricPrivateDataTreeRootSiblingPath,
fields.newHistoricContractDataTreeRootSiblingPath,
fields.newHistoricContractDataTreeRootSiblingPath,
fields.newL1ToL2MessageTreeRootSiblingPath,
fields.newHistoricL1ToL2MessageTreeRootSiblingPath,
fields.newL1ToL2Messages,
] as const;
}
Expand Down Expand Up @@ -79,6 +85,7 @@ export class RootRollupPublicInputs {
public endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot: AppendOnlyTreeSnapshot,

public calldataHash: [Fr, Fr],
public l1ToL2MessagesHash: [Fr, Fr],
) {}

static getFields(fields: FieldsOf<RootRollupPublicInputs>) {
Expand All @@ -99,6 +106,7 @@ export class RootRollupPublicInputs {
fields.startTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
fields.endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
fields.calldataHash,
fields.l1ToL2MessagesHash,
] as const;
}

Expand Down Expand Up @@ -129,6 +137,7 @@ export class RootRollupPublicInputs {
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
[reader.readFr(), reader.readFr()],
[reader.readFr(), reader.readFr()],
);
}
}

0 comments on commit 0dd1ca7

Please sign in to comment.