Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cipher: stream cipher improvements #1388

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions cipher/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ macro_rules! impl_seek_num {
{$($t:ty )*} => {
$(
impl SeekNum for $t {
fn from_block_byte<T: Counter>(block: T, byte: u8, bs: u8) -> Result<Self, OverflowError> {
debug_assert!(byte < bs);
let mut block: Self = block.try_into().map_err(|_| OverflowError)?;
if byte != 0 {
block -= 1;
}
let pos = block.checked_mul(bs as Self).ok_or(OverflowError)? + (byte as Self);
Ok(pos)
fn from_block_byte<T: Counter>(block: T, byte: u8, block_size: u8) -> Result<Self, OverflowError> {
debug_assert!(byte != 0);
let rem = block_size.checked_sub(byte).ok_or(OverflowError)?;
let block: Self = block.try_into().map_err(|_| OverflowError)?;
block
.checked_mul(block_size.into())
.and_then(|v| v.checked_sub(rem.into()))
.ok_or(OverflowError)
}

fn into_block_byte<T: Counter>(self, bs: u8) -> Result<(T, u8), OverflowError> {
let bs = bs as Self;
let byte = self % bs;
let block = T::try_from(self/bs).map_err(|_| OverflowError)?;
Ok((block, byte as u8))
fn into_block_byte<T: Counter>(self, block_size: u8) -> Result<(T, u8), OverflowError> {
let bs: Self = block_size.into();
let byte = (self % bs) as u8;
let block = T::try_from(self / bs).map_err(|_| OverflowError)?;
Ok((block, byte))
}
}
)*
Expand Down
25 changes: 2 additions & 23 deletions cipher/src/stream_core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{ParBlocks, ParBlocksSizeUser, StreamCipherError};
use crypto_common::{
array::{Array, ArraySize},
array::{slice_as_chunks_mut, Array},
typenum::Unsigned,
Block, BlockSizeUser, BlockSizes,
};
Expand Down Expand Up @@ -190,27 +190,6 @@ macro_rules! impl_counter {

impl_counter! { u32 u64 u128 }

/// Partition buffer into 2 parts: buffer of arrays and tail.
///
/// In case if `N` is less or equal to 1, buffer of arrays has length
/// of zero and tail is equal to `self`.
#[inline]
fn into_chunks<T, N: ArraySize>(buf: &mut [T]) -> (&mut [Array<T, N>], &mut [T]) {
use core::slice;
if N::USIZE <= 1 {
return (&mut [], buf);
}
let chunks_len = buf.len() / N::USIZE;
let tail_pos = N::USIZE * chunks_len;
let tail_len = buf.len() - tail_pos;
unsafe {
let ptr = buf.as_mut_ptr();
let chunks = slice::from_raw_parts_mut(ptr as *mut Array<T, N>, chunks_len);
let tail = slice::from_raw_parts_mut(ptr.add(tail_pos), tail_len);
(chunks, tail)
}
}

struct WriteBlockCtx<'a, BS: BlockSizes> {
block: &'a mut Block<Self>,
}
Expand All @@ -234,7 +213,7 @@ impl<'a, BS: BlockSizes> StreamClosure for WriteBlocksCtx<'a, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
let (chunks, tail) = into_chunks::<_, B::ParBlocksSize>(self.blocks);
let (chunks, tail) = slice_as_chunks_mut(self.blocks);
for chunk in chunks {
backend.gen_par_ks_blocks(chunk);
}
Expand Down
Loading