From d04b8379907db29ae0f6c88bb2071a82f371d95d Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 7 Jul 2023 10:54:36 -0500 Subject: [PATCH 1/8] Use boxes & refs for blobs in Rust bindings --- bindings/rust/benches/kzg_benches.rs | 32 +++---- bindings/rust/src/bindings/mod.rs | 94 +++++++++++-------- .../blob_to_kzg_commitment_test.rs | 2 +- .../test_formats/compute_blob_kzg_proof.rs | 2 +- .../test_formats/compute_kzg_proof.rs | 2 +- .../test_formats/verify_blob_kzg_proof.rs | 2 +- .../verify_blob_kzg_proof_batch.rs | 2 +- bindings/rust/src/lib.rs | 4 +- 8 files changed, 78 insertions(+), 62 deletions(-) diff --git a/bindings/rust/benches/kzg_benches.rs b/bindings/rust/benches/kzg_benches.rs index 96b81b0ef..b9fca612c 100644 --- a/bindings/rust/benches/kzg_benches.rs +++ b/bindings/rust/benches/kzg_benches.rs @@ -12,7 +12,7 @@ fn generate_random_field_element(rng: &mut ThreadRng) -> Bytes32 { arr.into() } -fn generate_random_blob(rng: &mut ThreadRng) -> Blob { +fn generate_random_blob(rng: &mut ThreadRng) -> Box { let mut arr = [0u8; BYTES_PER_BLOB]; rng.fill(&mut arr[..]); // Ensure that the blob is canonical by ensuring that @@ -31,12 +31,12 @@ pub fn criterion_benchmark(c: &mut Criterion) { let kzg_settings = Arc::new(KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap()); let blobs: Vec = (0..max_count) - .map(|_| generate_random_blob(&mut rng)) + .map(|_| *generate_random_blob(&mut rng)) .collect(); let commitments: Vec = blobs .iter() .map(|blob| { - KzgCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings) + KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings) .unwrap() .to_bytes() }) @@ -45,7 +45,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { .iter() .zip(commitments.iter()) .map(|(blob, commitment)| { - KzgProof::compute_blob_kzg_proof(blob.clone(), *commitment, &kzg_settings) + KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings) .unwrap() .to_bytes() }) @@ -56,15 +56,15 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("blob_to_kzg_commitment", |b| { b.iter(|| { - KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap().clone(), &kzg_settings) + KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap(), &kzg_settings) }) }); c.bench_function("compute_kzg_proof", |b| { b.iter(|| { KzgProof::compute_kzg_proof( - blobs.first().unwrap().clone(), - *fields.first().unwrap(), + blobs.first().unwrap(), + fields.first().unwrap(), &kzg_settings, ) }) @@ -73,8 +73,8 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("compute_blob_kzg_proof", |b| { b.iter(|| { KzgProof::compute_blob_kzg_proof( - blobs.first().unwrap().clone(), - *commitments.first().unwrap(), + blobs.first().unwrap(), + commitments.first().unwrap(), &kzg_settings, ) }) @@ -83,10 +83,10 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("verify_kzg_proof", |b| { b.iter(|| { KzgProof::verify_kzg_proof( - *commitments.first().unwrap(), - *fields.first().unwrap(), - *fields.first().unwrap(), - *proofs.first().unwrap(), + commitments.first().unwrap(), + fields.first().unwrap(), + fields.first().unwrap(), + proofs.first().unwrap(), &kzg_settings, ) }) @@ -95,9 +95,9 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function("verify_blob_kzg_proof", |b| { b.iter(|| { KzgProof::verify_blob_kzg_proof( - blobs.first().unwrap().clone(), - *commitments.first().unwrap(), - *proofs.first().unwrap(), + blobs.first().unwrap(), + commitments.first().unwrap(), + proofs.first().unwrap(), &kzg_settings, ) }) diff --git a/bindings/rust/src/bindings/mod.rs b/bindings/rust/src/bindings/mod.rs index c3d77281d..34ef3c482 100644 --- a/bindings/rust/src/bindings/mod.rs +++ b/bindings/rust/src/bindings/mod.rs @@ -194,7 +194,7 @@ impl Drop for KZGSettings { } impl Blob { - pub fn from_bytes(bytes: &[u8]) -> Result { + pub fn from_bytes(bytes: &[u8]) -> Result, Error> { if bytes.len() != BYTES_PER_BLOB { return Err(Error::InvalidBytesLength(format!( "Invalid byte length. Expected {} got {}", @@ -204,10 +204,10 @@ impl Blob { } let mut new_bytes = [0; BYTES_PER_BLOB]; new_bytes.copy_from_slice(bytes); - Ok(Self { bytes: new_bytes }) + Ok(Box::new(Self { bytes: new_bytes })) } - pub fn from_hex(hex_str: &str) -> Result { + pub fn from_hex(hex_str: &str) -> Result, Error> { Self::from_bytes(&hex_to_bytes(hex_str)?) } } @@ -277,8 +277,8 @@ impl KZGProof { } pub fn compute_kzg_proof( - blob: Blob, - z_bytes: Bytes32, + blob: &Blob, + z_bytes: &Bytes32, kzg_settings: &KZGSettings, ) -> Result<(Self, Bytes32), Error> { let mut kzg_proof = MaybeUninit::::uninit(); @@ -287,8 +287,8 @@ impl KZGProof { let res = compute_kzg_proof( kzg_proof.as_mut_ptr(), y_out.as_mut_ptr(), - &blob, - &z_bytes, + blob, + z_bytes, kzg_settings, ); if let C_KZG_RET::C_KZG_OK = res { @@ -300,16 +300,16 @@ impl KZGProof { } pub fn compute_blob_kzg_proof( - blob: Blob, - commitment_bytes: Bytes48, + blob: &Blob, + commitment_bytes: &Bytes48, kzg_settings: &KZGSettings, ) -> Result { let mut kzg_proof = MaybeUninit::::uninit(); unsafe { let res = compute_blob_kzg_proof( kzg_proof.as_mut_ptr(), - &blob, - &commitment_bytes, + blob, + commitment_bytes, kzg_settings, ); if let C_KZG_RET::C_KZG_OK = res { @@ -321,20 +321,20 @@ impl KZGProof { } pub fn verify_kzg_proof( - commitment_bytes: Bytes48, - z_bytes: Bytes32, - y_bytes: Bytes32, - proof_bytes: Bytes48, + commitment_bytes: &Bytes48, + z_bytes: &Bytes32, + y_bytes: &Bytes32, + proof_bytes: &Bytes48, kzg_settings: &KZGSettings, ) -> Result { let mut verified: MaybeUninit = MaybeUninit::uninit(); unsafe { let res = verify_kzg_proof( verified.as_mut_ptr(), - &commitment_bytes, - &z_bytes, - &y_bytes, - &proof_bytes, + commitment_bytes, + z_bytes, + y_bytes, + proof_bytes, kzg_settings, ); if let C_KZG_RET::C_KZG_OK = res { @@ -346,18 +346,18 @@ impl KZGProof { } pub fn verify_blob_kzg_proof( - blob: Blob, - commitment_bytes: Bytes48, - proof_bytes: Bytes48, + blob: &Blob, + commitment_bytes: &Bytes48, + proof_bytes: &Bytes48, kzg_settings: &KZGSettings, ) -> Result { let mut verified: MaybeUninit = MaybeUninit::uninit(); unsafe { let res = verify_blob_kzg_proof( verified.as_mut_ptr(), - &blob, - &commitment_bytes, - &proof_bytes, + blob, + commitment_bytes, + proof_bytes, kzg_settings, ); if let C_KZG_RET::C_KZG_OK = res { @@ -429,7 +429,10 @@ impl KZGCommitment { hex::encode(self.bytes) } - pub fn blob_to_kzg_commitment(blob: Blob, kzg_settings: &KZGSettings) -> Result { + pub fn blob_to_kzg_commitment( + blob: &Blob, + kzg_settings: &KZGSettings, + ) -> Result { let mut kzg_commitment: MaybeUninit = MaybeUninit::uninit(); unsafe { let res = blob_to_kzg_commitment( @@ -464,6 +467,12 @@ impl From<[u8; BYTES_PER_BLOB]> for Blob { } } +impl From<[u8; BYTES_PER_BLOB]> for Box { + fn from(value: [u8; BYTES_PER_BLOB]) -> Self { + Box::new(Blob { bytes: value }) + } +} + impl From<[u8; 32]> for Bytes32 { fn from(value: [u8; 32]) -> Self { Self { bytes: value } @@ -541,7 +550,7 @@ mod tests { verify_blob_kzg_proof, verify_blob_kzg_proof_batch, verify_kzg_proof, }; - fn generate_random_blob(rng: &mut ThreadRng) -> Blob { + fn generate_random_blob(rng: &mut ThreadRng) -> Box { let mut arr = [0u8; BYTES_PER_BLOB]; rng.fill(&mut arr[..]); // Ensure that the blob is canonical by ensuring that @@ -559,12 +568,14 @@ mod tests { let num_blobs: usize = rng.gen_range(1..16); let mut blobs: Vec = (0..num_blobs) - .map(|_| generate_random_blob(&mut rng)) + .map(|_| *generate_random_blob(&mut rng)) .collect(); let commitments: Vec = blobs .iter() - .map(|blob| KZGCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings).unwrap()) + .map(|blob| { + KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap() + }) .map(|commitment| commitment.to_bytes()) .collect(); @@ -572,7 +583,8 @@ mod tests { .iter() .zip(commitments.iter()) .map(|(blob, commitment)| { - KZGProof::compute_blob_kzg_proof(blob.clone(), *commitment, &kzg_settings).unwrap() + KZGProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings) + .unwrap() }) .map(|proof| proof.to_bytes()) .collect(); @@ -587,12 +599,16 @@ mod tests { blobs.pop(); - let error = - KZGProof::verify_blob_kzg_proof_batch(&blobs, &commitments, &proofs, &kzg_settings) - .unwrap_err(); + let error = KZGProof::verify_blob_kzg_proof_batch( + &blobs, + &commitments, + &proofs, + &kzg_settings, + ) + .unwrap_err(); assert!(matches!(error, Error::MismatchLength(_))); - let incorrect_blob = generate_random_blob(&mut rng); + let incorrect_blob = *generate_random_blob(&mut rng); blobs.push(incorrect_blob); assert!(!KZGProof::verify_blob_kzg_proof_batch( @@ -641,7 +657,7 @@ mod tests { continue; }; - match KZGCommitment::blob_to_kzg_commitment(blob, &kzg_settings) { + match KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings) { Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes), _ => assert!(test.get_output().is_none()), } @@ -668,7 +684,7 @@ mod tests { continue; }; - match KZGProof::compute_kzg_proof(blob, z, &kzg_settings) { + match KZGProof::compute_kzg_proof(&blob, &z, &kzg_settings) { Ok((proof, y)) => { assert_eq!(proof.bytes, test.get_output().unwrap().0.bytes); assert_eq!(y.bytes, test.get_output().unwrap().1.bytes); @@ -701,7 +717,7 @@ mod tests { continue; }; - match KZGProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings) { + match KZGProof::compute_blob_kzg_proof(&blob, &commitment, &kzg_settings) { Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes), _ => assert!(test.get_output().is_none()), } @@ -733,7 +749,7 @@ mod tests { continue; }; - match KZGProof::verify_kzg_proof(commitment, z, y, proof, &kzg_settings) { + match KZGProof::verify_kzg_proof(&commitment, &z, &y, &proof, &kzg_settings) { Ok(res) => assert_eq!(res, test.get_output().unwrap()), _ => assert!(test.get_output().is_none()), } @@ -764,7 +780,7 @@ mod tests { continue; }; - match KZGProof::verify_blob_kzg_proof(blob, commitment, proof, &kzg_settings) { + match KZGProof::verify_blob_kzg_proof(&blob, &commitment, &proof, &kzg_settings) { Ok(res) => assert_eq!(res, test.get_output().unwrap()), _ => assert!(test.get_output().is_none()), } diff --git a/bindings/rust/src/bindings/test_formats/blob_to_kzg_commitment_test.rs b/bindings/rust/src/bindings/test_formats/blob_to_kzg_commitment_test.rs index d39b13fa5..e90fb4ae1 100644 --- a/bindings/rust/src/bindings/test_formats/blob_to_kzg_commitment_test.rs +++ b/bindings/rust/src/bindings/test_formats/blob_to_kzg_commitment_test.rs @@ -9,7 +9,7 @@ pub struct Input<'a> { } impl Input<'_> { - pub fn get_blob(&self) -> Result { + pub fn get_blob(&self) -> Result, Error> { Blob::from_hex(self.blob) } } diff --git a/bindings/rust/src/bindings/test_formats/compute_blob_kzg_proof.rs b/bindings/rust/src/bindings/test_formats/compute_blob_kzg_proof.rs index 6a4aea9a0..3e479d921 100644 --- a/bindings/rust/src/bindings/test_formats/compute_blob_kzg_proof.rs +++ b/bindings/rust/src/bindings/test_formats/compute_blob_kzg_proof.rs @@ -10,7 +10,7 @@ pub struct Input<'a> { } impl Input<'_> { - pub fn get_blob(&self) -> Result { + pub fn get_blob(&self) -> Result, Error> { Blob::from_hex(self.blob) } diff --git a/bindings/rust/src/bindings/test_formats/compute_kzg_proof.rs b/bindings/rust/src/bindings/test_formats/compute_kzg_proof.rs index 335b9b4f0..fc5fec00a 100644 --- a/bindings/rust/src/bindings/test_formats/compute_kzg_proof.rs +++ b/bindings/rust/src/bindings/test_formats/compute_kzg_proof.rs @@ -10,7 +10,7 @@ pub struct Input<'a> { } impl Input<'_> { - pub fn get_blob(&self) -> Result { + pub fn get_blob(&self) -> Result, Error> { Blob::from_hex(self.blob) } diff --git a/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof.rs b/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof.rs index 137c2b07c..56a2dbcd8 100644 --- a/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof.rs +++ b/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof.rs @@ -11,7 +11,7 @@ pub struct Input<'a> { } impl Input<'_> { - pub fn get_blob(&self) -> Result { + pub fn get_blob(&self) -> Result, Error> { Blob::from_hex(self.blob) } diff --git a/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof_batch.rs b/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof_batch.rs index 3a30a0bce..6927746ec 100644 --- a/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof_batch.rs +++ b/bindings/rust/src/bindings/test_formats/verify_blob_kzg_proof_batch.rs @@ -14,7 +14,7 @@ impl Input { pub fn get_blobs(&self) -> Result, Error> { let mut v: Vec = Vec::new(); for blob in &self.blobs { - v.push(Blob::from_hex(blob)?); + v.push(*Blob::from_hex(blob)?); } return Ok(v); } diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 89788f917..73c026ac1 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -7,8 +7,8 @@ pub use bindings::{ }; // Expose the constants. pub use bindings::{ - BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, BYTES_PER_PROOF, - FIELD_ELEMENTS_PER_BLOB, BYTES_PER_G1_POINT, BYTES_PER_G2_POINT + BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1_POINT, + BYTES_PER_G2_POINT, BYTES_PER_PROOF, FIELD_ELEMENTS_PER_BLOB, }; // Expose the remaining relevant types. pub use bindings::{Blob, Bytes32, Bytes48, Error}; From d43539b0ecc6feb02c53c8aeb80e1c8dca8a93a4 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 7 Jul 2023 12:19:52 -0500 Subject: [PATCH 2/8] Run cargo fmt --- bindings/rust/benches/kzg_benches.rs | 4 +--- bindings/rust/src/bindings/mod.rs | 22 ++++++---------------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/bindings/rust/benches/kzg_benches.rs b/bindings/rust/benches/kzg_benches.rs index b9fca612c..8b72f2dde 100644 --- a/bindings/rust/benches/kzg_benches.rs +++ b/bindings/rust/benches/kzg_benches.rs @@ -55,9 +55,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { .collect(); c.bench_function("blob_to_kzg_commitment", |b| { - b.iter(|| { - KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap(), &kzg_settings) - }) + b.iter(|| KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap(), &kzg_settings)) }); c.bench_function("compute_kzg_proof", |b| { diff --git a/bindings/rust/src/bindings/mod.rs b/bindings/rust/src/bindings/mod.rs index 34ef3c482..93b52bd58 100644 --- a/bindings/rust/src/bindings/mod.rs +++ b/bindings/rust/src/bindings/mod.rs @@ -429,10 +429,7 @@ impl KZGCommitment { hex::encode(self.bytes) } - pub fn blob_to_kzg_commitment( - blob: &Blob, - kzg_settings: &KZGSettings, - ) -> Result { + pub fn blob_to_kzg_commitment(blob: &Blob, kzg_settings: &KZGSettings) -> Result { let mut kzg_commitment: MaybeUninit = MaybeUninit::uninit(); unsafe { let res = blob_to_kzg_commitment( @@ -573,9 +570,7 @@ mod tests { let commitments: Vec = blobs .iter() - .map(|blob| { - KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap() - }) + .map(|blob| KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap()) .map(|commitment| commitment.to_bytes()) .collect(); @@ -583,8 +578,7 @@ mod tests { .iter() .zip(commitments.iter()) .map(|(blob, commitment)| { - KZGProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings) - .unwrap() + KZGProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings).unwrap() }) .map(|proof| proof.to_bytes()) .collect(); @@ -599,13 +593,9 @@ mod tests { blobs.pop(); - let error = KZGProof::verify_blob_kzg_proof_batch( - &blobs, - &commitments, - &proofs, - &kzg_settings, - ) - .unwrap_err(); + let error = + KZGProof::verify_blob_kzg_proof_batch(&blobs, &commitments, &proofs, &kzg_settings) + .unwrap_err(); assert!(matches!(error, Error::MismatchLength(_))); let incorrect_blob = *generate_random_blob(&mut rng); From ad9ffa39162fa2e921cbfbca7397b968ac7900ec Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 7 Jul 2023 12:35:12 -0500 Subject: [PATCH 3/8] Fix a few typos --- bindings/rust/build.rs | 2 +- bindings/rust/src/bindings/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/rust/build.rs b/bindings/rust/build.rs index 37d0e4f1d..369abe84b 100644 --- a/bindings/rust/build.rs +++ b/bindings/rust/build.rs @@ -7,7 +7,7 @@ const MINIMAL_FIELD_ELEMENTS_PER_BLOB: usize = 4; /// Compiles blst. // // NOTE: This code is taken from https://github.com/supranational/blst `build.rs` `main`. The crate -// is not used as a depedency to avoid double link issues on dependants. +// is not used as a dependency to avoid double link issues on dependants. fn compile_blst(blst_base_dir: PathBuf) { // account for cross-compilation [by examining environment variables] let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); diff --git a/bindings/rust/src/bindings/mod.rs b/bindings/rust/src/bindings/mod.rs index 93b52bd58..3eb14a4d0 100644 --- a/bindings/rust/src/bindings/mod.rs +++ b/bindings/rust/src/bindings/mod.rs @@ -148,7 +148,7 @@ impl KZGSettings { .as_os_str() .to_str() .ok_or(Error::InvalidTrustedSetup(format!( - "Unsuported non unicode file path" + "Unsupported non unicode file path" )))? .as_bytes() }; @@ -180,7 +180,7 @@ impl KZGSettings { }; // We don't really care if this succeeds. - let _uncheched_close_result = unsafe { libc::fclose(file_ptr) }; + let _unchecked_close_result = unsafe { libc::fclose(file_ptr) }; drop(file_path); result From 3529b06f89e3cd9dec872706d17771f550077169 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 7 Jul 2023 13:22:45 -0500 Subject: [PATCH 4/8] Test removing benchmark band-aid fix --- bindings/rust/Cargo.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bindings/rust/Cargo.toml b/bindings/rust/Cargo.toml index e9b9f341a..6e79beff5 100644 --- a/bindings/rust/Cargo.toml +++ b/bindings/rust/Cargo.toml @@ -50,8 +50,3 @@ glob = "0.3" [[bench]] name = "kzg_benches" harness = false - -# The benchmarks crash on Windows with Rust 1.70. This is a band-aid fix for -# that. Refer to #318 for more details. This should be removed if fixed. -[profile.bench] -opt-level = 0 From 421cf3e6d213ce48ad9f55b30ab072e359ab79c6 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Fri, 7 Jul 2023 13:26:51 -0500 Subject: [PATCH 5/8] Revert "Test removing benchmark band-aid fix" This reverts commit 3529b06f89e3cd9dec872706d17771f550077169. --- bindings/rust/Cargo.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bindings/rust/Cargo.toml b/bindings/rust/Cargo.toml index 6e79beff5..e9b9f341a 100644 --- a/bindings/rust/Cargo.toml +++ b/bindings/rust/Cargo.toml @@ -50,3 +50,8 @@ glob = "0.3" [[bench]] name = "kzg_benches" harness = false + +# The benchmarks crash on Windows with Rust 1.70. This is a band-aid fix for +# that. Refer to #318 for more details. This should be removed if fixed. +[profile.bench] +opt-level = 0 From ad10cfc6965ea8f4fbc7e852f81752953ba5710e Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Wed, 12 Jul 2023 17:15:34 -0500 Subject: [PATCH 6/8] Do benchmark setup outside of bench --- bindings/rust/benches/kzg_benches.rs | 50 ++++++++++------------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/bindings/rust/benches/kzg_benches.rs b/bindings/rust/benches/kzg_benches.rs index 8b72f2dde..5f452f06e 100644 --- a/bindings/rust/benches/kzg_benches.rs +++ b/bindings/rust/benches/kzg_benches.rs @@ -55,50 +55,35 @@ pub fn criterion_benchmark(c: &mut Criterion) { .collect(); c.bench_function("blob_to_kzg_commitment", |b| { - b.iter(|| KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap(), &kzg_settings)) + let blob = blobs.first().unwrap(); + b.iter(|| KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings)) }); c.bench_function("compute_kzg_proof", |b| { - b.iter(|| { - KzgProof::compute_kzg_proof( - blobs.first().unwrap(), - fields.first().unwrap(), - &kzg_settings, - ) - }) + let blob = blobs.first().unwrap(); + let z = fields.first().unwrap(); + b.iter(|| KzgProof::compute_kzg_proof(blob, z, &kzg_settings)) }); c.bench_function("compute_blob_kzg_proof", |b| { - b.iter(|| { - KzgProof::compute_blob_kzg_proof( - blobs.first().unwrap(), - commitments.first().unwrap(), - &kzg_settings, - ) - }) + let blob = blobs.first().unwrap(); + let commitment = commitments.first().unwrap(); + b.iter(|| KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings)) }); c.bench_function("verify_kzg_proof", |b| { - b.iter(|| { - KzgProof::verify_kzg_proof( - commitments.first().unwrap(), - fields.first().unwrap(), - fields.first().unwrap(), - proofs.first().unwrap(), - &kzg_settings, - ) - }) + let commitment = commitments.first().unwrap(); + let z = fields.first().unwrap(); + let y = fields.first().unwrap(); + let proof = proofs.first().unwrap(); + b.iter(|| KzgProof::verify_kzg_proof(commitment, z, y, proof, &kzg_settings)) }); c.bench_function("verify_blob_kzg_proof", |b| { - b.iter(|| { - KzgProof::verify_blob_kzg_proof( - blobs.first().unwrap(), - commitments.first().unwrap(), - proofs.first().unwrap(), - &kzg_settings, - ) - }) + let blob = blobs.first().unwrap(); + let commitment = commitments.first().unwrap(); + let proof = proofs.first().unwrap(); + b.iter(|| KzgProof::verify_blob_kzg_proof(blob, commitment, proof, &kzg_settings)) }); let mut group = c.benchmark_group("verify_blob_kzg_proof_batch"); @@ -128,7 +113,6 @@ pub fn criterion_benchmark(c: &mut Criterion) { &proofs_subset, &kzg_settings, ) - .unwrap(); }, BatchSize::LargeInput, ); From 1883c89c3b5dd75423194a08df029ce129392a50 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Thu, 13 Jul 2023 09:39:57 -0500 Subject: [PATCH 7/8] Test without MaybeUninit --- bindings/rust/src/bindings/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/rust/src/bindings/mod.rs b/bindings/rust/src/bindings/mod.rs index 3eb14a4d0..055d5e762 100644 --- a/bindings/rust/src/bindings/mod.rs +++ b/bindings/rust/src/bindings/mod.rs @@ -351,17 +351,17 @@ impl KZGProof { proof_bytes: &Bytes48, kzg_settings: &KZGSettings, ) -> Result { - let mut verified: MaybeUninit = MaybeUninit::uninit(); + let mut verified: bool = false; unsafe { let res = verify_blob_kzg_proof( - verified.as_mut_ptr(), + &mut verified, blob, commitment_bytes, proof_bytes, kzg_settings, ); if let C_KZG_RET::C_KZG_OK = res { - Ok(verified.assume_init()) + Ok(verified) } else { Err(Error::CError(res)) } From 6b4b632759be75c986752b11c6b354dbff54328e Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Thu, 13 Jul 2023 10:29:18 -0500 Subject: [PATCH 8/8] Add debug print --- bindings/rust/src/bindings/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bindings/rust/src/bindings/mod.rs b/bindings/rust/src/bindings/mod.rs index 055d5e762..27fa7175b 100644 --- a/bindings/rust/src/bindings/mod.rs +++ b/bindings/rust/src/bindings/mod.rs @@ -360,6 +360,7 @@ impl KZGProof { proof_bytes, kzg_settings, ); + println!("foobar\n"); if let C_KZG_RET::C_KZG_OK = res { Ok(verified) } else {