Struct secp256k1::PublicKey

source ·
#[repr(transparent)]
pub struct PublicKey(_);
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 u8s 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§

Obtains a raw const pointer suitable for use with FFI functions.

Obtains a raw mutable pointer suitable for use with FFI functions.

Creates a new public key from a SecretKey.

Examples
use secp256k1::{rand, Secp256k1, SecretKey, PublicKey};

let secp = Secp256k1::new();
let secret_key = SecretKey::new(&mut rand::thread_rng());
let public_key = PublicKey::from_secret_key(&secp, &secret_key);

Creates a new public key from a SecretKey and the global SECP256K1 context.

Creates a public key directly from a slice.

Creates a new compressed public key using data from BIP-340 KeyPair.

Examples
use secp256k1::{rand, Secp256k1, PublicKey, KeyPair};

let secp = Secp256k1::new();
let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
let public_key = PublicKey::from_keypair(&key_pair);

Creates a PublicKey using the key material from pk combined with the parity.

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.

Serializes the key as a byte-encoded pair of values, in uncompressed form.

👎Deprecated since 0.23.0: Use negate instead

Negates the public key in place.

Negates the public key.

👎Deprecated since 0.23.0: Use add_exp_tweak instead

Adds other * G to self in place.

Errors

Returns an error if the resulting key would be invalid.

Tweaks a PublicKey by adding tweak * G modulo the curve order.

Errors

Returns an error if the resulting key would be invalid.

👎Deprecated since 0.23.0: Use mul_tweak instead

Muliplies the public key in place by the scalar other.

Errors

Returns an error if the resulting key would be invalid.

Tweaks a PublicKey by multiplying by tweak modulo the curve order.

Errors

Returns an error if the resulting key would be invalid.

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");

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");

Returns the XOnlyPublicKey (and it’s Parity) for this PublicKey.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.

Creates a new public key from a FFI public key

Converts to this type from the input type.
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
Formats the value using the given formatter.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.