@@ -162,16 +162,18 @@ void test_deprecated_flags(void) {
162162 unsigned int flags [] = { SECP256K1_CONTEXT_SIGN ,
163163 SECP256K1_CONTEXT_VERIFY ,
164164 SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
165+ secp256k1_context * none_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
165166 int i ;
166167 /* Check that a context created with any of the flags in the flags array is
167168 * identical to the NONE context. */
168169 for (i = 0 ; i < (int )(sizeof (flags )/sizeof (flags [0 ])); i ++ ) {
169170 secp256k1_context * tmp_ctx ;
170171 CHECK (secp256k1_context_preallocated_size (SECP256K1_CONTEXT_NONE ) == secp256k1_context_preallocated_size (flags [i ]));
171172 tmp_ctx = secp256k1_context_create (flags [i ]);
172- CHECK (context_eq (ctx , tmp_ctx ));
173+ CHECK (context_eq (none_ctx , tmp_ctx ));
173174 secp256k1_context_destroy (tmp_ctx );
174175 }
176+ secp256k1_context_destroy (none_ctx );
175177}
176178
177179void run_context_tests (int use_prealloc ) {
@@ -181,20 +183,21 @@ void run_context_tests(int use_prealloc) {
181183 unsigned char ctmp [32 ];
182184 int32_t ecount ;
183185 int32_t ecount2 ;
186+ secp256k1_context * my_ctx ;
184187 secp256k1_context * my_sttc ;
185- void * ctx_prealloc = NULL ;
188+ void * my_ctx_prealloc = NULL ;
186189
187190 secp256k1_gej pubj ;
188191 secp256k1_ge pub ;
189192 secp256k1_scalar msg , key , nonce ;
190193 secp256k1_scalar sigr , sigs ;
191194
192195 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 );
196199 } else {
197- ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
200+ my_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
198201 }
199202 my_sttc = malloc (sizeof (* secp256k1_context_static ));
200203 CHECK (my_sttc != NULL );
@@ -208,11 +211,11 @@ void run_context_tests(int use_prealloc) {
208211 ecount = 0 ;
209212 ecount2 = 10 ;
210213 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 );
212215 /* 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 );
216219
217220 /* Check that deprecated secp256k1_context_no_precomp is an alias to secp256k1_context_static. */
218221 CHECK (secp256k1_context_no_precomp == secp256k1_context_static );
@@ -239,103 +242,104 @@ void run_context_tests(int use_prealloc) {
239242 ecount = 0 ;
240243
241244 /* 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 ));
243246
244247 /*** clone and destroy all of them to make sure cloning was complete ***/
245248 {
246249 secp256k1_context * ctx_tmp ;
247250
248251 if (use_prealloc ) {
249252 /* 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 );
253256 } else {
254257 /* clone into a preallocated context and then again into a new non-preallocated one. */
255258 void * prealloc_tmp ;
256259
257260 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 );
260263 free (prealloc_tmp );
261264 }
262265 }
263266
264267 /* 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 );
267270 /* 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 );
270273
271274 /*** attempt to use them ***/
272275 random_scalar_order_test (& msg );
273276 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 );
275278 secp256k1_ge_set_gej (& pub , & pubj );
276279
277- /* Verify context-type checking illegal-argument errors. */
280+ /* Verify context-type checking illegal-argument errors.
281+ TODO Move this to a separate function. */
278282 memset (ctmp , 1 , 32 );
279283 CHECK (secp256k1_ec_pubkey_create (my_sttc , & pubkey , ctmp ) == 0 );
280284 CHECK (ecount == 1 );
281285 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 );
283287 VG_CHECK (& pubkey , sizeof (pubkey ));
284288 CHECK (secp256k1_ecdsa_sign (my_sttc , & sig , ctmp , ctmp , NULL , NULL ) == 0 );
285289 CHECK (ecount == 2 );
286290 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 );
288292 VG_CHECK (& sig , sizeof (sig ));
289293 CHECK (ecount2 == 10 );
290- CHECK (secp256k1_ecdsa_verify (ctx , & sig , ctmp , & pubkey ) == 1 );
294+ CHECK (secp256k1_ecdsa_verify (my_ctx , & sig , ctmp , & pubkey ) == 1 );
291295 CHECK (ecount2 == 10 );
292296 CHECK (secp256k1_ecdsa_verify (my_sttc , & sig , ctmp , & pubkey ) == 1 );
293297 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 );
295299 CHECK (ecount2 == 10 );
296300 CHECK (secp256k1_ec_pubkey_tweak_add (my_sttc , & pubkey , ctmp ) == 1 );
297301 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 );
299303 CHECK (ecount2 == 10 );
300304 CHECK (secp256k1_ec_pubkey_negate (my_sttc , & pubkey ) == 1 );
301305 CHECK (ecount == 2 );
302- CHECK (secp256k1_ec_pubkey_negate (ctx , & pubkey ) == 1 );
306+ CHECK (secp256k1_ec_pubkey_negate (my_ctx , & pubkey ) == 1 );
303307 CHECK (ecount == 2 );
304308 CHECK (secp256k1_ec_pubkey_negate (my_sttc , & zero_pubkey ) == 0 );
305309 CHECK (ecount == 3 );
306- CHECK (secp256k1_ec_pubkey_negate (ctx , NULL ) == 0 );
310+ CHECK (secp256k1_ec_pubkey_negate (my_ctx , NULL ) == 0 );
307311 CHECK (ecount2 == 11 );
308312 CHECK (secp256k1_ec_pubkey_tweak_mul (my_sttc , & pubkey , ctmp ) == 1 );
309313 CHECK (ecount == 3 );
310314 CHECK (secp256k1_context_randomize (my_sttc , ctmp ) == 0 );
311315 CHECK (ecount == 4 );
312316 CHECK (secp256k1_context_randomize (my_sttc , NULL ) == 0 );
313317 CHECK (ecount == 5 );
314- CHECK (secp256k1_context_randomize (ctx , ctmp ) == 1 );
318+ CHECK (secp256k1_context_randomize (my_ctx , ctmp ) == 1 );
315319 CHECK (ecount2 == 11 );
316- CHECK (secp256k1_context_randomize (ctx , NULL ) == 1 );
320+ CHECK (secp256k1_context_randomize (my_ctx , NULL ) == 1 );
317321 CHECK (ecount2 == 11 );
318322 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 );
320324
321325 /* obtain a working nonce */
322326 do {
323327 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 ));
325329
326330 /* 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 ));
328332
329333 /* try verifying */
330334 CHECK (secp256k1_ecdsa_sig_verify (& sigr , & sigs , & pub , & msg ));
331335
332336 /* cleanup */
333337 free (my_sttc );
334338 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 );
337341 } else {
338- secp256k1_context_destroy (ctx );
342+ secp256k1_context_destroy (my_ctx );
339343 }
340344
341345 /* Defined as no-op. */
0 commit comments