2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2016-01-21 21:08:08 +01:00
|
|
|
#include <assert.h>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/pubkey.h>
|
2016-03-30 08:24:16 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2016-01-21 21:08:08 +01:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
2020-05-16 03:29:05 +02:00
|
|
|
#include <wire/wire.h>
|
|
|
|
|
|
|
|
#ifndef SUPERVERBOSE
|
|
|
|
#define SUPERVERBOSE(...)
|
|
|
|
#endif
|
2015-06-02 04:23:59 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
|
2015-06-02 04:23:59 +02:00
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
if (len != PUBKEY_CMPR_LEN)
|
2016-01-21 21:11:47 +01:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &key->pubkey,
|
2016-07-01 03:49:28 +02:00
|
|
|
memcheck(der, len), len))
|
2016-01-21 21:11:47 +01:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
return true;
|
2015-06-02 04:23:59 +02:00
|
|
|
}
|
|
|
|
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
void pubkey_to_der(u8 der[PUBKEY_CMPR_LEN], const struct pubkey *key)
|
2016-07-01 03:49:28 +02:00
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
size_t outlen = PUBKEY_CMPR_LEN;
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen,
|
2016-07-01 03:49:28 +02:00
|
|
|
&key->pubkey,
|
|
|
|
SECP256K1_EC_COMPRESSED))
|
|
|
|
abort();
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
assert(outlen == PUBKEY_CMPR_LEN);
|
2016-07-01 03:49:28 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
bool pubkey_from_secret(const struct secret *secret, struct pubkey *key)
|
2015-06-05 04:54:58 +02:00
|
|
|
{
|
2016-12-02 08:42:58 +01:00
|
|
|
if (!secp256k1_ec_pubkey_create(secp256k1_ctx,
|
2018-07-23 04:23:02 +02:00
|
|
|
&key->pubkey, secret->data))
|
2016-01-21 21:11:47 +01:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
bool pubkey_from_privkey(const struct privkey *privkey,
|
|
|
|
struct pubkey *key)
|
|
|
|
{
|
|
|
|
return pubkey_from_secret(&privkey->secret, key);
|
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_hexstr(const char *derstr, size_t slen, struct pubkey *key)
|
2015-09-30 03:24:54 +02:00
|
|
|
{
|
2016-01-21 21:11:47 +01:00
|
|
|
size_t dlen;
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
unsigned char der[PUBKEY_CMPR_LEN];
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
dlen = hex_data_size(slen);
|
2016-03-30 08:24:16 +02:00
|
|
|
if (dlen != sizeof(der))
|
2015-06-05 04:54:58 +02:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
if (!hex_decode(derstr, slen, der, dlen))
|
2015-06-05 04:54:58 +02:00
|
|
|
return false;
|
2015-09-30 03:24:54 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
return pubkey_from_der(der, dlen, key);
|
2015-09-30 03:24:54 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key)
|
2016-07-01 03:49:28 +02:00
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
unsigned char der[PUBKEY_CMPR_LEN];
|
2016-07-01 03:49:28 +02:00
|
|
|
|
2016-12-02 08:42:58 +01:00
|
|
|
pubkey_to_der(der, key);
|
2016-07-01 03:49:28 +02:00
|
|
|
return tal_hexstr(ctx, der, sizeof(der));
|
|
|
|
}
|
2018-07-04 07:30:02 +02:00
|
|
|
REGISTER_TYPE_TO_STRING(pubkey, pubkey_to_hexstr);
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2021-12-04 12:25:06 +01:00
|
|
|
static char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key)
|
2017-01-25 00:32:43 +01:00
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
unsigned char der[PUBKEY_CMPR_LEN];
|
2017-01-25 00:32:43 +01:00
|
|
|
size_t outlen = sizeof(der);
|
|
|
|
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen, key,
|
|
|
|
SECP256K1_EC_COMPRESSED))
|
|
|
|
abort();
|
|
|
|
assert(outlen == sizeof(der));
|
|
|
|
return tal_hexstr(ctx, der, sizeof(der));
|
|
|
|
}
|
|
|
|
REGISTER_TYPE_TO_STRING(secp256k1_pubkey, secp256k1_pubkey_to_hexstr);
|
|
|
|
|
2016-12-12 13:12:58 +01:00
|
|
|
int pubkey_cmp(const struct pubkey *a, const struct pubkey *b)
|
|
|
|
{
|
|
|
|
u8 keya[33], keyb[33];
|
|
|
|
pubkey_to_der(keya, a);
|
|
|
|
pubkey_to_der(keyb, b);
|
|
|
|
return memcmp(keya, keyb, sizeof(keya));
|
|
|
|
}
|
2017-02-02 05:05:45 +01:00
|
|
|
|
2017-06-14 10:29:10 +02:00
|
|
|
void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash)
|
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
2017-06-14 10:29:10 +02:00
|
|
|
struct sha256 h;
|
|
|
|
|
|
|
|
pubkey_to_der(der, pk);
|
|
|
|
sha256(&h, der, sizeof(der));
|
|
|
|
ripemd160(hash, h.u.u8, sizeof(h));
|
|
|
|
}
|
2020-05-16 03:29:05 +02:00
|
|
|
|
|
|
|
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
|
|
|
|
{
|
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
|
|
|
|
|
|
|
if (!fromwire(cursor, max, der, sizeof(der)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!pubkey_from_der(der, sizeof(der), pubkey)) {
|
|
|
|
SUPERVERBOSE("not a valid point");
|
|
|
|
fromwire_fail(cursor, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
|
|
|
|
{
|
|
|
|
u8 output[PUBKEY_CMPR_LEN];
|
|
|
|
size_t outputlen = sizeof(output);
|
|
|
|
|
|
|
|
secp256k1_ec_pubkey_serialize(secp256k1_ctx, output, &outputlen,
|
|
|
|
&pubkey->pubkey,
|
|
|
|
SECP256K1_EC_COMPRESSED);
|
|
|
|
|
|
|
|
towire(pptr, output, outputlen);
|
|
|
|
}
|