From 1565f456d415a6d4ff9d2c9226b6f8070d939c7d Mon Sep 17 00:00:00 2001 From: LHerskind Date: Wed, 29 Mar 2023 12:44:03 +0000 Subject: [PATCH] intial root --- .../circuits/abis/membership_witness.hpp | 1 + .../abis/rollup/base/previous_rollup_data.hpp | 59 ++++ .../abis/rollup/root/root_rollup_inputs.hpp | 54 ++++ .../rollup/root/root_rollup_public_inputs.hpp | 97 ++++++ .../circuits/rollup/base/CMakeLists.txt | 1 + cpp/src/aztec3/circuits/rollup/root/.test.cpp | 297 ++++++++++++++++++ .../circuits/rollup/root/CMakeLists.txt | 5 + cpp/src/aztec3/circuits/rollup/root/index.hpp | 2 + cpp/src/aztec3/circuits/rollup/root/init.hpp | 30 ++ .../root/native_root_rollup_circuit.cpp | 111 +++++++ .../root/native_root_rollup_circuit.hpp | 18 ++ 11 files changed, 675 insertions(+) create mode 100644 cpp/src/aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp create mode 100644 cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp create mode 100644 cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_public_inputs.hpp create mode 100644 cpp/src/aztec3/circuits/rollup/root/.test.cpp create mode 100644 cpp/src/aztec3/circuits/rollup/root/CMakeLists.txt create mode 100644 cpp/src/aztec3/circuits/rollup/root/index.hpp create mode 100644 cpp/src/aztec3/circuits/rollup/root/init.hpp create mode 100644 cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp create mode 100644 cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.hpp diff --git a/cpp/src/aztec3/circuits/abis/membership_witness.hpp b/cpp/src/aztec3/circuits/abis/membership_witness.hpp index 79349514..5afb4d75 100644 --- a/cpp/src/aztec3/circuits/abis/membership_witness.hpp +++ b/cpp/src/aztec3/circuits/abis/membership_witness.hpp @@ -1,6 +1,7 @@ #pragma once #include "aztec3/utils/types/circuit_types.hpp" +#include "aztec3/utils/types/convert.hpp" namespace aztec3::circuits::abis { using aztec3::utils::types::CircuitTypes; diff --git a/cpp/src/aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp b/cpp/src/aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp new file mode 100644 index 00000000..b9824dff --- /dev/null +++ b/cpp/src/aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp @@ -0,0 +1,59 @@ +#pragma once +#include "aztec3/circuits/abis/append_only_tree_snapshot.hpp" +#include "aztec3/circuits/abis/membership_witness.hpp" +#include "aztec3/circuits/abis/rollup/base/base_rollup_public_inputs.hpp" +#include "aztec3/constants.hpp" +#include +#include +#include +#include + +namespace aztec3::circuits::abis { + +using aztec3::utils::types::CircuitTypes; +using aztec3::utils::types::NativeTypes; +using std::is_same; + +template struct PreviousRollupData { + BaseRollupPublicInputs base_rollup_public_inputs; + + NativeTypes::Proof proof; + std::shared_ptr vk; + NativeTypes::uint32 vk_index; + MembershipWitness vk_sibling_path; + + bool operator==(PreviousRollupData const&) const = default; +}; + +template void read(uint8_t const*& it, PreviousRollupData& obj) +{ + using serialize::read; + + read(it, obj.base_rollup_public_inputs); + read(it, obj.proof); + read(it, obj.vk); + read(it, obj.vk_index); + read(it, obj.vk_sibling_path); +}; + +template void write(std::vector& buf, PreviousRollupData const& obj) +{ + using serialize::write; + + write(buf, obj.base_rollup_public_inputs); + write(buf, obj.proof); + write(buf, *obj.vk); + write(buf, obj.vk_index); + write(buf, obj.vk_sibling_path); +}; + +template std::ostream& operator<<(std::ostream& os, PreviousRollupData const& obj) +{ + return os << "merge_rollup_public_inputs: " << obj.base_rollup_public_inputs << "\n" + << "proof: " << obj.proof << "\n" + << "vk: " << obj.vk << "\n" + << "vk_index: " << obj.vk_index << "\n" + << "vk_sibling_path: " << obj.vk_sibling_path << "\n"; +}; + +} // namespace aztec3::circuits::abis \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp b/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp new file mode 100644 index 00000000..2e6082da --- /dev/null +++ b/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp @@ -0,0 +1,54 @@ + + +#pragma once +#include "aztec3/circuits/abis/append_only_tree_snapshot.hpp" +#include "aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp" +#include "aztec3/constants.hpp" +#include +#include +#include +#include + +namespace aztec3::circuits::abis { + +using aztec3::utils::types::CircuitTypes; +using aztec3::utils::types::NativeTypes; + +template struct RootRollupInputs { + typedef typename NCT::fr fr; + + // All below are shared between the base and merge rollups + std::array, 2> previous_rollup_data; + + std::array new_historic_private_data_tree_root_sibling_path; + std::array new_historic_contract_tree_root_sibling_path; + + bool operator==(RootRollupInputs const&) const = default; +}; + +template void read(uint8_t const*& it, RootRollupInputs& obj) +{ + using serialize::read; + + read(it, obj.previous_rollup_data); + read(it, obj.new_historic_private_data_tree_roots); + read(it, obj.new_historic_contract_tree_roots); +}; + +template void write(std::vector& buf, RootRollupInputs const& obj) +{ + using serialize::write; + + write(buf, obj.previous_rollup_data); + write(buf, obj.new_historic_private_data_tree_roots); + write(buf, obj.new_historic_contract_tree_roots); +}; + +template std::ostream& operator<<(std::ostream& os, RootRollupInputs const& obj) +{ + return os << "previous_rollup_data: " << obj.previous_rollup_data << "\n" + << "new_historic_private_data_tree_roots: " << obj.new_historic_private_data_tree_roots << "\n" + << "new_historic_contract_tree_roots: " << obj.new_historic_contract_tree_roots << "\n"; +} + +} // namespace aztec3::circuits::abis \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_public_inputs.hpp b/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_public_inputs.hpp new file mode 100644 index 00000000..f61f2823 --- /dev/null +++ b/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_public_inputs.hpp @@ -0,0 +1,97 @@ + +#pragma once +#include "aztec3/circuits/abis/append_only_tree_snapshot.hpp" +#include +#include +#include +#include + +namespace aztec3::circuits::abis { + +using aztec3::utils::types::CircuitTypes; +using aztec3::utils::types::NativeTypes; + +template struct RootRollupPublicInputs { + typedef typename NCT::fr fr; + typedef typename NCT::AggregationObject AggregationObject; + + // All below are shared between the base and merge rollups + AggregationObject end_aggregation_object; + + AppendOnlyTreeSnapshot start_private_data_tree_snapshot; + AppendOnlyTreeSnapshot end_private_data_tree_snapshot; + + AppendOnlyTreeSnapshot start_nullifier_tree_snapshot; + AppendOnlyTreeSnapshot end_nullifier_tree_snapshot; + + AppendOnlyTreeSnapshot start_contract_tree_snapshot; + AppendOnlyTreeSnapshot end_contract_tree_snapshot; + + AppendOnlyTreeSnapshot start_tree_of_private_data_tree_roots_snapshot; + AppendOnlyTreeSnapshot end_tree_of_private_data_tree_roots_snapshot; + + AppendOnlyTreeSnapshot start_tree_of_historic_contract_tree_roots_snapshot; + AppendOnlyTreeSnapshot end_tree_of_historic_contract_tree_roots_snapshot; + + std::array calldata_hash; + + bool operator==(RootRollupPublicInputs const&) const = default; +}; + +template void read(uint8_t const*& it, RootRollupPublicInputs& obj) +{ + using serialize::read; + + read(it, obj.end_aggregation_object); + read(it, obj.start_private_data_tree_snapshot); + read(it, obj.end_private_data_tree_snapshot); + read(it, obj.start_nullifier_tree_snapshot); + read(it, obj.end_nullifier_tree_snapshot); + read(it, obj.start_contract_tree_snapshot); + read(it, obj.end_contract_tree_snapshot); + read(it, obj.start_tree_of_private_data_tree_roots_snapshot); + read(it, obj.end_tree_of_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.calldata_hash); +}; + +template void write(std::vector& buf, RootRollupPublicInputs const& obj) +{ + using serialize::write; + + write(buf, obj.end_aggregation_object); + write(buf, obj.start_private_data_tree_snapshot); + write(buf, obj.end_private_data_tree_snapshot); + write(buf, obj.start_nullifier_tree_snapshot); + write(buf, obj.end_nullifier_tree_snapshot); + write(buf, obj.start_contract_tree_snapshot); + write(buf, obj.end_contract_tree_snapshot); + write(buf, obj.start_tree_of_private_data_tree_roots_snapshot); + write(buf, obj.end_tree_of_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.calldata_hash); +}; + +template std::ostream& operator<<(std::ostream& os, RootRollupPublicInputs const& obj) +{ + return os << "end_aggregation_object: " << obj.end_aggregation_object << "\n" + << "start_private_data_tree_snapshot: " << obj.start_private_data_tree_snapshot << "\n" + << "end_private_data_tree_snapshot: " << obj.end_private_data_tree_snapshot << "\n" + << "start_nullifier_tree_snapshot: " << obj.start_nullifier_tree_snapshot << "\n" + << "end_nullifier_tree_snapshot: " << obj.end_nullifier_tree_snapshot << "\n" + << "start_contract_tree_snapshot: " << obj.start_contract_tree_snapshot << "\n" + << "end_contract_tree_snapshot: " << obj.end_contract_tree_snapshot << "\n" + << "start_tree_of_private_data_tree_roots_snapshot: " + << obj.start_tree_of_private_data_tree_roots_snapshot << "\n" + << "end_tree_of_private_data_tree_roots_snapshot: " << obj.end_tree_of_private_data_tree_roots_snapshot + << "\n" + << "start_tree_of_historic_contract_tree_roots_snapshot: " + << 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"; +}; + +} // namespace aztec3::circuits::abis \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/base/CMakeLists.txt b/cpp/src/aztec3/circuits/rollup/base/CMakeLists.txt index fb099fb3..4464c55a 100644 --- a/cpp/src/aztec3/circuits/rollup/base/CMakeLists.txt +++ b/cpp/src/aztec3/circuits/rollup/base/CMakeLists.txt @@ -1,4 +1,5 @@ barretenberg_module( aztec3_circuits_rollup + aztec3_circuits_kernel barretenberg ) \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/.test.cpp b/cpp/src/aztec3/circuits/rollup/root/.test.cpp new file mode 100644 index 00000000..06b17c87 --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/.test.cpp @@ -0,0 +1,297 @@ +// #include +// #include +// #include +// #include +// #include +#include "aztec3/circuits/abis/append_only_tree_snapshot.hpp" +#include "aztec3/circuits/abis/membership_witness.hpp" +#include "aztec3/circuits/abis/private_kernel/new_contract_data.hpp" +#include "aztec3/circuits/abis/private_kernel/previous_kernel_data.hpp" +#include "aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp" +#include "aztec3/circuits/abis/rollup/nullifier_leaf_preimage.hpp" +#include "aztec3/circuits/rollup/base/init.hpp" +#include "aztec3/circuits/kernel/private/utils.hpp" +#include "aztec3/constants.hpp" +#include "barretenberg/crypto/sha256/sha256.hpp" +#include "barretenberg/ecc/curves/bn254/fr.hpp" +#include "barretenberg/stdlib/merkle_tree/memory_tree.hpp" +#include "index.hpp" +#include "init.hpp" +#include "aztec3/circuits/rollup/base/native_base_rollup_circuit.hpp" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// #include +// #include + +#include + +// #include +#include + +#include +#include +#include +#include +#include +#include +#include + +// #include +// #include +// #include + +namespace { + +using aztec3::circuits::abis::CallContext; +using aztec3::circuits::abis::CallStackItem; +using aztec3::circuits::abis::CallType; +using aztec3::circuits::abis::ContractDeploymentData; +using aztec3::circuits::abis::FunctionData; +using aztec3::circuits::abis::OptionalPrivateCircuitPublicInputs; +using aztec3::circuits::abis::PrivateCircuitPublicInputs; +using aztec3::circuits::abis::SignedTxRequest; +using aztec3::circuits::abis::TxContext; +using aztec3::circuits::abis::TxRequest; + +using aztec3::circuits::abis::private_kernel::AccumulatedData; +using aztec3::circuits::abis::private_kernel::ConstantData; +using aztec3::circuits::abis::private_kernel::Globals; +using aztec3::circuits::abis::private_kernel::OldTreeRoots; +using aztec3::circuits::abis::private_kernel::PreviousKernelData; +using aztec3::circuits::abis::private_kernel::PrivateCallData; +using aztec3::circuits::abis::private_kernel::PrivateInputs; +using aztec3::circuits::abis::private_kernel::PublicInputs; + +using aztec3::circuits::apps::test_apps::basic_contract_deployment::constructor; +using aztec3::circuits::apps::test_apps::escrow::deposit; + +// using aztec3::circuits::mock::mock_circuit; +using aztec3::circuits::kernel::private_kernel::utils::default_previous_kernel; +using aztec3::circuits::mock::mock_kernel_circuit; +// using aztec3::circuits::mock::mock_kernel_inputs; + +using aztec3::circuits::abis::AppendOnlyTreeSnapshot; + +using aztec3::circuits::abis::MembershipWitness; +using aztec3::circuits::abis::NullifierLeafPreimage; +using aztec3::circuits::rollup::native_base_rollup::BaseRollupInputs; +using aztec3::circuits::rollup::native_base_rollup::BaseRollupPublicInputs; +using aztec3::circuits::rollup::native_base_rollup::ConstantRollupData; +using aztec3::circuits::rollup::native_base_rollup::NT; + +using aztec3::circuits::abis::PreviousRollupData; +using aztec3::circuits::rollup::native_root_rollup::RootRollupInputs; +using aztec3::circuits::rollup::native_root_rollup::RootRollupPublicInputs; + +using aztec3::circuits::abis::FunctionData; +using aztec3::circuits::abis::OptionallyRevealedData; +using aztec3::circuits::abis::private_kernel::NewContractData; + +} // namespace + +namespace aztec3::circuits::rollup::root::native_root_rollup_circuit { + +class root_rollup_tests : public ::testing::Test { + protected: + PreviousKernelData getEmptyPreviousKernelData() + { + + std::array, KERNEL_NEW_CONTRACTS_LENGTH> new_contracts; + new_contracts.fill(NewContractData{ + .contract_address = fr::zero(), .portal_contract_address = fr::zero(), .function_tree_root = fr::zero() }); + + std::array, KERNEL_OPTIONALLY_REVEALED_DATA_LENGTH> optionally_revealed_data; + + optionally_revealed_data.fill(OptionallyRevealedData{ .call_stack_item_hash = fr::zero(), + .function_data = FunctionData::empty(), + .emitted_events = { 0 }, + .vk_hash = fr::zero(), + .portal_contract_address = { 0 }, + .pay_fee_from_l1 = false, + .pay_fee_from_public_l2 = false, + .called_from_l1 = false, + .called_from_public_l2 = false }); + + native_base_rollup::AggregationObject agg_obj; + // Aggregation Object + AccumulatedData accumulated_data = { + .aggregation_object = agg_obj, + .private_call_count = fr::zero(), + .new_commitments = { 0 }, + .new_nullifiers = { 0 }, + .private_call_stack = { 0 }, + .public_call_stack = { 0 }, + .l1_msg_stack = { 0 }, + .new_contracts = new_contracts, + .optionally_revealed_data = optionally_revealed_data, + }; + + OldTreeRoots old_tree_roots = { + .private_data_tree_root = fr::zero(), + .nullifier_tree_root = fr::zero(), + .contract_tree_root = fr::zero(), + .private_kernel_vk_tree_root = fr::zero(), + }; + + TxContext tx_context = { + .is_fee_payment_tx = false, + .is_rebate_payment_tx = false, + .is_contract_deployment_tx = false, + .contract_deployment_data = { + .constructor_vk_hash = fr::zero(), + .function_tree_root = fr::zero(), + .contract_address_salt = fr::zero(), + .portal_contract_address = fr::zero(), + }, + }; + + PublicInputs kernel_public_inputs = { + .end = accumulated_data, + .constants = { .old_tree_roots = old_tree_roots, .tx_context = tx_context }, + .is_private = true, + }; + + PreviousKernelData kernel_data = { + .public_inputs = kernel_public_inputs, + }; + + return kernel_data; + } + + protected: + BaseRollupInputs getEmptyBaseRollupInputs() + { + ConstantRollupData constantRollupData = ConstantRollupData::empty(); + + std::array, 2 * KERNEL_NEW_NULLIFIERS_LENGTH> low_nullifier_leaf_preimages; + std::array, 2 * KERNEL_NEW_NULLIFIERS_LENGTH> + low_nullifier_membership_witness; + + for (size_t i = 0; i < 2 * KERNEL_NEW_NULLIFIERS_LENGTH; ++i) { + low_nullifier_leaf_preimages[i] = NullifierLeafPreimage::empty(); + low_nullifier_membership_witness[i] = MembershipWitness::empty(); + } + + std::array, 2> + historic_private_data_tree_root_membership_witnesses = { + MembershipWitness::empty(), + MembershipWitness::empty() + }; + + std::array, 2> + historic_contract_tree_root_membership_witnesses = { + MembershipWitness::empty(), + MembershipWitness::empty() + }; + + // Kernels + std::array, 2> kernel_data; + kernel_data[0] = getEmptyPreviousKernelData(); + kernel_data[1] = getEmptyPreviousKernelData(); + // @note If using VK when empty, it will fail with segfault. + + BaseRollupInputs baseRollupInputs = { .kernel_data = kernel_data, + .start_private_data_tree_snapshot = AppendOnlyTreeSnapshot::empty(), + .start_nullifier_tree_snapshot = AppendOnlyTreeSnapshot::empty(), + .start_contract_tree_snapshot = AppendOnlyTreeSnapshot::empty(), + .low_nullifier_leaf_preimages = low_nullifier_leaf_preimages, + .low_nullifier_membership_witness = low_nullifier_membership_witness, + .new_commitments_subtree_sibling_path = { 0 }, + .new_nullifiers_subtree_sibling_path = { 0 }, + .new_contracts_subtree_sibling_path = { 0 }, + .historic_private_data_tree_root_membership_witnesses = + historic_private_data_tree_root_membership_witnesses, + .historic_contract_tree_root_membership_witnesses = + historic_contract_tree_root_membership_witnesses, + .constants = constantRollupData }; + return baseRollupInputs; + } + + protected: + RootRollupInputs getEmptyRootRollupInputs() + { + BaseRollupInputs emptyInputs = getEmptyBaseRollupInputs(); + BaseRollupInputs emptyInputs2 = getEmptyBaseRollupInputs(); + BaseRollupPublicInputs outputs1 = + aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(emptyInputs); + BaseRollupPublicInputs outputs2 = + aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(emptyInputs2); + + PreviousKernelData mocked_kernel0 = default_previous_kernel(); + PreviousKernelData mocked_kernel1 = default_previous_kernel(); + + PreviousRollupData r1 = { + .base_rollup_public_inputs = outputs1, + .proof = mocked_kernel0.proof, + .vk = mocked_kernel0.vk, + .vk_index = 0, + .vk_sibling_path = MembershipWitness::empty(), + }; + + PreviousRollupData r2 = { + .base_rollup_public_inputs = outputs1, + .proof = mocked_kernel1.proof, + .vk = mocked_kernel1.vk, + .vk_index = 1, + .vk_sibling_path = MembershipWitness::empty(), + }; + + std::array, 2> previous_rollup_data = { r1, r2 }; + + RootRollupInputs rootRollupInputs = { + .previous_rollup_data = previous_rollup_data, + .new_historic_private_data_tree_root_sibling_path = { 0 }, + .new_historic_contract_tree_root_sibling_path = { 0 }, + }; + + return rootRollupInputs; + } +}; + +TEST_F(root_rollup_tests, blabber) +{ + std::vector zero_bytes_vec(704, 0); + auto call_data_hash_inner = sha256::sha256(zero_bytes_vec); + + std::array hash_input; + for (uint8_t i = 0; i < 32; ++i) { + hash_input[i] = call_data_hash_inner[i]; + hash_input[32 + i] = call_data_hash_inner[i]; + } + + std::vector calldata_hash_input_bytes_vec(hash_input.begin(), hash_input.end()); + + auto hash = sha256::sha256(calldata_hash_input_bytes_vec); + + RootRollupInputs inputs = getEmptyRootRollupInputs(); + RootRollupPublicInputs outputs = aztec3::circuits::rollup::native_root_rollup::root_rollup_circuit(inputs); + + std::array calldata_hash_fr = outputs.calldata_hash; + auto high_buffer = calldata_hash_fr[0].to_buffer(); + auto low_buffer = calldata_hash_fr[1].to_buffer(); + + std::array calldata_hash; + for (uint8_t i = 0; i < 16; ++i) { + calldata_hash[i] = high_buffer[16 + i]; + calldata_hash[16 + i] = low_buffer[16 + i]; + } + + ASSERT_EQ(hash, calldata_hash); +} + +} // namespace aztec3::circuits::rollup::root::native_root_rollup_circuit \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/CMakeLists.txt b/cpp/src/aztec3/circuits/rollup/root/CMakeLists.txt new file mode 100644 index 00000000..4464c55a --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/CMakeLists.txt @@ -0,0 +1,5 @@ +barretenberg_module( + aztec3_circuits_rollup + aztec3_circuits_kernel + barretenberg +) \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/index.hpp b/cpp/src/aztec3/circuits/rollup/root/index.hpp new file mode 100644 index 00000000..09198701 --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/index.hpp @@ -0,0 +1,2 @@ +#include "init.hpp" +#include "native_root_rollup_circuit.hpp" \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/init.hpp b/cpp/src/aztec3/circuits/rollup/root/init.hpp new file mode 100644 index 00000000..13f236d4 --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/init.hpp @@ -0,0 +1,30 @@ + +#pragma once + +#include "aztec3/circuits/abis/append_only_tree_snapshot.hpp" +#include "aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp" +#include "aztec3/circuits/abis/rollup/constant_rollup_data.hpp" +#include "aztec3/circuits/abis/rollup/root/root_rollup_public_inputs.hpp" +#include +#include + +#include +#include +#include +#include +#include + +namespace aztec3::circuits::rollup::native_root_rollup { + +using NT = aztec3::utils::types::NativeTypes; + +// Params +using ConstantRollupData = abis::ConstantRollupData; +using RootRollupInputs = abis::RootRollupInputs; +using RootRollupPublicInputs = abis::RootRollupPublicInputs; + +using Aggregator = aztec3::circuits::recursion::Aggregator; +using AggregationObject = utils::types::NativeTypes::AggregationObject; +using AppendOnlySnapshot = abis::AppendOnlyTreeSnapshot; + +} // namespace aztec3::circuits::rollup::native_root_rollup \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp b/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp new file mode 100644 index 00000000..763af2b1 --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp @@ -0,0 +1,111 @@ +#include "aztec3/constants.hpp" +#include "barretenberg/crypto/pedersen_hash/pedersen.hpp" +#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/membership.hpp" +#include "barretenberg/stdlib/merkle_tree/memory_tree.hpp" +#include "barretenberg/stdlib/merkle_tree/merkle_tree.hpp" +#include "init.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace aztec3::circuits::rollup::native_root_rollup { + +// TODO: can we aggregate proofs if we do not have a working circuit impl + +// TODO: change the public inputs array - we wont be using this? + +// Access Native types through NT namespace + +bool verify_merge_proof(NT::Proof merge_proof) +{ + std::cout << merge_proof << std::endl; // REMOVE_ME + return true; +} + +AggregationObject aggregate_proofs(RootRollupInputs rootRollupInputs) +{ + // TODO: NOTE: for now we simply return the aggregation object from the first proof + return rootRollupInputs.previous_rollup_data[0].base_rollup_public_inputs.end_aggregation_object; +} + +std::array compute_calldata_hash(RootRollupInputs rootRollupInputs) +{ + + // Compute the calldata hash + std::array calldata_hash_input_bytes; + for (uint8_t i = 0; i < 2; i++) { + std::array calldata_hash_fr = + rootRollupInputs.previous_rollup_data[i].base_rollup_public_inputs.calldata_hash; + + auto high_buffer = calldata_hash_fr[0].to_buffer(); + auto low_buffer = calldata_hash_fr[1].to_buffer(); + + for (uint8_t j = 0; j < 16; ++j) { + calldata_hash_input_bytes[i * 32 + j] = high_buffer[16 + j]; + calldata_hash_input_bytes[i * 32 + 16 + j] = low_buffer[16 + j]; + } + } + + std::vector calldata_hash_input_bytes_vec(calldata_hash_input_bytes.begin(), + calldata_hash_input_bytes.end()); + + auto h = sha256::sha256(calldata_hash_input_bytes_vec); + + // Split the hash into two fields, a high and a low + std::array 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 }; +} + +// Important types: +// - BaseRollupPublicInputs - where we want to put our return values +// +// TODO: replace auto +RootRollupPublicInputs root_rollup_circuit(RootRollupInputs rootRollupInputs) +{ + // First we compute the contract tree leaves + + AggregationObject aggregation_object = aggregate_proofs(rootRollupInputs); + + // Verify the previous merge proofs (for now these are actually base proofs) + for (size_t i = 0; i < 2; i++) { + NT::Proof proof = rootRollupInputs.previous_rollup_data[i].proof; + assert(verify_merge_proof(proof)); + } + + RootRollupPublicInputs public_inputs = { + .end_aggregation_object = aggregation_object, + .start_private_data_tree_snapshot = AppendOnlySnapshot::empty(), + .end_private_data_tree_snapshot = AppendOnlySnapshot::empty(), + .start_nullifier_tree_snapshot = AppendOnlySnapshot::empty(), + .end_nullifier_tree_snapshot = AppendOnlySnapshot::empty(), + .start_contract_tree_snapshot = AppendOnlySnapshot::empty(), + .end_contract_tree_snapshot = AppendOnlySnapshot::empty(), + .start_tree_of_private_data_tree_roots_snapshot = AppendOnlySnapshot::empty(), + .end_tree_of_private_data_tree_roots_snapshot = AppendOnlySnapshot::empty(), + .start_tree_of_historic_contract_tree_roots_snapshot = AppendOnlySnapshot::empty(), + .end_tree_of_historic_contract_tree_roots_snapshot = AppendOnlySnapshot::empty(), + .calldata_hash = compute_calldata_hash(rootRollupInputs), + }; + return public_inputs; +} + +} // namespace aztec3::circuits::rollup::native_root_rollup \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.hpp b/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.hpp new file mode 100644 index 00000000..ffa7a1bb --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "init.hpp" + +// TODO: not needed right at this moment for native impl +#include +#include +#include +#include + +#include +#include + +namespace aztec3::circuits::rollup::native_root_rollup { + +RootRollupPublicInputs root_rollup_circuit(RootRollupInputs rootRollupInputs); + +} // namespace aztec3::circuits::rollup::native_root_rollup \ No newline at end of file