From abdebec7bd59b9168e4ec34039c4baeab80419eb Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 25 Aug 2020 09:42:47 -0700 Subject: [PATCH] elliptic-curve: expose SEC1 Tag and coordinates These are helpful when implementing SEC1 point decoding --- elliptic-curve/src/sec1.rs | 42 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/elliptic-curve/src/sec1.rs b/elliptic-curve/src/sec1.rs index 79afe6a6a..4df0d9d48 100644 --- a/elliptic-curve/src/sec1.rs +++ b/elliptic-curve/src/sec1.rs @@ -151,15 +151,21 @@ where T::from_encoded_point(self) } + /// Get the SEC1 tag for this [`EncodedPoint`] + pub fn tag(&self) -> Tag { + // Tag is ensured valid by the constructor + Tag::from_u8(self.bytes[0]).expect("invalid tag") + } + /// Get the x-coordinate for this [`EncodedPoint`] - pub(crate) fn x(&self) -> &ElementBytes { + pub fn x(&self) -> &ElementBytes { self.coordinates().0 } /// Get the y-coordinate for this [`EncodedPoint`]. /// /// Returns `None` if this point is compressed. - fn y(&self) -> Option<&ElementBytes> { + pub fn y(&self) -> Option<&ElementBytes> { self.coordinates().1 } @@ -174,12 +180,6 @@ where (x.into(), Some(y.into())) } } - - /// Get the SEC1 tag for this [`EncodedPoint`] - fn tag(&self) -> Tag { - // Tag is ensured valid by the constructor - Tag::from_u8(self.bytes[0]).expect("invalid tag") - } } impl AsRef<[u8]> for EncodedPoint @@ -261,7 +261,7 @@ where /// Tag byte used by the `Elliptic-Curve-Point-to-Octet-String` encoding. #[derive(Copy, Clone, Debug, Eq, PartialEq)] #[repr(u8)] -enum Tag { +pub enum Tag { /// Compressed point with even y-coordinate CompressedEvenY = 2, @@ -283,18 +283,6 @@ impl Tag { } } - /// Compress the given y-coordinate, returning a `Tag::Compressed*` value - pub fn compress_y(y: &[u8]) -> Self { - debug_assert!(!y.is_empty()); - - // Is the y-coordinate odd in the SEC1 sense: `self mod 2 == 1`? - if y.as_ref().last().unwrap() & 1 == 1 { - Tag::CompressedOddY - } else { - Tag::CompressedEvenY - } - } - /// Is this point compressed? pub fn is_compressed(self) -> bool { match self { @@ -313,6 +301,18 @@ impl Tag { field_element_size * 2 } } + + /// Compress the given y-coordinate, returning a `Tag::Compressed*` value + fn compress_y(y: &[u8]) -> Self { + debug_assert!(!y.is_empty()); + + // Is the y-coordinate odd in the SEC1 sense: `self mod 2 == 1`? + if y.as_ref().last().unwrap() & 1 == 1 { + Tag::CompressedOddY + } else { + Tag::CompressedEvenY + } + } } impl From for u8 {