pub struct Secp256k1<C: Context> { /* private fields */ }
Expand description

The secp256k1 engine, used to execute all signature operations.

Implementations§

source§

impl<C: Context> Secp256k1<C>

source

pub fn gen_new() -> Secp256k1<C>

Available on crate feature alloc only.

Lets you create a context in a generic manner (sign/verify/all).

If rand-std feature is enabled, context will have been randomized using thread_rng. If rand-std feature is not enabled please consider randomizing the context as follows:

let mut ctx = Secp256k1::new();
// let seed = <32 bytes of random data>
ctx.seeded_randomize(&seed);
source§

impl Secp256k1<All>

source

pub fn new() -> Secp256k1<All>

Available on crate feature alloc only.

Creates a new Secp256k1 context with all capabilities.

If rand-std feature is enabled, context will have been randomized using thread_rng. If rand-std feature is not enabled please consider randomizing the context (see docs for Secp256k1::gen_new()).

source§

impl Secp256k1<SignOnly>

source

pub fn signing_only() -> Secp256k1<SignOnly>

Available on crate feature alloc only.

Creates a new Secp256k1 context that can only be used for signing.

If rand-std feature is enabled, context will have been randomized using thread_rng. If rand-std feature is not enabled please consider randomizing the context (see docs for Secp256k1::gen_new()).

source§

impl Secp256k1<VerifyOnly>

source

pub fn verification_only() -> Secp256k1<VerifyOnly>

Available on crate feature alloc only.

Creates a new Secp256k1 context that can only be used for verification.

  • If rand-std feature is enabled, context will have been randomized using thread_rng.
  • If rand-std feature is not enabled please consider randomizing the context (see docs for Secp256k1::gen_new()).
source§

impl<'buf, C: Context + PreallocatedContext<'buf>> Secp256k1<C>

source

pub fn preallocated_gen_new( buf: &'buf mut [AlignedType] ) -> Result<Secp256k1<C>, Error>

Lets you create a context with a preallocated buffer in a generic manner (sign/verify/all).

source§

impl<'buf> Secp256k1<AllPreallocated<'buf>>

source

pub fn preallocated_new( buf: &'buf mut [AlignedType] ) -> Result<Secp256k1<AllPreallocated<'buf>>, Error>

Creates a new Secp256k1 context with all capabilities.

source

pub fn preallocate_size() -> usize

Uses the ffi secp256k1_context_preallocated_size to check the memory size needed for a context.

source

pub unsafe fn from_raw_all( raw_ctx: NonNull<Context> ) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>>

Creates a context from a raw context.

The returned core::mem::ManuallyDrop context will never deallocate the memory pointed to by raw_ctx nor destroy the context. This may lead to memory leaks. ManuallyDrop::drop (or core::ptr::drop_in_place) will only destroy the context; the caller is required to free the memory.

Safety

This is highly unsafe due to a number of conditions that aren’t checked, specifically:

  • raw_ctx must be a valid pointer (live, aligned…) to memory that was initialized by secp256k1_context_preallocated_create (either called directly or from this library by one of the context creation methods - all of which call it internally).
  • The version of libsecp256k1 used to create raw_ctx must be exactly the one linked into this library.
  • The lifetime of the raw_ctx pointer must outlive 'buf.
  • raw_ctx must point to writable memory (cannot be ffi::secp256k1_context_no_precomp).
source§

impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>>

source

pub fn preallocated_signing_only( buf: &'buf mut [AlignedType] ) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error>

Creates a new Secp256k1 context that can only be used for signing.

source

pub fn preallocate_signing_size() -> usize

Uses the ffi secp256k1_context_preallocated_size to check the memory size needed for the context.

source

pub unsafe fn from_raw_signing_only( raw_ctx: NonNull<Context> ) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>>

Creates a context from a raw context that can only be used for signing.

Safety

Please see Secp256k1::from_raw_all for full documentation and safety requirements.

source§

impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>>

source

pub fn preallocated_verification_only( buf: &'buf mut [AlignedType] ) -> Result<Secp256k1<VerifyOnlyPreallocated<'buf>>, Error>

Creates a new Secp256k1 context that can only be used for verification

source

pub fn preallocate_verification_size() -> usize

Uses the ffi secp256k1_context_preallocated_size to check the memory size needed for the context.

source

pub unsafe fn from_raw_verification_only( raw_ctx: NonNull<Context> ) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>>

Creates a context from a raw context that can only be used for verification.

Safety

Please see Secp256k1::from_raw_all for full documentation and safety requirements.

source§

impl<C: Signing> Secp256k1<C>

source

pub fn sign_ecdsa_recoverable( &self, msg: &Message, sk: &SecretKey ) -> RecoverableSignature

Available on crate feature recovery only.

Constructs a signature for msg using the secret key sk and RFC6979 nonce Requires a signing-capable context.

source

pub fn sign_ecdsa_recoverable_with_noncedata( &self, msg: &Message, sk: &SecretKey, noncedata: &[u8; 32] ) -> RecoverableSignature

Available on crate feature recovery only.

Constructs a signature for msg using the secret key sk and RFC6979 nonce and includes 32 bytes of noncedata in the nonce generation via inclusion in one of the hash operations during nonce generation. This is useful when multiple signatures are needed for the same Message and SecretKey while still using RFC6979. Requires a signing-capable context.

source§

impl<C: Verification> Secp256k1<C>

source

pub fn recover_ecdsa( &self, msg: &Message, sig: &RecoverableSignature ) -> Result<PublicKey, Error>

Available on crate feature recovery only.

Determines the public key for which sig is a valid signature for msg. Requires a verify-capable context.

source§

impl<C: Signing> Secp256k1<C>

source

pub fn sign_ecdsa(&self, msg: &Message, sk: &SecretKey) -> Signature

Constructs a signature for msg using the secret key sk and RFC6979 nonce Requires a signing-capable context.

source

pub fn sign_ecdsa_with_noncedata( &self, msg: &Message, sk: &SecretKey, noncedata: &[u8; 32] ) -> Signature

Constructs a signature for msg using the secret key sk and RFC6979 nonce and includes 32 bytes of noncedata in the nonce generation via inclusion in one of the hash operations during nonce generation. This is useful when multiple signatures are needed for the same Message and SecretKey while still using RFC6979. Requires a signing-capable context.

source

pub fn sign_ecdsa_grind_r( &self, msg: &Message, sk: &SecretKey, bytes_to_grind: usize ) -> Signature

Constructs a signature for msg using the secret key sk, RFC6979 nonce and “grinds” the nonce by passing extra entropy if necessary to produce a signature that is less than 71 - bytes_to_grind bytes. The number of signing operation performed by this function is exponential in the number of bytes grinded. Requires a signing capable context.

source

pub fn sign_ecdsa_low_r(&self, msg: &Message, sk: &SecretKey) -> Signature

Constructs a signature for msg using the secret key sk, RFC6979 nonce and “grinds” the nonce by passing extra entropy if necessary to produce a signature that is less than 71 bytes and compatible with the low r signature implementation of bitcoin core. In average, this function will perform two signing operations. Requires a signing capable context.

source§

impl<C: Verification> Secp256k1<C>

source

pub fn verify_ecdsa( &self, msg: &Message, sig: &Signature, pk: &PublicKey ) -> Result<(), Error>

Checks that sig is a valid ECDSA signature for msg using the public key pubkey. Returns Ok(()) on success. Note that this function cannot be used for Bitcoin consensus checking since there may exist signatures which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a verify-capable context.

let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
let sig = secp.sign_ecdsa(&message, &secret_key);
assert_eq!(secp.verify_ecdsa(&message, &sig, &public_key), Ok(()));

let message = Message::from_digest_slice(&[0xcd; 32]).expect("32 bytes");
assert_eq!(secp.verify_ecdsa(&message, &sig, &public_key), Err(Error::IncorrectSignature));
source§

impl<C: Signing> Secp256k1<C>

source

pub fn sign_schnorr(&self, msg: &Message, keypair: &Keypair) -> Signature

Available on crate feature rand-std only.

Creates a schnorr signature internally using the rand::rngs::ThreadRng random number generator to generate the auxiliary random data.

source

pub fn sign_schnorr_no_aux_rand( &self, msg: &Message, keypair: &Keypair ) -> Signature

Creates a schnorr signature without using any auxiliary random data.

source

pub fn sign_schnorr_with_aux_rand( &self, msg: &Message, keypair: &Keypair, aux_rand: &[u8; 32] ) -> Signature

Creates a schnorr signature using the given auxiliary random data.

source

pub fn sign_schnorr_with_rng<R: Rng + CryptoRng>( &self, msg: &Message, keypair: &Keypair, rng: &mut R ) -> Signature

Available on crate feature rand only.

Creates a schnorr signature using the given random number generator to generate the auxiliary random data.

source§

impl<C: Verification> Secp256k1<C>

source

pub fn verify_schnorr( &self, sig: &Signature, msg: &Message, pubkey: &XOnlyPublicKey ) -> Result<(), Error>

Verifies a schnorr signature.

source§

impl<C: Context> Secp256k1<C>

source

pub fn ctx(&self) -> NonNull<Context>

Getter for the raw pointer to the underlying secp256k1 context. This shouldn’t be needed with normal usage of the library. It enables extending the Secp256k1 with more cryptographic algorithms outside of this crate.

source

pub fn preallocate_size_gen() -> usize

Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all).

source

pub fn randomize<R: Rng + ?Sized>(&mut self, rng: &mut R)

Available on crate feature rand only.

(Re)randomizes the Secp256k1 context for extra sidechannel resistance.

Requires compilation with “rand” feature. See comment by Gregory Maxwell in libsecp256k1.

source

pub fn seeded_randomize(&mut self, seed: &[u8; 32])

(Re)randomizes the Secp256k1 context for extra sidechannel resistance given 32 bytes of cryptographically-secure random data; see comment in libsecp256k1 commit d2275795f by Gregory Maxwell.

source§

impl<C: Signing> Secp256k1<C>

source

pub fn generate_keypair<R: Rng + ?Sized>( &self, rng: &mut R ) -> (SecretKey, PublicKey)

Available on crate feature rand only.

Generates a random keypair. Convenience function for SecretKey::new and PublicKey::from_secret_key.

Trait Implementations§

source§

impl<C: Context> Clone for Secp256k1<C>

Available on crate feature alloc only.
source§

fn clone(&self) -> Secp256k1<C>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<C: Context> Debug for Secp256k1<C>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Secp256k1<All>

Available on crate feature alloc only.
source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<C: Context> Drop for Secp256k1<C>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<C: Context> PartialEq for Secp256k1<C>

source§

fn eq(&self, _other: &Secp256k1<C>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<C: Context> Eq for Secp256k1<C>

source§

impl<C: Context> Send for Secp256k1<C>

source§

impl<C: Context> Sync for Secp256k1<C>

Auto Trait Implementations§

§

impl<C> RefUnwindSafe for Secp256k1<C>
where C: RefUnwindSafe,

§

impl<C> Unpin for Secp256k1<C>
where C: Unpin,

§

impl<C> UnwindSafe for Secp256k1<C>
where C: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V