mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
837a095d68
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>
98 lines
2.3 KiB
C
98 lines
2.3 KiB
C
#include "privkey.h"
|
|
#include "pubkey.h"
|
|
#include <assert.h>
|
|
#include <ccan/mem/mem.h>
|
|
#include <ccan/str/hex/hex.h>
|
|
#include <common/type_to_string.h>
|
|
#include <common/utils.h>
|
|
|
|
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
|
|
{
|
|
if (len != PUBKEY_CMPR_LEN)
|
|
return false;
|
|
|
|
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &key->pubkey,
|
|
memcheck(der, len), len))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
void pubkey_to_der(u8 der[PUBKEY_CMPR_LEN], const struct pubkey *key)
|
|
{
|
|
size_t outlen = PUBKEY_CMPR_LEN;
|
|
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen,
|
|
&key->pubkey,
|
|
SECP256K1_EC_COMPRESSED))
|
|
abort();
|
|
assert(outlen == PUBKEY_CMPR_LEN);
|
|
}
|
|
|
|
bool pubkey_from_secret(const struct secret *secret, struct pubkey *key)
|
|
{
|
|
if (!secp256k1_ec_pubkey_create(secp256k1_ctx,
|
|
&key->pubkey, secret->data))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
bool pubkey_from_privkey(const struct privkey *privkey,
|
|
struct pubkey *key)
|
|
{
|
|
return pubkey_from_secret(&privkey->secret, key);
|
|
}
|
|
|
|
bool pubkey_from_hexstr(const char *derstr, size_t slen, struct pubkey *key)
|
|
{
|
|
size_t dlen;
|
|
unsigned char der[PUBKEY_CMPR_LEN];
|
|
|
|
dlen = hex_data_size(slen);
|
|
if (dlen != sizeof(der))
|
|
return false;
|
|
|
|
if (!hex_decode(derstr, slen, der, dlen))
|
|
return false;
|
|
|
|
return pubkey_from_der(der, dlen, key);
|
|
}
|
|
|
|
char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key)
|
|
{
|
|
unsigned char der[PUBKEY_CMPR_LEN];
|
|
|
|
pubkey_to_der(der, key);
|
|
return tal_hexstr(ctx, der, sizeof(der));
|
|
}
|
|
REGISTER_TYPE_TO_STRING(pubkey, pubkey_to_hexstr);
|
|
|
|
char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key)
|
|
{
|
|
unsigned char der[PUBKEY_CMPR_LEN];
|
|
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);
|
|
|
|
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));
|
|
}
|
|
|
|
void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash)
|
|
{
|
|
u8 der[PUBKEY_CMPR_LEN];
|
|
struct sha256 h;
|
|
|
|
pubkey_to_der(der, pk);
|
|
sha256(&h, der, sizeof(der));
|
|
ripemd160(hash, h.u.u8, sizeof(h));
|
|
}
|