@@ -3,6 +3,7 @@ use crate::curve::curve_errors::CurveError;
33use crate :: felt:: Felt ;
44use core:: ops;
55use lambdaworks_math:: cyclic_group:: IsGroup ;
6+ use lambdaworks_math:: elliptic_curve:: point:: ProjectivePoint as LambdaworksProjectivePoint ;
67use lambdaworks_math:: elliptic_curve:: short_weierstrass:: curves:: stark_curve:: StarkCurve ;
78use lambdaworks_math:: elliptic_curve:: short_weierstrass:: point:: ShortWeierstrassProjectivePoint ;
89use lambdaworks_math:: elliptic_curve:: traits:: EllipticCurveError :: InvalidPoint ;
@@ -15,8 +16,23 @@ use lambdaworks_math::unsigned_integer::traits::IsUnsignedInteger;
1516pub struct ProjectivePoint ( pub ( crate ) ShortWeierstrassProjectivePoint < StarkCurve > ) ;
1617
1718impl ProjectivePoint {
18- pub fn new ( x : Felt , y : Felt , z : Felt ) -> ProjectivePoint {
19- Self ( ShortWeierstrassProjectivePoint :: new ( [ x. 0 , y. 0 , z. 0 ] ) )
19+ pub fn new ( x : Felt , y : Felt , z : Felt ) -> Result < ProjectivePoint , CurveError > {
20+ Ok ( Self ( ShortWeierstrassProjectivePoint :: new ( [ x. 0 , y. 0 , z. 0 ] ) ?) )
21+ }
22+
23+ /// Creates a new short Weierstrass projective point, assuming the coordinates are valid.
24+ ///
25+ /// The coordinates are valid if the equation `y^2 * z = x^3 + a * x * z^2 + b * z^3`
26+ /// holds, Where `a` and `b` are the short Weierstrass coefficients of the Stark
27+ /// curve. Furthermore, the coordinates in the form `[0, _, 0]` all satisfy the
28+ /// equation, and should be always transformed to the infinity value `[0, 1, 0]`.
29+ ///
30+ /// SAFETY: Failing to guarantee this assumptions could lead to a runtime panic
31+ /// while operating with the value.
32+ pub const fn new_unchecked ( x : Felt , y : Felt , z : Felt ) -> ProjectivePoint {
33+ Self ( ShortWeierstrassProjectivePoint (
34+ LambdaworksProjectivePoint :: new ( [ x. 0 , y. 0 , z. 0 ] ) ,
35+ ) )
2036 }
2137
2238 /// The point at infinity.
@@ -49,11 +65,9 @@ impl ProjectivePoint {
4965 /// This method should be used with caution, as it does not validate whether the provided coordinates
5066 /// correspond to a valid point on the curve.
5167 pub fn from_affine_unchecked ( x : Felt , y : Felt ) -> Self {
52- Self ( ShortWeierstrassProjectivePoint :: new ( [
53- x. 0 ,
54- y. 0 ,
55- Felt :: ONE . 0 ,
56- ] ) )
68+ Self ( ShortWeierstrassProjectivePoint (
69+ LambdaworksProjectivePoint :: new ( [ x. 0 , y. 0 , Felt :: ONE . 0 ] ) ,
70+ ) )
5771 }
5872
5973 /// Returns the `x` coordinate of the point.
@@ -201,7 +215,7 @@ mod test {
201215
202216 #[ test]
203217 fn try_from_affine ( ) {
204- let projective_point = ProjectivePoint :: new (
218+ let projective_point = ProjectivePoint :: new_unchecked (
205219 Felt :: from_dec_str (
206220 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
207221 )
@@ -265,15 +279,15 @@ mod test {
265279 assert ! ( identity. is_identity( ) ) ;
266280 assert_eq ! (
267281 identity,
268- ProjectivePoint :: new ( Felt :: from( 0 ) , Felt :: from( 1 ) , Felt :: from( 0 ) )
282+ ProjectivePoint :: new_unchecked ( Felt :: from( 0 ) , Felt :: from( 1 ) , Felt :: from( 0 ) )
269283 ) ;
270284
271285 assert_eq ! (
272286 identity. to_affine( ) ,
273287 Err ( CurveError :: EllipticCurveError ( InvalidPoint ) )
274288 ) ;
275289
276- let a = ProjectivePoint :: new (
290+ let a = ProjectivePoint :: new_unchecked (
277291 Felt :: from_dec_str (
278292 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
279293 )
@@ -290,7 +304,7 @@ mod test {
290304 #[ test]
291305 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
292306 fn add_operations ( ) {
293- let projective_point_1 = ProjectivePoint :: new (
307+ let projective_point_1 = ProjectivePoint :: new_unchecked (
294308 Felt :: from_dec_str (
295309 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
296310 )
@@ -325,7 +339,7 @@ mod test {
325339 #[ test]
326340 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
327341 fn add_operations_with_affine ( ) {
328- let projective_point = ProjectivePoint :: new (
342+ let projective_point = ProjectivePoint :: new_unchecked (
329343 Felt :: from_dec_str (
330344 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
331345 )
@@ -358,7 +372,7 @@ mod test {
358372 #[ test]
359373 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
360374 fn add_operations_with_affine_no_pointer ( ) {
361- let projective_point = ProjectivePoint :: new (
375+ let projective_point = ProjectivePoint :: new_unchecked (
362376 Felt :: from_dec_str (
363377 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
364378 )
@@ -391,7 +405,7 @@ mod test {
391405 #[ test]
392406 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
393407 fn add_assign_operations ( ) {
394- let mut projective_point_1 = ProjectivePoint :: new (
408+ let mut projective_point_1 = ProjectivePoint :: new_unchecked (
395409 Felt :: from_dec_str (
396410 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
397411 )
@@ -427,7 +441,7 @@ mod test {
427441 #[ test]
428442 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
429443 fn add_assign_operations_with_affine ( ) {
430- let mut projective_point = ProjectivePoint :: new (
444+ let mut projective_point = ProjectivePoint :: new_unchecked (
431445 Felt :: from_dec_str (
432446 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
433447 )
@@ -463,7 +477,7 @@ mod test {
463477 #[ test]
464478 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
465479 fn add_assign_operations_with_affine_no_pointer ( ) {
466- let mut projective_point = ProjectivePoint :: new (
480+ let mut projective_point = ProjectivePoint :: new_unchecked (
467481 Felt :: from_dec_str (
468482 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
469483 )
@@ -507,7 +521,7 @@ mod test {
507521 identity
508522 ) ;
509523
510- let projective_point_1 = ProjectivePoint :: new (
524+ let projective_point_1 = ProjectivePoint :: new_unchecked (
511525 Felt :: from_dec_str (
512526 "685118385380464480289795596422487144864558069280897344382334516257395969277" ,
513527 )
@@ -539,7 +553,7 @@ mod test {
539553
540554 #[ test]
541555 fn mul_by_scalar_operations_with_felt ( ) {
542- let a = ProjectivePoint :: new (
556+ let a = ProjectivePoint :: new_unchecked (
543557 Felt :: from_dec_str (
544558 "685118385380464480289795596422487144864558069280897344382334516257395969277" ,
545559 )
@@ -564,7 +578,7 @@ mod test {
564578 #[ test]
565579 // Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
566580 fn double_operations ( ) {
567- let projective_point = ProjectivePoint :: new (
581+ let projective_point = ProjectivePoint :: new_unchecked (
568582 Felt :: from_dec_str (
569583 "874739451078007766457464989774322083649278607533249481151382481072868806602" ,
570584 )
@@ -594,7 +608,7 @@ mod test {
594608
595609 #[ test]
596610 fn neg_operations ( ) {
597- let a = ProjectivePoint :: new (
611+ let a = ProjectivePoint :: new_unchecked (
598612 Felt :: from_dec_str (
599613 "685118385380464480289795596422487144864558069280897344382334516257395969277" ,
600614 )
@@ -606,7 +620,7 @@ mod test {
606620 Felt :: from ( 1 ) ,
607621 ) ;
608622
609- let b = ProjectivePoint :: new (
623+ let b = ProjectivePoint :: new_unchecked (
610624 Felt :: from_dec_str (
611625 "685118385380464480289795596422487144864558069280897344382334516257395969277" ,
612626 )
@@ -623,7 +637,7 @@ mod test {
623637
624638 #[ test]
625639 fn sub_operations_pointers ( ) {
626- let mut a = ProjectivePoint :: new (
640+ let mut a = ProjectivePoint :: new_unchecked (
627641 Felt :: from_dec_str (
628642 "685118385380464480289795596422487144864558069280897344382334516257395969277" ,
629643 )
@@ -645,7 +659,7 @@ mod test {
645659
646660 #[ test]
647661 fn sub_operations ( ) {
648- let mut a = ProjectivePoint :: new (
662+ let mut a = ProjectivePoint :: new_unchecked (
649663 Felt :: from_dec_str (
650664 "685118385380464480289795596422487144864558069280897344382334516257395969277" ,
651665 )
0 commit comments