Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/tests.c
Show All 10 Lines | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <time.h> | #include <time.h> | ||||
#include "secp256k1.c" | #include "secp256k1.c" | ||||
#include "include/secp256k1.h" | #include "include/secp256k1.h" | ||||
#include "include/secp256k1_preallocated.h" | |||||
#include "testrand_impl.h" | #include "testrand_impl.h" | ||||
#ifdef ENABLE_OPENSSL_TESTS | #ifdef ENABLE_OPENSSL_TESTS | ||||
#include "openssl/bn.h" | #include "openssl/bn.h" | ||||
#include "openssl/ec.h" | #include "openssl/ec.h" | ||||
#include "openssl/ecdsa.h" | #include "openssl/ecdsa.h" | ||||
#include "openssl/obj_mac.h" | #include "openssl/obj_mac.h" | ||||
# if OPENSSL_VERSION_NUMBER < 0x10100000L | # if OPENSSL_VERSION_NUMBER < 0x10100000L | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | do { | ||||
secp256k1_scalar_set_b32(num, b32, &overflow); | secp256k1_scalar_set_b32(num, b32, &overflow); | ||||
if (overflow || secp256k1_scalar_is_zero(num)) { | if (overflow || secp256k1_scalar_is_zero(num)) { | ||||
continue; | continue; | ||||
} | } | ||||
break; | break; | ||||
} while(1); | } while(1); | ||||
} | } | ||||
void run_context_tests(void) { | void run_context_tests(int use_prealloc) { | ||||
secp256k1_pubkey pubkey; | secp256k1_pubkey pubkey; | ||||
secp256k1_pubkey zero_pubkey; | secp256k1_pubkey zero_pubkey; | ||||
secp256k1_ecdsa_signature sig; | secp256k1_ecdsa_signature sig; | ||||
unsigned char ctmp[32]; | unsigned char ctmp[32]; | ||||
int32_t ecount; | int32_t ecount; | ||||
int32_t ecount2; | int32_t ecount2; | ||||
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE); | secp256k1_context *none; | ||||
secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); | secp256k1_context *sign; | ||||
secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); | secp256k1_context *vrfy; | ||||
secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); | secp256k1_context *both; | ||||
void *none_prealloc = NULL; | |||||
void *sign_prealloc = NULL; | |||||
void *vrfy_prealloc = NULL; | |||||
void *both_prealloc = NULL; | |||||
secp256k1_gej pubj; | secp256k1_gej pubj; | ||||
secp256k1_ge pub; | secp256k1_ge pub; | ||||
secp256k1_scalar msg, key, nonce; | secp256k1_scalar msg, key, nonce; | ||||
secp256k1_scalar sigr, sigs; | secp256k1_scalar sigr, sigs; | ||||
if (use_prealloc) { | |||||
none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); | |||||
sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); | |||||
vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); | |||||
both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); | |||||
CHECK(none_prealloc != NULL); | |||||
CHECK(sign_prealloc != NULL); | |||||
CHECK(vrfy_prealloc != NULL); | |||||
CHECK(both_prealloc != NULL); | |||||
none = secp256k1_context_preallocated_create(none_prealloc, SECP256K1_CONTEXT_NONE); | |||||
sign = secp256k1_context_preallocated_create(sign_prealloc, SECP256K1_CONTEXT_SIGN); | |||||
vrfy = secp256k1_context_preallocated_create(vrfy_prealloc, SECP256K1_CONTEXT_VERIFY); | |||||
both = secp256k1_context_preallocated_create(both_prealloc, SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); | |||||
} else { | |||||
none = secp256k1_context_create(SECP256K1_CONTEXT_NONE); | |||||
sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); | |||||
vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); | |||||
both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); | |||||
} | |||||
memset(&zero_pubkey, 0, sizeof(zero_pubkey)); | memset(&zero_pubkey, 0, sizeof(zero_pubkey)); | ||||
ecount = 0; | ecount = 0; | ||||
ecount2 = 10; | ecount2 = 10; | ||||
secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount); | secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount); | ||||
secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount2); | secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount2); | ||||
secp256k1_context_set_error_callback(sign, counting_illegal_callback_fn, NULL); | secp256k1_context_set_error_callback(sign, counting_illegal_callback_fn, NULL); | ||||
CHECK(vrfy->error_callback.fn != sign->error_callback.fn); | CHECK(vrfy->error_callback.fn != sign->error_callback.fn); | ||||
/* check if sizes for cloning are consistent */ | |||||
CHECK(secp256k1_context_preallocated_clone_size(none) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); | |||||
CHECK(secp256k1_context_preallocated_clone_size(sign) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); | |||||
CHECK(secp256k1_context_preallocated_clone_size(vrfy) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); | |||||
CHECK(secp256k1_context_preallocated_clone_size(both) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); | |||||
/*** clone and destroy all of them to make sure cloning was complete ***/ | /*** clone and destroy all of them to make sure cloning was complete ***/ | ||||
{ | { | ||||
secp256k1_context *ctx_tmp; | secp256k1_context *ctx_tmp; | ||||
ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_destroy(ctx_tmp); | if (use_prealloc) { | ||||
ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_destroy(ctx_tmp); | /* clone into a non-preallocated context and then again into a new preallocated one. */ | ||||
ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_destroy(ctx_tmp); | ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp); | ||||
ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_destroy(ctx_tmp); | free(none_prealloc); none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(none_prealloc != NULL); | ||||
ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, none_prealloc); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(sign_prealloc); sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(sign_prealloc != NULL); | |||||
ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, sign_prealloc); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(vrfy_prealloc); vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(vrfy_prealloc != NULL); | |||||
ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, vrfy_prealloc); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(both_prealloc); both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(both_prealloc != NULL); | |||||
ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, both_prealloc); secp256k1_context_destroy(ctx_tmp); | |||||
} else { | |||||
/* clone into a preallocated context and then again into a new non-preallocated one. */ | |||||
void *prealloc_tmp; | |||||
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL); | |||||
ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(prealloc_tmp); | |||||
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(prealloc_tmp != NULL); | |||||
ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(prealloc_tmp); | |||||
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL); | |||||
ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(prealloc_tmp); | |||||
prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL); | |||||
ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, prealloc_tmp); secp256k1_context_destroy(ctx_tmp); | |||||
ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp); | |||||
free(prealloc_tmp); | |||||
} | |||||
} | } | ||||
/* Verify that the error callback makes it across the clone. */ | /* Verify that the error callback makes it across the clone. */ | ||||
CHECK(vrfy->error_callback.fn != sign->error_callback.fn); | CHECK(vrfy->error_callback.fn != sign->error_callback.fn); | ||||
/* And that it resets back to default. */ | /* And that it resets back to default. */ | ||||
secp256k1_context_set_error_callback(sign, NULL, NULL); | secp256k1_context_set_error_callback(sign, NULL, NULL); | ||||
CHECK(vrfy->error_callback.fn == sign->error_callback.fn); | CHECK(vrfy->error_callback.fn == sign->error_callback.fn); | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | void run_context_tests(int use_prealloc) { | ||||
CHECK(ecount == 3); | CHECK(ecount == 3); | ||||
CHECK(secp256k1_context_randomize(sign, ctmp) == 1); | CHECK(secp256k1_context_randomize(sign, ctmp) == 1); | ||||
CHECK(ecount2 == 14); | CHECK(ecount2 == 14); | ||||
CHECK(secp256k1_context_randomize(sign, NULL) == 1); | CHECK(secp256k1_context_randomize(sign, NULL) == 1); | ||||
CHECK(ecount2 == 14); | CHECK(ecount2 == 14); | ||||
secp256k1_context_set_illegal_callback(vrfy, NULL, NULL); | secp256k1_context_set_illegal_callback(vrfy, NULL, NULL); | ||||
secp256k1_context_set_illegal_callback(sign, NULL, NULL); | secp256k1_context_set_illegal_callback(sign, NULL, NULL); | ||||
/* This shouldn't leak memory, due to already-set tests. */ | |||||
secp256k1_ecmult_gen_context_build(&sign->ecmult_gen_ctx, NULL); | |||||
secp256k1_ecmult_context_build(&vrfy->ecmult_ctx, NULL); | |||||
/* obtain a working nonce */ | /* obtain a working nonce */ | ||||
do { | do { | ||||
random_scalar_order_test(&nonce); | random_scalar_order_test(&nonce); | ||||
} while(!secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL)); | } while(!secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL)); | ||||
/* try signing */ | /* try signing */ | ||||
CHECK(secp256k1_ecdsa_sig_sign(&sign->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL)); | CHECK(secp256k1_ecdsa_sig_sign(&sign->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL)); | ||||
CHECK(secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL)); | CHECK(secp256k1_ecdsa_sig_sign(&both->ecmult_gen_ctx, &sigr, &sigs, &key, &msg, &nonce, NULL)); | ||||
/* try verifying */ | /* try verifying */ | ||||
CHECK(secp256k1_ecdsa_sig_verify(&vrfy->ecmult_ctx, &sigr, &sigs, &pub, &msg)); | CHECK(secp256k1_ecdsa_sig_verify(&vrfy->ecmult_ctx, &sigr, &sigs, &pub, &msg)); | ||||
CHECK(secp256k1_ecdsa_sig_verify(&both->ecmult_ctx, &sigr, &sigs, &pub, &msg)); | CHECK(secp256k1_ecdsa_sig_verify(&both->ecmult_ctx, &sigr, &sigs, &pub, &msg)); | ||||
/* cleanup */ | /* cleanup */ | ||||
if (use_prealloc) { | |||||
secp256k1_context_preallocated_destroy(none); | |||||
secp256k1_context_preallocated_destroy(sign); | |||||
secp256k1_context_preallocated_destroy(vrfy); | |||||
secp256k1_context_preallocated_destroy(both); | |||||
free(none_prealloc); | |||||
free(sign_prealloc); | |||||
free(vrfy_prealloc); | |||||
free(both_prealloc); | |||||
} else { | |||||
secp256k1_context_destroy(none); | secp256k1_context_destroy(none); | ||||
secp256k1_context_destroy(sign); | secp256k1_context_destroy(sign); | ||||
secp256k1_context_destroy(vrfy); | secp256k1_context_destroy(vrfy); | ||||
secp256k1_context_destroy(both); | secp256k1_context_destroy(both); | ||||
} | |||||
/* Defined as no-op. */ | /* Defined as no-op. */ | ||||
secp256k1_context_destroy(NULL); | secp256k1_context_destroy(NULL); | ||||
secp256k1_context_preallocated_destroy(NULL); | |||||
} | } | ||||
void run_scratch_tests(void) { | void run_scratch_tests(void) { | ||||
int32_t ecount = 0; | int32_t ecount = 0; | ||||
secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE); | secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE); | ||||
secp256k1_scratch_space *scratch; | secp256k1_scratch_space *scratch; | ||||
/* Test public API */ | /* Test public API */ | ||||
▲ Show 20 Lines • Show All 4,820 Lines • ▼ Show 20 Lines | if (argc > 2) { | ||||
} | } | ||||
} | } | ||||
secp256k1_rand_seed(seed16); | secp256k1_rand_seed(seed16); | ||||
printf("test count = %i\n", count); | 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]); | 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(); | run_context_tests(0); | ||||
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); | secp256k1_rand256(run32); | ||||
CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? run32 : NULL)); | CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? run32 : NULL)); | ||||
} | } | ||||
run_rand_bits(); | run_rand_bits(); | ||||
▲ Show 20 Lines • Show All 86 Lines • Show Last 20 Lines |