@@ -28,6 +28,11 @@ use Verification;
28
28
use constants;
29
29
use ffi:: { self , CPtr } ;
30
30
31
+ #[ cfg( feature = "global-context" ) ]
32
+ use { Message , ecdsa, SECP256K1 } ;
33
+ #[ cfg( all( feature = "global-context" , feature = "rand-std" ) ) ]
34
+ use schnorr;
35
+
31
36
/// Secret 256-bit key used as `x` in an ECDSA signature.
32
37
///
33
38
/// # Examples
@@ -279,6 +284,14 @@ impl SecretKey {
279
284
}
280
285
}
281
286
}
287
+
288
+ /// Constructs an ECDSA signature for `msg` using the global [`SECP256K1`] context.
289
+ #[ inline]
290
+ #[ cfg( feature = "global-context" ) ]
291
+ #[ cfg_attr( docsrs, doc( cfg( feature = "global-context" ) ) ) ]
292
+ pub fn sign_ecdsa ( & self , msg : Message ) -> ecdsa:: Signature {
293
+ SECP256K1 . sign_ecdsa ( & msg, self )
294
+ }
282
295
}
283
296
284
297
#[ cfg( feature = "serde" ) ]
@@ -349,6 +362,14 @@ impl PublicKey {
349
362
}
350
363
}
351
364
365
+ /// Creates a new public key from a [`SecretKey`] and the global [`SECP256K1`] context.
366
+ #[ inline]
367
+ #[ cfg( feature = "global-context" ) ]
368
+ #[ cfg_attr( docsrs, doc( cfg( feature = "global-context" ) ) ) ]
369
+ pub fn from_secret_key_global ( sk : & SecretKey ) -> PublicKey {
370
+ PublicKey :: from_secret_key ( & SECP256K1 , sk)
371
+ }
372
+
352
373
/// Creates a public key directly from a slice.
353
374
#[ inline]
354
375
pub fn from_slice ( data : & [ u8 ] ) -> Result < PublicKey , Error > {
@@ -738,6 +759,18 @@ impl KeyPair {
738
759
}
739
760
}
740
761
762
+ /// Creates a Schnorr [`KeyPair`] directly from a secret key string and the global [`SECP256K1`] context.
763
+ ///
764
+ /// # Errors
765
+ ///
766
+ /// [`Error::InvalidSecretKey`] if corresponding public key for the provided secret key is not even.
767
+ #[ inline]
768
+ #[ cfg( feature = "global-context" ) ]
769
+ #[ cfg_attr( docsrs, doc( cfg( feature = "global-context" ) ) ) ]
770
+ pub fn from_seckey_str_global ( s : & str ) -> Result < KeyPair , Error > {
771
+ KeyPair :: from_seckey_str ( SECP256K1 , s)
772
+ }
773
+
741
774
/// Generates a new random secret key.
742
775
/// # Examples
743
776
///
@@ -768,6 +801,14 @@ impl KeyPair {
768
801
}
769
802
}
770
803
804
+ /// Generates a new random secret key using the global [`SECP256K1`] context.
805
+ #[ inline]
806
+ #[ cfg( all( feature = "global-context" , feature = "rand" ) ) ]
807
+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "global-context" , feature = "rand" ) ) ) ) ]
808
+ pub fn new_global < R : :: rand:: Rng + ?Sized > ( rng : & mut R ) -> KeyPair {
809
+ KeyPair :: new ( SECP256K1 , rng)
810
+ }
811
+
771
812
/// Serializes the key pair as a secret key byte value.
772
813
#[ inline]
773
814
pub fn serialize_secret ( & self ) -> [ u8 ; constants:: SECRET_KEY_SIZE ] {
@@ -830,6 +871,14 @@ impl KeyPair {
830
871
pub fn public_key ( & self ) -> XOnlyPublicKey {
831
872
XOnlyPublicKey :: from_keypair ( self )
832
873
}
874
+
875
+ /// Constructs an schnorr signature for `msg` using the global [`SECP256K1`] context.
876
+ #[ inline]
877
+ #[ cfg( all( feature = "global-context" , feature = "rand-std" ) ) ]
878
+ #[ cfg_attr( docsrs, doc( cfg( all( feature = "global-context" , feature = "rand-std" ) ) ) ) ]
879
+ pub fn sign_schnorr ( & self , msg : Message ) -> schnorr:: Signature {
880
+ SECP256K1 . sign_schnorr ( & msg, self )
881
+ }
833
882
}
834
883
835
884
impl From < KeyPair > for SecretKey {
0 commit comments