diff --git a/crates/starknet-types-core/Cargo.toml b/crates/starknet-types-core/Cargo.toml index 01a451e..ecc88f9 100644 --- a/crates/starknet-types-core/Cargo.toml +++ b/crates/starknet-types-core/Cargo.toml @@ -12,7 +12,7 @@ readme = "README.md" [dependencies] bitvec = { version = "1.0.1", default-features = false } -lambdaworks-math = {version = "0.3.0", default-features = false} +lambdaworks-math = { version = "0.4.0", default-features = false} num-traits = { version = "0.2.16", default-features = false } num-bigint = { version = "0.4.4", default-features = false } @@ -24,7 +24,7 @@ lazy_static = { version = "1.4.0", default-features = false, features = [ # Optional arbitrary = { version = "1.3.0", optional = true } serde = { version = "1.0.163", optional = true, default-features = false, features = ["alloc"] } -lambdaworks-crypto = { version = "0.3.0", default-features = false, optional = true } +lambdaworks-crypto = { version = "0.4.0", default-features = false, optional = true } parity-scale-codec = { version = "3.2.2", default-features = false, optional = true } [features] diff --git a/crates/starknet-types-core/src/curve/projective_point.rs b/crates/starknet-types-core/src/curve/projective_point.rs index 5d174fb..694cfec 100644 --- a/crates/starknet-types-core/src/curve/projective_point.rs +++ b/crates/starknet-types-core/src/curve/projective_point.rs @@ -6,6 +6,7 @@ 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::elliptic_curve::traits::FromAffine; use lambdaworks_math::unsigned_integer::traits::IsUnsignedInteger; /// Represents a projective point on the Stark elliptic curve. @@ -33,6 +34,13 @@ impl ProjectivePoint { Ok(AffinePoint(self.0.to_affine())) } + pub fn from_affine(x: Felt, y: Felt) -> Result { + Ok(Self( + ShortWeierstrassProjectivePoint::from_affine(x.0, y.0) + .map_err(CurveError::EllipticCurveError)?, + )) + } + /// Returns the `x` coordinate of the point. pub fn x(&self) -> Felt { Felt(*self.0.x()) @@ -47,6 +55,10 @@ impl ProjectivePoint { pub fn z(&self) -> Felt { Felt(*self.0.z()) } + + pub fn double(&self) -> Self { + Self(self.0.double()) + } } impl ops::Add<&ProjectivePoint> for &ProjectivePoint { @@ -212,4 +224,35 @@ mod test { .unwrap() ) } + + #[test] + // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/ + fn double_operations() { + let projective_point = ProjectivePoint::new( + Felt::from_dec_str( + "874739451078007766457464989774322083649278607533249481151382481072868806602", + ) + .unwrap(), + Felt::from_dec_str( + "152666792071518830868575557812948353041420400780739481342941381225525861407", + ) + .unwrap(), + Felt::from(1), + ); + + assert_eq!( + projective_point.double().to_affine().unwrap(), + AffinePoint::new( + Felt::from_dec_str( + "3324833730090626974525872402899302150520188025637965566623476530814354734325", + ) + .unwrap(), + Felt::from_dec_str( + "3147007486456030910661996439995670279305852583596209647900952752170983517249", + ) + .unwrap() + ) + .unwrap() + ); + } }