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

Commit 180c6fb

Browse files
committed
Merge #173: Use stdlib integer/slice conversion methods
6f9c0dd Use chunks_exact (Tobin C. Harding) 99032cb Use stdlib byte conversion methods (Tobin C. Harding) 198f15f Add crate to fully qualify path (Tobin C. Harding) Pull request description: Now that we have Rust > 1.32 we can use methods on stdlib integers to convert to and from little and big endian slices. - Patch 1: preparatory cleanup - Patch 2: do the work - Patch 3: optimisation, use `chunks_exact` ACKs for top commit: apoelstra: ACK 6f9c0dd Tree-SHA512: 115bf480bd85c8fd12a11dc4d95f8b6e0056d5f03123910cd496b98d0042340ef5ebdbec325be81e1e6eca897458b282ee703ecbbbdcb8589a758aeaa82e6bfb
2 parents 561efa4 + 6f9c0dd commit 180c6fb

File tree

6 files changed

+38
-112
lines changed

6 files changed

+38
-112
lines changed

src/ripemd160.rs

Lines changed: 8 additions & 7 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

@@ -52,8 +53,8 @@ impl crate::HashEngine for HashEngine {
5253
#[cfg(not(fuzzing))]
5354
fn midstate(&self) -> [u8; 20] {
5455
let mut ret = [0; 20];
55-
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));
56+
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_exact_mut(4)) {
57+
ret_bytes.copy_from_slice(&(*val).to_le_bytes());
5758
}
5859
ret
5960
}
@@ -79,7 +80,7 @@ impl crate::HashEngine for HashEngine {
7980
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8081
#[repr(transparent)]
8182
pub struct Hash(
82-
#[cfg_attr(feature = "schemars", schemars(schema_with = "util::json_hex_string::len_20"))]
83+
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_20"))]
8384
[u8; 20]
8485
);
8586

@@ -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())
@@ -268,8 +269,8 @@ impl HashEngine {
268269
debug_assert_eq!(self.buffer.len(), BLOCK_SIZE);
269270

270271
let mut w = [0u32; 16];
271-
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(4)) {
272-
*w_val = util::slice_to_u32_le(buff_bytes);
272+
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks_exact(4)) {
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: 8 additions & 7 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

@@ -47,8 +48,8 @@ impl crate::HashEngine for HashEngine {
4748
#[cfg(not(fuzzing))]
4849
fn midstate(&self) -> [u8; 20] {
4950
let mut ret = [0; 20];
50-
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));
51+
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_exact_mut(4)) {
52+
ret_bytes.copy_from_slice(&val.to_be_bytes())
5253
}
5354
ret
5455
}
@@ -74,7 +75,7 @@ impl crate::HashEngine for HashEngine {
7475
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7576
#[repr(transparent)]
7677
pub struct Hash(
77-
#[cfg_attr(feature = "schemars", schemars(schema_with = "util::json_hex_string::len_20"))]
78+
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_20"))]
7879
[u8; 20]
7980
);
8081

@@ -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())
@@ -156,8 +157,8 @@ impl HashEngine {
156157
debug_assert_eq!(self.buffer.len(), BLOCK_SIZE);
157158

158159
let mut w = [0u32; 80];
159-
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(4)) {
160-
*w_val = util::slice_to_u32_be(buff_bytes);
160+
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks_exact(4)) {
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: 10 additions & 9 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

@@ -47,8 +48,8 @@ impl crate::HashEngine for HashEngine {
4748
#[cfg(not(fuzzing))]
4849
fn midstate(&self) -> Midstate {
4950
let mut ret = [0; 32];
50-
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));
51+
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_exact_mut(4)) {
52+
ret_bytes.copy_from_slice(&val.to_be_bytes());
5253
}
5354
Midstate(ret)
5455
}
@@ -74,7 +75,7 @@ impl crate::HashEngine for HashEngine {
7475
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7576
#[repr(transparent)]
7677
pub struct Hash(
77-
#[cfg_attr(feature = "schemars", schemars(schema_with = "util::json_hex_string::len_32"))]
78+
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_32"))]
7879
[u8; 32]
7980
);
8081

@@ -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())
@@ -258,8 +259,8 @@ impl HashEngine {
258259
assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size");
259260

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

265266
HashEngine {
@@ -274,8 +275,8 @@ impl HashEngine {
274275
debug_assert_eq!(self.buffer.len(), BLOCK_SIZE);
275276

276277
let mut w = [0u32; 16];
277-
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(4)) {
278-
*w_val = util::slice_to_u32_be(buff_bytes);
278+
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks_exact(4)) {
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: 8 additions & 7 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

@@ -55,8 +56,8 @@ impl crate::HashEngine for HashEngine {
5556
#[cfg(not(fuzzing))]
5657
fn midstate(&self) -> [u8; 64] {
5758
let mut ret = [0; 64];
58-
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));
59+
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_exact_mut(8)) {
60+
ret_bytes.copy_from_slice(&val.to_be_bytes());
6061
}
6162
ret
6263
}
@@ -81,7 +82,7 @@ impl crate::HashEngine for HashEngine {
8182
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8283
#[repr(transparent)]
8384
pub struct Hash(
84-
#[cfg_attr(feature = "schemars", schemars(schema_with = "util::json_hex_string::len_64"))]
85+
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_64"))]
8586
[u8; 64]
8687
);
8788

@@ -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())
@@ -236,8 +237,8 @@ impl HashEngine {
236237
debug_assert_eq!(self.buffer.len(), BLOCK_SIZE);
237238

238239
let mut w = [0u64; 16];
239-
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks(8)) {
240-
*w_val = util::slice_to_u64_be(buff_bytes);
240+
for (w_val, buff_bytes) in w.iter_mut().zip(self.buffer.chunks_exact(8)) {
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: 4 additions & 4 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) => {{
@@ -199,7 +199,7 @@ impl crate::HashEngine for HashEngine {
199199
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
200200
#[repr(transparent)]
201201
pub struct Hash(
202-
#[cfg_attr(feature = "schemars", schemars(schema_with = "util::json_hex_string::len_8"))]
202+
#[cfg_attr(feature = "schemars", schemars(schema_with = "crate::util::json_hex_string::len_8"))]
203203
[u8; 8]
204204
);
205205

@@ -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)