Skip to content

Commit

Permalink
fix PartialEq impl for AffinePoint and ProjectivePoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Feb 17, 2024
1 parent 6ebaa9b commit 2251813
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions starknet-curve/src/ec_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,38 @@ 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,
pub z: FieldElement,
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<Self> {
let y_squared = x * x * x + ALPHA * x + BETA;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2251813

Please sign in to comment.