This commit is contained in:
DarkWindman
2026-02-26 14:24:06 +02:00
36 changed files with 1138 additions and 574 deletions

View File

@@ -25,6 +25,8 @@
#include "checkmem.h"
#include "testutil.h"
#include "util.h"
#include "unit_test.h"
#include "unit_test.c"
#include "../contrib/lax_der_parsing.c"
#include "../contrib/lax_der_privatekey_parsing.c"
@@ -37,7 +39,6 @@
#define CONDITIONAL_TEST(cnt, nam) if (COUNT < (cnt)) { printf("Skipping %s (iteration count too low)\n", nam); } else
static int COUNT = 16;
static secp256k1_context *CTX = NULL;
static secp256k1_context *STATIC_CTX = NULL;
@@ -273,6 +274,12 @@ static void run_static_context_tests(int use_prealloc) {
}
}
static void run_all_static_context_tests(void)
{
run_static_context_tests(0);
run_static_context_tests(1);
}
static void run_proper_context_tests(int use_prealloc) {
int32_t dummy = 0;
secp256k1_context *my_ctx, *my_ctx_fresh;
@@ -395,6 +402,12 @@ static void run_proper_context_tests(int use_prealloc) {
secp256k1_context_preallocated_destroy(NULL);
}
static void run_all_proper_context_tests(void)
{
run_proper_context_tests(0);
run_proper_context_tests(1);
}
static void run_scratch_tests(void) {
const size_t adj_alloc = ((500 + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
@@ -6198,12 +6211,7 @@ static void run_ec_pubkey_parse_test(void) {
}
static void run_eckey_edge_case_test(void) {
const unsigned char orderc[32] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
};
const unsigned char *orderc = secp256k1_group_order_bytes;
const unsigned char zeros[sizeof(secp256k1_pubkey)] = {0x00};
unsigned char ctmp[33];
unsigned char ctmp2[33];
@@ -6517,13 +6525,7 @@ static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char
return 1;
}
if (counter < 5) {
static const unsigned char order[] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,
0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41
};
memcpy(nonce32, order, 32);
memcpy(nonce32, secp256k1_group_order_bytes, 32);
if (counter == 4) {
nonce32[31]++;
}
@@ -7541,12 +7543,7 @@ static void test_ecdsa_edge_cases(void) {
/* Privkey export where pubkey is the point at infinity. */
{
unsigned char privkey[300];
unsigned char seckey[32] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41,
};
const unsigned char *seckey = secp256k1_group_order_bytes;
size_t outlen = 300;
CHECK(!ec_privkey_export_der(CTX, privkey, &outlen, seckey, 0));
outlen = 300;
@@ -7860,38 +7857,137 @@ static void run_cmov_tests(void) {
ge_storage_cmov_test();
}
int main(int argc, char **argv) {
/* Disable buffering for stdout to improve reliability of getting
* diagnostic information. Happens right at the start of main because
* setbuf must be used before any other operation on the stream. */
setbuf(stdout, NULL);
/* Also disable buffering for stderr because it's not guaranteed that it's
* unbuffered on all systems. */
setbuf(stderr, NULL);
/* --------------------------------------------------------- */
/* Test Registry */
/* --------------------------------------------------------- */
/* find iteration count */
if (argc > 1) {
COUNT = strtol(argv[1], NULL, 0);
} else {
const char* env = getenv("SECP256K1_TEST_ITERS");
if (env && strlen(env) > 0) {
COUNT = strtol(env, NULL, 0);
}
}
if (COUNT <= 0) {
fputs("An iteration count of 0 or less is not allowed.\n", stderr);
return EXIT_FAILURE;
}
printf("test count = %i\n", COUNT);
/* --- Special test cases that must run before RNG initialization --- */
static const struct tf_test_entry tests_no_rng[] = {
CASE(xoshiro256pp_tests),
};
static const struct tf_test_module registry_modules_no_rng = MAKE_TEST_MODULE(no_rng);
/* run test RNG tests (must run before we really initialize the test RNG) */
run_xoshiro256pp_tests();
/* --- Standard test cases start here --- */
static const struct tf_test_entry tests_general[] = {
CASE(selftest_tests),
CASE(all_proper_context_tests),
CASE(all_static_context_tests),
CASE(deprecated_context_flags_test),
CASE(scratch_tests),
};
/* find random seed */
testrand_init(argc > 2 ? argv[2] : NULL);
static const struct tf_test_entry tests_integer[] = {
#ifdef SECP256K1_WIDEMUL_INT128
CASE(int128_tests),
#endif
CASE(ctz_tests),
CASE(modinv_tests),
CASE(inverse_tests),
};
/*** Setup test environment ***/
static const struct tf_test_entry tests_hash[] = {
CASE(sha256_known_output_tests),
CASE(sha256_counter_tests),
CASE(hmac_sha256_tests),
CASE(rfc6979_hmac_sha256_tests),
CASE(tagged_sha256_tests),
};
static const struct tf_test_entry tests_scalar[] = {
CASE(scalar_tests),
};
static const struct tf_test_entry tests_field[] = {
CASE(field_half),
CASE(field_misc),
CASE(field_convert),
CASE(field_be32_overflow),
CASE(fe_mul),
CASE(sqr),
CASE(sqrt),
};
static const struct tf_test_entry tests_group[] = {
CASE(ge),
CASE(gej),
CASE(group_decompress),
};
static const struct tf_test_entry tests_ecmult[] = {
CASE(ecmult_pre_g),
CASE(wnaf),
CASE(point_times_order),
CASE(ecmult_near_split_bound),
CASE(ecmult_chain),
CASE(ecmult_constants),
CASE(ecmult_gen_blind),
CASE(ecmult_const_tests),
CASE(ecmult_multi_tests),
CASE(ec_combine),
};
static const struct tf_test_entry tests_ec[] = {
CASE(endomorphism_tests),
CASE(ec_pubkey_parse_test),
CASE(eckey_edge_case_test),
CASE(eckey_negate_test),
};
static const struct tf_test_entry tests_ecdsa[] = {
CASE(ec_illegal_argument_tests),
CASE(pubkey_comparison),
CASE(pubkey_sort),
CASE(random_pubkeys),
CASE(ecdsa_der_parse),
CASE(ecdsa_sign_verify),
CASE(ecdsa_end_to_end),
CASE(ecdsa_edge_cases),
CASE(ecdsa_wycheproof),
};
static const struct tf_test_entry tests_utils[] = {
CASE(hsort_tests),
CASE(secp256k1_memczero_test),
CASE(secp256k1_is_zero_array_test),
CASE(secp256k1_byteorder_tests),
CASE(cmov_tests),
};
/* Register test modules */
static const struct tf_test_module registry_modules[] = {
MAKE_TEST_MODULE(general),
MAKE_TEST_MODULE(integer),
MAKE_TEST_MODULE(hash),
MAKE_TEST_MODULE(scalar),
MAKE_TEST_MODULE(field),
MAKE_TEST_MODULE(group),
MAKE_TEST_MODULE(ecmult),
MAKE_TEST_MODULE(ec),
#ifdef ENABLE_MODULE_ECDH
MAKE_TEST_MODULE(ecdh),
#endif
MAKE_TEST_MODULE(ecdsa),
#ifdef ENABLE_MODULE_RECOVERY
/* ECDSA pubkey recovery tests */
MAKE_TEST_MODULE(recovery),
#endif
#ifdef ENABLE_MODULE_EXTRAKEYS
MAKE_TEST_MODULE(extrakeys),
#endif
#ifdef ENABLE_MODULE_SCHNORRSIG
MAKE_TEST_MODULE(schnorrsig),
#endif
#ifdef ENABLE_MODULE_MUSIG
MAKE_TEST_MODULE(musig),
#endif
#ifdef ENABLE_MODULE_ELLSWIFT
MAKE_TEST_MODULE(ellswift),
#endif
MAKE_TEST_MODULE(utils),
};
/* Setup test environment */
static int setup(void) {
/* Create a global context available to all tests */
CTX = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
/* Randomize the context only with probability 15/16
@@ -7910,167 +8006,28 @@ int main(int argc, char **argv) {
CHECK(STATIC_CTX != NULL);
memcpy(STATIC_CTX, secp256k1_context_static, sizeof(secp256k1_context));
CHECK(!secp256k1_context_is_proper(STATIC_CTX));
return 0;
}
/*** Run actual tests ***/
/* selftest tests */
run_selftest_tests();
/* context tests */
run_proper_context_tests(0); run_proper_context_tests(1);
run_static_context_tests(0); run_static_context_tests(1);
run_deprecated_context_flags_test();
/* scratch tests */
run_scratch_tests();
/* util tests */
run_util_tests();
/* integer arithmetic tests */
#ifdef SECP256K1_WIDEMUL_INT128
run_int128_tests();
#endif
run_ctz_tests();
run_modinv_tests();
run_inverse_tests();
/* sorting tests */
run_hsort_tests();
/* hash tests */
run_sha256_known_output_tests();
run_sha256_counter_tests();
run_hmac_sha256_tests();
run_rfc6979_hmac_sha256_tests();
run_tagged_sha256_tests();
/* scalar tests */
run_scalar_tests();
/* field tests */
run_field_half();
run_field_misc();
run_field_convert();
run_field_be32_overflow();
run_fe_mul();
run_sqr();
run_sqrt();
/* group tests */
run_ge();
run_gej();
run_group_decompress();
/* ecmult tests */
run_ecmult_pre_g();
run_wnaf();
run_point_times_order();
run_ecmult_near_split_bound();
run_ecmult_chain();
run_ecmult_constants();
run_ecmult_gen_blind();
run_ecmult_const_tests();
run_ecmult_multi_tests();
run_ec_combine();
run_ec_commit();
/* endomorphism tests */
run_endomorphism_tests();
/* EC point parser test */
run_ec_pubkey_parse_test();
/* EC key edge cases */
run_eckey_edge_case_test();
/* EC key arithmetic test */
run_eckey_negate_test();
#ifdef ENABLE_MODULE_SCHNORRSIG_HALFAGG
run_schnorrsig_halfagg_tests();
#endif
#ifdef ENABLE_MODULE_BPPP
run_bppp_tests();
#endif
#ifdef ENABLE_MODULE_ECDH
/* ecdh tests */
run_ecdh_tests();
#endif
/* ecdsa tests */
run_ec_illegal_argument_tests();
run_pubkey_comparison();
run_pubkey_sort();
run_random_pubkeys();
run_ecdsa_der_parse();
run_ecdsa_sign_verify();
run_ecdsa_end_to_end();
run_ecdsa_edge_cases();
run_ecdsa_wycheproof();
#ifdef ENABLE_MODULE_RECOVERY
/* ECDSA pubkey recovery tests */
run_recovery_tests();
#endif
#ifdef ENABLE_MODULE_GENERATOR
run_generator_tests();
#endif
#ifdef ENABLE_MODULE_RANGEPROOF
run_rangeproof_tests();
#endif
#ifdef ENABLE_MODULE_WHITELIST
/* Key whitelisting tests */
run_whitelist_tests();
#endif
#ifdef ENABLE_MODULE_SURJECTIONPROOF
run_surjection_tests();
#endif
#ifdef ENABLE_MODULE_EXTRAKEYS
run_extrakeys_tests();
#endif
#ifdef ENABLE_MODULE_SCHNORRSIG
run_schnorrsig_tests();
#endif
#ifdef ENABLE_MODULE_MUSIG
run_musig_tests();
#endif
#ifdef ENABLE_MODULE_ELLSWIFT
run_ellswift_tests();
#endif
#ifdef ENABLE_MODULE_ECDSA_S2C
/* ECDSA sign to contract */
run_ecdsa_s2c_tests();
#endif
#ifdef ENABLE_MODULE_ECDSA_ADAPTOR
run_ecdsa_adaptor_tests();
#endif
/* util tests */
run_secp256k1_memczero_test();
run_secp256k1_is_zero_array_test();
run_secp256k1_byteorder_tests();
run_cmov_tests();
/*** Tear down test environment ***/
/* Shutdown test environment */
static int teardown(void) {
free(STATIC_CTX);
secp256k1_context_destroy(CTX);
testrand_finish();
printf("no problems found\n");
return EXIT_SUCCESS;
return 0;
}
int main(int argc, char **argv) {
struct tf_framework tf = {0};
tf.registry_modules = registry_modules;
tf.num_modules = sizeof(registry_modules) / sizeof(registry_modules[0]);
tf.registry_no_rng = &registry_modules_no_rng;
/* Add context creation/destruction functions */
tf.fn_setup = setup;
tf.fn_teardown = teardown;
/* Init and run framework */
if (tf_init(&tf, argc, argv) != 0) return EXIT_FAILURE;
return tf_run(&tf);
}