Skip to content

Commit

Permalink
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and niftynei committed Apr 9, 2019
1 parent 3c13369 commit 837a095
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 72 deletions.
16 changes: 8 additions & 8 deletions bitcoin/pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
{
if (len != PUBKEY_DER_LEN)
if (len != PUBKEY_CMPR_LEN)
return false;

if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &key->pubkey,
Expand All @@ -18,14 +18,14 @@ bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
return true;
}

void pubkey_to_der(u8 der[PUBKEY_DER_LEN], const struct pubkey *key)
void pubkey_to_der(u8 der[PUBKEY_CMPR_LEN], const struct pubkey *key)
{
size_t outlen = PUBKEY_DER_LEN;
size_t outlen = PUBKEY_CMPR_LEN;
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen,
&key->pubkey,
SECP256K1_EC_COMPRESSED))
abort();
assert(outlen == PUBKEY_DER_LEN);
assert(outlen == PUBKEY_CMPR_LEN);
}

bool pubkey_from_secret(const struct secret *secret, struct pubkey *key)
Expand All @@ -45,7 +45,7 @@ bool pubkey_from_privkey(const struct privkey *privkey,
bool pubkey_from_hexstr(const char *derstr, size_t slen, struct pubkey *key)
{
size_t dlen;
unsigned char der[PUBKEY_DER_LEN];
unsigned char der[PUBKEY_CMPR_LEN];

dlen = hex_data_size(slen);
if (dlen != sizeof(der))
Expand All @@ -59,7 +59,7 @@ bool pubkey_from_hexstr(const char *derstr, size_t slen, struct pubkey *key)

char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key)
{
unsigned char der[PUBKEY_DER_LEN];
unsigned char der[PUBKEY_CMPR_LEN];

pubkey_to_der(der, key);
return tal_hexstr(ctx, der, sizeof(der));
Expand All @@ -68,7 +68,7 @@ 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_DER_LEN];
unsigned char der[PUBKEY_CMPR_LEN];
size_t outlen = sizeof(der);
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen, key,
SECP256K1_EC_COMPRESSED))
Expand All @@ -88,7 +88,7 @@ int pubkey_cmp(const struct pubkey *a, const struct pubkey *b)

void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
struct sha256 h;

pubkey_to_der(der, pk);
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
struct privkey;
struct secret;

#define PUBKEY_DER_LEN 33
#define PUBKEY_CMPR_LEN 33

struct pubkey {
/* Unpacked pubkey (as used by libsecp256k1 internally) */
Expand Down Expand Up @@ -40,7 +40,7 @@ bool pubkey_from_privkey(const struct privkey *privkey,
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key);

/* Pubkey to DER encoding: must be valid pubkey. */
void pubkey_to_der(u8 der[PUBKEY_DER_LEN], const struct pubkey *key);
void pubkey_to_der(u8 der[PUBKEY_CMPR_LEN], const struct pubkey *key);

/* 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);
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static void add_number(u8 **script, u32 num)

static void add_push_key(u8 **scriptp, const struct pubkey *key)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
pubkey_to_der(der, key);

add_push_bytes(scriptp, der, sizeof(der));
Expand All @@ -120,7 +120,7 @@ static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig)

static u8 *stack_key(const tal_t *ctx, const struct pubkey *key)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
pubkey_to_der(der, key);

return tal_dup_arr(ctx, u8, der, sizeof(der), 0);
Expand Down Expand Up @@ -305,7 +305,7 @@ u8 *scriptpubkey_p2wpkh_derkey(const tal_t *ctx, const u8 der[33])
struct ripemd160 h;

add_op(&script, OP_0);
hash160(&h, der, PUBKEY_DER_LEN);
hash160(&h, der, PUBKEY_CMPR_LEN);
add_push_bytes(&script, &h, sizeof(h));
return script;
}
Expand Down
4 changes: 2 additions & 2 deletions common/bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ static char *decode_n(struct bolt11 *b11,
u5 **data, size_t *data_len,
size_t data_length, bool *have_n)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];

if (*have_n)
return unknown_field(b11, hu5, data, data_len, 'n',
Expand Down Expand Up @@ -787,7 +787,7 @@ static void encode_h(u5 **data, const struct sha256 *hash)

static void encode_n(u5 **data, const struct pubkey *id)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];

pubkey_to_der(der, id);
push_field(data, 'n', der, sizeof(der) * CHAR_BIT);
Expand Down
4 changes: 2 additions & 2 deletions common/initial_commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
const struct pubkey *accepter_payment_basepoint)
{
u8 ders[PUBKEY_DER_LEN * 2];
u8 ders[PUBKEY_CMPR_LEN * 2];
struct sha256 sha;
be64 obscurer = 0;

pubkey_to_der(ders, opener_payment_basepoint);
pubkey_to_der(ders + PUBKEY_DER_LEN, accepter_payment_basepoint);
pubkey_to_der(ders + PUBKEY_CMPR_LEN, accepter_payment_basepoint);

sha256(&sha, ders, sizeof(ders));
/* Lower 48 bits */
Expand Down
44 changes: 22 additions & 22 deletions common/key_derive.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ bool derive_simple_key(const struct pubkey *basepoint,
struct pubkey *key)
{
struct sha256 sha;
unsigned char der_keys[PUBKEY_DER_LEN * 2];
unsigned char der_keys[PUBKEY_CMPR_LEN * 2];

pubkey_to_der(der_keys, per_commitment_point);
pubkey_to_der(der_keys + PUBKEY_DER_LEN, basepoint);
pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, basepoint);
sha256(&sha, der_keys, sizeof(der_keys));
#ifdef SUPERVERBOSE
printf("# SHA256(per_commitment_point || basepoint)\n");
printf("# => SHA256(0x%s || 0x%s)\n",
tal_hexstr(tmpctx, der_keys, PUBKEY_DER_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_DER_LEN, PUBKEY_DER_LEN));
tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
printf("# = 0x%s\n",
tal_hexstr(tmpctx, &sha, sizeof(sha)));
#endif
Expand Down Expand Up @@ -66,16 +66,16 @@ bool derive_simple_privkey(const struct secret *base_secret,
struct privkey *key)
{
struct sha256 sha;
unsigned char der_keys[PUBKEY_DER_LEN * 2];
unsigned char der_keys[PUBKEY_CMPR_LEN * 2];

pubkey_to_der(der_keys, per_commitment_point);
pubkey_to_der(der_keys + PUBKEY_DER_LEN, basepoint);
pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, basepoint);
sha256(&sha, der_keys, sizeof(der_keys));
#ifdef SUPERVERBOSE
printf("# SHA256(per_commitment_point || basepoint)\n");
printf("# => SHA256(0x%s || 0x%s)\n",
tal_hexstr(tmpctx, der_keys, PUBKEY_DER_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_DER_LEN, PUBKEY_DER_LEN));
tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
printf("# = 0x%s\n", tal_hexstr(tmpctx, &sha, sizeof(sha)));
#endif

Expand Down Expand Up @@ -117,18 +117,18 @@ bool derive_revocation_key(const struct pubkey *basepoint,
struct pubkey *key)
{
struct sha256 sha;
unsigned char der_keys[PUBKEY_DER_LEN * 2];
unsigned char der_keys[PUBKEY_CMPR_LEN * 2];
secp256k1_pubkey add[2];
const secp256k1_pubkey *args[2];

pubkey_to_der(der_keys, basepoint);
pubkey_to_der(der_keys + PUBKEY_DER_LEN, per_commitment_point);
pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, per_commitment_point);
sha256(&sha, der_keys, sizeof(der_keys));
#ifdef SUPERVERBOSE
printf("# SHA256(revocation_basepoint || per_commitment_point)\n");
printf("# => SHA256(0x%s || 0x%s)\n",
tal_hexstr(tmpctx, der_keys, PUBKEY_DER_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_DER_LEN, PUBKEY_DER_LEN));
tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
printf("# = 0x%s\n", tal_hexstr(tmpctx, sha.u.u8, sizeof(sha.u.u8))),
#endif

Expand All @@ -141,13 +141,13 @@ bool derive_revocation_key(const struct pubkey *basepoint,
#endif

pubkey_to_der(der_keys, per_commitment_point);
pubkey_to_der(der_keys + PUBKEY_DER_LEN, basepoint);
pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, basepoint);
sha256(&sha, der_keys, sizeof(der_keys));
#ifdef SUPERVERBOSE
printf("# SHA256(per_commitment_point || revocation_basepoint)\n");
printf("# => SHA256(0x%s || 0x%s)\n",
tal_hexstr(tmpctx, der_keys, PUBKEY_DER_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_DER_LEN, PUBKEY_DER_LEN));
tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
printf("# = 0x%s\n", tal_hexstr(tmpctx, sha.u.u8, sizeof(sha.u.u8))),
#endif

Expand Down Expand Up @@ -188,17 +188,17 @@ bool derive_revocation_privkey(const struct secret *base_secret,
struct privkey *key)
{
struct sha256 sha;
unsigned char der_keys[PUBKEY_DER_LEN * 2];
unsigned char der_keys[PUBKEY_CMPR_LEN * 2];
struct secret part2;

pubkey_to_der(der_keys, basepoint);
pubkey_to_der(der_keys + PUBKEY_DER_LEN, per_commitment_point);
pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, per_commitment_point);
sha256(&sha, der_keys, sizeof(der_keys));
#ifdef SUPERVERBOSE
printf("# SHA256(revocation_basepoint || per_commitment_point)\n");
printf("# => SHA256(0x%s || 0x%s)\n",
tal_hexstr(tmpctx, der_keys, PUBKEY_DER_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_DER_LEN, PUBKEY_DER_LEN));
tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
printf("# = 0x%s\n", tal_hexstr(tmpctx, sha.u.u8, sizeof(sha.u.u8))),
#endif

Expand All @@ -214,13 +214,13 @@ bool derive_revocation_privkey(const struct secret *base_secret,
#endif

pubkey_to_der(der_keys, per_commitment_point);
pubkey_to_der(der_keys + PUBKEY_DER_LEN, basepoint);
pubkey_to_der(der_keys + PUBKEY_CMPR_LEN, basepoint);
sha256(&sha, der_keys, sizeof(der_keys));
#ifdef SUPERVERBOSE
printf("# SHA256(per_commitment_point || revocation_basepoint)\n");
printf("# => SHA256(0x%s || 0x%s)\n",
tal_hexstr(tmpctx, der_keys, PUBKEY_DER_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_DER_LEN, PUBKEY_DER_LEN));
tal_hexstr(tmpctx, der_keys, PUBKEY_CMPR_LEN),
tal_hexstr(tmpctx, der_keys + PUBKEY_CMPR_LEN, PUBKEY_CMPR_LEN));
printf("# = 0x%s\n", tal_hexstr(tmpctx, sha.u.u8, sizeof(sha.u.u8))),
#endif

Expand Down
8 changes: 4 additions & 4 deletions common/sphinx.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ u8 *serialize_onionpacket(
{
u8 *dst = tal_arr(ctx, u8, TOTAL_PACKET_SIZE);

u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
int p = 0;

pubkey_to_der(der, &m->ephemeralkey);
Expand All @@ -77,7 +77,7 @@ struct onionpacket *parse_onionpacket(const tal_t *ctx,
{
struct onionpacket *m;
int p = 0;
u8 rawEphemeralkey[PUBKEY_DER_LEN];
u8 rawEphemeralkey[PUBKEY_CMPR_LEN];

assert(srclen == TOTAL_PACKET_SIZE);

Expand Down Expand Up @@ -186,7 +186,7 @@ static void compute_blinding_factor(const struct pubkey *key,
u8 res[BLINDING_FACTOR_SIZE])
{
struct sha256_ctx ctx;
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
struct sha256 temp;

pubkey_to_der(der, key);
Expand Down Expand Up @@ -289,7 +289,7 @@ static struct hop_params *generate_hop_params(
/* Now hash temp and store it. This requires us to
* DER-serialize first and then skip the sign byte.
*/
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
pubkey_to_der(der, &temp);
struct sha256 h;
sha256(&h, der, sizeof(der));
Expand Down
4 changes: 2 additions & 2 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,11 +1191,11 @@ static struct io_plan *connect_activate(struct io_conn *conn,
static const char *seedname(const tal_t *ctx, const struct pubkey *id)
{
char bech32[100];
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
u5 *data = tal_arr(ctx, u5, 0);

pubkey_to_der(der, id);
bech32_push_bits(&data, der, PUBKEY_DER_LEN*8);
bech32_push_bits(&data, der, PUBKEY_CMPR_LEN*8);
bech32_encode(bech32, "ln", data, tal_count(data), sizeof(bech32));
return tal_fmt(ctx, "%s.lseed.bitcoinstats.com", bech32);
}
Expand Down
12 changes: 6 additions & 6 deletions connectd/handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum bolt8_side {
*/
struct act_one {
u8 v;
u8 pubkey[PUBKEY_DER_LEN];
u8 pubkey[PUBKEY_CMPR_LEN];
u8 tag[crypto_aead_chacha20poly1305_ietf_ABYTES];
};

Expand Down Expand Up @@ -68,7 +68,7 @@ static inline void check_act_one(const struct act_one *act1)
*/
struct act_two {
u8 v;
u8 pubkey[PUBKEY_DER_LEN];
u8 pubkey[PUBKEY_CMPR_LEN];
u8 tag[crypto_aead_chacha20poly1305_ietf_ABYTES];
};

Expand Down Expand Up @@ -98,7 +98,7 @@ static inline void check_act_two(const struct act_two *act2)
*/
struct act_three {
u8 v;
u8 ciphertext[PUBKEY_DER_LEN + crypto_aead_chacha20poly1305_ietf_ABYTES];
u8 ciphertext[PUBKEY_CMPR_LEN + crypto_aead_chacha20poly1305_ietf_ABYTES];
u8 tag[crypto_aead_chacha20poly1305_ietf_ABYTES];
};

Expand Down Expand Up @@ -211,7 +211,7 @@ static void sha_mix_in(struct sha256 *h, const void *data, size_t len)
/* h = SHA-256(h || pub.serializeCompressed()) */
static void sha_mix_in_key(struct sha256 *h, const struct pubkey *key)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];
size_t len = sizeof(der);

secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &len, &key->pubkey,
Expand Down Expand Up @@ -442,7 +442,7 @@ static struct handshake *new_handshake(const tal_t *ctx,
static struct io_plan *act_three_initiator(struct io_conn *conn,
struct handshake *h)
{
u8 spub[PUBKEY_DER_LEN];
u8 spub[PUBKEY_CMPR_LEN];
size_t len = sizeof(spub);

SUPERVERBOSE("Initiator: Act 3");
Expand Down Expand Up @@ -689,7 +689,7 @@ static struct io_plan *act_one_initiator(struct io_conn *conn,
static struct io_plan *act_three_responder2(struct io_conn *conn,
struct handshake *h)
{
u8 der[PUBKEY_DER_LEN];
u8 der[PUBKEY_CMPR_LEN];

SUPERVERBOSE("input: 0x%s", tal_hexstr(tmpctx, &h->act3, ACT_THREE_SIZE));

Expand Down
4 changes: 2 additions & 2 deletions hsmd/hsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ static void get_channel_seed(const struct pubkey *peer_id, u64 dbid,
struct secret *channel_seed)
{
struct secret channel_base;
u8 input[PUBKEY_DER_LEN + sizeof(dbid)];
u8 input[PUBKEY_CMPR_LEN + sizeof(dbid)];
/*~ Again, "per-peer" should be "per-channel", but Hysterical Raisins */
const char *info = "per-peer seed";

Expand All @@ -341,7 +341,7 @@ static void get_channel_seed(const struct pubkey *peer_id, u64 dbid,
/*~ For all that talk about platform-independence, note that this
* field is endian-dependent! But let's face it, little-endian won.
* In related news, we don't support EBCDIC or middle-endian. */
memcpy(input + PUBKEY_DER_LEN, &dbid, sizeof(dbid));
memcpy(input + PUBKEY_CMPR_LEN, &dbid, sizeof(dbid));

hkdf_sha256(channel_seed, sizeof(*channel_seed),
input, sizeof(input),
Expand Down
4 changes: 2 additions & 2 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ static void json_add_raw_pubkey(struct json_stream *response,
const u8 raw_pubkey[sizeof(struct pubkey)])
{
secp256k1_pubkey pubkey;
u8 der[PUBKEY_DER_LEN];
size_t outlen = PUBKEY_DER_LEN;
u8 der[PUBKEY_CMPR_LEN];
size_t outlen = PUBKEY_CMPR_LEN;

memcpy(&pubkey, raw_pubkey, sizeof(pubkey));
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen,
Expand Down
Loading

0 comments on commit 837a095

Please sign in to comment.