Skip to content

Commit

Permalink
feat(root): Cbinds for root rollup (#132)
Browse files Browse the repository at this point in the history
* 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 514682d.

* feat: pass build type to cmake

* initial commit with root cbinds

* root rollup cbind working

* fix root test

---------

Co-authored-by: LHerskind <[email protected]>
Co-authored-by: ludamad <[email protected]>
  • Loading branch information
3 people authored Mar 29, 2023
1 parent eeb1381 commit 055f5f5
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ template <typename NCT> void read(uint8_t const*& it, RootRollupInputs<NCT>& 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 <typename NCT> void write(std::vector<uint8_t>& buf, RootRollupInputs<NCT> 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);
write(buf, obj.new_historic_private_data_tree_root_sibling_path);
write(buf, obj.new_historic_contract_tree_root_sibling_path);
};

template <typename NCT> std::ostream& operator<<(std::ostream& os, RootRollupInputs<NCT> const& obj)
Expand Down
6 changes: 2 additions & 4 deletions cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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
Expand All @@ -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...");
}

Expand Down
67 changes: 67 additions & 0 deletions cpp/src/aztec3/circuits/rollup/root/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <aztec3/circuits/apps/test_apps/escrow/deposit.hpp>
#include <aztec3/circuits/apps/test_apps/basic_contract_deployment/basic_contract_deployment.hpp>
Expand Down Expand Up @@ -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<uint8_t> 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<uint8_t> 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<NullifierLeafPreimage<NT>, 2 * KERNEL_NEW_NULLIFIERS_LENGTH> low_nullifier_leaf_preimages;
Expand Down Expand Up @@ -227,6 +291,7 @@ TEST_F(root_rollup_tests, calldata_hash_empty_blocks)
}

ASSERT_EQ(hash, calldata_hash);
run_cbind(inputs, outputs, true);
}

template <size_t N>
Expand Down Expand Up @@ -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
83 changes: 83 additions & 0 deletions cpp/src/aztec3/circuits/rollup/root/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "index.hpp"
#include "init.hpp"
#include "c_bind.h"

#include <aztec3/constants.hpp>
#include <aztec3/utils/types/native_types.hpp>
#include "aztec3/circuits/abis/signed_tx_request.hpp"
#include "aztec3/circuits/abis/private_kernel/private_call_data.hpp"
#include <aztec3/circuits/abis/private_kernel/private_inputs.hpp>
#include <aztec3/circuits/abis/private_kernel/public_inputs.hpp>
#include <aztec3/circuits/mock/mock_kernel_circuit.hpp>

#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<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 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<uint8_t> 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"
16 changes: 16 additions & 0 deletions cpp/src/aztec3/circuits/rollup/root/c_bind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <cstdint>
#include <cstddef>

#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);

}

0 comments on commit 055f5f5

Please sign in to comment.