Skip to content

Commit 403f541

Browse files
committed
Remove context from verification functions
Done for both `schnorr` and `ecdsa`.
1 parent 074a4ac commit 403f541

File tree

9 files changed

+118
-146
lines changed

9 files changed

+118
-146
lines changed

examples/musig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,5 @@ fn main() {
9696

9797
let aggregated_signature = session.partial_sig_agg(partial_sigs_ref);
9898

99-
assert!(aggregated_signature.verify(&secp, &agg_pk, msg).is_ok());
99+
assert!(aggregated_signature.verify(&agg_pk, msg).is_ok());
100100
}

examples/sign_verify.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
1616
extern crate secp256k1;
1717

18-
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
18+
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing};
1919

20-
fn verify<C: Verification>(
21-
secp: &Secp256k1<C>,
22-
msg_digest: [u8; 32],
23-
sig: [u8; 64],
24-
pubkey: [u8; 33],
25-
) -> Result<bool, Error> {
20+
fn verify(sig: [u8; 64], msg_digest: [u8; 32], pubkey: [u8; 33]) -> Result<bool, Error> {
2621
let msg = Message::from_digest(msg_digest);
2722
let sig = ecdsa::Signature::from_compact(&sig)?;
2823
let pubkey = PublicKey::from_slice(&pubkey)?;
2924

30-
Ok(secp.verify_ecdsa(&sig, msg, &pubkey).is_ok())
25+
Ok(ecdsa::verify(&sig, msg, &pubkey).is_ok())
3126
}
3227

3328
fn sign<C: Signing>(
@@ -57,5 +52,5 @@ fn main() {
5752

5853
let serialize_sig = signature.serialize_compact();
5954

60-
assert!(verify(&secp, msg_digest, serialize_sig, pubkey).unwrap());
55+
assert!(verify(serialize_sig, msg_digest, pubkey).unwrap());
6156
}

no_std_test/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
8383
let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
8484

8585
let sig = secp.sign_ecdsa(message, &secret_key);
86-
assert!(secp.verify_ecdsa(&sig, message, &public_key).is_ok());
86+
assert!(ecdsa::verify(&sig, message, &public_key).is_ok());
8787

8888
let rec_sig = ecdsa::RecoverableSignature::sign_ecdsa_recoverable(message, &secret_key);
89-
assert!(secp.verify_ecdsa(&rec_sig.to_standard(), message, &public_key).is_ok());
89+
assert!(ecdsa::verify(&rec_sig.to_standard(), message, &public_key).is_ok());
9090
assert_eq!(public_key, rec_sig.recover_ecdsa(message).unwrap());
9191
let (rec_id, data) = rec_sig.serialize_compact();
9292
let new_rec_sig = ecdsa::RecoverableSignature::from_compact(&data, rec_id).unwrap();
@@ -110,7 +110,7 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
110110
let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
111111

112112
let sig = secp_alloc.sign_ecdsa(message, &secret_key);
113-
assert!(secp_alloc.verify_ecdsa(&sig, message, &public_key).is_ok());
113+
assert!(ecdsa::verify(&sig, message, &public_key).is_ok());
114114
unsafe { libc::printf("Verified alloc Successfully!\n\0".as_ptr() as _) };
115115
}
116116

src/ecdsa/mod.rs

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +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-
#[cfg(feature = "global-context")]
17-
use crate::SECP256K1;
18-
use crate::{
19-
ffi, from_hex, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification,
20-
};
16+
use crate::{ecdsa, ffi, from_hex, Error, Message, PublicKey, Secp256k1, SecretKey, Signing};
2117

2218
/// An ECDSA signature
2319
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
@@ -190,12 +186,12 @@ impl Signature {
190186
ret
191187
}
192188

193-
/// Verifies an ECDSA signature for `msg` using `pk` and the global [`SECP256K1`] context.
189+
/// Verifies an ECDSA signature for `msg` using `pk`.
190+
///
194191
/// The signature must be normalized or verification will fail (see [`Signature::normalize_s`]).
195192
#[inline]
196-
#[cfg(feature = "global-context")]
197193
pub fn verify(&self, msg: impl Into<Message>, pk: &PublicKey) -> Result<(), Error> {
198-
SECP256K1.verify_ecdsa(self, msg, pk)
194+
ecdsa::verify(self, msg, pk)
199195
}
200196
}
201197

@@ -359,48 +355,48 @@ impl<C: Signing> Secp256k1<C> {
359355
}
360356
}
361357

362-
impl<C: Verification> Secp256k1<C> {
363-
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
364-
/// key `pubkey`. Returns `Ok(())` on success. Note that this function cannot
365-
/// be used for Bitcoin consensus checking since there may exist signatures
366-
/// which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a
367-
/// verify-capable context.
368-
///
369-
/// ```rust
370-
/// # #[cfg(all(feature = "rand", feature = "std"))] {
371-
/// # use secp256k1::{rand, Secp256k1, Message, Error};
372-
/// #
373-
/// # let secp = Secp256k1::new();
374-
/// # let (secret_key, public_key) = secp256k1::generate_keypair(&mut rand::rng());
375-
/// #
376-
/// let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
377-
/// let sig = secp.sign_ecdsa(message, &secret_key);
378-
/// assert_eq!(secp.verify_ecdsa(&sig, message, &public_key), Ok(()));
379-
///
380-
/// let message = Message::from_digest_slice(&[0xcd; 32]).expect("32 bytes");
381-
/// assert_eq!(secp.verify_ecdsa(&sig, message, &public_key), Err(Error::IncorrectSignature));
382-
/// # }
383-
/// ```
384-
#[inline]
385-
pub fn verify_ecdsa(
386-
&self,
387-
sig: &Signature,
388-
msg: impl Into<Message>,
389-
pk: &PublicKey,
390-
) -> Result<(), Error> {
391-
let msg = msg.into();
392-
unsafe {
393-
if ffi::secp256k1_ecdsa_verify(
394-
self.ctx.as_ptr(),
395-
sig.as_c_ptr(),
396-
msg.as_c_ptr(),
397-
pk.as_c_ptr(),
398-
) == 0
399-
{
400-
Err(Error::IncorrectSignature)
401-
} else {
402-
Ok(())
403-
}
358+
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
359+
/// key `pubkey`. Returns `Ok(())` on success. Note that this function cannot
360+
/// be used for Bitcoin consensus checking since there may exist signatures
361+
/// which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a
362+
/// verify-capable context.
363+
///
364+
/// ```rust
365+
/// # #[cfg(all(feature = "rand", feature = "std"))] {
366+
/// # use secp256k1::{rand, ecdsa, Secp256k1, Message, Error};
367+
/// #
368+
/// # let secp = Secp256k1::new();
369+
/// # let (secret_key, public_key) = secp256k1::generate_keypair(&mut rand::rng());
370+
/// #
371+
/// let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
372+
/// let sig = secp.sign_ecdsa(message, &secret_key);
373+
/// assert_eq!(ecdsa::verify(&sig, message, &public_key), Ok(()));
374+
///
375+
/// let message = Message::from_digest_slice(&[0xcd; 32]).expect("32 bytes");
376+
/// assert_eq!(ecdsa::verify(&sig, message, &public_key), Err(Error::IncorrectSignature));
377+
/// # }
378+
/// ```
379+
#[inline]
380+
pub fn verify(sig: &Signature, msg: impl Into<Message>, pk: &PublicKey) -> Result<(), Error> {
381+
let msg = msg.into();
382+
// We have no seed here but we want rerandomiziation to happen for `rand` users.
383+
let seed = [0_u8; 32];
384+
unsafe {
385+
let res = crate::with_global_context(
386+
|secp: &Secp256k1<crate::AllPreallocated>| {
387+
ffi::secp256k1_ecdsa_verify(
388+
secp.ctx.as_ptr(),
389+
sig.as_c_ptr(),
390+
msg.as_c_ptr(),
391+
pk.as_c_ptr(),
392+
)
393+
},
394+
Some(&seed),
395+
);
396+
if res == 0 {
397+
Err(Error::IncorrectSignature)
398+
} else {
399+
Ok(())
404400
}
405401
}
406402
}

src/ecdsa/recovery.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ mod tests {
250250

251251
use super::*;
252252
use crate::constants::ONE;
253-
use crate::{Error, Message, Secp256k1, SecretKey};
253+
use crate::{ecdsa, Error, Message, Secp256k1, SecretKey};
254254

255255
#[test]
256256
fn capabilities() {
@@ -313,16 +313,14 @@ mod tests {
313313
#[test]
314314
#[cfg(feature = "std")]
315315
fn sign_and_verify_fail() {
316-
let s = Secp256k1::new();
317-
318316
let msg = Message::from_digest(crate::test_random_32_bytes());
319317
let (sk, pk) = crate::test_random_keypair();
320318

321319
let sigr = RecoverableSignature::sign_ecdsa_recoverable(msg, &sk);
322320
let sig = sigr.to_standard();
323321

324322
let msg = Message::from_digest(crate::test_random_32_bytes());
325-
assert_eq!(s.verify_ecdsa(&sig, msg, &pk), Err(Error::IncorrectSignature));
323+
assert_eq!(ecdsa::verify(&sig, msg, &pk), Err(Error::IncorrectSignature));
326324

327325
let recovered_key = sigr.recover_ecdsa(msg).unwrap();
328326
assert!(recovered_key != pk);

src/key/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,8 @@ impl PublicKey {
416416
}
417417

418418
/// Checks that `sig` is a valid ECDSA signature for `msg` using this public key.
419-
pub fn verify<C: Verification>(
420-
&self,
421-
secp: &Secp256k1<C>,
422-
msg: impl Into<Message>,
423-
sig: &ecdsa::Signature,
424-
) -> Result<(), Error> {
425-
secp.verify_ecdsa(sig, msg, self)
419+
pub fn verify(&self, msg: impl Into<Message>, sig: &ecdsa::Signature) -> Result<(), Error> {
420+
ecdsa::verify(sig, msg, self)
426421
}
427422
}
428423

@@ -1110,11 +1105,10 @@ impl XOnlyPublicKey {
11101105
/// Checks that `sig` is a valid schnorr signature for `msg` using this public key.
11111106
pub fn verify<C: Verification>(
11121107
&self,
1113-
secp: &Secp256k1<C>,
11141108
msg: &[u8],
11151109
sig: &schnorr::Signature,
11161110
) -> Result<(), Error> {
1117-
secp.verify_schnorr(sig, msg, self)
1111+
schnorr::verify(sig, msg, self)
11181112
}
11191113
}
11201114

0 commit comments

Comments
 (0)