@@ -3,6 +3,7 @@ use crate::curve::curve_errors::CurveError;
3
3
use crate :: felt:: Felt ;
4
4
use core:: ops;
5
5
use lambdaworks_math:: cyclic_group:: IsGroup ;
6
+ use lambdaworks_math:: elliptic_curve:: point:: ProjectivePoint as LambdaworksProjectivePoint ;
6
7
use lambdaworks_math:: elliptic_curve:: short_weierstrass:: curves:: stark_curve:: StarkCurve ;
7
8
use lambdaworks_math:: elliptic_curve:: short_weierstrass:: point:: ShortWeierstrassProjectivePoint ;
8
9
use lambdaworks_math:: elliptic_curve:: traits:: EllipticCurveError :: InvalidPoint ;
@@ -15,8 +16,23 @@ use lambdaworks_math::unsigned_integer::traits::IsUnsignedInteger;
15
16
pub struct ProjectivePoint ( pub ( crate ) ShortWeierstrassProjectivePoint < StarkCurve > ) ;
16
17
17
18
impl 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
+ ) )
20
36
}
21
37
22
38
/// The point at infinity.
@@ -49,11 +65,9 @@ impl ProjectivePoint {
49
65
/// This method should be used with caution, as it does not validate whether the provided coordinates
50
66
/// correspond to a valid point on the curve.
51
67
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
+ ) )
57
71
}
58
72
59
73
/// Returns the `x` coordinate of the point.
@@ -201,7 +215,7 @@ mod test {
201
215
202
216
#[ test]
203
217
fn try_from_affine ( ) {
204
- let projective_point = ProjectivePoint :: new (
218
+ let projective_point = ProjectivePoint :: new_unchecked (
205
219
Felt :: from_dec_str (
206
220
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
207
221
)
@@ -265,15 +279,15 @@ mod test {
265
279
assert ! ( identity. is_identity( ) ) ;
266
280
assert_eq ! (
267
281
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 ) )
269
283
) ;
270
284
271
285
assert_eq ! (
272
286
identity. to_affine( ) ,
273
287
Err ( CurveError :: EllipticCurveError ( InvalidPoint ) )
274
288
) ;
275
289
276
- let a = ProjectivePoint :: new (
290
+ let a = ProjectivePoint :: new_unchecked (
277
291
Felt :: from_dec_str (
278
292
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
279
293
)
@@ -290,7 +304,7 @@ mod test {
290
304
#[ test]
291
305
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
292
306
fn add_operations ( ) {
293
- let projective_point_1 = ProjectivePoint :: new (
307
+ let projective_point_1 = ProjectivePoint :: new_unchecked (
294
308
Felt :: from_dec_str (
295
309
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
296
310
)
@@ -325,7 +339,7 @@ mod test {
325
339
#[ test]
326
340
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
327
341
fn add_operations_with_affine ( ) {
328
- let projective_point = ProjectivePoint :: new (
342
+ let projective_point = ProjectivePoint :: new_unchecked (
329
343
Felt :: from_dec_str (
330
344
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
331
345
)
@@ -358,7 +372,7 @@ mod test {
358
372
#[ test]
359
373
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
360
374
fn add_operations_with_affine_no_pointer ( ) {
361
- let projective_point = ProjectivePoint :: new (
375
+ let projective_point = ProjectivePoint :: new_unchecked (
362
376
Felt :: from_dec_str (
363
377
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
364
378
)
@@ -391,7 +405,7 @@ mod test {
391
405
#[ test]
392
406
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
393
407
fn add_assign_operations ( ) {
394
- let mut projective_point_1 = ProjectivePoint :: new (
408
+ let mut projective_point_1 = ProjectivePoint :: new_unchecked (
395
409
Felt :: from_dec_str (
396
410
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
397
411
)
@@ -427,7 +441,7 @@ mod test {
427
441
#[ test]
428
442
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
429
443
fn add_assign_operations_with_affine ( ) {
430
- let mut projective_point = ProjectivePoint :: new (
444
+ let mut projective_point = ProjectivePoint :: new_unchecked (
431
445
Felt :: from_dec_str (
432
446
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
433
447
)
@@ -463,7 +477,7 @@ mod test {
463
477
#[ test]
464
478
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
465
479
fn add_assign_operations_with_affine_no_pointer ( ) {
466
- let mut projective_point = ProjectivePoint :: new (
480
+ let mut projective_point = ProjectivePoint :: new_unchecked (
467
481
Felt :: from_dec_str (
468
482
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
469
483
)
@@ -507,7 +521,7 @@ mod test {
507
521
identity
508
522
) ;
509
523
510
- let projective_point_1 = ProjectivePoint :: new (
524
+ let projective_point_1 = ProjectivePoint :: new_unchecked (
511
525
Felt :: from_dec_str (
512
526
"685118385380464480289795596422487144864558069280897344382334516257395969277" ,
513
527
)
@@ -539,7 +553,7 @@ mod test {
539
553
540
554
#[ test]
541
555
fn mul_by_scalar_operations_with_felt ( ) {
542
- let a = ProjectivePoint :: new (
556
+ let a = ProjectivePoint :: new_unchecked (
543
557
Felt :: from_dec_str (
544
558
"685118385380464480289795596422487144864558069280897344382334516257395969277" ,
545
559
)
@@ -564,7 +578,7 @@ mod test {
564
578
#[ test]
565
579
// Results checked against starknet-rs https://github.com/xJonathanLEI/starknet-rs/
566
580
fn double_operations ( ) {
567
- let projective_point = ProjectivePoint :: new (
581
+ let projective_point = ProjectivePoint :: new_unchecked (
568
582
Felt :: from_dec_str (
569
583
"874739451078007766457464989774322083649278607533249481151382481072868806602" ,
570
584
)
@@ -594,7 +608,7 @@ mod test {
594
608
595
609
#[ test]
596
610
fn neg_operations ( ) {
597
- let a = ProjectivePoint :: new (
611
+ let a = ProjectivePoint :: new_unchecked (
598
612
Felt :: from_dec_str (
599
613
"685118385380464480289795596422487144864558069280897344382334516257395969277" ,
600
614
)
@@ -606,7 +620,7 @@ mod test {
606
620
Felt :: from ( 1 ) ,
607
621
) ;
608
622
609
- let b = ProjectivePoint :: new (
623
+ let b = ProjectivePoint :: new_unchecked (
610
624
Felt :: from_dec_str (
611
625
"685118385380464480289795596422487144864558069280897344382334516257395969277" ,
612
626
)
@@ -623,7 +637,7 @@ mod test {
623
637
624
638
#[ test]
625
639
fn sub_operations_pointers ( ) {
626
- let mut a = ProjectivePoint :: new (
640
+ let mut a = ProjectivePoint :: new_unchecked (
627
641
Felt :: from_dec_str (
628
642
"685118385380464480289795596422487144864558069280897344382334516257395969277" ,
629
643
)
@@ -645,7 +659,7 @@ mod test {
645
659
646
660
#[ test]
647
661
fn sub_operations ( ) {
648
- let mut a = ProjectivePoint :: new (
662
+ let mut a = ProjectivePoint :: new_unchecked (
649
663
Felt :: from_dec_str (
650
664
"685118385380464480289795596422487144864558069280897344382334516257395969277" ,
651
665
)
0 commit comments