Skip to content

Commit

Permalink
cipher: stream cipher improvements (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Nov 12, 2023
1 parent 2b4f648 commit 6e7ee07
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 168 deletions.
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

0 comments on commit 6e7ee07

Please sign in to comment.