Skip to content

Commit 615419d

Browse files
committed
Drop byte_utils in favor of native to/from_be_bytes methods
Now that our MSRV supports the native methods, we have no need for the helpers anymore. Because LLVM was already matching our byte_utils methods as byteswap functions, this should have no impact on generated (optimzied) code. This removes most of the byte_utils usage, though some remains to keep the patch size reasonable.
1 parent 08ecb53 commit 615419d

File tree

7 files changed

+65
-119
lines changed

7 files changed

+65
-119
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use ln::{PaymentHash, PaymentSecret};
1111
use ln::channelmanager::HTLCSource;
1212
use ln::msgs;
1313
use routing::router::RouteHop;
14-
use util::byte_utils;
1514
use util::chacha20::ChaCha20;
1615
use util::errors::{self, APIError};
1716
use util::ser::{Readable, Writeable, LengthCalculatingWriter};
@@ -29,6 +28,7 @@ use bitcoin::secp256k1;
2928

3029
use prelude::*;
3130
use std::io::Cursor;
31+
use core::convert::TryInto;
3232
use core::ops::Deref;
3333

3434
pub(super) struct OnionKeys {
@@ -367,7 +367,7 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
367367
const NODE: u16 = 0x2000;
368368
const UPDATE: u16 = 0x1000;
369369

370-
let error_code = byte_utils::slice_to_be16(&error_code_slice);
370+
let error_code = u16::from_be_bytes(error_code_slice.try_into().expect("len is 2"));
371371
error_code_ret = Some(error_code);
372372
error_packet_ret = Some(err_packet.failuremsg[2..].to_vec());
373373

@@ -394,7 +394,7 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
394394
}
395395
else if error_code & UPDATE == UPDATE {
396396
if let Some(update_len_slice) = err_packet.failuremsg.get(debug_field_size+2..debug_field_size+4) {
397-
let update_len = byte_utils::slice_to_be16(&update_len_slice) as usize;
397+
let update_len = u16::from_be_bytes(update_len_slice.try_into().expect("len is 2")) as usize;
398398
if let Some(update_slice) = err_packet.failuremsg.get(debug_field_size + 4..debug_field_size + 4 + update_len) {
399399
if let Ok(chan_update) = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice)) {
400400
// if channel_update should NOT have caused the failure:

lightning/src/ln/peer_channel_encryptor.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
2121
use bitcoin::secp256k1;
2222

2323
use util::chacha20poly1305rfc::ChaCha20Poly1305RFC;
24-
use util::byte_utils;
2524
use bitcoin::hashes::hex::ToHex;
2625

2726
/// Maximum Lightning message data length according to
@@ -141,7 +140,7 @@ impl PeerChannelEncryptor {
141140
#[inline]
142141
fn encrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], plaintext: &[u8]) {
143142
let mut nonce = [0; 12];
144-
nonce[4..].copy_from_slice(&byte_utils::le64_to_array(n));
143+
nonce[4..].copy_from_slice(&n.to_le_bytes()[..]);
145144

146145
let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
147146
let mut tag = [0; 16];
@@ -152,7 +151,7 @@ impl PeerChannelEncryptor {
152151
#[inline]
153152
fn decrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], cyphertext: &[u8]) -> Result<(), LightningError> {
154153
let mut nonce = [0; 12];
155-
nonce[4..].copy_from_slice(&byte_utils::le64_to_array(n));
154+
nonce[4..].copy_from_slice(&n.to_le_bytes()[..]);
156155

157156
let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
158157
if !chacha.decrypt(&cyphertext[0..cyphertext.len() - 16], res, &cyphertext[cyphertext.len() - 16..]) {
@@ -406,7 +405,7 @@ impl PeerChannelEncryptor {
406405
*sn = 0;
407406
}
408407

409-
Self::encrypt_with_ad(&mut res[0..16+2], *sn, sk, &[0; 0], &byte_utils::be16_to_array(msg.len() as u16));
408+
Self::encrypt_with_ad(&mut res[0..16+2], *sn, sk, &[0; 0], &(msg.len() as u16).to_be_bytes());
410409
*sn += 1;
411410

412411
Self::encrypt_with_ad(&mut res[16+2..], *sn, sk, &[0; 0], msg);
@@ -435,7 +434,7 @@ impl PeerChannelEncryptor {
435434
let mut res = [0; 2];
436435
Self::decrypt_with_ad(&mut res, *rn, rk, &[0; 0], msg)?;
437436
*rn += 1;
438-
Ok(byte_utils::slice_to_be16(&res))
437+
Ok(u16::from_be_bytes(res))
439438
},
440439
_ => panic!("Tried to decrypt a message prior to noise handshake completion"),
441440
}

lightning/src/ln/wire.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ impl Encode for msgs::GossipTimestampFilter {
350350
#[cfg(test)]
351351
mod tests {
352352
use super::*;
353-
use util::byte_utils;
354353
use prelude::*;
354+
use core::convert::TryInto;
355355

356356
// Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
357357
const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
@@ -397,7 +397,7 @@ mod tests {
397397

398398
#[test]
399399
fn read_unknown_message() {
400-
let buffer = &byte_utils::be16_to_array(::core::u16::MAX);
400+
let buffer = &::core::u16::MAX.to_be_bytes();
401401
let mut reader = ::std::io::Cursor::new(buffer);
402402
let message = read(&mut reader).unwrap();
403403
match message {
@@ -414,7 +414,7 @@ mod tests {
414414

415415
let type_length = ::core::mem::size_of::<u16>();
416416
let (type_bytes, payload_bytes) = buffer.split_at(type_length);
417-
assert_eq!(byte_utils::slice_to_be16(type_bytes), msgs::Pong::TYPE);
417+
assert_eq!(u16::from_be_bytes(type_bytes.try_into().unwrap()), msgs::Pong::TYPE);
418418
assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]);
419419
}
420420

lightning/src/util/byte_utils.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
#[inline]
11-
pub fn slice_to_be16(v: &[u8]) -> u16 {
12-
((v[0] as u16) << 8*1) |
13-
((v[1] as u16) << 8*0)
14-
}
15-
#[inline]
16-
pub fn slice_to_be32(v: &[u8]) -> u32 {
17-
((v[0] as u32) << 8*3) |
18-
((v[1] as u32) << 8*2) |
19-
((v[2] as u32) << 8*1) |
20-
((v[3] as u32) << 8*0)
21-
}
22-
#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
23-
#[inline]
24-
pub fn slice_to_le32(v: &[u8]) -> u32 {
25-
((v[0] as u32) << 8*0) |
26-
((v[1] as u32) << 8*1) |
27-
((v[2] as u32) << 8*2) |
28-
((v[3] as u32) << 8*3)
29-
}
3010
#[inline]
3111
pub fn slice_to_be48(v: &[u8]) -> u64 {
3212
((v[0] as u64) << 8*5) |
@@ -64,16 +44,6 @@ pub fn be32_to_array(u: u32) -> [u8; 4] {
6444
v[3] = ((u >> 8*0) & 0xff) as u8;
6545
v
6646
}
67-
#[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
68-
#[inline]
69-
pub fn le32_to_array(u: u32) -> [u8; 4] {
70-
let mut v = [0; 4];
71-
v[0] = ((u >> 8*0) & 0xff) as u8;
72-
v[1] = ((u >> 8*1) & 0xff) as u8;
73-
v[2] = ((u >> 8*2) & 0xff) as u8;
74-
v[3] = ((u >> 8*3) & 0xff) as u8;
75-
v
76-
}
7747
#[inline]
7848
pub fn be48_to_array(u: u64) -> [u8; 6] {
7949
assert!(u & 0xffff_0000_0000_0000 == 0);
@@ -120,14 +90,10 @@ mod tests {
12090

12191
#[test]
12292
fn test_all() {
123-
assert_eq!(slice_to_be16(&[0xde, 0xad]), 0xdead);
124-
assert_eq!(slice_to_be32(&[0xde, 0xad, 0xbe, 0xef]), 0xdeadbeef);
125-
assert_eq!(slice_to_le32(&[0xef, 0xbe, 0xad, 0xde]), 0xdeadbeef);
12693
assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad);
12794
assert_eq!(slice_to_be64(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]), 0xdeadbeef1bad1dea);
12895
assert_eq!(be16_to_array(0xdead), [0xde, 0xad]);
12996
assert_eq!(be32_to_array(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]);
130-
assert_eq!(le32_to_array(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]);
13197
assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]);
13298
assert_eq!(be64_to_array(0xdeadbeef1bad1dea), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]);
13399
assert_eq!(le64_to_array(0xdeadbeef1bad1dea), [0xea, 0x1d, 0xad, 0x1b, 0xef, 0xbe, 0xad, 0xde]);

lightning/src/util/chacha20.rs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::io;
1414
#[cfg(not(feature = "fuzztarget"))]
1515
mod real_chacha {
1616
use core::cmp;
17-
use util::byte_utils::{slice_to_le32, le32_to_array};
17+
use core::convert::TryInto;
1818

1919
#[derive(Clone, Copy, PartialEq, Eq)]
2020
#[allow(non_camel_case_types)]
@@ -55,6 +55,17 @@ mod real_chacha {
5555
u32x4(self.0 << rhs.0, self.1 << rhs.1, self.2 << rhs.2, self.3 << rhs.3)
5656
}
5757
}
58+
impl u32x4 {
59+
fn from_bytes(bytes: &[u8]) -> Self {
60+
assert_eq!(bytes.len(), 4*4);
61+
Self (
62+
u32::from_le_bytes(bytes[0*4..1*4].try_into().expect("len is 4")),
63+
u32::from_le_bytes(bytes[1*4..2*4].try_into().expect("len is 4")),
64+
u32::from_le_bytes(bytes[2*4..3*4].try_into().expect("len is 4")),
65+
u32::from_le_bytes(bytes[3*4..4*4].try_into().expect("len is 4")),
66+
)
67+
}
68+
}
5869

5970
const BLOCK_SIZE: usize = 64;
6071

@@ -99,7 +110,7 @@ mod real_chacha {
99110
d1,d2,d3,d4
100111
];
101112
for i in 0..lens.len() {
102-
$output[i*4..(i+1)*4].copy_from_slice(&le32_to_array(lens[i]));
113+
$output[i*4..(i+1)*4].copy_from_slice(&lens[i].to_le_bytes());
103114
}
104115
}}
105116
}
@@ -147,54 +158,23 @@ mod real_chacha {
147158
_ => unreachable!(),
148159
};
149160
ChaChaState {
150-
a: u32x4(
151-
slice_to_le32(&constant[0..4]),
152-
slice_to_le32(&constant[4..8]),
153-
slice_to_le32(&constant[8..12]),
154-
slice_to_le32(&constant[12..16])
155-
),
156-
b: u32x4(
157-
slice_to_le32(&key[0..4]),
158-
slice_to_le32(&key[4..8]),
159-
slice_to_le32(&key[8..12]),
160-
slice_to_le32(&key[12..16])
161-
),
161+
a: u32x4::from_bytes(&constant[0..16]),
162+
b: u32x4::from_bytes(&key[0..16]),
162163
c: if key.len() == 16 {
163-
u32x4(
164-
slice_to_le32(&key[0..4]),
165-
slice_to_le32(&key[4..8]),
166-
slice_to_le32(&key[8..12]),
167-
slice_to_le32(&key[12..16])
168-
)
164+
u32x4::from_bytes(&key[0..16])
169165
} else {
170-
u32x4(
171-
slice_to_le32(&key[16..20]),
172-
slice_to_le32(&key[20..24]),
173-
slice_to_le32(&key[24..28]),
174-
slice_to_le32(&key[28..32])
175-
)
166+
u32x4::from_bytes(&key[16..32])
176167
},
177168
d: if nonce.len() == 16 {
178-
u32x4(
179-
slice_to_le32(&nonce[0..4]),
180-
slice_to_le32(&nonce[4..8]),
181-
slice_to_le32(&nonce[8..12]),
182-
slice_to_le32(&nonce[12..16])
183-
)
169+
u32x4::from_bytes(&nonce[0..16])
184170
} else if nonce.len() == 12 {
185-
u32x4(
186-
0,
187-
slice_to_le32(&nonce[0..4]),
188-
slice_to_le32(&nonce[4..8]),
189-
slice_to_le32(&nonce[8..12])
190-
)
171+
let mut nonce4 = [0; 4*4];
172+
nonce4[4..].copy_from_slice(nonce);
173+
u32x4::from_bytes(&nonce4)
191174
} else {
192-
u32x4(
193-
0,
194-
0,
195-
slice_to_le32(&nonce[0..4]),
196-
slice_to_le32(&nonce[4..8])
197-
)
175+
let mut nonce4 = [0; 4*4];
176+
nonce4[8..].copy_from_slice(nonce);
177+
u32x4::from_bytes(&nonce4)
198178
}
199179
}
200180
}

lightning/src/util/poly1305.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// https://github.com/floodyberry/poly1305-donna
99

1010
use core::cmp::min;
11-
use util::byte_utils::{slice_to_le32, le32_to_array};
11+
use core::convert::TryInto;
1212

1313
#[derive(Clone, Copy)]
1414
pub struct Poly1305 {
@@ -26,16 +26,16 @@ impl Poly1305 {
2626
let mut poly = Poly1305{ r: [0u32; 5], h: [0u32; 5], pad: [0u32; 4], leftover: 0, buffer: [0u8; 16], finalized: false };
2727

2828
// r &= 0xffffffc0ffffffc0ffffffc0fffffff
29-
poly.r[0] = (slice_to_le32(&key[0..4]) ) & 0x3ffffff;
30-
poly.r[1] = (slice_to_le32(&key[3..7]) >> 2) & 0x3ffff03;
31-
poly.r[2] = (slice_to_le32(&key[6..10]) >> 4) & 0x3ffc0ff;
32-
poly.r[3] = (slice_to_le32(&key[9..13]) >> 6) & 0x3f03fff;
33-
poly.r[4] = (slice_to_le32(&key[12..16]) >> 8) & 0x00fffff;
29+
poly.r[0] = (u32::from_le_bytes(key[ 0.. 4].try_into().expect("len is 4")) ) & 0x3ffffff;
30+
poly.r[1] = (u32::from_le_bytes(key[ 3.. 7].try_into().expect("len is 4")) >> 2) & 0x3ffff03;
31+
poly.r[2] = (u32::from_le_bytes(key[ 6..10].try_into().expect("len is 4")) >> 4) & 0x3ffc0ff;
32+
poly.r[3] = (u32::from_le_bytes(key[ 9..13].try_into().expect("len is 4")) >> 6) & 0x3f03fff;
33+
poly.r[4] = (u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;
3434

35-
poly.pad[0] = slice_to_le32(&key[16..20]);
36-
poly.pad[1] = slice_to_le32(&key[20..24]);
37-
poly.pad[2] = slice_to_le32(&key[24..28]);
38-
poly.pad[3] = slice_to_le32(&key[28..32]);
35+
poly.pad[0] = u32::from_le_bytes(key[16..20].try_into().expect("len is 4"));
36+
poly.pad[1] = u32::from_le_bytes(key[20..24].try_into().expect("len is 4"));
37+
poly.pad[2] = u32::from_le_bytes(key[24..28].try_into().expect("len is 4"));
38+
poly.pad[3] = u32::from_le_bytes(key[28..32].try_into().expect("len is 4"));
3939

4040
poly
4141
}
@@ -61,11 +61,11 @@ impl Poly1305 {
6161
let mut h4 = self.h[4];
6262

6363
// h += m
64-
h0 += (slice_to_le32(&m[0..4]) ) & 0x3ffffff;
65-
h1 += (slice_to_le32(&m[3..7]) >> 2) & 0x3ffffff;
66-
h2 += (slice_to_le32(&m[6..10]) >> 4) & 0x3ffffff;
67-
h3 += (slice_to_le32(&m[9..13]) >> 6) & 0x3ffffff;
68-
h4 += (slice_to_le32(&m[12..16]) >> 8) | hibit;
64+
h0 += (u32::from_le_bytes(m[ 0.. 4].try_into().expect("len is 4")) ) & 0x3ffffff;
65+
h1 += (u32::from_le_bytes(m[ 3.. 7].try_into().expect("len is 4")) >> 2) & 0x3ffffff;
66+
h2 += (u32::from_le_bytes(m[ 6..10].try_into().expect("len is 4")) >> 4) & 0x3ffffff;
67+
h3 += (u32::from_le_bytes(m[ 9..13].try_into().expect("len is 4")) >> 6) & 0x3ffffff;
68+
h4 += (u32::from_le_bytes(m[12..16].try_into().expect("len is 4")) >> 8) | hibit;
6969

7070
// h *= r
7171
let d0 = (h0 as u64 * r0 as u64) + (h1 as u64 * s4 as u64) + (h2 as u64 * s3 as u64) + (h3 as u64 * s2 as u64) + (h4 as u64 * s1 as u64);
@@ -196,10 +196,10 @@ impl Poly1305 {
196196
if !self.finalized{
197197
self.finish();
198198
}
199-
output[0..4].copy_from_slice(&le32_to_array(self.h[0]));
200-
output[4..8].copy_from_slice(&le32_to_array(self.h[1]));
201-
output[8..12].copy_from_slice(&le32_to_array(self.h[2]));
202-
output[12..16].copy_from_slice(&le32_to_array(self.h[3]));
199+
output[0..4].copy_from_slice(&self.h[0].to_le_bytes());
200+
output[4..8].copy_from_slice(&self.h[1].to_le_bytes());
201+
output[8..12].copy_from_slice(&self.h[2].to_le_bytes());
202+
output[12..16].copy_from_slice(&self.h[3].to_le_bytes());
203203
}
204204
}
205205

0 commit comments

Comments
 (0)