From 34cc371f5f47a4ce180bd81a3d7ecc3b53a3c67a Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 13 Jun 2025 17:41:19 +0200 Subject: don't allocate in pad_to_blocks This saves around 6% on my machine. --- src/lib.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 5a2e205..908cc1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -579,16 +579,13 @@ fn cipher_aez_core(mode: Mode, aez: &Aez, tweaks: Tweak, message: &mut [u8]) { blocks.set_m_y(c_y); } -fn pad_to_blocks(value: &[u8]) -> Vec { - let mut blocks = Vec::new(); - for chunk in value.chunks(16) { - if chunk.len() == 16 { - blocks.push(Block::from_slice(chunk)); +fn pad_to_blocks(value: &[u8]) -> impl Iterator { + value.chunks(16) + .map(|chunk| if chunk.len() == 16 { + Block::from_slice(chunk) } else { - blocks.push(Block::from_slice(chunk).pad(chunk.len() * 8)); - } - } - blocks + Block::from_slice(chunk).pad(chunk.len() * 8) + }) } fn aez_hash(aez: &Aez, tweaks: Tweak) -> Block { @@ -609,12 +606,12 @@ fn aez_hash(aez: &Aez, tweaks: Tweak) -> Block { } } else { let blocks = pad_to_blocks(tweak); - for (l, chunk) in blocks.iter().enumerate() { + for (l, chunk) in blocks.enumerate() { ej.advance(); - if l == blocks.len() - 1 { - hash = hash ^ e(j.try_into().unwrap(), 0, aez, *chunk); + if l == tweak.len() / 16 { + hash = hash ^ e(j.try_into().unwrap(), 0, aez, chunk); } else { - hash = hash ^ ej.eval(*chunk); + hash = hash ^ ej.eval(chunk); } } } -- cgit v1.2.3