Skip to content

Commit

Permalink
refactor: Tidy run_context_tests() by extracting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Jan 4, 2023
1 parent 18e0db3 commit fc16b69
Showing 1 changed file with 52 additions and 50 deletions.
102 changes: 52 additions & 50 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int context_eq(const secp256k1_context *a, const secp256k1_context *b) {
&& a->error_callback.data == b->error_callback.data;
}

void test_deprecated_flags(void) {
void run_deprecated_context_flags_test(void) {
unsigned int flags[] = { SECP256K1_CONTEXT_SIGN,
SECP256K1_CONTEXT_VERIFY,
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY };
Expand All @@ -174,11 +174,59 @@ void test_deprecated_flags(void) {
}
}

void run_context_tests(int use_prealloc) {
void run_ec_illegal_argument_tests(void) {
int ecount = 0;
int ecount2 = 10;
secp256k1_pubkey pubkey;
secp256k1_pubkey zero_pubkey;
secp256k1_ecdsa_signature sig;
unsigned char ctmp[32];

/* Setup */
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount2);
memset(ctmp, 1, 32);
memset(&zero_pubkey, 0, sizeof(zero_pubkey));

/* Verify context-type checking illegal-argument errors. */
CHECK(secp256k1_ec_pubkey_create(sttc, &pubkey, ctmp) == 0);
CHECK(ecount == 1);
VG_UNDEF(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
VG_CHECK(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ecdsa_sign(sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
CHECK(ecount == 2);
VG_UNDEF(&sig, sizeof(sig));
CHECK(secp256k1_ecdsa_sign(ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
VG_CHECK(&sig, sizeof(sig));
CHECK(ecount2 == 10);
CHECK(secp256k1_ecdsa_verify(ctx, &sig, ctmp, &pubkey) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ecdsa_verify(sttc, &sig, ctmp, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ec_pubkey_tweak_add(sttc, &pubkey, ctmp) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ec_pubkey_negate(sttc, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_negate(sttc, &zero_pubkey) == 0);
CHECK(ecount == 3);
CHECK(secp256k1_ec_pubkey_negate(ctx, NULL) == 0);
CHECK(ecount2 == 11);
CHECK(secp256k1_ec_pubkey_tweak_mul(sttc, &pubkey, ctmp) == 1);
CHECK(ecount == 3);

/* Clean up */
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
secp256k1_context_set_illegal_callback(ctx, NULL, NULL);
}

void run_context_tests(int use_prealloc) {
int32_t ecount;
int32_t ecount2;
void *ctx_prealloc = NULL;
Expand All @@ -199,10 +247,6 @@ void run_context_tests(int use_prealloc) {
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
}

test_deprecated_flags();

memset(&zero_pubkey, 0, sizeof(zero_pubkey));

ecount = 0;
ecount2 = 10;
secp256k1_context_set_illegal_callback(sttc, counting_illegal_callback_fn, &ecount);
Expand Down Expand Up @@ -249,50 +293,6 @@ void run_context_tests(int use_prealloc) {
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key);
secp256k1_ge_set_gej(&pub, &pubj);

/* Verify context-type checking illegal-argument errors. */
memset(ctmp, 1, 32);
CHECK(secp256k1_ec_pubkey_create(sttc, &pubkey, ctmp) == 0);
CHECK(ecount == 1);
VG_UNDEF(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, ctmp) == 1);
VG_CHECK(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ecdsa_sign(sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
CHECK(ecount == 2);
VG_UNDEF(&sig, sizeof(sig));
CHECK(secp256k1_ecdsa_sign(ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
VG_CHECK(&sig, sizeof(sig));
CHECK(ecount2 == 10);
CHECK(secp256k1_ecdsa_verify(ctx, &sig, ctmp, &pubkey) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ecdsa_verify(sttc, &sig, ctmp, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ec_pubkey_tweak_add(sttc, &pubkey, ctmp) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ec_pubkey_negate(sttc, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_negate(ctx, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_negate(ctx, NULL) == 0);
CHECK(ecount2 == 11);
CHECK(secp256k1_ec_pubkey_negate(sttc, &zero_pubkey) == 0);
CHECK(ecount == 3);
CHECK(secp256k1_ec_pubkey_tweak_mul(sttc, &pubkey, ctmp) == 1);
CHECK(ecount == 3);
CHECK(secp256k1_context_randomize(sttc, ctmp) == 1);
CHECK(ecount == 3);
CHECK(secp256k1_context_randomize(sttc, NULL) == 1);
CHECK(ecount == 3);
CHECK(secp256k1_context_randomize(ctx, ctmp) == 1);
CHECK(ecount2 == 11);
CHECK(secp256k1_context_randomize(ctx, NULL) == 1);
CHECK(ecount2 == 11);
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
secp256k1_context_set_illegal_callback(ctx, NULL, NULL);

/* obtain a working nonce */
do {
random_scalar_order_test(&nonce);
Expand Down Expand Up @@ -7361,6 +7361,7 @@ int main(int argc, char **argv) {
run_selftest_tests();
run_context_tests(0);
run_context_tests(1);
run_deprecated_context_flags_test();
run_scratch_tests();

ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
Expand Down Expand Up @@ -7435,6 +7436,7 @@ int main(int argc, char **argv) {
#endif

/* ecdsa tests */
run_ec_illegal_argument_tests();
run_pubkey_comparison();
run_random_pubkeys();
run_ecdsa_der_parse();
Expand Down

0 comments on commit fc16b69

Please sign in to comment.