Skip to content

Commit

Permalink
add clippy rules for ec crate (#929)
Browse files Browse the repository at this point in the history
* add clippy rules for ec crate

* fixes

* fmt

* fix

* fmt

---------

Co-authored-by: Pratyush Mishra <[email protected]>
  • Loading branch information
tcoratger and Pratyush authored Feb 19, 2025
1 parent 48b5348 commit 621be87
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 108 deletions.
9 changes: 6 additions & 3 deletions ec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand All @@ -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"]
15 changes: 7 additions & 8 deletions ec/src/hashing/curve_maps/elligator2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ mod test {
#[derive(ark_ff::MontConfig)]
#[modulus = "101"]
#[generator = "2"]
pub struct F101Config;
pub type F101 = Fp64<MontBackend<F101Config, 1>>;
pub(crate) struct F101Config;
pub(crate) type F101 = Fp64<MontBackend<F101Config, 1>>;

#[derive(ark_ff::MontConfig)]
#[modulus = "11"]
#[generator = "2"]
pub struct F11Config;
pub type F11 = Fp64<MontBackend<F11Config, 1>>;
pub(crate) struct F11Config;
pub(crate) type F11 = Fp64<MontBackend<F11Config, 1>>;

struct TestElligator2MapToCurveConfig;

Expand Down Expand Up @@ -225,10 +225,9 @@ mod test {
/// COEFF_D = 12
const COEFF_D: F101 = MontFp!("12");

const GENERATOR: Affine<TestElligator2MapToCurveConfig> =
Affine::new_unchecked(MontFp!("23"), MontFp!("24"));
const GENERATOR: Affine<Self> = Affine::new_unchecked(MontFp!("23"), MontFp!("24"));

type MontCurveConfig = TestElligator2MapToCurveConfig;
type MontCurveConfig = Self;
}

impl MontCurveConfig for TestElligator2MapToCurveConfig {
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions ec/src/hashing/curve_maps/swu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ impl<P: SWUConfig> MapToCurve<Projective<P>> for SWUMap<P> {
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!(
Expand Down Expand Up @@ -175,8 +175,8 @@ mod test {
#[derive(ark_ff::MontConfig)]
#[modulus = "127"]
#[generator = "6"]
pub struct F127Config;
pub type F127 = Fp64<MontBackend<F127Config, 1>>;
pub(crate) struct F127Config;
pub(crate) type F127 = Fp64<MontBackend<F127Config, 1>>;

const F127_ONE: F127 = MontFp!("1");

Expand Down
4 changes: 2 additions & 2 deletions ec/src/hashing/curve_maps/wb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ mod test {
#[derive(ark_ff::MontConfig)]
#[modulus = "127"]
#[generator = "6"]
pub struct F127Config;
pub type F127 = Fp64<MontBackend<F127Config, 1>>;
pub(crate) struct F127Config;
pub(crate) type F127 = Fp64<MontBackend<F127Config, 1>>;

const F127_ZERO: F127 = MontFp!("0");
const F127_ONE: F127 = MontFp!("1");
Expand Down
2 changes: 1 addition & 1 deletion ec/src/hashing/map_to_curve_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
fn new(domain: &[u8]) -> Result<Self, HashToCurveError> {
#[cfg(test)]
M2C::check_parameters()?;
Ok(MapToCurveBasedHasher {
Ok(Self {
field_hasher: H2F::new(domain),
_phantom: PhantomData,
})
Expand Down
3 changes: 1 addition & 2 deletions ec/src/hashing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions ec/src/models/bls12/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct G1Prepared<P: Bls12Config>(pub G1Affine<P>);

impl<P: Bls12Config> From<G1Affine<P>> for G1Prepared<P> {
fn from(other: G1Affine<P>) -> Self {
G1Prepared(other)
Self(other)
}
}

Expand All @@ -28,7 +28,7 @@ impl<P: Bls12Config> From<G1Projective<P>> for G1Prepared<P> {

impl<'a, P: Bls12Config> From<&'a G1Affine<P>> for G1Prepared<P> {
fn from(other: &'a G1Affine<P>) -> Self {
G1Prepared(*other)
Self(*other)
}
}

Expand All @@ -46,6 +46,6 @@ impl<P: Bls12Config> G1Prepared<P> {

impl<P: Bls12Config> Default for G1Prepared<P> {
fn default() -> Self {
G1Prepared(G1Affine::<P>::generator())
Self(G1Affine::<P>::generator())
}
}
4 changes: 2 additions & 2 deletions ec/src/models/bls12/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<P: Bls12Config> Default for G2Prepared<P> {
impl<P: Bls12Config> From<G2Affine<P>> for G2Prepared<P> {
fn from(q: G2Affine<P>) -> Self {
let two_inv = P::Fp::one().double().inverse().unwrap();
let zero = G2Prepared {
let zero = Self {
ell_coeffs: vec![],
infinity: true,
};
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<'a, P: Bls12Config> From<&'a G2Projective<P>> for G2Prepared<P> {
}

impl<P: Bls12Config> G2Prepared<P> {
pub fn is_zero(&self) -> bool {
pub const fn is_zero(&self) -> bool {
self.infinity
}
}
Expand Down
8 changes: 4 additions & 4 deletions ec/src/models/bn/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct G1Prepared<P: BnConfig>(pub G1Affine<P>);

impl<P: BnConfig> From<G1Affine<P>> for G1Prepared<P> {
fn from(other: G1Affine<P>) -> Self {
G1Prepared(other)
Self(other)
}
}

Expand All @@ -28,7 +28,7 @@ impl<P: BnConfig> From<G1Projective<P>> for G1Prepared<P> {

impl<'a, P: BnConfig> From<&'a G1Affine<P>> for G1Prepared<P> {
fn from(other: &'a G1Affine<P>) -> Self {
G1Prepared(*other)
Self(*other)
}
}

Expand All @@ -39,13 +39,13 @@ impl<'a, P: BnConfig> From<&'a G1Projective<P>> for G1Prepared<P> {
}

impl<P: BnConfig> G1Prepared<P> {
pub fn is_zero(&self) -> bool {
pub const fn is_zero(&self) -> bool {
self.0.infinity
}
}

impl<P: BnConfig> Default for G1Prepared<P> {
fn default() -> Self {
G1Prepared(G1Affine::<P>::generator())
Self(G1Affine::<P>::generator())
}
}
4 changes: 2 additions & 2 deletions ec/src/models/bn/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<P: BnConfig> Default for G2Prepared<P> {
impl<P: BnConfig> From<G2Affine<P>> for G2Prepared<P> {
fn from(q: G2Affine<P>) -> Self {
if q.infinity {
G2Prepared {
Self {
ell_coeffs: vec![],
infinity: true,
}
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<'a, P: BnConfig> From<&'a G2Projective<P>> for G2Prepared<P> {
}

impl<P: BnConfig> G2Prepared<P> {
pub fn is_zero(&self) -> bool {
pub const fn is_zero(&self) -> bool {
self.infinity
}
}
Expand Down
8 changes: 4 additions & 4 deletions ec/src/models/bw6/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct G1Prepared<P: BW6Config>(pub G1Affine<P>);

impl<P: BW6Config> From<G1Affine<P>> for G1Prepared<P> {
fn from(other: G1Affine<P>) -> Self {
G1Prepared(other)
Self(other)
}
}

Expand All @@ -28,7 +28,7 @@ impl<P: BW6Config> From<G1Projective<P>> for G1Prepared<P> {

impl<'a, P: BW6Config> From<&'a G1Affine<P>> for G1Prepared<P> {
fn from(other: &'a G1Affine<P>) -> Self {
G1Prepared(*other)
Self(*other)
}
}

Expand All @@ -39,13 +39,13 @@ impl<'a, P: BW6Config> From<&'a G1Projective<P>> for G1Prepared<P> {
}

impl<P: BW6Config> G1Prepared<P> {
pub fn is_zero(&self) -> bool {
pub const fn is_zero(&self) -> bool {
self.0.infinity
}
}

impl<P: BW6Config> Default for G1Prepared<P> {
fn default() -> Self {
G1Prepared(G1Affine::<P>::generator())
Self(G1Affine::<P>::generator())
}
}
4 changes: 2 additions & 2 deletions ec/src/models/bw6/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<P: BW6Config> From<G2HomProjective<P>> for G2Affine<P> {
let z_inv = q.z.inverse().unwrap();
let x = q.x * &z_inv;
let y = q.y * &z_inv;
G2Affine::<P>::new_unchecked(x, y)
Self::new_unchecked(x, y)
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ impl<P: BW6Config> From<G2Projective<P>> for G2Prepared<P> {
}

impl<P: BW6Config> G2Prepared<P> {
pub fn is_zero(&self) -> bool {
pub const fn is_zero(&self) -> bool {
self.infinity
}
}
Expand Down
13 changes: 5 additions & 8 deletions ec/src/models/bw6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,17 @@ impl<P: BW6Config> BW6<P> {
}

fn final_exponentiation_hard_part(f: &Fp6<P::Fp6Config>) -> Fp6<P::Fp6Config> {
// 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
Expand Down Expand Up @@ -296,10 +297,6 @@ impl<P: BW6Config> BW6<P> {
// 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()
Expand Down
2 changes: 1 addition & 1 deletion ec/src/models/mnt4/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<P: MNT4Config> From<G2Affine<P>> for G2Prepared<P> {
fn from(g: G2Affine<P>) -> 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,
Expand Down
2 changes: 1 addition & 1 deletion ec/src/models/mnt6/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<P: MNT6Config> From<G2Affine<P>> for G2Prepared<P> {
fn from(g: G2Affine<P>) -> 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,
Expand Down
14 changes: 7 additions & 7 deletions ec/src/models/short_weierstrass/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ impl<P: SWCurveConfig> Affine<P> {

/// 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
}
}

Expand Down Expand Up @@ -330,12 +330,12 @@ impl<P: SWCurveConfig, T: Borrow<P::ScalarField>> Mul<T> for Affine<P> {
// coordinates as X/Z^2, Y/Z^3.
impl<P: SWCurveConfig> From<Projective<P>> for Affine<P> {
#[inline]
fn from(p: Projective<P>) -> Affine<P> {
fn from(p: Projective<P>) -> 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();
Expand All @@ -347,7 +347,7 @@ impl<P: SWCurveConfig> From<Projective<P>> for Affine<P> {
// Y/Z^3
let y = p.y * &(zinv_squared * &zinv);

Affine::new_unchecked(x, y)
Self::new_unchecked(x, y)
}
}
}
Expand Down
Loading

0 comments on commit 621be87

Please sign in to comment.