diff --git a/src/tests.c b/src/tests.c index 004ee14ffc..e2e59b1e88 100644 --- a/src/tests.c +++ b/src/tests.c @@ -163,7 +163,7 @@ data && 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 }; @@ -181,11 +181,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; @@ -209,10 +257,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); @@ -244,7 +288,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)); @@ -282,51 +340,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); @@ -7407,6 +7420,9 @@ int main(int argc, char **argv) { memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context)); CHECK(!secp256k1_ecmult_gen_context_is_built(&sttc->ecmult_gen_ctx)); + run_deprecated_context_flags_test(); + run_ec_illegal_argument_tests(); + run_rand_bits(); run_rand_int();