pub struct PublicKey(/* private fields */);
Expand description
A Secp256k1 public key, used for verification of signatures.
§Serde support
Implements de/serialization with the serde
feature enabled. We treat the byte value as a tuple
of 33 u8
s for non-human-readable formats. This representation is optimal for for some formats
(e.g. bincode
) however other formats may be less optimal (e.g. cbor
).
§Examples
Basic usage:
use secp256k1::{SecretKey, Secp256k1, PublicKey};
let secp = Secp256k1::new();
let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
Implementations§
§impl PublicKey
impl PublicKey
pub fn as_ptr(&self) -> *const PublicKey
pub fn as_ptr(&self) -> *const PublicKey
Obtains a raw const pointer suitable for use with FFI functions.
pub fn as_mut_ptr(&mut self) -> *mut PublicKey
pub fn as_mut_ptr(&mut self) -> *mut PublicKey
Obtains a raw mutable pointer suitable for use with FFI functions.
pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: &SecretKey) -> PublicKeywhere
C: Signing,
pub fn from_secret_key<C>(secp: &Secp256k1<C>, sk: &SecretKey) -> PublicKeywhere
C: Signing,
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error>
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error>
Creates a public key directly from a slice.
pub fn from_keypair(keypair: &KeyPair) -> PublicKey
pub fn from_keypair(keypair: &KeyPair) -> PublicKey
pub fn from_x_only_public_key(pk: XOnlyPublicKey, parity: Parity) -> PublicKey
pub fn from_x_only_public_key(pk: XOnlyPublicKey, parity: Parity) -> PublicKey
Creates a PublicKey
using the key material from pk
combined with the parity
.
pub fn serialize(&self) -> [u8; 33]
pub fn serialize(&self) -> [u8; 33]
Serializes the key as a byte-encoded pair of values. In compressed form the y-coordinate is represented by only a single bit, as x determines it up to one bit.
pub fn serialize_uncompressed(&self) -> [u8; 65]
pub fn serialize_uncompressed(&self) -> [u8; 65]
Serializes the key as a byte-encoded pair of values, in uncompressed form.
pub fn negate_assign<C>(&mut self, secp: &Secp256k1<C>)where
C: Verification,
👎Deprecated since 0.23.0: Use negate instead
pub fn negate_assign<C>(&mut self, secp: &Secp256k1<C>)where
C: Verification,
Negates the public key in place.
pub fn negate<C>(self, secp: &Secp256k1<C>) -> PublicKeywhere
C: Verification,
pub fn negate<C>(self, secp: &Secp256k1<C>) -> PublicKeywhere
C: Verification,
Negates the public key.
pub fn add_exp_assign<C>(
&mut self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<(), Error>where
C: Verification,
👎Deprecated since 0.23.0: Use add_exp_tweak instead
pub fn add_exp_assign<C>(
&mut self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<(), Error>where
C: Verification,
pub fn add_exp_tweak<C>(
self,
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn add_exp_tweak<C>(
self,
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn mul_assign<C>(
&mut self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<(), Error>where
C: Verification,
👎Deprecated since 0.23.0: Use mul_tweak instead
pub fn mul_assign<C>(
&mut self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<(), Error>where
C: Verification,
Muliplies the public key in place by the scalar other
.
§Errors
Returns an error if the resulting key would be invalid.
pub fn mul_tweak<C>(
self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn mul_tweak<C>(
self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<PublicKey, Error>where
C: Verification,
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error>
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error>
Adds a second key to this one, returning the sum.
§Errors
If the result would be the point at infinity, i.e. adding this point to its own negation.
§Examples
use secp256k1::{rand, Secp256k1};
let secp = Secp256k1::new();
let mut rng = rand::thread_rng();
let (_, pk1) = secp.generate_keypair(&mut rng);
let (_, pk2) = secp.generate_keypair(&mut rng);
let sum = pk1.combine(&pk2).expect("It's improbable to fail for 2 random public keys");
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error>
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error>
Adds the keys in the provided slice together, returning the sum.
§Errors
Errors under any of the following conditions:
- The result would be the point at infinity, i.e. adding a point to its own negation.
- The provided slice is empty.
- The number of elements in the provided slice is greater than
i32::MAX
.
§Examples
use secp256k1::{rand, Secp256k1, PublicKey};
let secp = Secp256k1::new();
let mut rng = rand::thread_rng();
let (_, pk1) = secp.generate_keypair(&mut rng);
let (_, pk2) = secp.generate_keypair(&mut rng);
let (_, pk3) = secp.generate_keypair(&mut rng);
let sum = PublicKey::combine_keys(&[&pk1, &pk2, &pk3]).expect("It's improbable to fail for 3 random public keys");
pub fn x_only_public_key(&self) -> (XOnlyPublicKey, Parity)
pub fn x_only_public_key(&self) -> (XOnlyPublicKey, Parity)
Returns the XOnlyPublicKey
(and it’s Parity
) for this PublicKey
.
Trait Implementations§
§impl Deserialize for PublicKey
impl Deserialize for PublicKey
§impl From<PublicKey> for PayeePubKey
impl From<PublicKey> for PayeePubKey
§fn from(pk: PublicKey) -> PayeePubKey
fn from(pk: PublicKey) -> PayeePubKey
§impl From<PublicKey> for XOnlyPublicKey
impl From<PublicKey> for XOnlyPublicKey
§fn from(src: PublicKey) -> XOnlyPublicKey
fn from(src: PublicKey) -> XOnlyPublicKey
§impl Ord for PublicKey
impl Ord for PublicKey
§impl PartialOrd for PublicKey
impl PartialOrd for PublicKey
§impl Writeable for PublicKey
impl Writeable for PublicKey
impl Copy for PublicKey
impl Eq for PublicKey
impl StructuralPartialEq for PublicKey
Auto Trait Implementations§
impl Freeze for PublicKey
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request