Skip to content

Commit

Permalink
Divide the curve into two mods projective_point affine_point
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed Oct 23, 2023
1 parent fd14bf5 commit ee74daa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 45 deletions.
73 changes: 73 additions & 0 deletions crates/starknet-types-core/src/curve/affine_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::curve::projective_point::ProjectivePoint;
use crate::felt::Felt;
use core::ops;
use lambdaworks_math::cyclic_group::IsGroup;
use lambdaworks_math::elliptic_curve::short_weierstrass::curves::stark_curve::StarkCurve;
use lambdaworks_math::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint;
use lambdaworks_math::elliptic_curve::traits::{EllipticCurveError, FromAffine};
use lambdaworks_math::field::element::FieldElement;
use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField;
use lambdaworks_math::unsigned_integer::traits::IsUnsignedInteger;

pub struct AffinePoint(pub(crate) ShortWeierstrassProjectivePoint<StarkCurve>);

impl AffinePoint {
pub fn new(x: Felt, y: Felt) -> Result<AffinePoint, EllipticCurveError> {
Ok(Self(ShortWeierstrassProjectivePoint::from_affine(
x.0, y.0,
)?))
}

pub fn identity() -> AffinePoint {
Self(ShortWeierstrassProjectivePoint::neutral_element())
}

/// Returns the `x` coordinate of the point.
pub fn x(&self) -> &FieldElement<Stark252PrimeField> {
self.0.x()
}

/// Returns the `y` coordinate of the point.
pub fn y(&self) -> &FieldElement<Stark252PrimeField> {
self.0.y()
}
}

impl ops::Add<&AffinePoint> for &AffinePoint {
type Output = AffinePoint;

fn add(self, rhs: &AffinePoint) -> AffinePoint {
AffinePoint(self.0.operate_with(&rhs.0))
}
}

impl ops::AddAssign<&AffinePoint> for AffinePoint {
fn add_assign(&mut self, rhs: &AffinePoint) {
self.0 = self.0.operate_with(&rhs.0);
}
}

impl ops::Mul<&Felt> for &AffinePoint {
type Output = AffinePoint;

fn mul(self, rhs: &Felt) -> AffinePoint {
AffinePoint(self.0.operate_with_self(rhs.0.representative()))
}
}

impl<T> ops::Mul<T> for &AffinePoint
where
T: IsUnsignedInteger, // Asumiendo que "exponent" es una trait
{
type Output = AffinePoint;

fn mul(self, rhs: T) -> AffinePoint {
AffinePoint(self.0.operate_with_self(rhs))
}
}

impl From<AffinePoint> for ProjectivePoint {
fn from(affine_point: AffinePoint) -> ProjectivePoint {
ProjectivePoint(affine_point.0)
}
}
2 changes: 2 additions & 0 deletions crates/starknet-types-core/src/curve/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod affine_point;
pub mod projective_point;
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use lambdaworks_math::elliptic_curve::short_weierstrass::point::ShortWeierstrass
use lambdaworks_math::elliptic_curve::traits::{EllipticCurveError, FromAffine};
use lambdaworks_math::field::element::FieldElement;
use lambdaworks_math::field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectivePoint(ShortWeierstrassProjectivePoint<StarkCurve>);
use lambdaworks_math::unsigned_integer::traits::IsUnsignedInteger;

use crate::curve::affine_point::AffinePoint;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AffinePoint(ShortWeierstrassProjectivePoint<StarkCurve>);
pub struct ProjectivePoint(pub(crate) ShortWeierstrassProjectivePoint<StarkCurve>);

impl ProjectivePoint {
pub fn new(x: Felt, y: Felt, z: Felt) -> ProjectivePoint {
Expand Down Expand Up @@ -60,52 +61,21 @@ impl ops::AddAssign<&ProjectivePoint> for ProjectivePoint {
}
}

impl AffinePoint {
pub fn new(x: Felt, y: Felt) -> Result<AffinePoint, EllipticCurveError> {
Ok(Self(ShortWeierstrassProjectivePoint::from_affine(
x.0, y.0,
)?))
}

pub fn identity() -> AffinePoint {
Self(ShortWeierstrassProjectivePoint::neutral_element())
}

/// Returns the `x` coordinate of the point.
pub fn x(&self) -> &FieldElement<Stark252PrimeField> {
self.0.x()
}

/// Returns the `y` coordinate of the point.
pub fn y(&self) -> &FieldElement<Stark252PrimeField> {
self.0.y()
}
}

impl ops::Add<&AffinePoint> for &AffinePoint {
type Output = AffinePoint;

fn add(self, rhs: &AffinePoint) -> AffinePoint {
AffinePoint(self.0.operate_with(&rhs.0))
}
}
impl ops::Mul<&Felt> for &ProjectivePoint {
type Output = ProjectivePoint;

impl ops::AddAssign<&AffinePoint> for AffinePoint {
fn add_assign(&mut self, rhs: &AffinePoint) {
self.0 = self.0.operate_with(&rhs.0);
fn mul(self, rhs: &Felt) -> ProjectivePoint {
ProjectivePoint(self.0.operate_with_self(rhs.0.representative()))
}
}

impl ops::Mul<&Felt> for &AffinePoint {
type Output = AffinePoint;

fn mul(self, rhs: &Felt) -> AffinePoint {
AffinePoint(self.0.operate_with_self(rhs.0.representative()))
}
}
impl<T> ops::Mul<T> for &ProjectivePoint
where
T: IsUnsignedInteger, // Asumiendo que "exponent" es una trait
{
type Output = ProjectivePoint;

impl From<AffinePoint> for ProjectivePoint {
fn from(affine_point: AffinePoint) -> ProjectivePoint {
ProjectivePoint(affine_point.0)
fn mul(self, rhs: T) -> ProjectivePoint {
ProjectivePoint(self.0.operate_with_self(rhs))
}
}

0 comments on commit ee74daa

Please sign in to comment.