From 500911c8f40605ca3c97c6784670dfb9b113de05 Mon Sep 17 00:00:00 2001 From: sirse Date: Wed, 8 Jan 2025 21:53:03 +0200 Subject: [PATCH] Disable c_bindings feature by default --- arkworks/Cargo.toml | 1 - arkworks/src/eip_4844.rs | 9 ++- arkworks/src/eip_7594.rs | 5 +- arkworks3/Cargo.toml | 1 - arkworks3/src/eip_4844.rs | 118 +++++++++++++++++++++++++-- arkworks3/src/eip_7594.rs | 5 +- arkworks3/src/utils.rs | 97 +--------------------- blst/Cargo.toml | 1 - blst/src/eip_4844.rs | 124 +++++++++++++++++++++++++--- blst/src/eip_7594.rs | 3 +- blst/src/utils.rs | 96 ---------------------- blst/tests/c_bindings.rs | 2 +- constantine/Cargo.toml | 1 - constantine/src/eip_4844.rs | 138 ++++++++++++++++++++++++++------ constantine/src/eip_7594.rs | 5 +- constantine/src/utils.rs | 83 ------------------- constantine/tests/c_bindings.rs | 2 +- run-c-kzg-4844-benches.sh | 4 +- run-c-kzg-4844-fuzz.sh | 8 +- run-c-kzg-4844-tests.sh | 4 +- zkcrypto/Cargo.toml | 1 - zkcrypto/src/eip_4844.rs | 122 +++++++++++++++++++++++++--- zkcrypto/src/eip_7594.rs | 5 +- zkcrypto/src/utils.rs | 96 +--------------------- 24 files changed, 475 insertions(+), 456 deletions(-) diff --git a/arkworks/Cargo.toml b/arkworks/Cargo.toml index 93ee840d9..413072171 100644 --- a/arkworks/Cargo.toml +++ b/arkworks/Cargo.toml @@ -27,7 +27,6 @@ default = [ "std", "rand", "bgmw", - "c_bindings" ] std = [ "ark-ff/std", "ark-ec/std", "ark-poly/std", "ark-std/std", diff --git a/arkworks/src/eip_4844.rs b/arkworks/src/eip_4844.rs index 16e0c7c25..688f3f4d5 100644 --- a/arkworks/src/eip_4844.rs +++ b/arkworks/src/eip_4844.rs @@ -1,7 +1,9 @@ extern crate alloc; use crate::kzg_proofs::KZGSettings as LKZGSettings; +#[cfg(feature = "c_bindings")] use crate::utils::PRECOMPUTATION_TABLES; +#[cfg(feature = "c_bindings")] use kzg::{ eth::{ self, @@ -10,13 +12,14 @@ use kzg::{ Fr, G1, }; -#[cfg(feature = "std")] +#[cfg(feature = "c_bindings")] +use core::ptr; +#[cfg(all(feature = "std", feature = "c_bindings"))] use libc::FILE; #[cfg(feature = "std")] use std::fs::File; #[cfg(feature = "std")] use std::io::Read; -use std::ptr; #[cfg(feature = "std")] use kzg::eip_4844::load_trusted_setup_string; @@ -37,6 +40,7 @@ pub fn load_trusted_setup_filename_rust( load_trusted_setup_rust(&g1_monomial_bytes, &g1_lagrange_bytes, &g2_monomial_bytes) } +#[cfg(feature = "c_bindings")] pub(crate) fn kzg_settings_to_c(rust_settings: &LKZGSettings) -> CKZGSettings { CKZGSettings { roots_of_unity: Box::leak( @@ -119,6 +123,7 @@ pub(crate) fn kzg_settings_to_c(rust_settings: &LKZGSettings) -> CKZGSettings { } } +#[cfg(feature = "c_bindings")] macro_rules! handle_ckzg_badargs { ($x: expr) => { match $x { diff --git a/arkworks/src/eip_7594.rs b/arkworks/src/eip_7594.rs index 8fcecc5fc..da6c68ca1 100644 --- a/arkworks/src/eip_7594.rs +++ b/arkworks/src/eip_7594.rs @@ -2,8 +2,6 @@ extern crate alloc; use kzg::EcBackend; -use kzg::c_bindings_eip7594; - use crate::kzg_proofs::FFTSettings; use crate::kzg_proofs::KZGSettings; use crate::kzg_types::ArkFp; @@ -26,4 +24,5 @@ impl EcBackend for ArkBackend { type KZGSettings = KZGSettings; } -c_bindings_eip7594!(ArkBackend); +#[cfg(feature = "c_bindings")] +kzg::c_bindings_eip7594!(ArkBackend); diff --git a/arkworks3/Cargo.toml b/arkworks3/Cargo.toml index 82367cf54..70fe33cfc 100644 --- a/arkworks3/Cargo.toml +++ b/arkworks3/Cargo.toml @@ -28,7 +28,6 @@ rand = { version = "0.8.5" } default = [ "std", "rand", - "c_bindings" ] std = [ "ark-ff/std", "ark-ec/std", "ark-poly/std", "ark-std/std", diff --git a/arkworks3/src/eip_4844.rs b/arkworks3/src/eip_4844.rs index caec5a472..100d7c62e 100644 --- a/arkworks3/src/eip_4844.rs +++ b/arkworks3/src/eip_4844.rs @@ -1,23 +1,32 @@ extern crate alloc; use crate::kzg_proofs::KZGSettings; +#[cfg(feature = "c_bindings")] +use alloc::boxed::Box; +#[cfg(feature = "c_bindings")] use core::ptr; -use kzg::eip_4844::{ - load_trusted_setup_rust, BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, - TRUSTED_SETUP_NUM_G2_POINTS, -}; -use kzg::eth::c_bindings::{ - Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof, +use kzg::eip_4844::load_trusted_setup_rust; +#[cfg(feature = "c_bindings")] +use kzg::{ + eip_4844::{ + BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, + TRUSTED_SETUP_NUM_G2_POINTS, + }, + eth::{ + self, + c_bindings::{Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof}, + }, + Fr, G1, }; -use kzg::{eth, Fr, G1}; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", feature = "c_bindings"))] use libc::FILE; #[cfg(feature = "std")] use std::fs::File; #[cfg(feature = "std")] use std::io::Read; +#[cfg(feature = "c_bindings")] use crate::utils::{handle_ckzg_badargs, kzg_settings_to_c, PRECOMPUTATION_TABLES}; #[cfg(feature = "std")] @@ -37,6 +46,99 @@ pub fn load_trusted_setup_filename_rust( load_trusted_setup_rust(&g1_monomial_bytes, &g1_lagrange_bytes, &g2_monomial_bytes) } +#[cfg(feature = "c_bindings")] +macro_rules! handle_ckzg_badargs { + ($x: expr) => { + match $x { + Ok(value) => value, + Err(_) => return kzg::eth::c_bindings::CKzgRet::BadArgs, + } + }; +} + +#[cfg(feature = "c_bindings")] +fn kzg_settings_to_c(rust_settings: &KZGSettings) -> CKZGSettings { + CKZGSettings { + roots_of_unity: Box::leak( + rust_settings + .fs + .roots_of_unity + .iter() + .map(|r| r.to_blst_fr()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + brp_roots_of_unity: Box::leak( + rust_settings + .fs + .brp_roots_of_unity + .iter() + .map(|r| r.to_blst_fr()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + reverse_roots_of_unity: Box::leak( + rust_settings + .fs + .reverse_roots_of_unity + .iter() + .map(|r| r.to_blst_fr()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_monomial: Box::leak( + rust_settings + .g1_values_monomial + .iter() + .map(|r| r.to_blst_p1()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_lagrange_brp: Box::leak( + rust_settings + .g1_values_lagrange_brp + .iter() + .map(|r| r.to_blst_p1()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g2_values_monomial: Box::leak( + rust_settings + .g2_values_monomial + .iter() + .map(|r| r.to_blst_p2()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + x_ext_fft_columns: Box::leak( + rust_settings + .x_ext_fft_columns + .iter() + .map(|r| { + Box::leak( + r.iter() + .map(|it| it.to_blst_p1()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr() + }) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + tables: core::ptr::null_mut(), + wbits: 0, + scratch_size: 0, + } +} + /// # Safety #[cfg(feature = "c_bindings")] #[no_mangle] diff --git a/arkworks3/src/eip_7594.rs b/arkworks3/src/eip_7594.rs index 8fcecc5fc..da6c68ca1 100644 --- a/arkworks3/src/eip_7594.rs +++ b/arkworks3/src/eip_7594.rs @@ -2,8 +2,6 @@ extern crate alloc; use kzg::EcBackend; -use kzg::c_bindings_eip7594; - use crate::kzg_proofs::FFTSettings; use crate::kzg_proofs::KZGSettings; use crate::kzg_types::ArkFp; @@ -26,4 +24,5 @@ impl EcBackend for ArkBackend { type KZGSettings = KZGSettings; } -c_bindings_eip7594!(ArkBackend); +#[cfg(feature = "c_bindings")] +kzg::c_bindings_eip7594!(ArkBackend); diff --git a/arkworks3/src/utils.rs b/arkworks3/src/utils.rs index 512ac7ce1..89ef95ffe 100644 --- a/arkworks3/src/utils.rs +++ b/arkworks3/src/utils.rs @@ -1,13 +1,11 @@ extern crate alloc; -use alloc::boxed::Box; - use kzg::{ eip_4844::PrecomputationTableManager, eth::{self, c_bindings::CKZGSettings}, }; -use crate::kzg_proofs::{FFTSettings as LFFTSettings, KZGSettings as LKZGSettings}; +use crate::kzg_proofs::FFTSettings as LFFTSettings; use crate::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine}; use super::{Fp, P1}; @@ -28,17 +26,6 @@ pub struct PolyData { } // FIXME: Store just dense poly here -macro_rules! handle_ckzg_badargs { - ($x: expr) => { - match $x { - Ok(value) => value, - Err(_) => return kzg::eth::c_bindings::CKzgRet::BadArgs, - } - }; -} - -pub(crate) use handle_ckzg_badargs; - pub(crate) fn fft_settings_to_rust( c_settings: *const CKZGSettings, ) -> Result { @@ -90,88 +77,6 @@ pub(crate) static mut PRECOMPUTATION_TABLES: PrecomputationTableManager< ArkG1Affine, > = PrecomputationTableManager::new(); -pub(crate) fn kzg_settings_to_c(rust_settings: &LKZGSettings) -> CKZGSettings { - CKZGSettings { - roots_of_unity: Box::leak( - rust_settings - .fs - .roots_of_unity - .iter() - .map(|r| r.to_blst_fr()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - brp_roots_of_unity: Box::leak( - rust_settings - .fs - .brp_roots_of_unity - .iter() - .map(|r| r.to_blst_fr()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - reverse_roots_of_unity: Box::leak( - rust_settings - .fs - .reverse_roots_of_unity - .iter() - .map(|r| r.to_blst_fr()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_monomial: Box::leak( - rust_settings - .g1_values_monomial - .iter() - .map(|r| r.to_blst_p1()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_lagrange_brp: Box::leak( - rust_settings - .g1_values_lagrange_brp - .iter() - .map(|r| r.to_blst_p1()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g2_values_monomial: Box::leak( - rust_settings - .g2_values_monomial - .iter() - .map(|r| r.to_blst_p2()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - x_ext_fft_columns: Box::leak( - rust_settings - .x_ext_fft_columns - .iter() - .map(|r| { - Box::leak( - r.iter() - .map(|it| it.to_blst_p1()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr() - }) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - tables: core::ptr::null_mut(), - wbits: 0, - scratch_size: 0, - } -} - pub fn pc_poly_into_blst_poly(poly: DensePoly) -> PolyData { let mut bls_pol: Vec = { Vec::new() }; for x in poly.coeffs { diff --git a/blst/Cargo.toml b/blst/Cargo.toml index fc85178dd..5062118be 100644 --- a/blst/Cargo.toml +++ b/blst/Cargo.toml @@ -24,7 +24,6 @@ default = [ "std", "rand", "bgmw", - "c_bindings", ] std = [ "hex/std", diff --git a/blst/src/eip_4844.rs b/blst/src/eip_4844.rs index 338704360..b1fc46e26 100644 --- a/blst/src/eip_4844.rs +++ b/blst/src/eip_4844.rs @@ -1,18 +1,23 @@ extern crate alloc; -use alloc::boxed::Box; -use alloc::vec::Vec; +#[cfg(feature = "c_bindings")] +use alloc::{boxed::Box, vec::Vec}; +#[cfg(feature = "c_bindings")] use core::ptr; -use kzg::eip_4844::{ - load_trusted_setup_rust, BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, - TRUSTED_SETUP_NUM_G2_POINTS, -}; -use kzg::eth::c_bindings::{ - Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof, +use kzg::eip_4844::load_trusted_setup_rust; +#[cfg(feature = "c_bindings")] +use kzg::{ + eip_4844::{ + BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, + TRUSTED_SETUP_NUM_G2_POINTS, + }, + eth::{ + c_bindings::{Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof}, + FIELD_ELEMENTS_PER_CELL, FIELD_ELEMENTS_PER_EXT_BLOB, + }, + Fr, G1, }; -use kzg::eth::{FIELD_ELEMENTS_PER_CELL, FIELD_ELEMENTS_PER_EXT_BLOB}; -use kzg::{Fr, G1}; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", feature = "c_bindings"))] use libc::FILE; #[cfg(feature = "std")] use std::fs::File; @@ -22,8 +27,101 @@ use std::io::Read; #[cfg(feature = "std")] use kzg::eip_4844::load_trusted_setup_string; -use crate::types::kzg_settings::FsKZGSettings; -use crate::utils::{handle_ckzg_badargs, kzg_settings_to_c, PRECOMPUTATION_TABLES}; +#[cfg(feature = "c_bindings")] +use crate::{types::kzg_settings::FsKZGSettings, utils::PRECOMPUTATION_TABLES}; + +#[cfg(feature = "c_bindings")] +fn kzg_settings_to_c(rust_settings: &FsKZGSettings) -> CKZGSettings { + CKZGSettings { + roots_of_unity: Box::leak( + rust_settings + .fs + .roots_of_unity + .iter() + .map(|r| r.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + brp_roots_of_unity: Box::leak( + rust_settings + .fs + .brp_roots_of_unity + .iter() + .map(|r| r.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + reverse_roots_of_unity: Box::leak( + rust_settings + .fs + .reverse_roots_of_unity + .iter() + .map(|r| r.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_monomial: Box::leak( + rust_settings + .g1_values_monomial + .iter() + .map(|r| r.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_lagrange_brp: Box::leak( + rust_settings + .g1_values_lagrange_brp + .iter() + .map(|r| r.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g2_values_monomial: Box::leak( + rust_settings + .g2_values_monomial + .iter() + .map(|r| r.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + x_ext_fft_columns: Box::leak( + rust_settings + .x_ext_fft_columns + .iter() + .map(|r| { + Box::leak( + r.iter() + .map(|it| it.0) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr() + }) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + tables: core::ptr::null_mut(), + wbits: 0, + scratch_size: 0, + } +} + +#[cfg(feature = "c_bindings")] +macro_rules! handle_ckzg_badargs { + ($x: expr) => { + match $x { + Ok(value) => value, + Err(_) => return kzg::eth::c_bindings::CKzgRet::BadArgs, + } + }; +} #[cfg(feature = "std")] pub fn load_trusted_setup_filename_rust( diff --git a/blst/src/eip_7594.rs b/blst/src/eip_7594.rs index 12a08b665..3e45e2d9c 100644 --- a/blst/src/eip_7594.rs +++ b/blst/src/eip_7594.rs @@ -9,7 +9,6 @@ use crate::types::g1::FsG1Affine; use crate::types::g2::FsG2; use crate::types::kzg_settings::FsKZGSettings; use crate::types::poly::FsPoly; -use kzg::c_bindings_eip7594; use crate::types::fr::FsFr; @@ -27,4 +26,4 @@ impl EcBackend for BlstBackend { } #[cfg(feature = "c_bindings")] -c_bindings_eip7594!(BlstBackend); +kzg::c_bindings_eip7594!(BlstBackend); diff --git a/blst/src/utils.rs b/blst/src/utils.rs index 334ca72fc..89080f268 100644 --- a/blst/src/utils.rs +++ b/blst/src/utils.rs @@ -1,10 +1,8 @@ extern crate alloc; -use alloc::boxed::Box; use alloc::vec::Vec; use kzg::eip_4844::{hash_to_bls_field, PrecomputationTableManager}; -use kzg::eth::c_bindings::CKZGSettings; use kzg::{Fr, G1Mul, G2Mul}; use crate::consts::{G1_GENERATOR, G2_GENERATOR}; @@ -12,7 +10,6 @@ use crate::types::fp::FsFp; use crate::types::fr::FsFr; use crate::types::g1::{FsG1, FsG1Affine}; use crate::types::g2::FsG2; -use crate::types::kzg_settings::FsKZGSettings; pub fn generate_trusted_setup( n: usize, @@ -36,102 +33,9 @@ pub fn generate_trusted_setup( (s1, s2, s3) } -macro_rules! handle_ckzg_badargs { - ($x: expr) => { - match $x { - Ok(value) => value, - Err(_) => return kzg::eth::c_bindings::CKzgRet::BadArgs, - } - }; -} - -pub(crate) use handle_ckzg_badargs; - pub(crate) static mut PRECOMPUTATION_TABLES: PrecomputationTableManager< FsFr, FsG1, FsFp, FsG1Affine, > = PrecomputationTableManager::new(); - -pub(crate) fn kzg_settings_to_c(rust_settings: &FsKZGSettings) -> CKZGSettings { - CKZGSettings { - roots_of_unity: Box::leak( - rust_settings - .fs - .roots_of_unity - .iter() - .map(|r| r.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - brp_roots_of_unity: Box::leak( - rust_settings - .fs - .brp_roots_of_unity - .iter() - .map(|r| r.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - reverse_roots_of_unity: Box::leak( - rust_settings - .fs - .reverse_roots_of_unity - .iter() - .map(|r| r.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_monomial: Box::leak( - rust_settings - .g1_values_monomial - .iter() - .map(|r| r.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_lagrange_brp: Box::leak( - rust_settings - .g1_values_lagrange_brp - .iter() - .map(|r| r.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g2_values_monomial: Box::leak( - rust_settings - .g2_values_monomial - .iter() - .map(|r| r.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - x_ext_fft_columns: Box::leak( - rust_settings - .x_ext_fft_columns - .iter() - .map(|r| { - Box::leak( - r.iter() - .map(|it| it.0) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr() - }) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - tables: core::ptr::null_mut(), - wbits: 0, - scratch_size: 0, - } -} diff --git a/blst/tests/c_bindings.rs b/blst/tests/c_bindings.rs index e08446a4e..fd0d4c732 100644 --- a/blst/tests/c_bindings.rs +++ b/blst/tests/c_bindings.rs @@ -1,4 +1,4 @@ -#[cfg(test)] +#[cfg(all(test, feature = "c_bindings"))] mod tests { use kzg_bench::tests::c_bindings::{ blob_to_kzg_commitment_invalid_blob_test, diff --git a/constantine/Cargo.toml b/constantine/Cargo.toml index dc5b1472d..d62caa5e6 100644 --- a/constantine/Cargo.toml +++ b/constantine/Cargo.toml @@ -26,7 +26,6 @@ default = [ "std", "rand", "bgmw", - "c_bindings" ] std = [ "hex/std", diff --git a/constantine/src/eip_4844.rs b/constantine/src/eip_4844.rs index f6697c016..d5c464f39 100644 --- a/constantine/src/eip_4844.rs +++ b/constantine/src/eip_4844.rs @@ -1,14 +1,20 @@ extern crate alloc; -use alloc::boxed::Box; -use alloc::string::String; -use alloc::vec::Vec; +#[cfg(feature = "c_bindings")] +use alloc::{boxed::Box, string::String, vec::Vec}; +#[cfg(feature = "c_bindings")] use core::ptr; -use kzg::eth::c_bindings::{ - Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof, +#[cfg(feature = "c_bindings")] +use kzg::{ + eip_4844::{ + BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, + TRUSTED_SETUP_NUM_G2_POINTS, + }, + eth, + eth::c_bindings::{Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof}, + Fr, G1, }; -use kzg::{eth, Fr, G1}; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", feature = "c_bindings"))] use libc::FILE; #[cfg(feature = "std")] use std::fs::File; @@ -18,20 +24,17 @@ use std::io::Read; #[cfg(feature = "std")] use kzg::eip_4844::load_trusted_setup_string; -use kzg::eip_4844::{ - load_trusted_setup_rust, PrecomputationTableManager, BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, - TRUSTED_SETUP_NUM_G1_POINTS, TRUSTED_SETUP_NUM_G2_POINTS, -}; +use kzg::eip_4844::load_trusted_setup_rust; -use crate::types::fp::CtFp; -use crate::types::fr::CtFr; -use crate::types::g1::{CtG1, CtG1Affine}; - -use crate::types::kzg_settings::CtKZGSettings; -use crate::utils::kzg_settings_to_c; - -static mut PRECOMPUTATION_TABLES: PrecomputationTableManager = - PrecomputationTableManager::new(); +#[cfg(feature = "c_bindings")] +use crate::types::{ + fp::CtFp, + fr::CtFr, + g1::{CtG1, CtG1Affine}, + kzg_settings::CtKZGSettings, +}; +#[cfg(feature = "c_bindings")] +use crate::utils::PRECOMPUTATION_TABLES; #[cfg(feature = "std")] pub fn load_trusted_setup_filename_rust( @@ -47,6 +50,90 @@ pub fn load_trusted_setup_filename_rust( load_trusted_setup_rust(&g1_monomial_bytes, &g1_lagrange_bytes, &g2_monomial_bytes) } +#[cfg(feature = "c_bindings")] +fn kzg_settings_to_c(rust_settings: &CtKZGSettings) -> CKZGSettings { + CKZGSettings { + roots_of_unity: Box::leak( + rust_settings + .fs + .roots_of_unity + .iter() + .map(|r| r.to_blst_fr()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + brp_roots_of_unity: Box::leak( + rust_settings + .fs + .brp_roots_of_unity + .iter() + .map(|r| r.to_blst_fr()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + reverse_roots_of_unity: Box::leak( + rust_settings + .fs + .reverse_roots_of_unity + .iter() + .map(|r| r.to_blst_fr()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_monomial: Box::leak( + rust_settings + .g1_values_monomial + .iter() + .map(|r| r.to_blst_p1()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_lagrange_brp: Box::leak( + rust_settings + .g1_values_lagrange_brp + .iter() + .map(|r| r.to_blst_p1()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g2_values_monomial: Box::leak( + rust_settings + .g2_values_monomial + .iter() + .map(|r| r.to_blst_p2()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + x_ext_fft_columns: Box::leak( + rust_settings + .x_ext_fft_columns + .iter() + .map(|r| { + Box::leak( + r.iter() + .map(|it| it.to_blst_p1()) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr() + }) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + tables: core::ptr::null_mut(), + wbits: 0, + scratch_size: 0, + } +} + +#[cfg(feature = "c_bindings")] macro_rules! handle_ckzg_badargs { ($x: expr) => { match $x { @@ -400,14 +487,15 @@ pub unsafe extern "C" fn compute_kzg_proof( #[cfg(test)] mod tests { - use kzg_bench::tests::utils::get_trusted_setup_path; + #[cfg(feature = "c_bindings")] + #[test] + fn kzg_settings_to_rust_check_conversion() { + use kzg_bench::tests::utils::get_trusted_setup_path; - use crate::{eip_4844::kzg_settings_to_c, types::kzg_settings::CtKZGSettings}; + use crate::types::kzg_settings::CtKZGSettings; - use super::load_trusted_setup_filename_rust; + use super::{kzg_settings_to_c, load_trusted_setup_filename_rust}; - #[test] - fn kzg_settings_to_rust_check_conversion() { let settings = load_trusted_setup_filename_rust(get_trusted_setup_path().as_str()); assert!(settings.is_ok()); diff --git a/constantine/src/eip_7594.rs b/constantine/src/eip_7594.rs index 100f124c9..3951f3c7f 100644 --- a/constantine/src/eip_7594.rs +++ b/constantine/src/eip_7594.rs @@ -2,8 +2,6 @@ extern crate alloc; use kzg::EcBackend; -use kzg::c_bindings_eip7594; - use crate::types::fft_settings::CtFFTSettings; use crate::types::fp::CtFp; use crate::types::fr::CtFr; @@ -26,4 +24,5 @@ impl EcBackend for CtBackend { type KZGSettings = CtKZGSettings; } -c_bindings_eip7594!(CtBackend); +#[cfg(feature = "c_bindings")] +kzg::c_bindings_eip7594!(CtBackend); diff --git a/constantine/src/utils.rs b/constantine/src/utils.rs index 88d56afb2..1b76cc3c1 100644 --- a/constantine/src/utils.rs +++ b/constantine/src/utils.rs @@ -12,7 +12,6 @@ use crate::types::fp::CtFp; use crate::types::fr::CtFr; use crate::types::g1::{CtG1, CtG1Affine}; use crate::types::g2::CtG2; -use crate::types::kzg_settings::CtKZGSettings; pub fn generate_trusted_setup( n: usize, @@ -98,85 +97,3 @@ pub(crate) static mut PRECOMPUTATION_TABLES: PrecomputationTableManager< CtFp, CtG1Affine, > = PrecomputationTableManager::new(); - -pub(crate) fn kzg_settings_to_c(rust_settings: &CtKZGSettings) -> CKZGSettings { - CKZGSettings { - roots_of_unity: Box::leak( - rust_settings - .fs - .roots_of_unity - .iter() - .map(|r| r.to_blst_fr()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - brp_roots_of_unity: Box::leak( - rust_settings - .fs - .brp_roots_of_unity - .iter() - .map(|r| r.to_blst_fr()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - reverse_roots_of_unity: Box::leak( - rust_settings - .fs - .reverse_roots_of_unity - .iter() - .map(|r| r.to_blst_fr()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_monomial: Box::leak( - rust_settings - .g1_values_monomial - .iter() - .map(|r| r.to_blst_p1()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_lagrange_brp: Box::leak( - rust_settings - .g1_values_lagrange_brp - .iter() - .map(|r| r.to_blst_p1()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g2_values_monomial: Box::leak( - rust_settings - .g2_values_monomial - .iter() - .map(|r| r.to_blst_p2()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - x_ext_fft_columns: Box::leak( - rust_settings - .x_ext_fft_columns - .iter() - .map(|r| { - Box::leak( - r.iter() - .map(|it| it.to_blst_p1()) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr() - }) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - tables: core::ptr::null_mut(), - wbits: 0, - scratch_size: 0, - } -} diff --git a/constantine/tests/c_bindings.rs b/constantine/tests/c_bindings.rs index 0aead562b..857ce672b 100644 --- a/constantine/tests/c_bindings.rs +++ b/constantine/tests/c_bindings.rs @@ -1,4 +1,4 @@ -#[cfg(test)] +#[cfg(all(test, feature = "c_bindings"))] mod tests { use kzg_bench::tests::c_bindings::{ blob_to_kzg_commitment_invalid_blob_test, diff --git a/run-c-kzg-4844-benches.sh b/run-c-kzg-4844-benches.sh index d071f87a9..a1d96eaca 100755 --- a/run-c-kzg-4844-benches.sh +++ b/run-c-kzg-4844-benches.sh @@ -39,10 +39,10 @@ cd $backend if [[ "$parallel" = true ]]; then print_msg "Using parallel version" - cargo rustc --release --crate-type=staticlib --features=parallel + cargo rustc --release --crate-type=staticlib --features=c_bindings,parallel else print_msg "Using non-parallel version" - cargo rustc --release --crate-type=staticlib + cargo rustc --release --crate-type=staticlib --features=c_bindings fi ###################### cloning c-kzg-4844 ###################### diff --git a/run-c-kzg-4844-fuzz.sh b/run-c-kzg-4844-fuzz.sh index dc5678bad..47056d4e5 100644 --- a/run-c-kzg-4844-fuzz.sh +++ b/run-c-kzg-4844-fuzz.sh @@ -42,16 +42,16 @@ if [ "$backend" == "unknown" ]; then exit 1 fi -features="" +features="c_bindings" #Check if --arkmsm is specified if [ "$use_arkmsm" = true ]; then - features+="arkmsm," + features+=",arkmsm" fi # Check if --bgmw is specified if [ "$use_bgmw" = true ]; then - features+="bgmw" + features+=",bgmw" fi # Trim the trailing comma, if any @@ -63,7 +63,7 @@ cd $backend if [[ "$parallel" = true ]]; then print_msg "Using parallel version" - cargo rustc --release --crate-type=staticlib --features="$features" + cargo rustc --release --crate-type=staticlib --features="$features,parallel" else print_msg "Using non-parallel version" cargo rustc --release --crate-type=staticlib --features="$features" diff --git a/run-c-kzg-4844-tests.sh b/run-c-kzg-4844-tests.sh index 1e24354de..13dabed84 100755 --- a/run-c-kzg-4844-tests.sh +++ b/run-c-kzg-4844-tests.sh @@ -37,10 +37,10 @@ cd $backend if [[ "$parallel" = true ]]; then print_msg "Using parallel version" - cargo rustc --release --crate-type=staticlib --features=parallel + cargo rustc --release --crate-type=staticlib --features=c_bindings,parallel else print_msg "Using non-parallel version" - cargo rustc --release --crate-type=staticlib + cargo rustc --release --crate-type=staticlib --features=c_bindings fi ###################### cloning c-kzg-4844 ###################### diff --git a/zkcrypto/Cargo.toml b/zkcrypto/Cargo.toml index 6cb3b4ce0..eb3bc423f 100644 --- a/zkcrypto/Cargo.toml +++ b/zkcrypto/Cargo.toml @@ -24,7 +24,6 @@ rand = { version = "0.8.5" } default = [ "std", "rand", - "c_bindings" ] std = [ "kzg/std", diff --git a/zkcrypto/src/eip_4844.rs b/zkcrypto/src/eip_4844.rs index b48c40fe2..d715143f0 100644 --- a/zkcrypto/src/eip_4844.rs +++ b/zkcrypto/src/eip_4844.rs @@ -1,18 +1,27 @@ extern crate alloc; -use crate::kzg_proofs::KZGSettings; -use crate::utils::{handle_ckzg_badargs, kzg_settings_to_c, PRECOMPUTATION_TABLES}; -use kzg::eip_4844::{ - load_trusted_setup_rust, BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, - TRUSTED_SETUP_NUM_G2_POINTS, +#[cfg(feature = "c_bindings")] +use crate::utils::PRECOMPUTATION_TABLES; +#[cfg(feature = "c_bindings")] +use crate::{ + kzg_proofs::KZGSettings, + kzg_types::{ZFr, ZG1, ZG2}, }; -use kzg::eth::c_bindings::{ - Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof, +#[cfg(feature = "c_bindings")] +use core::ptr; +use kzg::eip_4844::load_trusted_setup_rust; +#[cfg(feature = "c_bindings")] +use kzg::{ + eip_4844::{ + BYTES_PER_G1, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, + TRUSTED_SETUP_NUM_G2_POINTS, + }, + eth, + eth::c_bindings::{Blob, Bytes32, Bytes48, CKZGSettings, CKzgRet, KZGCommitment, KZGProof}, + Fr, G1, }; -use kzg::{eth, Fr, G1}; -use std::ptr::{self}; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", feature = "c_bindings"))] use libc::FILE; #[cfg(feature = "std")] use std::fs::File; @@ -22,6 +31,99 @@ use std::io::Read; #[cfg(feature = "std")] use kzg::eip_4844::load_trusted_setup_string; +#[cfg(feature = "c_bindings")] +pub(crate) fn kzg_settings_to_c(rust_settings: &KZGSettings) -> CKZGSettings { + CKZGSettings { + roots_of_unity: Box::leak( + rust_settings + .fs + .roots_of_unity + .iter() + .map(ZFr::to_blst_fr) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + brp_roots_of_unity: Box::leak( + rust_settings + .fs + .brp_roots_of_unity + .iter() + .map(ZFr::to_blst_fr) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + reverse_roots_of_unity: Box::leak( + rust_settings + .fs + .reverse_roots_of_unity + .iter() + .map(ZFr::to_blst_fr) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_monomial: Box::leak( + rust_settings + .g1_values_monomial + .iter() + .map(ZG1::to_blst_p1) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g1_values_lagrange_brp: Box::leak( + rust_settings + .g1_values_lagrange_brp + .iter() + .map(ZG1::to_blst_p1) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + g2_values_monomial: Box::leak( + rust_settings + .g2_values_monomial + .iter() + .map(ZG2::to_blst_p2) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + x_ext_fft_columns: Box::leak( + rust_settings + .x_ext_fft_columns + .iter() + .map(|r| { + Box::leak( + r.iter() + .map(ZG1::to_blst_p1) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr() + }) + .collect::>() + .into_boxed_slice(), + ) + .as_mut_ptr(), + tables: core::ptr::null_mut(), + wbits: 0, + scratch_size: 0, + } +} + +#[cfg(feature = "c_bindings")] +macro_rules! handle_ckzg_badargs { + ($x: expr) => { + match $x { + Ok(value) => value, + Err(_) => return kzg::eth::c_bindings::CKzgRet::BadArgs, + } + }; +} + /// # Safety #[cfg(feature = "c_bindings")] #[no_mangle] diff --git a/zkcrypto/src/eip_7594.rs b/zkcrypto/src/eip_7594.rs index 216a9d561..0da0d90e5 100644 --- a/zkcrypto/src/eip_7594.rs +++ b/zkcrypto/src/eip_7594.rs @@ -2,8 +2,6 @@ extern crate alloc; use kzg::EcBackend; -use kzg::c_bindings_eip7594; - use crate::kzg_proofs::FFTSettings; use crate::kzg_proofs::KZGSettings; use crate::kzg_types::ZFp; @@ -26,4 +24,5 @@ impl EcBackend for ZBackend { type KZGSettings = KZGSettings; } -c_bindings_eip7594!(ZBackend); +#[cfg(feature = "c_bindings")] +kzg::c_bindings_eip7594!(ZBackend); diff --git a/zkcrypto/src/utils.rs b/zkcrypto/src/utils.rs index 6223da915..df1455ab4 100644 --- a/zkcrypto/src/utils.rs +++ b/zkcrypto/src/utils.rs @@ -1,7 +1,7 @@ use super::P1; use crate::{ - kzg_proofs::{FFTSettings, KZGSettings}, - kzg_types::{ZFp, ZFr, ZG1Affine, ZG1, ZG2}, + kzg_proofs::FFTSettings, + kzg_types::{ZFp, ZFr, ZG1Affine, ZG1}, P2, }; use bls12_381::{G1Projective, G2Projective, Scalar}; @@ -65,16 +65,6 @@ pub const fn pc_g2projective_into_blst_p2(p2: G2Projective) -> blst_p2 { blst_p2 { x, y, z } } -macro_rules! handle_ckzg_badargs { - ($x: expr) => { - match $x { - Ok(value) => value, - Err(_) => return kzg::eth::c_bindings::CKzgRet::BadArgs, - } - }; -} -pub(crate) use handle_ckzg_badargs; - pub(crate) fn fft_settings_to_rust(c_settings: *const CKZGSettings) -> Result { let settings = unsafe { &*c_settings }; @@ -117,87 +107,5 @@ pub(crate) fn fft_settings_to_rust(c_settings: *const CKZGSettings) -> Result CKZGSettings { - CKZGSettings { - roots_of_unity: Box::leak( - rust_settings - .fs - .roots_of_unity - .iter() - .map(ZFr::to_blst_fr) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - brp_roots_of_unity: Box::leak( - rust_settings - .fs - .brp_roots_of_unity - .iter() - .map(ZFr::to_blst_fr) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - reverse_roots_of_unity: Box::leak( - rust_settings - .fs - .reverse_roots_of_unity - .iter() - .map(ZFr::to_blst_fr) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_monomial: Box::leak( - rust_settings - .g1_values_monomial - .iter() - .map(ZG1::to_blst_p1) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g1_values_lagrange_brp: Box::leak( - rust_settings - .g1_values_lagrange_brp - .iter() - .map(ZG1::to_blst_p1) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - g2_values_monomial: Box::leak( - rust_settings - .g2_values_monomial - .iter() - .map(ZG2::to_blst_p2) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - x_ext_fft_columns: Box::leak( - rust_settings - .x_ext_fft_columns - .iter() - .map(|r| { - Box::leak( - r.iter() - .map(ZG1::to_blst_p1) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr() - }) - .collect::>() - .into_boxed_slice(), - ) - .as_mut_ptr(), - tables: core::ptr::null_mut(), - wbits: 0, - scratch_size: 0, - } -} - pub(crate) static mut PRECOMPUTATION_TABLES: PrecomputationTableManager = PrecomputationTableManager::new();