diff --git a/arkworks/benches/eip_4844.rs b/arkworks/benches/eip_4844.rs index b29fdd45c..0b996b34a 100644 --- a/arkworks/benches/eip_4844.rs +++ b/arkworks/benches/eip_4844.rs @@ -1,8 +1,8 @@ use criterion::{criterion_group, criterion_main, Criterion}; use kzg::eip_4844::{ blob_to_kzg_commitment_rust, bytes_to_blob, compute_blob_kzg_proof_rust, - compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust, - verify_kzg_proof_rust, compute_cells_and_kzg_proofs_rust, + compute_cells_and_kzg_proofs_rust, compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust, + verify_blob_kzg_proof_rust, verify_kzg_proof_rust, }; use kzg_bench::benches::eip_4844::bench_eip_4844; use rust_kzg_arkworks::eip_4844::load_trusted_setup_filename_rust; @@ -21,7 +21,7 @@ fn bench_eip_4844_(c: &mut Criterion) { &compute_blob_kzg_proof_rust, &verify_blob_kzg_proof_rust, &verify_blob_kzg_proof_batch_rust, - &compute_cells_and_kzg_proofs_rust + &compute_cells_and_kzg_proofs_rust, ); } diff --git a/arkworks/src/eip_4844.rs b/arkworks/src/eip_4844.rs index 2ded5b009..c98e372a3 100644 --- a/arkworks/src/eip_4844.rs +++ b/arkworks/src/eip_4844.rs @@ -8,9 +8,9 @@ use kzg::eip_4844::{ blob_to_kzg_commitment_rust, compute_blob_kzg_proof_rust, compute_kzg_proof_rust, load_trusted_setup_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust, verify_kzg_proof_rust, Blob, Bytes32, Bytes48, CKZGSettings, KZGCommitment, KZGProof, - PrecomputationTableManager, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET, - C_KZG_RET_BADARGS, C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, - TRUSTED_SETUP_NUM_G2_POINTS, BYTES_PER_BLOB, + PrecomputationTableManager, BYTES_PER_BLOB, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, + BYTES_PER_G2, C_KZG_RET, C_KZG_RET_BADARGS, C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB, + TRUSTED_SETUP_NUM_G1_POINTS, TRUSTED_SETUP_NUM_G2_POINTS, }; use kzg::{cfg_into_iter, Fr, G1}; use std::ptr::null_mut; @@ -424,7 +424,6 @@ pub unsafe extern "C" fn compute_kzg_proof( C_KZG_RET_OK } - /// # Safety #[no_mangle] pub unsafe extern "C" fn compute_cells_and_kzg_proofs( @@ -437,4 +436,4 @@ pub unsafe extern "C" fn compute_cells_and_kzg_proofs( if cells.is_null() || proofs.is_null() || blob.is_null() || s.is_null() { return C_KZG_RET_BADARGS; } -} \ No newline at end of file +} diff --git a/blst/tests/eip_7694.rs b/blst/tests/eip_7694.rs new file mode 100644 index 000000000..c3c7a219c --- /dev/null +++ b/blst/tests/eip_7694.rs @@ -0,0 +1,35 @@ +#[cfg(test)] +mod tests { + use kzg::eip_4844::{bytes_to_blob, compute_cells_and_kzg_proofs_rust}; + use kzg_bench::tests::eip_7594::test_vectors_compute_cells_and_kzg_proofs; + use rust_kzg_blst::{ + eip_4844::load_trusted_setup, + types::{ + fft_settings::FsFFTSettings, + fp::FsFp, + fr::FsFr, + g1::{FsG1, FsG1Affine}, + g2::FsG2, + kzg_settings::FsKZGSettings, + poly::FsPoly, + }, + }; + + #[test] + pub fn bytes_to_bls_field_test_() { + test_vectors_compute_cells_and_kzg_proofs::< + FsFr, + FsG1, + FsG2, + FsPoly, + FsFFTSettings, + FsKZGSettings, + FsFp, + FsG1Affine, + >( + &load_trusted_setup, + &compute_cells_and_kzg_proofs_rust, + &bytes_to_blob, + ); + } +} diff --git a/constantine/benches/eip_4844.rs b/constantine/benches/eip_4844.rs index 6e26a2e1e..a9d940e01 100644 --- a/constantine/benches/eip_4844.rs +++ b/constantine/benches/eip_4844.rs @@ -1,6 +1,8 @@ use criterion::{criterion_group, criterion_main, Criterion}; use kzg::eip_4844::{ - blob_to_kzg_commitment_rust, bytes_to_blob, compute_blob_kzg_proof_rust, compute_cells_and_kzg_proofs_rust, compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust, verify_kzg_proof_rust + blob_to_kzg_commitment_rust, bytes_to_blob, compute_blob_kzg_proof_rust, + compute_cells_and_kzg_proofs_rust, compute_kzg_proof_rust, verify_blob_kzg_proof_batch_rust, + verify_blob_kzg_proof_rust, verify_kzg_proof_rust, }; use kzg_bench::benches::eip_4844::bench_eip_4844; use rust_kzg_constantine::{ diff --git a/kzg-bench/src/benches/eip_4844.rs b/kzg-bench/src/benches/eip_4844.rs index e6f9e6746..57bb2a558 100644 --- a/kzg-bench/src/benches/eip_4844.rs +++ b/kzg-bench/src/benches/eip_4844.rs @@ -24,7 +24,10 @@ pub fn bench_eip_4844< compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, - compute_cells_and_kzg_proofs: &dyn Fn(&[u8], &TKZGSettings) -> Result<(Vec>, Vec), String>, + compute_cells_and_kzg_proofs: &dyn Fn( + &[u8], + &TKZGSettings, + ) -> Result<(Vec>, Vec), String>, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof_batch: &dyn Fn( &[Vec], diff --git a/kzg-bench/src/test_vectors/compute_cells_and_kzg_proofs.rs b/kzg-bench/src/test_vectors/compute_cells_and_kzg_proofs.rs index 3097ae491..60e513db9 100644 --- a/kzg-bench/src/test_vectors/compute_cells_and_kzg_proofs.rs +++ b/kzg-bench/src/test_vectors/compute_cells_and_kzg_proofs.rs @@ -1,20 +1,17 @@ #![allow(dead_code)] +use alloc::string::String; +use alloc::vec::Vec; use serde::Deserialize; #[derive(Deserialize)] pub struct Input<'a> { blob: &'a str, - z: &'a str, } impl Input<'_> { - pub fn get_blob_bytes(&self) -> Vec { - hex::decode(&self.blob[2..]).unwrap() - } - - pub fn get_z_bytes(&self) -> Vec { - hex::decode(&self.z[2..]).unwrap() + pub fn get_blob_bytes(&self) -> Result, String> { + hex::decode(self.blob[2..]).map_err(|_| "Invalid blob".to_string()) } } @@ -22,16 +19,21 @@ impl Input<'_> { pub struct Test<'a> { #[serde(borrow)] pub input: Input<'a>, - #[serde(borrow)] - output: Option<(&'a str, &'a str)>, + output: Option<(Vec, Vec)>, } impl Test<'_> { - pub fn get_output_bytes(&self) -> Option<(Vec, Vec)> { - self.output.map(|(proof, y)| { + pub fn get_output(&self) -> Option<(Vec>, Vec>)> { + self.output.clone().map(|(cells, proofs)| { ( - hex::decode(&proof[2..]).unwrap(), - hex::decode(&y[2..]).unwrap(), + cells + .iter() + .map(|s| hex::decode(s[2..]).unwrap()) + .collect::>(), + proofs + .iter() + .map(|s| hex::decode(s[2..]).unwrap()) + .collect::>(), ) }) } diff --git a/kzg-bench/src/test_vectors/mod.rs b/kzg-bench/src/test_vectors/mod.rs index 9ea93593b..14e963287 100644 --- a/kzg-bench/src/test_vectors/mod.rs +++ b/kzg-bench/src/test_vectors/mod.rs @@ -1,7 +1,9 @@ pub mod blob_to_kzg_commitment; pub mod compute_blob_kzg_proof; +pub mod compute_cells_and_kzg_proofs; pub mod compute_kzg_proof; +pub mod recover_cells_and_kzg_proofs; pub mod verify_blob_kzg_proof; pub mod verify_blob_kzg_proof_batch; +pub mod verify_cell_kzg_proof_batch; pub mod verify_kzg_proof; -pub mod compute_cells_and_kzg_proofs; diff --git a/kzg-bench/src/test_vectors/recover_cells_and_kzg_proofs.rs b/kzg-bench/src/test_vectors/recover_cells_and_kzg_proofs.rs new file mode 100644 index 000000000..df3a7ae99 --- /dev/null +++ b/kzg-bench/src/test_vectors/recover_cells_and_kzg_proofs.rs @@ -0,0 +1,49 @@ +#![allow(dead_code)] + +use crate::Bytes48; +use crate::{Cell, Error}; +use alloc::string::String; +use alloc::vec::Vec; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct Input { + cell_indices: Vec, + cells: Vec, +} + +impl Input { + pub fn get_cell_indices(&self) -> Result, Error> { + Ok(self.cell_indices.clone()) + } + + pub fn get_cells(&self) -> Result, Error> { + self.cells + .iter() + .map(|s| Cell::from_hex(s)) + .collect::, Error>>() + } +} + +#[derive(Deserialize)] +pub struct Test { + pub input: Input, + output: Option<(Vec, Vec)>, +} + +impl Test { + pub fn get_output(&self) -> Option<(Vec, Vec)> { + self.output.clone().map(|(cells, proofs)| { + ( + cells + .iter() + .map(|s| Cell::from_hex(s).unwrap()) + .collect::>(), + proofs + .iter() + .map(|s| Bytes48::from_hex(s).unwrap()) + .collect::>(), + ) + }) + } +} diff --git a/kzg-bench/src/test_vectors/verify_cell_kzg_proof_batch.rs b/kzg-bench/src/test_vectors/verify_cell_kzg_proof_batch.rs new file mode 100644 index 000000000..ac1d1cd92 --- /dev/null +++ b/kzg-bench/src/test_vectors/verify_cell_kzg_proof_batch.rs @@ -0,0 +1,53 @@ +#![allow(dead_code)] + +use crate::{Bytes48, Cell, Error}; +use alloc::string::String; +use alloc::vec::Vec; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct Input { + commitments: Vec, + cell_indices: Vec, + cells: Vec, + proofs: Vec, +} + +impl Input { + pub fn get_commitments(&self) -> Result, Error> { + self.commitments + .iter() + .map(|s| Bytes48::from_hex(s)) + .collect::, Error>>() + } + + pub fn get_cell_indices(&self) -> Result, Error> { + Ok(self.cell_indices.clone()) + } + + pub fn get_cells(&self) -> Result, Error> { + self.cells + .iter() + .map(|s| Cell::from_hex(s)) + .collect::, Error>>() + } + + pub fn get_proofs(&self) -> Result, Error> { + self.proofs + .iter() + .map(|s| Bytes48::from_hex(s)) + .collect::, Error>>() + } +} + +#[derive(Deserialize)] +pub struct Test { + pub input: Input, + output: Option, +} + +impl Test { + pub fn get_output(&self) -> Option { + self.output + } +} diff --git a/kzg-bench/src/tests/eip_7594.rs b/kzg-bench/src/tests/eip_7594.rs new file mode 100644 index 000000000..86c27233b --- /dev/null +++ b/kzg-bench/src/tests/eip_7594.rs @@ -0,0 +1,72 @@ +use super::utils::{get_manifest_dir, get_trusted_setup_path}; +use crate::test_vectors::compute_cells_and_kzg_proofs; +use kzg::{FFTSettings, Fr, G1Affine, G1Fp, G1GetFp, G1Mul, KZGSettings, Poly, G1, G2}; +use std::path::PathBuf; + +const COMPUTE_CELLS_AND_KZG_PROOFS_TEST_VECTORS: &str = + "src/test_vectors/compute_cells_and_kzg_proofs/*/*/*"; +const RECOVER_CELLS_AND_KZG_PROOFS_TEST_VECTORS: &str = + "src/test_vectors/recover_cells_and_kzg_proofs/*/*/*"; +const VERIFY_CELL_KZG_PROOF_BATCH_TEST_VECTORS: &str = + "src/test_vectors/verify_cell_kzg_proof_batch/*/*/*"; + +pub fn test_vectors_compute_cells_and_kzg_proofs< + TFr: Fr, + TG1: G1 + G1Mul + G1GetFp, + TG2: G2, + TPoly: Poly, + TFFTSettings: FFTSettings, + TKZGSettings: KZGSettings, + TG1Fp: G1Fp, + TG1Affine: G1Affine, +>( + load_trusted_setup: &dyn Fn(&str) -> Result, + compute_cells_and_kzg_proofs: &dyn Fn( + &[TFr], + &TKZGSettings, + ) -> Result<(Vec, Vec), String>, + bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, +) { + let settings = load_trusted_setup(get_trusted_setup_path().as_str()).unwrap(); + let test_files: Vec = glob::glob(&format!( + "{}/{}", + get_manifest_dir(), + COMPUTE_CELLS_AND_KZG_PROOFS_TEST_VECTORS + )) + .unwrap() + .collect::>>() + .unwrap(); + assert!(!test_files.is_empty()); + + for test_file in test_files { + let yaml_data = fs::read_to_string(test_file).unwrap(); + let test: compute_cells_and_kzg_proofs::Test = serde_yaml::from_str(&yaml_data).unwrap(); + + let blob = match bytes_to_blob(&test.input.get_blob_bytes()) { + Ok(blob) => blob, + Err(_) => { + assert!(test.get_output().is_none()); + continue; + } + }; + + match compute_cells_and_kzg_proofs(&blob, &settings) { + Err(_) => assert!(test.get_output().is_none()), + Ok((recv_cells, recv_proofs)) => { + let (exp_cells, exp_proofs) = test.get_output().unwrap(); + + let recv_cells = recv_cells + .into_iter() + .map(|it| it.to_bytes().to_vec()) + .collect::>>(); + let recv_proofs = recv_proofs + .into_iter() + .map(|it| it.to_bytes().to_vec()) + .collect::>>(); + + assert_eq!(recv_cells, exp_cells, "Cells do not match"); + assert_eq!(recv_proofs, exp_proofs, "Proofs do not match"); + } + } + } +} diff --git a/kzg-bench/src/tests/mod.rs b/kzg-bench/src/tests/mod.rs index 8115db334..4fbb022e5 100644 --- a/kzg-bench/src/tests/mod.rs +++ b/kzg-bench/src/tests/mod.rs @@ -3,6 +3,7 @@ pub mod c_bindings; pub mod consts; pub mod das; pub mod eip_4844; +pub mod eip_7594; pub mod fft_fr; pub mod fft_g1; pub mod finite; diff --git a/kzg/src/eip_4844.rs b/kzg/src/eip_4844.rs index 018f06941..9fc3bcb45 100644 --- a/kzg/src/eip_4844.rs +++ b/kzg/src/eip_4844.rs @@ -1,6 +1,18 @@ #![allow(non_camel_case_types)] extern crate alloc; +use crate::common_utils::reverse_bit_order; +use crate::fk20_proof::{ + fk20_multi_da_opt, fk20_single_da_opt, FK20MultiSettings, FK20SingleSettings, + KzgFK20MultiSettings, KzgFK20SingleSettings, +}; +use crate::msm::precompute::PrecomputationTable; +use crate::FFTFr; +use crate::G1Affine; +use crate::G1Fp; +use crate::G1GetFp; +use crate::G1LinComb; +use crate::{FFTSettings, Fr, G1Mul, KZGSettings, PairingVerify, Poly, G1, G2}; use alloc::collections::BTreeMap; use alloc::format; use alloc::string::String; @@ -8,25 +20,16 @@ use alloc::string::ToString; use alloc::sync::Arc; use alloc::vec; use alloc::vec::Vec; -use sha2::digest::generic_array::sequence; -use core::cell; -use core::result; pub use blst::{blst_fr, blst_p1, blst_p2}; +use core::cell; use core::ffi::c_uint; use core::hash::Hash; use core::hash::Hasher; +use core::result; +use kzg::{FFTFr, Fr}; +use sha2::digest::generic_array::sequence; use sha2::{Digest, Sha256}; use siphasher::sip::SipHasher; -use kzg::{FFTFr, Fr}; -use crate::common_utils::reverse_bit_order; -use crate::msm::precompute::PrecomputationTable; -use crate::FFTFr; -use crate::G1Affine; -use crate::G1Fp; -use crate::G1GetFp; -use crate::G1LinComb; -use crate::{FFTSettings, Fr, G1Mul, KZGSettings, PairingVerify, Poly, G1, G2}; -use crate::fk20_proof::{KzgFK20SingleSettings, KzgFK20MultiSettings, FK20SingleSettings, FK20MultiSettings, fk20_single_da_opt, fk20_multi_da_opt}; #[cfg(feature = "parallel")] use rayon::prelude::*; @@ -66,8 +69,10 @@ pub const FIELD_ELEMENTS_PER_EXT_BLOB: usize = 2 * FIELD_ELEMENTS_PER_BLOB; pub const FIELD_ELEMENTS_PER_CELL: usize = 64; pub const BYTES_PER_CELL: usize = FIELD_ELEMENTS_PER_CELL * BYTES_PER_FIELD_ELEMENT; pub const CELLS_PER_EXT_BLOB: usize = FIELD_ELEMENTS_PER_EXT_BLOB / FIELD_ELEMENTS_PER_CELL; -pub const RANDOM_CHALLENGE_KZG_CELL_BATCH_DOMAIN: [u8; 32] = [82, 67, 95, 86, 69, 82, 73, 70, 89, 95, 67, 69, 76, 76, 95, 75, -90, 71, 95, 80, 82, 79, 79, 70, 95, 66, 65, 84, 67, 72, 95, 86,]; // "b'RCKZGCBATCH__V1_'" +pub const RANDOM_CHALLENGE_KZG_CELL_BATCH_DOMAIN: [u8; 32] = [ + 82, 67, 95, 86, 69, 82, 73, 70, 89, 95, 67, 69, 76, 76, 95, 75, 90, 71, 95, 80, 82, 79, 79, 70, + 95, 66, 65, 84, 67, 72, 95, 86, +]; // "b'RCKZGCBATCH__V1_'" ////////////////////////////// C API for EIP-4844 ////////////////////////////// @@ -744,7 +749,6 @@ pub fn verify_blob_kzg_proof_batch_rust< } } - #[allow(clippy::useless_conversion)] pub fn bytes_to_blob(bytes: &[u8]) -> Result, String> { if bytes.len() != BYTES_PER_BLOB { @@ -947,62 +951,3 @@ pub fn load_trusted_setup_rust< reverse_bit_order(&mut g1_values)?; TKZGSettings::new(g1_values.as_slice(), g2_values.as_slice(), max_scale, &fs) } - -////////////////////////////// Trait based implementations of functions for EIP-7594 ////////////////////////////// - -pub fn compute_cells_and_kzg_proofs_rust< - TFr: Fr, - TPoly: Poly, - TG1: G1 + G1Mul + G1GetFp + PairingVerify, - TG2: G2, - TG1Fp: G1Fp, - TG1Affine: G1Affine, - TFFTSettings: FFTSettings + FFTFr, - TKZGSettings: KZGSettings, ->( - blob: &[TFr], - s: &TKZGSettings, -) -> Result< -( - Vec, - Vec -), String>{ - // Ensure blob length is equal to Bytes per blob - if blob.len() != BYTES_PER_BLOB { - return Err(String::from("Blob length must be BYTES_PER_BLOB")); - } - // Convert the blob to a polynomial. - polynomial = blob_to_polynomial(blob)?; - - // Allocate arrays to hold cells and proofs - let mut cells = vec![TFr::default(); CELLS_PER_EXT_BLOB]; - let mut proofs = vec![TFr::default(); CELLS_PER_EXT_BLOB]; - - // Compute cells - let mut data_fr = vec![TFr::zero(); FIELD_ELEMENTS_PER_EXT_BLOB]; - - // Perform FFT on the polynomial - data_fr = s.get_fft_settings().fft_fr(&polynomial.get_coeffs(), false)?; - - // Perform bit reversal permutation - reverse_bit_order(&mut data_fr)?; - - // Covert field elements to cell bytes - for (i, cell) in cells.iter_mut().enumerate() { - for j in 0..FIELD_ELEMENTS_PER_CELL { - let index = i * FIELD_ELEMENTS_PER_CELL + j; - let fr_bytes = data_fr[index].to_bytes(); - cell.bytes[j * BYTES_PER_FIELD_ELEMENT..(j+1) * BYTES_PER_FIELD_ELEMENT].copy_from_slice(&fr_bytes); - } - } - - // Compute proofs - let mut proofs_g1 = vec![TG1::identity(); CELLS_PER_EXT_BLOB]; - compute_fk20_proofs(&mut proofs_g1, &polynomial, FIELD_ELEMENTS_PER_BLOB, s); - reverse_bit_order(&mut proofs_g1)?; - - Ok((cells, proofs)) - -} - - diff --git a/kzg/src/eip_7694.rs b/kzg/src/eip_7694.rs new file mode 100644 index 000000000..edd99f25a --- /dev/null +++ b/kzg/src/eip_7694.rs @@ -0,0 +1,63 @@ + +////////////////////////////// Trait based implementations of functions for EIP-7594 ////////////////////////////// + +use crate::{common_utils::reverse_bit_order, eip_4844::{blob_to_polynomial, FIELD_ELEMENTS_PER_BLOB}, FFTFr, FFTSettings, Fr, G1Affine, G1Fp, G1GetFp, G1Mul, KZGSettings, PairingVerify, Poly, G1, G2}; + +pub fn poly_lagrange_to_monomial>(polynomial: TPoly) { + reverse_bit_order(vals); +} + +pub fn compute_cells_and_kzg_proofs_rust< + TFr: Fr, + TPoly: Poly, + TG1: G1 + G1Mul + G1GetFp + PairingVerify, + TG2: G2, + TG1Fp: G1Fp, + TG1Affine: G1Affine, + TFFTSettings: FFTSettings + FFTFr, + TKZGSettings: KZGSettings, +>( + blob: &[TFr], + s: &TKZGSettings, +) -> Result<(Vec, Vec), String> { + // Ensure blob length is equal to Bytes per blob + if blob.len() != FIELD_ELEMENTS_PER_BLOB { + return Err(String::from("Blob length must be FIELD_ELEMENTS_PER_BLOB")); + } + // Convert the blob to a polynomial. + let polynomial: TPoly = blob_to_polynomial(blob)?; + + // poly_lagrange_to_monomial(polynomial, ) + + // Allocate arrays to hold cells and proofs + let mut cells = vec![TFr::default(); CELLS_PER_EXT_BLOB]; + let mut proofs = vec![TFr::default(); CELLS_PER_EXT_BLOB]; + + // Compute cells + let mut data_fr = vec![TFr::zero(); FIELD_ELEMENTS_PER_EXT_BLOB]; + + // Perform FFT on the polynomial + data_fr = s + .get_fft_settings() + .fft_fr(&polynomial.get_coeffs(), false)?; + + // Perform bit reversal permutation + reverse_bit_order(&mut data_fr)?; + + // Covert field elements to cell bytes + for (i, cell) in cells.iter_mut().enumerate() { + for j in 0..FIELD_ELEMENTS_PER_CELL { + let index = i * FIELD_ELEMENTS_PER_CELL + j; + let fr_bytes = data_fr[index].to_bytes(); + cell.bytes[j * BYTES_PER_FIELD_ELEMENT..(j + 1) * BYTES_PER_FIELD_ELEMENT] + .copy_from_slice(&fr_bytes); + } + } + + // Compute proofs + let mut proofs_g1 = vec![TG1::identity(); CELLS_PER_EXT_BLOB]; + compute_fk20_proofs(&mut proofs_g1, &polynomial, FIELD_ELEMENTS_PER_BLOB, s); + reverse_bit_order(&mut proofs_g1)?; + + Ok((cells, proofs)) +} diff --git a/kzg/src/fk20_proof.rs b/kzg/src/fk20_proof.rs index e640d154c..65f52e234 100644 --- a/kzg/src/fk20_proof.rs +++ b/kzg/src/fk20_proof.rs @@ -1,20 +1,31 @@ -use crate::consts::G1_IDENTITY; -use crate::eip_4844::FIELD_ELEMENTS_PER_CELL; -use crate::kzg_types::{ArkG1, ArkFr as BlstFr}; -use crate::utils::PolyData; -use crate::{FFTFr, G1Mul, KZGSettings, G1}; -use crate::utils::PolyData; -use kzg::{G1, G1Mul, FFTSetings, FFTFr}; +// use crate::consts::G1_IDENTITY; +// use crate::eip_4844::FIELD_ELEMENTS_PER_CELL; +// use crate::kzg_types::{ArkFr as BlstFr, ArkG1}; +// use crate::utils::PolyData; +// use crate::utils::PolyData; +// use crate::{FFTFr, G1Mul, KZGSettings, G1}; +// use kzg::{FFTFr, FFTSetings, G1Mul, G1}; +use crate::{ + eip_4844::FIELD_ELEMENTS_PER_CELL, FFTFr, FFTSettings, Fr, G1Affine, G1Fp, G1GetFp, G1Mul, + KZGSettings, Poly, G1, G2, +}; fn compute_fk20_proofs< - TFr: FFTFr, - TG1: G1 + G1Mul + TFr: Fr, + TFFTFr: FFTFr, + TG1Fp: G1Fp, + TG1: G1 + G1Mul + G1GetFp, + TG2: G2, + TPoly: Poly, + TG1Affine: G1Affine, + TKZGSettings: KZGSettings, + TFFTSettings: FFTSettings, >( - p: &PolyData, + p: &TPoly, n: usize, - s: &KZGSettings -) -> Result, String>{ + s: &TKZGSettings, +) -> Result, String> { let k = n / FIELD_ELEMENTS_PER_CELL; let k2 = k * 2; @@ -23,10 +34,10 @@ fn compute_fk20_proofs< let mut h_ext_fft = vec![TG1::identity(); k2]; for i in 0..FIELD_ELEMENTS_PER_CELL { - toeplitz_coeffs_stride(p, &mut toeplitz_coeffs, n, FIELD_ELEMENTS_PER_CELL)?; - s.get_fft_settings().fft_fr(&toeplitz_coeffs, false)?; + toeplitz_coeffs_stride(p, &mut toeplitz_coeff, n, FIELD_ELEMENTS_PER_CELL)?; + s.get_fft_settings().fft_fr(&toeplitz_coeff, false)?; for j in 0..k2 { - h_ext_fft[j] = h_ext_fft[j].add_or_dbl(&s.x_ext_fft[j].mul(&toeplitz_coeffs[j])); + h_ext_fft[j] = h_ext_fft[j].add_or_dbl(&s.x_ext_fft[j].mul(&toeplitz_coeff[j])); } } @@ -36,13 +47,12 @@ fn compute_fk20_proofs< *i = h_ext_fft[i.len() - 1]; } for i in h.iter_mut().take(k2).skip(k) { - *i = G1_IDENTITY; + *i = TG1::identity(); } s.get_fft_settings().fft_g1(h.as_mut_slice(), false)?; Ok(h) - } fn toeplitz_coeffs_stride( @@ -77,4 +87,4 @@ fn toeplitz_coeffs_stride( j += stride; } Ok(out) -} \ No newline at end of file +} diff --git a/kzg/src/lib.rs b/kzg/src/lib.rs index ca1fd803a..882a57993 100644 --- a/kzg/src/lib.rs +++ b/kzg/src/lib.rs @@ -9,8 +9,9 @@ use msm::precompute::PrecomputationTable; pub mod common_utils; pub mod eip_4844; -pub mod msm; +pub mod eip_7694; pub mod fk20_proof; +pub mod msm; pub trait Fr: Default + Clone + PartialEq + Sync { fn null() -> Self; @@ -135,7 +136,7 @@ pub trait G1GetFp: G1 + Clone { } pub trait G1Mul: G1 + Clone { - fn mul(&self, b: &TFr) -> Self; + fn mul(&self, b: &TFr) -> Self; } pub trait G1LinComb>: diff --git a/kzg/tests/eip-7594_test.rs b/kzg/tests/eip-7594_test.rs index 9e1a5f842..45e8b4848 100644 --- a/kzg/tests/eip-7594_test.rs +++ b/kzg/tests/eip-7594_test.rs @@ -1,7 +1,7 @@ -use kzg::eip_4844::{compute_cells_and_kzg_proofs_rust, CELLS_PER_EXT_BLOB}; -use rand::Rng; use crate::compute_cells_and_kzg_proofs; -use crate::types::{Blob, Cell, KZGProof, S}; // Adjust imports as necessary +use crate::types::{Blob, Cell, KZGProof, S}; +use kzg::eip_4844::{compute_cells_and_kzg_proofs_rust, CELLS_PER_EXT_BLOB}; +use rand::Rng; // Adjust imports as necessary #[test] @@ -18,6 +18,6 @@ fn test_compute_cells_and_kzg_proofs() { } for _ in 0..5 { let ret = compute_cells_and_kzg_proofs_rust(blob, s); - assert_eq!(ret, C_KZG_RET_OK); // Replace `C_KZG_OK` with the appropriate success constant or value in Rust + assert_eq!(ret, C_KZG_RET_OK); // Replace `C_KZG_OK` with the appropriate success constant or value in Rust } -} \ No newline at end of file +}