@@ -822,11 +822,8 @@ pub struct InMemorySigner {
822
822
channel_value_satoshis : u64 ,
823
823
/// Key derivation parameters.
824
824
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 ,
830
827
}
831
828
832
829
impl PartialEq for InMemorySigner {
@@ -857,8 +854,7 @@ impl Clone for InMemorySigner {
857
854
channel_parameters : self . channel_parameters . clone ( ) ,
858
855
channel_value_satoshis : self . channel_value_satoshis ,
859
856
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 ( ) ) ,
862
858
}
863
859
}
864
860
}
@@ -892,8 +888,7 @@ impl InMemorySigner {
892
888
holder_channel_pubkeys,
893
889
channel_parameters : None ,
894
890
channel_keys_id,
895
- rand_bytes_unique_start,
896
- rand_bytes_index : AtomicCounter :: new ( ) ,
891
+ entropy_source : RandomBytes :: new ( rand_bytes_unique_start) ,
897
892
}
898
893
}
899
894
@@ -1069,10 +1064,7 @@ impl InMemorySigner {
1069
1064
1070
1065
impl EntropySource for InMemorySigner {
1071
1066
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 ( )
1076
1068
}
1077
1069
}
1078
1070
@@ -1350,8 +1342,7 @@ impl<ES: Deref> ReadableArgs<ES> for InMemorySigner where ES::Target: EntropySou
1350
1342
holder_channel_pubkeys,
1351
1343
channel_parameters : counterparty_channel_data,
1352
1344
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 ( ) ) ,
1355
1346
} )
1356
1347
}
1357
1348
}
@@ -1379,8 +1370,7 @@ pub struct KeysManager {
1379
1370
channel_master_key : ExtendedPrivKey ,
1380
1371
channel_child_index : AtomicUsize ,
1381
1372
1382
- rand_bytes_unique_start : [ u8 ; 32 ] ,
1383
- rand_bytes_index : AtomicCounter ,
1373
+ entropy_source : RandomBytes ,
1384
1374
1385
1375
seed : [ u8 ; 32 ] ,
1386
1376
starting_time_secs : u64 ,
@@ -1449,8 +1439,7 @@ impl KeysManager {
1449
1439
channel_master_key,
1450
1440
channel_child_index : AtomicUsize :: new ( 0 ) ,
1451
1441
1452
- rand_bytes_unique_start,
1453
- rand_bytes_index : AtomicCounter :: new ( ) ,
1442
+ entropy_source : RandomBytes :: new ( rand_bytes_unique_start) ,
1454
1443
1455
1444
seed : * seed,
1456
1445
starting_time_secs,
@@ -1631,10 +1620,7 @@ impl KeysManager {
1631
1620
1632
1621
impl EntropySource for KeysManager {
1633
1622
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 ( )
1638
1624
}
1639
1625
}
1640
1626
@@ -1888,6 +1874,35 @@ impl PhantomKeysManager {
1888
1874
}
1889
1875
}
1890
1876
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
+
1891
1906
// Ensure that EcdsaChannelSigner can have a vtable
1892
1907
#[ test]
1893
1908
pub fn dyn_sign ( ) {
0 commit comments