Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 224 additions & 37 deletions barretenberg/bb_rs/build.rs

Large diffs are not rendered by default.

39 changes: 17 additions & 22 deletions barretenberg/bb_rs/scripts/fix_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,26 @@ def fix_duplicate_types(bindings_file_path):

# Track seen type definitions
seen_types = set()
lines = content.split('\n')
cleaned_lines = []

# Pattern to match type definitions like: pub type <name> = <definition>;
type_def_pattern = re.compile(r'^pub type\s+(\w+)\s*=')

for line in lines:
match = type_def_pattern.match(line.strip())

if match:
type_name = match.group(1)
if type_name in seen_types:
# Skip this duplicate type definition
print(f" Removing duplicate type definition: {type_name}")
continue
else:
# Keep this type definition and mark it as seen
seen_types.add(type_name)
cleaned_lines.append(line)

# Pattern to match type definitions inline: pub type <name> = <definition>;
# This handles both multiline and inline type definitions
type_def_pattern = re.compile(r'pub\s+type\s+(\w+)\s*=\s*([^;]+);')

def replacer(match):
type_name = match.group(1)
if type_name in seen_types:
# Remove this duplicate type definition
print(f" Removing duplicate type definition: {type_name}")
return '' # Remove the entire match
else:
# Keep non-type-definition lines
cleaned_lines.append(line)
# Keep this type definition and mark it as seen
seen_types.add(type_name)
return match.group(0) # Keep the original match

# Replace duplicates
cleaned_content = type_def_pattern.sub(replacer, content)

# Write the cleaned content back
cleaned_content = '\n'.join(cleaned_lines)
with open(bindings_file_path, 'w') as f:
f.write(cleaned_content)

Expand Down
61 changes: 61 additions & 0 deletions barretenberg/bb_rs/src/barretenberg_api/acir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,45 @@ pub unsafe fn acir_prove_ultra_keccak_zk_honk(
.to_vec()
}

// Client IVC prove (proof + vk)
pub unsafe fn acir_prove_aztec_client(ivc_inputs_buf: &[u8]) -> (Vec<u8>, Vec<u8>) {
let mut proof_out_ptr = ptr::null_mut();
let mut vk_out_ptr = ptr::null_mut();
bindgen::acir_prove_aztec_client(
ivc_inputs_buf.to_buffer().as_slice().as_ptr(),
&mut proof_out_ptr,
&mut vk_out_ptr,
);
let proof = Buffer::from_ptr(Buffer::from_ptr(proof_out_ptr).unwrap().to_vec().as_slice().as_ptr())
.unwrap()
.to_vec();
let vk = Buffer::from_ptr(Buffer::from_ptr(vk_out_ptr).unwrap().to_vec().as_slice().as_ptr())
.unwrap()
.to_vec();
(proof, vk)
}

// Client IVC verify
pub unsafe fn acir_verify_aztec_client(proof_buf: &[u8], vkey_buf: &[u8]) -> bool {
let mut result = false;
bindgen::acir_verify_aztec_client(
proof_buf.to_buffer().as_ptr(),
vkey_buf.as_ptr(),
&mut result,
);
result
}

// Client IVC gates API (big-endian u32 array)
pub unsafe fn acir_gates_aztec_client(ivc_inputs_buf: &[u8]) -> Vec<u8> {
let mut out_ptr = ptr::null_mut();
bindgen::acir_gates_aztec_client(
ivc_inputs_buf.to_buffer().as_slice().as_ptr(),
&mut out_ptr,
);
Buffer::from_ptr(out_ptr).unwrap().to_vec()
}

pub unsafe fn acir_get_ultra_honk_verification_key(constraint_system_buf: &[u8]) -> Vec<u8> {
let mut out_ptr = ptr::null_mut();
bindgen::acir_write_vk_ultra_honk(
Expand Down Expand Up @@ -265,6 +304,28 @@ pub unsafe fn acir_serialize_verification_key_into_fields(
(Buffer::from_ptr(out_vkey).unwrap().to_vec(), out_key_hash)
}

pub unsafe fn acir_proof_as_fields_ultra_honk(proof_buf: &[u8]) -> Vec<String> {
from_biguints_to_hex_strings(&pack_proof_into_biguints(&proof_buf))
}

pub unsafe fn acir_vk_as_fields_ultra_honk(vk_buf: &[u8]) -> Vec<u8> {
let mut out_ptr = ptr::null_mut();
bindgen::acir_vk_as_fields_ultra_honk(
vk_buf.as_ptr(),
&mut out_ptr,
);
Buffer::from_ptr(out_ptr).unwrap().to_vec()
}

pub unsafe fn acir_vk_as_fields_mega_honk(vk_buf: &[u8]) -> Vec<u8> {
let mut out_ptr = ptr::null_mut();
bindgen::acir_vk_as_fields_mega_honk(
vk_buf.as_ptr(),
&mut out_ptr,
);
Buffer::from_ptr(out_ptr).unwrap().to_vec()
}

pub fn acir_set_slow_low_memory(enabled: bool) {
if enabled {
env::set_var("BB_SLOW_LOW_MEMORY", "1");
Expand Down
7 changes: 7 additions & 0 deletions barretenberg/bb_rs/src/barretenberg_api/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ pub unsafe fn init_srs(points_buf: &[u8], num_points: u32, g2_point_buf: &[u8])
g2_point_buf.as_ptr(),
);
}

pub unsafe fn init_grumpkin_srs(points_buf: &[u8], num_points: u32) {
bindgen::srs_init_grumpkin_srs(
points_buf.as_ptr(),
&num_points.to_be(),
);
}
30 changes: 30 additions & 0 deletions barretenberg/bb_rs/src/barretenberg_api/tests/acir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,33 @@ fn test_acir_get_circuit_size() {
assert_eq!(circuit_sizes.total, 56);
assert_eq!(circuit_sizes.subgroup, 64);
}

#[test]
fn test_acir_vk_as_fields_mega_honk() {
// Load input data from vk_in_args.json
let vk_in_args_json = include_str!("vk_in_args.json");
let vk_in_args: serde_json::Value = serde_json::from_str(vk_in_args_json).unwrap();
let vk_input: Vec<u8> = vk_in_args["data"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_u64().unwrap() as u8)
.collect();

// Load expected output data from vk_serialized.json
let vk_serialized_json = include_str!("vk_serialized.json");
let vk_serialized: serde_json::Value = serde_json::from_str(vk_serialized_json).unwrap();
let expected_output: Vec<u8> = vk_serialized["data"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_u64().unwrap() as u8)
.collect();

// Call the function
let result = unsafe { acir::acir_vk_as_fields_mega_honk(&vk_input) };

// Compare the result with expected output
assert_eq!(result.len(), expected_output.len(), "Output length mismatch");
assert_eq!(result, expected_output, "Output data mismatch");
}
9 changes: 0 additions & 9 deletions barretenberg/bb_rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
use std::ffi::{c_char, CStr};
use tracing::debug;

pub mod barretenberg_api;

#[no_mangle]
extern "C" fn logstr(char_ptr: *const c_char) {
let c_str = unsafe { CStr::from_ptr(char_ptr) };
debug!("{}", c_str.to_str().unwrap());
}
6 changes: 5 additions & 1 deletion barretenberg/cpp/cmake/lmdb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ ExternalProject_Add(
GIT_TAG ddd0a773e2f44d38e4e31ec9ed81af81f4e4ccbb
BUILD_IN_SOURCE YES
CONFIGURE_COMMAND "" # No configure step
BUILD_COMMAND make -C libraries/liblmdb -e XCFLAGS=-fPIC liblmdb.a
# Pass the CMAKE toolchain settings to make for cross-compilation
BUILD_COMMAND ${CMAKE_COMMAND} -E env
CC=${CMAKE_C_COMPILER}
CFLAGS=${CMAKE_C_FLAGS}
make -C libraries/liblmdb -e XCFLAGS=-fPIC liblmdb.a
INSTALL_COMMAND ""
UPDATE_COMMAND "" # No update step
BUILD_BYPRODUCTS ${LMDB_LIB}
Expand Down
9 changes: 7 additions & 2 deletions barretenberg/cpp/cmake/module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ function(barretenberg_module MODULE_NAME)
endif()

file(GLOB_RECURSE TEST_SOURCE_FILES *.test.cpp)
# Only build tests if BUILD_TESTING is not explicitly OFF
if(TEST_SOURCE_FILES AND NOT FUZZING)
if(NOT DEFINED BUILD_TESTING OR BUILD_TESTING)
add_library(
${MODULE_NAME}_test_objects
OBJECT
Expand Down Expand Up @@ -170,7 +172,8 @@ function(barretenberg_module MODULE_NAME)
# Currently haven't found a way to easily wrap the calls in wasmtime when run from ctest.
gtest_discover_tests(${MODULE_NAME}_tests WORKING_DIRECTORY ${CMAKE_BINARY_DIR} TEST_FILTER -*_SKIP_CI*)
endif()
endif()
endif() # Close BUILD_TESTING check
endif() # Close TEST_SOURCE_FILES AND NOT FUZZING check

file(GLOB_RECURSE FUZZERS_SOURCE_FILES *.fuzzer.cpp)
if(FUZZING AND FUZZERS_SOURCE_FILES)
Expand Down Expand Up @@ -203,8 +206,9 @@ function(barretenberg_module MODULE_NAME)
endforeach()
endif()
file(GLOB_RECURSE BENCH_SOURCE_FILES *.bench.cpp)

# Only build benchmarks if BUILD_TESTING is not explicitly OFF
if(BENCH_SOURCE_FILES AND NOT FUZZING)
if(NOT DEFINED BUILD_TESTING OR BUILD_TESTING)
foreach(BENCHMARK_SOURCE ${BENCH_SOURCE_FILES})
get_filename_component(BENCHMARK_NAME ${BENCHMARK_SOURCE} NAME_WE) # extract name without extension
add_library(
Expand Down Expand Up @@ -265,6 +269,7 @@ function(barretenberg_module MODULE_NAME)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
endforeach()
endif() # Close BUILD_TESTING check
endif()

set(${MODULE_NAME}_lib_targets ${lib_targets} PARENT_SCOPE)
Expand Down
23 changes: 20 additions & 3 deletions barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,23 @@ add_subdirectory(barretenberg/transcript)
add_subdirectory(barretenberg/translator_vm)
add_subdirectory(barretenberg/ultra_honk)
add_subdirectory(barretenberg/wasi)

# DISABLED: world_state causes iOS compilation issues
# if(NOT FUZZING)
# # world_state requires ThreadPool (multithreading)
# if(MULTITHREADING)
# add_subdirectory(barretenberg/world_state)
# endif()
if(NOT FUZZING)
add_subdirectory(barretenberg/world_state)
add_subdirectory(barretenberg/vm2)
# world_state requires ThreadPool (multithreading)
if(MULTITHREADING)
# DISABLED: world_state causes iOS compilation issues
# add_subdirectory(barretenberg/world_state)
endif()
# vm2 is the Aztec VM
if(NOT DISABLE_AZTEC_VM)
add_subdirectory(barretenberg/vm2)
endif()
endif()

if(SMT)
Expand All @@ -117,7 +131,10 @@ if(SMT AND ACIR_FORMAL_PROOFS)
add_subdirectory(barretenberg/acir_formal_proofs)
endif()

add_subdirectory(barretenberg/benchmark)
# Benchmarks are only built when testing is enabled
if(NOT DEFINED BUILD_TESTING OR BUILD_TESTING)
add_subdirectory(barretenberg/benchmark)
endif()

include(GNUInstallDirs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ inline void StaticAnalyzer_<FF>::process_current_plookup_gate(bb::UltraCircuitBu
};
auto& lookup_block = ultra_circuit_builder.blocks.lookup;
auto& lookup_tables = ultra_circuit_builder.lookup_tables;
auto table_index = static_cast<size_t>(lookup_block.q_3()[gate_index]);
auto table_index = static_cast<size_t>(static_cast<uint256_t>(lookup_block.q_3()[gate_index]));
for (const auto& table : lookup_tables) {
if (table.table_index == table_index) {
std::unordered_set<bb::fr> column_1(table.column_1.begin(), table.column_1.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ barretenberg_module(
lmdblib
)

if (NOT FUZZING)
if (NOT FUZZING AND TARGET crypto_merkle_tree_tests)
# but the tests use pedersen and poseidon
target_link_libraries(crypto_merkle_tree_tests PRIVATE stdlib_pedersen_hash stdlib_poseidon2)
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
#include <utility>
#include <vector>

#ifndef NO_MULTITHREADING
#include "barretenberg/common/thread_pool.hpp"
#else
// Forward declare ThreadPool when multithreading is disabled
namespace bb { class ThreadPool; }
#endif
#include "barretenberg/crypto/merkle_tree/hash_path.hpp"
#include "barretenberg/crypto/merkle_tree/indexed_tree/indexed_leaf.hpp"
#include "barretenberg/crypto/merkle_tree/lmdb_store/lmdb_tree_store.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

#pragma once

#ifndef NO_MULTITHREADING
#include "barretenberg/common/thread_pool.hpp"
#else
// Forward declare ThreadPool when multithreading is disabled
namespace bb { class ThreadPool; }
#endif
#include "barretenberg/crypto/merkle_tree/lmdb_store/lmdb_tree_store.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/numeric/random/engine.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ContentAddressedIndexedTree : public ContentAddressedAppendOnlyTree<Store,
ContentAddressedIndexedTree(std::unique_ptr<Store> store,
std::shared_ptr<ThreadPool> workers,
const index_t& initial_size)
: ContentAddressedIndexedTree(std::move(store), workers, initial_size, std::vector<LeafValueType>()) {};
: ContentAddressedIndexedTree(std::move(store), workers, initial_size, std::vector<LeafValueType>()){};
ContentAddressedIndexedTree(ContentAddressedIndexedTree const& other) = delete;
ContentAddressedIndexedTree(ContentAddressedIndexedTree&& other) = delete;
~ContentAddressedIndexedTree() = default;
Expand Down
14 changes: 14 additions & 0 deletions barretenberg/cpp/src/barretenberg/ecc/curves/bn254/c_bind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// === AUDIT STATUS ===
// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
// =====================

#include "../bn254/fr.hpp"

// Silencing warnings about reserved identifiers. Fixing would break downstream code that calls our WASM API.
// NOLINTBEGIN(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier)
WASM_EXPORT void bn254_fr_sqrt(uint8_t const* input, uint8_t* result);
// NOLINTEND(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier)


25 changes: 25 additions & 0 deletions barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/c_bind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// === AUDIT STATUS ===
// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
// =====================

#include "grumpkin.hpp"

// Silencing warnings about reserved identifiers. Fixing would break downstream code that calls our WASM API.
// NOLINTBEGIN(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier)
WASM_EXPORT void ecc_grumpkin__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result);

WASM_EXPORT void ecc_grumpkin__add(uint8_t const* point_a_buf, uint8_t const* point_b_buf, uint8_t* result);

WASM_EXPORT void ecc_grumpkin__batch_mul(uint8_t const* point_buf,
uint8_t const* scalar_buf,
uint32_t num_points,
uint8_t* result);

WASM_EXPORT void ecc_grumpkin__get_random_scalar_mod_circuit_modulus(uint8_t* result);

WASM_EXPORT void ecc_grumpkin__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result);
// NOLINTEND(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier)


6 changes: 6 additions & 0 deletions barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
#include <cstring>
#include <functional>
#include <random>
#if defined(__APPLE__) || defined(__wasm__)
#include <unistd.h>
// Declare getentropy for iOS/macOS (it's in libc but not always in headers)
extern "C" int getentropy(void* buffer, size_t length);
#else
#include <sys/random.h>
#endif

namespace bb::numeric {

Expand Down
Loading