Further refactoring of get_point_from_x()

This commit is contained in:
Sean Bowe 2017-10-05 12:35:04 -06:00
parent 683f21a4d5
commit dbac57c27b
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
1 changed files with 9 additions and 32 deletions

View File

@ -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 {