Skip to content

Commit

Permalink
add cbinds for merge (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari authored Apr 13, 2023
1 parent 55a80a4 commit 633fc18
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
52 changes: 51 additions & 1 deletion cpp/src/aztec3/circuits/rollup/merge/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gtest/gtest.h>
#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;
Expand All @@ -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<uint8_t> 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<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]);
}
}
}
free((void*)public_inputs_buf);
info("finished retesting via cbinds...");
}
};

TEST_F(merge_rollup_tests, test_different_rollup_type_fails)
{
Expand Down Expand Up @@ -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
37 changes: 37 additions & 0 deletions cpp/src/aztec3/circuits/rollup/merge/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -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<NT> 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<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());
*merge_rollup_public_inputs_buf = raw_public_inputs_buf;

return public_inputs_vec.size();
}
} // extern "C"
10 changes: 10 additions & 0 deletions cpp/src/aztec3/circuits/rollup/merge/c_bind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <cstdint>
#include <cstddef>

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

0 comments on commit 633fc18

Please sign in to comment.