Browse Source

onion_key: allowing both odd and even pubkeys

output compressed public keys; accept compressed pubkey in test_onion
ppa-0.6.1
Anthony Towns 10 years ago
parent
commit
9ffac49c6f
  1. 10
      test/onion_key.c
  2. 5
      test/onion_key.h
  3. 4
      test/test_onion.c

10
test/onion_key.c

@ -20,6 +20,7 @@ static void random_bytes(void *dst, size_t n)
d[i] = random() % 256; d[i] = random() % 256;
} }
#if 0
/* Compressed key would start with 0x3? Subtract from group. Thanks /* Compressed key would start with 0x3? Subtract from group. Thanks
* Greg Maxwell. */ * Greg Maxwell. */
static void flip_key(struct seckey *seckey) 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); seckey->u.be64[i] = cpu_to_be64(v);
} }
} }
#endif
#if 0 #if 0
int main(int argc, char *argv[]) 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 */ /* We don't want to spend a byte encoding sign, so make sure it's 0x2 */
static void gen_keys(secp256k1_context *ctx, 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]; unsigned char tmp[33];
secp256k1_pubkey pkey; secp256k1_pubkey pkey;
@ -108,16 +110,18 @@ static void gen_keys(secp256k1_context *ctx,
secp256k1_ec_pubkey_serialize(ctx, tmp, &len, &pkey, secp256k1_ec_pubkey_serialize(ctx, tmp, &len, &pkey,
SECP256K1_EC_COMPRESSED); SECP256K1_EC_COMPRESSED);
assert(len == sizeof(tmp)); assert(len == sizeof(tmp));
#if 0
if (tmp[0] == 0x3) if (tmp[0] == 0x3)
flip_key(seckey); flip_key(seckey);
memcpy(pubkey, tmp+1, sizeof(*pubkey)); #endif
memcpy(pubkey, tmp, sizeof(*pubkey));
} }
void print_keypair(int pub, int priv) void print_keypair(int pub, int priv)
{ {
secp256k1_context *ctx; secp256k1_context *ctx;
struct seckey seckey; struct seckey seckey;
struct onion_pubkey pubkey; struct compressed_pubkey pubkey;
char sechex[hex_str_size(sizeof(seckey))]; char sechex[hex_str_size(sizeof(seckey))];
char pubhex[hex_str_size(sizeof(pubkey))]; char pubhex[hex_str_size(sizeof(pubkey))];

5
test/onion_key.h

@ -11,6 +11,11 @@ struct seckey {
} u; } 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 */ /* Prepend 0x02 to get pubkey for libsecp256k1 */
struct onion_pubkey { struct onion_pubkey {
unsigned char u8[32]; unsigned char u8[32];

4
test/test_onion.c

@ -588,9 +588,9 @@ bool peel_onion(struct onion *onion,
static bool parse_onion_pubkey(secp256k1_context *ctx, static bool parse_onion_pubkey(secp256k1_context *ctx,
const char *arg, secp256k1_pubkey *pubkey) 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 false;
return secp256k1_ec_pubkey_parse(ctx, pubkey, tmp, sizeof(tmp)); return secp256k1_ec_pubkey_parse(ctx, pubkey, tmp, sizeof(tmp));

Loading…
Cancel
Save