Skip to content

Commit

Permalink
fix: align bbmalloc implementations (#1513)
Browse files Browse the repository at this point in the history
This was wrong in circuits. This should have never been duplicated (mea
culpa). The implementations drifted with the WASI SDK version 20 push.

Don't think we hit issues with this, but they'd be nasty if we did.
  • Loading branch information
ludamad authored Aug 11, 2023
1 parent 3fb17ec commit c512fcd
Show file tree
Hide file tree
Showing 20 changed files with 14 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// External Barretenberg C++ API
#include "common/container.hpp"
#include "common/map.hpp"
#include "common/mem.hpp"
#include "common/serialize.hpp"
#include "common/streams.hpp"
#include "common/throw_or_abort.hpp"
Expand Down
3 changes: 0 additions & 3 deletions circuits/cpp/barretenberg/cpp/src/barretenberg/common/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "./slab_allocator.hpp"
#include "./wasm_export.hpp"

extern "C" {

WASM_EXPORT void* bbmalloc(size_t size)
{
return barretenberg::get_mem_slab_raw(size);
Expand All @@ -13,4 +11,3 @@ WASM_EXPORT void bbfree(void* ptr)
{
barretenberg::free_mem_slab_raw(ptr);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "log.hpp"
#include "memory.h"
#include "wasm_export.hpp"
#include <memory>
#include <stdlib.h>
// #include <malloc.h>
Expand Down Expand Up @@ -72,4 +73,7 @@ inline void aligned_free(void* mem)
// info("Total allocated space (uordblks): ", minfo.uordblks);
// info("Total free space (fordblks): ", minfo.fordblks);
// info("Top-most, releasable space (keepcost): ", minfo.keepcost);
// }
// }

WASM_EXPORT void* bbmalloc(size_t size);
WASM_EXPORT void bbfree(void* ptr);
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once
#ifdef __clang__
#define WASM_EXPORT extern "C" __attribute__((visibility("default"))) __attribute__((annotate("wasm_export")))
#define ASYNC_WASM_EXPORT \
extern "C" __attribute__((visibility("default"))) __attribute__((annotate("async_wasm_export")))
#else
#define WASM_EXPORT extern "C" __attribute__((visibility("default")))
#define ASYNC_WASM_EXPORT extern "C" __attribute__((visibility("default")))
#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "aes128.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {
#include "barretenberg/common/wasm_export.hpp"

WASM_EXPORT void aes__encrypt_buffer_cbc(uint8_t* in, uint8_t* iv, const uint8_t* key, const size_t length, uint8_t* r)
{
Expand All @@ -19,4 +16,3 @@ WASM_EXPORT void aes__decrypt_buffer_cbc(uint8_t* in, uint8_t* iv, const uint8_t
r[i] = in[i];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/common/wasm_export.hpp"
#include "blake3s.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void blake3s_to_field(uint8_t const* data, size_t length, uint8_t* r)
{
std::vector<uint8_t> inputv(data, data + length);
std::vector<uint8_t> output = blake3::blake3s(inputv);
auto result = barretenberg::fr::serialize_from_buffer(output.data());
barretenberg::fr::serialize_to_buffer(result, r);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "ecdsa.hpp"
#include <barretenberg/ecc/curves/secp256k1/secp256k1.hpp>

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf)
{
auto priv_key = from_buffer<secp256k1::fr>(private_key);
Expand Down Expand Up @@ -67,4 +63,3 @@ WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
return crypto::ecdsa::verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), pubk, sig);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <ecc/curves/secp256k1/secp256k1.hpp>

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {
#include "barretenberg/common/wasm_export.hpp"

WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf);

Expand All @@ -26,4 +23,3 @@ WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
uint8_t const* sig_r,
uint8_t const* sig_s,
uint8_t const* sig_v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include "barretenberg/common/timer.hpp"
#include "pedersen.hpp"
#include "pedersen_lookup.hpp"
#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void pedersen__init()
{
Expand Down Expand Up @@ -100,4 +97,3 @@ WASM_EXPORT void pedersen__buffer_to_field(uint8_t const* data, size_t length, u
auto output = crypto::pedersen_commitment::compress_native(to_compress);
write(r, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/common/streams.hpp"
#include "barretenberg/common/timer.hpp"
#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void pedersen__init();

Expand All @@ -27,4 +24,3 @@ WASM_EXPORT void pedersen_plookup_commit_with_hash_index(uint8_t const* inputs_b
uint32_t hash_index);

WASM_EXPORT void pedersen__buffer_to_field(uint8_t const* data, size_t length, uint8_t* r);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf)
{
auto priv_key = from_buffer<grumpkin::fr>(private_key);
Expand Down Expand Up @@ -137,4 +133,3 @@ WASM_EXPORT bool multisig_combine_signatures(uint8_t const* message,
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "sha256.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {
#include "barretenberg/common/wasm_export.hpp"

WASM_EXPORT void sha256__hash(uint8_t* in, const size_t length, uint8_t* r)
{
Expand All @@ -16,4 +13,3 @@ WASM_EXPORT void sha256__hash(uint8_t* in, const size_t length, uint8_t* r)
r[i] = output[i];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// TODO: Delete this cbind once funcs working in root cbind of ecc module.
#include "grumpkin.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {
#include "barretenberg/common/wasm_export.hpp"

WASM_EXPORT void ecc_grumpkin__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result)
{
Expand Down Expand Up @@ -50,4 +47,3 @@ WASM_EXPORT void ecc_grumpkin__reduce512_buffer_mod_circuit_modulus(uint8_t* inp
uint512_t target_output = bigint_input % barretenberg_modulus;
write(result, target_output.lo);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "secp256k1.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void ecc_secp256k1__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result)
{
using serialize::write;
Expand All @@ -28,4 +24,3 @@ WASM_EXPORT void ecc_secp256k1__reduce512_buffer_mod_circuit_modulus(uint8_t* in
uint512_t target_output = bigint_input % barretenberg_modulus;
write(result, target_output.lo);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#include "secp256k1.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void ecc_secp256k1__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result);

WASM_EXPORT void ecc_secp256k1__get_random_scalar_mod_circuit_modulus(uint8_t* result);

WASM_EXPORT void ecc_secp256k1__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
using namespace barretenberg;
using namespace join_split_example::proofs::join_split;

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT void join_split__init_proving_key(bool mock)
{
init_proving_key(barretenberg::srs::get_crs_factory(), mock);
Expand Down Expand Up @@ -100,4 +96,3 @@ WASM_EXPORT bool join_split__verify_proof(uint8_t* proof, uint32_t length)
plonk::proof pp = { std::vector<uint8_t>(proof, proof + length) };
return verify_proof(pp);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#include <cstdint>

#define WASM_EXPORT __attribute__((visibility("default")))

extern "C" {

WASM_EXPORT uint32_t join_split__get_new_proving_key_data(uint8_t** output);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "prover.hpp"

#define WASM_EXPORT extern "C" __attribute__((visibility("default")))

using namespace barretenberg;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <string>
#include <utility>
#include "barretenberg/common/wasm_export.hpp"
// CBIND forward declarations for msgback default bind format (encode as tuple of args and return value as msgpack
// string)
#define WASM_EXPORT extern "C" __attribute__((visibility("default")))

#define CBIND_DECL(cname) \
WASM_EXPORT void cname( \
Expand Down
8 changes: 0 additions & 8 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@ template <size_t TREE_HEIGHT> void rightfill_with_zeroleaves(std::vector<NT::fr>

} // namespace

// Note: We don't have a simple way of calling the barretenberg c-bind.
// Mimic bbmalloc behaviour.
static void* bbmalloc(size_t size)
{
auto* ptr = aligned_alloc(64, size);
return ptr;
}

/** Copy this string to a bbmalloc'd buffer */
static const char* bbmalloc_copy_string(const char* data, size_t len)
{
Expand Down

0 comments on commit c512fcd

Please sign in to comment.