Skip to main content

PublicKey

Struct PublicKey 

pub struct PublicKey<C>
where C: CurveArithmetic,
{ /* private fields */ }
Expand description

Elliptic curve public keys.

This is a wrapper type for AffinePoint which ensures an inner non-identity point and provides a common place to handle encoding/decoding.

§Parsing “SPKI” Keys

X.509 SubjectPublicKeyInfo (SPKI) is a commonly used format for encoding public keys, notably public keys corresponding to PKCS#8 private keys. (especially ones generated by OpenSSL).

Keys in SPKI format are either binary (ASN.1 BER/DER), or PEM encoded (ASCII) and begin with the following:

-----BEGIN PUBLIC KEY-----

To decode an elliptic curve public key from SPKI, enable the pkcs8 feature of this crate (or the pkcs8 feature of a specific RustCrypto elliptic curve crate) and use the [elliptic_curve::pkcs8::DecodePublicKey][pkcs8::DecodePublicKey] trait to parse it.

When the pem feature of this crate (or a specific RustCrypto elliptic curve crate) is enabled, a [FromStr] impl is also available.

§serde support

When the optional serde feature of this create is enabled, [Serialize] and [Deserialize] impls are provided for this type.

The serialization is binary-oriented and supports ASN.1 DER Subject Public Key Info (SPKI) as the encoding format.

For a more text-friendly encoding of public keys, use [JwkEcKey] instead.

Implementations§

§

impl<C> PublicKey<C>
where C: CurveArithmetic,

pub fn from_affine( point: <C as CurveArithmetic>::AffinePoint, ) -> Result<PublicKey<C>, Error>

Convert an AffinePoint into a PublicKey

pub fn from_secret_scalar(scalar: &NonZeroScalar<C>) -> PublicKey<C>

Compute a PublicKey from a secret NonZeroScalar value (i.e. a secret key represented as a raw scalar value)

pub fn from_sec1_bytes(bytes: &[u8]) -> Result<PublicKey<C>, Error>

Decode PublicKey (compressed or uncompressed) from 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

pub fn as_affine(&self) -> &<C as CurveArithmetic>::AffinePoint

Borrow the inner AffinePoint from this PublicKey.

In ECC, public keys are elliptic curve points.

pub fn to_projective(&self) -> <C as CurveArithmetic>::ProjectivePoint

Convert this PublicKey to a ProjectivePoint for the given curve

pub fn to_nonidentity(&self) -> NonIdentity<<C as CurveArithmetic>::AffinePoint>

Convert this PublicKey to a NonIdentity of the inner AffinePoint

Trait Implementations§

§

impl<C> AsRef<<C as CurveArithmetic>::AffinePoint> for PublicKey<C>
where C: CurveArithmetic,

§

fn as_ref(&self) -> &<C as CurveArithmetic>::AffinePoint

Converts this type into a shared reference of the (usually inferred) input type.
§

impl<C> Clone for PublicKey<C>

§

fn clone(&self) -> PublicKey<C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<C> Debug for PublicKey<C>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<C, P> From<&NonIdentity<P>> for PublicKey<C>

§

fn from(value: &NonIdentity<P>) -> PublicKey<C>

Converts to this type from the input type.
§

impl<C> From<&PublicKey<C>> for AffinePoint<C>
where C: PrimeCurveParams,

§

fn from(public_key: &PublicKey<C>) -> AffinePoint<C>

Converts to this type from the input type.
§

impl<C> From<&PublicKey<C>> for EncodedPoint<<C as Curve>::FieldBytesSize>

Available on crate feature sec1 only.
§

fn from(public_key: &PublicKey<C>) -> EncodedPoint<<C as Curve>::FieldBytesSize>

Converts to this type from the input type.
§

impl<C> From<&PublicKey<C>> for GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>

Available on crate feature sec1 only.
§

fn from( public_key: &PublicKey<C>, ) -> GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>

Converts to this type from the input type.
§

impl<C> From<&PublicKey<C>> for NonIdentity<<C as CurveArithmetic>::AffinePoint>
where C: CurveArithmetic,

§

fn from( value: &PublicKey<C>, ) -> NonIdentity<<C as CurveArithmetic>::AffinePoint>

Converts to this type from the input type.
§

impl<C> From<&PublicKey<C>> for ProjectivePoint<C>
where C: PrimeCurveParams,

§

fn from(public_key: &PublicKey<C>) -> ProjectivePoint<C>

Converts to this type from the input type.
§

impl<C> From<&PublicKey<C>> for VerifyingKey<C>

§

fn from(public_key: &PublicKey<C>) -> VerifyingKey<C>

Converts to this type from the input type.
§

impl From<&PublicKey<Secp256k1>> for AffinePoint

§

fn from(public_key: &PublicKey<Secp256k1>) -> AffinePoint

Converts to this type from the input type.
§

impl From<&PublicKey<Secp256k1>> for ProjectivePoint

§

fn from(public_key: &PublicKey<Secp256k1>) -> ProjectivePoint

Converts to this type from the input type.
§

impl<C> From<&VerifyingKey<C>> for PublicKey<C>

§

fn from(verifying_key: &VerifyingKey<C>) -> PublicKey<C>

Converts to this type from the input type.
§

impl<C, P> From<NonIdentity<P>> for PublicKey<C>

§

fn from(value: NonIdentity<P>) -> PublicKey<C>

Converts to this type from the input type.
§

impl<C> From<PublicKey<C>> for AffinePoint<C>
where C: PrimeCurveParams,

§

fn from(public_key: PublicKey<C>) -> AffinePoint<C>

Converts to this type from the input type.
§

impl<C> From<PublicKey<C>> for EncodedPoint<<C as Curve>::FieldBytesSize>

Available on crate feature sec1 only.
§

fn from(public_key: PublicKey<C>) -> EncodedPoint<<C as Curve>::FieldBytesSize>

Converts to this type from the input type.
§

impl<C> From<PublicKey<C>> for GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>

Available on crate feature sec1 only.
§

fn from( public_key: PublicKey<C>, ) -> GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>

Converts to this type from the input type.
§

impl<C> From<PublicKey<C>> for NonIdentity<<C as CurveArithmetic>::AffinePoint>
where C: CurveArithmetic,

§

fn from(value: PublicKey<C>) -> NonIdentity<<C as CurveArithmetic>::AffinePoint>

Converts to this type from the input type.
§

impl<C> From<PublicKey<C>> for ProjectivePoint<C>
where C: PrimeCurveParams,

§

fn from(public_key: PublicKey<C>) -> ProjectivePoint<C>

Converts to this type from the input type.
§

impl<C> From<PublicKey<C>> for VerifyingKey<C>

§

fn from(public_key: PublicKey<C>) -> VerifyingKey<C>

Converts to this type from the input type.
§

impl From<PublicKey<Secp256k1>> for AffinePoint

§

fn from(public_key: PublicKey<Secp256k1>) -> AffinePoint

Converts to this type from the input type.
§

impl From<PublicKey<Secp256k1>> for ProjectivePoint

§

fn from(public_key: PublicKey<Secp256k1>) -> ProjectivePoint

Converts to this type from the input type.
§

impl<C> From<VerifyingKey<C>> for PublicKey<C>

§

fn from(verifying_key: VerifyingKey<C>) -> PublicKey<C>

Converts to this type from the input type.
§

impl<C> FromEncodedPoint<C> for PublicKey<C>

Available on crate feature sec1 only.
§

fn from_encoded_point( encoded_point: &EncodedPoint<<C as Curve>::FieldBytesSize>, ) -> CtOption<PublicKey<C>>

Initialize PublicKey from an EncodedPoint

§

impl<C> Ord for PublicKey<C>

Available on crate feature sec1 only.
§

fn cmp(&self, other: &PublicKey<C>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
§

impl<C> PartialEq for PublicKey<C>

§

fn eq(&self, other: &PublicKey<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<C> PartialOrd for PublicKey<C>

Available on crate feature sec1 only.
§

fn partial_cmp(&self, other: &PublicKey<C>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl<C> ToEncodedPoint<C> for PublicKey<C>

Available on crate feature sec1 only.
§

fn to_encoded_point( &self, compress: bool, ) -> EncodedPoint<<C as Curve>::FieldBytesSize>

Serialize this PublicKey as a SEC1 EncodedPoint, optionally applying point compression

§

impl<C> TryFrom<&AffinePoint<C>> for PublicKey<C>
where C: PrimeCurveParams,

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(affine_point: &AffinePoint<C>) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl TryFrom<&AffinePoint> for PublicKey<Secp256k1>

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(affine_point: &AffinePoint) -> Result<PublicKey<Secp256k1>, Error>

Performs the conversion.
§

impl<C> TryFrom<&EncodedPoint<<C as Curve>::FieldBytesSize>> for PublicKey<C>

Available on crate feature sec1 only.
§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from( point: &EncodedPoint<<C as Curve>::FieldBytesSize>, ) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl<C> TryFrom<&GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>> for PublicKey<C>

Available on crate feature sec1 only.
§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from( point: &GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>, ) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl<C> TryFrom<&ProjectivePoint<C>> for PublicKey<C>
where C: PrimeCurveParams,

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(point: &ProjectivePoint<C>) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl TryFrom<&ProjectivePoint> for PublicKey<Secp256k1>

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(point: &ProjectivePoint) -> Result<PublicKey<Secp256k1>, Error>

Performs the conversion.
§

impl<C> TryFrom<AffinePoint<C>> for PublicKey<C>
where C: PrimeCurveParams,

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(affine_point: AffinePoint<C>) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl TryFrom<AffinePoint> for PublicKey<Secp256k1>

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(affine_point: AffinePoint) -> Result<PublicKey<Secp256k1>, Error>

Performs the conversion.
§

impl<C> TryFrom<EncodedPoint<<C as Curve>::FieldBytesSize>> for PublicKey<C>

Available on crate feature sec1 only.
§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from( point: EncodedPoint<<C as Curve>::FieldBytesSize>, ) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl<C> TryFrom<GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>> for PublicKey<C>

Available on crate feature sec1 only.
§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from( point: GenericArray<u8, <<C as Curve>::FieldBytesSize as ModulusSize>::CompressedPointSize>, ) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl<C> TryFrom<ProjectivePoint<C>> for PublicKey<C>
where C: PrimeCurveParams,

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(point: ProjectivePoint<C>) -> Result<PublicKey<C>, Error>

Performs the conversion.
§

impl TryFrom<ProjectivePoint> for PublicKey<Secp256k1>

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(point: ProjectivePoint) -> Result<PublicKey<Secp256k1>, Error>

Performs the conversion.
§

impl<C> Copy for PublicKey<C>
where C: CurveArithmetic,

§

impl<C> Eq for PublicKey<C>
where C: Eq + CurveArithmetic,

§

impl<C> StructuralPartialEq for PublicKey<C>
where C: CurveArithmetic,

Auto Trait Implementations§

§

impl<C> Freeze for PublicKey<C>

§

impl<C> RefUnwindSafe for PublicKey<C>

§

impl<C> Send for PublicKey<C>

§

impl<C> Sync for PublicKey<C>

§

impl<C> Unpin for PublicKey<C>

§

impl<C> UnwindSafe for PublicKey<C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V