Skip to content

Commit 17fda75

Browse files
committed
reduce vector allocations for message encryption
1 parent 19b7700 commit 17fda75

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lightning/src/ln/peers/conduit.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use ln::peers::{chacha, hkdf};
44
use util::byte_utils;
55

6-
/// Returned after a successful handshake to encrypt and decrypt communication with peer nodes
6+
/// Returned after a successful handshake to encrypt and decrypt communication with peer nodes.
7+
/// It should not normally be manually instantiated.
78
/// Automatically handles key rotation.
89
/// For decryption, it is recommended to call `decrypt_message_stream` for automatic buffering.
910
pub struct Conduit {
@@ -25,14 +26,14 @@ impl Conduit {
2526
let length = buffer.len() as u16;
2627
let length_bytes = byte_utils::be16_to_array(length);
2728

28-
let encrypted_length = chacha::encrypt(&self.sending_key, self.sending_nonce as u64, &[0; 0], &length_bytes);
29+
let mut ciphertext = vec![0u8; 18 + length as usize + 16];
30+
31+
ciphertext[0..18].copy_from_slice(&chacha::encrypt(&self.sending_key, self.sending_nonce as u64, &[0; 0], &length_bytes));
2932
self.increment_sending_nonce();
3033

31-
let encrypted_message = chacha::encrypt(&self.sending_key, self.sending_nonce as u64, &[0; 0], buffer);
34+
ciphertext[18..].copy_from_slice(&chacha::encrypt(&self.sending_key, self.sending_nonce as u64, &[0; 0], buffer));
3235
self.increment_sending_nonce();
3336

34-
let mut ciphertext = encrypted_length;
35-
ciphertext.extend_from_slice(&encrypted_message);
3637
ciphertext
3738
}
3839

0 commit comments

Comments
 (0)