Skip to content

Commit

Permalink
feat!: Convert to using generic composer interface FFI (#2)
Browse files Browse the repository at this point in the history
* change bindings for dsl funcs

* composer c bind function rename

* add new total circuit size method

* update flake

* spellcheck

* change names and add solgen

* remove constraint system from verifier

* new branch & nix cleanup

* pedersen plookup stuff

* no cargo check

* update lock

* update flake

* temp use branch for CI

* trying out usize

* update lockfile

* update lockfile

* Use proper flags for native builds (to match nix)

* pin to commit and update docs

* cspell

* Add a ref comment

* cleanup

* update lockfile

* update ref

* and ref in readme

* Update src/composer.rs

---------

Co-authored-by: Blaine Bublitz <[email protected]>
  • Loading branch information
vezenovm and phated authored Apr 6, 2023
1 parent 279be62 commit 7f39dee
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 66 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ jobs:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- name: Cargo build
run: |
nix build -L .#checks.${{ matrix.target }}.cargo-check
- name: Cargo clippy
run: |
nix build -L .#checks.${{ matrix.target }}.cargo-clippy
Expand All @@ -58,8 +54,7 @@ jobs:
with:
repository: AztecProtocol/barretenberg
path: barretenberg
# TODO: We should decide on a tag or commit
# ref: foobarbaz
ref: 486d73842b4b7d6aa84fa12d7462fe52e892d416

- name: Setup Linux environment
if: matrix.os == 'ubuntu-latest'
Expand All @@ -78,7 +73,7 @@ jobs:
- name: Build and install barretenberg
working-directory: barretenberg/cpp
run: |
cmake --preset ${{ env.PRESET }}
cmake --preset ${{ env.PRESET }} -DTESTING=OFF -DBENCHMARKS=OFF -DCMAKE_BUILD_TYPE=RelWithAssert
cmake --build --preset ${{ env.PRESET }}
sudo cmake --install build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To leverage the `barretenberg-sys` crate, you'll need to install some global pac

Linker provided by Clang, but might need to be installed via `apt install lld`.

4. `barretenberg`
4. `barretenberg` (preferably at commit `486d73842b4b7d6aa84fa12d7462fe52e892d416`)

Needs to be built and installed following the instructions [in the README](https://github.com/AztecProtocol/barretenberg#getting-started). Note that barretenberg has its own [dependencies](https://github.com/AztecProtocol/barretenberg#dependencies) that will need to be installed, such as `cmake` and `ninja`.

Expand Down
21 changes: 12 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,24 @@ fn main() -> Result<()> {
.header_contents(
"wrapper.hpp",
r#"
#include <barretenberg/dsl/turbo_proofs/c_bind.hpp>
#include <barretenberg/dsl/acir_proofs/c_bind.hpp>
#include <barretenberg/crypto/blake2s/c_bind.hpp>
#include <barretenberg/crypto/pedersen/c_bind.hpp>
#include <barretenberg/crypto/schnorr/c_bind.hpp>
#include <barretenberg/ecc/curves/bn254/scalar_multiplication/c_bind.hpp>
"#,
)
.allowlist_function("blake2s_to_field")
.allowlist_function("turbo_get_exact_circuit_size")
.allowlist_function("turbo_init_proving_key")
.allowlist_function("turbo_init_verification_key")
.allowlist_function("turbo_new_proof")
.allowlist_function("turbo_verify_proof")
.allowlist_function("pedersen__compress_fields")
.allowlist_function("pedersen__compress")
.allowlist_function("pedersen__commit")
.allowlist_function("acir_proofs_get_solidity_verifier")
.allowlist_function("acir_proofs_get_exact_circuit_size")
.allowlist_function("acir_proofs_get_total_circuit_size")
.allowlist_function("acir_proofs_init_proving_key")
.allowlist_function("acir_proofs_init_verification_key")
.allowlist_function("acir_proofs_new_proof")
.allowlist_function("acir_proofs_verify_proof")
.allowlist_function("pedersen_plookup_compress_fields")
.allowlist_function("pedersen_plookup_compress")
.allowlist_function("pedersen_plookup_commit")
.allowlist_function("new_pippenger")
.allowlist_function("compute_public_key")
.allowlist_function("construct_signature")
Expand Down Expand Up @@ -136,6 +138,7 @@ fn link_lib_omp(os: &OS) {
}

fn find_linux_search_paths() -> Option<Vec<PathBuf>> {
// Based on https://gitlab.com/kornelski/openmp-rs/-/blob/a922ab9073a95fb5161a38f13f5c12d37d1f1811/build.rs#L39-78
let comp = cc::Build::new()
.flag("-v")
.flag("-print-search-dirs")
Expand Down
9 changes: 8 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
"ignorePaths": [
"target"
],
"words": []
"words": [
"libomp",
"pedersen",
"bindgen",
"Bindgen",
"thiserror",
"Barretenberg"
]
}
36 changes: 18 additions & 18 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@
in
rec {
checks = {
cargo-check = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;

doCheck = true;
});

cargo-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;

Expand All @@ -107,7 +101,7 @@
cargo-test = craneLib.cargoTest (commonArgs // {
inherit cargoArtifacts;

cargoTestArgs = "-- --test-threads=1";
cargoTestExtraArgs = "--workspace -- --test-threads=1";

doCheck = true;
});
Expand Down
2 changes: 1 addition & 1 deletion src/blake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {
use hex;

#[test]
fn black2s() {
fn blake2s() {
struct Test<'a> {
input: Vec<u8>,
expected_hex: &'a str,
Expand Down
34 changes: 21 additions & 13 deletions src/composer.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use crate::*;

/// # Safety
/// pippenger must point to a valid Pippenger object
pub unsafe fn smart_contract(
_pippenger: *mut ::std::os::raw::c_void,
_g2_ptr: &[u8],
_cs_ptr: &[u8],
_output_buf: *mut *mut u8,
) -> u32 {
unimplemented!()
pub unsafe fn get_solidity_verifier(
g2_ptr: &[u8],
vk_ptr: &[u8],
output_buf: *mut *mut u8,
) -> usize {
acir_proofs_get_solidity_verifier(
g2_ptr.as_ptr() as *const u8,
vk_ptr.as_ptr() as *const u8,
output_buf,
)
}

/// # Safety
/// cs_prt must point to a valid constraints system structure of type standard_format
pub unsafe fn get_exact_circuit_size(cs_prt: *const u8) -> u32 {
turbo_get_exact_circuit_size(cs_prt)
acir_proofs_get_exact_circuit_size(cs_prt)
}

/// # Safety
/// cs_prt must point to a valid constraints system structure of type standard_format
pub unsafe fn get_total_circuit_size(cs_prt: *const u8) -> u32 {
acir_proofs_get_total_circuit_size(cs_prt)
}

/// # Safety
/// cs_prt must point to a valid constraints system structure of type standard_format
pub unsafe fn init_proving_key(cs_ptr: &[u8], pk_data_ptr: *mut *mut u8) -> usize {
let cs_ptr = cs_ptr.as_ptr();
turbo_init_proving_key(cs_ptr, pk_data_ptr as *const *mut u8 as *mut *const u8)
acir_proofs_init_proving_key(cs_ptr, pk_data_ptr as *const *mut u8 as *mut *const u8)
}

/// # Safety
Expand All @@ -32,7 +40,7 @@ pub unsafe fn init_verification_key(
pk_ptr: &[u8],
vk_data_ptr: *mut *mut u8,
) -> usize {
turbo_init_verification_key(
acir_proofs_init_verification_key(
pippenger,
g2_ptr.as_ptr() as *const u8,
pk_ptr.as_ptr() as *const u8,
Expand All @@ -52,7 +60,7 @@ pub unsafe fn create_proof_with_pk(
) -> usize {
let cs_ptr = cs_ptr.as_ptr() as *const u8;
let pk_ptr = pk_ptr.as_ptr() as *const u8;
turbo_new_proof(
acir_proofs_new_proof(
pippenger,
g2_ptr.as_ptr(),
pk_ptr,
Expand All @@ -67,7 +75,7 @@ pub unsafe fn create_proof_with_pk(
pub unsafe fn verify_with_vk(g2_ptr: &[u8], vk_ptr: &[u8], cs_ptr: &[u8], proof: &[u8]) -> bool {
let proof_ptr = proof.as_ptr() as *const u8;

turbo_verify_proof(
acir_proofs_verify_proof(
g2_ptr.as_ptr() as *const u8,
vk_ptr.as_ptr() as *const u8,
cs_ptr.as_ptr() as *const u8,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests {
f_one[31] = 1;
let got = pedersen::compress_native(&f_zero, &f_one);
assert_eq!(
"229fb88be21cec523e9223a21324f2e305aea8bff9cdbcb3d0c6bba384666ea1",
"11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef",
hex::encode(got)
);
}
Expand Down
16 changes: 8 additions & 8 deletions src/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn compress_native(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
let mut result = [0_u8; 32];

unsafe {
pedersen__compress_fields(
pedersen_plookup_compress_fields(
left.as_ptr() as *const u8,
right.as_ptr() as *const u8,
result.as_mut_ptr(),
Expand All @@ -30,7 +30,7 @@ pub fn compress_many(inputs: &[[u8; 32]]) -> [u8; 32] {

let mut result = [0_u8; 32];
unsafe {
pedersen__compress(buffer.as_ptr() as *const u8, result.as_mut_ptr());
pedersen_plookup_compress(buffer.as_ptr() as *const u8, result.as_mut_ptr());
}
result
}
Expand All @@ -45,7 +45,7 @@ pub fn encrypt(inputs_buffer: &[[u8; 32]]) -> ([u8; 32], [u8; 32]) {
}

unsafe {
pedersen__commit(buffer.as_ptr() as *const u8, result.as_mut_ptr());
pedersen_plookup_commit(buffer.as_ptr() as *const u8, result.as_mut_ptr());
}
let s: [u8; 32] = (result[0..32]).try_into().unwrap();
let e: [u8; 32] = (result[32..64]).try_into().unwrap();
Expand All @@ -72,17 +72,17 @@ mod tests {
Test {
input_left: f_zero,
input_right: f_one,
expected_hex: "229fb88be21cec523e9223a21324f2e305aea8bff9cdbcb3d0c6bba384666ea1",
expected_hex: "11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef",
},
Test {
input_left: f_one,
input_right: f_one,
expected_hex: "26425ddf29b4af6ee91008e8dbcbee975653170eee849efd75abf8301dee114e",
expected_hex: "1044a769e185fcdf077c8289a6bf87c5c77ff9561cab69d39fadd90a07ee4af4",
},
Test {
input_left: f_one,
input_right: f_zero,
expected_hex: "08f3cb4f0fdd7a9ef130c6d4590af6750b1475161020a198a56eced45078ccf2",
expected_hex: "17d213c8fe83e89a2f3190933d437a3e231124e0383e6dc6a7b6e6358833e427",
},
];

Expand All @@ -103,8 +103,8 @@ mod tests {
let inputs: Vec<[u8; 32]> = vec![f_zero, f_one];

let (x, y) = encrypt(&inputs);
let expected_x = "229fb88be21cec523e9223a21324f2e305aea8bff9cdbcb3d0c6bba384666ea1";
let expected_y = "296b4b4605e586a91caa3202baad557628a8c56d0a1d6dff1a7ca35aed3029d5";
let expected_x = "11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef";
let expected_y = "0ecf9d98be4597a88c46a7e0fa8836b57a7dcb41ee30f8d8787b11cc259c83fa";
assert_eq!(expected_x, hex::encode(x));
assert_eq!(expected_y, hex::encode(y));
}
Expand Down

0 comments on commit 7f39dee

Please sign in to comment.