diff --git a/Cargo.lock b/Cargo.lock index 2c3d9895..3bae53d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -308,7 +308,7 @@ dependencies = [ [[package]] name = "ecdsa" version = "0.13.0-pre" -source = "git+https://github.com/RustCrypto/signatures.git#010da678be92aa557a26154b6a5d5345dcc0dd92" +source = "git+https://github.com/RustCrypto/signatures.git#757e9b289f9977a200c32bed7e9c3d40c4d80089" dependencies = [ "der", "elliptic-curve", @@ -325,7 +325,7 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "elliptic-curve" version = "0.11.0-pre" -source = "git+https://github.com/RustCrypto/traits.git#fea0010f3356186804b42e57a8cb3612b96dab9f" +source = "git+https://github.com/RustCrypto/traits.git#148e5da89e85104fe172163093413785fea06c06" dependencies = [ "base64ct", "crypto-bigint", diff --git a/k256/src/arithmetic/affine.rs b/k256/src/arithmetic/affine.rs index dad385d3..4f9fd972 100644 --- a/k256/src/arithmetic/affine.rs +++ b/k256/src/arithmetic/affine.rs @@ -105,40 +105,6 @@ impl PartialEq for AffinePoint { impl Eq for AffinePoint {} -impl AffinePoint { - /// Decode this point from a SEC1-encoded point. - pub(crate) fn decode(encoded_point: &EncodedPoint) -> CtOption { - match encoded_point.coordinates() { - sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()), - sec1::Coordinates::Compact { .. } => { - // TODO(tarcieri): add decompaction support - CtOption::new(Self::default(), 0.into()) - } - sec1::Coordinates::Compressed { x, y_is_odd } => { - AffinePoint::decompress(x, Choice::from(y_is_odd as u8)) - } - sec1::Coordinates::Uncompressed { x, y } => { - let x = FieldElement::from_bytes(x); - let y = FieldElement::from_bytes(y); - - x.and_then(|x| { - y.and_then(|y| { - // Check that the point is on the curve - let lhs = (y * &y).negate(1); - let rhs = x * &x * &x + &CURVE_EQUATION_B; - let point = AffinePoint { - x, - y, - infinity: Choice::from(0), - }; - CtOption::new(point, (lhs + &rhs).normalizes_to_zero()) - }) - }) - } - } - } -} - impl DecompressPoint for AffinePoint { fn decompress(x_bytes: &FieldBytes, y_is_odd: Choice) -> CtOption { FieldElement::from_bytes(x_bytes).and_then(|x| { @@ -189,8 +155,35 @@ impl FromEncodedPoint for AffinePoint { /// # Returns /// /// `None` value if `encoded_point` is not on the secp256k1 curve. - fn from_encoded_point(encoded_point: &EncodedPoint) -> Option { - Self::decode(encoded_point).into() + fn from_encoded_point(encoded_point: &EncodedPoint) -> CtOption { + match encoded_point.coordinates() { + sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()), + sec1::Coordinates::Compact { .. } => { + // TODO(tarcieri): add decompaction support + CtOption::new(Self::default(), 0.into()) + } + sec1::Coordinates::Compressed { x, y_is_odd } => { + AffinePoint::decompress(x, Choice::from(y_is_odd as u8)) + } + sec1::Coordinates::Uncompressed { x, y } => { + let x = FieldElement::from_bytes(x); + let y = FieldElement::from_bytes(y); + + x.and_then(|x| { + y.and_then(|y| { + // Check that the point is on the curve + let lhs = (y * &y).negate(1); + let rhs = x * &x * &x + &CURVE_EQUATION_B; + let point = AffinePoint { + x, + y, + infinity: Choice::from(0), + }; + CtOption::new(point, (lhs + &rhs).normalizes_to_zero()) + }) + }) + } + } } } diff --git a/k256/src/arithmetic/projective.rs b/k256/src/arithmetic/projective.rs index 87f4af92..6bb1dd25 100644 --- a/k256/src/arithmetic/projective.rs +++ b/k256/src/arithmetic/projective.rs @@ -62,7 +62,7 @@ impl From for AffinePoint { } impl FromEncodedPoint for ProjectivePoint { - fn from_encoded_point(p: &EncodedPoint) -> Option { + fn from_encoded_point(p: &EncodedPoint) -> CtOption { AffinePoint::from_encoded_point(p).map(ProjectivePoint::from) } } diff --git a/p256/src/arithmetic/affine.rs b/p256/src/arithmetic/affine.rs index 4dae4e92..0c7caabe 100644 --- a/p256/src/arithmetic/affine.rs +++ b/p256/src/arithmetic/affine.rs @@ -105,36 +105,6 @@ impl PartialEq for AffinePoint { } } -impl AffinePoint { - fn decode(encoded_point: &EncodedPoint) -> CtOption { - match encoded_point.coordinates() { - sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()), - sec1::Coordinates::Compact { x } => AffinePoint::decompact(x), - sec1::Coordinates::Compressed { x, y_is_odd } => { - AffinePoint::decompress(x, Choice::from(y_is_odd as u8)) - } - sec1::Coordinates::Uncompressed { x, y } => { - let x = FieldElement::from_bytes(x); - let y = FieldElement::from_bytes(y); - - x.and_then(|x| { - y.and_then(|y| { - // Check that the point is on the curve - let lhs = y * &y; - let rhs = x * &x * &x + &(CURVE_EQUATION_A * &x) + &CURVE_EQUATION_B; - let point = AffinePoint { - x, - y, - infinity: Choice::from(0), - }; - CtOption::new(point, lhs.ct_eq(&rhs)) - }) - }) - } - } - } -} - impl DecompressPoint for AffinePoint { fn decompress(x_bytes: &FieldBytes, y_is_odd: Choice) -> CtOption { FieldElement::from_bytes(x_bytes).and_then(|x| { @@ -205,8 +175,32 @@ impl FromEncodedPoint for AffinePoint { /// # Returns /// /// `None` value if `encoded_point` is not on the secp256r1 curve. - fn from_encoded_point(encoded_point: &EncodedPoint) -> Option { - Self::decode(encoded_point).into() + fn from_encoded_point(encoded_point: &EncodedPoint) -> CtOption { + match encoded_point.coordinates() { + sec1::Coordinates::Identity => CtOption::new(Self::identity(), 1.into()), + sec1::Coordinates::Compact { x } => AffinePoint::decompact(x), + sec1::Coordinates::Compressed { x, y_is_odd } => { + AffinePoint::decompress(x, Choice::from(y_is_odd as u8)) + } + sec1::Coordinates::Uncompressed { x, y } => { + let x = FieldElement::from_bytes(x); + let y = FieldElement::from_bytes(y); + + x.and_then(|x| { + y.and_then(|y| { + // Check that the point is on the curve + let lhs = y * &y; + let rhs = x * &x * &x + &(CURVE_EQUATION_A * &x) + &CURVE_EQUATION_B; + let point = AffinePoint { + x, + y, + infinity: Choice::from(0), + }; + CtOption::new(point, lhs.ct_eq(&rhs)) + }) + }) + } + } } } diff --git a/p256/src/arithmetic/projective.rs b/p256/src/arithmetic/projective.rs index 24fc7619..1923616b 100644 --- a/p256/src/arithmetic/projective.rs +++ b/p256/src/arithmetic/projective.rs @@ -115,7 +115,7 @@ impl From for AffinePoint { } impl FromEncodedPoint for ProjectivePoint { - fn from_encoded_point(p: &EncodedPoint) -> Option { + fn from_encoded_point(p: &EncodedPoint) -> CtOption { AffinePoint::from_encoded_point(p).map(ProjectivePoint::from) } }