Skip to content

Commit 6017379

Browse files
KeysInterface: add new ecdh method
This method will help us avoid retrieving our node secret, something we want to get rid of entirely. It will be used in upcoming commits when decoding the onion message packet, and in future PRs to help us get rid of KeysInterface::get_node_secret usages across the codebase
1 parent 33ff274 commit 6017379

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use utils::test_logger::{self, Output};
5454
use utils::test_persister::TestPersister;
5555

5656
use bitcoin::secp256k1::{PublicKey,SecretKey};
57+
use bitcoin::secp256k1::ecdh::SharedSecret;
5758
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
5859
use bitcoin::secp256k1::Secp256k1;
5960

@@ -165,6 +166,14 @@ impl KeysInterface for KeyProvider {
165166
Ok(SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id]).unwrap())
166167
}
167168

169+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> {
170+
let mut node_secret = self.get_node_secret(recipient)?;
171+
if let Some(tweak) = tweak {
172+
node_secret.mul_assign(tweak).map_err(|_| ())?;
173+
}
174+
Ok(SharedSecret::new(other_key, &node_secret))
175+
}
176+
168177
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
169178
KeyMaterial([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id])
170179
}

fuzz/src/full_stack.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use utils::test_logger;
5151
use utils::test_persister::TestPersister;
5252

5353
use bitcoin::secp256k1::{PublicKey,SecretKey};
54+
use bitcoin::secp256k1::ecdh::SharedSecret;
5455
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
5556
use bitcoin::secp256k1::Secp256k1;
5657

@@ -269,6 +270,14 @@ impl KeysInterface for KeyProvider {
269270
Ok(self.node_secret.clone())
270271
}
271272

273+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> {
274+
let mut node_secret = self.get_node_secret(recipient)?;
275+
if let Some(tweak) = tweak {
276+
node_secret.mul_assign(tweak).map_err(|_| ())?;
277+
}
278+
Ok(SharedSecret::new(other_key, &node_secret))
279+
}
280+
272281
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
273282
self.inbound_payment_key.clone()
274283
}

lightning/src/chain/keysinterface.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use bitcoin::hash_types::WPubkeyHash;
2727

2828
use bitcoin::secp256k1::{SecretKey, PublicKey};
2929
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature, Signing};
30+
use bitcoin::secp256k1::ecdh::SharedSecret;
3031
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
3132
use bitcoin::{secp256k1, Witness};
3233

@@ -404,6 +405,12 @@ pub trait KeysInterface {
404405
/// This method must return the same value each time it is called with a given `Recipient`
405406
/// parameter.
406407
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()>;
408+
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
409+
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
410+
/// secret, though this is less efficient.
411+
///
412+
/// [`node secret`]: Self::get_node_secret
413+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()>;
407414
/// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
408415
///
409416
/// This method should return a different value each time it is called, to avoid linking
@@ -1133,6 +1140,14 @@ impl KeysInterface for KeysManager {
11331140
}
11341141
}
11351142

1143+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> {
1144+
let mut node_secret = self.get_node_secret(recipient)?;
1145+
if let Some(tweak) = tweak {
1146+
node_secret.mul_assign(tweak).map_err(|_| ())?;
1147+
}
1148+
Ok(SharedSecret::new(other_key, &node_secret))
1149+
}
1150+
11361151
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
11371152
self.inbound_payment_key.clone()
11381153
}
@@ -1217,6 +1232,14 @@ impl KeysInterface for PhantomKeysManager {
12171232
}
12181233
}
12191234

1235+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> {
1236+
let mut node_secret = self.get_node_secret(recipient)?;
1237+
if let Some(tweak) = tweak {
1238+
node_secret.mul_assign(tweak).map_err(|_| ())?;
1239+
}
1240+
Ok(SharedSecret::new(other_key, &node_secret))
1241+
}
1242+
12201243
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
12211244
self.inbound_payment_key.clone()
12221245
}

lightning/src/ln/channel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6583,6 +6583,7 @@ mod tests {
65836583
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
65846584
use bitcoin::secp256k1::ffi::Signature as FFISignature;
65856585
use bitcoin::secp256k1::{SecretKey,PublicKey};
6586+
use bitcoin::secp256k1::ecdh::SharedSecret;
65866587
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
65876588
use bitcoin::hashes::sha256::Hash as Sha256;
65886589
use bitcoin::hashes::Hash;
@@ -6621,6 +6622,7 @@ mod tests {
66216622
type Signer = InMemorySigner;
66226623

66236624
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { panic!(); }
6625+
fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> { panic!(); }
66246626
fn get_inbound_payment_key_material(&self) -> KeyMaterial { panic!(); }
66256627
fn get_destination_script(&self) -> Script {
66266628
let secp_ctx = Secp256k1::signing_only();

lightning/src/util/test_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use bitcoin::network::constants::Network;
3636
use bitcoin::hash_types::{BlockHash, Txid};
3737

3838
use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1, ecdsa::Signature};
39+
use bitcoin::secp256k1::ecdh::SharedSecret;
3940
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
4041

4142
use regex;
@@ -74,6 +75,7 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
7475
type Signer = EnforcingSigner;
7576

7677
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { unreachable!(); }
78+
fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> { unreachable!(); }
7779
fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!(); }
7880
fn get_destination_script(&self) -> Script { unreachable!(); }
7981
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); }
@@ -599,6 +601,9 @@ impl keysinterface::KeysInterface for TestKeysInterface {
599601
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
600602
self.backing.get_node_secret(recipient)
601603
}
604+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> {
605+
self.backing.ecdh(recipient, other_key, tweak)
606+
}
602607
fn get_inbound_payment_key_material(&self) -> keysinterface::KeyMaterial {
603608
self.backing.get_inbound_payment_key_material()
604609
}

0 commit comments

Comments
 (0)