Skip to content

Commit

Permalink
elliptic-curve: add sec1::Coordinates::tag method (#407)
Browse files Browse the repository at this point in the history
Obtain the `Tag` needed to encode a given set of `sec1::Coordinates`
  • Loading branch information
tarcieri authored Dec 17, 2020
1 parent 2aac343 commit 01ee4e5
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,36 +353,21 @@ pub enum Coordinates<'a, C: Curve> {
},
}

/// Trait for deserializing a value from a SEC1 encoded curve point.
///
/// This is intended for use with the `AffinePoint` type for a given elliptic curve.
pub trait FromEncodedPoint<C>
where
C: Curve,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
Self: Sized,
{
/// Deserialize the type this trait is impl'd on from an [`EncodedPoint`].
///
/// # Returns
///
/// `None` if the [`EncodedPoint`] is invalid.
fn from_encoded_point(public_key: &EncodedPoint<C>) -> Option<Self>;
}

/// Trait for serializing a value to a SEC1 encoded curve point.
///
/// This is intended for use with the `AffinePoint` type for a given elliptic curve.
pub trait ToEncodedPoint<C>
where
C: Curve,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
/// Serialize this value as a SEC1 [`EncodedPoint`], optionally applying
/// point compression.
fn to_encoded_point(&self, compress: bool) -> EncodedPoint<C>;
impl<'a, C: Curve> Coordinates<'a, C> {
/// Get the tag value needed to encode this set of [`Coordinates`]
pub fn tag(&self) -> Tag {
match self {
Coordinates::Identity => Tag::Identity,
Coordinates::Compressed { y_is_odd, .. } => {
if *y_is_odd {
Tag::CompressedOddY
} else {
Tag::CompressedEvenY
}
}
Coordinates::Uncompressed { .. } => Tag::Uncompressed,
}
}
}

/// Tag byte used by the `Elliptic-Curve-Point-to-Octet-String` encoding.
Expand Down Expand Up @@ -454,6 +439,38 @@ impl From<Tag> for u8 {
}
}

/// Trait for deserializing a value from a SEC1 encoded curve point.
///
/// This is intended for use with the `AffinePoint` type for a given elliptic curve.
pub trait FromEncodedPoint<C>
where
C: Curve,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
Self: Sized,
{
/// Deserialize the type this trait is impl'd on from an [`EncodedPoint`].
///
/// # Returns
///
/// `None` if the [`EncodedPoint`] is invalid.
fn from_encoded_point(public_key: &EncodedPoint<C>) -> Option<Self>;
}

/// Trait for serializing a value to a SEC1 encoded curve point.
///
/// This is intended for use with the `AffinePoint` type for a given elliptic curve.
pub trait ToEncodedPoint<C>
where
C: Curve,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
/// Serialize this value as a SEC1 [`EncodedPoint`], optionally applying
/// point compression.
fn to_encoded_point(&self, compress: bool) -> EncodedPoint<C>;
}

#[cfg(test)]
mod tests {
use super::{Coordinates, Tag};
Expand Down

0 comments on commit 01ee4e5

Please sign in to comment.