#[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

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);
This is supported on crate feature global-context only.

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

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.

Negates the public key in place.

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.

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.

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

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

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Creates a new public key from a FFI public key

Performs the conversion.

Performs the conversion.

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 !=.

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

Serialize this value into the given Serde serializer. 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.

Outputs the hash in hexadecimal form

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.