Skip to content

Commit a517b2c

Browse files
committed
onion_key: allowing both odd and even pubkeys
output compressed public keys; accept compressed pubkey in test_onion
1 parent 0289ee2 commit a517b2c

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

test/onion_key.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static void random_bytes(void *dst, size_t n)
2020
d[i] = random() % 256;
2121
}
2222

23+
#if 0
2324
/* Compressed key would start with 0x3? Subtract from group. Thanks
2425
* Greg Maxwell. */
2526
static void flip_key(struct seckey *seckey)
@@ -47,6 +48,7 @@ static void flip_key(struct seckey *seckey)
4748
seckey->u.be64[i] = cpu_to_be64(v);
4849
}
4950
}
51+
#endif
5052

5153
#if 0
5254
int main(int argc, char *argv[])
@@ -97,7 +99,7 @@ static void random_key(secp256k1_context *ctx,
9799

98100
/* We don't want to spend a byte encoding sign, so make sure it's 0x2 */
99101
static void gen_keys(secp256k1_context *ctx,
100-
struct seckey *seckey, struct onion_pubkey *pubkey)
102+
struct seckey *seckey, struct compressed_pubkey *pubkey)
101103
{
102104
unsigned char tmp[33];
103105
secp256k1_pubkey pkey;
@@ -108,16 +110,18 @@ static void gen_keys(secp256k1_context *ctx,
108110
secp256k1_ec_pubkey_serialize(ctx, tmp, &len, &pkey,
109111
SECP256K1_EC_COMPRESSED);
110112
assert(len == sizeof(tmp));
113+
#if 0
111114
if (tmp[0] == 0x3)
112115
flip_key(seckey);
113-
memcpy(pubkey, tmp+1, sizeof(*pubkey));
116+
#endif
117+
memcpy(pubkey, tmp, sizeof(*pubkey));
114118
}
115119

116120
void print_keypair(int pub, int priv)
117121
{
118122
secp256k1_context *ctx;
119123
struct seckey seckey;
120-
struct onion_pubkey pubkey;
124+
struct compressed_pubkey pubkey;
121125
char sechex[hex_str_size(sizeof(seckey))];
122126
char pubhex[hex_str_size(sizeof(pubkey))];
123127

test/onion_key.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ struct seckey {
1111
} u;
1212
};
1313

14+
/* First byte is 0x02 or 0x03 indicating even or odd y */
15+
struct compressed_pubkey {
16+
unsigned char u8[33];
17+
};
18+
1419
/* Prepend 0x02 to get pubkey for libsecp256k1 */
1520
struct onion_pubkey {
1621
unsigned char u8[32];

test/test_onion.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ bool peel_onion(struct onion *onion,
588588
static bool parse_onion_pubkey(secp256k1_context *ctx,
589589
const char *arg, secp256k1_pubkey *pubkey)
590590
{
591-
unsigned char tmp[33] = { 0x2 };
591+
unsigned char tmp[33] = { };
592592

593-
if (!hex_decode(arg, strlen(arg), tmp + 1, sizeof(tmp) - 1))
593+
if (!hex_decode(arg, strlen(arg), tmp, sizeof(tmp)))
594594
return false;
595595

596596
return secp256k1_ec_pubkey_parse(ctx, pubkey, tmp, sizeof(tmp));

0 commit comments

Comments
 (0)