diff --git a/starknet-curve/src/ec_point.rs b/starknet-curve/src/ec_point.rs index 43ff8439..fa3e2829 100644 --- a/starknet-curve/src/ec_point.rs +++ b/starknet-curve/src/ec_point.rs @@ -5,14 +5,14 @@ use crate::curve_params::{ALPHA, BETA}; use core::ops; /// A point on an elliptic curve over [FieldElement]. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug)] pub struct AffinePoint { pub x: FieldElement, pub y: FieldElement, pub infinity: bool, } -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug)] pub struct ProjectivePoint { pub x: FieldElement, pub y: FieldElement, @@ -20,6 +20,23 @@ pub struct ProjectivePoint { pub infinity: bool, } +impl PartialEq for AffinePoint { + fn eq(&self, other: &Self) -> bool { + if self.infinity && other.infinity { + // Two infinity points are considered equal + true + } else if self.infinity || other.infinity { + // An infinity point is not equal to a non-infinity point + false + } else { + // Compare the x and y coordinates + self.x == other.x && self.y == other.y + } + } +} + +impl Eq for AffinePoint {} + impl AffinePoint { pub fn from_x(x: FieldElement) -> Option { let y_squared = x * x * x + ALPHA * x + BETA; @@ -141,6 +158,27 @@ impl ops::Mul<&[bool]> for &AffinePoint { } } +impl PartialEq for ProjectivePoint { + fn eq(&self, other: &Self) -> bool { + if self.infinity && other.infinity { + // Two infinity points are considered equal + true + } else if self.infinity || other.infinity { + // An infinity point is not equal to a non-infinity point + false + } else { + // Calculate the inverse of z-coordinates + let z1inv = self.z.invert().unwrap(); + let z2inv = other.z.invert().unwrap(); + + // Compare the affine coordinates after applying the inverse of z-coordinates + self.x * z1inv == other.x * z2inv && self.y * z1inv == other.y * z2inv + } + } +} + +impl Eq for ProjectivePoint {} + impl ProjectivePoint { pub const fn from_affine_point(p: &AffinePoint) -> Self { Self {