diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5c89cbe..9dc178a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -19,7 +19,7 @@ jobs: - name: Build run: cargo build --verbose - name: Clippy Format test - run: cargo clippy -- -D warnings + run: cargo clippy --all --manifest-path Cargo.toml -- -D warnings - name: Run tests run: cargo test --verbose - name: Run tests with mainnet data diff --git a/README.md b/README.md index 61afa0b..d659d13 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ This code is unaudited and under construction. This is experimental software and 2. Specify the files in `kzg.setup()` function, leave the `g2_points` empty, and specify the `srs_order` per the guide. 3. Note that this is process will take a few minutes to load since it is a bit intensive. +## Clippy +Linting can be triggered via running `cargo clippy --all --manifest-path Cargo.toml -- -D warnings`. + ## Quick Start 1. Check the test in `test_compute_kzg_proof` function to see the end to end usage of the library for quick start. diff --git a/src/helpers.rs b/src/helpers.rs index 518d456..d5db66d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -190,7 +190,7 @@ pub fn lexicographically_largest(z: &Fq) -> bool { borrow == 0 } -pub fn read_g2_point_from_bytes_be(g2_bytes_be: &Vec) -> Result { +pub fn read_g2_point_from_bytes_be(g2_bytes_be: &[u8]) -> Result { if g2_bytes_be.len() != SIZE_OF_G2_AFFINE_COMPRESSED { return Err("not enough bytes for g2 point"); } @@ -255,7 +255,7 @@ pub fn read_g2_point_from_bytes_be(g2_bytes_be: &Vec) -> Result) -> Result { +pub fn read_g1_point_from_bytes_be(g1_bytes_be: &[u8]) -> Result { if g1_bytes_be.len() != SIZE_OF_G1_AFFINE_COMPRESSED { return Err("not enough bytes for g1 point"); } @@ -301,6 +301,7 @@ pub fn process_chunks(receiver: Receiver<(Vec, usize)>) -> Vec<(T, usize) where T: ReadPointFromBytes, { + #[allow(clippy::unnecessary_filter_map)] receiver .iter() .map(|(chunk, position)| { diff --git a/src/kzg.rs b/src/kzg.rs index f862dcd..71cd6c8 100644 --- a/src/kzg.rs +++ b/src/kzg.rs @@ -49,19 +49,17 @@ impl Kzg { let g1_points = Self::parallel_read_g1_points(path_to_g1_points.to_owned(), srs_points_to_load) .map_err(|e| KzgError::SerializationError(e.to_string()))?; - - let g2_points: Vec; - if !path_to_g2_points.is_empty() { - g2_points = - Self::parallel_read_g2_points(path_to_g2_points.to_owned(), srs_points_to_load) - .map_err(|e| KzgError::SerializationError(e.to_string()))?; - } else if !g2_power_of2_path.is_empty() { - g2_points = Self::read_g2_point_on_power_of_2(g2_power_of2_path)?; - } else { - return Err(KzgError::GenericError( + + let g2_points_result: Result, KzgError> = match (path_to_g2_points.is_empty(), g2_power_of2_path.is_empty()) { + (false, _) => Self::parallel_read_g2_points(path_to_g2_points.to_owned(), srs_points_to_load) + .map_err(|e| KzgError::SerializationError(e.to_string())), + (_, false) => Self::read_g2_point_on_power_of_2(g2_power_of2_path), + (true, true) => return Err(KzgError::GenericError( "both g2 point files are empty, need the proper file specified".to_string(), - )); - } + )), + }; + + let g2_points = g2_points_result?; Ok(Self { g1: g1_points, @@ -446,7 +444,7 @@ impl Kzg { &self, polynomial: &Polynomial, index: u64, - root_of_unities: &Vec, + root_of_unities: &[Fr], ) -> Result { if !self.params.completed_setup { return Err(KzgError::GenericError( @@ -520,18 +518,17 @@ impl Kzg { z_fr: Fr, eval_fr: &[Fr], value_fr: Fr, - roots_of_unities: &Vec, + roots_of_unities: &[Fr], ) -> Fr { let mut quotient = Fr::zero(); - let mut fi: Fr; - let mut numerator: Fr; - let mut denominator: Fr; - let mut temp: Fr; - - for i in 0..roots_of_unities.len() { - let omega_i = roots_of_unities[i]; - if omega_i == z_fr { - continue; + let mut fi: Fr = Fr::zero(); + let mut numerator: Fr = Fr::zero(); + let mut denominator: Fr = Fr::zero(); + let mut temp: Fr = Fr::zero(); + + roots_of_unities.iter().enumerate().for_each(|(i, omega_i)| { + if *omega_i == z_fr { + return; } fi = eval_fr[i] - value_fr; numerator = fi * omega_i; @@ -539,7 +536,8 @@ impl Kzg { denominator *= z_fr; temp = numerator.div(denominator); quotient += temp; - } + }); + quotient } diff --git a/src/traits.rs b/src/traits.rs index 269777d..db08545 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -10,14 +10,14 @@ pub trait ReadPointFromBytes: AffineRepr { // Implement this trait for G1Affine and G2Affine impl ReadPointFromBytes for G1Affine { fn read_point_from_bytes_be(bytes: &[u8]) -> io::Result { - helpers::read_g1_point_from_bytes_be(&bytes.to_vec()) + helpers::read_g1_point_from_bytes_be(bytes) .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) } } impl ReadPointFromBytes for G2Affine { fn read_point_from_bytes_be(bytes: &[u8]) -> io::Result { - helpers::read_g2_point_from_bytes_be(&bytes.to_vec()) + helpers::read_g2_point_from_bytes_be(bytes) .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) } }