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
+ } ;
7
11
8
12
/// Represents a point on the Stark elliptic curve.
9
13
/// Doc: https://docs.starkware.co/starkex/crypto/stark-curve.html
@@ -47,6 +51,11 @@ impl AffinePoint {
47
51
pub fn y ( & self ) -> Felt {
48
52
Felt ( * self . 0 . y ( ) )
49
53
}
54
+
55
+ // Returns the generator point of the StarkCurve
56
+ pub fn generator ( ) -> Self {
57
+ AffinePoint ( StarkCurve :: generator ( ) )
58
+ }
50
59
}
51
60
52
61
impl core:: ops:: Neg for & AffinePoint {
@@ -57,6 +66,23 @@ impl core::ops::Neg for &AffinePoint {
57
66
}
58
67
}
59
68
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
+
60
86
#[ cfg( test) ]
61
87
mod test {
62
88
use super :: * ;
@@ -117,4 +143,52 @@ mod test {
117
143
) ;
118
144
assert_eq ! ( -& AffinePoint :: identity( ) , AffinePoint :: identity( ) ) ;
119
145
}
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
+ }
120
194
}
0 commit comments