From b572cea7c44b74051d8295c32c627e8f0706f9ab Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Thu, 21 Oct 2021 10:17:13 +0200 Subject: [PATCH] processing: cache the secp context --- fuzz/targets/process_sign_message.rs | 1 + src/bin/cosignerd.rs | 3 ++- src/processing.rs | 9 ++++++--- src/tests/builder.rs | 2 ++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/fuzz/targets/process_sign_message.rs b/fuzz/targets/process_sign_message.rs index 12127e9..b45e06c 100644 --- a/fuzz/targets/process_sign_message.rs +++ b/fuzz/targets/process_sign_message.rs @@ -34,6 +34,7 @@ fn main() { &builder.config, msg, &builder.bitcoin_privkey, + &builder.secp, ) { Ok(res) => res, Err(cosignerd::processing::SignProcessingError::Database(e)) => panic!("{}", e), diff --git a/src/bin/cosignerd.rs b/src/bin/cosignerd.rs index 0f9820d..4eeefed 100644 --- a/src/bin/cosignerd.rs +++ b/src/bin/cosignerd.rs @@ -65,6 +65,7 @@ fn daemon_main( }); let managers_noise_pubkeys: Vec = config.managers.iter().map(|m| m.noise_key).collect(); + let secp_ctx = secp256k1::Secp256k1::new(); // We expect a single connection once in a while, there is *no need* for complexity here so // just treat incoming connections sequentially. @@ -86,7 +87,7 @@ fn daemon_main( RequestParams::Sign(sign_req) => { log::trace!("Decoded request: {:#?}", sign_req); - let res = match process_sign_message(&config, sign_req, bitcoin_privkey) { + let res = match process_sign_message(&config, sign_req, bitcoin_privkey, &secp_ctx) { Ok(res) => res, Err(e) => { log::error!("Error when processing 'sign' message: '{}'", e); diff --git a/src/processing.rs b/src/processing.rs index 9c1982d..aa0cd49 100644 --- a/src/processing.rs +++ b/src/processing.rs @@ -5,7 +5,7 @@ use crate::{ use revault_net::message::cosigner::{SignRequest, SignResult}; use revault_tx::{ - bitcoin::{secp256k1, PublicKey as BitcoinPubkey, SigHashType, util::bip143::SigHashCache}, + bitcoin::{secp256k1, util::bip143::SigHashCache, PublicKey as BitcoinPubkey, SigHashType}, error::InputSatisfactionError, transactions::RevaultTransaction, }; @@ -42,10 +42,9 @@ pub fn process_sign_message( config: &Config, sign_msg: SignRequest, bitcoin_privkey: &secp256k1::SecretKey, + secp: &secp256k1::Secp256k1, ) -> Result { let db_path = config.db_file(); - // TODO: Cache it in the caller - let secp = secp256k1::Secp256k1::new(); let our_pubkey = BitcoinPubkey { compressed: true, key: secp256k1::PublicKey::from_secret_key(&secp, bitcoin_privkey), @@ -173,6 +172,7 @@ mod test { &test_framework.config, sign_a.clone(), &test_framework.bitcoin_privkey, + &test_framework.secp, ) .unwrap(); let tx = tx.unwrap(); @@ -190,6 +190,7 @@ mod test { &test_framework.config, sign_a, &test_framework.bitcoin_privkey, + &test_framework.secp, ) .unwrap(); assert_eq!(tx, second_psbt.unwrap()); @@ -211,6 +212,7 @@ mod test { &test_framework.config, sign_a, &test_framework.bitcoin_privkey, + &test_framework.secp, ) .unwrap(); assert!(tx.is_none(), "It contains a duplicated outpoint"); @@ -226,6 +228,7 @@ mod test { &test_framework.config, sign_msg, &test_framework.bitcoin_privkey, + &test_framework.secp, ) .unwrap_err(); } diff --git a/src/tests/builder.rs b/src/tests/builder.rs index 20f974f..03aad81 100644 --- a/src/tests/builder.rs +++ b/src/tests/builder.rs @@ -35,6 +35,7 @@ pub struct CosignerTestBuilder { pub noise_privkey: NoisePrivkey, pub bitcoin_privkey: secp256k1::SecretKey, pub managers_keys: Vec, + pub secp: secp256k1::Secp256k1, } impl CosignerTestBuilder { @@ -95,6 +96,7 @@ impl CosignerTestBuilder { noise_privkey, bitcoin_privkey, managers_keys, + secp, } }