Skip to content

Commit

Permalink
changing to clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
anupsv committed May 31, 2024
1 parent 36983d5 commit 49b7789
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
run: cargo test --verbose
- name: Run tests with mainnet data
run: KZG_ENV=mainnet-data cargo test --verbose
- name: Format test
run: cargo fmt --all -- --check
- name: Clippy Format test
run: cargo clippy -- -D warnings
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rust-kzg-bn254"
version = "0.1.0"
edition = "2021"
authors = ["Anup Swamy Veena"]
authors = ["Anup Swamy Veena", "Teddy Knox"]
rust-version = "1.73"
description = "This library offers a set of functions for generating and interacting with bn254 KZG commitments and proofs in rust, with the motivation of supporting fraud and validity proof logic in EigenDA rollup integrations."
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion benches/bench_kzg_commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg};
use std::time::Duration;
Expand Down
6 changes: 3 additions & 3 deletions benches/bench_kzg_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
.unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
b.iter(|| kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone()));
b.iter(|| kzg.verify_kzg_proof(commitment, proof, *value_fr, *z_fr));
});

c.bench_function("bench_kzg_verify_30000", |b| {
Expand All @@ -45,7 +45,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
.unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
b.iter(|| kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone()));
b.iter(|| kzg.verify_kzg_proof(commitment, proof, *value_fr, *z_fr));
});

c.bench_function("bench_kzg_verify_50000", |b| {
Expand All @@ -62,7 +62,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
.unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
b.iter(|| kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone()));
b.iter(|| kzg.verify_kzg_proof(commitment, proof, *value_fr, *z_fr));
});
}

Expand Down
15 changes: 7 additions & 8 deletions src/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use ark_bn254::Fq;
use ark_ff::PrimeField;

pub fn montgomery_reduce(z_0: &u64, z_1: &u64, z_2: &u64, z_3: &u64) -> (u64, u64, u64, u64) {
let mut z0 = z_0.clone();
let mut z1 = z_1.clone();
let mut z2 = z_2.clone();
let mut z3 = z_3.clone();
let mut z0 = *z_0;
let mut z1 = *z_1;
let mut z2 = *z_2;
let mut z3 = *z_3;
let inv: u64 = 9786893198990664585;
let modulus = <Fq as PrimeField>::MODULUS.0;

Expand Down Expand Up @@ -69,16 +69,15 @@ fn sub_64(x: u64, y: u64, borrow: u64) -> (u64, u64) {
pub fn madd0(a: u64, b: u64, c: u64) -> u64 {
let mut hi: u64;
let mut lo: u128; // Using u128 to handle overflow from multiplication
let carry: u64;

// Perform the multiplication
lo = (a as u128) * (b as u128);
hi = (lo >> 64) as u64; // Extract the high 64 bits
lo = lo & 0xFFFFFFFFFFFFFFFF; // Keep only the low 64 bits
lo &= 0xFFFFFFFFFFFFFFFF; // Keep only the low 64 bits

// Add c to the low part of the result
let sum_with_c = (lo as u64).wrapping_add(c);
carry = if sum_with_c < lo as u64 { 1 } else { 0 };
let carry: u64 = if sum_with_c < lo as u64 { 1 } else { 0 };

// Add the carry to the high part of the result
hi = hi.wrapping_add(carry);
Expand All @@ -94,7 +93,7 @@ pub fn madd2(a: u64, b: u64, c: u64, d: u64) -> (u64, u64) {
// Perform the multiplication
lo = (a as u128) * (b as u128);
hi = (lo >> 64) as u64; // Extract the high 64 bits
lo = lo & 0xFFFFFFFFFFFFFFFF; // Keep only the low 64 bits
lo &= 0xFFFFFFFFFFFFFFFF; // Keep only the low 64 bits

// Add c and d
let sum_cd = c.overflowing_add(d);
Expand Down
4 changes: 2 additions & 2 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ impl Blob {
}

pub fn get_length_after_padding(&self) -> usize {
return self.length_after_padding;
self.length_after_padding
}

/// Creates a new `Blob` from the given data.
pub fn is_padded(&self) -> bool {
return self.is_padded;
self.is_padded
}

/// Creates a new `Blob` from the provided byte slice and pads it according
Expand Down
20 changes: 8 additions & 12 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

pub fn blob_to_polynomial(blob: &Vec<u8>) -> Vec<Fr> {
to_fr_array(&blob)
to_fr_array(blob)
}

pub fn set_bytes_canonical_manual(data: &[u8]) -> Fr {
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn remove_empty_byte_from_padded_bytes(data: &[u8]) -> Vec<u8> {
}

pub fn set_bytes_canonical(data: &[u8]) -> Fr {
return Fr::from_be_bytes_mod_order(&data);
Fr::from_be_bytes_mod_order(data)
}

pub fn get_num_element(data_len: usize, symbol_size: usize) -> usize {
Expand Down Expand Up @@ -244,10 +244,8 @@ pub fn read_g2_point_from_bytes_be(g2_bytes_be: &Vec<u8>) -> Result<G2Affine, &s
if m_data == m_compressed_smallest {
y_sqrt.neg_in_place();
}
} else {
if m_data == m_compressed_largest {
y_sqrt.neg_in_place();
}
} else if m_data == m_compressed_largest {
y_sqrt.neg_in_place();
}

let point = G2Affine::new_unchecked(x, y_sqrt);
Expand Down Expand Up @@ -289,10 +287,8 @@ pub fn read_g1_point_from_bytes_be(g1_bytes_be: &Vec<u8>) -> Result<G1Affine, &s
if m_data == m_compressed_smallest {
y_sqrt.neg_in_place();
}
} else {
if m_data == m_compressed_largest {
y_sqrt.neg_in_place();
}
} else if m_data == m_compressed_largest {
y_sqrt.neg_in_place();
}
let point = G1Affine::new_unchecked(x, y_sqrt);
if !point.is_in_correct_subgroup_assuming_on_curve()
Expand Down Expand Up @@ -326,8 +322,8 @@ fn get_b_twist_curve_coeff() -> Fq2 {
let mut twist_curve_coeff = Fq2::new(twist_c0, twist_c1);
twist_curve_coeff = *twist_curve_coeff.inverse_in_place().unwrap();

twist_curve_coeff.c0 = twist_curve_coeff.c0 * Fq::from(3);
twist_curve_coeff.c1 = twist_curve_coeff.c1 * Fq::from(3);
twist_curve_coeff.c0 *= Fq::from(3);
twist_curve_coeff.c1 *= Fq::from(3);
twist_curve_coeff
}

Expand Down
15 changes: 7 additions & 8 deletions src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Kzg {
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)?;
g2_points = Self::read_g2_point_on_power_of_2(g2_power_of2_path)?;
} else {
return Err(KzgError::GenericError(
"both g2 point files are empty, need the proper file specified".to_string(),
Expand Down Expand Up @@ -88,8 +88,7 @@ impl Kzg {
if bytes_read == 0 {
break; // End of file reached
}
chunks
.push(G2Affine::read_point_from_bytes_be(&buffer[..bytes_read].to_vec()).unwrap());
chunks.push(G2Affine::read_point_from_bytes_be(&buffer[..bytes_read]).unwrap());
}
Ok(chunks)
}
Expand Down Expand Up @@ -480,7 +479,7 @@ impl Kzg {
z_fr,
&eval_fr,
value_fr,
&root_of_unities,
root_of_unities,
));
} else {
quotient_poly.push(poly_shift[i].div(denom_poly[i]));
Expand Down Expand Up @@ -517,9 +516,9 @@ impl Kzg {
fi = eval_fr[i] - value_fr;
numerator = fi.mul(omega_i);
denominator = z_fr - omega_i;
denominator = denominator * z_fr;
denominator *= z_fr;
temp = numerator.div(denominator);
quotient = quotient + temp;
quotient += temp;
}
quotient
}
Expand Down Expand Up @@ -554,9 +553,9 @@ impl Kzg {
z_fr: Fr,
) -> bool {
let g2_tau = if self.g2.len() > 28 {
self.g2.get(1).unwrap().clone()
*self.g2.get(1).unwrap()
} else {
self.g2.get(0).unwrap().clone()
*self.g2.first().unwrap()
};
let value_g1 = (G1Affine::generator() * value_fr).into_affine();
let commit_minus_value = (commitment - value_g1).into_affine();
Expand Down
12 changes: 6 additions & 6 deletions tests/blob_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ mod tests {
vec![0, 104, 105],
"testing adding padding"
);
assert_eq!(blob.is_padded(), true, "has to be padded");
assert!(blob.is_padded(), "has to be padded");

blob.remove_padding().unwrap();
assert_eq!(
blob.get_blob_data(),
vec![104, 105],
"testing removing padding"
);
assert_eq!(blob.is_padded(), false, "cannot be padded");
assert!(!blob.is_padded(), "cannot be padded");

let result: Vec<u8> = vec![
0, 70, 111, 117, 114, 115, 99, 111, 114, 101, 32, 97, 110, 100, 32, 115, 101, 118, 101,
Expand Down Expand Up @@ -119,11 +119,11 @@ mod tests {

blob = Blob::from_bytes_and_pad(GETTYSBURG_ADDRESS_BYTES);
assert_eq!(blob.get_blob_data(), result, "testing adding padding");
assert_eq!(blob.is_padded(), true, "has to be padded");
assert!(blob.is_padded(), "has to be padded");
assert_eq!(blob.get_length_after_padding(), 1515);

blob.remove_padding().unwrap();
assert_eq!(blob.is_padded(), false, "cannot be padded");
assert!(!blob.is_padded(), "cannot be padded");
assert_eq!(
blob.get_blob_data(),
GETTYSBURG_ADDRESS_BYTES,
Expand All @@ -138,7 +138,7 @@ mod tests {

blob_raw.pad_data().unwrap();
assert_eq!(blob_raw, blob_from, "testing adding padding");
assert_eq!(blob_raw.is_padded(), true, "has to be padded");
assert_eq!(blob_from.is_padded(), true, "has to be padded");
assert!(blob_raw.is_padded(), "has to be padded");
assert!(blob_from.is_padded(), "has to be padded");
}
}
10 changes: 5 additions & 5 deletions tests/helpers_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fn test_g1_is_on_curve() {
let rng = &mut thread_rng();
for _ in 0..1000 {
let point = G1Affine::rand(rng);
assert_eq!(is_on_curve_g1(&G1Projective::from(point)), true);
assert!(is_on_curve_g1(&G1Projective::from(point)));
let mut not_on_curve = point;
not_on_curve.x += Fq::one();
assert_eq!(is_on_curve_g1(&G1Projective::from(not_on_curve)), false);
assert!(!is_on_curve_g1(&G1Projective::from(not_on_curve)));
}
}

Expand All @@ -33,10 +33,10 @@ fn test_g2_is_on_curve() {
let rng = &mut thread_rng();
for _ in 0..1000 {
let point = G2Affine::rand(rng);
assert_eq!(is_on_curve_g2(&G2Projective::from(point)), true);
assert!(is_on_curve_g2(&G2Projective::from(point)));
let mut not_on_curve = point;
not_on_curve.x += Fq2::one();
assert_eq!(is_on_curve_g2(&G2Projective::from(not_on_curve)), false);
assert!(!is_on_curve_g2(&G2Projective::from(not_on_curve)));
}
}
// Loads data from files. This data was generated by gnark and is DA compatible.
Expand All @@ -51,7 +51,7 @@ fn test_blob_to_polynomial() {

let file = File::open("tests/test-files/blobs.txt").unwrap();
let mut reader = BufReader::new(file);
let mut buffer = vec![0u8; SIZE_OF_G1_AFFINE_COMPRESSED];
let mut buffer = [0u8; SIZE_OF_G1_AFFINE_COMPRESSED];
let mut read_fr_from_bytes: Vec<Fr> = vec![];
let mut fr_from_str_vec: Vec<Fr> = vec![];

Expand Down
2 changes: 1 addition & 1 deletion tests/polynomial_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests {
);

let polynomial_non_empty = Polynomial::new(&vec![Fr::one()], 2);
assert_eq!(polynomial_non_empty.unwrap().is_empty(), false);
assert!(!polynomial_non_empty.unwrap().is_empty());
}

#[test]
Expand Down

0 comments on commit 49b7789

Please sign in to comment.