pub struct Secp256k1<C>where
C: Context,{ /* private fields */ }
Expand description
The secp256k1 engine, used to execute all signature operations.
Implementations§
Source§impl<C> Secp256k1<C>where
C: Context,
impl<C> Secp256k1<C>where
C: Context,
Sourcepub fn gen_new() -> Secp256k1<C>
pub fn gen_new() -> Secp256k1<C>
Lets you create a context in a generic manner (sign/verify/all).
If rand
and std
feature is enabled, context will have been randomized using
thread_rng
.
If rand
or 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<SignOnly>
impl Secp256k1<SignOnly>
Sourcepub fn signing_only() -> Secp256k1<SignOnly>
pub fn signing_only() -> Secp256k1<SignOnly>
Creates a new Secp256k1 context that can only be used for signing.
If rand
and std
feature is enabled, context will have been randomized using
thread_rng
.
If rand
or std
feature is not enabled please consider randomizing the context (see
docs for Secp256k1::gen_new()
).
Source§impl Secp256k1<VerifyOnly>
impl Secp256k1<VerifyOnly>
Sourcepub fn verification_only() -> Secp256k1<VerifyOnly>
pub fn verification_only() -> Secp256k1<VerifyOnly>
Creates a new Secp256k1 context that can only be used for verification.
If rand
and std
feature is enabled, context will have been randomized using
thread_rng
.
If rand
or std
feature is not enabled please consider randomizing the context (see
docs for Secp256k1::gen_new()
).
Source§impl<'buf, C> Secp256k1<C>where
C: Context + PreallocatedContext<'buf>,
impl<'buf, C> Secp256k1<C>where
C: Context + PreallocatedContext<'buf>,
Sourcepub fn preallocated_gen_new(
buf: &'buf mut [AlignedType],
) -> Result<Secp256k1<C>, Error>
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>>
impl<'buf> Secp256k1<AllPreallocated<'buf>>
Sourcepub fn preallocated_new(
buf: &'buf mut [AlignedType],
) -> Result<Secp256k1<AllPreallocated<'buf>>, Error>
pub fn preallocated_new( buf: &'buf mut [AlignedType], ) -> Result<Secp256k1<AllPreallocated<'buf>>, Error>
Creates a new Secp256k1 context with all capabilities.
Sourcepub fn preallocate_size() -> usize
pub fn preallocate_size() -> usize
Uses the ffi secp256k1_context_preallocated_size
to check the memory size needed for a context.
Sourcepub unsafe fn from_raw_all(
raw_ctx: NonNull<Context>,
) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>>
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 bysecp256k1_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 createraw_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 beffi::secp256k1_context_no_precomp
).
Source§impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>>
impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>>
Sourcepub fn preallocated_signing_only(
buf: &'buf mut [AlignedType],
) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error>
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.
Sourcepub fn preallocate_signing_size() -> usize
pub fn preallocate_signing_size() -> usize
Uses the ffi secp256k1_context_preallocated_size
to check the memory size needed for the context.
Sourcepub unsafe fn from_raw_signing_only(
raw_ctx: NonNull<Context>,
) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>>
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>>
impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>>
Sourcepub fn preallocated_verification_only(
buf: &'buf mut [AlignedType],
) -> Result<Secp256k1<VerifyOnlyPreallocated<'buf>>, Error>
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
Sourcepub fn preallocate_verification_size() -> usize
pub fn preallocate_verification_size() -> usize
Uses the ffi secp256k1_context_preallocated_size
to check the memory size needed for the context.
Sourcepub unsafe fn from_raw_verification_only(
raw_ctx: NonNull<Context>,
) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>>
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> Secp256k1<C>where
C: Signing,
impl<C> Secp256k1<C>where
C: Signing,
Sourcepub fn sign_ecdsa(&self, msg: &Message, sk: &SecretKey) -> Signature
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.
Sourcepub fn sign_ecdsa_with_noncedata(
&self,
msg: &Message,
sk: &SecretKey,
noncedata: &[u8; 32],
) -> Signature
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.
Sourcepub fn sign_ecdsa_grind_r(
&self,
msg: &Message,
sk: &SecretKey,
bytes_to_grind: usize,
) -> Signature
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.
Sourcepub fn sign_ecdsa_low_r(&self, msg: &Message, sk: &SecretKey) -> Signature
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> Secp256k1<C>where
C: Verification,
impl<C> Secp256k1<C>where
C: Verification,
Sourcepub fn verify_ecdsa(
&self,
msg: &Message,
sig: &Signature,
pk: &PublicKey,
) -> Result<(), Error>
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> Secp256k1<C>where
C: Signing,
impl<C> Secp256k1<C>where
C: Signing,
Sourcepub fn sign_schnorr(&self, msg: &[u8], keypair: &Keypair) -> Signature
Available on crate features rand
and std
only.
pub fn sign_schnorr(&self, msg: &[u8], keypair: &Keypair) -> Signature
rand
and std
only.Creates a schnorr signature internally using the rand::rngs::ThreadRng
random number
generator to generate the auxiliary random data.
Sourcepub fn sign_schnorr_no_aux_rand(
&self,
msg: &[u8],
keypair: &Keypair,
) -> Signature
pub fn sign_schnorr_no_aux_rand( &self, msg: &[u8], keypair: &Keypair, ) -> Signature
Creates a schnorr signature without using any auxiliary random data.
Source§impl<C> Secp256k1<C>where
C: Verification,
impl<C> Secp256k1<C>where
C: Verification,
Sourcepub fn verify_schnorr(
&self,
sig: &Signature,
msg: &[u8],
pubkey: &XOnlyPublicKey,
) -> Result<(), Error>
pub fn verify_schnorr( &self, sig: &Signature, msg: &[u8], pubkey: &XOnlyPublicKey, ) -> Result<(), Error>
Verifies a schnorr signature.
Source§impl<C> Secp256k1<C>where
C: Context,
impl<C> Secp256k1<C>where
C: Context,
Sourcepub fn ctx(&self) -> NonNull<Context>
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.
Sourcepub fn preallocate_size_gen() -> usize
pub fn preallocate_size_gen() -> usize
Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all).
Sourcepub fn randomize<R>(&mut self, rng: &mut R)
Available on crate feature rand
only.
pub fn randomize<R>(&mut self, rng: &mut R)
rand
only.(Re)randomizes the Secp256k1 context for extra sidechannel resistance.
Requires compilation with “rand” feature. See comment by Gregory Maxwell in libsecp256k1.
Sourcepub fn seeded_randomize(&mut self, seed: &[u8; 32])
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> Secp256k1<C>where
C: Signing,
impl<C> Secp256k1<C>where
C: Signing,
Sourcepub fn generate_keypair<R>(&self, rng: &mut R) -> (SecretKey, PublicKey)
Available on crate feature rand
only.
pub fn generate_keypair<R>(&self, rng: &mut R) -> (SecretKey, PublicKey)
rand
only.Generates a random keypair. Convenience function for SecretKey::new
and
PublicKey::from_secret_key
.
Trait Implementations§
impl<C> Eq for Secp256k1<C>where
C: Context,
impl<C> Send for Secp256k1<C>where
C: Context,
impl<C> Sync for Secp256k1<C>where
C: Context,
Auto Trait Implementations§
impl<C> Freeze for Secp256k1<C>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.