@@ -45,33 +45,40 @@ impl AffineCoordinates for P256Point {
4545 type FieldRepr = FieldBytes ;
4646
4747 fn x ( & self ) -> FieldBytes {
48- * FieldBytes :: from_slice ( & <Self as WeierstrassPoint >:: x ( self ) . to_be_bytes ( ) )
48+ let n = self . normalize ( ) ;
49+ * FieldBytes :: from_slice ( & <Self as WeierstrassPoint >:: x ( & n) . to_be_bytes ( ) )
4950 }
5051
5152 fn y_is_odd ( & self ) -> Choice {
52- ( self . y ( ) . as_le_bytes ( ) [ 0 ] & 1 ) . into ( )
53+ let n = self . normalize ( ) ;
54+ ( n. y ( ) . as_le_bytes ( ) [ 0 ] & 1 ) . into ( )
5355 }
5456}
5557
5658impl Copy for P256Point { }
5759
5860impl ConditionallySelectable for P256Point {
5961 fn conditional_select ( a : & P256Point , b : & P256Point , choice : Choice ) -> P256Point {
60- P256Point :: from_xy_unchecked (
62+ P256Point :: from_xyz_unchecked (
6163 P256Coord :: conditional_select (
6264 <Self as WeierstrassPoint >:: x ( a) ,
6365 <Self as WeierstrassPoint >:: x ( b) ,
6466 choice,
6567 ) ,
6668 P256Coord :: conditional_select ( a. y ( ) , b. y ( ) , choice) ,
69+ P256Coord :: conditional_select ( a. z ( ) , b. z ( ) , choice) ,
6770 )
6871 }
6972}
7073
7174impl ConstantTimeEq for P256Point {
7275 fn ct_eq ( & self , other : & P256Point ) -> Choice {
73- <Self as WeierstrassPoint >:: x ( self ) . ct_eq ( <Self as WeierstrassPoint >:: x ( other) )
74- & self . y ( ) . ct_eq ( other. y ( ) )
76+ // Projective equivalence: (X1*Z2 == X2*Z1) && (Y1*Z2 == Y2*Z1)
77+ let x1z2 = <Self as WeierstrassPoint >:: x ( self ) * other. z ( ) ;
78+ let x2z1 = <Self as WeierstrassPoint >:: x ( other) * self . z ( ) ;
79+ let y1z2 = self . y ( ) * other. z ( ) ;
80+ let y2z1 = other. y ( ) * self . z ( ) ;
81+ x1z2. ct_eq ( & x2z1) & y1z2. ct_eq ( & y2z1)
7582 }
7683}
7784
@@ -160,7 +167,7 @@ impl elliptic_curve::group::Curve for P256Point {
160167 type AffineRepr = P256Point ;
161168
162169 fn to_affine ( & self ) -> P256Point {
163- * self
170+ self . normalize ( )
164171 }
165172}
166173
@@ -214,10 +221,11 @@ impl FromEncodedPoint<NistP256> for P256Point {
214221
215222impl ToEncodedPoint < NistP256 > for P256Point {
216223 fn to_encoded_point ( & self , compress : bool ) -> EncodedPoint {
224+ let n = self . normalize ( ) ;
217225 EncodedPoint :: conditional_select (
218226 & EncodedPoint :: from_affine_coordinates (
219- & <Self as WeierstrassPoint >:: x ( self ) . to_be_bytes ( ) . into ( ) ,
220- & <Self as WeierstrassPoint >:: y ( self ) . to_be_bytes ( ) . into ( ) ,
227+ & <Self as WeierstrassPoint >:: x ( & n ) . to_be_bytes ( ) . into ( ) ,
228+ & <Self as WeierstrassPoint >:: y ( & n ) . to_be_bytes ( ) . into ( ) ,
221229 compress,
222230 ) ,
223231 & EncodedPoint :: identity ( ) ,
0 commit comments