Skip to content

Commit 659653c

Browse files
committed
DRY up EntropySource implementation
The ChaCha20-based EntropySource implementation is duplicated within the sign module. Refactor those into a RandomBytes implementation so that it may be reused both there. Also useful as a standalone EntropySource implementation for tests where an independent EntropySource is needed to ensure that backwards-compatibility testing is not broken.
1 parent c51c049 commit 659653c

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

lightning/src/sign/mod.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,8 @@ pub struct InMemorySigner {
822822
channel_value_satoshis: u64,
823823
/// Key derivation parameters.
824824
channel_keys_id: [u8; 32],
825-
/// Seed from which all randomness produced is derived from.
826-
rand_bytes_unique_start: [u8; 32],
827-
/// Tracks the number of times we've produced randomness to ensure we don't return the same
828-
/// bytes twice.
829-
rand_bytes_index: AtomicCounter,
825+
/// A source of random bytes.
826+
entropy_source: RandomBytes,
830827
}
831828

832829
impl PartialEq for InMemorySigner {
@@ -857,8 +854,7 @@ impl Clone for InMemorySigner {
857854
channel_parameters: self.channel_parameters.clone(),
858855
channel_value_satoshis: self.channel_value_satoshis,
859856
channel_keys_id: self.channel_keys_id,
860-
rand_bytes_unique_start: self.get_secure_random_bytes(),
861-
rand_bytes_index: AtomicCounter::new(),
857+
entropy_source: RandomBytes::new(self.get_secure_random_bytes()),
862858
}
863859
}
864860
}
@@ -892,8 +888,7 @@ impl InMemorySigner {
892888
holder_channel_pubkeys,
893889
channel_parameters: None,
894890
channel_keys_id,
895-
rand_bytes_unique_start,
896-
rand_bytes_index: AtomicCounter::new(),
891+
entropy_source: RandomBytes::new(rand_bytes_unique_start),
897892
}
898893
}
899894

@@ -1069,10 +1064,7 @@ impl InMemorySigner {
10691064

10701065
impl EntropySource for InMemorySigner {
10711066
fn get_secure_random_bytes(&self) -> [u8; 32] {
1072-
let index = self.rand_bytes_index.get_increment();
1073-
let mut nonce = [0u8; 16];
1074-
nonce[..8].copy_from_slice(&index.to_be_bytes());
1075-
ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce)
1067+
self.entropy_source.get_secure_random_bytes()
10761068
}
10771069
}
10781070

@@ -1350,8 +1342,7 @@ impl<ES: Deref> ReadableArgs<ES> for InMemorySigner where ES::Target: EntropySou
13501342
holder_channel_pubkeys,
13511343
channel_parameters: counterparty_channel_data,
13521344
channel_keys_id: keys_id,
1353-
rand_bytes_unique_start: entropy_source.get_secure_random_bytes(),
1354-
rand_bytes_index: AtomicCounter::new(),
1345+
entropy_source: RandomBytes::new(entropy_source.get_secure_random_bytes()),
13551346
})
13561347
}
13571348
}
@@ -1379,8 +1370,7 @@ pub struct KeysManager {
13791370
channel_master_key: ExtendedPrivKey,
13801371
channel_child_index: AtomicUsize,
13811372

1382-
rand_bytes_unique_start: [u8; 32],
1383-
rand_bytes_index: AtomicCounter,
1373+
entropy_source: RandomBytes,
13841374

13851375
seed: [u8; 32],
13861376
starting_time_secs: u64,
@@ -1449,8 +1439,7 @@ impl KeysManager {
14491439
channel_master_key,
14501440
channel_child_index: AtomicUsize::new(0),
14511441

1452-
rand_bytes_unique_start,
1453-
rand_bytes_index: AtomicCounter::new(),
1442+
entropy_source: RandomBytes::new(rand_bytes_unique_start),
14541443

14551444
seed: *seed,
14561445
starting_time_secs,
@@ -1631,10 +1620,7 @@ impl KeysManager {
16311620

16321621
impl EntropySource for KeysManager {
16331622
fn get_secure_random_bytes(&self) -> [u8; 32] {
1634-
let index = self.rand_bytes_index.get_increment();
1635-
let mut nonce = [0u8; 16];
1636-
nonce[..8].copy_from_slice(&index.to_be_bytes());
1637-
ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce)
1623+
self.entropy_source.get_secure_random_bytes()
16381624
}
16391625
}
16401626

@@ -1888,6 +1874,35 @@ impl PhantomKeysManager {
18881874
}
18891875
}
18901876

1877+
/// An implementation of [`EntropySource`] using [`ChaCha20`].
1878+
#[derive(Debug)]
1879+
struct RandomBytes {
1880+
/// Seed from which all randomness produced is derived from.
1881+
seed: [u8; 32],
1882+
/// Tracks the number of times we've produced randomness to ensure we don't return the same
1883+
/// bytes twice.
1884+
index: AtomicCounter,
1885+
}
1886+
1887+
impl RandomBytes {
1888+
/// Creates a new instance using the given seed.
1889+
pub fn new(seed: [u8; 32]) -> Self {
1890+
Self {
1891+
seed,
1892+
index: AtomicCounter::new(),
1893+
}
1894+
}
1895+
}
1896+
1897+
impl EntropySource for RandomBytes {
1898+
fn get_secure_random_bytes(&self) -> [u8; 32] {
1899+
let index = self.index.get_increment();
1900+
let mut nonce = [0u8; 16];
1901+
nonce[..8].copy_from_slice(&index.to_be_bytes());
1902+
ChaCha20::get_single_block(&self.seed, &nonce)
1903+
}
1904+
}
1905+
18911906
// Ensure that EcdsaChannelSigner can have a vtable
18921907
#[test]
18931908
pub fn dyn_sign() {

0 commit comments

Comments
 (0)