Skip to content

Commit 0300949

Browse files
committed
Put back in deprecated context methods
During the removal of the context from the public API I brashly deleted a bunch of impls on the `Secp256k1` type when we could have just deprecated them. This means that when upgrading downstream crates the upgrade is way more painful than it is if we deprecate. Add back in the context impls. To do this I copied from `v0.31.1`. I'm not totally sure that there wasn't changes to this code in `master` but since we are explicitly doing this to help upgrade then it seems reasonable to do it like this. Add deprecated since 0.32 attributes because we are going to push this as a `0.32.0-beta.1` release. Sorry for the pain in reviewing this, I'm not entirely sure how you are supposed to convince yourself that the code is exactly as it was?
1 parent 4cfcc4b commit 0300949

File tree

4 files changed

+194
-3
lines changed

4 files changed

+194
-3
lines changed

src/ecdsa/mod.rs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::{fmt, ptr, str};
1313
pub use self::recovery::{RecoverableSignature, RecoveryId};
1414
pub use self::serialized_signature::SerializedSignature;
1515
use crate::ffi::CPtr;
16-
use crate::{ecdsa, ffi, from_hex, Error, Message, PublicKey, Secp256k1, SecretKey};
16+
use crate::{ecdsa, ffi, from_hex, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
1717

1818
/// An ECDSA signature
1919
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
@@ -341,6 +341,91 @@ pub fn sign_low_r(msg: impl Into<Message>, sk: &SecretKey) -> Signature {
341341
sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
342342
}
343343

344+
impl<C: Signing> Secp256k1<C> {
345+
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
346+
/// Requires a signing-capable context.
347+
#[deprecated(since = "0.32", note = "use ecdsa::sign instead")]
348+
pub fn sign_ecdsa(&self, msg: impl Into<Message>, sk: &SecretKey) -> Signature {
349+
self::sign(msg, sk)
350+
}
351+
352+
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
353+
/// and includes 32 bytes of noncedata in the nonce generation via inclusion in
354+
/// one of the hash operations during nonce generation. This is useful when multiple
355+
/// signatures are needed for the same Message and SecretKey while still using RFC6979.
356+
/// Requires a signing-capable context.
357+
#[deprecated(since = "0.32", note = "use ecdsa::sign_with_noncedata instead")]
358+
pub fn sign_ecdsa_with_noncedata(
359+
&self,
360+
msg: impl Into<Message>,
361+
sk: &SecretKey,
362+
noncedata: &[u8; 32],
363+
) -> Signature {
364+
self::sign_with_noncedata(msg, sk, noncedata)
365+
}
366+
367+
/// Constructs a signature for `msg` using the secret key `sk`, RFC6979 nonce
368+
/// and "grinds" the nonce by passing extra entropy if necessary to produce
369+
/// a signature that is less than 71 - `bytes_to_grind` bytes. The number
370+
/// of signing operation performed by this function is exponential in the
371+
/// number of bytes grinded.
372+
/// Requires a signing capable context.
373+
#[deprecated(since = "0.32", note = "use ecdsa::sign_grind_r instead")]
374+
pub fn sign_ecdsa_grind_r(
375+
&self,
376+
msg: impl Into<Message>,
377+
sk: &SecretKey,
378+
bytes_to_grind: usize,
379+
) -> Signature {
380+
self::sign_grind_r(msg, sk, bytes_to_grind)
381+
}
382+
383+
/// Constructs a signature for `msg` using the secret key `sk`, RFC6979 nonce
384+
/// and "grinds" the nonce by passing extra entropy if necessary to produce
385+
/// a signature that is less than 71 bytes and compatible with the low r
386+
/// signature implementation of bitcoin core. In average, this function
387+
/// will perform two signing operations.
388+
/// Requires a signing capable context.
389+
#[deprecated(since = "0.32", note = "use ecdsa::sign_low_r instead")]
390+
pub fn sign_ecdsa_low_r(&self, msg: impl Into<Message>, sk: &SecretKey) -> Signature {
391+
self::sign_low_r(msg, sk)
392+
}
393+
}
394+
395+
impl<C: Verification> Secp256k1<C> {
396+
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
397+
/// key `pubkey`. Returns `Ok(())` on success. Note that this function cannot
398+
/// be used for Bitcoin consensus checking since there may exist signatures
399+
/// which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a
400+
/// verify-capable context.
401+
///
402+
/// ```rust
403+
/// # #[cfg(all(feature = "rand", feature = "std"))] {
404+
/// # use secp256k1::{rand, Secp256k1, Message, Error};
405+
/// #
406+
/// # let secp = Secp256k1::new();
407+
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rand::rng());
408+
/// #
409+
/// let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
410+
/// let sig = secp.sign_ecdsa(message, &secret_key);
411+
/// assert_eq!(secp.verify_ecdsa(&sig, message, &public_key), Ok(()));
412+
///
413+
/// let message = Message::from_digest_slice(&[0xcd; 32]).expect("32 bytes");
414+
/// assert_eq!(secp.verify_ecdsa(&sig, message, &public_key), Err(Error::IncorrectSignature));
415+
/// # }
416+
/// ```
417+
#[inline]
418+
#[deprecated(since = "0.32", note = "use ecdsa::verify instead")]
419+
pub fn verify_ecdsa(
420+
&self,
421+
sig: &Signature,
422+
msg: impl Into<Message>,
423+
pk: &PublicKey,
424+
) -> Result<(), Error> {
425+
self::verify(sig, msg, pk)
426+
}
427+
}
428+
344429
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
345430
/// key `pubkey`. Returns `Ok(())` on success. Note that this function cannot
346431
/// be used for Bitcoin consensus checking since there may exist signatures

src/ecdsa/recovery.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use self::super_ffi::CPtr;
1010
use super::ffi as super_ffi;
1111
use crate::ecdsa::Signature;
1212
use crate::ffi::recovery as ffi;
13-
use crate::{key, Error, Message};
13+
use crate::{key, Error, Message, Secp256k1, Signing, Verification};
1414

1515
/// A tag used for recovering the public key from a compact signature.
1616
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -229,6 +229,45 @@ impl RecoverableSignature {
229229
}
230230
}
231231

232+
impl<C: Signing> Secp256k1<C> {
233+
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
234+
/// Requires a signing-capable context.
235+
#[deprecated(since = "0.32", note = "use RecoverableSignature::sign_ecdsa_recoverable instead")]
236+
pub fn sign_ecdsa_recoverable(
237+
&self,
238+
msg: impl Into<Message>,
239+
sk: &key::SecretKey,
240+
) -> RecoverableSignature {
241+
RecoverableSignature::sign_ecdsa_recoverable(msg, sk)
242+
}
243+
244+
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
245+
/// and includes 32 bytes of noncedata in the nonce generation via inclusion in
246+
/// one of the hash operations during nonce generation. This is useful when multiple
247+
/// signatures are needed for the same Message and SecretKey while still using RFC6979.
248+
/// Requires a signing-capable context.
249+
#[deprecated(since = "0.32", note = "use RecoverableSignature::sign_ecdsa_recoverable_with_noncedata instead")]
250+
pub fn sign_ecdsa_recoverable_with_noncedata(
251+
&self,
252+
msg: impl Into<Message>,
253+
sk: &key::SecretKey,
254+
noncedata: &[u8; 32],
255+
) -> RecoverableSignature {
256+
RecoverableSignature::sign_ecdsa_recoverable_with_noncedata(msg, sk, noncedata)
257+
}
258+
}
259+
260+
impl<C: Verification> Secp256k1<C> {
261+
/// Determines the public key for which `sig` is a valid signature for
262+
/// `msg`. Requires a verify-capable context.
263+
#[deprecated(since = "0.32", note = "use sig.recover_ecdsa instead")]
264+
pub fn recover_ecdsa(
265+
&self,
266+
msg: impl Into<Message>,
267+
sig: &RecoverableSignature,
268+
) -> Result<key::PublicKey, Error> { sig.recover_ecdsa(msg) }
269+
}
270+
232271
#[cfg(test)]
233272
#[allow(unused_imports)]
234273
mod tests {

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,20 @@ impl<C: Context> Secp256k1<C> {
410410
}
411411
}
412412

413+
impl<C: Signing> Secp256k1<C> {
414+
/// Generates a random keypair. Convenience function for [`SecretKey::new`] and
415+
/// [`PublicKey::from_secret_key`].
416+
#[inline]
417+
#[cfg(feature = "rand")]
418+
#[deprecated(since = "0.32", note = "use secp256k1::generate_keypair instead")]
419+
pub fn generate_keypair<R: rand::Rng + ?Sized>(
420+
&self,
421+
rng: &mut R,
422+
) -> (key::SecretKey, key::PublicKey) {
423+
generate_keypair(rng)
424+
}
425+
}
426+
413427
/// Generates a random keypair. Convenience function for [`SecretKey::new`] and
414428
/// [`PublicKey::from_secret_key`].
415429
#[inline]

src/schnorr.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use secp256k1_sys::SchnorrSigExtraParams;
1111

1212
use crate::ffi::{self, CPtr};
1313
use crate::key::{Keypair, XOnlyPublicKey};
14-
use crate::{constants, from_hex, Error, Secp256k1};
14+
use crate::{constants, from_hex, Error, Secp256k1, Signing, Verification};
1515

1616
/// Represents a schnorr signature.
1717
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -183,6 +183,59 @@ pub fn verify(sig: &Signature, msg: &[u8], pubkey: &XOnlyPublicKey) -> Result<()
183183
}
184184
}
185185

186+
impl<C: Signing> Secp256k1<C> {
187+
/// Creates a schnorr signature internally using the [`rand::rngs::ThreadRng`] random number
188+
/// generator to generate the auxiliary random data.
189+
#[cfg(all(feature = "rand", feature = "std"))]
190+
#[deprecated(since = "0.32", note = "use schnorr::sign instead")]
191+
pub fn sign_schnorr(&self, msg: &[u8], keypair: &Keypair) -> Signature {
192+
self::sign(msg, keypair)
193+
}
194+
195+
/// Creates a schnorr signature without using any auxiliary random data.
196+
#[deprecated(since = "0.32", note = "use schnorr::sign_no_aux_rand instead")]
197+
pub fn sign_schnorr_no_aux_rand(&self, msg: &[u8], keypair: &Keypair) -> Signature {
198+
self::sign_no_aux_rand(msg, keypair)
199+
}
200+
201+
/// Creates a schnorr signature using the given auxiliary random data.
202+
#[deprecated(since = "0.32", note = "use schnorr::sign_with_aux_rand instead")]
203+
pub fn sign_schnorr_with_aux_rand(
204+
&self,
205+
msg: &[u8],
206+
keypair: &Keypair,
207+
aux_rand: &[u8; 32],
208+
) -> Signature {
209+
self::sign_with_aux_rand(msg, keypair, aux_rand)
210+
}
211+
212+
/// Creates a schnorr signature using the given random number generator to
213+
/// generate the auxiliary random data.
214+
#[cfg(feature = "rand")]
215+
#[deprecated(since = "0.32", note = "use schnorr::sign_with_rng instead")]
216+
pub fn sign_schnorr_with_rng<R: Rng + CryptoRng>(
217+
&self,
218+
msg: &[u8],
219+
keypair: &Keypair,
220+
rng: &mut R,
221+
) -> Signature {
222+
self::sign_with_rng(msg, keypair, rng)
223+
}
224+
}
225+
226+
impl<C: Verification> Secp256k1<C> {
227+
/// Verifies a schnorr signature.
228+
#[deprecated(since = "0.32", note = "use schnorr::verify instead")]
229+
pub fn verify_schnorr(
230+
&self,
231+
sig: &Signature,
232+
msg: &[u8],
233+
pubkey: &XOnlyPublicKey,
234+
) -> Result<(), Error> {
235+
self::verify(sig, msg, pubkey)
236+
}
237+
}
238+
186239
#[cfg(test)]
187240
#[allow(unused_imports)]
188241
mod tests {

0 commit comments

Comments
 (0)