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

chore(circuits): Base rollup cbind msgpack #2263

Merged
merged 8 commits into from
Sep 14, 2023
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
9 changes: 7 additions & 2 deletions circuits/cpp/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@ cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert
cmake --build --preset $PRESET ${@/#/--target }

# Build WASM.
cmake --preset wasm
cmake --build --preset wasm
if [ -n "${WASM_DEBUG:-}" ] ; then
cmake --preset wasm-dbg
cmake --build --preset wasm-dbg
else
cmake --preset wasm
cmake --build --preset wasm
fi
3 changes: 3 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/packers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct ConstantsPacker {
NUM_UNENCRYPTED_LOGS_HASHES_PER_TX,
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
KERNELS_PER_BASE_ROLLUP,
MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_BASE_ROLLUP,
MAX_PUBLIC_DATA_READS_PER_BASE_ROLLUP,
VK_TREE_HEIGHT,
FUNCTION_TREE_HEIGHT,
CONTRACT_TREE_HEIGHT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const uint32_t MERGE_ROLLUP_TYPE = 1;
template <typename NCT> struct BaseOrMergeRollupPublicInputs {
using fr = typename NCT::fr;
using AggregationObject = typename NCT::AggregationObject;
using boolean = typename NCT::boolean;

uint32_t rollup_type;
// subtree height is always 0 for base.
Expand Down Expand Up @@ -52,7 +53,10 @@ template <typename NCT> struct BaseOrMergeRollupPublicInputs {
start_public_data_tree_root,
end_public_data_tree_root,
calldata_hash);
bool operator==(BaseOrMergeRollupPublicInputs<NCT> const&) const = default;
boolean operator==(BaseOrMergeRollupPublicInputs<NCT> const& other) const
{
return msgpack_derived_equals<boolean>(*this, other);
};
};

} // namespace aztec3::circuits::abis
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ namespace aztec3::circuits::abis {

template <typename NCT> struct BaseRollupInputs {
using fr = typename NCT::fr;
using boolean = typename NCT::boolean;

std::array<PreviousKernelData<NCT>, 2> kernel_data{};
std::array<PreviousKernelData<NCT>, KERNELS_PER_BASE_ROLLUP> kernel_data{};

AppendOnlyTreeSnapshot<NCT> start_private_data_tree_snapshot{};
AppendOnlyTreeSnapshot<NCT> start_nullifier_tree_snapshot{};
AppendOnlyTreeSnapshot<NCT> start_contract_tree_snapshot{};
fr start_public_data_tree_root{};
AppendOnlyTreeSnapshot<NCT> start_historic_blocks_tree_snapshot{};

std::array<NullifierLeafPreimage<NCT>, 2 * MAX_NEW_NULLIFIERS_PER_TX> low_nullifier_leaf_preimages{};
std::array<MembershipWitness<NCT, NULLIFIER_TREE_HEIGHT>, 2 * MAX_NEW_NULLIFIERS_PER_TX>
std::array<NullifierLeafPreimage<NCT>, MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP> low_nullifier_leaf_preimages{};
std::array<MembershipWitness<NCT, NULLIFIER_TREE_HEIGHT>, MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP>
low_nullifier_membership_witness{};

// For inserting the new subtrees into their respective trees:
// Note: the insertion leaf index can be derived from the above snapshots' `next_available_leaf_index` values.
std::array<fr, PRIVATE_DATA_SUBTREE_SIBLING_PATH_LENGTH> new_commitments_subtree_sibling_path{};
std::array<fr, NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH> new_nullifiers_subtree_sibling_path{};
std::array<fr, CONTRACT_SUBTREE_SIBLING_PATH_LENGTH> new_contracts_subtree_sibling_path{};
std::array<std::array<fr, PUBLIC_DATA_TREE_HEIGHT>, 2 * MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX>
std::array<std::array<fr, PUBLIC_DATA_TREE_HEIGHT>, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_BASE_ROLLUP>
new_public_data_update_requests_sibling_paths{};
std::array<std::array<fr, PUBLIC_DATA_TREE_HEIGHT>, 2 * MAX_PUBLIC_DATA_READS_PER_TX>
std::array<std::array<fr, PUBLIC_DATA_TREE_HEIGHT>, MAX_PUBLIC_DATA_READS_PER_BASE_ROLLUP>
new_public_data_reads_sibling_paths{};

std::array<MembershipWitness<NCT, HISTORIC_BLOCKS_TREE_HEIGHT>, 2> historic_blocks_tree_root_membership_witnesses{};
std::array<MembershipWitness<NCT, HISTORIC_BLOCKS_TREE_HEIGHT>, KERNELS_PER_BASE_ROLLUP>
historic_blocks_tree_root_membership_witnesses{};

ConstantRollupData<NCT> constants{};

Expand All @@ -56,7 +58,11 @@ template <typename NCT> struct BaseRollupInputs {
new_public_data_reads_sibling_paths,
historic_blocks_tree_root_membership_witnesses,
constants);
bool operator==(BaseRollupInputs<NCT> const&) const = default;

boolean operator==(BaseRollupInputs<NCT> const& other) const
{
return msgpack_derived_equals<boolean>(*this, other);
};
};

} // namespace aztec3::circuits::abis
160 changes: 81 additions & 79 deletions circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,67 +64,57 @@ class base_rollup_tests : public ::testing::Test {
protected:
static void SetUpTestSuite() { barretenberg::srs::init_crs_factory("../barretenberg/cpp/srs_db/ignition"); }

static void run_cbind(BaseRollupInputs& base_rollup_inputs,
BaseOrMergeRollupPublicInputs& expected_public_inputs,
bool compare_pubins = true,
bool assert_no_circuit_failure = true)
{
info("Retesting via cbinds....");
// TODO(banks12) might be able to get rid of proving key buffer
uint8_t const* pk_buf = nullptr;
size_t const pk_size = base_rollup__init_proving_key(&pk_buf);
(void)pk_size;
// info("Proving key size: ", pk_size);

// TODO(banks12) might be able to get rid of verification key buffer
uint8_t const* vk_buf = nullptr;
size_t const vk_size = base_rollup__init_verification_key(pk_buf, &vk_buf);
(void)vk_size;
// info("Verification key size: ", vk_size);

std::vector<uint8_t> base_rollup_inputs_vec;
serialize::write(base_rollup_inputs_vec, base_rollup_inputs);

// uint8_t const* proof_data;
// size_t proof_data_size;
uint8_t const* public_inputs_buf = nullptr;
size_t public_inputs_size = 0;
// info("simulating circuit via cbind");
uint8_t* const circuit_failure_ptr =
base_rollup__sim(base_rollup_inputs_vec.data(), &public_inputs_size, &public_inputs_buf);

ASSERT_TRUE(assert_no_circuit_failure ? circuit_failure_ptr == nullptr : circuit_failure_ptr != nullptr);
// info("Proof size: ", proof_data_size);
// info("PublicInputs size: ", public_inputs_size);

if (compare_pubins) {
BaseOrMergeRollupPublicInputs public_inputs;
uint8_t const* public_inputs_buf_tmp = public_inputs_buf;
serialize::read(public_inputs_buf_tmp, public_inputs);
ASSERT_EQ(public_inputs.calldata_hash.size(), expected_public_inputs.calldata_hash.size());
for (size_t i = 0; i < public_inputs.calldata_hash.size(); i++) {
ASSERT_EQ(public_inputs.calldata_hash[i], expected_public_inputs.calldata_hash[i]);
}

std::vector<uint8_t> expected_public_inputs_vec;
serialize::write(expected_public_inputs_vec, expected_public_inputs);

ASSERT_EQ(public_inputs_size, expected_public_inputs_vec.size());
// Just compare the first 10 bytes of the serialized public outputs
if (public_inputs_size > 10) {
// for (size_t 0; i < public_inputs_size; i++) {
for (size_t i = 0; i < 10; i++) {
ASSERT_EQ(public_inputs_buf[i], expected_public_inputs_vec[i]);
}
}
}

free((void*)pk_buf);
free((void*)vk_buf);
// free((void*)proof_data);
free((void*)public_inputs_buf);
// info("finished retesting via cbinds...");
}
// TODO(1998): uncomment once https://github.com/AztecProtocol/aztec-packages/issues/1998 is solved and
// use new pattern such as call_func_and_wrapper from test_helper.hpp

// static void run_cbind(BaseRollupInputs& base_rollup_inputs,
// BaseOrMergeRollupPublicInputs& expected_public_inputs,
// bool compare_pubins = true,
// bool assert_no_circuit_failure = true)
// {
// info("Retesting via cbinds....");

// std::vector<uint8_t> base_rollup_inputs_vec;
// serialize::write(base_rollup_inputs_vec, base_rollup_inputs);

// // uint8_t const* proof_data;
// // size_t proof_data_size;
// uint8_t const* public_inputs_buf = nullptr;
// size_t public_inputs_size = 0;
// // info("simulating circuit via cbind");
// uint8_t* const circuit_failure_ptr =
// base_rollup__sim(base_rollup_inputs_vec.data(), &public_inputs_size, &public_inputs_buf);

// ASSERT_TRUE(assert_no_circuit_failure ? circuit_failure_ptr == nullptr : circuit_failure_ptr != nullptr);
// // info("Proof size: ", proof_data_size);
// // info("PublicInputs size: ", public_inputs_size);

// if (compare_pubins) {
// BaseOrMergeRollupPublicInputs public_inputs;
// uint8_t const* public_inputs_buf_tmp = public_inputs_buf;
// serialize::read(public_inputs_buf_tmp, public_inputs);
// ASSERT_EQ(public_inputs.calldata_hash.size(), expected_public_inputs.calldata_hash.size());
// for (size_t i = 0; i < public_inputs.calldata_hash.size(); i++) {
// ASSERT_EQ(public_inputs.calldata_hash[i], expected_public_inputs.calldata_hash[i]);
// }

// std::vector<uint8_t> expected_public_inputs_vec;
// serialize::write(expected_public_inputs_vec, expected_public_inputs);

// ASSERT_EQ(public_inputs_size, expected_public_inputs_vec.size());
// // Just compare the first 10 bytes of the serialized public outputs
// if (public_inputs_size > 10) {
// // for (size_t 0; i < public_inputs_size; i++) {
// for (size_t i = 0; i < 10; i++) {
// ASSERT_EQ(public_inputs_buf[i], expected_public_inputs_vec[i]);
// }
// }
// }

// // free((void*)proof_data);
// free((void*)public_inputs_buf);
// // info("finished retesting via cbinds...");
// }
};

TEST_F(base_rollup_tests, native_no_new_contract_leafs)
Expand Down Expand Up @@ -154,7 +144,8 @@ TEST_F(base_rollup_tests, native_no_new_contract_leafs)
ASSERT_EQ(outputs.end_contract_tree_snapshot, expectedEndContractTreeSnapshot);
ASSERT_EQ(outputs.start_contract_tree_snapshot, emptyInputs.start_contract_tree_snapshot);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(emptyInputs, outputs);
// TODO(1998): see above
// run_cbind(emptyInputs, outputs);
}

TEST_F(base_rollup_tests, native_contract_leaf_inserted)
Expand Down Expand Up @@ -199,7 +190,8 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted)
ASSERT_EQ(outputs.start_contract_tree_snapshot, inputs.start_contract_tree_snapshot);
ASSERT_EQ(outputs.end_contract_tree_snapshot, expected_end_contracts_snapshot);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_contract_leaf_inserted_in_non_empty_snapshot_tree)
Expand Down Expand Up @@ -255,7 +247,8 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted_in_non_empty_snapshot_tr
ASSERT_EQ(outputs.start_contract_tree_snapshot, inputs.start_contract_tree_snapshot);
ASSERT_EQ(outputs.end_contract_tree_snapshot, expected_end_contracts_snapshot);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_new_commitments_tree)
Expand Down Expand Up @@ -297,7 +290,8 @@ TEST_F(base_rollup_tests, native_new_commitments_tree)
ASSERT_EQ(outputs.start_private_data_tree_snapshot, inputs.start_private_data_tree_snapshot);
ASSERT_EQ(outputs.end_private_data_tree_snapshot, expected_end_commitments_snapshot);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

template <size_t N> NT::fr calc_root(NT::fr leaf, NT::uint32 leafIndex, std::array<NT::fr, N> siblingPath)
Expand Down Expand Up @@ -477,8 +471,8 @@ TEST_F(base_rollup_tests, native_nullifier_tree_regression)
// This test runs after some data has already been inserted into the tree
// This test will pre-populate the tree with 6 * KERNEL_NEW_NULLIFIERS_LENGTH values (0 item + 6 *
// KERNEL_NEW_NULLIFIERS_LENGTH -1 more) simulating that a rollup inserting two random values has already
// succeeded. Note that this corresponds to 3 (1 already initialized and 2 new ones) base rollups. This rollup then
// adds two further random values that will end up having their low nullifiers point at each other
// succeeded. Note that this corresponds to 3 (1 already initialized and 2 new ones) base rollups. This rollup
// then adds two further random values that will end up having their low nullifiers point at each other
std::vector<fr> initial_values(6 * MAX_NEW_NULLIFIERS_PER_TX - 1, 0);
for (size_t i = 0; i < 2 * MAX_NEW_NULLIFIERS_PER_TX - 1; i++) {
initial_values[i] = i + 1;
Expand Down Expand Up @@ -585,14 +579,14 @@ TEST_F(base_rollup_tests, native_empty_block_calldata_hash)
ASSERT_TRUE(compare_field_hash_to_expected(output_calldata_hash, expected_calldata_hash) == true);

ASSERT_FALSE(builder.failed()) << builder.failure_msgs;

run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_calldata_hash)
{
// Execute the base rollup circuit with nullifiers, commitments and a contract deployment. Then check the calldata
// hash against the expected value.
// Execute the base rollup circuit with nullifiers, commitments and a contract deployment. Then check the
// calldata hash against the expected value.
std::array<PreviousKernelData<NT>, 2> kernel_data = { get_empty_kernel(), get_empty_kernel() };

// Commitments inserted are [1,2,3,4,5,6,7,8 ...]. Nullifiers inserted are [8,9,10,11,12,13,14,15 ...]
Expand Down Expand Up @@ -630,7 +624,8 @@ TEST_F(base_rollup_tests, native_calldata_hash)
ASSERT_EQ(expected_calldata_hash, output_calldata_hash);

ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_compute_membership_historic_blocks_tree_negative)
Expand Down Expand Up @@ -676,7 +671,8 @@ TEST_F(base_rollup_tests, native_constants_dont_change)
aztec3::circuits::rollup::native_base_rollup::base_rollup_circuit(builder, inputs);
ASSERT_EQ(inputs.constants, outputs.constants);
EXPECT_FALSE(builder.failed());
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_constants_dont_match_kernels_chain_id)
Expand Down Expand Up @@ -730,7 +726,8 @@ TEST_F(base_rollup_tests, native_cbind_0)
// @todo Error handling?
BaseRollupInputs inputs = base_rollup_inputs_from_kernels({ get_empty_kernel(), get_empty_kernel() });
BaseOrMergeRollupPublicInputs ignored_public_inputs;
run_cbind(inputs, ignored_public_inputs, false);
// TODO(1998): see above
// run_cbind(inputs, ignored_public_inputs, false);
}

TEST_F(base_rollup_tests, native_single_public_state_read)
Expand Down Expand Up @@ -765,7 +762,8 @@ TEST_F(base_rollup_tests, native_single_public_state_read)
ASSERT_EQ(outputs.end_public_data_tree_root, public_data_tree.root());
ASSERT_EQ(outputs.end_public_data_tree_root, outputs.start_public_data_tree_root);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_single_public_state_write)
Expand Down Expand Up @@ -803,7 +801,8 @@ TEST_F(base_rollup_tests, native_single_public_state_write)
ASSERT_EQ(outputs.end_public_data_tree_root, public_data_tree.root());
ASSERT_NE(outputs.end_public_data_tree_root, outputs.start_public_data_tree_root);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
Expand All @@ -823,7 +822,8 @@ TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)

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

// We set up reads and writes such that the right tx will read or write to indices already modified by the left tx
// We set up reads and writes such that the right tx will read or write to indices already modified by the left
// tx
kernel_data[0].public_inputs.end.public_data_reads[0] = make_public_read(fr(1), fr(101));
kernel_data[0].public_inputs.end.public_data_reads[1] = make_public_read(fr(2), fr(102));
kernel_data[0].public_inputs.end.public_data_update_requests[0] =
Expand All @@ -850,7 +850,8 @@ TEST_F(base_rollup_tests, native_multiple_public_state_read_writes)
ASSERT_EQ(outputs.end_public_data_tree_root, public_data_tree.root());
ASSERT_NE(outputs.end_public_data_tree_root, outputs.start_public_data_tree_root);
ASSERT_FALSE(builder.failed()) << builder.failure_msgs;
run_cbind(inputs, outputs);
// TODO(1998): see above
// run_cbind(inputs, outputs);
}

TEST_F(base_rollup_tests, native_invalid_public_state_read)
Expand Down Expand Up @@ -889,7 +890,8 @@ TEST_F(base_rollup_tests, native_invalid_public_state_read)
ASSERT_EQ(outputs.end_public_data_tree_root, public_data_tree.root());
ASSERT_EQ(outputs.end_public_data_tree_root, outputs.start_public_data_tree_root);
ASSERT_TRUE(builder.failed());
run_cbind(inputs, outputs, true, false);
// TODO(1998): see above
// run_cbind(inputs, outputs, true, false);
}

} // namespace aztec3::circuits::rollup::base::native_base_rollup_circuit
Loading