From 561b0e1044efb71edf06f3d27e6a783203238cfa Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 27 May 2013 01:18:48 +0200 Subject: [PATCH] Private/public key tweaking --- src/secp256k1.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/secp256k1.c b/src/secp256k1.c index 1f9e1f6dc9..9fd7e7f7cf 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -146,6 +146,54 @@ int secp256k1_ecdsa_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) { return 1; } +int secp256k1_ecdsa_privkey_tweak(unsigned char *seckey, const unsigned char *tweak) { + int ret = 1; + secp256k1_num_t term; + secp256k1_num_init(&term); + secp256k1_num_set_bin(&term, tweak, 32); + if (secp256k1_num_cmp(&term, &secp256k1_ge_consts->order) >= 0) + ret = 0; + secp256k1_num_t sec; + secp256k1_num_init(&sec); + if (ret) { + secp256k1_num_set_bin(&term, seckey, 32); + secp256k1_num_add(&sec, &sec, &term); + secp256k1_num_mod(&sec, &secp256k1_ge_consts->order); + if (secp256k1_num_is_zero(&sec)) + ret = 0; + } + if (ret) + secp256k1_num_get_bin(seckey, 32, &sec); + secp256k1_num_free(&sec); + secp256k1_num_free(&term); + return ret; +} + +int secp256k1_ecdsa_pubkey_tweak(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) { + int ret = 1; + secp256k1_num_t term; + secp256k1_num_init(&term); + secp256k1_num_set_bin(&term, tweak, 32); + if (secp256k1_num_cmp(&term, &secp256k1_ge_consts->order) >= 0) + ret = 0; + secp256k1_ge_t p; + if (ret) { + if (!secp256k1_ecdsa_pubkey_parse(&p, pubkey, pubkeylen)) + ret = 0; + } + if (ret) { + secp256k1_gej_t pt; + secp256k1_ecmult_gen(&pt, &term); + secp256k1_gej_add_ge(&pt, &pt, &p); + secp256k1_ge_set_gej(&p, &pt); + int oldlen = pubkeylen; + secp256k1_ecdsa_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33); + assert(pubkeylen == oldlen); + } + secp256k1_num_free(&term); + return ret; +} + int secp256k1_ecdsa_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) { secp256k1_num_t key; secp256k1_num_init(&key);