Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods from_affine & double to ProjectivePoint #32

Merged
merged 7 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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]
Expand Down
43 changes: 43 additions & 0 deletions crates/starknet-types-core/src/curve/projective_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -33,6 +34,13 @@ impl ProjectivePoint {
Ok(AffinePoint(self.0.to_affine()))
}

pub fn from_affine(x: Felt, y: Felt) -> Result<Self, CurveError> {
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())
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
);
}
}