#[cfg(feature = "std")]
use std::io::{Read, Result as IoResult};
use crate::{
backend::{ActiveBackend, DigestBackend},
hash256,
state::BLOCK_LEN,
};
#[inline(always)]
fn apply_slice<const XOR: bool>(dst: &mut [u8], src: &[u8]) {
debug_assert_eq!(dst.len(), src.len());
if XOR {
let (dst_chunk_li, dst_tail) = dst.as_chunks_mut::<8>();
let (src_chunk_li, src_tail) = src.as_chunks::<8>();
for (d, s) in dst_chunk_li.iter_mut().zip(src_chunk_li.iter()) {
let v = u64::from_ne_bytes(*d) ^ u64::from_ne_bytes(*s);
*d = v.to_ne_bytes();
}
for (d, s) in dst_tail.iter_mut().zip(src_tail.iter()) {
*d ^= *s;
}
} else {
dst.copy_from_slice(src);
}
}
#[inline(always)]
pub(crate) fn stream_range<const XOR: bool, B: DigestBackend>(
root: &[u8; 32],
pos: &mut u64,
mut buf: &mut [u8],
backend: B,
) {
if buf.is_empty() {
return;
}
let cur = *pos;
if cur == 0 && buf.len() >= 32 {
let (head, rest) = buf.split_at_mut(32);
apply_slice::<XOR>(head, root);
*pos = 32;
buf = rest;
} else if cur < 32 {
let head = cur as usize;
let take = (32 - head).min(buf.len());
let (dst, rest) = buf.split_at_mut(take);
apply_slice::<XOR>(dst, &root[head..head + take]);
*pos = cur.wrapping_add(take as u64);
buf = rest;
if buf.is_empty() {
return;
}
}
let rel = *pos - 32;
let mut counter = rel / BLOCK_LEN as u64;
let head_off = (rel % BLOCK_LEN as u64) as usize;
if head_off != 0 {
let mut blk_li = [0u8; BLOCK_LEN];
backend.ctr::<false>(root, counter, &mut blk_li);
let take = (BLOCK_LEN - head_off).min(buf.len());
let (dst, rest) = buf.split_at_mut(take);
apply_slice::<XOR>(dst, &blk_li[head_off..head_off + take]);
*pos = (*pos).wrapping_add(take as u64);
counter = counter.wrapping_add(1);
buf = rest;
}
let full_len = buf.len() & !(BLOCK_LEN - 1);
if full_len > 0 {
let (full, rest) = buf.split_at_mut(full_len);
backend.ctr::<XOR>(root, counter, full);
counter = counter.wrapping_add((full_len / BLOCK_LEN) as u64);
*pos = (*pos).wrapping_add(full_len as u64);
buf = rest;
}
if !buf.is_empty() {
let mut blk_li = [0u8; BLOCK_LEN];
backend.ctr::<false>(root, counter, &mut blk_li);
apply_slice::<XOR>(buf, &blk_li[..buf.len()]);
*pos = (*pos).wrapping_add(buf.len() as u64);
}
}
#[inline(always)]
pub(crate) fn fill_xof_root<B: DigestBackend>(root: [u8; 32], out: &mut [u8], backend: B) {
stream_range::<false, _>(&root, &mut 0, out, backend);
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct StreamCore {
pub(crate) root: [u8; 32],
pub(crate) pos: u64,
}
impl StreamCore {
#[inline(always)]
pub(crate) const fn from_root(root: [u8; 32]) -> Self {
Self { root, pos: 0 }
}
#[inline(always)]
pub(crate) fn fill_with<B: DigestBackend>(&mut self, backend: B, out: &mut [u8]) {
stream_range::<false, _>(&self.root, &mut self.pos, out, backend);
}
#[inline(always)]
pub(crate) fn xor_with<B: DigestBackend>(&mut self, backend: B, buf: &mut [u8]) {
stream_range::<true, _>(&self.root, &mut self.pos, buf, backend);
}
}
macro_rules! impl_stream_api {
($ty:ty, $core_field:tt) => {
impl $ty {
#[must_use]
#[inline]
pub const fn root(&self) -> &[u8; 32] {
&self.$core_field.root
}
#[must_use]
#[inline]
pub const fn position(&self) -> u64 {
self.$core_field.pos
}
#[inline]
pub fn seek(&mut self, pos: u64) -> &mut Self {
self.$core_field.pos = pos;
self
}
#[inline]
pub fn fill(&mut self, out: &mut [u8]) {
let backend = self.backend();
self.$core_field.fill_with(backend, out);
}
#[inline]
pub fn xor_keystream(&mut self, buf: &mut [u8]) {
let backend = self.backend();
self.$core_field.xor_with(backend, buf);
}
}
#[cfg(feature = "std")]
impl Read for $ty {
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
self.fill(buf);
Ok(buf.len())
}
}
};
}
#[cfg(feature = "cpu")]
pub(crate) use impl_stream_api;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stream(StreamCore);
impl Stream {
#[must_use]
#[inline]
pub fn new(data: &[u8]) -> Self {
Self::from_root(hash256(data))
}
#[must_use]
#[inline]
pub const fn from_root(root: [u8; 32]) -> Self {
Self(StreamCore::from_root(root))
}
#[inline(always)]
const fn backend(&self) -> ActiveBackend {
ActiveBackend
}
}
impl_stream_api!(Stream, 0);