Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/tests.c
Show First 20 Lines • Show All 5,531 Lines • ▼ Show 20 Lines | void run_cmov_tests(void) { | ||||
int_cmov_test(); | int_cmov_test(); | ||||
fe_cmov_test(); | fe_cmov_test(); | ||||
fe_storage_cmov_test(); | fe_storage_cmov_test(); | ||||
scalar_cmov_test(); | scalar_cmov_test(); | ||||
ge_storage_cmov_test(); | ge_storage_cmov_test(); | ||||
} | } | ||||
int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||||
unsigned char seed16[16] = {0}; | |||||
unsigned char run32[32] = {0}; | |||||
/* Disable buffering for stdout to improve reliability of getting | /* Disable buffering for stdout to improve reliability of getting | ||||
* diagnostic information. Happens right at the start of main because | * diagnostic information. Happens right at the start of main because | ||||
* setbuf must be used before any other operation on the stream. */ | * setbuf must be used before any other operation on the stream. */ | ||||
setbuf(stdout, NULL); | setbuf(stdout, NULL); | ||||
/* Also disable buffering for stderr because it's not guaranteed that it's | /* Also disable buffering for stderr because it's not guaranteed that it's | ||||
* unbuffered on all systems. */ | * unbuffered on all systems. */ | ||||
setbuf(stderr, NULL); | setbuf(stderr, NULL); | ||||
/* find iteration count */ | /* find iteration count */ | ||||
if (argc > 1) { | if (argc > 1) { | ||||
count = strtol(argv[1], NULL, 0); | count = strtol(argv[1], NULL, 0); | ||||
} | } | ||||
printf("test count = %i\n", count); | |||||
/* find random seed */ | /* find random seed */ | ||||
if (argc > 2) { | secp256k1_rand_init(argc > 2 ? argv[2] : NULL); | ||||
int pos = 0; | |||||
const char* ch = argv[2]; | |||||
while (pos < 16 && ch[0] != 0 && ch[1] != 0) { | |||||
unsigned short sh; | |||||
if ((sscanf(ch, "%2hx", &sh)) == 1) { | |||||
seed16[pos] = sh; | |||||
} else { | |||||
break; | |||||
} | |||||
ch += 2; | |||||
pos++; | |||||
} | |||||
} else { | |||||
FILE *frand = fopen("/dev/urandom", "r"); | |||||
if ((frand == NULL) || fread(&seed16, 1, sizeof(seed16), frand) != sizeof(seed16)) { | |||||
uint64_t t = time(NULL) * (uint64_t)1337; | |||||
fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n"); | |||||
seed16[0] ^= t; | |||||
seed16[1] ^= t >> 8; | |||||
seed16[2] ^= t >> 16; | |||||
seed16[3] ^= t >> 24; | |||||
seed16[4] ^= t >> 32; | |||||
seed16[5] ^= t >> 40; | |||||
seed16[6] ^= t >> 48; | |||||
seed16[7] ^= t >> 56; | |||||
} | |||||
if (frand) { | |||||
fclose(frand); | |||||
} | |||||
} | |||||
secp256k1_rand_seed(seed16); | |||||
printf("test count = %i\n", count); | |||||
printf("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]); | |||||
/* initialize */ | /* initialize */ | ||||
run_context_tests(0); | run_context_tests(0); | ||||
run_context_tests(1); | run_context_tests(1); | ||||
run_scratch_tests(); | run_scratch_tests(); | ||||
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); | ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); | ||||
if (secp256k1_rand_bits(1)) { | if (secp256k1_rand_bits(1)) { | ||||
secp256k1_rand256(run32); | unsigned char rand32[32]; | ||||
CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? run32 : NULL)); | secp256k1_rand256(rand32); | ||||
CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? rand32 : NULL)); | |||||
} | } | ||||
run_rand_bits(); | run_rand_bits(); | ||||
run_rand_int(); | run_rand_int(); | ||||
run_sha256_tests(); | run_sha256_tests(); | ||||
run_hmac_sha256_tests(); | run_hmac_sha256_tests(); | ||||
run_rfc6979_hmac_sha256_tests(); | run_rfc6979_hmac_sha256_tests(); | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | #ifdef ENABLE_MODULE_SCHNORRSIG | ||||
run_schnorrsig_tests(); | run_schnorrsig_tests(); | ||||
#endif | #endif | ||||
/* util tests */ | /* util tests */ | ||||
run_memczero_test(); | run_memczero_test(); | ||||
run_cmov_tests(); | run_cmov_tests(); | ||||
secp256k1_rand256(run32); | secp256k1_rand_finish(); | ||||
printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]); | |||||
/* shutdown */ | /* shutdown */ | ||||
secp256k1_context_destroy(ctx); | secp256k1_context_destroy(ctx); | ||||
printf("no problems found\n"); | printf("no problems found\n"); | ||||
return 0; | return 0; | ||||
} | } |