use core::array::from_fn;
use fearless_simd::{Simd, SimdBase, u8x64};
use super::sbox::{SimdSbox4, VpermOp, dispatch_sbox, splat_x4};
use crate::{
arch::soft::{Soft128, word_to_state},
backend::ctr::{CtrKernel, split_root},
const_fn::M_CTR,
state::State,
};
#[inline(always)]
const fn pair_to_state(lo: u64, hi: u64) -> State {
((lo as u128) | ((hi as u128) << 64)).to_le_bytes()
}
#[inline(always)]
pub(super) fn ctr_soft<const XOR: bool>(root: &[u8; 32], blk: u64, buf: &mut [u8]) {
unsafe { Soft128.ctr::<XOR>(root, blk, buf) };
}
struct CtrOp<'a, const XOR: bool> {
root: &'a [u8; 32],
blk: u64,
buf: &'a mut [u8],
}
impl<S: Simd, const XOR: bool> VpermOp<S> for CtrOp<'_, XOR> {
type Out = ();
#[inline(always)]
fn run<E: SimdSbox4<S>>(self, simd: S) {
let sbox = E::new(simd);
let [rk0_x4, rk1_x4, rk2_x4, rk3_x4] =
unsafe { split_root(Soft128, self.root) }.map(|w| splat_x4(simd, word_to_state(w)));
let mut blk = self.blk;
let (chunk_li, rest) = self.buf.as_chunks_mut::<64>();
for chunk in chunk_li {
let c_li: [State; 4] = from_fn(|i| {
let c = blk.wrapping_add(i as u64);
pair_to_state(c, c.wrapping_mul(M_CTR))
});
let x = sbox.aes_round(u8x64::from_slice(simd, c_li.as_flattened()) ^ rk0_x4) ^ rk1_x4;
let x = sbox.aes_round(x) ^ rk2_x4;
let x = sbox.aes_round(x) ^ rk3_x4;
let ks = sbox.aes_last_x4(x) ^ rk0_x4;
if XOR {
let src = u8x64::from_slice(simd, chunk);
(src ^ ks).store_slice(chunk);
} else {
ks.store_slice(chunk);
}
blk = blk.wrapping_add(4);
}
if !rest.is_empty() {
ctr_soft::<XOR>(self.root, blk, rest);
}
}
#[inline(always)]
fn fallback(self) {
ctr_soft::<XOR>(self.root, self.blk, self.buf);
}
}
#[inline(always)]
pub(super) fn ctr_simd<S: Simd, const XOR: bool>(
simd: S,
root: &[u8; 32],
blk: u64,
buf: &mut [u8],
) {
dispatch_sbox(simd, CtrOp::<XOR> { root, blk, buf });
}