@@ -681,12 +681,15 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
681
681
mod tests {
682
682
use rand:: { RngCore , thread_rng} ;
683
683
use std:: str:: FromStr ;
684
+ use std:: marker:: PhantomData ;
684
685
685
686
use key:: { SecretKey , PublicKey } ;
686
687
use super :: from_hex;
687
688
use super :: constants;
688
689
use super :: { Secp256k1 , Signature , Message } ;
689
690
use super :: Error :: { InvalidMessage , IncorrectSignature , InvalidSignature } ;
691
+ use ffi;
692
+ use context:: * ;
690
693
691
694
macro_rules! hex {
692
695
( $hex: expr) => ( {
@@ -696,6 +699,35 @@ mod tests {
696
699
} ) ;
697
700
}
698
701
702
+
703
+ #[ test]
704
+ fn test_manual_create_destroy ( ) {
705
+ let ctx_full = unsafe { ffi:: secp256k1_context_create ( AllPreallocated :: FLAGS ) } ;
706
+ let ctx_sign = unsafe { ffi:: secp256k1_context_create ( SignOnlyPreallocated :: FLAGS ) } ;
707
+ let ctx_vrfy = unsafe { ffi:: secp256k1_context_create ( VerifyOnlyPreallocated :: FLAGS ) } ;
708
+
709
+ let buf: * mut [ u8 ] = & mut [ 0u8 ; 0 ] as _ ;
710
+ let full: Secp256k1 < AllPreallocated > = Secp256k1 { ctx : ctx_full, phantom : PhantomData , buf} ;
711
+ let sign: Secp256k1 < SignOnlyPreallocated > = Secp256k1 { ctx : ctx_sign, phantom : PhantomData , buf} ;
712
+ let vrfy: Secp256k1 < VerifyOnlyPreallocated > = Secp256k1 { ctx : ctx_vrfy, phantom : PhantomData , buf} ;
713
+
714
+ let ( sk, pk) = full. generate_keypair ( & mut thread_rng ( ) ) ;
715
+ let msg = Message :: from_slice ( & [ 2u8 ; 32 ] ) . unwrap ( ) ;
716
+ // Try signing
717
+ assert_eq ! ( sign. sign( & msg, & sk) , full. sign( & msg, & sk) ) ;
718
+ let sig = full. sign ( & msg, & sk) ;
719
+
720
+ // Try verifying
721
+ assert ! ( vrfy. verify( & msg, & sig, & pk) . is_ok( ) ) ;
722
+ assert ! ( full. verify( & msg, & sig, & pk) . is_ok( ) ) ;
723
+
724
+ drop ( full) ; drop ( sign) ; drop ( vrfy) ;
725
+
726
+ unsafe { ffi:: secp256k1_context_destroy ( ctx_vrfy) } ;
727
+ unsafe { ffi:: secp256k1_context_destroy ( ctx_sign) } ;
728
+ unsafe { ffi:: secp256k1_context_destroy ( ctx_full) } ;
729
+ }
730
+
699
731
#[ test]
700
732
fn test_preallocation ( ) {
701
733
let mut buf_ful = vec ! [ 0u8 ; Secp256k1 :: preallocate_size( ) ] ;
0 commit comments