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 f0f57d1 commit 45eef02
Showing 1 changed file with 68 additions and 52 deletions.
120 changes: 68 additions & 52 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 @@ -176,11 +176,59 @@ void test_deprecated_flags(void) {
secp256k1_context_destroy(none_ctx);
}

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;
secp256k1_context *my_ctx;
Expand All @@ -204,10 +252,6 @@ void run_context_tests(int use_prealloc) {
memcpy(my_sttc, secp256k1_context_static, sizeof(secp256k1_context));
CHECK(!secp256k1_ecmult_gen_context_is_built(&my_sttc->ecmult_gen_ctx));

test_deprecated_flags();

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

ecount = 0;
ecount2 = 10;
secp256k1_context_set_illegal_callback(my_sttc, counting_illegal_callback_fn, &ecount);
Expand Down Expand Up @@ -239,7 +283,21 @@ void run_context_tests(int use_prealloc) {
secp256k1_context_destroy(my_sttc);
CHECK(ecount == 2);
}
ecount = 0;

/* Randomizing secp256k1_context_static is not supported. */
{
unsigned char ctmp[32];
memset(ctmp, 1, sizeof(ctmp));
ecount = 0;
CHECK(secp256k1_context_randomize(my_sttc, ctmp) == 0);
CHECK(ecount == 1);
CHECK(secp256k1_context_randomize(my_sttc, NULL) == 0);
CHECK(ecount == 2);
CHECK(secp256k1_context_randomize(my_ctx, ctmp) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_context_randomize(my_ctx, NULL) == 1);
CHECK(ecount == 2);
}

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

/* Verify context-type checking illegal-argument errors.
TODO Move this to a separate function. */
memset(ctmp, 1, 32);
CHECK(secp256k1_ec_pubkey_create(my_sttc, &pubkey, ctmp) == 0);
CHECK(ecount == 1);
VG_UNDEF(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ec_pubkey_create(my_ctx, &pubkey, ctmp) == 1);
VG_CHECK(&pubkey, sizeof(pubkey));
CHECK(secp256k1_ecdsa_sign(my_sttc, &sig, ctmp, ctmp, NULL, NULL) == 0);
CHECK(ecount == 2);
VG_UNDEF(&sig, sizeof(sig));
CHECK(secp256k1_ecdsa_sign(my_ctx, &sig, ctmp, ctmp, NULL, NULL) == 1);
VG_CHECK(&sig, sizeof(sig));
CHECK(ecount2 == 10);
CHECK(secp256k1_ecdsa_verify(my_ctx, &sig, ctmp, &pubkey) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ecdsa_verify(my_sttc, &sig, ctmp, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_tweak_add(my_ctx, &pubkey, ctmp) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ec_pubkey_tweak_add(my_sttc, &pubkey, ctmp) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_tweak_mul(my_ctx, &pubkey, ctmp) == 1);
CHECK(ecount2 == 10);
CHECK(secp256k1_ec_pubkey_negate(my_sttc, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_negate(my_ctx, &pubkey) == 1);
CHECK(ecount == 2);
CHECK(secp256k1_ec_pubkey_negate(my_sttc, &zero_pubkey) == 0);
CHECK(ecount == 3);
CHECK(secp256k1_ec_pubkey_negate(my_ctx, NULL) == 0);
CHECK(ecount2 == 11);
CHECK(secp256k1_ec_pubkey_tweak_mul(my_sttc, &pubkey, ctmp) == 1);
CHECK(ecount == 3);
CHECK(secp256k1_context_randomize(my_sttc, ctmp) == 0);
CHECK(ecount == 4);
CHECK(secp256k1_context_randomize(my_sttc, NULL) == 0);
CHECK(ecount == 5);
CHECK(secp256k1_context_randomize(my_ctx, ctmp) == 1);
CHECK(ecount2 == 11);
CHECK(secp256k1_context_randomize(my_ctx, NULL) == 1);
CHECK(ecount2 == 11);
secp256k1_context_set_illegal_callback(my_sttc, NULL, NULL);
secp256k1_context_set_illegal_callback(my_ctx, NULL, NULL);

/* obtain a working nonce */
do {
random_scalar_order_test(&nonce);
Expand Down Expand Up @@ -7404,6 +7417,9 @@ int main(int argc, char **argv) {
memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context));
CHECK(!secp256k1_context_is_proper(sttc));

run_deprecated_context_flags_test();
run_ec_illegal_argument_tests();

run_rand_bits();
run_rand_int();

Expand Down

0 comments on commit 45eef02

Please sign in to comment.