#[repr(transparent)]pub struct PublicKey(_);
Expand description
A Secp256k1 public key, used for verification of signatures.
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
sourceimpl PublicKey
impl PublicKey
sourcepub fn as_ptr(&self) -> *const PublicKey
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
pub fn as_mut_ptr(&mut self) -> *mut PublicKey
Obtains a raw mutable pointer suitable for use with FFI functions.
sourcepub fn from_secret_key_global(sk: &SecretKey) -> PublicKey
This is supported on crate feature global-context
only.
pub fn from_secret_key_global(sk: &SecretKey) -> PublicKey
global-context
only.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 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_assign<C: Verification>(&mut self, secp: &Secp256k1<C>)
pub fn negate_assign<C: Verification>(&mut self, secp: &Secp256k1<C>)
Negates the public key in place.
sourcepub fn add_exp_assign<C: Verification>(
&mut self,
secp: &Secp256k1<C>,
other: &[u8]
) -> Result<(), Error>
pub fn add_exp_assign<C: Verification>(
&mut self,
secp: &Secp256k1<C>,
other: &[u8]
) -> Result<(), Error>
Adds the other
public key to self
in place.
Errors
Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte length slice.
sourcepub fn mul_assign<C: Verification>(
&mut self,
secp: &Secp256k1<C>,
other: &[u8]
) -> Result<(), Error>
pub fn mul_assign<C: Verification>(
&mut self,
secp: &Secp256k1<C>,
other: &[u8]
) -> Result<(), Error>
Muliplies the public key in place by the scalar other
.
Errors
Returns an error if the resulting key would be invalid or if the tweak was not a 32-byte length slice.
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");
Trait Implementations
sourceimpl<'de> Deserialize<'de> for PublicKey
This is supported on crate feature serde
only.
impl<'de> Deserialize<'de> for PublicKey
serde
only.sourcefn deserialize<D: Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error>
Deserialize this value from the given Serde deserializer. Read more
sourceimpl From<PublicKey> for XOnlyPublicKey
impl From<PublicKey> for XOnlyPublicKey
sourcefn from(src: PublicKey) -> XOnlyPublicKey
fn from(src: PublicKey) -> XOnlyPublicKey
Performs the conversion.
sourceimpl Ord for PublicKey
impl Ord for PublicKey
sourceimpl PartialOrd<PublicKey> for PublicKey
impl PartialOrd<PublicKey> for PublicKey
sourcefn partial_cmp(&self, other: &PublicKey) -> Option<Ordering>
fn partial_cmp(&self, other: &PublicKey) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for PublicKey
impl Eq for PublicKey
impl StructuralEq for PublicKey
impl StructuralPartialEq for PublicKey
Auto Trait Implementations
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more