@@ -231,7 +231,8 @@ void run_ec_illegal_argument_tests(void) {
231
231
void run_context_tests (int use_prealloc ) {
232
232
int32_t ecount ;
233
233
int32_t ecount2 ;
234
- void * ctx_prealloc = NULL ;
234
+ secp256k1_context * my_ctx ;
235
+ void * my_ctx_prealloc = NULL ;
235
236
236
237
secp256k1_gej pubj ;
237
238
secp256k1_ge pub ;
@@ -242,24 +243,24 @@ void run_context_tests(int use_prealloc) {
242
243
CHECK (secp256k1_context_no_precomp == secp256k1_context_static );
243
244
244
245
if (use_prealloc ) {
245
- ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
246
- CHECK (ctx_prealloc != NULL );
247
- ctx = secp256k1_context_preallocated_create (ctx_prealloc , SECP256K1_CONTEXT_NONE );
246
+ my_ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
247
+ CHECK (my_ctx_prealloc != NULL );
248
+ my_ctx = secp256k1_context_preallocated_create (my_ctx_prealloc , SECP256K1_CONTEXT_NONE );
248
249
} else {
249
- ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
250
+ my_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
250
251
}
251
252
252
253
ecount = 0 ;
253
254
ecount2 = 10 ;
254
255
secp256k1_context_set_illegal_callback (sttc , counting_illegal_callback_fn , & ecount );
255
- secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount2 );
256
+ secp256k1_context_set_illegal_callback (my_ctx , counting_illegal_callback_fn , & ecount2 );
256
257
/* set error callback (to a function that still aborts in case malloc() fails in secp256k1_context_clone() below) */
257
- secp256k1_context_set_error_callback (ctx , secp256k1_default_illegal_callback_fn , NULL );
258
- CHECK (ctx -> error_callback .fn != sttc -> error_callback .fn );
259
- CHECK (ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
258
+ secp256k1_context_set_error_callback (my_ctx , secp256k1_default_illegal_callback_fn , NULL );
259
+ CHECK (my_ctx -> error_callback .fn != sttc -> error_callback .fn );
260
+ CHECK (my_ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
260
261
261
262
/* check if sizes for cloning are consistent */
262
- CHECK (secp256k1_context_preallocated_clone_size (ctx ) == secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
263
+ CHECK (secp256k1_context_preallocated_clone_size (my_ctx ) == secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
263
264
CHECK (secp256k1_context_preallocated_clone_size (sttc ) >= sizeof (secp256k1_context ));
264
265
265
266
/*** clone and destroy all of them to make sure cloning was complete ***/
@@ -268,50 +269,50 @@ void run_context_tests(int use_prealloc) {
268
269
269
270
if (use_prealloc ) {
270
271
/* clone into a non-preallocated context and then again into a new preallocated one. */
271
- ctx_tmp = ctx ; ctx = secp256k1_context_clone (ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
272
- free (ctx_prealloc ); ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE )); CHECK (ctx_prealloc != NULL );
273
- ctx_tmp = ctx ; ctx = secp256k1_context_preallocated_clone (ctx , ctx_prealloc ); secp256k1_context_destroy (ctx_tmp );
272
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_clone (my_ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
273
+ free (my_ctx_prealloc ); my_ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE )); CHECK (my_ctx_prealloc != NULL );
274
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_preallocated_clone (my_ctx , my_ctx_prealloc ); secp256k1_context_destroy (ctx_tmp );
274
275
} else {
275
276
/* clone into a preallocated context and then again into a new non-preallocated one. */
276
277
void * prealloc_tmp ;
277
278
278
279
prealloc_tmp = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE )); CHECK (prealloc_tmp != NULL );
279
- ctx_tmp = ctx ; ctx = secp256k1_context_preallocated_clone (ctx , prealloc_tmp ); secp256k1_context_destroy (ctx_tmp );
280
- ctx_tmp = ctx ; ctx = secp256k1_context_clone (ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
280
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_preallocated_clone (my_ctx , prealloc_tmp ); secp256k1_context_destroy (ctx_tmp );
281
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_clone (my_ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
281
282
free (prealloc_tmp );
282
283
}
283
284
}
284
285
285
286
/* Verify that the error callback makes it across the clone. */
286
- CHECK (ctx -> error_callback .fn != sttc -> error_callback .fn );
287
- CHECK (ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
287
+ CHECK (my_ctx -> error_callback .fn != sttc -> error_callback .fn );
288
+ CHECK (my_ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
288
289
/* And that it resets back to default. */
289
- secp256k1_context_set_error_callback (ctx , NULL , NULL );
290
- CHECK (ctx -> error_callback .fn == sttc -> error_callback .fn );
290
+ secp256k1_context_set_error_callback (my_ctx , NULL , NULL );
291
+ CHECK (my_ctx -> error_callback .fn == sttc -> error_callback .fn );
291
292
292
293
/*** attempt to use them ***/
293
294
random_scalar_order_test (& msg );
294
295
random_scalar_order_test (& key );
295
- secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubj , & key );
296
+ secp256k1_ecmult_gen (& my_ctx -> ecmult_gen_ctx , & pubj , & key );
296
297
secp256k1_ge_set_gej (& pub , & pubj );
297
298
298
299
/* obtain a working nonce */
299
300
do {
300
301
random_scalar_order_test (& nonce );
301
- } while (!secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
302
+ } while (!secp256k1_ecdsa_sig_sign (& my_ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
302
303
303
304
/* try signing */
304
- CHECK (secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
305
+ CHECK (secp256k1_ecdsa_sig_sign (& my_ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
305
306
306
307
/* try verifying */
307
308
CHECK (secp256k1_ecdsa_sig_verify (& sigr , & sigs , & pub , & msg ));
308
309
309
310
/* cleanup */
310
311
if (use_prealloc ) {
311
- secp256k1_context_preallocated_destroy (ctx );
312
- free (ctx_prealloc );
312
+ secp256k1_context_preallocated_destroy (my_ctx );
313
+ free (my_ctx_prealloc );
313
314
} else {
314
- secp256k1_context_destroy (ctx );
315
+ secp256k1_context_destroy (my_ctx );
315
316
}
316
317
/* Defined as no-op. */
317
318
secp256k1_context_destroy (NULL );
@@ -7360,11 +7361,6 @@ int main(int argc, char **argv) {
7360
7361
memcpy (sttc , secp256k1_context_static , sizeof (secp256k1_context ));
7361
7362
CHECK (!secp256k1_context_is_proper (sttc ));
7362
7363
7363
- run_selftest_tests ();
7364
- run_context_tests (0 );
7365
- run_context_tests (1 );
7366
- run_deprecated_context_flags_test ();
7367
-
7368
7364
ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
7369
7365
/* Randomize the context only with probability 15/16
7370
7366
to make sure we test without context randomization from time to time.
@@ -7375,6 +7371,10 @@ int main(int argc, char **argv) {
7375
7371
CHECK (secp256k1_context_randomize (ctx , rand32 ));
7376
7372
}
7377
7373
7374
+ run_selftest_tests ();
7375
+ run_context_tests (0 );
7376
+ run_context_tests (1 );
7377
+ run_deprecated_context_flags_test ();
7378
7378
run_scratch_tests ();
7379
7379
7380
7380
run_rand_bits ();
0 commit comments