163163 && a -> error_callback .data == b -> error_callback .data ;
164164}
165165
166- void test_deprecated_flags (void ) {
166+ void run_deprecated_context_flags_test (void ) {
167167 unsigned int flags [] = { SECP256K1_CONTEXT_SIGN ,
168168 SECP256K1_CONTEXT_VERIFY ,
169169 SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
@@ -181,11 +181,59 @@ void test_deprecated_flags(void) {
181181 secp256k1_context_destroy (none_ctx );
182182}
183183
184- void run_context_tests (int use_prealloc ) {
184+ void run_ec_illegal_argument_tests (void ) {
185+ int ecount = 0 ;
186+ int ecount2 = 10 ;
185187 secp256k1_pubkey pubkey ;
186188 secp256k1_pubkey zero_pubkey ;
187189 secp256k1_ecdsa_signature sig ;
188190 unsigned char ctmp [32 ];
191+
192+ /* Setup */
193+ secp256k1_context_set_illegal_callback (sttc , counting_illegal_callback_fn , & ecount );
194+ secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount2 );
195+ memset (ctmp , 1 , 32 );
196+ memset (& zero_pubkey , 0 , sizeof (zero_pubkey ));
197+
198+ /* Verify context-type checking illegal-argument errors. */
199+ CHECK (secp256k1_ec_pubkey_create (sttc , & pubkey , ctmp ) == 0 );
200+ CHECK (ecount == 1 );
201+ VG_UNDEF (& pubkey , sizeof (pubkey ));
202+ CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , ctmp ) == 1 );
203+ VG_CHECK (& pubkey , sizeof (pubkey ));
204+ CHECK (secp256k1_ecdsa_sign (sttc , & sig , ctmp , ctmp , NULL , NULL ) == 0 );
205+ CHECK (ecount == 2 );
206+ VG_UNDEF (& sig , sizeof (sig ));
207+ CHECK (secp256k1_ecdsa_sign (ctx , & sig , ctmp , ctmp , NULL , NULL ) == 1 );
208+ VG_CHECK (& sig , sizeof (sig ));
209+ CHECK (ecount2 == 10 );
210+ CHECK (secp256k1_ecdsa_verify (ctx , & sig , ctmp , & pubkey ) == 1 );
211+ CHECK (ecount2 == 10 );
212+ CHECK (secp256k1_ecdsa_verify (sttc , & sig , ctmp , & pubkey ) == 1 );
213+ CHECK (ecount == 2 );
214+ CHECK (secp256k1_ec_pubkey_tweak_add (ctx , & pubkey , ctmp ) == 1 );
215+ CHECK (ecount2 == 10 );
216+ CHECK (secp256k1_ec_pubkey_tweak_add (sttc , & pubkey , ctmp ) == 1 );
217+ CHECK (ecount == 2 );
218+ CHECK (secp256k1_ec_pubkey_tweak_mul (ctx , & pubkey , ctmp ) == 1 );
219+ CHECK (ecount2 == 10 );
220+ CHECK (secp256k1_ec_pubkey_negate (sttc , & pubkey ) == 1 );
221+ CHECK (ecount == 2 );
222+ CHECK (secp256k1_ec_pubkey_negate (ctx , & pubkey ) == 1 );
223+ CHECK (ecount == 2 );
224+ CHECK (secp256k1_ec_pubkey_negate (sttc , & zero_pubkey ) == 0 );
225+ CHECK (ecount == 3 );
226+ CHECK (secp256k1_ec_pubkey_negate (ctx , NULL ) == 0 );
227+ CHECK (ecount2 == 11 );
228+ CHECK (secp256k1_ec_pubkey_tweak_mul (sttc , & pubkey , ctmp ) == 1 );
229+ CHECK (ecount == 3 );
230+
231+ /* Clean up */
232+ secp256k1_context_set_illegal_callback (sttc , NULL , NULL );
233+ secp256k1_context_set_illegal_callback (ctx , NULL , NULL );
234+ }
235+
236+ void run_context_tests (int use_prealloc ) {
189237 int32_t ecount ;
190238 int32_t ecount2 ;
191239 secp256k1_context * my_ctx ;
@@ -209,10 +257,6 @@ void run_context_tests(int use_prealloc) {
209257 memcpy (my_sttc , secp256k1_context_static , sizeof (secp256k1_context ));
210258 CHECK (!secp256k1_ecmult_gen_context_is_built (& my_sttc -> ecmult_gen_ctx ));
211259
212- test_deprecated_flags ();
213-
214- memset (& zero_pubkey , 0 , sizeof (zero_pubkey ));
215-
216260 ecount = 0 ;
217261 ecount2 = 10 ;
218262 secp256k1_context_set_illegal_callback (my_sttc , counting_illegal_callback_fn , & ecount );
@@ -244,7 +288,21 @@ void run_context_tests(int use_prealloc) {
244288 secp256k1_context_destroy (my_sttc );
245289 CHECK (ecount == 2 );
246290 }
247- ecount = 0 ;
291+
292+ /* Randomizing secp256k1_context_static is not supported. */
293+ {
294+ unsigned char ctmp [32 ];
295+ memset (ctmp , 1 , sizeof (ctmp ));
296+ ecount = 0 ;
297+ CHECK (secp256k1_context_randomize (my_sttc , ctmp ) == 0 );
298+ CHECK (ecount == 1 );
299+ CHECK (secp256k1_context_randomize (my_sttc , NULL ) == 0 );
300+ CHECK (ecount == 2 );
301+ CHECK (secp256k1_context_randomize (my_ctx , ctmp ) == 1 );
302+ CHECK (ecount == 2 );
303+ CHECK (secp256k1_context_randomize (my_ctx , NULL ) == 1 );
304+ CHECK (ecount == 2 );
305+ }
248306
249307 /* check if sizes for cloning are consistent */
250308 CHECK (secp256k1_context_preallocated_clone_size (my_ctx ) == secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
@@ -282,51 +340,6 @@ void run_context_tests(int use_prealloc) {
282340 secp256k1_ecmult_gen (& my_ctx -> ecmult_gen_ctx , & pubj , & key );
283341 secp256k1_ge_set_gej (& pub , & pubj );
284342
285- /* Verify context-type checking illegal-argument errors.
286- TODO Move this to a separate function. */
287- memset (ctmp , 1 , 32 );
288- CHECK (secp256k1_ec_pubkey_create (my_sttc , & pubkey , ctmp ) == 0 );
289- CHECK (ecount == 1 );
290- VG_UNDEF (& pubkey , sizeof (pubkey ));
291- CHECK (secp256k1_ec_pubkey_create (my_ctx , & pubkey , ctmp ) == 1 );
292- VG_CHECK (& pubkey , sizeof (pubkey ));
293- CHECK (secp256k1_ecdsa_sign (my_sttc , & sig , ctmp , ctmp , NULL , NULL ) == 0 );
294- CHECK (ecount == 2 );
295- VG_UNDEF (& sig , sizeof (sig ));
296- CHECK (secp256k1_ecdsa_sign (my_ctx , & sig , ctmp , ctmp , NULL , NULL ) == 1 );
297- VG_CHECK (& sig , sizeof (sig ));
298- CHECK (ecount2 == 10 );
299- CHECK (secp256k1_ecdsa_verify (my_ctx , & sig , ctmp , & pubkey ) == 1 );
300- CHECK (ecount2 == 10 );
301- CHECK (secp256k1_ecdsa_verify (my_sttc , & sig , ctmp , & pubkey ) == 1 );
302- CHECK (ecount == 2 );
303- CHECK (secp256k1_ec_pubkey_tweak_add (my_ctx , & pubkey , ctmp ) == 1 );
304- CHECK (ecount2 == 10 );
305- CHECK (secp256k1_ec_pubkey_tweak_add (my_sttc , & pubkey , ctmp ) == 1 );
306- CHECK (ecount == 2 );
307- CHECK (secp256k1_ec_pubkey_tweak_mul (my_ctx , & pubkey , ctmp ) == 1 );
308- CHECK (ecount2 == 10 );
309- CHECK (secp256k1_ec_pubkey_negate (my_sttc , & pubkey ) == 1 );
310- CHECK (ecount == 2 );
311- CHECK (secp256k1_ec_pubkey_negate (my_ctx , & pubkey ) == 1 );
312- CHECK (ecount == 2 );
313- CHECK (secp256k1_ec_pubkey_negate (my_sttc , & zero_pubkey ) == 0 );
314- CHECK (ecount == 3 );
315- CHECK (secp256k1_ec_pubkey_negate (my_ctx , NULL ) == 0 );
316- CHECK (ecount2 == 11 );
317- CHECK (secp256k1_ec_pubkey_tweak_mul (my_sttc , & pubkey , ctmp ) == 1 );
318- CHECK (ecount == 3 );
319- CHECK (secp256k1_context_randomize (my_sttc , ctmp ) == 0 );
320- CHECK (ecount == 4 );
321- CHECK (secp256k1_context_randomize (my_sttc , NULL ) == 0 );
322- CHECK (ecount == 5 );
323- CHECK (secp256k1_context_randomize (my_ctx , ctmp ) == 1 );
324- CHECK (ecount2 == 11 );
325- CHECK (secp256k1_context_randomize (my_ctx , NULL ) == 1 );
326- CHECK (ecount2 == 11 );
327- secp256k1_context_set_illegal_callback (my_sttc , NULL , NULL );
328- secp256k1_context_set_illegal_callback (my_ctx , NULL , NULL );
329-
330343 /* obtain a working nonce */
331344 do {
332345 random_scalar_order_test (& nonce );
@@ -7407,6 +7420,9 @@ int main(int argc, char **argv) {
74077420 memcpy (sttc , secp256k1_context_static , sizeof (secp256k1_context ));
74087421 CHECK (!secp256k1_context_is_proper (sttc ));
74097422
7423+ run_deprecated_context_flags_test ();
7424+ run_ec_illegal_argument_tests ();
7425+
74107426 run_rand_bits ();
74117427 run_rand_int ();
74127428
0 commit comments