Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 99032cb

Browse files
committed
Use stdlib byte conversion methods
Now that we have Rust > 1.32 we can use methods on stdlib integers to convert to and from little and big endian slices.
1 parent 198f15f commit 99032cb

File tree

6 files changed

+24
-98
lines changed

6 files changed

+24
-98
lines changed

src/ripemd160.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
//!
2222
2323
use core::{cmp, str};
24+
use core::convert::TryInto;
2425
use core::ops::Index;
2526
use core::slice::SliceIndex;
2627

27-
use crate::{Error, HashEngine as _, hex, util};
28+
use crate::{Error, HashEngine as _, hex};
2829

2930
const BLOCK_SIZE: usize = 64;
3031

@@ -53,7 +54,7 @@ impl crate::HashEngine for HashEngine {
5354
fn midstate(&self) -> [u8; 20] {
5455
let mut ret = [0; 20];
5556
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(4)) {
56-
ret_bytes.copy_from_slice(&util::u32_to_array_le(*val));
57+
ret_bytes.copy_from_slice(&(*val).to_le_bytes());
5758
}
5859
ret
5960
}
@@ -121,7 +122,7 @@ impl crate::Hash for Hash {
121122
e.input(&zeroes[..pad_length]);
122123
debug_assert_eq!(e.length % BLOCK_SIZE, zeroes.len());
123124

124-
e.input(&util::u64_to_array_le(8 * data_len));
125+
e.input(&(8 * data_len).to_le_bytes());
125126
debug_assert_eq!(e.length % BLOCK_SIZE, 0);
126127

127128
Hash(e.midstate())
@@ -269,7 +270,7 @@ impl HashEngine {
269270

270271
let mut w = [0u32; 16];
271272
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(4)) {
272-
*w_val = util::slice_to_u32_le(buff_bytes);
273+
*w_val = u32::from_le_bytes(buff_bytes.try_into().expect("4 byte slice"))
273274
}
274275

275276
process_block!(self.h, w,

src/sha1.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
//!
1717
1818
use core::{cmp, str};
19+
use core::convert::TryInto;
1920
use core::ops::Index;
2021
use core::slice::SliceIndex;
2122

22-
use crate::{Error, HashEngine as _, hex, util};
23+
use crate::{Error, HashEngine as _, hex};
2324

2425
const BLOCK_SIZE: usize = 64;
2526

@@ -48,7 +49,7 @@ impl crate::HashEngine for HashEngine {
4849
fn midstate(&self) -> [u8; 20] {
4950
let mut ret = [0; 20];
5051
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(4)) {
51-
ret_bytes.copy_from_slice(&util::u32_to_array_be(*val));
52+
ret_bytes.copy_from_slice(&val.to_be_bytes())
5253
}
5354
ret
5455
}
@@ -115,7 +116,7 @@ impl crate::Hash for Hash {
115116
e.input(&zeroes[..pad_length]);
116117
debug_assert_eq!(e.length % BLOCK_SIZE, zeroes.len());
117118

118-
e.input(&util::u64_to_array_be(8 * data_len));
119+
e.input(&(8 * data_len).to_be_bytes());
119120
debug_assert_eq!(e.length % BLOCK_SIZE, 0);
120121

121122
Hash(e.midstate())
@@ -157,7 +158,7 @@ impl HashEngine {
157158

158159
let mut w = [0u32; 80];
159160
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(4)) {
160-
*w_val = util::slice_to_u32_be(buff_bytes);
161+
*w_val = u32::from_be_bytes(buff_bytes.try_into().expect("4 bytes slice"))
161162
}
162163
for i in 16..80 {
163164
w[i] =(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]).rotate_left(1);

src/sha256.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
//!
1717
1818
use core::{cmp, str};
19+
use core::convert::TryInto;
1920
use core::ops::Index;
2021
use core::slice::SliceIndex;
2122

22-
use crate::{Error, HashEngine as _, hex, util};
23+
use crate::{Error, HashEngine as _, hex};
2324

2425
const BLOCK_SIZE: usize = 64;
2526

@@ -48,7 +49,7 @@ impl crate::HashEngine for HashEngine {
4849
fn midstate(&self) -> Midstate {
4950
let mut ret = [0; 32];
5051
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(4)) {
51-
ret_bytes.copy_from_slice(&util::u32_to_array_be(*val));
52+
ret_bytes.copy_from_slice(&val.to_be_bytes());
5253
}
5354
Midstate(ret)
5455
}
@@ -116,7 +117,7 @@ impl crate::Hash for Hash {
116117
e.input(&zeroes[..pad_length]);
117118
debug_assert_eq!(e.length % BLOCK_SIZE, zeroes.len());
118119

119-
e.input(&util::u64_to_array_be(8 * data_len));
120+
e.input(&(8 * data_len).to_be_bytes());
120121
debug_assert_eq!(e.length % BLOCK_SIZE, 0);
121122

122123
Hash(e.midstate().into_inner())
@@ -259,7 +260,7 @@ impl HashEngine {
259260

260261
let mut ret = [0; 8];
261262
for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate[..].chunks(4)) {
262-
*ret_val = util::slice_to_u32_be(midstate_bytes);
263+
*ret_val = u32::from_be_bytes(midstate_bytes.try_into().expect("4 byte slice"));
263264
}
264265

265266
HashEngine {
@@ -275,7 +276,7 @@ impl HashEngine {
275276

276277
let mut w = [0u32; 16];
277278
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(4)) {
278-
*w_val = util::slice_to_u32_be(buff_bytes);
279+
*w_val = u32::from_be_bytes(buff_bytes.try_into().expect("4 byte slice"));
279280
}
280281

281282
let mut a = self.h[0];

src/sha512.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
//!
2222
2323
use core::{cmp, hash, str};
24+
use core::convert::TryInto;
2425
use core::ops::Index;
2526
use core::slice::SliceIndex;
2627

27-
use crate::{Error, HashEngine as _, hex, util};
28+
use crate::{Error, HashEngine as _, hex};
2829

2930
const BLOCK_SIZE: usize = 128;
3031

@@ -56,7 +57,7 @@ impl crate::HashEngine for HashEngine {
5657
fn midstate(&self) -> [u8; 64] {
5758
let mut ret = [0; 64];
5859
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(8)) {
59-
ret_bytes.copy_from_slice(&util::u64_to_array_be(*val));
60+
ret_bytes.copy_from_slice(&val.to_be_bytes());
6061
}
6162
ret
6263
}
@@ -166,7 +167,7 @@ impl crate::Hash for Hash {
166167
debug_assert_eq!(e.length % BLOCK_SIZE, zeroes.len());
167168

168169
e.input(&[0; 8]);
169-
e.input(&util::u64_to_array_be(8 * data_len));
170+
e.input(&(8 * data_len).to_be_bytes());
170171
debug_assert_eq!(e.length % BLOCK_SIZE, 0);
171172

172173
Hash(e.midstate())
@@ -237,7 +238,7 @@ impl HashEngine {
237238

238239
let mut w = [0u64; 16];
239240
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(8)) {
240-
*w_val = util::slice_to_u64_be(buff_bytes);
241+
*w_val = u64::from_be_bytes(buff_bytes.try_into().expect("8 byte slice"));
241242
}
242243

243244
let mut a = self.h[0];

src/siphash24.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::{cmp, mem, ptr, str};
2424
use core::ops::Index;
2525
use core::slice::SliceIndex;
2626

27-
use crate::{Error, Hash as _, HashEngine as _, hex, util};
27+
use crate::{Error, Hash as _, HashEngine as _, hex};
2828

2929
macro_rules! compress {
3030
($state:expr) => {{
@@ -257,12 +257,12 @@ impl Hash {
257257

258258
/// Returns the (little endian) 64-bit integer representation of the hash value.
259259
pub fn as_u64(&self) -> u64 {
260-
util::slice_to_u64_le(&self.0[..])
260+
u64::from_le_bytes(self.0)
261261
}
262262

263263
/// Creates a hash from its (little endian) 64-bit integer representation.
264264
pub fn from_u64(hash: u64) -> Hash {
265-
Hash(util::u64_to_array_le(hash))
265+
Hash(hash.to_le_bytes())
266266
}
267267
}
268268

src/util.rs

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -109,69 +109,6 @@ macro_rules! engine_input_impl(
109109

110110

111111

112-
macro_rules! define_slice_to_be {
113-
($name: ident, $type: ty) => {
114-
#[inline]
115-
pub fn $name(slice: &[u8]) -> $type {
116-
assert_eq!(slice.len(), core::mem::size_of::<$type>());
117-
let mut res = 0;
118-
for i in 0..::core::mem::size_of::<$type>() {
119-
res |= (slice[i] as $type) << (::core::mem::size_of::<$type>() - i - 1)*8;
120-
}
121-
res
122-
}
123-
}
124-
}
125-
macro_rules! define_slice_to_le {
126-
($name: ident, $type: ty) => {
127-
#[inline]
128-
pub fn $name(slice: &[u8]) -> $type {
129-
assert_eq!(slice.len(), core::mem::size_of::<$type>());
130-
let mut res = 0;
131-
for i in 0..::core::mem::size_of::<$type>() {
132-
res |= (slice[i] as $type) << i*8;
133-
}
134-
res
135-
}
136-
}
137-
}
138-
macro_rules! define_be_to_array {
139-
($name: ident, $type: ty, $byte_len: expr) => {
140-
#[inline]
141-
pub fn $name(val: $type) -> [u8; $byte_len] {
142-
assert_eq!(::core::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
143-
let mut res = [0; $byte_len];
144-
for i in 0..$byte_len {
145-
res[i] = ((val >> ($byte_len - i - 1)*8) & 0xff) as u8;
146-
}
147-
res
148-
}
149-
}
150-
}
151-
macro_rules! define_le_to_array {
152-
($name: ident, $type: ty, $byte_len: expr) => {
153-
#[inline]
154-
pub fn $name(val: $type) -> [u8; $byte_len] {
155-
assert_eq!(::core::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
156-
let mut res = [0; $byte_len];
157-
for i in 0..$byte_len {
158-
res[i] = ((val >> i*8) & 0xff) as u8;
159-
}
160-
res
161-
}
162-
}
163-
}
164-
165-
define_slice_to_be!(slice_to_u32_be, u32);
166-
define_slice_to_be!(slice_to_u64_be, u64);
167-
define_be_to_array!(u32_to_array_be, u32, 4);
168-
define_be_to_array!(u64_to_array_be, u64, 8);
169-
170-
define_slice_to_le!(slice_to_u32_le, u32);
171-
define_slice_to_le!(slice_to_u64_le, u64);
172-
define_le_to_array!(u32_to_array_le, u32, 4);
173-
define_le_to_array!(u64_to_array_le, u64, 8);
174-
175112
/// Creates a new newtype around a [`Hash`] type.
176113
#[macro_export]
177114
macro_rules! hash_newtype {
@@ -302,28 +239,13 @@ pub mod json_hex_string {
302239
mod test {
303240
use crate::{Hash, sha256};
304241

305-
use super::*;
306-
307242
#[test]
308243
fn borrow_slice_impl_to_vec() {
309244
// Test that the borrow_slice_impl macro gives to_vec.
310245
let hash = sha256::Hash::hash(&[3, 50]);
311246
assert_eq!(hash.to_vec().len(), sha256::Hash::LEN);
312247
}
313248

314-
#[test]
315-
fn endianness_test() {
316-
assert_eq!(slice_to_u32_be(&[0xde, 0xad, 0xbe, 0xef]), 0xdeadbeef);
317-
assert_eq!(slice_to_u64_be(&[0x1b, 0xad, 0xca, 0xfe, 0xde, 0xad, 0xbe, 0xef]), 0x1badcafedeadbeef);
318-
assert_eq!(u32_to_array_be(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]);
319-
assert_eq!(u64_to_array_be(0x1badcafedeadbeef), [0x1b, 0xad, 0xca, 0xfe, 0xde, 0xad, 0xbe, 0xef]);
320-
321-
assert_eq!(slice_to_u32_le(&[0xef, 0xbe, 0xad, 0xde]), 0xdeadbeef);
322-
assert_eq!(slice_to_u64_le(&[0xef, 0xbe, 0xad, 0xde, 0xfe, 0xca, 0xad, 0x1b]), 0x1badcafedeadbeef);
323-
assert_eq!(u32_to_array_le(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]);
324-
assert_eq!(u64_to_array_le(0x1badcafedeadbeef), [0xef, 0xbe, 0xad, 0xde, 0xfe, 0xca, 0xad, 0x1b]);
325-
}
326-
327249
hash_newtype!(TestHash, crate::sha256d::Hash, 32, doc="Test hash.");
328250

329251
#[test]

0 commit comments

Comments
 (0)