@@ -103,6 +103,47 @@ void random_scalar_order(secp256k1_scalar_t *num) {
103103 } while (1 );
104104}
105105
106+ void run_context_tests (void ) {
107+ secp256k1_context_t * none = secp256k1_context_create (0 );
108+ secp256k1_context_t * sign = secp256k1_context_create (SECP256K1_CONTEXT_SIGN );
109+ secp256k1_context_t * vrfy = secp256k1_context_create (SECP256K1_CONTEXT_VERIFY );
110+ secp256k1_context_t * both = secp256k1_context_create (SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY );
111+
112+ secp256k1_gej_t pubj ;
113+ secp256k1_ge_t pub ;
114+ secp256k1_scalar_t msg , key , nonce ;
115+ secp256k1_ecdsa_sig_t sig ;
116+
117+ /*** clone and destroy all of them to make sure cloning was complete ***/
118+ {
119+ secp256k1_context_t * ctx_tmp ;
120+
121+ ctx_tmp = none ; none = secp256k1_context_clone (none ); secp256k1_context_destroy (ctx_tmp );
122+ ctx_tmp = sign ; sign = secp256k1_context_clone (sign ); secp256k1_context_destroy (ctx_tmp );
123+ ctx_tmp = vrfy ; vrfy = secp256k1_context_clone (vrfy ); secp256k1_context_destroy (ctx_tmp );
124+ ctx_tmp = both ; both = secp256k1_context_clone (both ); secp256k1_context_destroy (ctx_tmp );
125+ }
126+
127+ /*** attempt to use them ***/
128+ random_scalar_order_test (& msg );
129+ random_scalar_order_test (& key );
130+ secp256k1_ecmult_gen (& both -> ecmult_gen_ctx , & pubj , & key );
131+ secp256k1_ge_set_gej (& pub , & pubj );
132+
133+ /* obtain a working nonce */
134+ do {
135+ random_scalar_order_test (& nonce );
136+ } while (!secp256k1_ecdsa_sig_sign (& both -> ecmult_gen_ctx , & sig , & key , & msg , & nonce , NULL ));
137+
138+ /* try signing */
139+ CHECK (secp256k1_ecdsa_sig_sign (& sign -> ecmult_gen_ctx , & sig , & key , & msg , & nonce , NULL ));
140+ CHECK (secp256k1_ecdsa_sig_sign (& both -> ecmult_gen_ctx , & sig , & key , & msg , & nonce , NULL ));
141+
142+ /* try verifying */
143+ CHECK (secp256k1_ecdsa_sig_verify (& vrfy -> ecmult_ctx , & sig , & pub , & msg ));
144+ CHECK (secp256k1_ecdsa_sig_verify (& both -> ecmult_ctx , & sig , & pub , & msg ));
145+ }
146+
106147/***** HASH TESTS *****/
107148
108149void run_sha256_tests (void ) {
@@ -1863,6 +1904,7 @@ int main(int argc, char **argv) {
18631904 printf ("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n" , seed16 [0 ], seed16 [1 ], seed16 [2 ], seed16 [3 ], seed16 [4 ], seed16 [5 ], seed16 [6 ], seed16 [7 ], seed16 [8 ], seed16 [9 ], seed16 [10 ], seed16 [11 ], seed16 [12 ], seed16 [13 ], seed16 [14 ], seed16 [15 ]);
18641905
18651906 /* initialize */
1907+ run_context_tests ();
18661908 ctx = secp256k1_context_create (SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY );
18671909
18681910 run_sha256_tests ();
0 commit comments