Skip to content

Commit 3815ee6

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 3815ee6

File tree

4 files changed

+204
-3
lines changed

4 files changed

+204
-3
lines changed

src/ecdsa/mod.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ 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::{
17+
ecdsa, ffi, from_hex, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification,
18+
};
1719

1820
/// An ECDSA signature
1921
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
@@ -341,6 +343,91 @@ pub fn sign_low_r(msg: impl Into<Message>, sk: &SecretKey) -> Signature {
341343
sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
342344
}
343345

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

src/ecdsa/recovery.rs

Lines changed: 48 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,53 @@ 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(
236+
since = "0.32.0",
237+
note = "use RecoverableSignature::sign_ecdsa_recoverable instead"
238+
)]
239+
pub fn sign_ecdsa_recoverable(
240+
&self,
241+
msg: impl Into<Message>,
242+
sk: &key::SecretKey,
243+
) -> RecoverableSignature {
244+
RecoverableSignature::sign_ecdsa_recoverable(msg, sk)
245+
}
246+
247+
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
248+
/// and includes 32 bytes of noncedata in the nonce generation via inclusion in
249+
/// one of the hash operations during nonce generation. This is useful when multiple
250+
/// signatures are needed for the same Message and SecretKey while still using RFC6979.
251+
/// Requires a signing-capable context.
252+
#[deprecated(
253+
since = "0.32.0",
254+
note = "use RecoverableSignature::sign_ecdsa_recoverable_with_noncedata instead"
255+
)]
256+
pub fn sign_ecdsa_recoverable_with_noncedata(
257+
&self,
258+
msg: impl Into<Message>,
259+
sk: &key::SecretKey,
260+
noncedata: &[u8; 32],
261+
) -> RecoverableSignature {
262+
RecoverableSignature::sign_ecdsa_recoverable_with_noncedata(msg, sk, noncedata)
263+
}
264+
}
265+
266+
impl<C: Verification> Secp256k1<C> {
267+
/// Determines the public key for which `sig` is a valid signature for
268+
/// `msg`. Requires a verify-capable context.
269+
#[deprecated(since = "0.32.0", note = "use sig.recover_ecdsa instead")]
270+
pub fn recover_ecdsa(
271+
&self,
272+
msg: impl Into<Message>,
273+
sig: &RecoverableSignature,
274+
) -> Result<key::PublicKey, Error> {
275+
sig.recover_ecdsa(msg)
276+
}
277+
}
278+
232279
#[cfg(test)]
233280
#[allow(unused_imports)]
234281
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.0", 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.0", 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.0", 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.0", 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.0", 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.0", 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)