Skip to content

Commit

Permalink
ChaCha20 Polyfill: Remove ChunksFixedMut.
Browse files Browse the repository at this point in the history
It is only used in one place so it isn't justified.
  • Loading branch information
briansmith committed Oct 13, 2023
1 parent f8cad22 commit 75c620a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/aead/chacha/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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::<u32>())
.zip(x.iter())
.for_each(|(output, &x)| output.copy_from_slice(&x.to_le_bytes()));
}

#[inline(always)]
Expand Down
33 changes: 0 additions & 33 deletions src/polyfill/chunks_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &'a mut Chunk>;

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 ) => {
Expand All @@ -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())
}
}
};
}

Expand All @@ -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);

0 comments on commit 75c620a

Please sign in to comment.