From 36c8fc7ef8011edf35e13a4d9b8ac4840bdd12d7 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 2 Dec 2016 18:13:27 +1030 Subject: [PATCH] lightningd: remove secpctx Use the global in the few remaining places. Signed-off-by: Rusty Russell --- daemon/cryptopkt.c | 12 ++++++------ daemon/lightningd.c | 5 +++-- daemon/lightningd.h | 3 --- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/daemon/cryptopkt.c b/daemon/cryptopkt.c index 06f2a23c9..5af70d05e 100644 --- a/daemon/cryptopkt.c +++ b/daemon/cryptopkt.c @@ -8,6 +8,7 @@ #include "peer.h" #include "protobuf_convert.h" #include "secrets.h" +#include "utils.h" #include #include #include @@ -507,7 +508,7 @@ static struct io_plan *keys_exchanged(struct io_conn *conn, } /* Derive shared secret. */ - if (!secp256k1_ecdh(neg->dstate->secpctx, shared_secret, + if (!secp256k1_ecdh(secp256k1_ctx, shared_secret, &sessionkey.pubkey, neg->seckey)) { log_unusual(neg->log, "Bad ECDH"); return io_close(conn); @@ -590,13 +591,12 @@ static struct io_plan *session_key_len_receive(struct io_conn *conn, session_key_receive, neg); } -static void gen_sessionkey(secp256k1_context *ctx, - u8 seckey[32], +static void gen_sessionkey(u8 seckey[32], secp256k1_pubkey *pubkey) { do { randombytes_buf(seckey, 32); - } while (!secp256k1_ec_pubkey_create(ctx, pubkey, seckey)); + } while (!secp256k1_ec_pubkey_create(secp256k1_ctx, pubkey, seckey)); } static struct io_plan *write_sessionkey(struct io_conn *conn, @@ -638,10 +638,10 @@ struct io_plan *peer_crypto_setup_(struct io_conn *conn, neg->expected_id = id; neg->log = log; - gen_sessionkey(dstate->secpctx, neg->seckey, &sessionkey); + gen_sessionkey(neg->seckey, &sessionkey); outputlen = sizeof(neg->our_sessionpubkey); - secp256k1_ec_pubkey_serialize(dstate->secpctx, + secp256k1_ec_pubkey_serialize(secp256k1_ctx, neg->our_sessionpubkey, &outputlen, &sessionkey, SECP256K1_EC_COMPRESSED); diff --git a/daemon/lightningd.c b/daemon/lightningd.c index 0203900c4..a73bba35a 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -356,8 +356,6 @@ static struct lightningd_state *lightningd_state(void) timers_init(&dstate->timers, time_mono()); txwatch_hash_init(&dstate->txwatches); txowatch_hash_init(&dstate->txowatches); - secp256k1_ctx = dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY - | SECP256K1_CONTEXT_SIGN); list_head_init(&dstate->bitcoin_req); list_head_init(&dstate->wallet); list_head_init(&dstate->unpaid); @@ -483,6 +481,9 @@ int main(int argc, char *argv[]) errx(1, "Compiled against protobuf %s, but have %s", PROTOBUF_C_VERSION, protobuf_c_version()); + secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY + | SECP256K1_CONTEXT_SIGN); + opt_register_noarg("--help|-h", opt_usage_and_exit, "\n" "A bitcoin lightning daemon.", diff --git a/daemon/lightningd.h b/daemon/lightningd.h index 82c1dce9f..52975afed 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -107,9 +107,6 @@ struct lightningd_state { /* Any outstanding "pay" commands. */ struct list_head pay_commands; - /* Crypto tables for global use. */ - secp256k1_context *secpctx; - /* Our private key */ struct secret *secret;