Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/modules/schnorrsig/tests_impl.h
Show All 11 Lines | |||||
/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many | /* Checks that a bit flip in the n_flip-th argument (that has n_bytes many | ||||
* bytes) changes the hash function | * bytes) changes the hash function | ||||
*/ | */ | ||||
void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes) { | void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes) { | ||||
unsigned char nonces[2][32]; | unsigned char nonces[2][32]; | ||||
CHECK(nonce_function_bip340(nonces[0], args[0], args[1], args[2], args[3], args[4]) == 1); | CHECK(nonce_function_bip340(nonces[0], args[0], args[1], args[2], args[3], args[4]) == 1); | ||||
secp256k1_testrand_flip(args[n_flip], n_bytes); | secp256k1_testrand_flip(args[n_flip], n_bytes); | ||||
CHECK(nonce_function_bip340(nonces[1], args[0], args[1], args[2], args[3], args[4]) == 1); | CHECK(nonce_function_bip340(nonces[1], args[0], args[1], args[2], args[3], args[4]) == 1); | ||||
CHECK(memcmp(nonces[0], nonces[1], 32) != 0); | CHECK(secp256k1_memcmp_var(nonces[0], nonces[1], 32) != 0); | ||||
} | } | ||||
/* Tests for the equality of two sha256 structs. This function only produces a | /* Tests for the equality of two sha256 structs. This function only produces a | ||||
* correct result if an integer multiple of 64 many bytes have been written | * correct result if an integer multiple of 64 many bytes have been written | ||||
* into the hash functions. */ | * into the hash functions. */ | ||||
void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256 *sha2) { | void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256 *sha2) { | ||||
/* Is buffer fully consumed? */ | /* Is buffer fully consumed? */ | ||||
CHECK((sha1->bytes & 0x3F) == 0); | CHECK((sha1->bytes & 0x3F) == 0); | ||||
CHECK(sha1->bytes == sha2->bytes); | CHECK(sha1->bytes == sha2->bytes); | ||||
CHECK(memcmp(sha1->s, sha2->s, sizeof(sha1->s)) == 0); | CHECK(secp256k1_memcmp_var(sha1->s, sha2->s, sizeof(sha1->s)) == 0); | ||||
} | } | ||||
void run_nonce_function_bip340_tests(void) { | void run_nonce_function_bip340_tests(void) { | ||||
unsigned char tag[13] = "BIP0340/nonce"; | unsigned char tag[13] = "BIP0340/nonce"; | ||||
unsigned char aux_tag[11] = "BIP0340/aux"; | unsigned char aux_tag[11] = "BIP0340/aux"; | ||||
unsigned char algo16[16] = "BIP0340/nonce\0\0\0"; | unsigned char algo16[16] = "BIP0340/nonce\0\0\0"; | ||||
secp256k1_sha256 sha; | secp256k1_sha256 sha; | ||||
secp256k1_sha256 sha_optimized; | secp256k1_sha256 sha_optimized; | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | |||||
* Signs the message and checks that it's the same as expected_sig. */ | * Signs the message and checks that it's the same as expected_sig. */ | ||||
void test_schnorrsig_bip_vectors_check_signing(const unsigned char *sk, const unsigned char *pk_serialized, unsigned char *aux_rand, const unsigned char *msg, const unsigned char *expected_sig) { | void test_schnorrsig_bip_vectors_check_signing(const unsigned char *sk, const unsigned char *pk_serialized, unsigned char *aux_rand, const unsigned char *msg, const unsigned char *expected_sig) { | ||||
unsigned char sig[64]; | unsigned char sig[64]; | ||||
secp256k1_keypair keypair; | secp256k1_keypair keypair; | ||||
secp256k1_xonly_pubkey pk, pk_expected; | secp256k1_xonly_pubkey pk, pk_expected; | ||||
CHECK(secp256k1_keypair_create(ctx, &keypair, sk)); | CHECK(secp256k1_keypair_create(ctx, &keypair, sk)); | ||||
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, aux_rand)); | CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, aux_rand)); | ||||
CHECK(memcmp(sig, expected_sig, 64) == 0); | CHECK(secp256k1_memcmp_var(sig, expected_sig, 64) == 0); | ||||
CHECK(secp256k1_xonly_pubkey_parse(ctx, &pk_expected, pk_serialized)); | CHECK(secp256k1_xonly_pubkey_parse(ctx, &pk_expected, pk_serialized)); | ||||
CHECK(secp256k1_keypair_xonly_pub(ctx, &pk, NULL, &keypair)); | CHECK(secp256k1_keypair_xonly_pub(ctx, &pk, NULL, &keypair)); | ||||
CHECK(memcmp(&pk, &pk_expected, sizeof(pk)) == 0); | CHECK(secp256k1_memcmp_var(&pk, &pk_expected, sizeof(pk)) == 0); | ||||
CHECK(secp256k1_schnorrsig_verify(ctx, sig, msg, &pk)); | CHECK(secp256k1_schnorrsig_verify(ctx, sig, msg, &pk)); | ||||
} | } | ||||
/* Helper function for schnorrsig_bip_vectors | /* Helper function for schnorrsig_bip_vectors | ||||
* Checks that both verify and verify_batch (TODO) return the same value as expected. */ | * Checks that both verify and verify_batch (TODO) return the same value as expected. */ | ||||
void test_schnorrsig_bip_vectors_check_verify(const unsigned char *pk_serialized, const unsigned char *msg32, const unsigned char *sig, int expected) { | void test_schnorrsig_bip_vectors_check_verify(const unsigned char *pk_serialized, const unsigned char *msg32, const unsigned char *sig, int expected) { | ||||
secp256k1_xonly_pubkey pk; | secp256k1_xonly_pubkey pk; | ||||
▲ Show 20 Lines • Show All 464 Lines • ▼ Show 20 Lines | void test_schnorrsig_sign(void) { | ||||
secp256k1_testrand256(sk); | secp256k1_testrand256(sk); | ||||
CHECK(secp256k1_keypair_create(ctx, &keypair, sk)); | CHECK(secp256k1_keypair_create(ctx, &keypair, sk)); | ||||
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, NULL) == 1); | CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, NULL) == 1); | ||||
/* Test different nonce functions */ | /* Test different nonce functions */ | ||||
memset(sig, 1, sizeof(sig)); | memset(sig, 1, sizeof(sig)); | ||||
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, nonce_function_failing, NULL) == 0); | CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, nonce_function_failing, NULL) == 0); | ||||
CHECK(memcmp(sig, zeros64, sizeof(sig)) == 0); | CHECK(secp256k1_memcmp_var(sig, zeros64, sizeof(sig)) == 0); | ||||
memset(&sig, 1, sizeof(sig)); | memset(&sig, 1, sizeof(sig)); | ||||
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, nonce_function_0, NULL) == 0); | CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, nonce_function_0, NULL) == 0); | ||||
CHECK(memcmp(sig, zeros64, sizeof(sig)) == 0); | CHECK(secp256k1_memcmp_var(sig, zeros64, sizeof(sig)) == 0); | ||||
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, nonce_function_overflowing, NULL) == 1); | CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, nonce_function_overflowing, NULL) == 1); | ||||
CHECK(memcmp(sig, zeros64, sizeof(sig)) != 0); | CHECK(secp256k1_memcmp_var(sig, zeros64, sizeof(sig)) != 0); | ||||
} | } | ||||
#define N_SIGS 3 | #define N_SIGS 3 | ||||
/* Creates N_SIGS valid signatures and verifies them with verify and | /* Creates N_SIGS valid signatures and verifies them with verify and | ||||
* verify_batch (TODO). Then flips some bits and checks that verification now | * verify_batch (TODO). Then flips some bits and checks that verification now | ||||
* fails. */ | * fails. */ | ||||
void test_schnorrsig_sign_verify(void) { | void test_schnorrsig_sign_verify(void) { | ||||
unsigned char sk[32]; | unsigned char sk[32]; | ||||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |