From 633fc180abc9ac8c32331ba7ae9e8bedebebe4da Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Thu, 13 Apr 2023 02:31:01 +0100 Subject: [PATCH] add cbinds for merge (#227) --- .../aztec3/circuits/rollup/merge/.test.cpp | 52 ++++++++++++++++++- .../aztec3/circuits/rollup/merge/c_bind.cpp | 37 +++++++++++++ cpp/src/aztec3/circuits/rollup/merge/c_bind.h | 10 ++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 cpp/src/aztec3/circuits/rollup/merge/c_bind.cpp create mode 100644 cpp/src/aztec3/circuits/rollup/merge/c_bind.h diff --git a/cpp/src/aztec3/circuits/rollup/merge/.test.cpp b/cpp/src/aztec3/circuits/rollup/merge/.test.cpp index 73b6ee14813..ecfa5171635 100644 --- a/cpp/src/aztec3/circuits/rollup/merge/.test.cpp +++ b/cpp/src/aztec3/circuits/rollup/merge/.test.cpp @@ -2,6 +2,7 @@ #include #include "aztec3/circuits/rollup/merge/init.hpp" #include "aztec3/circuits/rollup/merge/utils.hpp" +#include "c_bind.h" namespace { using aztec3::circuits::rollup::merge::utils::dummy_merge_rollup_inputs_with_vk_proof; @@ -14,7 +15,49 @@ using DummyComposer = aztec3::utils::DummyComposer; } // namespace namespace aztec3::circuits::rollup::merge::native_merge_rollup_circuit { -class merge_rollup_tests : public ::testing::Test {}; +class merge_rollup_tests : public ::testing::Test { + protected: + void run_cbind(MergeRollupInputs& merge_rollup_inputs, + BaseOrMergeRollupPublicInputs& expected_public_inputs, + bool compare_pubins = true) + { + std::vector merge_rollup_inputs_vec; + write(merge_rollup_inputs_vec, merge_rollup_inputs); + + uint8_t const* public_inputs_buf; + info("creating proof"); + size_t public_inputs_size = merge_rollup__sim(merge_rollup_inputs_vec.data(), &public_inputs_buf); + info("PublicInputs size: ", public_inputs_size); + + if (compare_pubins) { + BaseOrMergeRollupPublicInputs public_inputs; + info("about to read..."); + uint8_t const* public_inputs_buf_tmp = public_inputs_buf; + read(public_inputs_buf_tmp, public_inputs); + info("about to assert..."); + 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]); + } + + info("about to write expected..."); + std::vector expected_public_inputs_vec; + write(expected_public_inputs_vec, expected_public_inputs); + + info("about to assert buffers eq..."); + 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); + info("finished retesting via cbinds..."); + } +}; TEST_F(merge_rollup_tests, test_different_rollup_type_fails) { @@ -189,4 +232,11 @@ TEST_F(merge_rollup_tests, test_aggregate) ASSERT_EQ(inputs.previous_rollup_data[0].base_or_merge_rollup_public_inputs.end_aggregation_object.public_inputs, outputs.end_aggregation_object.public_inputs); } + +TEST_F(merge_rollup_tests, test_merge_cbind) +{ + MergeRollupInputs inputs = dummy_merge_rollup_inputs_with_vk_proof(); + BaseOrMergeRollupPublicInputs ignored_public_inputs; + run_cbind(inputs, ignored_public_inputs, false); +} } // namespace aztec3::circuits::rollup::merge::native_merge_rollup_circuit diff --git a/cpp/src/aztec3/circuits/rollup/merge/c_bind.cpp b/cpp/src/aztec3/circuits/rollup/merge/c_bind.cpp new file mode 100644 index 00000000000..1e19e95a09e --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/merge/c_bind.cpp @@ -0,0 +1,37 @@ +#include "aztec3/utils/dummy_composer.hpp" +#include "index.hpp" +#include "init.hpp" +#include "c_bind.h" + +namespace { +using NT = aztec3::utils::types::NativeTypes; +using DummyComposer = aztec3::utils::DummyComposer; +using aztec3::circuits::abis::BaseOrMergeRollupPublicInputs; +using aztec3::circuits::abis::MergeRollupInputs; +using aztec3::circuits::rollup::native_merge_rollup::merge_rollup_circuit; + +} // namespace +#define WASM_EXPORT __attribute__((visibility("default"))) +// WASM Cbinds +extern "C" { + +WASM_EXPORT size_t merge_rollup__sim(uint8_t const* merge_rollup_inputs_buf, + uint8_t const** merge_rollup_public_inputs_buf) +{ + DummyComposer composer = DummyComposer(); + MergeRollupInputs merge_rollup_inputs; + read(merge_rollup_inputs_buf, merge_rollup_inputs); + + BaseOrMergeRollupPublicInputs public_inputs = merge_rollup_circuit(composer, merge_rollup_inputs); + + // serialize public inputs to bytes vec + std::vector public_inputs_vec; + 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; + + return public_inputs_vec.size(); +} +} // extern "C" \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/merge/c_bind.h b/cpp/src/aztec3/circuits/rollup/merge/c_bind.h new file mode 100644 index 00000000000..b298f75338a --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/merge/c_bind.h @@ -0,0 +1,10 @@ +#include +#include + +#define WASM_EXPORT __attribute__((visibility("default"))) + +extern "C" { + +WASM_EXPORT size_t merge_rollup__sim(uint8_t const* merge_rollup_inputs_buf, + uint8_t const** merge_rollup_public_inputs_buf); +} \ No newline at end of file