From 055f5f59805db1114d912f16ba1f3bfb41f4774c Mon Sep 17 00:00:00 2001 From: David Banks <47112877+dbanks12@users.noreply.github.com> Date: Wed, 29 Mar 2023 12:30:04 -0400 Subject: [PATCH] feat(root): Cbinds for root rollup (#132) * intial root * feat: pass on snaphots * fix: update naming for consistency + extend root rollup logic * feat: serialization debug utility (#126) * feat: serialize canary * bump bb * feat: pass along asan settings * Revert "bump bb" This reverts commit 514682dbd3e53bc88ad0f35a3e24393e13a601d0. * feat: pass build type to cmake * initial commit with root cbinds * root rollup cbind working * fix root test --------- Co-authored-by: LHerskind Co-authored-by: ludamad --- .../abis/rollup/root/root_rollup_inputs.hpp | 8 +- cpp/src/aztec3/circuits/rollup/base/.test.cpp | 6 +- cpp/src/aztec3/circuits/rollup/root/.test.cpp | 67 +++++++++++++++ .../aztec3/circuits/rollup/root/c_bind.cpp | 83 +++++++++++++++++++ cpp/src/aztec3/circuits/rollup/root/c_bind.h | 16 ++++ 5 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 cpp/src/aztec3/circuits/rollup/root/c_bind.cpp create mode 100644 cpp/src/aztec3/circuits/rollup/root/c_bind.h 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 index 2e6082da..76efda0d 100644 --- a/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp +++ b/cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp @@ -31,8 +31,8 @@ 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); + read(it, obj.new_historic_private_data_tree_root_sibling_path); + read(it, obj.new_historic_contract_tree_root_sibling_path); }; template void write(std::vector& buf, RootRollupInputs const& obj) @@ -40,8 +40,8 @@ template void write(std::vector& buf, RootRollupInputs std::ostream& operator<<(std::ostream& os, RootRollupInputs const& obj) diff --git a/cpp/src/aztec3/circuits/rollup/base/.test.cpp b/cpp/src/aztec3/circuits/rollup/base/.test.cpp index 9f7bebea..b7bd1bda 100644 --- a/cpp/src/aztec3/circuits/rollup/base/.test.cpp +++ b/cpp/src/aztec3/circuits/rollup/base/.test.cpp @@ -135,13 +135,11 @@ class base_rollup_tests : public ::testing::Test { 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("after assert..."); - // TODO why do the post-write buffers not match? - // something in aggregation object [de]serialization? 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 @@ -160,7 +158,7 @@ class base_rollup_tests : public ::testing::Test { free((void*)vk_buf); // free((void*)proof_data); // SCARY WARNING TODO FIXME why does this free cause issues - // free((void*)public_inputs_buf); + free((void*)public_inputs_buf); info("finished retesting via cbinds..."); } diff --git a/cpp/src/aztec3/circuits/rollup/root/.test.cpp b/cpp/src/aztec3/circuits/rollup/root/.test.cpp index 2741072a..6a6c1a1a 100644 --- a/cpp/src/aztec3/circuits/rollup/root/.test.cpp +++ b/cpp/src/aztec3/circuits/rollup/root/.test.cpp @@ -17,6 +17,8 @@ #include "barretenberg/stdlib/merkle_tree/memory_tree.hpp" #include "index.hpp" #include "init.hpp" +#include "c_bind.h" + #include "aztec3/circuits/rollup/base/native_base_rollup_circuit.hpp" #include #include @@ -108,9 +110,71 @@ namespace aztec3::circuits::rollup::root::native_root_rollup_circuit { class root_rollup_tests : public ::testing::Test { protected: + void run_cbind(RootRollupInputs& root_rollup_inputs, + RootRollupPublicInputs& expected_public_inputs, + bool compare_pubins = true) + { + // TODO might be able to get rid of proving key buffer + uint8_t const* pk_buf; + size_t pk_size = root_rollup__init_proving_key(&pk_buf); + info("Proving key size: ", pk_size); + + // TODO might be able to get rid of verification key buffer + uint8_t const* vk_buf; + size_t vk_size = root_rollup__init_verification_key(pk_buf, &vk_buf); + info("Verification key size: ", vk_size); + + std::vector root_rollup_inputs_vec; + write(root_rollup_inputs_vec, root_rollup_inputs); + + // uint8_t const* proof_data; + // size_t proof_data_size; + uint8_t const* public_inputs_buf; + info("creating proof"); + size_t public_inputs_size = root_rollup__sim(root_rollup_inputs_vec.data(), &public_inputs_buf); + // info("Proof size: ", proof_data_size); + info("PublicInputs size: ", public_inputs_size); + + if (compare_pubins) { + RootRollupPublicInputs 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]); + } + } + } + (void)root_rollup_inputs; // unused + (void)expected_public_inputs; // unused + (void)compare_pubins; // unused + + free((void*)pk_buf); + free((void*)vk_buf); + // free((void*)proof_data); + free((void*)public_inputs_buf); + info("finished retesting via cbinds..."); + } + protected: BaseRollupInputs getEmptyBaseRollupInputs() { + // TODO standardize function naming ConstantRollupData constantRollupData = ConstantRollupData::empty(); std::array, 2 * KERNEL_NEW_NULLIFIERS_LENGTH> low_nullifier_leaf_preimages; @@ -227,6 +291,7 @@ TEST_F(root_rollup_tests, calldata_hash_empty_blocks) } ASSERT_EQ(hash, calldata_hash); + run_cbind(inputs, outputs, true); } template @@ -344,6 +409,8 @@ TEST_F(root_rollup_tests, blabber) std::cout << "data root: " << data_tree.root() << std::endl; std::cout << r2.base_rollup_public_inputs.end_private_data_tree_snapshot << std::endl; + + run_cbind(rootRollupInputs, outputs, true); } } // namespace aztec3::circuits::rollup::root::native_root_rollup_circuit \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/c_bind.cpp b/cpp/src/aztec3/circuits/rollup/root/c_bind.cpp new file mode 100644 index 00000000..6bf4fa76 --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/c_bind.cpp @@ -0,0 +1,83 @@ +#include "index.hpp" +#include "init.hpp" +#include "c_bind.h" + +#include +#include +#include "aztec3/circuits/abis/signed_tx_request.hpp" +#include "aztec3/circuits/abis/private_kernel/private_call_data.hpp" +#include +#include +#include + +#include "barretenberg/srs/reference_string/env_reference_string.hpp" + +#include "barretenberg/common/serialize.hpp" +#include "barretenberg/plonk/composer/turbo_composer.hpp" + +namespace { +using NT = aztec3::utils::types::NativeTypes; +using aztec3::circuits::rollup::native_root_rollup::root_rollup_circuit; +using aztec3::circuits::rollup::native_root_rollup::RootRollupInputs; +using aztec3::circuits::rollup::native_root_rollup::RootRollupPublicInputs; + +using plonk::TurboComposer; +using namespace plonk::stdlib::types; +} // namespace + +#define WASM_EXPORT __attribute__((visibility("default"))) +// WASM Cbinds +extern "C" { + +WASM_EXPORT size_t root_rollup__init_proving_key(uint8_t const** pk_buf) +{ + std::vector 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 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 size_t root_rollup__sim(uint8_t const* root_rollup_inputs_buf, + uint8_t const** root_rollup_public_inputs_buf) +{ + RootRollupInputs root_rollup_inputs; + read(root_rollup_inputs_buf, root_rollup_inputs); + + RootRollupPublicInputs public_inputs = root_rollup_circuit(root_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()); + *root_rollup_public_inputs_buf = raw_public_inputs_buf; + + return public_inputs_vec.size(); +} + +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 true; +} + +} // extern "C" \ No newline at end of file diff --git a/cpp/src/aztec3/circuits/rollup/root/c_bind.h b/cpp/src/aztec3/circuits/rollup/root/c_bind.h new file mode 100644 index 00000000..e05f2e53 --- /dev/null +++ b/cpp/src/aztec3/circuits/rollup/root/c_bind.h @@ -0,0 +1,16 @@ +#include +#include + +#define WASM_EXPORT __attribute__((visibility("default"))) + +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 size_t root_rollup__sim(uint8_t const* root_rollup_inputs_buf, + 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); + +} \ No newline at end of file