@@ -158,27 +158,77 @@ int context_eq(const secp256k1_context *a, const secp256k1_context *b) {
158
158
&& a -> error_callback .data == b -> error_callback .data ;
159
159
}
160
160
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. */
162
164
unsigned int flags [] = { SECP256K1_CONTEXT_SIGN ,
163
165
SECP256K1_CONTEXT_VERIFY ,
164
166
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
167
+ secp256k1_context * none_ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
165
168
int i ;
166
- /* Check that a context created with any of the flags in the flags array is
167
- * 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
- void run_context_tests (int use_prealloc ) {
179
+ void run_ec_illegal_argument_tests (void ) {
180
+ int ecount = 0 ;
181
+ int ecount2 = 10 ;
178
182
secp256k1_pubkey pubkey ;
179
183
secp256k1_pubkey zero_pubkey ;
180
184
secp256k1_ecdsa_signature sig ;
181
185
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 ) {
182
232
int32_t ecount ;
183
233
int32_t ecount2 ;
184
234
void * ctx_prealloc = NULL ;
@@ -199,10 +249,6 @@ void run_context_tests(int use_prealloc) {
199
249
ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
200
250
}
201
251
202
- test_deprecated_flags ();
203
-
204
- memset (& zero_pubkey , 0 , sizeof (zero_pubkey ));
205
-
206
252
ecount = 0 ;
207
253
ecount2 = 10 ;
208
254
secp256k1_context_set_illegal_callback (sttc , counting_illegal_callback_fn , & ecount );
@@ -249,50 +295,6 @@ void run_context_tests(int use_prealloc) {
249
295
secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & pubj , & key );
250
296
secp256k1_ge_set_gej (& pub , & pubj );
251
297
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
-
296
298
/* obtain a working nonce */
297
299
do {
298
300
random_scalar_order_test (& nonce );
@@ -7361,7 +7363,7 @@ int main(int argc, char **argv) {
7361
7363
run_selftest_tests ();
7362
7364
run_context_tests (0 );
7363
7365
run_context_tests (1 );
7364
- run_scratch_tests ();
7366
+ run_deprecated_context_flags_test ();
7365
7367
7366
7368
ctx = secp256k1_context_create (SECP256K1_CONTEXT_NONE );
7367
7369
/* Randomize the context only with probability 15/16
@@ -7373,6 +7375,8 @@ int main(int argc, char **argv) {
7373
7375
CHECK (secp256k1_context_randomize (ctx , rand32 ));
7374
7376
}
7375
7377
7378
+ run_scratch_tests ();
7379
+
7376
7380
run_rand_bits ();
7377
7381
run_rand_int ();
7378
7382
@@ -7435,6 +7439,7 @@ int main(int argc, char **argv) {
7435
7439
#endif
7436
7440
7437
7441
/* ecdsa tests */
7442
+ run_ec_illegal_argument_tests ();
7438
7443
run_pubkey_comparison ();
7439
7444
run_random_pubkeys ();
7440
7445
run_ecdsa_der_parse ();
0 commit comments