Struct secp256k1::XOnlyPublicKey
source · [−]pub struct XOnlyPublicKey(_);
Expand description
An x-only public key, used for verification of Schnorr signatures and serialized according to BIP-340.
Examples
Basic usage:
use secp256k1::{rand, Secp256k1, KeyPair, XOnlyPublicKey};
let secp = Secp256k1::new();
let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
let xonly = XOnlyPublicKey::from_keypair(&key_pair);
Implementations
sourceimpl XOnlyPublicKey
impl XOnlyPublicKey
sourcepub fn as_ptr(&self) -> *const XOnlyPublicKey
pub fn as_ptr(&self) -> *const XOnlyPublicKey
Obtains a raw const pointer suitable for use with FFI functions.
sourcepub fn as_mut_ptr(&mut self) -> *mut XOnlyPublicKey
pub fn as_mut_ptr(&mut self) -> *mut XOnlyPublicKey
Obtains a raw mutable pointer suitable for use with FFI functions.
sourcepub fn from_keypair(keypair: &KeyPair) -> XOnlyPublicKey
pub fn from_keypair(keypair: &KeyPair) -> XOnlyPublicKey
Creates a new Schnorr public key from a Schnorr key pair.
sourcepub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error>
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error>
Creates a Schnorr public key directly from a slice.
Errors
Returns Error::InvalidPublicKey
if the length of the data slice is not 32 bytes or the
slice does not represent a valid Secp256k1 point x coordinate.
sourcepub fn serialize(&self) -> [u8; 32]
pub fn serialize(&self) -> [u8; 32]
Serializes the key as a byte-encoded x coordinate value (32 bytes).
sourcepub fn tweak_add_assign<V: Verification>(
&mut self,
secp: &Secp256k1<V>,
tweak: &[u8]
) -> Result<Parity, Error>
pub fn tweak_add_assign<V: Verification>(
&mut self,
secp: &Secp256k1<V>,
tweak: &[u8]
) -> Result<Parity, Error>
Tweaks an x-only PublicKey by adding the generator multiplied with the given tweak to it.
Returns
An opaque type representing the parity of the tweaked key, this should be provided to
tweak_add_check
which can be used to verify a tweak more efficiently than regenerating
it and checking equality.
Errors
If the resulting key would be invalid or if the tweak was not a 32-byte length slice.
Examples
use secp256k1::{Secp256k1, KeyPair};
use secp256k1::rand::{RngCore, thread_rng};
let secp = Secp256k1::new();
let mut tweak = [0u8; 32];
thread_rng().fill_bytes(&mut tweak);
let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
let mut public_key = key_pair.public_key();
public_key.tweak_add_assign(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
sourcepub fn tweak_add_check<V: Verification>(
&self,
secp: &Secp256k1<V>,
tweaked_key: &Self,
tweaked_parity: Parity,
tweak: [u8; 32]
) -> bool
pub fn tweak_add_check<V: Verification>(
&self,
secp: &Secp256k1<V>,
tweaked_key: &Self,
tweaked_parity: Parity,
tweak: [u8; 32]
) -> bool
Verifies that a tweak produced by XOnlyPublicKey::tweak_add_assign
was computed correctly.
Should be called on the original untweaked key. Takes the tweaked key and output parity from
XOnlyPublicKey::tweak_add_assign
as input.
Currently this is not much more efficient than just recomputing the tweak and checking equality. However, in future this API will support batch verification, which is significantly faster, so it is wise to design protocols with this in mind.
Returns
True if tweak and check is successful, false otherwise.
Examples
use secp256k1::{Secp256k1, KeyPair};
use secp256k1::rand::{thread_rng, RngCore};
let secp = Secp256k1::new();
let mut tweak = [0u8; 32];
thread_rng().fill_bytes(&mut tweak);
let mut key_pair = KeyPair::new(&secp, &mut thread_rng());
let mut public_key = key_pair.public_key();
let original = public_key;
let parity = public_key.tweak_add_assign(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
assert!(original.tweak_add_check(&secp, &public_key, parity, tweak));
Trait Implementations
sourceimpl CPtr for XOnlyPublicKey
impl CPtr for XOnlyPublicKey
type Target = XOnlyPublicKey
fn as_c_ptr(&self) -> *const Self::Target
fn as_mut_c_ptr(&mut self) -> *mut Self::Target
sourceimpl Clone for XOnlyPublicKey
impl Clone for XOnlyPublicKey
sourcefn clone(&self) -> XOnlyPublicKey
fn clone(&self) -> XOnlyPublicKey
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for XOnlyPublicKey
impl Debug for XOnlyPublicKey
sourceimpl<'de> Deserialize<'de> for XOnlyPublicKey
This is supported on crate feature serde
only.
impl<'de> Deserialize<'de> for XOnlyPublicKey
serde
only.sourcefn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Display for XOnlyPublicKey
impl Display for XOnlyPublicKey
sourceimpl From<PublicKey> for XOnlyPublicKey
impl From<PublicKey> for XOnlyPublicKey
sourcefn from(src: PublicKey) -> XOnlyPublicKey
fn from(src: PublicKey) -> XOnlyPublicKey
Performs the conversion.
sourceimpl From<XOnlyPublicKey> for XOnlyPublicKey
impl From<XOnlyPublicKey> for XOnlyPublicKey
Creates a new Schnorr public key from a FFI x-only public key.
sourcefn from(pk: XOnlyPublicKey) -> XOnlyPublicKey
fn from(pk: XOnlyPublicKey) -> XOnlyPublicKey
Performs the conversion.
sourceimpl FromStr for XOnlyPublicKey
impl FromStr for XOnlyPublicKey
sourceimpl Hash for XOnlyPublicKey
impl Hash for XOnlyPublicKey
sourceimpl LowerHex for XOnlyPublicKey
impl LowerHex for XOnlyPublicKey
sourceimpl Ord for XOnlyPublicKey
impl Ord for XOnlyPublicKey
sourceimpl PartialEq<XOnlyPublicKey> for XOnlyPublicKey
impl PartialEq<XOnlyPublicKey> for XOnlyPublicKey
sourcefn eq(&self, other: &XOnlyPublicKey) -> bool
fn eq(&self, other: &XOnlyPublicKey) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &XOnlyPublicKey) -> bool
fn ne(&self, other: &XOnlyPublicKey) -> bool
This method tests for !=
.
sourceimpl PartialOrd<XOnlyPublicKey> for XOnlyPublicKey
impl PartialOrd<XOnlyPublicKey> for XOnlyPublicKey
sourcefn partial_cmp(&self, other: &XOnlyPublicKey) -> Option<Ordering>
fn partial_cmp(&self, other: &XOnlyPublicKey) -> 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
sourceimpl Serialize for XOnlyPublicKey
This is supported on crate feature serde
only.
impl Serialize for XOnlyPublicKey
serde
only.impl Copy for XOnlyPublicKey
impl Eq for XOnlyPublicKey
impl StructuralEq for XOnlyPublicKey
impl StructuralPartialEq for XOnlyPublicKey
Auto Trait Implementations
impl RefUnwindSafe for XOnlyPublicKey
impl Send for XOnlyPublicKey
impl Sync for XOnlyPublicKey
impl Unpin for XOnlyPublicKey
impl UnwindSafe for XOnlyPublicKey
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