From fe19c9d243a54115938bff80695a88e0b9755418 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Fri, 24 Jan 2025 12:52:17 +0200 Subject: [PATCH 1/2] chore: code dedup in felt from bigint (#94) --- crates/starknet-types-core/src/felt/mod.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/crates/starknet-types-core/src/felt/mod.rs b/crates/starknet-types-core/src/felt/mod.rs index 11d99b9..3b8e6d8 100644 --- a/crates/starknet-types-core/src/felt/mod.rs +++ b/crates/starknet-types-core/src/felt/mod.rs @@ -549,15 +549,10 @@ impl From<&BigInt> for Felt { impl From for Felt { fn from(bigint: BigInt) -> Felt { - let (sign, bytes) = bigint.to_bytes_le(); - let felt = Felt::from_bytes_le_slice(&bytes); - if sign == Sign::Minus { - felt.neg() - } else { - felt - } + Self::from(&bigint) } } + impl From<&BigUint> for Felt { fn from(biguint: &BigUint) -> Felt { Felt::from_bytes_le_slice(&biguint.to_bytes_le()) @@ -566,7 +561,7 @@ impl From<&BigUint> for Felt { impl From for Felt { fn from(biguint: BigUint) -> Felt { - Felt::from_bytes_le_slice(&biguint.to_bytes_le()) + Self::from(&biguint) } } From e0eff28053b0280629c3bf6827e6246995a27503 Mon Sep 17 00:00:00 2001 From: Filip Laurentiu Date: Fri, 24 Jan 2025 17:32:43 +0200 Subject: [PATCH 2/2] feat: implement multiplication and addition operations for AffinePoint (#114) --- .../src/curve/affine_point.rs | 86 +++++++++++++++++-- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/crates/starknet-types-core/src/curve/affine_point.rs b/crates/starknet-types-core/src/curve/affine_point.rs index 20bedcf..76a73b9 100644 --- a/crates/starknet-types-core/src/curve/affine_point.rs +++ b/crates/starknet-types-core/src/curve/affine_point.rs @@ -1,9 +1,13 @@ -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::FromAffine; +use crate::{curve::curve_errors::CurveError, felt::Felt}; +use lambdaworks_math::{ + cyclic_group::IsGroup, + elliptic_curve::{ + short_weierstrass::{ + curves::stark_curve::StarkCurve, point::ShortWeierstrassProjectivePoint, + }, + traits::{FromAffine, IsEllipticCurve}, + }, +}; /// Represents a point on the Stark elliptic curve. /// Doc: https://docs.starkware.co/starkex/crypto/stark-curve.html @@ -47,6 +51,11 @@ impl AffinePoint { pub fn y(&self) -> Felt { Felt(*self.0.y()) } + + // Returns the generator point of the StarkCurve + pub fn generator() -> Self { + AffinePoint(StarkCurve::generator()) + } } impl core::ops::Neg for &AffinePoint { @@ -57,6 +66,23 @@ impl core::ops::Neg for &AffinePoint { } } +impl core::ops::Add for AffinePoint { + type Output = AffinePoint; + + fn add(self, rhs: Self) -> Self::Output { + AffinePoint(self.0.operate_with_affine(&rhs.0)) + } +} + +impl core::ops::Mul for &AffinePoint { + type Output = AffinePoint; + + // Add the point (`self`) to itself for `scalar` many times + fn mul(self, rhs: Felt) -> AffinePoint { + AffinePoint(self.0.operate_with_self(rhs.0.representative())) + } +} + #[cfg(test)] mod test { use super::*; @@ -117,4 +143,52 @@ mod test { ); assert_eq!(-&AffinePoint::identity(), AffinePoint::identity()); } + + #[test] + fn affine_add() { + let p = AffinePoint::new( + Felt::from_hex_unchecked("0x2d39148a92f479fb077389d"), + Felt::from_hex_unchecked( + "0x6e5d97edf7283fe7a7fe9deef2619224f42cb1bd531dd23380ad066c61ee20b", + ), + ) + .unwrap(); + + assert_eq!( + p.clone() + p, + AffinePoint::new( + Felt::from_hex_unchecked( + "0x23a1c9a32dd397fb1e7f758b9089757c1223057aea1d8b52cbec583ad74eaab", + ), + Felt::from_hex_unchecked( + "0x466880caf4086bac129ae52ee98ddf75b2b394ae7c7ed1a19d9c61aa1f69f62", + ), + ) + .unwrap() + ); + } + + #[test] + fn affine_mul() { + let p = AffinePoint::new( + Felt::from_hex_unchecked("0x2d39148a92f479fb077389d"), + Felt::from_hex_unchecked( + "0x6e5d97edf7283fe7a7fe9deef2619224f42cb1bd531dd23380ad066c61ee20b", + ), + ) + .unwrap(); + + assert_eq!( + &p * Felt::from(2), + AffinePoint::new( + Felt::from_hex_unchecked( + "0x23a1c9a32dd397fb1e7f758b9089757c1223057aea1d8b52cbec583ad74eaab", + ), + Felt::from_hex_unchecked( + "0x466880caf4086bac129ae52ee98ddf75b2b394ae7c7ed1a19d9c61aa1f69f62", + ), + ) + .unwrap() + ); + } }