@@ -46,7 +46,6 @@ use crate::ln::script::ShutdownScript;
46
46
use crate :: prelude:: * ;
47
47
use core:: convert:: TryInto ;
48
48
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
49
- use crate :: sync:: Mutex ;
50
49
use crate :: io:: { self , Error } ;
51
50
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
52
51
use crate :: util:: chacha20:: ChaCha20 ;
@@ -980,7 +979,8 @@ pub struct KeysManager {
980
979
channel_master_key : ExtendedPrivKey ,
981
980
channel_child_index : AtomicUsize ,
982
981
983
- chacha : Mutex < ChaCha20 > ,
982
+ rand_bytes_unique_start : [ u8 ; 32 ] ,
983
+ rand_bytes_index : AtomicUsize ,
984
984
985
985
seed : [ u8 ; 32 ] ,
986
986
starting_time_secs : u64 ,
@@ -1030,10 +1030,9 @@ impl KeysManager {
1030
1030
let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
1031
1031
inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
1032
1032
1033
- let mut nonce = [ 0u8 ; 12 ] ;
1034
- nonce[ ..8 ] . clone_from_slice ( & starting_time_secs. to_be_bytes ( ) ) ;
1035
- nonce[ 8 ..12 ] . clone_from_slice ( & starting_time_nanos. to_be_bytes ( ) ) ;
1036
- let chacha = Mutex :: new ( ChaCha20 :: new ( seed, & nonce) ) ;
1033
+ let mut rand_bytes_unique_start = [ 0u8 ; 32 ] ;
1034
+ rand_bytes_unique_start[ ..8 ] . clone_from_slice ( & starting_time_secs. to_be_bytes ( ) ) ;
1035
+ rand_bytes_unique_start[ 8 ..12 ] . clone_from_slice ( & starting_time_nanos. to_be_bytes ( ) ) ;
1037
1036
1038
1037
let mut res = KeysManager {
1039
1038
secp_ctx,
@@ -1047,7 +1046,8 @@ impl KeysManager {
1047
1046
channel_master_key,
1048
1047
channel_child_index : AtomicUsize :: new ( 0 ) ,
1049
1048
1050
- chacha,
1049
+ rand_bytes_unique_start,
1050
+ rand_bytes_index : AtomicUsize :: new ( 0 ) ,
1051
1051
1052
1052
seed : * seed,
1053
1053
starting_time_secs,
@@ -1244,11 +1244,10 @@ impl KeysManager {
1244
1244
1245
1245
impl EntropySource for KeysManager {
1246
1246
fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
1247
- let mut chacha = self . chacha . lock ( ) . unwrap ( ) ;
1248
-
1249
- let mut random_bytes = [ 0 ; 32 ] ;
1250
- chacha. process_in_place ( & mut random_bytes) ;
1251
- random_bytes
1247
+ let index = self . rand_bytes_index . fetch_add ( 1 , Ordering :: AcqRel ) ;
1248
+ let mut nonce = [ 0u8 ; 16 ] ;
1249
+ nonce[ ..8 ] . clone_from_slice ( & index. to_be_bytes ( ) ) ;
1250
+ ChaCha20 :: get_single_block ( & self . rand_bytes_unique_start , & nonce)
1252
1251
}
1253
1252
}
1254
1253
0 commit comments