Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add C bindings for KZG functions #54

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ serde_yaml = { version = "0.9.25", optional = true }
criterion = "0.5.1"
rand = "0.8.5"

[build-dependencies]
cbindgen = "0.26.0"

[features]
default = []
rand = ["dep:rand"]
Expand Down
19 changes: 19 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern crate cbindgen;

use std::env;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut config = cbindgen::Config::default();
config.autogen_warning =
Some("/* WARNING: this file was auto-generated by cbindgen. do not modify. */".into());
config.tab_width = 4;

cbindgen::Builder::new()
.with_crate(crate_dir)
.with_language(cbindgen::Language::C)
.with_cpp_compat(false)
.generate()
.expect("cbindgen unable to generate C bindings")
.write_to_file("kateth.h");
}
52 changes: 52 additions & 0 deletions kateth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Setup_4096__65 Setup_4096__65;

typedef P1 Proof;

typedef blst_fr Fr;
#define Fr_BITS 256
#define Fr_BYTES (Fr_BITS / 8)

typedef struct KzgProofAndEval {
Proof proof;
Fr eval;
} KzgProofAndEval;

typedef struct Setup_4096__65 EthSetup;

typedef uint8_t EthBlob[131072];

typedef uint8_t Bytes32[32];

typedef uint8_t Bytes48[48];

typedef P1 Commitment;

struct KzgProofAndEval compute_kzg_proof(const EthSetup *setup,
const EthBlob *blob,
const Bytes32 *z);

bool verify_kzg_proof(const EthSetup *setup,
const Bytes48 *commitment,
const Bytes32 *z,
const Bytes32 *y,
const Bytes48 *proof);

Commitment blob_to_kzg_commitment(const EthSetup *setup, const EthBlob *blob);

Proof compute_blob_kzg_proof(const EthSetup *setup, const EthBlob *blob, const Bytes48 *commitment);

bool verify_blob_kzg_proof(const EthSetup *setup,
const EthBlob *blob,
const Bytes48 *commitment,
const Bytes48 *proof);

bool verify_blob_kzg_proof_batch(const EthSetup *setup,
const EthBlob *blobs,
const Bytes48 *commitments,
const Bytes48 *proofs,
uintptr_t n);
2 changes: 2 additions & 0 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ impl From<FiniteFieldError> for Error {
}

#[derive(Clone, Debug)]
#[repr(C)]
pub struct Blob<const N: usize> {
pub(crate) elements: Box<[Fr; N]>,
}

impl<const N: usize> Blob<N> {
/// cbindgen:no-export
pub const BYTES: usize = Fr::BYTES * N;

pub fn from_slice(bytes: impl AsRef<[u8]>) -> Result<Self, Error> {
Expand Down
1 change: 1 addition & 0 deletions src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub trait Decompress: Sized {
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(transparent)]
pub struct Fr {
element: blst_fr,
}
Expand Down
79 changes: 79 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use core::slice;

use crate::{
bls::Fr,
kzg::{Bytes32, Bytes48, Commitment, Proof, Setup},
};

pub type EthBlob = [u8; 131072];

#[repr(transparent)]
pub struct EthSetup(Setup<4096, 65>);

#[repr(C)]
pub struct KzgProofAndEval {
proof: Proof,
eval: Fr,
}

#[no_mangle]
pub extern "C" fn compute_kzg_proof(
setup: &EthSetup,
blob: &EthBlob,
z: &Bytes32,
) -> KzgProofAndEval {
let (proof, eval) = setup.0.proof(blob, z).unwrap();
KzgProofAndEval { proof, eval }
}

#[no_mangle]
pub extern "C" fn verify_kzg_proof(
setup: &EthSetup,
commitment: &Bytes48,
z: &Bytes32,
y: &Bytes32,
proof: &Bytes48,
) -> bool {
setup.0.verify_proof(proof, commitment, z, y).unwrap()
}

#[no_mangle]
pub extern "C" fn blob_to_kzg_commitment(setup: &EthSetup, blob: &EthBlob) -> Commitment {
setup.0.blob_to_commitment(blob).unwrap()
}

#[no_mangle]
pub extern "C" fn compute_blob_kzg_proof(
setup: &EthSetup,
blob: &EthBlob,
commitment: &Bytes48,
) -> Proof {
setup.0.blob_proof(blob, commitment).unwrap()
}

#[no_mangle]
pub extern "C" fn verify_blob_kzg_proof(
setup: &EthSetup,
blob: &EthBlob,
commitment: &Bytes48,
proof: &Bytes48,
) -> bool {
setup.0.verify_blob_proof(blob, commitment, proof).unwrap()
}

#[no_mangle]
pub extern "C" fn verify_blob_kzg_proof_batch(
setup: &EthSetup,
blobs: *const EthBlob,
commitments: *const Bytes48,
proofs: *const Bytes48,
n: usize,
) -> bool {
let blobs = unsafe { slice::from_raw_parts(blobs, n) };
let commitments = unsafe { slice::from_raw_parts(commitments, n) };
let proofs = unsafe { slice::from_raw_parts(proofs, n) };
setup
.0
.verify_blob_proof_batch(blobs, commitments, proofs)
.unwrap()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod bls;
mod bytes;
mod ffi;
mod math;

pub use bls::{Compress, Decompress};
Expand Down
4 changes: 2 additions & 2 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn roots_of_unity<const ORDER: usize>() -> [Fr; ORDER] {
/// # Panics
///
/// This function will panic if the length of `elements` is not a power of 2.
pub(crate) fn bit_reversal_permutation<T>(elements: impl AsRef<[T]>) -> Vec<T>
pub fn bit_reversal_permutation<T>(elements: impl AsRef<[T]>) -> Vec<T>
where
T: Copy,
{
Expand All @@ -50,7 +50,7 @@ where
/// This function will panic if the length of `elements` is not equal to `N`.
///
/// This function will panic if the length of `elements` is not a power of 2.
pub(crate) fn bit_reversal_permutation_boxed_array<T, const N: usize>(
pub fn bit_reversal_permutation_boxed_array<T, const N: usize>(
elements: impl AsRef<[T]>,
) -> Box<[T; N]>
where
Expand Down
Loading