Skip to content

Commit

Permalink
elliptic-curve: add to_sec1_bytes (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
KizzyCode committed Sep 9, 2022
1 parent 9d1b5b3 commit 726e5cd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions elliptic-curve/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ use pkcs8::EncodePublicKey;
#[cfg(any(feature = "jwk", feature = "pem"))]
use alloc::string::{String, ToString};

#[cfg(feature = "alloc")]
use alloc::boxed::Box;

/// Elliptic curve public keys.
///
/// This is a wrapper type for [`AffinePoint`] which ensures an inner
Expand Down Expand Up @@ -121,6 +124,23 @@ where
Option::from(Self::from_encoded_point(&point)).ok_or(Error)
}

/// Convert this [`PublicKey`] into the
/// `Elliptic-Curve-Point-to-Octet-String` encoding described in
/// SEC 1: Elliptic Curve Cryptography (Version 2.0) section 2.3.3
/// (page 10).
///
/// <http://www.secg.org/sec1-v2.pdf>
#[cfg(feature = "alloc")]
pub fn to_sec1_bytes(&self) -> Box<[u8]>
where
C: Curve + ProjectiveArithmetic + PointCompression,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
{
let point = EncodedPoint::<C>::from(self);
point.to_bytes()
}

/// Borrow the inner [`AffinePoint`] from this [`PublicKey`].
///
/// In ECC, public keys are elliptic curve points.
Expand Down

0 comments on commit 726e5cd

Please sign in to comment.