From d50fca63d26e92b39213b0bd3791dba5c03b951b Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 25 Feb 2023 16:36:54 -0700 Subject: [PATCH] ascon: eliminate `util` module (#38) Factors the `u8` <-> `u64` conversions to be inline --- ascon/src/lib.rs | 14 +++++++++----- ascon/src/util.rs | 16 ---------------- 2 files changed, 9 insertions(+), 21 deletions(-) delete mode 100644 ascon/src/util.rs diff --git a/ascon/src/lib.rs b/ascon/src/lib.rs index 3d59bec..4833db7 100644 --- a/ascon/src/lib.rs +++ b/ascon/src/lib.rs @@ -41,9 +41,7 @@ extern crate alloc; #[cfg(feature = "alloc")] pub mod aead; -mod util; - -use crate::util::{u64_to_u8, u8_to_u64}; +use core::convert::TryInto; /// Key length. const KEY_LEN: usize = 16; @@ -64,7 +62,11 @@ const B: usize = 8; pub fn permutation(s: &mut [u8], start: usize, rounds: usize) { let mut x = [0; 5]; let mut t = [0; 5]; - u8_to_u64(s, &mut x); + + #[allow(clippy::unwrap_used)] + for (i, b) in x.iter_mut().enumerate() { + *b = u64::from_be_bytes(s[(i * 8)..((i + 1) * 8)].try_into().unwrap()); + } for i in start as u64..(start + rounds) as u64 { x[2] ^= ((0xfu64 - i) << 4) | i; @@ -104,7 +106,9 @@ pub fn permutation(s: &mut [u8], start: usize, rounds: usize) { x[4] ^= x[4].rotate_right(7) ^ x[4].rotate_right(41); } - u64_to_u8(&x, s); + for (i, &b) in x.iter().enumerate() { + s[(i * 8)..((i + 1) * 8)].copy_from_slice(&b.to_be_bytes()) + } } /// Initialize Ascon permutation. diff --git a/ascon/src/util.rs b/ascon/src/util.rs deleted file mode 100644 index d400a61..0000000 --- a/ascon/src/util.rs +++ /dev/null @@ -1,16 +0,0 @@ -use core::convert::TryInto; - -#[inline] -#[allow(clippy::unwrap_used)] -pub fn u8_to_u64(input: &[u8], output: &mut [u64]) { - for (i, b) in output.iter_mut().enumerate() { - *b = u64::from_be_bytes(input[(i * 8)..((i + 1) * 8)].try_into().unwrap()); - } -} - -#[inline] -pub fn u64_to_u8(input: &[u64], output: &mut [u8]) { - for (i, &b) in input.iter().enumerate() { - output[(i * 8)..((i + 1) * 8)].copy_from_slice(&b.to_be_bytes()) - } -}