Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/modules/extrakeys/tests_impl.h
Show First 20 Lines • Show All 305 Lines • ▼ Show 20 Lines | for (i = N_PUBKEYS - 1; i > 0; i--) { | ||||
CHECK(secp256k1_xonly_pubkey_from_pubkey(ctx, &xonly_pk, NULL, &pk[i - 1]) == 1); | CHECK(secp256k1_xonly_pubkey_from_pubkey(ctx, &xonly_pk, NULL, &pk[i - 1]) == 1); | ||||
CHECK(secp256k1_xonly_pubkey_tweak_add_check(ctx, pk_serialized, pk_parity, &xonly_pk, tweak[i - 1]) == 1); | CHECK(secp256k1_xonly_pubkey_tweak_add_check(ctx, pk_serialized, pk_parity, &xonly_pk, tweak[i - 1]) == 1); | ||||
} | } | ||||
} | } | ||||
#undef N_PUBKEYS | #undef N_PUBKEYS | ||||
void test_keypair(void) { | void test_keypair(void) { | ||||
unsigned char sk[32]; | unsigned char sk[32]; | ||||
unsigned char sk_tmp[32]; | |||||
unsigned char zeros96[96] = { 0 }; | unsigned char zeros96[96] = { 0 }; | ||||
unsigned char overflows[32]; | unsigned char overflows[32]; | ||||
secp256k1_keypair keypair; | secp256k1_keypair keypair; | ||||
secp256k1_pubkey pk, pk_tmp; | secp256k1_pubkey pk, pk_tmp; | ||||
secp256k1_xonly_pubkey xonly_pk, xonly_pk_tmp; | secp256k1_xonly_pubkey xonly_pk, xonly_pk_tmp; | ||||
int pk_parity, pk_parity_tmp; | int pk_parity, pk_parity_tmp; | ||||
int ecount; | int ecount; | ||||
secp256k1_context *none = api_test_context(SECP256K1_CONTEXT_NONE, &ecount); | secp256k1_context *none = api_test_context(SECP256K1_CONTEXT_NONE, &ecount); | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | void test_keypair(void) { | ||||
/** keypair holds the same xonly pubkey as pubkey_create **/ | /** keypair holds the same xonly pubkey as pubkey_create **/ | ||||
CHECK(secp256k1_ec_pubkey_create(sign, &pk, sk) == 1); | CHECK(secp256k1_ec_pubkey_create(sign, &pk, sk) == 1); | ||||
CHECK(secp256k1_xonly_pubkey_from_pubkey(none, &xonly_pk, &pk_parity, &pk) == 1); | CHECK(secp256k1_xonly_pubkey_from_pubkey(none, &xonly_pk, &pk_parity, &pk) == 1); | ||||
CHECK(secp256k1_keypair_create(sign, &keypair, sk) == 1); | CHECK(secp256k1_keypair_create(sign, &keypair, sk) == 1); | ||||
CHECK(secp256k1_keypair_xonly_pub(none, &xonly_pk_tmp, &pk_parity_tmp, &keypair) == 1); | CHECK(secp256k1_keypair_xonly_pub(none, &xonly_pk_tmp, &pk_parity_tmp, &keypair) == 1); | ||||
CHECK(secp256k1_memcmp_var(&xonly_pk, &xonly_pk_tmp, sizeof(pk)) == 0); | CHECK(secp256k1_memcmp_var(&xonly_pk, &xonly_pk_tmp, sizeof(pk)) == 0); | ||||
CHECK(pk_parity == pk_parity_tmp); | CHECK(pk_parity == pk_parity_tmp); | ||||
/* Test keypair_seckey */ | |||||
ecount = 0; | |||||
secp256k1_testrand256(sk); | |||||
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1); | |||||
CHECK(secp256k1_keypair_sec(none, sk_tmp, &keypair) == 1); | |||||
CHECK(secp256k1_keypair_sec(none, NULL, &keypair) == 0); | |||||
CHECK(ecount == 1); | |||||
CHECK(secp256k1_keypair_sec(none, sk_tmp, NULL) == 0); | |||||
CHECK(ecount == 2); | |||||
CHECK(secp256k1_memcmp_var(zeros96, sk_tmp, sizeof(sk_tmp)) == 0); | |||||
/* keypair returns the same seckey it got */ | |||||
CHECK(secp256k1_keypair_create(sign, &keypair, sk) == 1); | |||||
CHECK(secp256k1_keypair_sec(none, sk_tmp, &keypair) == 1); | |||||
CHECK(secp256k1_memcmp_var(sk, sk_tmp, sizeof(sk_tmp)) == 0); | |||||
/* Using an invalid keypair is fine for keypair_seckey */ | |||||
memset(&keypair, 0, sizeof(keypair)); | |||||
CHECK(secp256k1_keypair_sec(none, sk_tmp, &keypair) == 1); | |||||
CHECK(secp256k1_memcmp_var(zeros96, sk_tmp, sizeof(sk_tmp)) == 0); | |||||
secp256k1_context_destroy(none); | secp256k1_context_destroy(none); | ||||
secp256k1_context_destroy(sign); | secp256k1_context_destroy(sign); | ||||
secp256k1_context_destroy(verify); | secp256k1_context_destroy(verify); | ||||
} | } | ||||
void test_keypair_add(void) { | void test_keypair_add(void) { | ||||
unsigned char sk[32]; | unsigned char sk[32]; | ||||
secp256k1_keypair keypair; | secp256k1_keypair keypair; | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | void test_keypair_add(void) { | ||||
/* Check that the keypair_tweak_add implementation is correct */ | /* Check that the keypair_tweak_add implementation is correct */ | ||||
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1); | CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1); | ||||
for (i = 0; i < count; i++) { | for (i = 0; i < count; i++) { | ||||
secp256k1_xonly_pubkey internal_pk; | secp256k1_xonly_pubkey internal_pk; | ||||
secp256k1_xonly_pubkey output_pk; | secp256k1_xonly_pubkey output_pk; | ||||
secp256k1_pubkey output_pk_xy; | secp256k1_pubkey output_pk_xy; | ||||
secp256k1_pubkey output_pk_expected; | secp256k1_pubkey output_pk_expected; | ||||
unsigned char pk32[32]; | unsigned char pk32[32]; | ||||
unsigned char sk32[32]; | |||||
int pk_parity; | int pk_parity; | ||||
secp256k1_testrand256(tweak); | secp256k1_testrand256(tweak); | ||||
CHECK(secp256k1_keypair_xonly_pub(ctx, &internal_pk, NULL, &keypair) == 1); | CHECK(secp256k1_keypair_xonly_pub(ctx, &internal_pk, NULL, &keypair) == 1); | ||||
CHECK(secp256k1_keypair_xonly_tweak_add(ctx, &keypair, tweak) == 1); | CHECK(secp256k1_keypair_xonly_tweak_add(ctx, &keypair, tweak) == 1); | ||||
CHECK(secp256k1_keypair_xonly_pub(ctx, &output_pk, &pk_parity, &keypair) == 1); | CHECK(secp256k1_keypair_xonly_pub(ctx, &output_pk, &pk_parity, &keypair) == 1); | ||||
/* Check that it passes xonly_pubkey_tweak_add_check */ | /* Check that it passes xonly_pubkey_tweak_add_check */ | ||||
CHECK(secp256k1_xonly_pubkey_serialize(ctx, pk32, &output_pk) == 1); | CHECK(secp256k1_xonly_pubkey_serialize(ctx, pk32, &output_pk) == 1); | ||||
CHECK(secp256k1_xonly_pubkey_tweak_add_check(ctx, pk32, pk_parity, &internal_pk, tweak) == 1); | CHECK(secp256k1_xonly_pubkey_tweak_add_check(ctx, pk32, pk_parity, &internal_pk, tweak) == 1); | ||||
/* Check that the resulting pubkey matches xonly_pubkey_tweak_add */ | /* Check that the resulting pubkey matches xonly_pubkey_tweak_add */ | ||||
CHECK(secp256k1_keypair_pub(ctx, &output_pk_xy, &keypair) == 1); | CHECK(secp256k1_keypair_pub(ctx, &output_pk_xy, &keypair) == 1); | ||||
CHECK(secp256k1_xonly_pubkey_tweak_add(ctx, &output_pk_expected, &internal_pk, tweak) == 1); | CHECK(secp256k1_xonly_pubkey_tweak_add(ctx, &output_pk_expected, &internal_pk, tweak) == 1); | ||||
CHECK(secp256k1_memcmp_var(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0); | CHECK(secp256k1_memcmp_var(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0); | ||||
/* Check that the secret key in the keypair is tweaked correctly */ | /* Check that the secret key in the keypair is tweaked correctly */ | ||||
CHECK(secp256k1_ec_pubkey_create(ctx, &output_pk_expected, &keypair.data[0]) == 1); | CHECK(secp256k1_keypair_sec(none, sk32, &keypair) == 1); | ||||
CHECK(secp256k1_ec_pubkey_create(ctx, &output_pk_expected, sk32) == 1); | |||||
CHECK(secp256k1_memcmp_var(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0); | CHECK(secp256k1_memcmp_var(&output_pk_xy, &output_pk_expected, sizeof(output_pk_xy)) == 0); | ||||
} | } | ||||
secp256k1_context_destroy(none); | secp256k1_context_destroy(none); | ||||
secp256k1_context_destroy(sign); | secp256k1_context_destroy(sign); | ||||
secp256k1_context_destroy(verify); | secp256k1_context_destroy(verify); | ||||
} | } | ||||
void run_extrakeys_tests(void) { | void run_extrakeys_tests(void) { | ||||
Show All 12 Lines |