From ae6bc76e32016063a591399a7a2d7bac646603bc Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 27 Oct 2014 02:51:58 -0700 Subject: [PATCH] [API CHANGE] Use secp256k1_ec_ prefix for non-ECDSA key operations --- include/secp256k1.h | 47 ++++++++++++++++++++++++++++++++------------- src/secp256k1.c | 20 +++++++++---------- src/tests.c | 22 ++++++++++----------- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/include/secp256k1.h b/include/secp256k1.h index a8ddf43..cb5fd3c 100644 --- a/include/secp256k1.h +++ b/include/secp256k1.h @@ -87,13 +87,13 @@ int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, * 0: secret key is invalid * In: seckey: pointer to a 32-byte secret key */ -int secp256k1_ecdsa_seckey_verify(const unsigned char *seckey); +int secp256k1_ec_seckey_verify(const unsigned char *seckey); /** Just validate a public key. * Returns: 1: valid public key * 0: invalid public key */ -int secp256k1_ecdsa_pubkey_verify(const unsigned char *pubkey, int pubkeylen); +int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen); /** Compute the public key for a secret key. * In: compressed: whether the computed public key should be compressed @@ -106,21 +106,42 @@ int secp256k1_ecdsa_pubkey_verify(const unsigned char *pubkey, int pubkeylen); * 0: secret was invalid, try again. * Requires starting using SECP256K1_START_SIGN. */ -int secp256k1_ecdsa_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed); +int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed); -int secp256k1_ecdsa_pubkey_decompress(unsigned char *pubkey, int *pubkeylen); +/** Decompress a public key. + * In/Out: pubkey: pointer to a 65-byte array to put the decompressed public key. + It must contain a 33-byte or 65-byte public key already. + * pubkeylen: pointer to the size of the public key pointed to be pubkey. + It will be updated to reflect the new size. + * Returns: 0 if the passed public key was invalid, 1 otherwise. If 1 is returned, the + pubkey is replaced with its decompressed version. + */ +int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen); -int secp256k1_ecdsa_privkey_export(const unsigned char *seckey, - unsigned char *privkey, int *privkeylen, - int compressed); +/** Export a private key in DER format. */ +int secp256k1_ec_privkey_export(const unsigned char *seckey, + unsigned char *privkey, int *privkeylen, + int compressed); -int secp256k1_ecdsa_privkey_import(unsigned char *seckey, - const unsigned char *privkey, int privkeylen); +/** Import a private key in DER format. */ +int secp256k1_ec_privkey_import(unsigned char *seckey, + const unsigned char *privkey, int privkeylen); -int secp256k1_ecdsa_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak); -int secp256k1_ecdsa_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak); -int secp256k1_ecdsa_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak); -int secp256k1_ecdsa_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak); +/** Tweak a private key by adding tweak to it. */ +int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak); + +/** Tweak a public key by adding tweak times the generator to it. + * Requires starting with SECP256K1_START_VERIFY. + */ +int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak); + +/** Tweak a private key by multiplying it with tweak. */ +int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak); + +/** Tweak a public key by multiplying it with tweak. + * Requires starting with SECP256K1_START_VERIFY. + */ +int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak); #ifdef __cplusplus } diff --git a/src/secp256k1.c b/src/secp256k1.c index b1ef48d..772ddff 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -165,7 +165,7 @@ int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, const return ret; } -int secp256k1_ecdsa_seckey_verify(const unsigned char *seckey) { +int secp256k1_ec_seckey_verify(const unsigned char *seckey) { DEBUG_CHECK(seckey != NULL); secp256k1_num_t sec; @@ -178,14 +178,14 @@ int secp256k1_ecdsa_seckey_verify(const unsigned char *seckey) { return ret; } -int secp256k1_ecdsa_pubkey_verify(const unsigned char *pubkey, int pubkeylen) { +int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen) { DEBUG_CHECK(pubkey != NULL); secp256k1_ge_t q; return secp256k1_ecdsa_pubkey_parse(&q, pubkey, pubkeylen); } -int secp256k1_ecdsa_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed) { +int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed) { DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL); DEBUG_CHECK(pubkey != NULL); DEBUG_CHECK(pubkeylen != NULL); @@ -204,7 +204,7 @@ int secp256k1_ecdsa_pubkey_create(unsigned char *pubkey, int *pubkeylen, const u return 1; } -int secp256k1_ecdsa_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) { +int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) { DEBUG_CHECK(pubkey != NULL); DEBUG_CHECK(pubkeylen != NULL); @@ -215,7 +215,7 @@ int secp256k1_ecdsa_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) { return 1; } -int secp256k1_ecdsa_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) { +int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) { DEBUG_CHECK(seckey != NULL); DEBUG_CHECK(tweak != NULL); @@ -243,7 +243,7 @@ int secp256k1_ecdsa_privkey_tweak_add(unsigned char *seckey, const unsigned char return ret; } -int secp256k1_ecdsa_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) { +int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) { DEBUG_CHECK(secp256k1_ecmult_consts != NULL); DEBUG_CHECK(pubkey != NULL); DEBUG_CHECK(tweak != NULL); @@ -278,7 +278,7 @@ int secp256k1_ecdsa_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const return ret; } -int secp256k1_ecdsa_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak) { +int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak) { DEBUG_CHECK(seckey != NULL); DEBUG_CHECK(tweak != NULL); @@ -303,7 +303,7 @@ int secp256k1_ecdsa_privkey_tweak_mul(unsigned char *seckey, const unsigned char return ret; } -int secp256k1_ecdsa_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) { +int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) { DEBUG_CHECK(secp256k1_ecmult_consts != NULL); DEBUG_CHECK(pubkey != NULL); DEBUG_CHECK(tweak != NULL); @@ -338,7 +338,7 @@ int secp256k1_ecdsa_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const return ret; } -int secp256k1_ecdsa_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) { +int secp256k1_ec_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) { DEBUG_CHECK(seckey != NULL); DEBUG_CHECK(privkey != NULL); DEBUG_CHECK(privkeylen != NULL); @@ -351,7 +351,7 @@ int secp256k1_ecdsa_privkey_export(const unsigned char *seckey, unsigned char *p return ret; } -int secp256k1_ecdsa_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen) { +int secp256k1_ec_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen) { DEBUG_CHECK(seckey != NULL); DEBUG_CHECK(privkey != NULL); diff --git a/src/tests.c b/src/tests.c index 7f1d642..28bd4ee 100644 --- a/src/tests.c +++ b/src/tests.c @@ -560,28 +560,28 @@ void test_ecdsa_end_to_end() { } // Construct and verify corresponding public key. - CHECK(secp256k1_ecdsa_seckey_verify(privkey) == 1); + CHECK(secp256k1_ec_seckey_verify(privkey) == 1); char pubkey[65]; int pubkeylen = 65; - CHECK(secp256k1_ecdsa_pubkey_create(pubkey, &pubkeylen, privkey, secp256k1_rand32() % 2) == 1); - CHECK(secp256k1_ecdsa_pubkey_verify(pubkey, pubkeylen)); + CHECK(secp256k1_ec_pubkey_create(pubkey, &pubkeylen, privkey, secp256k1_rand32() % 2) == 1); + CHECK(secp256k1_ec_pubkey_verify(pubkey, pubkeylen)); // Verify private key import and export. unsigned char seckey[300]; int seckeylen = 300; - CHECK(secp256k1_ecdsa_privkey_export(privkey, seckey, &seckeylen, secp256k1_rand32() % 2) == 1); + CHECK(secp256k1_ec_privkey_export(privkey, seckey, &seckeylen, secp256k1_rand32() % 2) == 1); unsigned char privkey2[32]; - CHECK(secp256k1_ecdsa_privkey_import(privkey2, seckey, seckeylen) == 1); + CHECK(secp256k1_ec_privkey_import(privkey2, seckey, seckeylen) == 1); CHECK(memcmp(privkey, privkey2, 32) == 0); // Optionally tweak the keys using addition. if (secp256k1_rand32() % 3 == 0) { unsigned char rnd[32]; secp256k1_rand256_test(rnd); - int ret1 = secp256k1_ecdsa_privkey_tweak_add(privkey, rnd); - int ret2 = secp256k1_ecdsa_pubkey_tweak_add(pubkey, pubkeylen, rnd); + int ret1 = secp256k1_ec_privkey_tweak_add(privkey, rnd); + int ret2 = secp256k1_ec_pubkey_tweak_add(pubkey, pubkeylen, rnd); CHECK(ret1 == ret2); if (ret1 == 0) return; char pubkey2[65]; int pubkeylen2 = 65; - CHECK(secp256k1_ecdsa_pubkey_create(pubkey2, &pubkeylen2, privkey, pubkeylen == 33) == 1); + CHECK(secp256k1_ec_pubkey_create(pubkey2, &pubkeylen2, privkey, pubkeylen == 33) == 1); CHECK(memcmp(pubkey, pubkey2, pubkeylen) == 0); } @@ -589,12 +589,12 @@ void test_ecdsa_end_to_end() { if (secp256k1_rand32() % 3 == 0) { unsigned char rnd[32]; secp256k1_rand256_test(rnd); - int ret1 = secp256k1_ecdsa_privkey_tweak_mul(privkey, rnd); - int ret2 = secp256k1_ecdsa_pubkey_tweak_mul(pubkey, pubkeylen, rnd); + int ret1 = secp256k1_ec_privkey_tweak_mul(privkey, rnd); + int ret2 = secp256k1_ec_pubkey_tweak_mul(pubkey, pubkeylen, rnd); CHECK(ret1 == ret2); if (ret1 == 0) return; char pubkey2[65]; int pubkeylen2 = 65; - CHECK(secp256k1_ecdsa_pubkey_create(pubkey2, &pubkeylen2, privkey, pubkeylen == 33) == 1); + CHECK(secp256k1_ec_pubkey_create(pubkey2, &pubkeylen2, privkey, pubkeylen == 33) == 1); CHECK(memcmp(pubkey, pubkey2, pubkeylen) == 0); }