From 0380ba18ad23ac300565f5a2462793fdd45b6998 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Mon, 1 Oct 2018 15:23:43 -0400 Subject: [PATCH] remove the dependency on byteorder Since that crate takes slices in its API, it tends to introduce unnecessary bounds checks. --- Cargo.toml | 1 - src/lib.rs | 25 ++++++++++++++++++------- src/portable.rs | 23 +++++++++++++++++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 76d14ca..d058b66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ readme = "README.md" [dependencies] arrayref = "0.3.5" arrayvec = { version = "0.4.7", default-features = false, features = ["use_union"] } -byteorder = { version = "1.2.4", default-features = false } constant_time_eq = "0.1.3" rayon = { version = "1.0.2", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 8ca49c1..48a06ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,7 @@ //! throughput of 1.8 cycles per byte. #![cfg_attr(not(feature = "std"), no_std)] +#![feature(int_to_from_bytes)] #[cfg(feature = "std")] extern crate core; @@ -109,11 +110,9 @@ extern crate core; #[macro_use] extern crate arrayref; extern crate arrayvec; -extern crate byteorder; extern crate constant_time_eq; use arrayvec::ArrayString; -use byteorder::{ByteOrder, LittleEndian}; use core::cmp; use core::fmt; @@ -372,6 +371,8 @@ impl State { } fn with_params(params: &Params) -> Self { + let salt_refs = array_refs!(¶ms.salt, 8, 8); + let personal_refs = array_refs!(¶ms.personal, 8, 8); let mut state = Self { h: [ IV[0] @@ -383,10 +384,10 @@ impl State { IV[1] ^ params.node_offset, IV[2] ^ params.node_depth as u64 ^ (params.inner_hash_length as u64) << 8, IV[3], - IV[4] ^ LittleEndian::read_u64(¶ms.salt[..8]), - IV[5] ^ LittleEndian::read_u64(¶ms.salt[8..]), - IV[6] ^ LittleEndian::read_u64(¶ms.personal[..8]), - IV[7] ^ LittleEndian::read_u64(¶ms.personal[8..]), + IV[4] ^ u64::from_le_bytes(*salt_refs.0), + IV[5] ^ u64::from_le_bytes(*salt_refs.1), + IV[6] ^ u64::from_le_bytes(*personal_refs.0), + IV[7] ^ u64::from_le_bytes(*personal_refs.1), ], compress_fn: default_compress_impl(), buf: [0; BLOCKBYTES], @@ -459,7 +460,17 @@ impl State { bytes: [0; OUTBYTES], len: self.hash_length, }; - LittleEndian::write_u64_into(&h_copy, &mut hash.bytes); + { + let bytes_refs = mut_array_refs!(&mut hash.bytes, 8, 8, 8, 8, 8, 8, 8, 8); + *bytes_refs.0 = h_copy[0].to_le_bytes(); + *bytes_refs.1 = h_copy[1].to_le_bytes(); + *bytes_refs.2 = h_copy[2].to_le_bytes(); + *bytes_refs.3 = h_copy[3].to_le_bytes(); + *bytes_refs.4 = h_copy[4].to_le_bytes(); + *bytes_refs.5 = h_copy[5].to_le_bytes(); + *bytes_refs.6 = h_copy[6].to_le_bytes(); + *bytes_refs.7 = h_copy[7].to_le_bytes(); + } hash } diff --git a/src/portable.rs b/src/portable.rs index 971f464..78193ec 100644 --- a/src/portable.rs +++ b/src/portable.rs @@ -1,5 +1,3 @@ -use byteorder::{ByteOrder, LittleEndian}; - use Block; use StateWords; use IV; @@ -81,8 +79,25 @@ pub fn compress(h: &mut StateWords, msg: &Block, count: u128, lastblock: u64, la ]; // Parse the message bytes as ints in little endian order. - let mut m = [0; 16]; - LittleEndian::read_u64_into(msg, &mut m); + let msg_refs = array_refs!(msg, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8); + let m = [ + u64::from_le_bytes(*msg_refs.0), + u64::from_le_bytes(*msg_refs.1), + u64::from_le_bytes(*msg_refs.2), + u64::from_le_bytes(*msg_refs.3), + u64::from_le_bytes(*msg_refs.4), + u64::from_le_bytes(*msg_refs.5), + u64::from_le_bytes(*msg_refs.6), + u64::from_le_bytes(*msg_refs.7), + u64::from_le_bytes(*msg_refs.8), + u64::from_le_bytes(*msg_refs.9), + u64::from_le_bytes(*msg_refs.10), + u64::from_le_bytes(*msg_refs.11), + u64::from_le_bytes(*msg_refs.12), + u64::from_le_bytes(*msg_refs.13), + u64::from_le_bytes(*msg_refs.14), + u64::from_le_bytes(*msg_refs.15), + ]; round(0, &m, &mut v); round(1, &m, &mut v);