Skip to content

Commit e4efd65

Browse files
committed
refactor: Remove unnecessary copy_from_slice calls in states.rs
Use the slice refs from the act array instead.
1 parent 5bac3ae commit e4efd65

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

lightning/src/ln/peers/handshake/states.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,12 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
297297
let chaining_key = self.chaining_key;
298298

299299
// 1. Read exactly 66 bytes from the network buffer
300-
let mut act_three_bytes = [0u8; ACT_THREE_LENGTH];
301-
act_three_bytes.copy_from_slice(&read_buffer[..ACT_THREE_LENGTH]);
302-
read_buffer.drain(..ACT_THREE_LENGTH);
300+
let act_three_bytes: Vec<u8> = read_buffer.drain(..ACT_THREE_LENGTH).collect();
303301

304302
// 2. Parse the read message (m) into v, c, and t
305303
let version = act_three_bytes[0];
306-
307-
let mut tagged_encrypted_pubkey = [0u8; 49];
308-
tagged_encrypted_pubkey.copy_from_slice(&act_three_bytes[1..50]);
309-
310-
let mut chacha_tag = [0u8; 16];
311-
chacha_tag.copy_from_slice(&act_three_bytes[50..66]);
304+
let tagged_encrypted_pubkey = &act_three_bytes[1..50];
305+
let chacha_tag = &act_three_bytes[50..66];
312306

313307
// 3. If v is an unrecognized handshake version, then the responder MUST abort the connection attempt.
314308
if version != 0 {
@@ -318,9 +312,7 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
318312

319313
// 4. rs = decryptWithAD(temp_k2, 1, h, c)
320314
let remote_pubkey_vec = chacha::decrypt(&temporary_key, 1, &hash, &tagged_encrypted_pubkey)?;
321-
let mut initiator_pubkey_bytes = [0u8; 33];
322-
initiator_pubkey_bytes.copy_from_slice(remote_pubkey_vec.as_slice());
323-
let initiator_pubkey = if let Ok(public_key) = PublicKey::from_slice(&initiator_pubkey_bytes) {
315+
let initiator_pubkey = if let Ok(public_key) = PublicKey::from_slice(remote_pubkey_vec.as_slice()) {
324316
public_key
325317
} else {
326318
return Err("invalid remote public key".to_string());
@@ -419,24 +411,19 @@ fn process_act_message(read_buffer: &mut Vec<u8>, local_private_key: &SecretKey,
419411
return Err("need at least 50 bytes".to_string());
420412
}
421413

422-
let mut act_bytes = [0u8; ACT_ONE_TWO_LENGTH];
423-
act_bytes.copy_from_slice(&read_buffer[..ACT_ONE_TWO_LENGTH]);
424-
read_buffer.drain(..ACT_ONE_TWO_LENGTH);
414+
let act_bytes: Vec<u8> = read_buffer.drain(..ACT_ONE_TWO_LENGTH).collect();
425415

426416
// 2.Parse the read message (m) into v, re, and c
427417
let version = act_bytes[0];
418+
let ephemeral_public_key_bytes = &act_bytes[1..34];
419+
let chacha_tag = &act_bytes[34..50];
428420

429-
let mut ephemeral_public_key_bytes = [0u8; 33];
430-
ephemeral_public_key_bytes.copy_from_slice(&act_bytes[1..34]);
431421
let ephemeral_public_key = if let Ok(public_key) = PublicKey::from_slice(&ephemeral_public_key_bytes) {
432422
public_key
433423
} else {
434424
return Err("invalid remote ephemeral public key".to_string());
435425
};
436426

437-
let mut chacha_tag = [0u8; 16];
438-
chacha_tag.copy_from_slice(&act_bytes[34..50]);
439-
440427
// 3. If v is an unrecognized handshake version, then the responder MUST abort the connection attempt
441428
if version != 0 {
442429
// this should not crash the process, hence no panic

0 commit comments

Comments
 (0)