@@ -17,13 +17,12 @@ use crate::traits::AsBytes;
1717use alloc:: vec:: Vec ;
1818
1919#[ derive( Clone , Debug ) ]
20- pub struct ShortWeierstrassProjectivePoint < E : IsEllipticCurve > ( pub ProjectivePoint < E > ) ;
20+ pub struct ShortWeierstrassProjectivePoint < E : IsEllipticCurve > ( ProjectivePoint < E > ) ;
2121
2222impl < E : IsShortWeierstrass > ShortWeierstrassProjectivePoint < E > {
2323 /// Creates an elliptic curve point giving the projective [x: y: z] coordinates.
2424 pub fn new ( value : [ FieldElement < E :: BaseField > ; 3 ] ) -> Result < Self , EllipticCurveError > {
2525 let ( x, y, z) = ( & value[ 0 ] , & value[ 1 ] , & value[ 2 ] ) ;
26-
2726 if z != & FieldElement :: < E :: BaseField > :: zero ( )
2827 && E :: defining_equation_projective ( x, y, z) == FieldElement :: < E :: BaseField > :: zero ( )
2928 {
@@ -43,6 +42,22 @@ impl<E: IsShortWeierstrass> ShortWeierstrassProjectivePoint<E> {
4342 }
4443 }
4544
45+ /// Creates an elliptic curve point giving the projective [x: y: z] coordinates without
46+ /// checking that the point satisfies the curve equation.
47+ pub const fn new_unchecked ( value : [ FieldElement < E :: BaseField > ; 3 ] ) -> Self {
48+ // SAFETY: The caller MUST ensure that [x:y:z] represents valid point on the
49+ // curve. Passing arbitrary coordinates here can violate the invariant
50+ // and produce silently incorrect results in subsequent operations.
51+ Self ( ProjectivePoint :: new ( value) )
52+ }
53+
54+ /// Changes the point coordinates without checking that it satisfies the curve equation.
55+ pub fn set_unchecked ( & mut self , value : [ FieldElement < E :: BaseField > ; 3 ] ) {
56+ // SAFETY: The caller MUST ensure that the provided coordinates represent a valid curve
57+ // point. Setting invalid coordinates may lead to silently incorrect computations later on.
58+ self . 0 . value = value
59+ }
60+
4661 /// Returns the `x` coordinate of the point.
4762 pub fn x ( & self ) -> & FieldElement < E :: BaseField > {
4863 self . 0 . x ( )
@@ -111,8 +126,7 @@ impl<E: IsShortWeierstrass> ShortWeierstrassProjectivePoint<E> {
111126 ) ;
112127 // SAFETY: The values `x_p, y_p, z_p` are computed correctly to be on the curve.
113128 // The assertion above verifies that the resulting point is valid.
114- let point = Self :: new ( [ xp, yp, zp] ) ;
115- point. unwrap ( )
129+ Self :: new_unchecked ( [ xp, yp, zp] )
116130 }
117131 // https://hyperelliptic.org/EFD/g1p/data/shortw/projective/addition/madd-1998-cmo
118132 /// More efficient than operate_with, but must ensure that other is in affine form
@@ -132,12 +146,11 @@ impl<E: IsShortWeierstrass> ShortWeierstrassProjectivePoint<E> {
132146 if u == * py {
133147 if v != * px || * py == FieldElement :: zero ( ) {
134148 // SAFETY: The point (0, 1, 0) is defined as the point at infinity.
135- return Self :: new ( [
149+ return Self :: new_unchecked ( [
136150 FieldElement :: zero ( ) ,
137151 FieldElement :: one ( ) ,
138152 FieldElement :: zero ( ) ,
139- ] )
140- . unwrap ( ) ;
153+ ] ) ;
141154 } else {
142155 return self . double ( ) ;
143156 }
@@ -161,8 +174,7 @@ impl<E: IsShortWeierstrass> ShortWeierstrassProjectivePoint<E> {
161174 ) ;
162175 // SAFETY: The values `x, y, z` are computed correctly to be on the curve.
163176 // The assertion above verifies that the resulting point is valid.
164- let point = Self :: new ( [ x, y, z] ) ;
165- point. unwrap ( )
177+ Self :: new_unchecked ( [ x, y, z] )
166178 }
167179}
168180
@@ -189,13 +201,11 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassProjectivePoint<E> {
189201 fn neutral_element ( ) -> Self {
190202 // SAFETY:
191203 // - `(0, 1, 0)` is **mathematically valid** as the neutral element.
192- // - `unwrap()` is safe because this is **a known valid point**.
193- Self :: new ( [
204+ Self :: new_unchecked ( [
194205 FieldElement :: zero ( ) ,
195206 FieldElement :: one ( ) ,
196207 FieldElement :: zero ( ) ,
197208 ] )
198- . unwrap ( )
199209 }
200210
201211 fn is_neutral_element ( & self ) -> bool {
@@ -245,7 +255,7 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassProjectivePoint<E> {
245255 ) ;
246256 // SAFETY: The values `x_p, y_p, z_p` are computed correctly to be on the curve.
247257 // The assertion above verifies that the resulting point is valid.
248- Self :: new ( [ xp, yp, zp] ) . unwrap ( )
258+ Self :: new_unchecked ( [ xp, yp, zp] )
249259 }
250260 }
251261 }
@@ -255,8 +265,7 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassProjectivePoint<E> {
255265 let [ px, py, pz] = self . coordinates ( ) ;
256266 // SAFETY:
257267 // - Negating `y` maintains the curve structure.
258- // - `unwraps()` is safe because negation **is always valid**.
259- Self :: new ( [ px. clone ( ) , -py, pz. clone ( ) ] ) . unwrap ( )
268+ Self :: new_unchecked ( [ px. clone ( ) , -py, pz. clone ( ) ] )
260269 }
261270}
262271
@@ -454,6 +463,15 @@ impl<E: IsShortWeierstrass> ShortWeierstrassJacobianPoint<E> {
454463 }
455464 }
456465
466+ /// Creates an elliptic curve point giving the projective [x: y: z] coordinates without
467+ /// checking that the point satisfies the curve equation.
468+ pub const fn new_unchecked ( value : [ FieldElement < E :: BaseField > ; 3 ] ) -> Self {
469+ // SAFETY: The caller MUST ensure that [x:y:z] represents either a valid point on the
470+ // curve. Passing arbitrary coordinates here can violate the invariant
471+ // and produce silently incorrect results in subsequent operations.
472+ Self ( JacobianPoint :: new ( value) )
473+ }
474+
457475 /// Returns the `x` coordinate of the point.
458476 pub fn x ( & self ) -> & FieldElement < E :: BaseField > {
459477 self . 0 . x ( )
@@ -508,7 +526,7 @@ impl<E: IsShortWeierstrass> ShortWeierstrassJacobianPoint<E> {
508526 ) ;
509527 // SAFETY: The values `x_3, y_3, z_3` are computed correctly to be on the curve.
510528 // The assertion above verifies that the resulting point is valid.
511- Self :: new ( [ x3, y3, z3] ) . unwrap ( )
529+ Self :: new_unchecked ( [ x3, y3, z3] )
512530 } else {
513531 // http://www.hyperelliptic.org/EFD/g1p/data/shortw/jacobian-0/doubling/dbl-2009-alnr
514532 // http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
@@ -528,7 +546,7 @@ impl<E: IsShortWeierstrass> ShortWeierstrassJacobianPoint<E> {
528546 ) ;
529547 // SAFETY: The values `x_3, y_3, z_3` are computed correctly to be on the curve.
530548 // The assertion above verifies that the resulting point is valid.
531- Self :: new ( [ x3, y3, z3] ) . unwrap ( )
549+ Self :: new_unchecked ( [ x3, y3, z3] )
532550 }
533551 }
534552
@@ -572,7 +590,7 @@ impl<E: IsShortWeierstrass> ShortWeierstrassJacobianPoint<E> {
572590 ) ;
573591 // SAFETY: The values `x_3, y_3, z_3` are computed correctly to be on the curve.
574592 // The assertion above verifies that the resulting point is valid.
575- Self :: new ( [ x3, y3, z3] ) . unwrap ( )
593+ Self :: new_unchecked ( [ x3, y3, z3] )
576594 }
577595 }
578596}
@@ -600,14 +618,11 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassJacobianPoint<E> {
600618 fn neutral_element ( ) -> Self {
601619 // SAFETY:
602620 // - `(1, 1, 0)` is **mathematically valid** as the neutral element.
603- // - `unwrap()` is safe because this is **a known valid point**.
604-
605- Self :: new ( [
621+ Self :: new_unchecked ( [
606622 FieldElement :: one ( ) ,
607623 FieldElement :: one ( ) ,
608624 FieldElement :: zero ( ) ,
609625 ] )
610- . unwrap ( )
611626 }
612627
613628 fn is_neutral_element ( & self ) -> bool {
@@ -677,7 +692,7 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassJacobianPoint<E> {
677692 ) ;
678693 // SAFETY: The values `x_3, y_3, z_3` are computed correctly to be on the curve.
679694 // The assertion above verifies that the resulting point is valid.
680- Self :: new ( [ x3, y3, z3] ) . unwrap ( )
695+ Self :: new_unchecked ( [ x3, y3, z3] )
681696 }
682697
683698 /// Returns the additive inverse of the jacobian point `p`
@@ -686,7 +701,7 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassJacobianPoint<E> {
686701 // SAFETY:
687702 // - The negation formula for Short Weierstrass curves is well-defined.
688703 // - The result remains a valid curve point.
689- Self :: new ( [ x. clone ( ) , -y, z. clone ( ) ] ) . unwrap ( )
704+ Self :: new_unchecked ( [ x. clone ( ) , -y, z. clone ( ) ] )
690705 }
691706}
692707
0 commit comments