-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add C bindings for KZG functions
- Loading branch information
1 parent
9c32605
commit 0655ecf
Showing
8 changed files
with
159 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters