Skip to content

Commit 7ccafd3

Browse files
committed
Remove unnecessary checks from CompressedEdwardsY::decompress()
1 parent 8471bc7 commit 7ccafd3

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

ed448-goldilocks/src/edwards/affine.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

ed448-goldilocks/src/edwards/extended.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,8 @@ mod tests {
961961
"13b6714c7a5f53101bbec88f2f17cd30f42e37fae363a5474efb4197ed6005df5861ae178a0c2c16ad378b7befed0d0904b7ced35e9f674180"
962962
);
963963
let compressed = CompressedEdwardsY(bytes);
964-
let decompressed = compressed.decompress();
965-
assert_eq!(decompressed.is_none().unwrap_u8(), 1u8);
964+
let decompressed = compressed.decompress().unwrap();
965+
assert_eq!(decompressed.to_edwards().is_torsion_free().unwrap_u8(), 0u8);
966966
}
967967

968968
#[test]

0 commit comments

Comments
 (0)