From c18715878a98f667ea60cf0d2ab663614bb27545 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Fri, 30 Dec 2022 20:54:20 +0300 Subject: [PATCH] Fix some clippy lints --- ec/src/lib.rs | 2 +- ec/src/models/bn/mod.rs | 2 +- ec/src/models/bw6/mod.rs | 2 +- ec/src/models/mnt4/mod.rs | 6 +++--- ec/src/models/mnt6/mod.rs | 6 +++--- ec/src/models/short_weierstrass/mod.rs | 2 +- ec/src/models/twisted_edwards/mod.rs | 2 +- ec/src/pairing.rs | 2 +- ec/src/scalar_mul/variable_base/mod.rs | 11 +++++----- ff-macros/src/lib.rs | 2 +- ff-macros/src/montgomery/biginteger.rs | 2 -- ff-macros/src/montgomery/mod.rs | 1 - ff-macros/src/montgomery/mul.rs | 3 +-- ff-macros/src/montgomery/square.rs | 3 +-- ff-macros/src/montgomery/sum_of_products.rs | 3 +-- ff-macros/src/unroll.rs | 6 +++--- ff/src/biginteger/arithmetic.rs | 6 +----- ff/src/fields/fft_friendly.rs | 2 +- .../multivariate/multilinear/sparse.rs | 2 +- poly/src/polynomial/multivariate/mod.rs | 2 +- poly/src/polynomial/multivariate/sparse.rs | 2 +- poly/src/polynomial/univariate/sparse.rs | 8 ++++---- test-templates/src/h2c/json.rs | 20 ++++++++++++++----- 23 files changed, 48 insertions(+), 49 deletions(-) diff --git a/ec/src/lib.rs b/ec/src/lib.rs index 9efa9329d..daf169566 100644 --- a/ec/src/lib.rs +++ b/ec/src/lib.rs @@ -264,7 +264,7 @@ pub trait AffineRepr: /// `Self::ScalarField`. #[must_use] fn mul_by_cofactor_inv(&self) -> Self { - self.mul_bigint(&Self::Config::COFACTOR_INV.into_bigint()) + self.mul_bigint(Self::Config::COFACTOR_INV.into_bigint()) .into() } } diff --git a/ec/src/models/bn/mod.rs b/ec/src/models/bn/mod.rs index 611906bc0..62f7d4c4c 100644 --- a/ec/src/models/bn/mod.rs +++ b/ec/src/models/bn/mod.rs @@ -82,7 +82,7 @@ impl Bn

{ } fn exp_by_neg_x(mut f: Fp12) -> Fp12 { - f = f.cyclotomic_exp(&P::X); + f = f.cyclotomic_exp(P::X); if !P::X_IS_NEGATIVE { f.cyclotomic_inverse_in_place(); } diff --git a/ec/src/models/bw6/mod.rs b/ec/src/models/bw6/mod.rs index 1dddd0d29..62c053fb8 100644 --- a/ec/src/models/bw6/mod.rs +++ b/ec/src/models/bw6/mod.rs @@ -160,7 +160,7 @@ impl BW6

{ } fn exp_by_x(mut f: Fp6) -> Fp6 { - f = f.cyclotomic_exp(&P::X); + f = f.cyclotomic_exp(P::X); if P::X_IS_NEGATIVE { f.cyclotomic_inverse_in_place(); } diff --git a/ec/src/models/mnt4/mod.rs b/ec/src/models/mnt4/mod.rs index 1fdec8f9f..fc40d4cfc 100644 --- a/ec/src/models/mnt4/mod.rs +++ b/ec/src/models/mnt4/mod.rs @@ -185,11 +185,11 @@ impl MNT4

{ let mut elt_q = *elt; elt_q.frobenius_map_in_place(1); - let w1_part = elt_q.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_1); + let w1_part = elt_q.cyclotomic_exp(P::FINAL_EXPONENT_LAST_CHUNK_1); let w0_part = if P::FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG { - elt_inv_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) + elt_inv_clone.cyclotomic_exp(P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) } else { - elt_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) + elt_clone.cyclotomic_exp(P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) }; w1_part * &w0_part diff --git a/ec/src/models/mnt6/mod.rs b/ec/src/models/mnt6/mod.rs index 2f6b24f04..82e0b2ad9 100644 --- a/ec/src/models/mnt6/mod.rs +++ b/ec/src/models/mnt6/mod.rs @@ -191,11 +191,11 @@ impl MNT6

{ let mut elt_q = *elt; elt_q.frobenius_map_in_place(1); - let w1_part = elt_q.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_1); + let w1_part = elt_q.cyclotomic_exp(P::FINAL_EXPONENT_LAST_CHUNK_1); let w0_part = if P::FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG { - elt_inv_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) + elt_inv_clone.cyclotomic_exp(P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) } else { - elt_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) + elt_clone.cyclotomic_exp(P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0) }; w1_part * &w0_part diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 3f5e0e9ed..965cbb83f 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -112,7 +112,7 @@ pub trait SWCurveConfig: super::CurveConfig { ) -> Result, usize> { (bases.len() == scalars.len()) .then(|| VariableBaseMSM::msm_unchecked(bases, scalars)) - .ok_or(usize::min(bases.len(), scalars.len())) + .ok_or(bases.len().min(scalars.len())) } /// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is diff --git a/ec/src/models/twisted_edwards/mod.rs b/ec/src/models/twisted_edwards/mod.rs index 7bda50f83..67402ed69 100644 --- a/ec/src/models/twisted_edwards/mod.rs +++ b/ec/src/models/twisted_edwards/mod.rs @@ -92,7 +92,7 @@ pub trait TECurveConfig: super::CurveConfig { ) -> Result, usize> { (bases.len() == scalars.len()) .then(|| VariableBaseMSM::msm_unchecked(bases, scalars)) - .ok_or(usize::min(bases.len(), scalars.len())) + .ok_or(bases.len().min(scalars.len())) } /// If uncompressed, serializes both x and y coordinates. diff --git a/ec/src/pairing.rs b/ec/src/pairing.rs index 33e31aac3..05071a593 100644 --- a/ec/src/pairing.rs +++ b/ec/src/pairing.rs @@ -152,7 +152,7 @@ impl CanonicalSerialize for PairingOutput

{ impl Valid for PairingOutput

{ fn check(&self) -> Result<(), SerializationError> { - if self.0.pow(&P::ScalarField::characteristic()).is_one() { + if self.0.pow(P::ScalarField::characteristic()).is_one() { Ok(()) } else { Err(SerializationError::InvalidData) diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 2c139b17e..bc4719c8f 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -34,7 +34,7 @@ pub trait VariableBaseMSM: ScalarMul { fn msm(bases: &[Self::MulBase], scalars: &[Self::ScalarField]) -> Result { (bases.len() == scalars.len()) .then(|| Self::msm_unchecked(bases, scalars)) - .ok_or(usize::min(bases.len(), scalars.len())) + .ok_or(bases.len().min(scalars.len())) } /// Optimized implementation of multi-scalar multiplication. @@ -265,15 +265,14 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec { let u64_idx = bit_offset / 64; let bit_idx = bit_offset % 64; // Read the bits from the scalar - let bit_buf: u64; - if bit_idx < 64 - w || u64_idx == scalar.len() - 1 { + let bit_buf = if bit_idx < 64 - w || u64_idx == scalar.len() - 1 { // This window's bits are contained in a single u64, // or it's the last u64 anyway. - bit_buf = scalar[u64_idx] >> bit_idx; + scalar[u64_idx] >> bit_idx } else { // Combine the current u64's bits with the bits from the next u64 - bit_buf = (scalar[u64_idx] >> bit_idx) | (scalar[1 + u64_idx] << (64 - bit_idx)); - } + (scalar[u64_idx] >> bit_idx) | (scalar[1 + u64_idx] << (64 - bit_idx)) + }; // Read the actual coefficient value from the window let coef = carry + (bit_buf & window_mask); // coef = [0, 2^r) diff --git a/ff-macros/src/lib.rs b/ff-macros/src/lib.rs index 23348bb6c..d642d1a79 100644 --- a/ff-macros/src/lib.rs +++ b/ff-macros/src/lib.rs @@ -92,7 +92,7 @@ pub fn unroll_for_loops(args: TokenStream, input: TokenStream) -> TokenStream { block: ref box_block, .. } = &item_fn; - unroll::unroll_in_block(&**box_block, unroll_by) + unroll::unroll_in_block(box_block, unroll_by) }; let new_item = Item::Fn(ItemFn { block: Box::new(new_block), diff --git a/ff-macros/src/montgomery/biginteger.rs b/ff-macros/src/montgomery/biginteger.rs index 4a764f6a1..b1a14304b 100644 --- a/ff-macros/src/montgomery/biginteger.rs +++ b/ff-macros/src/montgomery/biginteger.rs @@ -23,7 +23,6 @@ pub(super) fn add_with_carry_impl(num_limbs: usize) -> proc_macro2::TokenStream #body } } - .into() } pub(super) fn sub_with_borrow_impl(num_limbs: usize) -> proc_macro2::TokenStream { @@ -49,7 +48,6 @@ pub(super) fn sub_with_borrow_impl(num_limbs: usize) -> proc_macro2::TokenStream #body } } - .into() } pub(super) fn subtract_modulus_impl( diff --git a/ff-macros/src/montgomery/mod.rs b/ff-macros/src/montgomery/mod.rs index a086399c1..4b3cdd146 100644 --- a/ff-macros/src/montgomery/mod.rs +++ b/ff-macros/src/montgomery/mod.rs @@ -170,5 +170,4 @@ pub fn mont_config_helper( #sub_with_borrow } } - .into() } diff --git a/ff-macros/src/montgomery/mul.rs b/ff-macros/src/montgomery/mul.rs index 8f6859aea..e4c9d1c97 100644 --- a/ff-macros/src/montgomery/mul.rs +++ b/ff-macros/src/montgomery/mul.rs @@ -26,8 +26,7 @@ pub(super) fn mul_assign_impl( let mut carry2 = 0u64; fa::mac_discard(r[0], k, #modulus_0, &mut carry2); }); - for j in 1..num_limbs { - let modulus_j = modulus_limbs[j]; + for (j, modulus_j) in modulus_limbs.iter().enumerate().take(num_limbs).skip(1) { let idx = j - 1; default.extend(quote! { r[#j] = fa::mac_with_carry(r[#j], (a.0).0[#j], (b.0).0[#i], &mut carry1); diff --git a/ff-macros/src/montgomery/square.rs b/ff-macros/src/montgomery/square.rs index df4c866e5..8fffc510f 100644 --- a/ff-macros/src/montgomery/square.rs +++ b/ff-macros/src/montgomery/square.rs @@ -49,9 +49,8 @@ pub(super) fn square_in_place_impl( let mut carry = 0; fa::mac_discard(r[#i], k, #modulus_0, &mut carry); }); - for j in 1..num_limbs { + for (j, modulus_j) in modulus_limbs.iter().enumerate().take(num_limbs).skip(1) { let idx = j + i; - let modulus_j = modulus_limbs[j]; default.extend(quote! { r[#idx] = fa::mac_with_carry(r[#idx], k, #modulus_j, &mut carry); }); diff --git a/ff-macros/src/montgomery/sum_of_products.rs b/ff-macros/src/montgomery/sum_of_products.rs index 5e1d58d52..57b992e7a 100644 --- a/ff-macros/src/montgomery/sum_of_products.rs +++ b/ff-macros/src/montgomery/sum_of_products.rs @@ -33,8 +33,7 @@ pub(super) fn sum_of_products_impl(num_limbs: usize, modulus: &[u64]) -> proc_ma }); } let mut mont_red_body = proc_macro2::TokenStream::new(); - for i in 1..num_limbs { - let modulus_i = modulus[i]; + for (i, modulus_i) in modulus.iter().enumerate().take(num_limbs).skip(1) { mont_red_body.extend(quote! { result.0[#i - 1] = fa::mac_with_carry(result.0[#i], k, #modulus_i, &mut carry2); }); diff --git a/ff-macros/src/unroll.rs b/ff-macros/src/unroll.rs index 63fad7d01..f90d89d34 100644 --- a/ff-macros/src/unroll.rs +++ b/ff-macros/src/unroll.rs @@ -196,17 +196,17 @@ fn unroll(expr: &Expr, unroll_by: usize) -> Expr { .. } = *if_expr; Expr::If(ExprIf { - cond: Box::new(unroll(&**cond, unroll_by)), + cond: Box::new(unroll(cond, unroll_by)), then_branch: unroll_in_block(then_branch, unroll_by), else_branch: else_branch .as_ref() - .map(|x| (x.0, Box::new(unroll(&*x.1, unroll_by)))), + .map(|x| (x.0, Box::new(unroll(&x.1, unroll_by)))), ..(*if_expr).clone() }) } else if let Expr::Let(let_expr) = expr { let ExprLet { ref expr, .. } = *let_expr; Expr::Let(ExprLet { - expr: Box::new(unroll(&**expr, unroll_by)), + expr: Box::new(unroll(expr, unroll_by)), ..(*let_expr).clone() }) } else if let Expr::Block(expr_block) = expr { diff --git a/ff/src/biginteger/arithmetic.rs b/ff/src/biginteger/arithmetic.rs index ee2f5efd2..9b4ca702f 100644 --- a/ff/src/biginteger/arithmetic.rs +++ b/ff/src/biginteger/arithmetic.rs @@ -60,11 +60,7 @@ macro_rules! sbb { pub(crate) fn sbb(a: &mut u64, b: u64, borrow: u64) -> u64 { let tmp = (1u128 << 64) + (*a as u128) - (b as u128) - (borrow as u128); *a = tmp as u64; - if tmp >> 64 == 0 { - 1 - } else { - 0 - } + (tmp >> 64 == 0) as u64 } /// Sets a = a - b - borrow, and returns the borrow. diff --git a/ff/src/fields/fft_friendly.rs b/ff/src/fields/fft_friendly.rs index 8a2a5bbdd..74367e0f6 100644 --- a/ff/src/fields/fft_friendly.rs +++ b/ff/src/fields/fft_friendly.rs @@ -56,7 +56,7 @@ pub trait FftField: crate::Field { omega = large_subgroup_root_of_unity; for _ in q_adicity..small_subgroup_base_adicity { - omega = omega.pow(&[q as u64]); + omega = omega.pow([q as u64]); } for _ in two_adicity..Self::TWO_ADICITY { diff --git a/poly/src/evaluations/multivariate/multilinear/sparse.rs b/poly/src/evaluations/multivariate/multilinear/sparse.rs index 033189d5e..68b4726d2 100644 --- a/poly/src/evaluations/multivariate/multilinear/sparse.rs +++ b/poly/src/evaluations/multivariate/multilinear/sparse.rs @@ -107,7 +107,7 @@ fn precompute_eq(g: &[F]) -> Vec { dp[0] = F::one() - g[0]; dp[1] = g[0]; for i in 1..dim { - let dp_prev = (&dp[0..(1 << i)]).to_vec(); + let dp_prev = dp[0..(1 << i)].to_vec(); for b in 0..(1 << i) { dp[b] = dp_prev[b] * (F::one() - g[i]); dp[b + (1 << i)] = dp_prev[b] * g[i]; diff --git a/poly/src/polynomial/multivariate/mod.rs b/poly/src/polynomial/multivariate/mod.rs index 19f0ffae3..558e41b5f 100644 --- a/poly/src/polynomial/multivariate/mod.rs +++ b/poly/src/polynomial/multivariate/mod.rs @@ -110,7 +110,7 @@ impl Term for SparseTerm { /// Evaluates `self` at the given `point` in the field. fn evaluate(&self, point: &[F]) -> F { cfg_into_iter!(self) - .map(|(var, power)| point[*var].pow(&[*power as u64])) + .map(|(var, power)| point[*var].pow([*power as u64])) .product() } } diff --git a/poly/src/polynomial/multivariate/sparse.rs b/poly/src/polynomial/multivariate/sparse.rs index 7f6694bc7..613f2cfe4 100644 --- a/poly/src/polynomial/multivariate/sparse.rs +++ b/poly/src/polynomial/multivariate/sparse.rs @@ -239,7 +239,7 @@ impl Neg for SparsePolynomial { #[inline] fn neg(mut self) -> SparsePolynomial { for coeff in &mut self.terms { - (*coeff).0 = -coeff.0; + (coeff).0 = -coeff.0; } self } diff --git a/poly/src/polynomial/univariate/sparse.rs b/poly/src/polynomial/univariate/sparse.rs index 04efa6370..83a473dfe 100644 --- a/poly/src/polynomial/univariate/sparse.rs +++ b/poly/src/polynomial/univariate/sparse.rs @@ -89,11 +89,11 @@ impl Polynomial for SparsePolynomial { .iter() .map(|(i, c)| { debug_assert_eq!( - F::pow_with_table(&powers_of_2[..], &[*i as u64]).unwrap(), - point.pow(&[*i as u64]), + F::pow_with_table(&powers_of_2[..], [*i as u64]).unwrap(), + point.pow([*i as u64]), "pows not equal" ); - *c * F::pow_with_table(&powers_of_2[..], &[*i as u64]).unwrap() + *c * F::pow_with_table(&powers_of_2[..], [*i as u64]).unwrap() }) .sum(); total @@ -207,7 +207,7 @@ impl<'b, F: Field> Mul for &'b SparsePolynomial { } else { let mut result = self.clone(); cfg_iter_mut!(result).for_each(|e| { - (*e).1 *= elem; + e.1 *= elem; }); result } diff --git a/test-templates/src/h2c/json.rs b/test-templates/src/h2c/json.rs index 8f2694db1..b2244abcd 100644 --- a/test-templates/src/h2c/json.rs +++ b/test-templates/src/h2c/json.rs @@ -1,4 +1,6 @@ -#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] +#[derive( + Default, Debug, Clone, PartialEq, Eq, serde_derive::Serialize, serde_derive::Deserialize, +)] #[serde(rename_all = "camelCase")] pub struct SuiteVector { pub ciphersuite: String, @@ -11,20 +13,26 @@ pub struct SuiteVector { pub vectors: Vec, } -#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] +#[derive( + Default, Debug, Clone, PartialEq, Eq, serde_derive::Serialize, serde_derive::Deserialize, +)] #[serde(rename_all = "camelCase")] pub struct Field { pub m: String, pub p: String, } -#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] +#[derive( + Default, Debug, Clone, PartialEq, Eq, serde_derive::Serialize, serde_derive::Deserialize, +)] #[serde(rename_all = "camelCase")] pub struct Map { pub name: String, } -#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] +#[derive( + Default, Debug, Clone, PartialEq, Eq, serde_derive::Serialize, serde_derive::Deserialize, +)] #[serde(rename_all = "camelCase")] pub struct Vector { #[serde(rename = "P")] @@ -33,7 +41,9 @@ pub struct Vector { pub u: Vec, } -#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)] +#[derive( + Default, Debug, Clone, PartialEq, Eq, serde_derive::Serialize, serde_derive::Deserialize, +)] #[serde(rename_all = "camelCase")] pub struct P { pub x: String,