From 75c620a5f12d44d06ba28e43b41008b129431efb Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 12 Oct 2023 22:53:31 -0700 Subject: [PATCH] ChaCha20 Polyfill: Remove `ChunksFixedMut`. It is only used in one place so it isn't justified. --- src/aead/chacha/fallback.rs | 8 ++++---- src/polyfill/chunks_fixed.rs | 33 --------------------------------- 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/src/aead/chacha/fallback.rs b/src/aead/chacha/fallback.rs index c7f83c899f..a90959a8ac 100644 --- a/src/aead/chacha/fallback.rs +++ b/src/aead/chacha/fallback.rs @@ -16,7 +16,6 @@ // Adapted from the BoringSSL crypto/chacha/chacha.c. use super::{Counter, Key, BLOCK_LEN}; -use crate::polyfill::ChunksFixedMut; use core::ops::RangeFrom; pub(super) fn ChaCha20_ctr32( @@ -82,9 +81,10 @@ fn chacha_core(output: &mut [u8; BLOCK_LEN], input: &State) { *x = x.wrapping_add(*input); } - for (output, &x) in ChunksFixedMut::<[u8; 4]>::chunks_fixed_mut(output).zip(x.iter()) { - *output = u32::to_le_bytes(x) - } + output + .chunks_exact_mut(core::mem::size_of::()) + .zip(x.iter()) + .for_each(|(output, &x)| output.copy_from_slice(&x.to_le_bytes())); } #[inline(always)] diff --git a/src/polyfill/chunks_fixed.rs b/src/polyfill/chunks_fixed.rs index 9b95105e5a..114cc91c12 100644 --- a/src/polyfill/chunks_fixed.rs +++ b/src/polyfill/chunks_fixed.rs @@ -6,23 +6,6 @@ where fn chunks_fixed(self) -> Chunks; } -/// Allows iterating over a mutable array in fixed-length chunks. -/// -/// The design of this is different than that for `ChunksFixed` because it -/// isn't clear that we can legally (according to Rust's rules) convert create -/// a mutable reference to the chunked type from a mutable reference. -/// -/// TODO: Get clarification on the rules and refactor this tp be more like -/// `ChunksFixed`. -pub trait ChunksFixedMut<'a, Chunk> -where - Chunk: 'a, -{ - type MutIterator: Iterator; - - fn chunks_fixed_mut(self) -> Self::MutIterator; -} - /// `$unchuncked_len` must be divisible by `$chunk_len`. macro_rules! define_chunks_fixed { ( $unchuncked_len:expr, $chunk_len:expr ) => { @@ -40,21 +23,6 @@ macro_rules! define_chunks_fixed { unsafe { &*as_ptr } } } - - impl<'a, T> ChunksFixedMut<'a, [T; $chunk_len]> for &'a mut [T; $unchuncked_len] { - type MutIterator = core::iter::Map< - core::slice::ChunksExactMut<'a, T>, - fn(&'a mut [T]) -> &'a mut [T; $chunk_len], - >; - - #[inline(always)] - fn chunks_fixed_mut(self) -> Self::MutIterator { - // There will be no remainder because `$unchuncked_len` must be divisible by - // `$chunk_len`. The `unwrap()` will not fail for the same reason. - self.chunks_exact_mut($chunk_len) - .map(|slice| slice.try_into().unwrap()) - } - } }; } @@ -63,6 +31,5 @@ define_chunks_fixed!(12, 4); define_chunks_fixed!(16, 4); define_chunks_fixed!(16, 8); define_chunks_fixed!(32, 4); -define_chunks_fixed!(64, 4); define_chunks_fixed!(64, 32); define_chunks_fixed!(80, 20);