utils: add a global secp, fix wire to use it.

This repairs make check.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-12-02 18:11:06 +10:30
parent 1f447688bc
commit c938ebb5c0
7 changed files with 43 additions and 54 deletions

View File

@ -11,6 +11,7 @@
#include "routing.h" #include "routing.h"
#include "secrets.h" #include "secrets.h"
#include "timeout.h" #include "timeout.h"
#include "utils.h"
#include <ccan/container_of/container_of.h> #include <ccan/container_of/container_of.h>
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <ccan/io/io.h> #include <ccan/io/io.h>
@ -355,7 +356,7 @@ static struct lightningd_state *lightningd_state(void)
timers_init(&dstate->timers, time_mono()); timers_init(&dstate->timers, time_mono());
txwatch_hash_init(&dstate->txwatches); txwatch_hash_init(&dstate->txwatches);
txowatch_hash_init(&dstate->txowatches); txowatch_hash_init(&dstate->txowatches);
dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY secp256k1_ctx = dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN); | SECP256K1_CONTEXT_SIGN);
list_head_init(&dstate->bitcoin_req); list_head_init(&dstate->bitcoin_req);
list_head_init(&dstate->wallet); list_head_init(&dstate->wallet);

View File

@ -1,6 +1,8 @@
#include "utils.h" #include "utils.h"
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
secp256k1_context *secp256k1_ctx;
char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
{ {
char *str = tal_arr(ctx, char, hex_str_size(len)); char *str = tal_arr(ctx, char, hex_str_size(len));

View File

@ -3,6 +3,9 @@
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <secp256k1.h>
extern secp256k1_context *secp256k1_ctx;
/* Allocate and fill in a hex-encoded string of this data. */ /* Allocate and fill in a hex-encoded string of this data. */
char *tal_hexstr(const tal_t *ctx, const void *data, size_t len); char *tal_hexstr(const tal_t *ctx, const void *data, size_t len);

View File

@ -1,3 +1,4 @@
#include "utils.h"
#include "wire.h" #include "wire.h"
#include <bitcoin/pubkey.h> #include <bitcoin/pubkey.h>
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
@ -64,25 +65,25 @@ u64 fromwire_u64(const u8 **cursor, size_t *max)
return be64_to_cpu(ret); return be64_to_cpu(ret);
} }
void fromwire_pubkey(secp256k1_context *secpctx, const u8 **cursor, size_t *max, struct pubkey *pubkey) void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
{ {
u8 der[PUBKEY_DER_LEN]; u8 der[PUBKEY_DER_LEN];
if (!fromwire(cursor, max, der, sizeof(der))) if (!fromwire(cursor, max, der, sizeof(der)))
return; return;
if (!pubkey_from_der(secpctx, der, sizeof(der), pubkey)) if (!pubkey_from_der(secp256k1_ctx, der, sizeof(der), pubkey))
fail_pull(cursor, max); fail_pull(cursor, max);
} }
void fromwire_signature(secp256k1_context *secpctx, const u8 **cursor, size_t *max, struct signature *sig) void fromwire_signature(const u8 **cursor, size_t *max, struct signature *sig)
{ {
u8 compact[64]; u8 compact[64];
if (!fromwire(cursor, max, compact, sizeof(compact))) if (!fromwire(cursor, max, compact, sizeof(compact)))
return; return;
if (secp256k1_ecdsa_signature_parse_compact(secpctx, if (secp256k1_ecdsa_signature_parse_compact(secp256k1_ctx,
&sig->sig, compact) &sig->sig, compact)
!= 1) != 1)
fail_pull(cursor, max); fail_pull(cursor, max);
@ -122,12 +123,11 @@ void fromwire_pad_array(const u8 **cursor, size_t *max, u8 *arr, size_t num)
fromwire(cursor, max, arr, num); fromwire(cursor, max, arr, num);
} }
void fromwire_signature_array(secp256k1_context *secpctx, void fromwire_signature_array(const u8 **cursor, size_t *max,
const u8 **cursor, size_t *max,
struct signature *arr, size_t num) struct signature *arr, size_t num)
{ {
size_t i; size_t i;
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
fromwire_signature(secpctx, cursor, max, arr + i); fromwire_signature(cursor, max, arr + i);
} }

View File

@ -8,20 +8,12 @@ import re
Enumtype = namedtuple('Enumtype', ['name', 'value']) Enumtype = namedtuple('Enumtype', ['name', 'value'])
# Field types that require a crypto context
crypto_types = [
'struct pubkey',
'struct signature'
]
class Field(object): class Field(object):
def __init__(self,message,name,size): def __init__(self,message,name,size):
self.message = message self.message = message
self.name = name.replace('-', '_') self.name = name.replace('-', '_')
(self.typename, self.basesize) = Field._guess_type(message,self.name,size) (self.typename, self.basesize) = Field._guess_type(message,self.name,size)
self.is_crypto = self.typename in crypto_types
try: try:
if int(size) % self.basesize != 0: if int(size) % self.basesize != 0:
raise ValueError('Invalid size {} for {}.{} not a multiple of {}'.format(size,self.message,self.name,self.basesize)) raise ValueError('Invalid size {} for {}.{} not a multiple of {}'.format(size,self.message,self.name,self.basesize))
@ -110,7 +102,6 @@ class Message(object):
self.name = name self.name = name
self.enum = enum self.enum = enum
self.fields = [] self.fields = []
self.is_crypto = False
def checkLenField(self,field): def checkLenField(self,field):
for f in self.fields: for f in self.fields:
@ -132,7 +123,6 @@ class Message(object):
if field.is_variable_size(): if field.is_variable_size():
self.checkLenField(field) self.checkLenField(field)
self.fields.append(field) self.fields.append(field)
self.is_crypto |= field.is_crypto
def print_structure(self): def print_structure(self):
print('struct msg_{} {{'.format(self.name)); print('struct msg_{} {{'.format(self.name));
@ -149,9 +139,7 @@ class Message(object):
print('};') print('};')
def print_fromwire(self,is_header): def print_fromwire(self,is_header):
crypto_arg = "secp256k1_context *secpctx, " if self.is_crypto else "" print('struct msg_{0} *fromwire_{0}(const tal_t *ctx, const void *p, size_t *len)'.format(self.name), end='')
print('struct msg_{0} *fromwire_{0}({1}const tal_t *ctx, const void *p, size_t *len)'.format(self.name, crypto_arg), end='')
if is_header: if is_header:
print(';') print(';')
@ -168,25 +156,24 @@ class Message(object):
if f.typename.startswith('struct '): if f.typename.startswith('struct '):
basetype=f.typename[7:] basetype=f.typename[7:]
crypto_param = "secpctx, " if f.is_crypto else ""
if f.is_array(): if f.is_array():
print("\t//1th case", f.name) print("\t//1th case", f.name)
print('\tfromwire_{}_array({}&cursor, len, in->{}, {});' print('\tfromwire_{}_array(&cursor, len, in->{}, {});'
.format(basetype, crypto_param, f.name, f.num_elems)) .format(basetype, f.name, f.num_elems))
elif f.is_variable_size(): elif f.is_variable_size():
print("\t//2th case", f.name) print("\t//2th case", f.name)
print('\tin->{} = tal_arr(in, {}, in->{});' print('\tin->{} = tal_arr(in, {}, in->{});'
.format(f.name, f.typename, f.lenvar)) .format(f.name, f.typename, f.lenvar))
print('\tfromwire_{}_array({}&cursor, len, in->{}, in->{});' print('\tfromwire_{}_array(&cursor, len, in->{}, in->{});'
.format(basetype, crypto_param, f.name, f.lenvar)) .format(basetype, f.name, f.lenvar))
elif f.is_assignable(): elif f.is_assignable():
print("\t//3th case", f.name) print("\t//3th case", f.name)
print('\tin->{} = fromwire_{}(&cursor, len);' print('\tin->{} = fromwire_{}(&cursor, len);'
.format(f.name, basetype)) .format(f.name, basetype))
else: else:
print("\t//4th case", f.name) print("\t//4th case", f.name)
print('\tfromwire_{}({}&cursor, len, &in->{});' print('\tfromwire_{}(&cursor, len, &in->{});'
.format(basetype, crypto_param, f.name)) .format(basetype, f.name))
print('\n' print('\n'
'\tif (!cursor)\n' '\tif (!cursor)\n'
@ -195,8 +182,7 @@ class Message(object):
'}\n') '}\n')
def print_towire(self,is_header): def print_towire(self,is_header):
crypto_arg = "secp256k1_context *secpctx, " if self.is_crypto else "" print('u8 *towire_{0}(const tal_t *ctx, const struct msg_{0} *out)'.format(self.name), end='')
print('u8 *towire_{0}({1}const tal_t *ctx, const struct msg_{0} *out)'.format(self.name, crypto_arg), end='')
if is_header: if is_header:
print(';') print(';')
@ -212,19 +198,18 @@ class Message(object):
if f.typename.startswith('struct '): if f.typename.startswith('struct '):
basetype=f.typename[7:] basetype=f.typename[7:]
crypto_param = "secpctx, " if f.is_crypto else ""
if f.is_array(): if f.is_array():
print('\ttowire_{}_array({}&p, out->{}, {});' print('\ttowire_{}_array(&p, out->{}, {});'
.format(basetype, crypto_param, f.name, f.num_elems)) .format(basetype, f.name, f.num_elems))
elif f.is_variable_size(): elif f.is_variable_size():
print('\ttowire_{}_array({}&p, out->{}, out->{});' print('\ttowire_{}_array(&p, out->{}, out->{});'
.format(basetype, crypto_param, f.name, f.lenvar)) .format(basetype, f.name, f.lenvar))
elif f.is_assignable(): elif f.is_assignable():
print('\ttowire_{}({}&p, out->{});' print('\ttowire_{}(&p, out->{});'
.format(basetype, crypto_param, f.name)) .format(basetype, f.name))
else: else:
print('\ttowire_{}({}&p, &out->{});' print('\ttowire_{}(&p, &out->{});'
.format(basetype, crypto_param, f.name)) .format(basetype, f.name))
print('\n' print('\n'
'\treturn p;\n' '\treturn p;\n'

View File

@ -1,3 +1,4 @@
#include "utils.h"
#include "wire.h" #include "wire.h"
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
@ -34,22 +35,22 @@ void towire_u64(u8 **pptr, u64 v)
towire(pptr, &l, sizeof(l)); towire(pptr, &l, sizeof(l));
} }
void towire_pubkey(secp256k1_context *secpctx, u8 **pptr, const struct pubkey *pubkey) void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
{ {
u8 output[PUBKEY_DER_LEN]; u8 output[PUBKEY_DER_LEN];
size_t outputlen = sizeof(output); size_t outputlen = sizeof(output);
secp256k1_ec_pubkey_serialize(secpctx, output, &outputlen, secp256k1_ec_pubkey_serialize(secp256k1_ctx, output, &outputlen,
&pubkey->pubkey, &pubkey->pubkey,
SECP256K1_EC_COMPRESSED); SECP256K1_EC_COMPRESSED);
towire(pptr, output, outputlen); towire(pptr, output, outputlen);
} }
void towire_signature(secp256k1_context *secpctx, u8 **pptr, const struct signature *sig) void towire_signature(u8 **pptr, const struct signature *sig)
{ {
u8 compact[64]; u8 compact[64];
secp256k1_ecdsa_signature_serialize_compact(secpctx, secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
compact, &sig->sig); compact, &sig->sig);
towire(pptr, compact, sizeof(compact)); towire(pptr, compact, sizeof(compact));
} }
@ -88,10 +89,10 @@ void towire_pad_array(u8 **pptr, const u8 *arr, size_t num)
memset(*pptr + oldsize, 0, num); memset(*pptr + oldsize, 0, num);
} }
void towire_signature_array(secp256k1_context *secpctx, u8 **pptr, const struct signature *arr, size_t num) void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num)
{ {
size_t i; size_t i;
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
towire_signature(secpctx, pptr, arr+i); towire_signature(pptr, arr+i);
} }

View File

@ -7,9 +7,6 @@
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <stdlib.h> #include <stdlib.h>
/* FIXME: Move this declaration! */
extern secp256k1_context *secp256k1_ctx;
struct pubkey; struct pubkey;
struct sha256; struct sha256;
struct channel_id { struct channel_id {
@ -22,8 +19,8 @@ struct ipv6 {
}; };
void towire(u8 **pptr, const void *data, size_t len); void towire(u8 **pptr, const void *data, size_t len);
void towire_pubkey(secp256k1_context *secpctx, u8 **pptr, const struct pubkey *pubkey); void towire_pubkey(u8 **pptr, const struct pubkey *pubkey);
void towire_signature(secp256k1_context *secpctx, u8 **pptr, const struct signature *signature); void towire_signature(u8 **pptr, const struct signature *signature);
void towire_channel_id(u8 **pptr, const struct channel_id *channel_id); void towire_channel_id(u8 **pptr, const struct channel_id *channel_id);
void towire_sha256(u8 **pptr, const struct sha256 *sha256); void towire_sha256(u8 **pptr, const struct sha256 *sha256);
void towire_ipv6(u8 **pptr, const struct ipv6 *ipv6); void towire_ipv6(u8 **pptr, const struct ipv6 *ipv6);
@ -34,7 +31,7 @@ void towire_u64(u8 **pptr, u64 v);
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num); void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
void towire_pad_array(u8 **pptr, const u8 *arr, size_t num); void towire_pad_array(u8 **pptr, const u8 *arr, size_t num);
void towire_signature_array(secp256k1_context *secpctx, u8 **pptr, const struct signature *arr, size_t num); void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num);
const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n); const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n);
@ -42,8 +39,8 @@ u8 fromwire_u8(const u8 **cursor, size_t *max);
u16 fromwire_u16(const u8 **cursor, size_t *max); u16 fromwire_u16(const u8 **cursor, size_t *max);
u32 fromwire_u32(const u8 **cursor, size_t *max); u32 fromwire_u32(const u8 **cursor, size_t *max);
u64 fromwire_u64(const u8 **cursor, size_t *max); u64 fromwire_u64(const u8 **cursor, size_t *max);
void fromwire_pubkey(secp256k1_context *secpctx, const u8 **cursor, size_t *max, struct pubkey *pubkey); void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);
void fromwire_signature(secp256k1_context *secpctx, const u8 **cursor, size_t *max, void fromwire_signature(const u8 **cursor, size_t *max,
struct signature *signature); struct signature *signature);
void fromwire_channel_id(const u8 **cursor, size_t *max, void fromwire_channel_id(const u8 **cursor, size_t *max,
struct channel_id *channel_id); struct channel_id *channel_id);
@ -54,7 +51,7 @@ void fromwire_u8_array(const u8 **cursor, size_t *max,
u8 *arr, size_t num); u8 *arr, size_t num);
void fromwire_pad_array(const u8 **cursor, size_t *max, void fromwire_pad_array(const u8 **cursor, size_t *max,
u8 *arr, size_t num); u8 *arr, size_t num);
void fromwire_signature_array(secp256k1_context *secpctx, const u8 **cursor, size_t *max, void fromwire_signature_array(const u8 **cursor, size_t *max,
struct signature *arr, size_t num); struct signature *arr, size_t num);
#endif /* LIGHTNING_WIRE_WIRE_H */ #endif /* LIGHTNING_WIRE_WIRE_H */