@@ -158,27 +158,77 @@ int context_eq(const secp256k1_context *a, const secp256k1_context *b) {
158158 && a -> error_callback .data == b -> error_callback .data ;
159159}
160160
161- void test_deprecated_flags (void ) {
161+ void run_deprecated_context_flags_test (void ) {
162+ /* Check that a context created with any of the flags in the flags array is
163+ * identical to the NONE context. */
162164 unsigned int flags [] = { SECP256K1_CONTEXT_SIGN ,
163165 SECP256K1_CONTEXT_VERIFY ,
164166 SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
167+ secp256k1_context * none_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
165168 int i ;
166- /* Check that a context created with any of the flags in the flags array is
167- * 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
177- void run_context_tests (int use_prealloc ) {
179+ void run_ec_illegal_argument_tests (void ) {
180+ int ecount = 0 ;
181+ int ecount2 = 10 ;
178182 secp256k1_pubkey pubkey ;
179183 secp256k1_pubkey zero_pubkey ;
180184 secp256k1_ecdsa_signature sig ;
181185 unsigned char ctmp [32 ];
186+
187+ /* Setup */
188+ secp256k1_context_set_illegal_callback (sttc , counting_illegal_callback_fn , & ecount );
189+ secp256k1_context_set_illegal_callback (ctx , counting_illegal_callback_fn , & ecount2 );
190+ memset (ctmp , 1 , 32 );
191+ memset (& zero_pubkey , 0 , sizeof (zero_pubkey ));
192+
193+ /* Verify context-type checking illegal-argument errors. */
194+ CHECK (secp256k1_ec_pubkey_create (sttc , & pubkey , ctmp ) == 0 );
195+ CHECK (ecount == 1 );
196+ VG_UNDEF (& pubkey , sizeof (pubkey ));
197+ CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , ctmp ) == 1 );
198+ VG_CHECK (& pubkey , sizeof (pubkey ));
199+ CHECK (secp256k1_ecdsa_sign (sttc , & sig , ctmp , ctmp , NULL , NULL ) == 0 );
200+ CHECK (ecount == 2 );
201+ VG_UNDEF (& sig , sizeof (sig ));
202+ CHECK (secp256k1_ecdsa_sign (ctx , & sig , ctmp , ctmp , NULL , NULL ) == 1 );
203+ VG_CHECK (& sig , sizeof (sig ));
204+ CHECK (ecount2 == 10 );
205+ CHECK (secp256k1_ecdsa_verify (ctx , & sig , ctmp , & pubkey ) == 1 );
206+ CHECK (ecount2 == 10 );
207+ CHECK (secp256k1_ecdsa_verify (sttc , & sig , ctmp , & pubkey ) == 1 );
208+ CHECK (ecount == 2 );
209+ CHECK (secp256k1_ec_pubkey_tweak_add (ctx , & pubkey , ctmp ) == 1 );
210+ CHECK (ecount2 == 10 );
211+ CHECK (secp256k1_ec_pubkey_tweak_add (sttc , & pubkey , ctmp ) == 1 );
212+ CHECK (ecount == 2 );
213+ CHECK (secp256k1_ec_pubkey_tweak_mul (ctx , & pubkey , ctmp ) == 1 );
214+ CHECK (ecount2 == 10 );
215+ CHECK (secp256k1_ec_pubkey_negate (sttc , & pubkey ) == 1 );
216+ CHECK (ecount == 2 );
217+ CHECK (secp256k1_ec_pubkey_negate (ctx , & pubkey ) == 1 );
218+ CHECK (ecount == 2 );
219+ CHECK (secp256k1_ec_pubkey_negate (sttc , & zero_pubkey ) == 0 );
220+ CHECK (ecount == 3 );
221+ CHECK (secp256k1_ec_pubkey_negate (ctx , NULL ) == 0 );
222+ CHECK (ecount2 == 11 );
223+ CHECK (secp256k1_ec_pubkey_tweak_mul (sttc , & pubkey , ctmp ) == 1 );
224+ CHECK (ecount == 3 );
225+
226+ /* Clean up */
227+ secp256k1_context_set_illegal_callback (sttc , NULL , NULL );
228+ secp256k1_context_set_illegal_callback (ctx , NULL , NULL );
229+ }
230+
231+ void run_context_tests (int use_prealloc ) {
182232 int32_t ecount ;
183233 int32_t ecount2 ;
184234 void * ctx_prealloc = NULL ;
@@ -199,10 +249,6 @@ void run_context_tests(int use_prealloc) {
199249 ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
200250 }
201251
202- test_deprecated_flags ();
203-
204- memset (& zero_pubkey , 0 , sizeof (zero_pubkey ));
205-
206252 ecount = 0 ;
207253 ecount2 = 10 ;
208254 secp256k1_context_set_illegal_callback (sttc , counting_illegal_callback_fn , & ecount );
@@ -249,50 +295,6 @@ void run_context_tests(int use_prealloc) {
249295 secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubj , & key );
250296 secp256k1_ge_set_gej (& pub , & pubj );
251297
252- /* Verify context-type checking illegal-argument errors. */
253- memset (ctmp , 1 , 32 );
254- CHECK (secp256k1_ec_pubkey_create (sttc , & pubkey , ctmp ) == 0 );
255- CHECK (ecount == 1 );
256- VG_UNDEF (& pubkey , sizeof (pubkey ));
257- CHECK (secp256k1_ec_pubkey_create (ctx , & pubkey , ctmp ) == 1 );
258- VG_CHECK (& pubkey , sizeof (pubkey ));
259- CHECK (secp256k1_ecdsa_sign (sttc , & sig , ctmp , ctmp , NULL , NULL ) == 0 );
260- CHECK (ecount == 2 );
261- VG_UNDEF (& sig , sizeof (sig ));
262- CHECK (secp256k1_ecdsa_sign (ctx , & sig , ctmp , ctmp , NULL , NULL ) == 1 );
263- VG_CHECK (& sig , sizeof (sig ));
264- CHECK (ecount2 == 10 );
265- CHECK (secp256k1_ecdsa_verify (ctx , & sig , ctmp , & pubkey ) == 1 );
266- CHECK (ecount2 == 10 );
267- CHECK (secp256k1_ecdsa_verify (sttc , & sig , ctmp , & pubkey ) == 1 );
268- CHECK (ecount == 2 );
269- CHECK (secp256k1_ec_pubkey_tweak_add (ctx , & pubkey , ctmp ) == 1 );
270- CHECK (ecount2 == 10 );
271- CHECK (secp256k1_ec_pubkey_tweak_add (sttc , & pubkey , ctmp ) == 1 );
272- CHECK (ecount == 2 );
273- CHECK (secp256k1_ec_pubkey_tweak_mul (ctx , & pubkey , ctmp ) == 1 );
274- CHECK (ecount2 == 10 );
275- CHECK (secp256k1_ec_pubkey_negate (sttc , & pubkey ) == 1 );
276- CHECK (ecount == 2 );
277- CHECK (secp256k1_ec_pubkey_negate (ctx , & pubkey ) == 1 );
278- CHECK (ecount == 2 );
279- CHECK (secp256k1_ec_pubkey_negate (ctx , NULL ) == 0 );
280- CHECK (ecount2 == 11 );
281- CHECK (secp256k1_ec_pubkey_negate (sttc , & zero_pubkey ) == 0 );
282- CHECK (ecount == 3 );
283- CHECK (secp256k1_ec_pubkey_tweak_mul (sttc , & pubkey , ctmp ) == 1 );
284- CHECK (ecount == 3 );
285- CHECK (secp256k1_context_randomize (sttc , ctmp ) == 1 );
286- CHECK (ecount == 3 );
287- CHECK (secp256k1_context_randomize (sttc , NULL ) == 1 );
288- CHECK (ecount == 3 );
289- CHECK (secp256k1_context_randomize (ctx , ctmp ) == 1 );
290- CHECK (ecount2 == 11 );
291- CHECK (secp256k1_context_randomize (ctx , NULL ) == 1 );
292- CHECK (ecount2 == 11 );
293- secp256k1_context_set_illegal_callback (sttc , NULL , NULL );
294- secp256k1_context_set_illegal_callback (ctx , NULL , NULL );
295-
296298 /* obtain a working nonce */
297299 do {
298300 random_scalar_order_test (& nonce );
@@ -7361,7 +7363,7 @@ int main(int argc, char **argv) {
73617363 run_selftest_tests ();
73627364 run_context_tests (0 );
73637365 run_context_tests (1 );
7364- run_scratch_tests ();
7366+ run_deprecated_context_flags_test ();
73657367
73667368 ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
73677369 /* Randomize the context only with probability 15/16
@@ -7373,6 +7375,8 @@ int main(int argc, char **argv) {
73737375 CHECK (secp256k1_context_randomize (ctx , rand32 ));
73747376 }
73757377
7378+ run_scratch_tests ();
7379+
73767380 run_rand_bits ();
73777381 run_rand_int ();
73787382
@@ -7435,6 +7439,7 @@ int main(int argc, char **argv) {
74357439#endif
74367440
74377441 /* ecdsa tests */
7442+ run_ec_illegal_argument_tests ();
74387443 run_pubkey_comparison ();
74397444 run_random_pubkeys ();
74407445 run_ecdsa_der_parse ();
0 commit comments