From dbac57c27bc4628abe0170d73ea5d2b67193bfff Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 5 Oct 2017 12:35:04 -0600 Subject: [PATCH] Further refactoring of get_point_from_x() --- src/bls12_381/ec.rs | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/src/bls12_381/ec.rs b/src/bls12_381/ec.rs index 16c5401..f459e0b 100644 --- a/src/bls12_381/ec.rs +++ b/src/bls12_381/ec.rs @@ -85,11 +85,12 @@ macro_rules! curve_impl { } impl $affine { - /// Constructs an affine point with the lexicographically smallest - /// y-coordinate, given an x-coordinate, so long as the x-coordinate - /// exists on the curve. The point is not guaranteed to be in the - /// prime order subgroup. - fn get_point_from_x(x: $basefield) -> Option<$affine> { + /// Attempts to construct an affine point given an x-coordinate. The + /// point is not guaranteed to be in the prime order subgroup. + /// + /// If and only if `greatest` is set will the lexicographically + /// largest y-coordinate be selected. + fn get_point_from_x(x: $basefield, greatest: bool) -> Option<$affine> { // Compute x^3 + b let mut x3b = x; x3b.square(); @@ -102,7 +103,7 @@ macro_rules! curve_impl { $affine { x: x, - y: if y < negy { + y: if (y < negy) ^ greatest { y } else { negy @@ -808,19 +809,7 @@ pub mod g1 { // Interpret as Fq element. let x = Fq::from_repr(x).map_err(|e| GroupDecodingError::CoordinateDecodingError("x coordinate", e))?; - match G1Affine::get_point_from_x(x) { - Some(mut p) => { - if greatest { - p.negate(); - } - - Ok(p) - }, - None => { - // Point must not be on the curve. - Err(GroupDecodingError::NotOnCurve) - } - } + G1Affine::get_point_from_x(x, greatest).ok_or(GroupDecodingError::NotOnCurve) } } fn from_affine(affine: G1Affine) -> Self { @@ -1321,19 +1310,7 @@ pub mod g2 { c1: Fq::from_repr(x_c1).map_err(|e| GroupDecodingError::CoordinateDecodingError("x coordinate (c1)", e))? }; - match G2Affine::get_point_from_x(x) { - Some(mut p) => { - if greatest { - p.negate(); - } - - Ok(p) - }, - None => { - // Point must not be on the curve. - Err(GroupDecodingError::NotOnCurve) - } - } + G2Affine::get_point_from_x(x, greatest).ok_or(GroupDecodingError::NotOnCurve) } } fn from_affine(affine: G2Affine) -> Self {