@@ -13,11 +13,7 @@ use core::{fmt, ptr, str};
1313pub use self :: recovery:: { RecoverableSignature , RecoveryId } ;
1414pub use self :: serialized_signature:: SerializedSignature ;
1515use 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}
0 commit comments