@@ -162,16 +162,18 @@ void test_deprecated_flags(void) {
162
162
unsigned int flags [] = { SECP256K1_CONTEXT_SIGN ,
163
163
SECP256K1_CONTEXT_VERIFY ,
164
164
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
165
+ secp256k1_context * none_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
165
166
int i ;
166
167
/* Check that a context created with any of the flags in the flags array is
167
168
* identical to the NONE context. */
168
169
for (i = 0 ; i < (int )(sizeof (flags )/sizeof (flags [0 ])); i ++ ) {
169
170
secp256k1_context * tmp_ctx ;
170
171
CHECK (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ) == secp256k1_context_preallocated_size (flags [i ]));
171
172
tmp_ctx = secp256k1_context_create (flags [i ]);
172
- CHECK (context_eq (ctx , tmp_ctx ));
173
+ CHECK (context_eq (none_ctx , tmp_ctx ));
173
174
secp256k1_context_destroy (tmp_ctx );
174
175
}
176
+ secp256k1_context_destroy (none_ctx );
175
177
}
176
178
177
179
void run_context_tests (int use_prealloc ) {
@@ -181,20 +183,21 @@ void run_context_tests(int use_prealloc) {
181
183
unsigned char ctmp [32 ];
182
184
int32_t ecount ;
183
185
int32_t ecount2 ;
186
+ secp256k1_context * my_ctx ;
184
187
secp256k1_context * my_sttc ;
185
- void * ctx_prealloc = NULL ;
188
+ void * my_ctx_prealloc = NULL ;
186
189
187
190
secp256k1_gej pubj ;
188
191
secp256k1_ge pub ;
189
192
secp256k1_scalar msg , key , nonce ;
190
193
secp256k1_scalar sigr , sigs ;
191
194
192
195
if (use_prealloc ) {
193
- ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
194
- CHECK (ctx_prealloc != NULL );
195
- ctx = secp256k1_context_preallocated_create (ctx_prealloc , SECP256K1_CONTEXT_NONE );
196
+ my_ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
197
+ CHECK (my_ctx_prealloc != NULL );
198
+ my_ctx = secp256k1_context_preallocated_create (my_ctx_prealloc , SECP256K1_CONTEXT_NONE );
196
199
} else {
197
- ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
200
+ my_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
198
201
}
199
202
my_sttc = malloc (sizeof (* secp256k1_context_static ));
200
203
CHECK (my_sttc != NULL );
@@ -208,11 +211,11 @@ void run_context_tests(int use_prealloc) {
208
211
ecount = 0 ;
209
212
ecount2 = 10 ;
210
213
secp256k1_context_set_illegal_callback (my_sttc , counting_illegal_callback_fn , & ecount );
211
- secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount2 );
214
+ secp256k1_context_set_illegal_callback (my_ctx , counting_illegal_callback_fn , & ecount2 );
212
215
/* set error callback (to a function that still aborts in case malloc() fails in secp256k1_context_clone() below) */
213
- secp256k1_context_set_error_callback (ctx , secp256k1_default_illegal_callback_fn , NULL );
214
- CHECK (ctx -> error_callback .fn != my_sttc -> error_callback .fn );
215
- CHECK (ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
216
+ secp256k1_context_set_error_callback (my_ctx , secp256k1_default_illegal_callback_fn , NULL );
217
+ CHECK (my_ctx -> error_callback .fn != my_sttc -> error_callback .fn );
218
+ CHECK (my_ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
216
219
217
220
/* Check that deprecated secp256k1_context_no_precomp is an alias to secp256k1_context_static. */
218
221
CHECK (secp256k1_context_no_precomp == secp256k1_context_static );
@@ -239,103 +242,104 @@ void run_context_tests(int use_prealloc) {
239
242
ecount = 0 ;
240
243
241
244
/* check if sizes for cloning are consistent */
242
- CHECK (secp256k1_context_preallocated_clone_size (ctx ) == secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
245
+ CHECK (secp256k1_context_preallocated_clone_size (my_ctx ) == secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ));
243
246
244
247
/*** clone and destroy all of them to make sure cloning was complete ***/
245
248
{
246
249
secp256k1_context * ctx_tmp ;
247
250
248
251
if (use_prealloc ) {
249
252
/* clone into a non-preallocated context and then again into a new preallocated one. */
250
- ctx_tmp = ctx ; ctx = secp256k1_context_clone (ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
251
- free (ctx_prealloc ); ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE )); CHECK (ctx_prealloc != NULL );
252
- ctx_tmp = ctx ; ctx = secp256k1_context_preallocated_clone (ctx , ctx_prealloc ); secp256k1_context_destroy (ctx_tmp );
253
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_clone (my_ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
254
+ free (my_ctx_prealloc ); my_ctx_prealloc = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE )); CHECK (my_ctx_prealloc != NULL );
255
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_preallocated_clone (my_ctx , my_ctx_prealloc ); secp256k1_context_destroy (ctx_tmp );
253
256
} else {
254
257
/* clone into a preallocated context and then again into a new non-preallocated one. */
255
258
void * prealloc_tmp ;
256
259
257
260
prealloc_tmp = malloc (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE )); CHECK (prealloc_tmp != NULL );
258
- ctx_tmp = ctx ; ctx = secp256k1_context_preallocated_clone (ctx , prealloc_tmp ); secp256k1_context_destroy (ctx_tmp );
259
- ctx_tmp = ctx ; ctx = secp256k1_context_clone (ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
261
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_preallocated_clone (my_ctx , prealloc_tmp ); secp256k1_context_destroy (ctx_tmp );
262
+ ctx_tmp = my_ctx ; my_ctx = secp256k1_context_clone (my_ctx ); secp256k1_context_preallocated_destroy (ctx_tmp );
260
263
free (prealloc_tmp );
261
264
}
262
265
}
263
266
264
267
/* Verify that the error callback makes it across the clone. */
265
- CHECK (ctx -> error_callback .fn != my_sttc -> error_callback .fn );
266
- CHECK (ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
268
+ CHECK (my_ctx -> error_callback .fn != my_sttc -> error_callback .fn );
269
+ CHECK (my_ctx -> error_callback .fn == secp256k1_default_illegal_callback_fn );
267
270
/* And that it resets back to default. */
268
- secp256k1_context_set_error_callback (ctx , NULL , NULL );
269
- CHECK (ctx -> error_callback .fn == my_sttc -> error_callback .fn );
271
+ secp256k1_context_set_error_callback (my_ctx , NULL , NULL );
272
+ CHECK (my_ctx -> error_callback .fn == my_sttc -> error_callback .fn );
270
273
271
274
/*** attempt to use them ***/
272
275
random_scalar_order_test (& msg );
273
276
random_scalar_order_test (& key );
274
- secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubj , & key );
277
+ secp256k1_ecmult_gen (& my_ctx -> ecmult_gen_ctx , & pubj , & key );
275
278
secp256k1_ge_set_gej (& pub , & pubj );
276
279
277
- /* Verify context-type checking illegal-argument errors. */
280
+ /* Verify context-type checking illegal-argument errors.
281
+ TODO Move this to a separate function. */
278
282
memset (ctmp , 1 , 32 );
279
283
CHECK (secp256k1_ec_pubkey_create (my_sttc , & pubkey , ctmp ) == 0 );
280
284
CHECK (ecount == 1 );
281
285
VG_UNDEF (& pubkey , sizeof (pubkey ));
282
- CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , ctmp ) == 1 );
286
+ CHECK (secp256k1_ec_pubkey_create (my_ctx , & pubkey , ctmp ) == 1 );
283
287
VG_CHECK (& pubkey , sizeof (pubkey ));
284
288
CHECK (secp256k1_ecdsa_sign (my_sttc , & sig , ctmp , ctmp , NULL , NULL ) == 0 );
285
289
CHECK (ecount == 2 );
286
290
VG_UNDEF (& sig , sizeof (sig ));
287
- CHECK (secp256k1_ecdsa_sign (ctx , & sig , ctmp , ctmp , NULL , NULL ) == 1 );
291
+ CHECK (secp256k1_ecdsa_sign (my_ctx , & sig , ctmp , ctmp , NULL , NULL ) == 1 );
288
292
VG_CHECK (& sig , sizeof (sig ));
289
293
CHECK (ecount2 == 10 );
290
- CHECK (secp256k1_ecdsa_verify (ctx , & sig , ctmp , & pubkey ) == 1 );
294
+ CHECK (secp256k1_ecdsa_verify (my_ctx , & sig , ctmp , & pubkey ) == 1 );
291
295
CHECK (ecount2 == 10 );
292
296
CHECK (secp256k1_ecdsa_verify (my_sttc , & sig , ctmp , & pubkey ) == 1 );
293
297
CHECK (ecount == 2 );
294
- CHECK (secp256k1_ec_pubkey_tweak_add (ctx , & pubkey , ctmp ) == 1 );
298
+ CHECK (secp256k1_ec_pubkey_tweak_add (my_ctx , & pubkey , ctmp ) == 1 );
295
299
CHECK (ecount2 == 10 );
296
300
CHECK (secp256k1_ec_pubkey_tweak_add (my_sttc , & pubkey , ctmp ) == 1 );
297
301
CHECK (ecount == 2 );
298
- CHECK (secp256k1_ec_pubkey_tweak_mul (ctx , & pubkey , ctmp ) == 1 );
302
+ CHECK (secp256k1_ec_pubkey_tweak_mul (my_ctx , & pubkey , ctmp ) == 1 );
299
303
CHECK (ecount2 == 10 );
300
304
CHECK (secp256k1_ec_pubkey_negate (my_sttc , & pubkey ) == 1 );
301
305
CHECK (ecount == 2 );
302
- CHECK (secp256k1_ec_pubkey_negate (ctx , & pubkey ) == 1 );
306
+ CHECK (secp256k1_ec_pubkey_negate (my_ctx , & pubkey ) == 1 );
303
307
CHECK (ecount == 2 );
304
308
CHECK (secp256k1_ec_pubkey_negate (my_sttc , & zero_pubkey ) == 0 );
305
309
CHECK (ecount == 3 );
306
- CHECK (secp256k1_ec_pubkey_negate (ctx , NULL ) == 0 );
310
+ CHECK (secp256k1_ec_pubkey_negate (my_ctx , NULL ) == 0 );
307
311
CHECK (ecount2 == 11 );
308
312
CHECK (secp256k1_ec_pubkey_tweak_mul (my_sttc , & pubkey , ctmp ) == 1 );
309
313
CHECK (ecount == 3 );
310
314
CHECK (secp256k1_context_randomize (my_sttc , ctmp ) == 0 );
311
315
CHECK (ecount == 4 );
312
316
CHECK (secp256k1_context_randomize (my_sttc , NULL ) == 0 );
313
317
CHECK (ecount == 5 );
314
- CHECK (secp256k1_context_randomize (ctx , ctmp ) == 1 );
318
+ CHECK (secp256k1_context_randomize (my_ctx , ctmp ) == 1 );
315
319
CHECK (ecount2 == 11 );
316
- CHECK (secp256k1_context_randomize (ctx , NULL ) == 1 );
320
+ CHECK (secp256k1_context_randomize (my_ctx , NULL ) == 1 );
317
321
CHECK (ecount2 == 11 );
318
322
secp256k1_context_set_illegal_callback (my_sttc , NULL , NULL );
319
- secp256k1_context_set_illegal_callback (ctx , NULL , NULL );
323
+ secp256k1_context_set_illegal_callback (my_ctx , NULL , NULL );
320
324
321
325
/* obtain a working nonce */
322
326
do {
323
327
random_scalar_order_test (& nonce );
324
- } while (!secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
328
+ } while (!secp256k1_ecdsa_sig_sign (& my_ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
325
329
326
330
/* try signing */
327
- CHECK (secp256k1_ecdsa_sig_sign (& ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
331
+ CHECK (secp256k1_ecdsa_sig_sign (& my_ctx -> ecmult_gen_ctx , & sigr , & sigs , & key , & msg , & nonce , NULL ));
328
332
329
333
/* try verifying */
330
334
CHECK (secp256k1_ecdsa_sig_verify (& sigr , & sigs , & pub , & msg ));
331
335
332
336
/* cleanup */
333
337
free (my_sttc );
334
338
if (use_prealloc ) {
335
- secp256k1_context_preallocated_destroy (ctx );
336
- free (ctx_prealloc );
339
+ secp256k1_context_preallocated_destroy (my_ctx );
340
+ free (my_ctx_prealloc );
337
341
} else {
338
- secp256k1_context_destroy (ctx );
342
+ secp256k1_context_destroy (my_ctx );
339
343
}
340
344
341
345
/* Defined as no-op. */
0 commit comments