diff --git a/ec/Cargo.toml b/ec/Cargo.toml index 7f6538e49..8d8d412ea 100644 --- a/ec/Cargo.toml +++ b/ec/Cargo.toml @@ -15,6 +15,9 @@ metadata.docs.rs.workspace = true package.metadata.release.workspace = true keywords = ["cryptography", "elliptic-curves", "pairing"] +[lints] +workspace = true + [dependencies] ark-std.workspace = true ark-serialize.workspace = true @@ -30,7 +33,7 @@ hashbrown.workspace = true itertools.workspace = true [target.'cfg(all(target_has_atomic = "8", target_has_atomic = "16", target_has_atomic = "32", target_has_atomic = "64", target_has_atomic = "ptr"))'.dependencies] -ahash = { version = "0.8", default-features = false} +ahash = { version = "0.8", default-features = false } [target.'cfg(not(all(target_has_atomic = "8", target_has_atomic = "16", target_has_atomic = "32", target_has_atomic = "64", target_has_atomic = "ptr")))'.dependencies] fnv = { version = "1.0", default-features = false } @@ -46,5 +49,5 @@ hex.workspace = true [features] default = [] -std = [ "ark-std/std", "ark-ff/std", "ark-serialize/std" ] -parallel = [ "std", "rayon", "ark-std/parallel", "ark-serialize/parallel" ] +std = ["ark-std/std", "ark-ff/std", "ark-serialize/std"] +parallel = ["std", "rayon", "ark-std/parallel", "ark-serialize/parallel"] diff --git a/ec/src/hashing/curve_maps/elligator2.rs b/ec/src/hashing/curve_maps/elligator2.rs index b80d83514..142171a9c 100644 --- a/ec/src/hashing/curve_maps/elligator2.rs +++ b/ec/src/hashing/curve_maps/elligator2.rs @@ -182,14 +182,14 @@ mod test { #[derive(ark_ff::MontConfig)] #[modulus = "101"] #[generator = "2"] - pub struct F101Config; - pub type F101 = Fp64>; + pub(crate) struct F101Config; + pub(crate) type F101 = Fp64>; #[derive(ark_ff::MontConfig)] #[modulus = "11"] #[generator = "2"] - pub struct F11Config; - pub type F11 = Fp64>; + pub(crate) struct F11Config; + pub(crate) type F11 = Fp64>; struct TestElligator2MapToCurveConfig; @@ -225,10 +225,9 @@ mod test { /// COEFF_D = 12 const COEFF_D: F101 = MontFp!("12"); - const GENERATOR: Affine = - Affine::new_unchecked(MontFp!("23"), MontFp!("24")); + const GENERATOR: Affine = Affine::new_unchecked(MontFp!("23"), MontFp!("24")); - type MontCurveConfig = TestElligator2MapToCurveConfig; + type MontCurveConfig = Self; } impl MontCurveConfig for TestElligator2MapToCurveConfig { @@ -238,7 +237,7 @@ mod test { /// COEFF_B = 23 const COEFF_B: F101 = MontFp!("23"); - type TECurveConfig = TestElligator2MapToCurveConfig; + type TECurveConfig = Self; } /// sage: find_z_ell2(F101) diff --git a/ec/src/hashing/curve_maps/swu.rs b/ec/src/hashing/curve_maps/swu.rs index fa88ae3d2..4172a2651 100644 --- a/ec/src/hashing/curve_maps/swu.rs +++ b/ec/src/hashing/curve_maps/swu.rs @@ -126,10 +126,10 @@ impl MapToCurve> for SWUMap

{ let y = if gx1_square { y1 } else { y2 }; let x_affine = num_x / div; - let y_affine = if parity(&y) != parity(&element) { - -y - } else { + let y_affine = if parity(&y) == parity(&element) { y + } else { + -y }; let point_on_curve = Affine::new_unchecked(x_affine, y_affine); debug_assert!( @@ -175,8 +175,8 @@ mod test { #[derive(ark_ff::MontConfig)] #[modulus = "127"] #[generator = "6"] - pub struct F127Config; - pub type F127 = Fp64>; + pub(crate) struct F127Config; + pub(crate) type F127 = Fp64>; const F127_ONE: F127 = MontFp!("1"); diff --git a/ec/src/hashing/curve_maps/wb.rs b/ec/src/hashing/curve_maps/wb.rs index ee12cfd43..0690462f6 100644 --- a/ec/src/hashing/curve_maps/wb.rs +++ b/ec/src/hashing/curve_maps/wb.rs @@ -135,8 +135,8 @@ mod test { #[derive(ark_ff::MontConfig)] #[modulus = "127"] #[generator = "6"] - pub struct F127Config; - pub type F127 = Fp64>; + pub(crate) struct F127Config; + pub(crate) type F127 = Fp64>; const F127_ZERO: F127 = MontFp!("0"); const F127_ONE: F127 = MontFp!("1"); diff --git a/ec/src/hashing/map_to_curve_hasher.rs b/ec/src/hashing/map_to_curve_hasher.rs index 328edb3af..473682430 100644 --- a/ec/src/hashing/map_to_curve_hasher.rs +++ b/ec/src/hashing/map_to_curve_hasher.rs @@ -41,7 +41,7 @@ where fn new(domain: &[u8]) -> Result { #[cfg(test)] M2C::check_parameters()?; - Ok(MapToCurveBasedHasher { + Ok(Self { field_hasher: H2F::new(domain), _phantom: PhantomData, }) diff --git a/ec/src/hashing/mod.rs b/ec/src/hashing/mod.rs index e57c997b4..59d0aa24d 100644 --- a/ec/src/hashing/mod.rs +++ b/ec/src/hashing/mod.rs @@ -31,8 +31,7 @@ impl ark_std::error::Error for HashToCurveError {} impl fmt::Display for HashToCurveError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { - HashToCurveError::UnsupportedCurveError(s) => write!(f, "{}", s), - HashToCurveError::MapToCurveError(s) => write!(f, "{}", s), + Self::UnsupportedCurveError(s) | Self::MapToCurveError(s) => write!(f, "{}", s), } } } diff --git a/ec/src/lib.rs b/ec/src/lib.rs index afa1031ec..9268dc901 100644 --- a/ec/src/lib.rs +++ b/ec/src/lib.rs @@ -119,6 +119,7 @@ pub trait CurveGroup: /// to this group element. /// /// The point is guaranteed to be in the correct prime order subgroup. +#[allow(clippy::trait_duplication_in_bounds)] pub trait AffineRepr: Eq + 'static diff --git a/ec/src/models/bls12/g1.rs b/ec/src/models/bls12/g1.rs index 1c852ac37..38a07c05f 100644 --- a/ec/src/models/bls12/g1.rs +++ b/ec/src/models/bls12/g1.rs @@ -16,7 +16,7 @@ pub struct G1Prepared(pub G1Affine

); impl From> for G1Prepared

{ fn from(other: G1Affine

) -> Self { - G1Prepared(other) + Self(other) } } @@ -28,7 +28,7 @@ impl From> for G1Prepared

{ impl<'a, P: Bls12Config> From<&'a G1Affine

> for G1Prepared

{ fn from(other: &'a G1Affine

) -> Self { - G1Prepared(*other) + Self(*other) } } @@ -46,6 +46,6 @@ impl G1Prepared

{ impl Default for G1Prepared

{ fn default() -> Self { - G1Prepared(G1Affine::

::generator()) + Self(G1Affine::

::generator()) } } diff --git a/ec/src/models/bls12/g2.rs b/ec/src/models/bls12/g2.rs index 25025593c..27ebde301 100644 --- a/ec/src/models/bls12/g2.rs +++ b/ec/src/models/bls12/g2.rs @@ -45,7 +45,7 @@ impl Default for G2Prepared

{ impl From> for G2Prepared

{ fn from(q: G2Affine

) -> Self { let two_inv = P::Fp::one().double().inverse().unwrap(); - let zero = G2Prepared { + let zero = Self { ell_coeffs: vec![], infinity: true, }; @@ -92,7 +92,7 @@ impl<'a, P: Bls12Config> From<&'a G2Projective

> for G2Prepared

{ } impl G2Prepared

{ - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.infinity } } diff --git a/ec/src/models/bn/g1.rs b/ec/src/models/bn/g1.rs index 3f4c1d727..e74e3f6ec 100644 --- a/ec/src/models/bn/g1.rs +++ b/ec/src/models/bn/g1.rs @@ -16,7 +16,7 @@ pub struct G1Prepared(pub G1Affine

); impl From> for G1Prepared

{ fn from(other: G1Affine

) -> Self { - G1Prepared(other) + Self(other) } } @@ -28,7 +28,7 @@ impl From> for G1Prepared

{ impl<'a, P: BnConfig> From<&'a G1Affine

> for G1Prepared

{ fn from(other: &'a G1Affine

) -> Self { - G1Prepared(*other) + Self(*other) } } @@ -39,13 +39,13 @@ impl<'a, P: BnConfig> From<&'a G1Projective

> for G1Prepared

{ } impl G1Prepared

{ - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0.infinity } } impl Default for G1Prepared

{ fn default() -> Self { - G1Prepared(G1Affine::

::generator()) + Self(G1Affine::

::generator()) } } diff --git a/ec/src/models/bn/g2.rs b/ec/src/models/bn/g2.rs index b8444b2a2..08bd389ab 100644 --- a/ec/src/models/bn/g2.rs +++ b/ec/src/models/bn/g2.rs @@ -99,7 +99,7 @@ impl Default for G2Prepared

{ impl From> for G2Prepared

{ fn from(q: G2Affine

) -> Self { if q.infinity { - G2Prepared { + Self { ell_coeffs: vec![], infinity: true, } @@ -163,7 +163,7 @@ impl<'a, P: BnConfig> From<&'a G2Projective

> for G2Prepared

{ } impl G2Prepared

{ - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.infinity } } diff --git a/ec/src/models/bw6/g1.rs b/ec/src/models/bw6/g1.rs index 78b819091..f534ac7ca 100644 --- a/ec/src/models/bw6/g1.rs +++ b/ec/src/models/bw6/g1.rs @@ -16,7 +16,7 @@ pub struct G1Prepared(pub G1Affine

); impl From> for G1Prepared

{ fn from(other: G1Affine

) -> Self { - G1Prepared(other) + Self(other) } } @@ -28,7 +28,7 @@ impl From> for G1Prepared

{ impl<'a, P: BW6Config> From<&'a G1Affine

> for G1Prepared

{ fn from(other: &'a G1Affine

) -> Self { - G1Prepared(*other) + Self(*other) } } @@ -39,13 +39,13 @@ impl<'a, P: BW6Config> From<&'a G1Projective

> for G1Prepared

{ } impl G1Prepared

{ - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0.infinity } } impl Default for G1Prepared

{ fn default() -> Self { - G1Prepared(G1Affine::

::generator()) + Self(G1Affine::

::generator()) } } diff --git a/ec/src/models/bw6/g2.rs b/ec/src/models/bw6/g2.rs index 0094bb58c..9b8471e85 100644 --- a/ec/src/models/bw6/g2.rs +++ b/ec/src/models/bw6/g2.rs @@ -44,7 +44,7 @@ impl From> for G2Affine

{ let z_inv = q.z.inverse().unwrap(); let x = q.x * &z_inv; let y = q.y * &z_inv; - G2Affine::

::new_unchecked(x, y) + Self::new_unchecked(x, y) } } @@ -132,7 +132,7 @@ impl From> for G2Prepared

{ } impl G2Prepared

{ - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.infinity } } diff --git a/ec/src/models/bw6/mod.rs b/ec/src/models/bw6/mod.rs index b40f17959..a1c65e98e 100644 --- a/ec/src/models/bw6/mod.rs +++ b/ec/src/models/bw6/mod.rs @@ -247,16 +247,17 @@ impl BW6

{ } fn final_exponentiation_hard_part(f: &Fp6) -> Fp6 { + // A = m**(u-1) + let a = Self::exp_by_x_minus_1(f); + // A = A**(u-1) + let a = Self::exp_by_x_minus_1(&a); + // Generic implementation of the hard part of the final exponentiation for the BW6 family. // Computes (u+1)*Phi_k(p(u))/r(u) if P::T_MOD_R_IS_ZERO { // Algorithm 4.3 from https://yelhousni.github.io/phd.pdf // Follows the implementation https://gitlab.inria.fr/zk-curves/snark-2-chains/-/blob/master/sage/pairing_bw6_bls12.py#L1036 - // A = m**(u-1) - let a = Self::exp_by_x_minus_1(f); - // A = A**(u-1) - let a = Self::exp_by_x_minus_1(&a); // A = (m * A).conjugate() * m.frobenius() let a = (f * &a).cyclotomic_inverse().unwrap() * f.frobenius_map(1); // B = A**(u+1) * m @@ -296,10 +297,6 @@ impl BW6

{ // Algorithm 4.4 from https://yelhousni.github.io/phd.pdf // Follows the implementation https://gitlab.inria.fr/zk-curves/snark-2-chains/-/blob/master/sage/pairing_bw6_bls12.py#L969 - // A = m**(u-1) - let a = Self::exp_by_x_minus_1(f); - // A = A**(u-1) - let a = Self::exp_by_x_minus_1(&a); // A = A * m.frobenius() let a = a * f.frobenius_map(1); // B = A**(u+1) * m.conjugate() diff --git a/ec/src/models/mnt4/g2.rs b/ec/src/models/mnt4/g2.rs index 64a88f2f7..69a94497c 100644 --- a/ec/src/models/mnt4/g2.rs +++ b/ec/src/models/mnt4/g2.rs @@ -34,7 +34,7 @@ impl From> for G2Prepared

{ fn from(g: G2Affine

) -> Self { let twist_inv = P::TWIST.inverse().unwrap(); - let mut g_prep = G2Prepared { + let mut g_prep = Self { x: g.x, y: g.y, x_over_twist: g.x * &twist_inv, diff --git a/ec/src/models/mnt6/g2.rs b/ec/src/models/mnt6/g2.rs index 272674fd2..bbf2dd3b1 100644 --- a/ec/src/models/mnt6/g2.rs +++ b/ec/src/models/mnt6/g2.rs @@ -34,7 +34,7 @@ impl From> for G2Prepared

{ fn from(g: G2Affine

) -> Self { let twist_inv = P::TWIST.inverse().unwrap(); - let mut g_prep = G2Prepared { + let mut g_prep = Self { x: g.x, y: g.y, x_over_twist: g.x * &twist_inv, diff --git a/ec/src/models/short_weierstrass/affine.rs b/ec/src/models/short_weierstrass/affine.rs index 026f1f286..75d0e1b3d 100644 --- a/ec/src/models/short_weierstrass/affine.rs +++ b/ec/src/models/short_weierstrass/affine.rs @@ -137,15 +137,15 @@ impl Affine

{ /// Checks if `self` is a valid point on the curve. pub fn is_on_curve(&self) -> bool { - if !self.infinity { + if self.infinity { + true + } else { // Rust does not optimise away addition with zero let mut x3b = P::add_b(self.x.square() * self.x); if !P::COEFF_A.is_zero() { x3b += P::mul_by_a(self.x); }; self.y.square() == x3b - } else { - true } } @@ -330,12 +330,12 @@ impl> Mul for Affine

{ // coordinates as X/Z^2, Y/Z^3. impl From> for Affine

{ #[inline] - fn from(p: Projective

) -> Affine

{ + fn from(p: Projective

) -> Self { if p.is_zero() { - Affine::identity() + Self::identity() } else if p.z.is_one() { // If Z is one, the point is already normalized. - Affine::new_unchecked(p.x, p.y) + Self::new_unchecked(p.x, p.y) } else { // Z is nonzero, so it must have an inverse in a field. let zinv = p.z.inverse().unwrap(); @@ -347,7 +347,7 @@ impl From> for Affine

{ // Y/Z^3 let y = p.y * &(zinv_squared * &zinv); - Affine::new_unchecked(x, y) + Self::new_unchecked(x, y) } } } diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index d4eda2a06..c98003f14 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -72,10 +72,10 @@ impl PartialEq for Projective

{ let z1z1 = self.z.square(); let z2z2 = other.z.square(); - if self.x * &z2z2 != other.x * &z1z1 { - false - } else { + if self.x * &z2z2 == other.x * &z1z1 { self.y * &(z2z2 * &other.z) == other.y * &(z1z1 * &self.z) + } else { + false } } } @@ -189,20 +189,17 @@ impl AdditiveGroup for Projective

{ // D = 2*((X1+B)^2-A-C) // = 2 * (X1 + Y1^2)^2 - A - C // = 2 * 2 * X1 * Y1^2 - let d = if [1, 2].contains(&P::BaseField::extension_degree()) { - let mut d = self.x; + let mut d = self.x; + if [1, 2].contains(&P::BaseField::extension_degree()) { d *= &b; d.double_in_place().double_in_place(); - d } else { - let mut d = self.x; d += &b; d.square_in_place(); d -= a; d -= c; d.double_in_place(); - d - }; + } // E = 3*A let e = a + &*a.double_in_place(); @@ -222,7 +219,6 @@ impl AdditiveGroup for Projective

{ self.y -= &self.x; self.y *= &e; self.y -= c.double_in_place().double_in_place().double_in_place(); - self } else { // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l // XX = X1^2 @@ -264,9 +260,9 @@ impl AdditiveGroup for Projective

{ self.y -= &self.x; self.y *= &m; self.y -= yyyy.double_in_place().double_in_place().double_in_place(); - - self } + + self } } @@ -573,8 +569,8 @@ impl> Mul for Projective

{ // coordinates with Z = 1. impl From> for Projective

{ #[inline] - fn from(p: Affine

) -> Projective

{ - p.xy().map_or(Projective::zero(), |(x, y)| Self { + fn from(p: Affine

) -> Self { + p.xy().map_or_else(Self::zero, |(x, y)| Self { x, y, z: P::BaseField::one(), @@ -654,6 +650,6 @@ impl VariableBaseMSM for Projective

{ impl>> core::iter::Sum for Projective

{ fn sum>(iter: I) -> Self { - iter.fold(Projective::zero(), |sum, x| sum + x.borrow()) + iter.fold(Self::zero(), |sum, x| sum + x.borrow()) } } diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 96fedaa65..e181966a2 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -106,7 +106,7 @@ pub trait SWCurveConfig: super::CurveConfig { ) -> Result, usize> { (bases.len() == scalars.len()) .then(|| VariableBaseMSM::msm_unchecked(bases, scalars)) - .ok_or(bases.len().min(scalars.len())) + .ok_or_else(|| bases.len().min(scalars.len())) } /// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is @@ -176,7 +176,7 @@ pub trait SWCurveConfig: super::CurveConfig { Ok(Affine::identity()) } else { let point = Affine::new_unchecked(x, y); - if let Validate::Yes = validate { + if validate == Validate::Yes { point.check()?; } Ok(point) diff --git a/ec/src/models/short_weierstrass/serialization_flags.rs b/ec/src/models/short_weierstrass/serialization_flags.rs index c2c9c0b41..2e5a3c9c1 100644 --- a/ec/src/models/short_weierstrass/serialization_flags.rs +++ b/ec/src/models/short_weierstrass/serialization_flags.rs @@ -15,8 +15,8 @@ pub enum SWFlags { impl SWFlags { #[inline] - pub fn infinity() -> Self { - SWFlags::PointAtInfinity + pub const fn infinity() -> Self { + Self::PointAtInfinity } #[inline] @@ -29,16 +29,16 @@ impl SWFlags { } #[inline] - pub fn is_infinity(&self) -> bool { - matches!(self, SWFlags::PointAtInfinity) + pub const fn is_infinity(&self) -> bool { + matches!(self, Self::PointAtInfinity) } #[inline] - pub fn is_positive(&self) -> Option { + pub const fn is_positive(&self) -> Option { match self { - SWFlags::PointAtInfinity => None, - SWFlags::YIsPositive => Some(true), - SWFlags::YIsNegative => Some(false), + Self::PointAtInfinity => None, + Self::YIsPositive => Some(true), + Self::YIsNegative => Some(false), } } } @@ -47,7 +47,7 @@ impl Default for SWFlags { #[inline] fn default() -> Self { // YIsNegative doesn't change the serialization - SWFlags::YIsNegative + Self::YIsNegative } } @@ -58,8 +58,8 @@ impl Flags for SWFlags { fn u8_bitmask(&self) -> u8 { let mut mask = 0; match self { - SWFlags::PointAtInfinity => mask |= 1 << 6, - SWFlags::YIsNegative => mask |= 1 << 7, + Self::PointAtInfinity => mask |= 1 << 6, + Self::YIsNegative => mask |= 1 << 7, _ => (), } mask @@ -73,9 +73,9 @@ impl Flags for SWFlags { // This is invalid because we only want *one* way to serialize // the point at infinity. (true, true) => None, - (false, true) => Some(SWFlags::PointAtInfinity), - (true, false) => Some(SWFlags::YIsNegative), - (false, false) => Some(SWFlags::YIsPositive), + (false, true) => Some(Self::PointAtInfinity), + (true, false) => Some(Self::YIsNegative), + (false, false) => Some(Self::YIsPositive), } } } diff --git a/ec/src/models/twisted_edwards/affine.rs b/ec/src/models/twisted_edwards/affine.rs index 299b699f3..3481b05c5 100644 --- a/ec/src/models/twisted_edwards/affine.rs +++ b/ec/src/models/twisted_edwards/affine.rs @@ -294,18 +294,18 @@ impl> Mul for Affine

{ // The projective point X, Y, T, Z is represented in the affine // coordinates as X/Z, Y/Z. impl From> for Affine

{ - fn from(p: Projective

) -> Affine

{ + fn from(p: Projective

) -> Self { if p.is_zero() { - Affine::zero() + Self::zero() } else if p.z.is_one() { // If Z is one, the point is already normalized. - Affine::new_unchecked(p.x, p.y) + Self::new_unchecked(p.x, p.y) } else { // Z is nonzero, so it must have an inverse in a field. let z_inv = p.z.inverse().unwrap(); let x = p.x * &z_inv; let y = p.y * &z_inv; - Affine::new_unchecked(x, y) + Self::new_unchecked(x, y) } } } diff --git a/ec/src/models/twisted_edwards/group.rs b/ec/src/models/twisted_edwards/group.rs index 1edf8e074..b430ba52e 100644 --- a/ec/src/models/twisted_edwards/group.rs +++ b/ec/src/models/twisted_edwards/group.rs @@ -35,7 +35,7 @@ use crate::{ /// This implementation uses the unified addition formulae from that paper (see /// Section 3.1). #[derive(Educe)] -#[educe(Copy, Clone, Eq(bound(P: TECurveConfig)), Debug)] +#[educe(Copy, Clone, Eq, Debug)] #[must_use] pub struct Projective { pub x: P::BaseField, @@ -398,7 +398,7 @@ impl>> ark_std::iter::Sum for Projectiv // The affine point (X, Y) is represented in the Extended Projective coordinates // with Z = 1. impl From> for Projective

{ - fn from(p: Affine

) -> Projective

{ + fn from(p: Affine

) -> Self { Self::new_unchecked(p.x, p.y, p.x * &p.y, P::BaseField::one()) } } @@ -417,7 +417,7 @@ impl Display for MontgomeryAffine

{ } impl MontgomeryAffine

{ - pub fn new(x: P::BaseField, y: P::BaseField) -> Self { + pub const fn new(x: P::BaseField, y: P::BaseField) -> Self { Self { x, y } } } diff --git a/ec/src/models/twisted_edwards/mod.rs b/ec/src/models/twisted_edwards/mod.rs index 64c52a65b..5c037b1a2 100644 --- a/ec/src/models/twisted_edwards/mod.rs +++ b/ec/src/models/twisted_edwards/mod.rs @@ -93,7 +93,7 @@ pub trait TECurveConfig: super::CurveConfig { ) -> Result, usize> { (bases.len() == scalars.len()) .then(|| VariableBaseMSM::msm_unchecked(bases, scalars)) - .ok_or(bases.len().min(scalars.len())) + .ok_or_else(|| bases.len().min(scalars.len())) } /// If uncompressed, serializes both x and y coordinates. @@ -143,7 +143,7 @@ pub trait TECurveConfig: super::CurveConfig { }, }; let point = Affine::new_unchecked(x, y); - if let Validate::Yes = validate { + if validate == Validate::Yes { point.check()?; } Ok(point) diff --git a/ec/src/models/twisted_edwards/serialization_flags.rs b/ec/src/models/twisted_edwards/serialization_flags.rs index 56ae85e91..cc2e4966e 100644 --- a/ec/src/models/twisted_edwards/serialization_flags.rs +++ b/ec/src/models/twisted_edwards/serialization_flags.rs @@ -13,15 +13,15 @@ impl TEFlags { #[inline] pub fn from_x_coordinate(x: impl Field) -> Self { if x <= -x { - TEFlags::XIsPositive + Self::XIsPositive } else { - TEFlags::XIsNegative + Self::XIsNegative } } #[inline] - pub fn is_negative(&self) -> bool { - matches!(*self, TEFlags::XIsNegative) + pub const fn is_negative(&self) -> bool { + matches!(*self, Self::XIsNegative) } } @@ -29,7 +29,7 @@ impl Default for TEFlags { #[inline] fn default() -> Self { // XIsPositive doesn't change the serialization - TEFlags::XIsPositive + Self::XIsPositive } } @@ -39,7 +39,7 @@ impl Flags for TEFlags { #[inline] fn u8_bitmask(&self) -> u8 { let mut mask = 0; - if let Self::XIsNegative = self { + if matches!(self, Self::XIsNegative) { mask |= 1 << 7; } mask diff --git a/ec/src/pairing.rs b/ec/src/pairing.rs index 22ac96805..a0a59f9c4 100644 --- a/ec/src/pairing.rs +++ b/ec/src/pairing.rs @@ -165,7 +165,7 @@ impl CanonicalDeserialize for PairingOutput

{ validate: Validate, ) -> Result { let f = P::TargetField::deserialize_with_mode(reader, compress, validate).map(Self)?; - if let Validate::Yes = validate { + if validate == Validate::Yes { f.check()?; } Ok(f) diff --git a/ec/src/scalar_mul/mod.rs b/ec/src/scalar_mul/mod.rs index ad9dc0771..9eddc79ab 100644 --- a/ec/src/scalar_mul/mod.rs +++ b/ec/src/scalar_mul/mod.rs @@ -21,7 +21,7 @@ use rayon::prelude::*; /// [`Explanation of usage`] /// /// [`Explanation of usage`]: https://github.com/scipr-lab/zexe/issues/79#issue-556220473 -fn ln_without_floats(a: usize) -> usize { +const fn ln_without_floats(a: usize) -> usize { // log2(a) * ln(2) (ark_std::log2(a) * 69 / 100) as usize } @@ -222,7 +222,7 @@ impl BatchMulPreprocessing { } } - pub fn compute_window_size(num_scalars: usize) -> usize { + pub const fn compute_window_size(num_scalars: usize) -> usize { if num_scalars < 32 { 3 } else { diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 3c6f478bd..b230316d2 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -52,7 +52,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(bases.len().min(scalars.len())) + .ok_or_else(|| bases.len().min(scalars.len())) } /// Optimized implementation of multi-scalar multiplication. diff --git a/ec/src/scalar_mul/variable_base/stream_pippenger.rs b/ec/src/scalar_mul/variable_base/stream_pippenger.rs index 79f121dba..1efce90b2 100644 --- a/ec/src/scalar_mul/variable_base/stream_pippenger.rs +++ b/ec/src/scalar_mul/variable_base/stream_pippenger.rs @@ -99,7 +99,7 @@ impl HashMapPippenger { .or_insert(G::ScalarField::zero()); *entry += *scalar.borrow(); if self.buffer.len() == self.buf_size { - let bases = self.buffer.keys().cloned().collect::>(); + let bases = self.buffer.keys().copied().collect::>(); let scalars = self .buffer .values() @@ -114,7 +114,7 @@ impl HashMapPippenger { #[inline(always)] pub fn finalize(mut self) -> G { if !self.buffer.is_empty() { - let bases = self.buffer.keys().cloned().collect::>(); + let bases = self.buffer.keys().copied().collect::>(); let scalars = self .buffer .values() diff --git a/ff-asm/src/context/mod.rs b/ff-asm/src/context/mod.rs index efc8693bb..30f5d446e 100644 --- a/ff-asm/src/context/mod.rs +++ b/ff-asm/src/context/mod.rs @@ -53,7 +53,11 @@ impl<'a> Context<'a> { *self.get_decl_name(name).unwrap() } - pub(crate) fn get_decl_with_fallback(&self, name: &str, fallback_name: &str) -> Declaration<'_> { + pub(crate) fn get_decl_with_fallback( + &self, + name: &str, + fallback_name: &str, + ) -> Declaration<'_> { self.get_decl_name(name) .copied() .unwrap_or_else(|| self.get_decl(fallback_name))