From 9ffac49c6f91329487b93534e9f444f97c6a7a2c Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 6 Oct 2015 23:49:52 +1000 Subject: [PATCH] onion_key: allowing both odd and even pubkeys output compressed public keys; accept compressed pubkey in test_onion --- test/onion_key.c | 10 +++++++--- test/onion_key.h | 5 +++++ test/test_onion.c | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/onion_key.c b/test/onion_key.c index 9ab4740a7..810e8fcee 100644 --- a/test/onion_key.c +++ b/test/onion_key.c @@ -20,6 +20,7 @@ static void random_bytes(void *dst, size_t n) d[i] = random() % 256; } +#if 0 /* Compressed key would start with 0x3? Subtract from group. Thanks * Greg Maxwell. */ static void flip_key(struct seckey *seckey) @@ -47,6 +48,7 @@ static void flip_key(struct seckey *seckey) seckey->u.be64[i] = cpu_to_be64(v); } } +#endif #if 0 int main(int argc, char *argv[]) @@ -97,7 +99,7 @@ static void random_key(secp256k1_context *ctx, /* We don't want to spend a byte encoding sign, so make sure it's 0x2 */ static void gen_keys(secp256k1_context *ctx, - struct seckey *seckey, struct onion_pubkey *pubkey) + struct seckey *seckey, struct compressed_pubkey *pubkey) { unsigned char tmp[33]; secp256k1_pubkey pkey; @@ -108,16 +110,18 @@ static void gen_keys(secp256k1_context *ctx, secp256k1_ec_pubkey_serialize(ctx, tmp, &len, &pkey, SECP256K1_EC_COMPRESSED); assert(len == sizeof(tmp)); +#if 0 if (tmp[0] == 0x3) flip_key(seckey); - memcpy(pubkey, tmp+1, sizeof(*pubkey)); +#endif + memcpy(pubkey, tmp, sizeof(*pubkey)); } void print_keypair(int pub, int priv) { secp256k1_context *ctx; struct seckey seckey; - struct onion_pubkey pubkey; + struct compressed_pubkey pubkey; char sechex[hex_str_size(sizeof(seckey))]; char pubhex[hex_str_size(sizeof(pubkey))]; diff --git a/test/onion_key.h b/test/onion_key.h index 17335ed5c..61cd36083 100644 --- a/test/onion_key.h +++ b/test/onion_key.h @@ -11,6 +11,11 @@ struct seckey { } u; }; +/* First byte is 0x02 or 0x03 indicating even or odd y */ +struct compressed_pubkey { + unsigned char u8[33]; +}; + /* Prepend 0x02 to get pubkey for libsecp256k1 */ struct onion_pubkey { unsigned char u8[32]; diff --git a/test/test_onion.c b/test/test_onion.c index e6f6f6bff..3c5df9110 100644 --- a/test/test_onion.c +++ b/test/test_onion.c @@ -588,9 +588,9 @@ bool peel_onion(struct onion *onion, static bool parse_onion_pubkey(secp256k1_context *ctx, const char *arg, secp256k1_pubkey *pubkey) { - unsigned char tmp[33] = { 0x2 }; + unsigned char tmp[33] = { }; - if (!hex_decode(arg, strlen(arg), tmp + 1, sizeof(tmp) - 1)) + if (!hex_decode(arg, strlen(arg), tmp, sizeof(tmp))) return false; return secp256k1_ec_pubkey_parse(ctx, pubkey, tmp, sizeof(tmp));