Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 13addc3

Browse files
committedJan 24, 2025·
Merge branch 'main' of https://github.com/starknet-io/types-rs into varun/hash-single
2 parents 4483284 + e0eff28 commit 13addc3

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed
 

‎crates/starknet-types-core/src/curve/affine_point.rs

+80-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
use crate::curve::curve_errors::CurveError;
2-
use crate::felt::Felt;
3-
use lambdaworks_math::cyclic_group::IsGroup;
4-
use lambdaworks_math::elliptic_curve::short_weierstrass::curves::stark_curve::StarkCurve;
5-
use lambdaworks_math::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint;
6-
use lambdaworks_math::elliptic_curve::traits::FromAffine;
1+
use crate::{curve::curve_errors::CurveError, felt::Felt};
2+
use lambdaworks_math::{
3+
cyclic_group::IsGroup,
4+
elliptic_curve::{
5+
short_weierstrass::{
6+
curves::stark_curve::StarkCurve, point::ShortWeierstrassProjectivePoint,
7+
},
8+
traits::{FromAffine, IsEllipticCurve},
9+
},
10+
};
711

812
/// Represents a point on the Stark elliptic curve.
913
/// Doc: https://docs.starkware.co/starkex/crypto/stark-curve.html
@@ -47,6 +51,11 @@ impl AffinePoint {
4751
pub fn y(&self) -> Felt {
4852
Felt(*self.0.y())
4953
}
54+
55+
// Returns the generator point of the StarkCurve
56+
pub fn generator() -> Self {
57+
AffinePoint(StarkCurve::generator())
58+
}
5059
}
5160

5261
impl core::ops::Neg for &AffinePoint {
@@ -57,6 +66,23 @@ impl core::ops::Neg for &AffinePoint {
5766
}
5867
}
5968

69+
impl core::ops::Add<AffinePoint> for AffinePoint {
70+
type Output = AffinePoint;
71+
72+
fn add(self, rhs: Self) -> Self::Output {
73+
AffinePoint(self.0.operate_with_affine(&rhs.0))
74+
}
75+
}
76+
77+
impl core::ops::Mul<Felt> for &AffinePoint {
78+
type Output = AffinePoint;
79+
80+
// Add the point (`self`) to itself for `scalar` many times
81+
fn mul(self, rhs: Felt) -> AffinePoint {
82+
AffinePoint(self.0.operate_with_self(rhs.0.representative()))
83+
}
84+
}
85+
6086
#[cfg(test)]
6187
mod test {
6288
use super::*;
@@ -117,4 +143,52 @@ mod test {
117143
);
118144
assert_eq!(-&AffinePoint::identity(), AffinePoint::identity());
119145
}
146+
147+
#[test]
148+
fn affine_add() {
149+
let p = AffinePoint::new(
150+
Felt::from_hex_unchecked("0x2d39148a92f479fb077389d"),
151+
Felt::from_hex_unchecked(
152+
"0x6e5d97edf7283fe7a7fe9deef2619224f42cb1bd531dd23380ad066c61ee20b",
153+
),
154+
)
155+
.unwrap();
156+
157+
assert_eq!(
158+
p.clone() + p,
159+
AffinePoint::new(
160+
Felt::from_hex_unchecked(
161+
"0x23a1c9a32dd397fb1e7f758b9089757c1223057aea1d8b52cbec583ad74eaab",
162+
),
163+
Felt::from_hex_unchecked(
164+
"0x466880caf4086bac129ae52ee98ddf75b2b394ae7c7ed1a19d9c61aa1f69f62",
165+
),
166+
)
167+
.unwrap()
168+
);
169+
}
170+
171+
#[test]
172+
fn affine_mul() {
173+
let p = AffinePoint::new(
174+
Felt::from_hex_unchecked("0x2d39148a92f479fb077389d"),
175+
Felt::from_hex_unchecked(
176+
"0x6e5d97edf7283fe7a7fe9deef2619224f42cb1bd531dd23380ad066c61ee20b",
177+
),
178+
)
179+
.unwrap();
180+
181+
assert_eq!(
182+
&p * Felt::from(2),
183+
AffinePoint::new(
184+
Felt::from_hex_unchecked(
185+
"0x23a1c9a32dd397fb1e7f758b9089757c1223057aea1d8b52cbec583ad74eaab",
186+
),
187+
Felt::from_hex_unchecked(
188+
"0x466880caf4086bac129ae52ee98ddf75b2b394ae7c7ed1a19d9c61aa1f69f62",
189+
),
190+
)
191+
.unwrap()
192+
);
193+
}
120194
}

‎crates/starknet-types-core/src/felt/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,10 @@ impl From<&BigInt> for Felt {
549549

550550
impl From<BigInt> for Felt {
551551
fn from(bigint: BigInt) -> Felt {
552-
let (sign, bytes) = bigint.to_bytes_le();
553-
let felt = Felt::from_bytes_le_slice(&bytes);
554-
if sign == Sign::Minus {
555-
felt.neg()
556-
} else {
557-
felt
558-
}
552+
Self::from(&bigint)
559553
}
560554
}
555+
561556
impl From<&BigUint> for Felt {
562557
fn from(biguint: &BigUint) -> Felt {
563558
Felt::from_bytes_le_slice(&biguint.to_bytes_le())
@@ -566,7 +561,7 @@ impl From<&BigUint> for Felt {
566561

567562
impl From<BigUint> for Felt {
568563
fn from(biguint: BigUint) -> Felt {
569-
Felt::from_bytes_le_slice(&biguint.to_bytes_le())
564+
Self::from(&biguint)
570565
}
571566
}
572567

‎crates/starknet-types-core/src/hash/pedersen.rs

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ mod tests {
5555
)
5656
}
5757

58+
#[test]
59+
fn test_pedersen_hash_collision() {
60+
let x =
61+
Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
62+
.unwrap();
63+
assert_eq!(
64+
Pedersen::hash_single(&x),
65+
Pedersen::hash(&x, &Felt::from(0))
66+
)
67+
}
68+
5869
#[test]
5970
fn test_pedersen_hash() {
6071
let x =

0 commit comments

Comments
 (0)
Please sign in to comment.