Skip to content

Commit

Permalink
chore(circuits): merge and root rollup cbind msgpack (#2192)
Browse files Browse the repository at this point in the history
Overview of changes
---------------------

- Use CBIND macro for merge_rollup__sim and root_rollup__sim
- Data structure of the inputs/outputs of these function need to be
reconciled between C++ and TS (member names need to match as
circuits.gen.ts will require reference to TS data structure generated
from the C++ counterparts. In addition, migration from array to TS tuple
is required.)
- Activate new code path in the upper layer (remove wrapper functions in
rollup_wasm_wrapper and directly use mergeRollupSim and rootRollupSim
from circuits.gen.ts).


Resolves #2116
Resolves #2117


# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [x] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [x] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [x] Every change is related to the PR description.
- [x] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
jeanmon authored Sep 12, 2023
1 parent a7ffa2d commit 4f3ecee
Show file tree
Hide file tree
Showing 28 changed files with 994 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ template <typename NCT> struct RootRollupInputs {
std::array<PreviousRollupData<NCT>, 2> previous_rollup_data{};

// 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_SIBLING_PATH_LENGTH> new_l1_to_l2_message_tree_root_sibling_path{};
std::array<fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP> new_l1_to_l2_messages{};
std::array<fr, L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH> new_l1_to_l2_messages_tree_root_sibling_path{};

AppendOnlyTreeSnapshot<NCT> start_l1_to_l2_message_tree_snapshot{};
AppendOnlyTreeSnapshot<NCT> start_l1_to_l2_messages_tree_snapshot{};

// inputs required to add the block hash
AppendOnlyTreeSnapshot<NCT> start_historic_blocks_tree_snapshot{};
std::array<fr, HISTORIC_BLOCKS_TREE_HEIGHT> new_historic_blocks_tree_sibling_path{};

// For serialization, update with new fields
MSGPACK_FIELDS(previous_rollup_data,
l1_to_l2_messages,
new_l1_to_l2_message_tree_root_sibling_path,
start_l1_to_l2_message_tree_snapshot,
new_l1_to_l2_messages,
new_l1_to_l2_messages_tree_root_sibling_path,
start_l1_to_l2_messages_tree_snapshot,
start_historic_blocks_tree_snapshot,
new_historic_blocks_tree_sibling_path);
bool operator==(RootRollupInputs<NCT> const&) const = default;
Expand Down
87 changes: 46 additions & 41 deletions circuits/cpp/src/aztec3/circuits/rollup/merge/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,49 @@ class merge_rollup_tests : public ::testing::Test {
protected:
static void SetUpTestSuite() { barretenberg::srs::init_crs_factory("../barretenberg/cpp/srs_db/ignition"); }

static void run_cbind(MergeRollupInputs& merge_rollup_inputs,
BaseOrMergeRollupPublicInputs& expected_public_inputs,
bool compare_pubins = true)
{
info("Retesting via cbinds....");
std::vector<uint8_t> merge_rollup_inputs_vec;
serialize::write(merge_rollup_inputs_vec, merge_rollup_inputs);

uint8_t const* public_inputs_buf = nullptr;
// info("simulating circuit via cbind");
size_t public_inputs_size = 0;
info("creating proof");
auto* circuit_failure_ptr =
merge_rollup__sim(merge_rollup_inputs_vec.data(), &public_inputs_size, &public_inputs_buf);
ASSERT_TRUE(circuit_failure_ptr == nullptr);
// 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*)public_inputs_buf);
}
// 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(MergeRollupInputs& merge_rollup_inputs,
// BaseOrMergeRollupPublicInputs& expected_public_inputs,
// bool compare_pubins = true)
// {
// info("Retesting via cbinds....");
// std::vector<uint8_t> merge_rollup_inputs_vec;
// serialize::write(merge_rollup_inputs_vec, merge_rollup_inputs);

// uint8_t const* public_inputs_buf = nullptr;
// // info("simulating circuit via cbind");
// size_t public_inputs_size = 0;
// info("creating proof");
// auto* circuit_failure_ptr =
// merge_rollup__sim(merge_rollup_inputs_vec.data(), &public_inputs_size, &public_inputs_buf);
// ASSERT_TRUE(circuit_failure_ptr == nullptr);
// // 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*)public_inputs_buf);
// }
};

TEST_F(merge_rollup_tests, native_different_rollup_type_fails)
Expand Down Expand Up @@ -299,6 +302,8 @@ TEST_F(merge_rollup_tests, native_merge_cbind)

ASSERT_FALSE(builder.failed());
BaseOrMergeRollupPublicInputs ignored_public_inputs;
run_cbind(inputs, ignored_public_inputs, false);

// TODO(1998): see above
// run_cbind(inputs, ignored_public_inputs, false);
}
} // namespace aztec3::circuits::rollup::merge::native_merge_rollup_circuit
26 changes: 4 additions & 22 deletions circuits/cpp/src/aztec3/circuits/rollup/merge/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,14 @@
namespace {
using NT = aztec3::utils::types::NativeTypes;
using DummyCircuitBuilder = aztec3::utils::DummyCircuitBuilder;
using aztec3::circuits::abis::BaseOrMergeRollupPublicInputs;
using aztec3::circuits::abis::MergeRollupInputs;
using aztec3::circuits::rollup::merge::merge_rollup_circuit;
} // namespace

// WASM Cbinds
extern "C" {

WASM_EXPORT uint8_t* merge_rollup__sim(uint8_t const* merge_rollup_inputs_buf,
size_t* merge_rollup_public_inputs_size_out,
uint8_t const** merge_rollup_public_inputs_buf)
{
CBIND(merge_rollup__sim, [](MergeRollupInputs<NT> const& merge_rollup_inputs) {
DummyCircuitBuilder builder = DummyCircuitBuilder("merge_rollup__sim");
MergeRollupInputs<NT> merge_rollup_inputs;
serialize::read(merge_rollup_inputs_buf, merge_rollup_inputs);

BaseOrMergeRollupPublicInputs const public_inputs = merge_rollup_circuit(builder, merge_rollup_inputs);

// serialize public inputs to bytes vec
std::vector<uint8_t> public_inputs_vec;
serialize::write(public_inputs_vec, public_inputs);
// copy public inputs to output buffer
auto* raw_public_inputs_buf = (uint8_t*)malloc(public_inputs_vec.size());
memcpy(raw_public_inputs_buf, (void*)public_inputs_vec.data(), public_inputs_vec.size());
*merge_rollup_public_inputs_buf = raw_public_inputs_buf;
*merge_rollup_public_inputs_size_out = public_inputs_vec.size();
return builder.alloc_and_serialize_first_failure();
}
} // extern "C"
auto const& public_inputs = merge_rollup_circuit(builder, merge_rollup_inputs);
return builder.result_or_error(public_inputs);
});
7 changes: 1 addition & 6 deletions circuits/cpp/src/aztec3/circuits/rollup/merge/c_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
#include <cstddef>
#include <cstdint>

extern "C" {

WASM_EXPORT uint8_t* merge_rollup__sim(uint8_t const* merge_rollup_inputs_buf,
size_t* merge_rollup_public_inputs_size_out,
uint8_t const** merge_rollup_public_inputs_buf);
}
CBIND_DECL(merge_rollup__sim);
113 changes: 53 additions & 60 deletions circuits/cpp/src/aztec3/circuits/rollup/root/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,64 +64,55 @@ class root_rollup_tests : public ::testing::Test {
protected:
static void SetUpTestSuite() { barretenberg::srs::init_crs_factory("../barretenberg/cpp/srs_db/ignition"); }

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

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

std::vector<uint8_t> root_rollup_inputs_vec;
serialize::write(root_rollup_inputs_vec, root_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 =
root_rollup__sim(root_rollup_inputs_vec.data(), &public_inputs_size, &public_inputs_buf);
ASSERT_TRUE(circuit_failure_ptr == nullptr);
// info("Proof size: ", proof_data_size);
// info("PublicInputs size: ", public_inputs_size);

if (compare_pubins) {
RootRollupPublicInputs 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);
}
// 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(RootRollupInputs& root_rollup_inputs,
// RootRollupPublicInputs& expected_public_inputs,
// bool compare_pubins = true)
// {
// info("Retesting via cbinds....");
// // info("Verification key size: ", vk_size);

// std::vector<uint8_t> root_rollup_inputs_vec;
// serialize::write(root_rollup_inputs_vec, root_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 =
// root_rollup__sim(root_rollup_inputs_vec.data(), &public_inputs_size, &public_inputs_buf);
// ASSERT_TRUE(circuit_failure_ptr == nullptr);
// // info("Proof size: ", proof_data_size);
// // info("PublicInputs size: ", public_inputs_size);

// if (compare_pubins) {
// RootRollupPublicInputs 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);
// }
};

TEST_F(root_rollup_tests, native_check_block_hashes_empty_blocks)
Expand Down Expand Up @@ -161,7 +152,8 @@ TEST_F(root_rollup_tests, native_check_block_hashes_empty_blocks)

EXPECT_FALSE(builder.failed());

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

TEST_F(root_rollup_tests, native_root_missing_nullifier_logic)
Expand Down Expand Up @@ -312,7 +304,8 @@ TEST_F(root_rollup_tests, native_root_missing_nullifier_logic)

EXPECT_FALSE(builder.failed());

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

} // namespace aztec3::circuits::rollup::root::native_root_rollup_circuit
59 changes: 4 additions & 55 deletions circuits/cpp/src/aztec3/circuits/rollup/root/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,8 @@ using aztec3::circuits::rollup::native_root_rollup::RootRollupPublicInputs;
} // namespace

// WASM Cbinds
extern "C" {

WASM_EXPORT size_t root_rollup__init_proving_key(uint8_t const** pk_buf)
{
std::vector<uint8_t> pk_vec(42, 0);

auto* raw_buf = (uint8_t*)malloc(pk_vec.size());
memcpy(raw_buf, (void*)pk_vec.data(), pk_vec.size());
*pk_buf = raw_buf;

return pk_vec.size();
}

WASM_EXPORT size_t root_rollup__init_verification_key(uint8_t const* pk_buf, uint8_t const** vk_buf)
{
std::vector<uint8_t> vk_vec(42, 0);
// TODO remove when proving key is used
(void)pk_buf; // unused

auto* raw_buf = (uint8_t*)malloc(vk_vec.size());
memcpy(raw_buf, (void*)vk_vec.data(), vk_vec.size());
*vk_buf = raw_buf;

return vk_vec.size();
}

WASM_EXPORT uint8_t* root_rollup__sim(uint8_t const* root_rollup_inputs_buf,
size_t* root_rollup_public_inputs_size_out,
uint8_t const** root_rollup_public_inputs_buf)
{
RootRollupInputs root_rollup_inputs;
serialize::read(root_rollup_inputs_buf, root_rollup_inputs);

CBIND(root_rollup__sim, [](RootRollupInputs const& root_rollup_inputs) {
DummyCircuitBuilder builder = DummyCircuitBuilder("root_rollup__sim");
RootRollupPublicInputs const public_inputs = root_rollup_circuit(builder, root_rollup_inputs);

// serialize public inputs to bytes vec
std::vector<uint8_t> public_inputs_vec;
serialize::write(public_inputs_vec, public_inputs);
// copy public inputs to output buffer
auto* raw_public_inputs_buf = (uint8_t*)malloc(public_inputs_vec.size());
memcpy(raw_public_inputs_buf, (void*)public_inputs_vec.data(), public_inputs_vec.size());
*root_rollup_public_inputs_buf = raw_public_inputs_buf;
*root_rollup_public_inputs_size_out = public_inputs_vec.size();
return builder.alloc_and_serialize_first_failure();
}

WASM_EXPORT size_t root_rollup__verify_proof(uint8_t const* vk_buf, uint8_t const* proof, uint32_t length)
{
(void)vk_buf; // unused
(void)proof; // unused
(void)length; // unused
return 1U;
}

} // extern "C"
auto const& public_inputs = root_rollup_circuit(builder, root_rollup_inputs);
return builder.result_or_error(public_inputs);
});
10 changes: 1 addition & 9 deletions circuits/cpp/src/aztec3/circuits/rollup/root/c_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,4 @@
#include <cstddef>
#include <cstdint>

extern "C" {

WASM_EXPORT size_t root_rollup__init_proving_key(uint8_t const** pk_buf);
WASM_EXPORT size_t root_rollup__init_verification_key(uint8_t const* pk_buf, uint8_t const** vk_buf);
WASM_EXPORT uint8_t* root_rollup__sim(uint8_t const* root_rollup_inputs_buf,
size_t* root_rollup_public_inputs_size_out,
uint8_t const** root_rollup_public_inputs_buf);
WASM_EXPORT size_t root_rollup__verify_proof(uint8_t const* vk_buf, uint8_t const* proof, uint32_t length);
}
CBIND_DECL(root_rollup__sim);
Loading

0 comments on commit 4f3ecee

Please sign in to comment.