From 1d4b099297f352c2d4d1a3cfca43570a946d7b95 Mon Sep 17 00:00:00 2001 From: sirse Date: Sat, 30 Sep 2023 21:21:05 +0300 Subject: [PATCH] Proper errors for blob_to_kzg_commitment --- blst/src/eip_4844.rs | 114 +++++++++++------- blst/src/types/poly.rs | 8 +- blst/tests/eip_4844.rs | 84 +++++++++---- blst/tests/local_tests/local_poly.rs | 28 ++--- blst/tests/local_tests/local_recovery.rs | 6 +- kzg-bench/src/benches/fk20.rs | 4 +- kzg-bench/src/benches/kzg.rs | 4 +- kzg-bench/src/benches/poly.rs | 4 +- kzg-bench/src/tests/eip_4844.rs | 147 ++++++++++++----------- kzg-bench/src/tests/fk20_proofs.rs | 6 +- kzg-bench/src/tests/kzg_proofs.rs | 10 +- kzg-bench/src/tests/poly.rs | 24 ++-- kzg-bench/src/tests/zero_poly.rs | 8 +- kzg/src/lib.rs | 2 +- 14 files changed, 256 insertions(+), 193 deletions(-) diff --git a/blst/src/eip_4844.rs b/blst/src/eip_4844.rs index 2572cec9..d9d5ea33 100644 --- a/blst/src/eip_4844.rs +++ b/blst/src/eip_4844.rs @@ -169,9 +169,13 @@ fn g1_lincomb(points: &[FsG1], scalars: &[FsFr], length: usize) -> FsG1 { out } -pub fn blob_to_kzg_commitment_rust(blob: &[FsFr], s: &FsKZGSettings) -> FsG1 { - let p = blob_to_polynomial_rust(blob); - poly_to_kzg_commitment(&p, s) +pub fn blob_to_kzg_commitment_rust( + blob: &[FsFr], + settings: &FsKZGSettings, +) -> Result { + let polynomial = blob_to_polynomial_rust(blob)?; + + Ok(poly_to_kzg_commitment(&polynomial, settings)) } pub fn verify_kzg_proof_rust( @@ -235,17 +239,21 @@ pub fn verify_kzg_proof_batch( ) } -pub fn compute_kzg_proof_rust(blob: &[FsFr], z: &FsFr, s: &FsKZGSettings) -> (FsG1, FsFr) { +pub fn compute_kzg_proof_rust( + blob: &[FsFr], + z: &FsFr, + s: &FsKZGSettings, +) -> Result<(FsG1, FsFr), String> { assert_eq!(blob.len(), FIELD_ELEMENTS_PER_BLOB); - let polynomial = blob_to_polynomial_rust(blob); + let polynomial = blob_to_polynomial_rust(blob)?; let y = evaluate_polynomial_in_evaluation_form_rust(&polynomial, z, s); let mut tmp: FsFr; let roots_of_unity: &Vec = &s.fs.roots_of_unity; let mut m: usize = 0; - let mut q: FsPoly = FsPoly::new(FIELD_ELEMENTS_PER_BLOB).unwrap(); + let mut q: FsPoly = FsPoly::new(FIELD_ELEMENTS_PER_BLOB); let mut inverses_in: Vec = vec![FsFr::default(); FIELD_ELEMENTS_PER_BLOB]; let mut inverses: Vec = vec![FsFr::default(); FIELD_ELEMENTS_PER_BLOB]; @@ -297,7 +305,7 @@ pub fn compute_kzg_proof_rust(blob: &[FsFr], z: &FsFr, s: &FsKZGSettings) -> (Fs } let proof = g1_lincomb(&s.secret_g1, &q.coeffs, FIELD_ELEMENTS_PER_BLOB); - (proof, y) + Ok((proof, y)) } pub fn evaluate_polynomial_in_evaluation_form_rust( @@ -428,15 +436,16 @@ fn compute_r_powers( compute_powers(&r, n) } -pub fn blob_to_polynomial_rust(blob: &[FsFr]) -> FsPoly { - assert_eq!(blob.len(), FIELD_ELEMENTS_PER_BLOB); - let mut p: FsPoly = FsPoly::new(FIELD_ELEMENTS_PER_BLOB).unwrap(); +pub fn blob_to_polynomial_rust(blob: &[FsFr]) -> Result { + if blob.len() != FIELD_ELEMENTS_PER_BLOB { + return Err(String::from("Blob length must be FIELD_ELEMENTS_PER_BLOB")); + } + let mut p: FsPoly = FsPoly::new(FIELD_ELEMENTS_PER_BLOB); p.coeffs = blob.to_vec(); - p + Ok(p) } fn poly_to_kzg_commitment(p: &FsPoly, s: &FsKZGSettings) -> FsG1 { - assert_eq!(p.coeffs.len(), FIELD_ELEMENTS_PER_BLOB); g1_lincomb(&s.secret_g1, &p.coeffs, FIELD_ELEMENTS_PER_BLOB) } @@ -450,7 +459,7 @@ pub fn compute_blob_kzg_proof_rust( } let evaluation_challenge_fr = compute_challenge(blob, commitment); - let (proof, _) = compute_kzg_proof_rust(blob, &evaluation_challenge_fr, ts); + let (proof, _) = compute_kzg_proof_rust(blob, &evaluation_challenge_fr, ts)?; Ok(proof) } @@ -467,7 +476,7 @@ pub fn verify_blob_kzg_proof_rust( return Err("Invalid proof".to_string()); } - let polynomial = blob_to_polynomial_rust(blob); + let polynomial = blob_to_polynomial_rust(blob)?; let evaluation_challenge_fr = compute_challenge(blob, commitment_g1); let y_fr = evaluate_polynomial_in_evaluation_form_rust(&polynomial, &evaluation_challenge_fr, ts); @@ -478,12 +487,12 @@ fn compute_challenges_and_evaluate_polynomial( blobs: &[Vec], commitments_g1: &[FsG1], ts: &FsKZGSettings, -) -> (Vec, Vec) { +) -> Result<(Vec, Vec), String> { let mut evaluation_challenges_fr = Vec::with_capacity(blobs.len()); let mut ys_fr = Vec::with_capacity(blobs.len()); for i in 0..blobs.len() { - let polynomial = blob_to_polynomial_rust(&blobs[i]); + let polynomial = blob_to_polynomial_rust(&blobs[i])?; let evaluation_challenge_fr = compute_challenge(&blobs[i], &commitments_g1[i]); let y_fr = evaluate_polynomial_in_evaluation_form_rust(&polynomial, &evaluation_challenge_fr, ts); @@ -492,7 +501,7 @@ fn compute_challenges_and_evaluate_polynomial( ys_fr.push(y_fr); } - (evaluation_challenges_fr, ys_fr) + Ok((evaluation_challenges_fr, ys_fr)) } fn validate_batched_input(commitments: &[FsG1], proofs: &[FsG1]) -> Result<(), String> { @@ -540,10 +549,10 @@ pub fn verify_blob_kzg_proof_batch_rust( // Process blobs in parallel subgroups let blobs_per_group = num_blobs / num_cores; - Ok(blobs + blobs .par_chunks(blobs_per_group) .enumerate() - .all(|(i, blob_group)| { + .map(|(i, blob_group)| { let num_blobs_in_group = blob_group.len(); let commitment_group = &commitments_g1 [blobs_per_group * i..blobs_per_group * i + num_blobs_in_group]; @@ -554,16 +563,17 @@ pub fn verify_blob_kzg_proof_batch_rust( blob_group, commitment_group, ts, - ); + )?; - verify_kzg_proof_batch( + Ok(verify_kzg_proof_batch( commitment_group, &evaluation_challenges_fr, &ys_fr, proof_group, ts, - ) - })) + )) + }) + .try_reduce(|| true, |a, b| Ok(a && b)) } else { // Each group contains either one or zero blobs, so iterate // over the single blob verification function in parallel @@ -580,7 +590,7 @@ pub fn verify_blob_kzg_proof_batch_rust( { validate_batched_input(commitments_g1, proofs_g1)?; let (evaluation_challenges_fr, ys_fr) = - compute_challenges_and_evaluate_polynomial(blobs, commitments_g1, ts); + compute_challenges_and_evaluate_polynomial(blobs, commitments_g1, ts)?; Ok(verify_kzg_proof_batch( commitments_g1, @@ -639,7 +649,7 @@ fn kzg_settings_to_rust(c_settings: &CKZGSettings) -> FsKZGSettings { res } -fn kzg_settings_to_c(rust_settings: &FsKZGSettings) -> CKZGSettings { +pub fn kzg_settings_to_c(rust_settings: &FsKZGSettings) -> CKZGSettings { let g1_val = rust_settings .secret_g1 .iter() @@ -695,14 +705,23 @@ pub unsafe extern "C" fn blob_to_kzg_commitment( blob: *const Blob, s: &CKZGSettings, ) -> C_KZG_RET { - let deserialized_blob = deserialize_blob(blob); - if let Ok(blob_) = deserialized_blob { - let tmp = blob_to_kzg_commitment_rust(&blob_, &kzg_settings_to_rust(s)); - (*out).bytes = tmp.to_bytes(); - C_KZG_RET_OK - } else { - deserialized_blob.err().unwrap() - } + if TRUSTED_SETUP_NUM_G1_POINTS == 0 { + // FIXME: load_trusted_setup should set this value, but if not, it fails + TRUSTED_SETUP_NUM_G1_POINTS = FIELD_ELEMENTS_PER_BLOB + }; + + let deserialized_blob = match deserialize_blob(blob) { + Ok(value) => value, + Err(err) => return err, + }; + + let tmp = match blob_to_kzg_commitment_rust(&deserialized_blob, &kzg_settings_to_rust(s)) { + Ok(value) => value, + Err(_) => return C_KZG_RET_BADARGS, + }; + + (*out).bytes = tmp.to_bytes(); + C_KZG_RET_OK } /// # Safety @@ -944,19 +963,22 @@ pub unsafe extern "C" fn compute_kzg_proof( z_bytes: *const Bytes32, s: &CKZGSettings, ) -> C_KZG_RET { - let deserialized_blob = deserialize_blob(blob); - if deserialized_blob.is_err() { - return deserialized_blob.err().unwrap(); - } - let frz = FsFr::from_bytes(&(*z_bytes).bytes); - if frz.is_err() { - return C_KZG_RET_BADARGS; - } - let (proof_out_tmp, fry_tmp) = compute_kzg_proof_rust( - &deserialized_blob.unwrap(), - &frz.unwrap(), - &kzg_settings_to_rust(s), - ); + let deserialized_blob = match deserialize_blob(blob) { + Ok(value) => value, + Err(err) => return err, + }; + + let frz = match FsFr::from_bytes(&(*z_bytes).bytes) { + Ok(value) => value, + Err(_) => return C_KZG_RET_BADARGS, + }; + + let (proof_out_tmp, fry_tmp) = + match compute_kzg_proof_rust(&deserialized_blob, &frz, &kzg_settings_to_rust(s)) { + Ok(value) => value, + Err(_) => return C_KZG_RET_BADARGS, + }; + (*proof_out).bytes = proof_out_tmp.to_bytes(); (*y_out).bytes = fry_tmp.to_bytes(); C_KZG_RET_OK diff --git a/blst/src/types/poly.rs b/blst/src/types/poly.rs index 2350b381..5f316700 100644 --- a/blst/src/types/poly.rs +++ b/blst/src/types/poly.rs @@ -17,10 +17,10 @@ pub struct FsPoly { } impl Poly for FsPoly { - fn new(size: usize) -> Result { - Ok(Self { + fn new(size: usize) -> Self { + Self { coeffs: vec![FsFr::default(); size], - }) + } } fn get_coeff_at(&self, i: usize) -> FsFr { @@ -251,7 +251,7 @@ impl Poly for FsPoly { fn mul_direct(&mut self, multiplier: &Self, output_len: usize) -> Result { if self.len() == 0 || multiplier.len() == 0 { - return Ok(FsPoly::new(0).unwrap()); + return Ok(FsPoly::new(0)); } let a_degree = self.len() - 1; diff --git a/blst/tests/eip_4844.rs b/blst/tests/eip_4844.rs index f58da9ab..2a3a107d 100644 --- a/blst/tests/eip_4844.rs +++ b/blst/tests/eip_4844.rs @@ -1,12 +1,19 @@ #[cfg(test)] mod tests { + use std::env::set_current_dir; + + use kzg::eip_4844::{ + Blob, KZGCommitment, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, C_KZG_RET_BADARGS, + TRUSTED_SETUP_PATH, + }; use kzg_bench::tests::eip_4844::{ blob_to_kzg_commitment_test, bytes_to_bls_field_test, compute_and_verify_blob_kzg_proof_fails_with_incorrect_proof_test, compute_and_verify_blob_kzg_proof_test, compute_and_verify_kzg_proof_fails_with_incorrect_proof_test, compute_and_verify_kzg_proof_round_trip_test, compute_kzg_proof_test, compute_powers_test, - verify_kzg_proof_batch_fails_with_incorrect_proof_test, verify_kzg_proof_batch_test, + generate_random_blob_bytes, verify_kzg_proof_batch_fails_with_incorrect_proof_test, + verify_kzg_proof_batch_test, }; #[cfg(not(feature = "minimal-spec"))] use kzg_bench::tests::eip_4844::{ @@ -16,20 +23,17 @@ mod tests { test_vectors_verify_kzg_proof, }; use rust_kzg_blst::eip_4844::{ - blob_to_kzg_commitment_rust, blob_to_polynomial_rust, bytes_to_blob, - compute_blob_kzg_proof_rust, compute_kzg_proof_rust, compute_powers, - evaluate_polynomial_in_evaluation_form_rust, load_trusted_setup_filename_rust, - verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust, verify_kzg_proof_rust, + blob_to_kzg_commitment, blob_to_kzg_commitment_rust, blob_to_polynomial_rust, + bytes_to_blob, compute_blob_kzg_proof_rust, compute_kzg_proof_rust, compute_powers, + evaluate_polynomial_in_evaluation_form_rust, kzg_settings_to_c, + load_trusted_setup_filename_rust, verify_blob_kzg_proof_batch_rust, + verify_blob_kzg_proof_rust, verify_kzg_proof_rust, }; use rust_kzg_blst::types::{ fft_settings::FsFFTSettings, fr::FsFr, g1::FsG1, g2::FsG2, kzg_settings::FsKZGSettings, poly::FsPoly, }; - fn load_trusted_setup_filename_rust_tst(filepath: &str) -> FsKZGSettings { - load_trusted_setup_filename_rust(filepath).unwrap() - } - #[test] pub fn bytes_to_bls_field_test_() { bytes_to_bls_field_test::(); @@ -43,7 +47,7 @@ mod tests { #[test] pub fn blob_to_kzg_commitment_test_() { blob_to_kzg_commitment_test::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, ); } @@ -51,7 +55,7 @@ mod tests { #[test] pub fn compute_kzg_proof_test_() { compute_kzg_proof_test::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &compute_kzg_proof_rust, &blob_to_polynomial_rust, &evaluate_polynomial_in_evaluation_form_rust, @@ -68,7 +72,7 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_kzg_proof_rust, @@ -89,7 +93,7 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_kzg_proof_rust, @@ -109,7 +113,7 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_kzg_proof_rust, @@ -129,7 +133,7 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_blob_kzg_proof_rust, @@ -147,7 +151,7 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_blob_kzg_proof_rust, @@ -158,7 +162,7 @@ mod tests { #[test] pub fn verify_kzg_proof_batch_test_() { verify_kzg_proof_batch_test::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_blob_kzg_proof_rust, @@ -176,7 +180,7 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, &compute_blob_kzg_proof_rust, @@ -188,7 +192,7 @@ mod tests { #[test] pub fn test_vectors_blob_to_kzg_commitment_() { test_vectors_blob_to_kzg_commitment::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &blob_to_kzg_commitment_rust, &bytes_to_blob, ); @@ -198,7 +202,7 @@ mod tests { #[test] pub fn test_vectors_compute_kzg_proof_() { test_vectors_compute_kzg_proof::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &compute_kzg_proof_rust, &bytes_to_blob, ); @@ -208,7 +212,7 @@ mod tests { #[test] pub fn test_vectors_compute_blob_kzg_proof_() { test_vectors_compute_blob_kzg_proof::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &bytes_to_blob, &compute_blob_kzg_proof_rust, ); @@ -218,7 +222,7 @@ mod tests { #[test] pub fn test_vectors_verify_kzg_proof_() { test_vectors_verify_kzg_proof::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &verify_kzg_proof_rust, ); } @@ -227,7 +231,7 @@ mod tests { #[test] pub fn test_vectors_verify_blob_kzg_proof_() { test_vectors_verify_blob_kzg_proof::( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &bytes_to_blob, &verify_blob_kzg_proof_rust, ); @@ -244,9 +248,41 @@ mod tests { FsFFTSettings, FsKZGSettings, >( - &load_trusted_setup_filename_rust_tst, + &load_trusted_setup_filename_rust, &bytes_to_blob, &verify_blob_kzg_proof_batch_rust, ); } + + #[test] + pub fn blob_to_kzg_commitment_invalid_blob() { + set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); + println!("{}", env!("CARGO_MANIFEST_DIR")); + println!("{}", format!("../kzg-bench/{}", TRUSTED_SETUP_PATH)); + let settings = + load_trusted_setup_filename_rust(&format!("../kzg-bench/{}", TRUSTED_SETUP_PATH)) + .unwrap(); + + let c_settings = kzg_settings_to_c(&settings); + + let mut rng = rand::thread_rng(); + let mut blob_bytes = generate_random_blob_bytes(&mut rng); + + let bls_modulus: [u8; BYTES_PER_FIELD_ELEMENT] = [ + 0x73, 0xED, 0xA7, 0x53, 0x29, 0x9D, 0x7D, 0x48, 0x33, 0x39, 0xD8, 0x08, 0x09, 0xA1, + 0xD8, 0x05, 0x53, 0xBD, 0xA4, 0x02, 0xFF, 0xFE, 0x5B, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x01, + ]; + // Make first field element equal to BLS_MODULUS + blob_bytes[0..BYTES_PER_FIELD_ELEMENT].copy_from_slice(&bls_modulus); + + let blob = Blob { bytes: blob_bytes }; + let mut commitment = KZGCommitment { + bytes: [0; BYTES_PER_COMMITMENT], + }; + + let output = unsafe { blob_to_kzg_commitment(&mut commitment, &blob, &c_settings) }; + + assert_eq!(output, C_KZG_RET_BADARGS) + } } diff --git a/blst/tests/local_tests/local_poly.rs b/blst/tests/local_tests/local_poly.rs index 4dfd0df2..4c4f290a 100644 --- a/blst/tests/local_tests/local_poly.rs +++ b/blst/tests/local_tests/local_poly.rs @@ -6,7 +6,7 @@ use rust_kzg_blst::types::fr::FsFr; use rust_kzg_blst::types::poly::FsPoly; pub fn create_poly_of_length_ten() { - let poly = FsPoly::new(10).unwrap(); + let poly = FsPoly::new(10); assert_eq!(poly.len(), 10); } @@ -15,7 +15,7 @@ pub fn poly_pad_works_rand() { for _k in 0..256 { let poly_length: usize = (1 + (rng.next_u64() % 1000)) as usize; - let mut poly = FsPoly::new(poly_length).unwrap(); + let mut poly = FsPoly::new(poly_length); for i in 0..poly.len() { poly.set_coeff_at(i, &FsFr::rand()); } @@ -32,7 +32,7 @@ pub fn poly_pad_works_rand() { pub fn poly_eval_check() { let n: usize = 10; - let mut poly = FsPoly::new(n).unwrap(); + let mut poly = FsPoly::new(n); for i in 0..n { let fr = FsFr::from_u64((i + 1) as u64); poly.set_coeff_at(i, &fr); @@ -45,7 +45,7 @@ pub fn poly_eval_check() { pub fn poly_eval_0_check() { let n: usize = 7; let a: usize = 597; - let mut poly = FsPoly::new(n).unwrap(); + let mut poly = FsPoly::new(n); for i in 0..n { let fr = FsFr::from_u64((i + a) as u64); poly.set_coeff_at(i, &fr); @@ -57,7 +57,7 @@ pub fn poly_eval_0_check() { pub fn poly_eval_nil_check() { let n: usize = 0; - let poly = FsPoly::new(n).unwrap(); + let poly = FsPoly::new(n); let actual = poly.eval(&FsFr::one()); assert!(actual.equals(&FsFr::zero())); } @@ -65,7 +65,7 @@ pub fn poly_eval_nil_check() { pub fn poly_inverse_simple_0() { // 1 / (1 - x) = 1 + x + x^2 + ... let d: usize = 16; - let mut p = FsPoly::new(2).unwrap(); + let mut p = FsPoly::new(2); p.set_coeff_at(0, &FsFr::one()); p.set_coeff_at(1, &FsFr::one()); p.set_coeff_at(1, &FsFr::negate(&p.get_coeff_at(1))); @@ -80,7 +80,7 @@ pub fn poly_inverse_simple_0() { pub fn poly_inverse_simple_1() { // 1 / (1 + x) = 1 - x + x^2 - ... let d: usize = 16; - let mut p = FsPoly::new(2).unwrap(); + let mut p = FsPoly::new(2); p.set_coeff_at(0, &FsFr::one()); p.set_coeff_at(1, &FsFr::one()); let result = p.inverse(d); @@ -174,7 +174,7 @@ fn test_data(a: usize, b: usize) -> Vec { } fn new_test_poly(coeffs: &[i64]) -> FsPoly { - let mut p = FsPoly::new(0).unwrap(); + let mut p = FsPoly::new(0); for &coeff in coeffs.iter() { if coeff >= 0 { @@ -237,12 +237,12 @@ pub fn poly_div_fast_test() { } pub fn test_poly_div_by_zero() { - let mut dividend = FsPoly::new(2).unwrap(); + let mut dividend = FsPoly::new(2); dividend.set_coeff_at(0, &FsFr::from_u64(1)); dividend.set_coeff_at(1, &FsFr::from_u64(1)); - let divisor = FsPoly::new(0).unwrap(); + let divisor = FsPoly::new(0); let dummy = dividend.div(&divisor); assert!(dummy.is_err()); @@ -304,13 +304,13 @@ pub fn poly_mul_random() { for _k in 0..256 { let multiplicand_length: usize = (1 + (rng.next_u64() % 1000)) as usize; - let mut multiplicand = FsPoly::new(multiplicand_length).unwrap(); + let mut multiplicand = FsPoly::new(multiplicand_length); for i in 0..multiplicand.len() { multiplicand.set_coeff_at(i, &FsFr::rand()); } let multiplier_length: usize = (1 + (rng.next_u64() % 1000)) as usize; - let mut multiplier = FsPoly::new(multiplier_length).unwrap(); + let mut multiplier = FsPoly::new(multiplier_length); for i in 0..multiplier.len() { multiplier.set_coeff_at(i, &FsFr::rand()); } @@ -340,8 +340,8 @@ pub fn poly_div_random() { let dividend_length: usize = (2 + (rng.next_u64() % 1000)) as usize; let divisor_length: usize = 1 + ((rng.next_u64() as usize) % dividend_length); - let mut dividend = FsPoly::new(dividend_length).unwrap(); - let mut divisor = FsPoly::new(divisor_length).unwrap(); + let mut dividend = FsPoly::new(dividend_length); + let mut divisor = FsPoly::new(divisor_length); for i in 0..dividend_length { dividend.set_coeff_at(i, &FsFr::rand()); diff --git a/blst/tests/local_tests/local_recovery.rs b/blst/tests/local_tests/local_recovery.rs index e8496b70..3de2e1f4 100644 --- a/blst/tests/local_tests/local_recovery.rs +++ b/blst/tests/local_tests/local_recovery.rs @@ -49,8 +49,7 @@ pub fn recover_simple< let max_width: usize = fs.get_max_width(); let poly_query = TPoly::new(max_width); - assert!(poly_query.is_ok()); - let mut poly = poly_query.unwrap(); + let mut poly = poly_query; for i in 0..(max_width / 2) { poly.set_coeff_at(i, &TFr::from_u64(i.try_into().unwrap())); @@ -106,8 +105,7 @@ pub fn recover_random< let max_width: usize = fs.get_max_width(); // let mut poly = TPoly::default(); let poly_query = TPoly::new(max_width); - assert!(poly_query.is_ok()); - let mut poly = poly_query.unwrap(); + let mut poly = poly_query; for i in 0..(max_width / 2) { poly.set_coeff_at(i, &TFr::from_u64(i.try_into().unwrap())); diff --git a/kzg-bench/src/benches/fk20.rs b/kzg-bench/src/benches/fk20.rs index 38e4d3ef..e3170a51 100644 --- a/kzg-bench/src/benches/fk20.rs +++ b/kzg-bench/src/benches/fk20.rs @@ -44,7 +44,7 @@ pub fn bench_fk_single_da< let secrets_len = n_len + 1; assert!(n_len >= 2 * poly_len); - let mut p = TPoly::new(poly_len).unwrap(); + let mut p = TPoly::new(poly_len); for (i, &coeff) in coeffs.iter().enumerate() { p.set_coeff_at(i, &TFr::from_u64(coeff)); } @@ -95,7 +95,7 @@ pub fn bench_fk_multi_da< let fk = TFK20MultiSettings::new(&ks, secrets_len, chunk_len).unwrap(); // Create a test polynomial of size n that's independent of chunk_len - let mut p = TPoly::new(n).unwrap(); + let mut p = TPoly::new(n); for i in 0..chunk_count { for j in 0..chunk_len { let p_index = i * chunk_len + j; diff --git a/kzg-bench/src/benches/kzg.rs b/kzg-bench/src/benches/kzg.rs index 3050156a..f60b844e 100644 --- a/kzg-bench/src/benches/kzg.rs +++ b/kzg-bench/src/benches/kzg.rs @@ -22,7 +22,7 @@ pub fn bench_commit_to_poly< let fs = TFFTSettings::new(BENCH_SCALE).unwrap(); let (s1, s2) = generate_trusted_setup(fs.get_max_width(), SECRET); let ks = TKZGSettings::new(&s1, &s2, fs.get_max_width(), &fs).unwrap(); - let mut poly = TPoly::new(fs.get_max_width()).unwrap(); + let mut poly = TPoly::new(fs.get_max_width()); for i in 0..fs.get_max_width() { poly.set_coeff_at(i, &TFr::rand()); } @@ -44,7 +44,7 @@ pub fn bench_compute_proof_single< let fs = TFFTSettings::new(BENCH_SCALE).unwrap(); let (s1, s2) = generate_trusted_setup(fs.get_max_width(), SECRET); let ks = TKZGSettings::new(&s1, &s2, fs.get_max_width(), &fs).unwrap(); - let mut poly = TPoly::new(fs.get_max_width()).unwrap(); + let mut poly = TPoly::new(fs.get_max_width()); for i in 0..fs.get_max_width() { poly.set_coeff_at(i, &TFr::rand()); } diff --git a/kzg-bench/src/benches/poly.rs b/kzg-bench/src/benches/poly.rs index 6c87e8f3..4fe1c436 100644 --- a/kzg-bench/src/benches/poly.rs +++ b/kzg-bench/src/benches/poly.rs @@ -7,8 +7,8 @@ pub fn bench_new_poly_div>(c: &mut Criterion) { let dividend_length = 1 << BENCH_SCALE; let divisor_length = dividend_length / 2; - let mut dividend = TPoly::new(dividend_length).unwrap(); - let mut divisor = TPoly::new(divisor_length).unwrap(); + let mut dividend = TPoly::new(dividend_length); + let mut divisor = TPoly::new(divisor_length); // Randomize the polynomials' coefficients for i in 0..dividend.len() { diff --git a/kzg-bench/src/tests/eip_4844.rs b/kzg-bench/src/tests/eip_4844.rs index 4a881d3b..715e3e49 100644 --- a/kzg-bench/src/tests/eip_4844.rs +++ b/kzg-bench/src/tests/eip_4844.rs @@ -77,6 +77,7 @@ pub fn compute_powers_test(compute_powers: &dyn Fn(&TFr, usize) -> Vec< } } +#[allow(clippy::type_complexity)] pub fn blob_to_kzg_commitment_test< TFr: Fr + Copy, TG1: G1, @@ -85,11 +86,11 @@ pub fn blob_to_kzg_commitment_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let field_element = TFr::from_hex("0x14629a3a39f7b854e6aa49aa2edb450267eac2c14bb2d4f97a0b81a3f57055ad") @@ -100,7 +101,7 @@ pub fn blob_to_kzg_commitment_test< blob[0] = field_element; // Get a commitment to this particular blob - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); // We expect the commitment to match // If it doesn't match, something important has changed @@ -130,13 +131,13 @@ pub fn compute_kzg_proof_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> (TG1, TFr), - blob_to_polynomial: &dyn Fn(&[TFr]) -> TPoly, + load_trusted_setup: &dyn Fn(&str) -> Result, + compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, + blob_to_polynomial: &dyn Fn(&[TFr]) -> Result, evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let field_element = TFr::from_hex("0x69386e69dbae0357b399b8d645a57a3062dfbe00bd8e97170b9bdd6bc6168a13") @@ -150,7 +151,7 @@ pub fn compute_kzg_proof_test< blob[0] = field_element; // Compute the KZG proof for the given blob & z - let (proof, output_value) = compute_kzg_proof(&blob, &input_value, &ts); + let (proof, output_value) = compute_kzg_proof(&blob, &input_value, &ts).unwrap(); // Compare the computed proof to the expected proof let expected_proof = if cfg!(feature = "minimal-spec") { @@ -170,7 +171,7 @@ pub fn compute_kzg_proof_test< assert!(proof.equals(&expected_proof)); // Get the expected y by evaluating the polynomial at input_value - let poly = blob_to_polynomial(&blob); + let poly = blob_to_polynomial(&blob).unwrap(); let expected_output_value = evaluate_polynomial_in_evaluation_form(&poly, &input_value, &ts); assert!(output_value.equals(&expected_output_value)); @@ -185,16 +186,16 @@ pub fn compute_and_verify_kzg_proof_round_trip_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, - compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> (TG1, TFr), - blob_to_polynomial: &dyn Fn(&[TFr]) -> TPoly, + compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, + blob_to_polynomial: &dyn Fn(&[TFr]) -> Result, evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); let z_fr = { @@ -208,14 +209,14 @@ pub fn compute_and_verify_kzg_proof_round_trip_test< }; // Get a commitment to that particular blob - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); // Compute the proof - let (proof, computed_y) = compute_kzg_proof(&blob, &z_fr, &ts); + let (proof, computed_y) = compute_kzg_proof(&blob, &z_fr, &ts).unwrap(); // Now let's attempt to verify the proof // First convert the blob to field elements - let poly = blob_to_polynomial(&blob); + let poly = blob_to_polynomial(&blob).unwrap(); // Now evaluate the poly at `z` to learn `y` let y_fr = evaluate_polynomial_in_evaluation_form(&poly, &z_fr, &ts); @@ -238,16 +239,16 @@ pub fn compute_and_verify_kzg_proof_within_domain_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, - compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> (TG1, TFr), - blob_to_polynomial: &dyn Fn(&[TFr]) -> TPoly, + compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, + blob_to_polynomial: &dyn Fn(&[TFr]) -> Result, evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); for i in 0..25 { @@ -257,14 +258,14 @@ pub fn compute_and_verify_kzg_proof_within_domain_test< }; // Get a commitment to that particular blob - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); // Get the polynomial version of the blob - let poly = blob_to_polynomial(&blob); + let poly = blob_to_polynomial(&blob).unwrap(); // Compute the proof let z_fr = ts.get_roots_of_unity_at(i); - let (proof, computed_y) = compute_kzg_proof(&blob, &z_fr, &ts); + let (proof, computed_y) = compute_kzg_proof(&blob, &z_fr, &ts).unwrap(); // Now evaluate the poly at `z` to learn `y` let y_fr = evaluate_polynomial_in_evaluation_form(&poly, &z_fr, &ts); @@ -287,16 +288,16 @@ pub fn compute_and_verify_kzg_proof_fails_with_incorrect_proof_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, - compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> (TG1, TFr), - blob_to_polynomial: &dyn Fn(&[TFr]) -> TPoly, + compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, + blob_to_polynomial: &dyn Fn(&[TFr]) -> Result, evaluate_polynomial_in_evaluation_form: &dyn Fn(&TPoly, &TFr, &TKZGSettings) -> TFr, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); let z_fr = { @@ -310,14 +311,14 @@ pub fn compute_and_verify_kzg_proof_fails_with_incorrect_proof_test< }; // Get a commitment to that particular blob - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); // Compute the proof - let (mut proof, _) = compute_kzg_proof(&blob, &z_fr, &ts); + let (mut proof, _) = compute_kzg_proof(&blob, &z_fr, &ts).unwrap(); // Now let's attempt to verify the proof // First convert the blob to field elements - let poly = blob_to_polynomial(&blob); + let poly = blob_to_polynomial(&blob).unwrap(); // Now evaluate the poly at `z` to learn `y` let y_fr = evaluate_polynomial_in_evaluation_form(&poly, &z_fr, &ts); @@ -339,14 +340,14 @@ pub fn compute_and_verify_blob_kzg_proof_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); // Some preparation @@ -356,7 +357,7 @@ pub fn compute_and_verify_blob_kzg_proof_test< }; // Compute the proof - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); let proof = compute_blob_kzg_proof(&blob, &commitment, &ts).unwrap(); // Finally verify the proof @@ -373,14 +374,14 @@ pub fn compute_and_verify_blob_kzg_proof_fails_with_incorrect_proof_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); // Some preparation @@ -390,7 +391,7 @@ pub fn compute_and_verify_blob_kzg_proof_fails_with_incorrect_proof_test< }; // Compute the proof - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); let mut proof = compute_blob_kzg_proof(&blob, &commitment, &ts).unwrap(); // Change the proof so it should not verify @@ -410,8 +411,8 @@ pub fn verify_kzg_proof_batch_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof_batch: &dyn Fn( @@ -422,7 +423,7 @@ pub fn verify_kzg_proof_batch_test< ) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); const N_SAMPLES: usize = 16; @@ -438,7 +439,7 @@ pub fn verify_kzg_proof_batch_test< bytes_to_blob(&blob_bytes).unwrap() }; - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); let proof = compute_blob_kzg_proof(&blob, &commitment, &ts).unwrap(); blobs.push(blob); @@ -469,8 +470,8 @@ pub fn verify_kzg_proof_batch_fails_with_incorrect_proof_test< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, verify_blob_kzg_proof_batch: &dyn Fn( @@ -481,7 +482,7 @@ pub fn verify_kzg_proof_batch_fails_with_incorrect_proof_test< ) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let mut rng = rand::thread_rng(); const N_SAMPLES: usize = 2; @@ -497,7 +498,7 @@ pub fn verify_kzg_proof_batch_fails_with_incorrect_proof_test< bytes_to_blob(&blob_bytes).unwrap() }; - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); let proof = compute_blob_kzg_proof(&blob, &commitment, &ts).unwrap(); blobs.push(blob); @@ -521,6 +522,7 @@ const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS: &str = "src/test_vectors/verify_blob_kzg_proof_batch/*/*/*"; #[cfg(not(feature = "minimal-spec"))] +#[allow(clippy::type_complexity)] pub fn test_vectors_blob_to_kzg_commitment< TFr: Fr, TG1: G1, @@ -529,12 +531,12 @@ pub fn test_vectors_blob_to_kzg_commitment< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> TG1, + load_trusted_setup: &dyn Fn(&str) -> Result, + blob_to_kzg_commitment: &dyn Fn(&[TFr], &TKZGSettings) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let test_files: Vec = glob::glob(BLOB_TO_KZG_COMMITMENT_TESTS) .unwrap() .map(Result::unwrap) @@ -558,12 +560,13 @@ pub fn test_vectors_blob_to_kzg_commitment< TG1::from_bytes(&commitment_bytes).unwrap() }; - let commitment = blob_to_kzg_commitment(&blob, &ts); + let commitment = blob_to_kzg_commitment(&blob, &ts).unwrap(); assert!(commitment.equals(&expected_commitment)); } } #[cfg(not(feature = "minimal-spec"))] +#[allow(clippy::type_complexity)] pub fn test_vectors_compute_kzg_proof< TFr: Fr, TG1: G1, @@ -572,12 +575,12 @@ pub fn test_vectors_compute_kzg_proof< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, - compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> (TG1, TFr), + load_trusted_setup: &dyn Fn(&str) -> Result, + compute_kzg_proof: &dyn Fn(&[TFr], &TFr, &TKZGSettings) -> Result<(TG1, TFr), String>, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let test_files: Vec = glob::glob(COMPUTE_KZG_PROOF_TESTS) .unwrap() .map(Result::unwrap) @@ -613,7 +616,7 @@ pub fn test_vectors_compute_kzg_proof< ); // Compute the proof - let (proof, y) = compute_kzg_proof(&input.0, &input.1, &ts); + let (proof, y) = compute_kzg_proof(&input.0, &input.1, &ts).unwrap(); // Compare the computed and expected proofs assert!(proof.equals(&output.0.unwrap())); @@ -624,6 +627,7 @@ pub fn test_vectors_compute_kzg_proof< } #[cfg(not(feature = "minimal-spec"))] +#[allow(clippy::type_complexity)] pub fn test_vectors_compute_blob_kzg_proof< TFr: Fr, TG1: G1, @@ -632,12 +636,12 @@ pub fn test_vectors_compute_blob_kzg_proof< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, + load_trusted_setup: &dyn Fn(&str) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, compute_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let test_files: Vec = glob::glob(COMPUTE_BLOB_KZG_PROOF_TESTS) .unwrap() .map(Result::unwrap) @@ -665,7 +669,7 @@ pub fn test_vectors_compute_blob_kzg_proof< }, ); - let proof = match compute_blob_kzg_proof(&input.0, &input.1, &ts) { + match compute_blob_kzg_proof(&input.0, &input.1, &ts) { Ok(proof) => { let expected_commitment = test .get_output_bytes() @@ -682,6 +686,7 @@ pub fn test_vectors_compute_blob_kzg_proof< } #[cfg(not(feature = "minimal-spec"))] +#[allow(clippy::type_complexity)] pub fn test_vectors_verify_kzg_proof< TFr: Fr, TG1: G1, @@ -690,11 +695,11 @@ pub fn test_vectors_verify_kzg_proof< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, + load_trusted_setup: &dyn Fn(&str) -> Result, verify_kzg_proof: &dyn Fn(&TG1, &TFr, &TFr, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let test_files: Vec = glob::glob(VERIFY_KZG_PROOF_TESTS) .unwrap() .map(Result::unwrap) @@ -736,7 +741,7 @@ pub fn test_vectors_verify_kzg_proof< }, ); - let result = match verify_kzg_proof(&input.0, &input.1, &input.2, &input.3, &ts) { + match verify_kzg_proof(&input.0, &input.1, &input.2, &input.3, &ts) { Ok(result) => assert_eq!(result, test.get_output().unwrap()), Err(_) => { assert!(test.get_output().is_none()); @@ -747,6 +752,7 @@ pub fn test_vectors_verify_kzg_proof< } #[cfg(not(feature = "minimal-spec"))] +#[allow(clippy::type_complexity)] pub fn test_vectors_verify_blob_kzg_proof< TFr: Fr, TG1: G1, @@ -755,12 +761,12 @@ pub fn test_vectors_verify_blob_kzg_proof< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, + load_trusted_setup: &dyn Fn(&str) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, verify_blob_kzg_proof: &dyn Fn(&[TFr], &TG1, &TG1, &TKZGSettings) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let test_files: Vec = glob::glob(VERIFY_BLOB_KZG_PROOF_TESTS) .unwrap() .map(Result::unwrap) @@ -795,7 +801,7 @@ pub fn test_vectors_verify_blob_kzg_proof< }, ); - let result = match verify_blob_kzg_proof(&input.0, &input.1, &input.2, &ts) { + match verify_blob_kzg_proof(&input.0, &input.1, &input.2, &ts) { Ok(result) => assert_eq!(result, test.get_output().unwrap()), Err(_) => { assert!(test.get_output().is_none()); @@ -806,6 +812,7 @@ pub fn test_vectors_verify_blob_kzg_proof< } #[cfg(not(feature = "minimal-spec"))] +#[allow(clippy::type_complexity)] pub fn test_vectors_verify_blob_kzg_proof_batch< TFr: Fr, TG1: G1, @@ -814,7 +821,7 @@ pub fn test_vectors_verify_blob_kzg_proof_batch< TFFTSettings: FFTSettings, TKZGSettings: KZGSettings, >( - load_trusted_setup: &dyn Fn(&str) -> TKZGSettings, + load_trusted_setup: &dyn Fn(&str) -> Result, bytes_to_blob: &dyn Fn(&[u8]) -> Result, String>, verify_blob_kzg_proof_batch: &dyn Fn( &[Vec], @@ -824,7 +831,7 @@ pub fn test_vectors_verify_blob_kzg_proof_batch< ) -> Result, ) { set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap(); - let ts = load_trusted_setup(TRUSTED_SETUP_PATH); + let ts = load_trusted_setup(TRUSTED_SETUP_PATH).unwrap(); let test_files: Vec = glob::glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS) .unwrap() .map(Result::unwrap) @@ -876,7 +883,7 @@ pub fn test_vectors_verify_blob_kzg_proof_batch< continue; } - let result = match verify_blob_kzg_proof_batch(&blobs, &commitments, &proofs, &ts) { + match verify_blob_kzg_proof_batch(&blobs, &commitments, &proofs, &ts) { Ok(result) => assert_eq!(result, test.get_output().unwrap()), Err(_) => { assert!(test.get_output().is_none()); diff --git a/kzg-bench/src/tests/fk20_proofs.rs b/kzg-bench/src/tests/fk20_proofs.rs index 091819ac..37dfe126 100644 --- a/kzg-bench/src/tests/fk20_proofs.rs +++ b/kzg-bench/src/tests/fk20_proofs.rs @@ -59,7 +59,7 @@ pub fn fk_single< let secrets_len = n_len + 1; assert!(n_len >= 2 * poly_len); - let mut p = TPoly::new(poly_len).unwrap(); + let mut p = TPoly::new(poly_len); for (i, &coeff) in coeffs.iter().enumerate() { p.set_coeff_at(i, &TFr::from_u64(coeff)); } @@ -118,7 +118,7 @@ pub fn fk_single_strided< let secrets_len = n_len + 1; assert!(n_len >= 2 * poly_len); - let mut p = TPoly::new(poly_len).unwrap(); + let mut p = TPoly::new(poly_len); for (i, &coeff) in coeffs.iter().enumerate() { p.set_coeff_at(i, &TFr::from_u64(coeff)); } @@ -196,7 +196,7 @@ fn fk_multi_case< let fk = TFK20MultiSettings::new(&ks, n * 2, chunk_len).unwrap(); // Create a test polynomial of size n that's independent of chunk_len - let mut p = TPoly::new(n).unwrap(); + let mut p = TPoly::new(n); for i in 0..chunk_count { for j in 0..chunk_len { let p_index = i * chunk_len + j; diff --git a/kzg-bench/src/tests/kzg_proofs.rs b/kzg-bench/src/tests/kzg_proofs.rs index 721a6529..03a69dd3 100644 --- a/kzg-bench/src/tests/kzg_proofs.rs +++ b/kzg-bench/src/tests/kzg_proofs.rs @@ -22,7 +22,7 @@ pub fn proof_single< let secrets_len = poly_len + 1; // Create the polynomial - let mut p = TPoly::new(poly_len).unwrap(); + let mut p = TPoly::new(poly_len); for (x, &coeff) in coeffs.iter().enumerate() { p.set_coeff_at(x, &TFr::from_u64(coeff)); } @@ -68,7 +68,7 @@ pub fn commit_to_nil_poly< let fs = TFFTSettings::new(4).unwrap(); let ks = TKZGSettings::new(&s1, &s2, secrets_len, &fs).unwrap(); - let a = TPoly::new(0).unwrap(); + let a = TPoly::new(0); let result = ks.commit_to_poly(&a).unwrap(); assert!(result.equals(&TG1::default())); } @@ -95,7 +95,7 @@ pub fn commit_to_too_long_poly< let fs = TFFTSettings::new(4).unwrap(); let ks = TKZGSettings::new(&s1, &s2, secrets_len, &fs).unwrap(); - let a = TPoly::new(poly_len).unwrap(); + let a = TPoly::new(poly_len); let _result = ks.commit_to_poly(&a); } } @@ -119,7 +119,7 @@ pub fn commit_to_too_long_poly_returns_err< let fs = TFFTSettings::new(4).unwrap(); let ks = TKZGSettings::new(&s1, &s2, secrets_len, &fs).unwrap(); - let a = TPoly::new(poly_len).unwrap(); + let a = TPoly::new(poly_len); let _result = ks.commit_to_poly(&a); assert!(_result.is_err()); } @@ -151,7 +151,7 @@ pub fn proof_multi< }; // Create the polynomial - let mut p = TPoly::new(poly_len).unwrap(); + let mut p = TPoly::new(poly_len); for (x, &coeff) in coeffs.iter().enumerate() { p.set_coeff_at(x, &TFr::from_u64(coeff)); } diff --git a/kzg-bench/src/tests/poly.rs b/kzg-bench/src/tests/poly.rs index ca62396a..5abf0c6f 100644 --- a/kzg-bench/src/tests/poly.rs +++ b/kzg-bench/src/tests/poly.rs @@ -3,13 +3,13 @@ use rand::rngs::StdRng; use rand::{RngCore, SeedableRng}; pub fn create_poly_of_length_ten>() { - let poly = TPoly::new(10).unwrap(); + let poly = TPoly::new(10); assert_eq!(poly.len(), 10); } pub fn poly_eval_check>() { let n: usize = 10; - let mut poly = TPoly::new(n).unwrap(); + let mut poly = TPoly::new(n); for i in 0..n { let fr = TFr::from_u64((i + 1) as u64); poly.set_coeff_at(i, &fr); @@ -22,7 +22,7 @@ pub fn poly_eval_check>() { pub fn poly_eval_0_check>() { let n: usize = 7; let a: usize = 597; - let mut poly = TPoly::new(n).unwrap(); + let mut poly = TPoly::new(n); for i in 0..n { let fr = TFr::from_u64((i + a) as u64); poly.set_coeff_at(i, &fr); @@ -34,7 +34,7 @@ pub fn poly_eval_0_check>() { pub fn poly_eval_nil_check>() { let n: usize = 0; - let poly = TPoly::new(n).unwrap(); + let poly = TPoly::new(n); let actual = poly.eval(&TFr::one()); assert!(actual.equals(&TFr::zero())); } @@ -42,7 +42,7 @@ pub fn poly_eval_nil_check>() { pub fn poly_inverse_simple_0>() { // 1 / (1 - x) = 1 + x + x^2 + ... let d: usize = 16; - let mut p = TPoly::new(2).unwrap(); + let mut p = TPoly::new(2); p.set_coeff_at(0, &TFr::one()); p.set_coeff_at(1, &TFr::one()); p.set_coeff_at(1, &TFr::negate(&p.get_coeff_at(1))); @@ -57,7 +57,7 @@ pub fn poly_inverse_simple_0>() { pub fn poly_inverse_simple_1>() { // 1 / (1 + x) = 1 - x + x^2 - ... let d: usize = 16; - let mut p = TPoly::new(2).unwrap(); + let mut p = TPoly::new(2); p.set_coeff_at(0, &TFr::one()); p.set_coeff_at(1, &TFr::one()); let result = p.inverse(d); @@ -122,7 +122,7 @@ fn test_data(a: usize, b: usize) -> Vec { } fn new_test_poly>(coeffs: &Vec) -> TPoly { - let mut p = TPoly::new(coeffs.len()).unwrap(); + let mut p = TPoly::new(coeffs.len()); for (i, &coeff) in coeffs.iter().enumerate() { if coeff >= 0 { let c = TFr::from_u64(coeff as u64); @@ -161,7 +161,7 @@ pub fn poly_div_by_zero>() { //Arrange let coeffs: Vec = vec![1, 1]; let mut dividend: TPoly = new_test_poly(&coeffs); - let divisor = TPoly::new(0).unwrap(); + let divisor = TPoly::new(0); //Act let result = dividend.div(&divisor); @@ -241,8 +241,8 @@ pub fn poly_mul_random< let multiplier_length: usize = (1 + (rng.next_u64() % 1000)) as usize; let out_length: usize = (1 + (rng.next_u64() % 1000)) as usize; - let mut multiplicand = TPoly::new(multiplicand_length).unwrap(); - let mut multiplier = TPoly::new(multiplier_length).unwrap(); + let mut multiplicand = TPoly::new(multiplicand_length); + let mut multiplier = TPoly::new(multiplier_length); for i in 0..multiplicand_length { let coef = TFr::rand(); @@ -287,8 +287,8 @@ pub fn poly_div_random>() { let dividend_length: usize = (2 + (rng.next_u64() % 1000)) as usize; let divisor_length: usize = 1 + ((rng.next_u64() as usize) % dividend_length); - let mut dividend = TPoly::new(dividend_length).unwrap(); - let mut divisor = TPoly::new(divisor_length).unwrap(); + let mut dividend = TPoly::new(dividend_length); + let mut divisor = TPoly::new(divisor_length); for i in 0..dividend_length { let coef = TFr::rand(); diff --git a/kzg-bench/src/tests/zero_poly.rs b/kzg-bench/src/tests/zero_poly.rs index fdc22aa2..498f6c02 100644 --- a/kzg-bench/src/tests/zero_poly.rs +++ b/kzg-bench/src/tests/zero_poly.rs @@ -146,8 +146,8 @@ pub fn reduce_partials_random< /// Check that polynomial evaluation works against precomputed values pub fn check_test_data + FFTFr, TPoly: Poly>() { - let mut expected_eval = TPoly::new(16).unwrap(); - let mut expected_poly = TPoly::new(16).unwrap(); + let mut expected_eval = TPoly::new(16); + let mut expected_poly = TPoly::new(16); let fft_settings = TFFTSettings::new(4).unwrap(); @@ -187,8 +187,8 @@ pub fn zero_poly_known< let fft_settings = TFFTSettings::new(4).unwrap(); let mut missing_idxs = Vec::new(); - let mut expected_eval = TPoly::new(16).unwrap(); - let mut expected_poly = TPoly::new(16).unwrap(); + let mut expected_eval = TPoly::new(16); + let mut expected_poly = TPoly::new(16); for (i, &exists) in EXISTS.iter().enumerate() { expected_eval.set_coeff_at(i, &TFr::from_u64_arr(&EXPECTED_EVAL_U64[i])); diff --git a/kzg/src/lib.rs b/kzg/src/lib.rs index ee11e4d3..6f4e163c 100644 --- a/kzg/src/lib.rs +++ b/kzg/src/lib.rs @@ -185,7 +185,7 @@ pub trait FFTSettingsPoly, FSettings: FFTSett } pub trait Poly: Default + Clone { - fn new(size: usize) -> Result; + fn new(size: usize) -> Self; fn get_coeff_at(&self, i: usize) -> Coeff;