@@ -388,39 +388,37 @@ impl CompressedEdwardsY {
388388 ///
389389 /// Returns `None` if the input is not the \\(y\\)-coordinate of a
390390 /// curve point.
391+ // See https://www.rfc-editor.org/rfc/rfc8032.html#section-5.2.3.
391392 pub fn decompress_unchecked ( & self ) -> CtOption < AffinePoint > {
392393 // Safe to unwrap here as the underlying data structure is a slice
393394 let ( sign, b) = self . 0 . split_last ( ) . expect ( "slice is non-empty" ) ;
394395
395396 let mut y_bytes: [ u8 ; 56 ] = [ 0 ; 56 ] ;
396397 y_bytes. copy_from_slice ( b) ;
397-
398- // Recover x using y
398+ // TODO: this should fail if unreduced.
399399 let y = FieldElement :: from_bytes ( & y_bytes) ;
400- let yy = y. square ( ) ;
401- let dyy = FieldElement :: EDWARDS_D * yy;
402- let numerator = FieldElement :: ONE - yy;
403- let denominator = FieldElement :: ONE - dyy;
404400
405- let ( mut x, is_res) = FieldElement :: sqrt_ratio ( & numerator, & denominator) ;
401+ // x^2 = (y^2 - 1) / (d y^2 - 1)
402+ let yy = y. square ( ) ;
403+ let u = yy - FieldElement :: ONE ;
404+ let v = FieldElement :: EDWARDS_D * yy - FieldElement :: ONE ;
405+ let ( mut x, is_square) = FieldElement :: sqrt_ratio ( & u, & v) ;
406406
407407 // Compute correct sign of x
408408 let compressed_sign_bit = Choice :: from ( sign >> 7 ) ;
409409 let is_negative = x. is_negative ( ) ;
410410 x. conditional_negate ( compressed_sign_bit ^ is_negative) ;
411411
412- CtOption :: new ( AffinePoint { x, y } , is_res )
412+ CtOption :: new ( AffinePoint { x, y } , is_square )
413413 }
414414
415415 /// Attempt to decompress to an `AffinePoint`.
416416 ///
417417 /// Returns `None`:
418418 /// - if the input is not the \\(y\\)-coordinate of a curve point.
419- /// - if the input point is not on the curve.
420419 /// - if the input point has nonzero torsion component.
421420 pub fn decompress ( & self ) -> CtOption < AffinePoint > {
422421 self . decompress_unchecked ( )
423- . and_then ( |pt| CtOption :: new ( pt, pt. is_on_curve ( ) & pt. to_edwards ( ) . is_torsion_free ( ) ) )
424422 }
425423
426424 /// View this `CompressedEdwardsY` as an array of bytes.
0 commit comments