Skip to content

Commit 45eef02

Browse files
refactor: Tidy run_context_tests() by extracting functions
1 parent f0f57d1 commit 45eef02

File tree

1 file changed

+68
-52
lines changed

1 file changed

+68
-52
lines changed

src/tests.c

+68-52
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ 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) {
162162
unsigned int flags[] = { SECP256K1_CONTEXT_SIGN,
163163
SECP256K1_CONTEXT_VERIFY,
164164
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
@@ -176,11 +176,59 @@ void test_deprecated_flags(void) {
176176
secp256k1_context_destroy(none_ctx);
177177
}
178178

179-
void run_context_tests(int use_prealloc) {
179+
void run_ec_illegal_argument_tests(void) {
180+
int ecount = 0;
181+
int ecount2 = 10;
180182
secp256k1_pubkey pubkey;
181183
secp256k1_pubkey zero_pubkey;
182184
secp256k1_ecdsa_signature sig;
183185
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) {
184232
int32_t ecount;
185233
int32_t ecount2;
186234
secp256k1_context *my_ctx;
@@ -204,10 +252,6 @@ void run_context_tests(int use_prealloc) {
204252
memcpy(my_sttc, secp256k1_context_static, sizeof(secp256k1_context));
205253
CHECK(!secp256k1_ecmult_gen_context_is_built(&my_sttc->ecmult_gen_ctx));
206254

207-
test_deprecated_flags();
208-
209-
memset(&zero_pubkey, 0, sizeof(zero_pubkey));
210-
211255
ecount = 0;
212256
ecount2 = 10;
213257
secp256k1_context_set_illegal_callback(my_sttc, counting_illegal_callback_fn, &ecount);
@@ -239,7 +283,21 @@ void run_context_tests(int use_prealloc) {
239283
secp256k1_context_destroy(my_sttc);
240284
CHECK(ecount == 2);
241285
}
242-
ecount = 0;
286+
287+
/* Randomizing secp256k1_context_static is not supported. */
288+
{
289+
unsigned char ctmp[32];
290+
memset(ctmp, 1, sizeof(ctmp));
291+
ecount = 0;
292+
CHECK(secp256k1_context_randomize(my_sttc, ctmp) == 0);
293+
CHECK(ecount == 1);
294+
CHECK(secp256k1_context_randomize(my_sttc, NULL) == 0);
295+
CHECK(ecount == 2);
296+
CHECK(secp256k1_context_randomize(my_ctx, ctmp) == 1);
297+
CHECK(ecount == 2);
298+
CHECK(secp256k1_context_randomize(my_ctx, NULL) == 1);
299+
CHECK(ecount == 2);
300+
}
243301

244302
/* check if sizes for cloning are consistent */
245303
CHECK(secp256k1_context_preallocated_clone_size(my_ctx) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
@@ -277,51 +335,6 @@ void run_context_tests(int use_prealloc) {
277335
secp256k1_ecmult_gen(&my_ctx->ecmult_gen_ctx, &pubj, &key);
278336
secp256k1_ge_set_gej(&pub, &pubj);
279337

280-
/* Verify context-type checking illegal-argument errors.
281-
TODO Move this to a separate function. */
282-
memset(ctmp, 1, 32);
283-
CHECK(secp256k1_ec_pubkey_create(my_sttc, &pubkey, ctmp) == 0);
284-
CHECK(ecount == 1);
285-
VG_UNDEF(&pubkey, sizeof(pubkey));
286-
CHECK(secp256k1_ec_pubkey_create(my_ctx, &pubkey, ctmp) == 1);
287-
VG_CHECK(&pubkey, sizeof(pubkey));
288-
CHECK(secp256k1_ecdsa_sign(my_sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
289-
CHECK(ecount == 2);
290-
VG_UNDEF(&sig, sizeof(sig));
291-
CHECK(secp256k1_ecdsa_sign(my_ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
292-
VG_CHECK(&sig, sizeof(sig));
293-
CHECK(ecount2 == 10);
294-
CHECK(secp256k1_ecdsa_verify(my_ctx, &sig, ctmp, &pubkey) == 1);
295-
CHECK(ecount2 == 10);
296-
CHECK(secp256k1_ecdsa_verify(my_sttc, &sig, ctmp, &pubkey) == 1);
297-
CHECK(ecount == 2);
298-
CHECK(secp256k1_ec_pubkey_tweak_add(my_ctx, &pubkey, ctmp) == 1);
299-
CHECK(ecount2 == 10);
300-
CHECK(secp256k1_ec_pubkey_tweak_add(my_sttc, &pubkey, ctmp) == 1);
301-
CHECK(ecount == 2);
302-
CHECK(secp256k1_ec_pubkey_tweak_mul(my_ctx, &pubkey, ctmp) == 1);
303-
CHECK(ecount2 == 10);
304-
CHECK(secp256k1_ec_pubkey_negate(my_sttc, &pubkey) == 1);
305-
CHECK(ecount == 2);
306-
CHECK(secp256k1_ec_pubkey_negate(my_ctx, &pubkey) == 1);
307-
CHECK(ecount == 2);
308-
CHECK(secp256k1_ec_pubkey_negate(my_sttc, &zero_pubkey) == 0);
309-
CHECK(ecount == 3);
310-
CHECK(secp256k1_ec_pubkey_negate(my_ctx, NULL) == 0);
311-
CHECK(ecount2 == 11);
312-
CHECK(secp256k1_ec_pubkey_tweak_mul(my_sttc, &pubkey, ctmp) == 1);
313-
CHECK(ecount == 3);
314-
CHECK(secp256k1_context_randomize(my_sttc, ctmp) == 0);
315-
CHECK(ecount == 4);
316-
CHECK(secp256k1_context_randomize(my_sttc, NULL) == 0);
317-
CHECK(ecount == 5);
318-
CHECK(secp256k1_context_randomize(my_ctx, ctmp) == 1);
319-
CHECK(ecount2 == 11);
320-
CHECK(secp256k1_context_randomize(my_ctx, NULL) == 1);
321-
CHECK(ecount2 == 11);
322-
secp256k1_context_set_illegal_callback(my_sttc, NULL, NULL);
323-
secp256k1_context_set_illegal_callback(my_ctx, NULL, NULL);
324-
325338
/* obtain a working nonce */
326339
do {
327340
random_scalar_order_test(&nonce);
@@ -7404,6 +7417,9 @@ int main(int argc, char **argv) {
74047417
memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context));
74057418
CHECK(!secp256k1_context_is_proper(sttc));
74067419

7420+
run_deprecated_context_flags_test();
7421+
run_ec_illegal_argument_tests();
7422+
74077423
run_rand_bits();
74087424
run_rand_int();
74097425

0 commit comments

Comments
 (0)