Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/starknet-io/types-rs into v…
Browse files Browse the repository at this point in the history
…arun/hash-single
  • Loading branch information
varun-doshi committed Jan 24, 2025
2 parents 4483284 + e0eff28 commit 13addc3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
86 changes: 80 additions & 6 deletions crates/starknet-types-core/src/curve/affine_point.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -57,6 +66,23 @@ impl core::ops::Neg for &AffinePoint {
}
}

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

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

impl core::ops::Mul<Felt> 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::*;
Expand Down Expand Up @@ -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()
);
}
}
11 changes: 3 additions & 8 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,10 @@ impl From<&BigInt> for Felt {

impl From<BigInt> 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())
Expand All @@ -566,7 +561,7 @@ impl From<&BigUint> for Felt {

impl From<BigUint> for Felt {
fn from(biguint: BigUint) -> Felt {
Felt::from_bytes_le_slice(&biguint.to_bytes_le())
Self::from(&biguint)
}
}

Expand Down
11 changes: 11 additions & 0 deletions crates/starknet-types-core/src/hash/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ mod tests {
)
}

#[test]
fn test_pedersen_hash_collision() {
let x =
Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
.unwrap();
assert_eq!(
Pedersen::hash_single(&x),
Pedersen::hash(&x, &Felt::from(0))
)
}

#[test]
fn test_pedersen_hash() {
let x =
Expand Down

0 comments on commit 13addc3

Please sign in to comment.