Skip to content

Commit

Permalink
Add CurveError
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed Oct 25, 2023
1 parent b54dbdb commit 3239971
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions crates/starknet-types-core/src/curve/affine_point.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::curve::curve_errors::CurveError;
use crate::felt::Felt;
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::elliptic_curve::traits::FromAffine;

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

impl AffinePoint {
pub fn new(x: Felt, y: Felt) -> Result<AffinePoint, EllipticCurveError> {
pub fn new(x: Felt, y: Felt) -> Result<AffinePoint, CurveError> {
Ok(Self(ShortWeierstrassProjectivePoint::from_affine(
x.0, y.0,
)?))
Expand Down
13 changes: 13 additions & 0 deletions crates/starknet-types-core/src/curve/curve_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::fmt::Debug;
use lambdaworks_math::elliptic_curve::traits::EllipticCurveError;

#[derive(Debug, PartialEq, Eq)]
pub enum CurveError {
EllipticCurveError(EllipticCurveError),
}

impl From<EllipticCurveError> for CurveError {
fn from(error: EllipticCurveError) -> CurveError {
CurveError::EllipticCurveError(error)
}
}
1 change: 1 addition & 0 deletions crates/starknet-types-core/src/curve/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod affine_point;
pub mod curve_errors;
pub mod projective_point;
15 changes: 9 additions & 6 deletions crates/starknet-types-core/src/curve/projective_point.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::curve::affine_point::AffinePoint;
use crate::curve::curve_errors::CurveError;
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::InvalidPoint;
use lambdaworks_math::unsigned_integer::traits::IsUnsignedInteger;

use crate::curve::affine_point::AffinePoint;
use lambdaworks_math::elliptic_curve::traits::EllipticCurveError;

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

Expand All @@ -23,9 +23,9 @@ impl ProjectivePoint {
/// Creates the same point in affine coordinates. That is,
/// returns [x / z: y / z: 1] where `self` is [x: y: z].
/// Panics if `self` is the point at infinity.
pub fn to_affine(&self) -> Result<AffinePoint, EllipticCurveError> {
pub fn to_affine(&self) -> Result<AffinePoint, CurveError> {
if self.z() == Felt::ZERO {
return Err(EllipticCurveError::InvalidPoint);
return Err(CurveError::EllipticCurveError(InvalidPoint));
}
Ok(AffinePoint(self.0.to_affine()))
}
Expand Down Expand Up @@ -92,7 +92,10 @@ mod test {
ProjectivePoint::new(Felt::from(0), Felt::from(1), Felt::from(0))
);

assert_eq!(identity.to_affine(), Err(EllipticCurveError::InvalidPoint));
assert_eq!(
identity.to_affine(),
Err(CurveError::EllipticCurveError(InvalidPoint))
);
}

#[test]
Expand Down

0 comments on commit 3239971

Please sign in to comment.