pub struct PublicKey(/* private fields */);
Expand description
Public key - used to verify ECDSA signatures and to do Taproot tweaks.
§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§
source§impl PublicKey
impl PublicKey
sourcepub fn cmp_fast_unstable(&self, other: &Self) -> Ordering
pub fn cmp_fast_unstable(&self, other: &Self) -> Ordering
Like cmp::Cmp
but faster and with no guarantees across library versions.
The Cmp
implementation for FFI types is stable but slow because it first
serializes self
and other
before comparing them. This function provides a faster
comparison if you know that your types come from the same library version.
sourcepub fn eq_fast_unstable(&self, other: &Self) -> bool
pub fn eq_fast_unstable(&self, other: &Self) -> bool
Like cmp::Eq
but faster and with no guarantees across library versions.
The Eq
implementation for FFI types is stable but slow because it first serializes
self
and other
before comparing them. This function provides a faster equality
check if you know that your types come from the same library version.
source§impl PublicKey
impl PublicKey
sourcepub fn as_ptr(&self) -> *const PublicKey
👎Deprecated since 0.25.0: Use Self::as_c_ptr if you need to access the FFI layer
pub fn as_ptr(&self) -> *const PublicKey
Obtains a raw const pointer suitable for use with FFI functions.
sourcepub fn as_mut_ptr(&mut self) -> *mut PublicKey
👎Deprecated since 0.25.0: Use Self::as_mut_c_ptr if you need to access the FFI layer
pub fn as_mut_ptr(&mut self) -> *mut PublicKey
Obtains a raw mutable pointer suitable for use with FFI functions.
sourcepub fn from_ellswift(ellswift: ElligatorSwift) -> PublicKey
pub fn from_ellswift(ellswift: ElligatorSwift) -> PublicKey
Creates a new public key from an ElligatorSwift
.
sourcepub fn from_secret_key_global(sk: &SecretKey) -> PublicKey
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey
sourcepub 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.
sourcepub fn from_keypair(keypair: &Keypair) -> Self
pub fn from_keypair(keypair: &Keypair) -> Self
sourcepub 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
.
sourcepub 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.
sourcepub 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.
sourcepub fn negate<C: Verification>(self, secp: &Secp256k1<C>) -> PublicKey
pub fn negate<C: Verification>(self, secp: &Secp256k1<C>) -> PublicKey
Negates the public key.
sourcepub fn add_exp_tweak<C: Verification>(
self,
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<PublicKey, Error>
pub fn add_exp_tweak<C: Verification>( self, secp: &Secp256k1<C>, tweak: &Scalar, ) -> Result<PublicKey, Error>
sourcepub fn mul_tweak<C: Verification>(
self,
secp: &Secp256k1<C>,
other: &Scalar,
) -> Result<PublicKey, Error>
pub fn mul_tweak<C: Verification>( self, secp: &Secp256k1<C>, other: &Scalar, ) -> Result<PublicKey, Error>
sourcepub 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");
sourcepub 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");
sourcepub 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§
source§impl CPtr for PublicKey
impl CPtr for PublicKey
This trait enables interaction with the FFI layer and even though it is part of the public API normal users should never need to directly interact with FFI types.
source§impl From<PublicKey> for PublicKey
impl From<PublicKey> for PublicKey
Creates a new public key from a FFI public key.
Note, normal users should never need to interact directly with FFI types.
source§impl From<PublicKey> for XOnlyPublicKey
impl From<PublicKey> for XOnlyPublicKey
source§fn from(src: PublicKey) -> XOnlyPublicKey
fn from(src: PublicKey) -> XOnlyPublicKey
source§impl Ord for PublicKey
impl Ord for PublicKey
source§impl PartialEq for PublicKey
impl PartialEq for PublicKey
source§impl PartialOrd for PublicKey
impl PartialOrd for PublicKey
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl 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§
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: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)