2015-06-12 04:23:27 +02:00
|
|
|
#ifndef LIGHTNING_BITCOIN_PUBKEY_H
|
|
|
|
#define LIGHTNING_BITCOIN_PUBKEY_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
2017-06-14 10:29:10 +02:00
|
|
|
#include <ccan/crypto/ripemd160/ripemd160.h>
|
2015-06-02 04:23:59 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2018-07-04 07:30:02 +02:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2015-06-02 04:23:59 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2020-12-05 03:17:54 +01:00
|
|
|
#include <secp256k1_extrakeys.h>
|
2015-09-30 03:24:54 +02:00
|
|
|
|
|
|
|
struct privkey;
|
2018-07-23 04:23:02 +02:00
|
|
|
struct secret;
|
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
|
|
|
#define PUBKEY_CMPR_LEN 33
|
2016-07-01 03:49:28 +02:00
|
|
|
|
2015-06-02 04:23:59 +02:00
|
|
|
struct pubkey {
|
2015-09-30 03:24:54 +02:00
|
|
|
/* Unpacked pubkey (as used by libsecp256k1 internally) */
|
|
|
|
secp256k1_pubkey pubkey;
|
2015-06-02 04:23:59 +02:00
|
|
|
};
|
2018-07-04 07:30:02 +02:00
|
|
|
/* Define pubkey_eq (no padding) */
|
|
|
|
STRUCTEQ_DEF(pubkey, 0, pubkey.data);
|
2015-06-02 04:23:59 +02:00
|
|
|
|
2021-10-08 00:54:42 +02:00
|
|
|
struct point32 {
|
2020-12-05 03:17:54 +01:00
|
|
|
/* Unpacked pubkey (as used by libsecp256k1 internally) */
|
|
|
|
secp256k1_xonly_pubkey pubkey;
|
|
|
|
};
|
|
|
|
/* Define pubkey_eq (no padding) */
|
2021-10-08 00:54:42 +02:00
|
|
|
STRUCTEQ_DEF(point32, 0, pubkey.data);
|
2020-12-05 03:17:54 +01:00
|
|
|
|
2015-09-30 03:24:54 +02:00
|
|
|
/* Convert from hex string of DER (scriptPubKey from validateaddress) */
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_hexstr(const char *derstr, size_t derlen, struct pubkey *key);
|
2015-09-30 03:24:54 +02:00
|
|
|
|
2016-07-01 03:49:28 +02:00
|
|
|
/* Convert from hex string of DER (scriptPubKey from validateaddress) */
|
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
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
/* Point from secret */
|
|
|
|
bool pubkey_from_secret(const struct secret *secret, struct pubkey *key);
|
|
|
|
|
2015-09-30 03:24:54 +02:00
|
|
|
/* Pubkey from privkey */
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_privkey(const struct privkey *privkey,
|
2016-07-01 03:57:57 +02:00
|
|
|
struct pubkey *key);
|
2015-06-02 04:23:59 +02:00
|
|
|
|
2015-09-30 03:24:54 +02:00
|
|
|
/* Pubkey from DER encoding. */
|
2016-12-02 08:42:58 +01:00
|
|
|
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key);
|
2015-06-05 04:54:58 +02:00
|
|
|
|
2016-07-01 03:49:28 +02:00
|
|
|
/* Pubkey to DER encoding: must be valid pubkey. */
|
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
|
|
|
|
2016-12-12 13:12:58 +01:00
|
|
|
/* Compare the keys `a` and `b`. Return <0 if `a`<`b`, 0 if equal and >0 otherwise */
|
|
|
|
int pubkey_cmp(const struct pubkey *a, const struct pubkey *b);
|
2017-06-14 10:29:10 +02:00
|
|
|
|
2020-07-23 04:08:58 +02:00
|
|
|
/* If the two pubkeys[] are id1 and id2, which index would id1 be? */
|
2019-01-15 05:02:27 +01:00
|
|
|
static inline int pubkey_idx(const struct pubkey *id1, const struct pubkey *id2)
|
|
|
|
{
|
|
|
|
return pubkey_cmp(id1, id2) > 0;
|
|
|
|
}
|
|
|
|
|
2017-06-14 10:29:10 +02:00
|
|
|
/**
|
|
|
|
* pubkey_to_hash160 - Get the hash for p2pkh payments for a given pubkey
|
|
|
|
*/
|
|
|
|
void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash);
|
2020-05-16 03:29:05 +02:00
|
|
|
|
|
|
|
/* marshal/unmarshal functions */
|
|
|
|
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey);
|
|
|
|
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);
|
|
|
|
|
2021-10-08 00:54:42 +02:00
|
|
|
/* FIXME: Old spec uses pubkey32 */
|
|
|
|
#define pubkey32 point32
|
|
|
|
#define towire_pubkey32 towire_point32
|
|
|
|
#define fromwire_pubkey32 fromwire_point32
|
|
|
|
|
2020-12-05 03:17:54 +01:00
|
|
|
/* marshal/unmarshal functions */
|
2021-10-08 00:54:42 +02:00
|
|
|
void towire_point32(u8 **pptr, const struct point32 *pubkey);
|
|
|
|
void fromwire_point32(const u8 **cursor, size_t *max, struct point32 *pubkey);
|
2020-12-05 03:17:54 +01:00
|
|
|
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_BITCOIN_PUBKEY_H */
|