diff options
author | Daniel Schadt <kingdread@gmx.de> | 2025-06-13 17:41:19 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2025-06-13 17:41:19 +0200 |
commit | 34cc371f5f47a4ce180bd81a3d7ecc3b53a3c67a (patch) | |
tree | 94d4a4539db725bd98e1a4ada72ba6fa7975ffc8 /src/lib.rs | |
parent | 4bf0986d8c7a19c0b0eb7d875817fa8981c4d32e (diff) | |
download | zears-34cc371f5f47a4ce180bd81a3d7ecc3b53a3c67a.tar.gz zears-34cc371f5f47a4ce180bd81a3d7ecc3b53a3c67a.tar.bz2 zears-34cc371f5f47a4ce180bd81a3d7ecc3b53a3c67a.zip |
don't allocate in pad_to_blocks
This saves around 6% on my machine.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 23 |
1 files changed, 10 insertions, 13 deletions
@@ -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<Block> { - 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<Item=Block> { + 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); } } } |