ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
use fearless_simd::{Bytes, Select, Simd, SimdBase, SimdFrom, i8x64, u8x16, u8x64, u32x16};

use super::tbl::{
  EXP16_LI, IPT_HI_LI, IPT_LO_LI, LOG_INV16_LI, LOG16_LI, SBO_HI_LI, SBO_LO_LI, SQ_NU16_LI,
  SR_PERM_LI, SR_ROT8_PERM_LI,
};
use crate::{const_fn::SBOX_LI, state::State};

#[inline(always)]
pub(super) fn splat_x4<S: Simd>(simd: S, state: State) -> u8x64<S> {
  u8x64::block_splat(u8x16::simd_from(simd, state))
}

const PROBE_TBL_LI: [u8; 16] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

#[inline(always)]
fn swizzle_probe_zero<S: Simd>(simd: S, idx: u8) -> bool {
  u8x16::simd_from(simd, PROBE_TBL_LI).swizzle_dyn_within_blocks(u8x16::splat(simd, idx))[0] == 0
}

/// Checks via a branchless single-lane swizzle probe whether `S` executes byte shuffles natively in hardware.
#[inline(always)]
fn has_hw_byte_swizzle<S: Simd>(simd: S) -> bool {
  if <S::u8s as SimdBase<S>>::LEN >= 32 {
    return true;
  }
  swizzle_probe_zero(simd, 0x80)
}

/// Checks whether a wide 64-byte table lookup outperforms tower-field arithmetic on backend `S`.
#[inline(always)]
fn prefers_direct_sbox<S: Simd>(simd: S) -> bool {
  let lane_cnt = <S::u8s as SimdBase<S>>::LEN;
  if lane_cnt >= 64 {
    return true;
  }
  if lane_cnt == 32 {
    return false;
  }
  swizzle_probe_zero(simd, 0x10)
}

/// Kernel operation parameterized by compile-time S-box strategy ([`DirectSbox4`] vs [`TowerSbox4`]).
pub(super) trait VpermOp<S: Simd> {
  type Out;
  fn run<E: SimdSbox4<S>>(self, simd: S) -> Self::Out;
  fn fallback(self) -> Self::Out;
}

/// Dispatches `op` across hardware-table, tower-field, or scalar fallback paths.
#[inline(always)]
pub(super) fn dispatch_sbox<S: Simd, O: VpermOp<S>>(simd: S, op: O) -> O::Out {
  if !has_hw_byte_swizzle(simd) {
    return op.fallback();
  }
  if prefers_direct_sbox(simd) {
    op.run::<DirectSbox4<S>>(simd)
  } else {
    op.run::<TowerSbox4<S>>(simd)
  }
}

/// 4-block (64-byte) portable SIMD AES round engine parameterized by S-box strategy.
pub(super) trait SimdSbox4<S: Simd> {
  fn new(simd: S) -> Self;
  fn sub_byte(&self, x: u8x64<S>) -> u8x64<S>;
  fn mix_consts(&self) -> &MixConsts<S>;

  #[inline(always)]
  fn aes_round(&self, x: u8x64<S>) -> u8x64<S> {
    let mc = self.mix_consts();
    let simd = mc.simd;
    let sb = self.sub_byte(x);
    let sr = sb.swizzle_dyn_within_blocks(mc.sr_perm);
    let rot1 = sb.swizzle_dyn_within_blocks(mc.sr_rot8_perm);
    let t = sr ^ rot1;
    let t_u32 = t.bitcast::<u32x16<S>>();
    let t_rot2 = ((t_u32 >> 16) | (t_u32 << 16)).bitcast::<u8x64<S>>();
    let zero_u8 = u8x64::splat(simd, 0);
    let c_1b = u8x64::splat(simd, 0x1B);
    let xtime_mask = t
      .bitcast::<i8x64<S>>()
      .simd_lt(zero_u8.bitcast::<i8x64<S>>());
    let xtime_t = (t + t) ^ xtime_mask.select(c_1b, zero_u8);
    xtime_t ^ rot1 ^ t_rot2
  }

  #[inline(always)]
  fn aes_last_x4(&self, x: u8x64<S>) -> u8x64<S> {
    let sb = self.sub_byte(x);
    sb.swizzle_dyn_within_blocks(self.mix_consts().sr_perm)
  }
}

pub(super) struct MixConsts<S: Simd> {
  simd: S,
  sr_perm: u8x64<S>,
  sr_rot8_perm: u8x64<S>,
}

impl<S: Simd> MixConsts<S> {
  #[inline(always)]
  fn new(simd: S) -> Self {
    Self {
      simd,
      sr_perm: splat_x4(simd, SR_PERM_LI),
      sr_rot8_perm: splat_x4(simd, SR_ROT8_PERM_LI),
    }
  }
}

/// Full 256-entry 64B-banked S-box engine for wide-table SIMD tiers (NEON `TBL4`, AVX-512 VBMI, WASM).
struct DirectSbox4<S: Simd> {
  sb0: u8x64<S>,
  sb1: u8x64<S>,
  sb2: u8x64<S>,
  sb3: u8x64<S>,
  mix: MixConsts<S>,
}

impl<S: Simd> SimdSbox4<S> for DirectSbox4<S> {
  #[inline(always)]
  fn new(simd: S) -> Self {
    Self {
      sb0: u8x64::from_slice(simd, &SBOX_LI[..64]),
      sb1: u8x64::from_slice(simd, &SBOX_LI[64..128]),
      sb2: u8x64::from_slice(simd, &SBOX_LI[128..192]),
      sb3: u8x64::from_slice(simd, &SBOX_LI[192..]),
      mix: MixConsts::new(simd),
    }
  }

  #[inline(always)]
  fn sub_byte(&self, x: u8x64<S>) -> u8x64<S> {
    let c_80 = u8x64::splat(self.mix.simd, 0x80);
    let lo_half = self.sb0.concat_swizzle_dyn_precise(self.sb1, x);
    let hi_half = self.sb2.concat_swizzle_dyn_precise(self.sb3, x ^ c_80);
    lo_half | hi_half
  }

  #[inline(always)]
  fn mix_consts(&self) -> &MixConsts<S> {
    &self.mix
  }
}

/// Composite `GF((2^4)^2)` S-box engine using within-16B byte shuffles (optimal on AVX2 / SSE4.2).
struct TowerSbox4<S: Simd> {
  c_0f: u8x64<S>,
  c_8f: u8x64<S>,
  ipt_lo: u8x64<S>,
  ipt_hi: u8x64<S>,
  log16: u8x64<S>,
  exp16: u8x64<S>,
  log_inv16: u8x64<S>,
  sq_nu16: u8x64<S>,
  sbo_lo: u8x64<S>,
  sbo_hi: u8x64<S>,
  mix: MixConsts<S>,
}

impl<S: Simd> TowerSbox4<S> {
  #[inline(always)]
  fn gf16_mul(&self, la: u8x64<S>, lb: u8x64<S>) -> u8x64<S> {
    let s = la + lb;
    // Sentinel bit 7 (`>= 0x80`) zeroes out-of-range hardware shuffles with zero compare/select instructions.
    let idx = (s & self.c_8f) + (s >> 4);
    self.exp16.swizzle_dyn_within_blocks(idx)
  }
}

impl<S: Simd> SimdSbox4<S> for TowerSbox4<S> {
  #[inline(always)]
  fn new(simd: S) -> Self {
    Self {
      c_0f: u8x64::splat(simd, 0x0F),
      c_8f: u8x64::splat(simd, 0x8F),
      ipt_lo: splat_x4(simd, IPT_LO_LI),
      ipt_hi: splat_x4(simd, IPT_HI_LI),
      log16: splat_x4(simd, LOG16_LI),
      exp16: splat_x4(simd, EXP16_LI),
      log_inv16: splat_x4(simd, LOG_INV16_LI),
      sq_nu16: splat_x4(simd, SQ_NU16_LI),
      sbo_lo: splat_x4(simd, SBO_LO_LI),
      sbo_hi: splat_x4(simd, SBO_HI_LI),
      mix: MixConsts::new(simd),
    }
  }

  #[inline(always)]
  fn sub_byte(&self, x: u8x64<S>) -> u8x64<S> {
    let lo = x & self.c_0f;
    let hi = x >> 4;
    let p = self.ipt_lo.swizzle_dyn_within_blocks(lo) ^ self.ipt_hi.swizzle_dyn_within_blocks(hi);
    let a0 = p & self.c_0f;
    let a1 = p >> 4;
    let a01 = a0 ^ a1;

    let la0 = self.log16.swizzle_dyn_within_blocks(a0);
    let la1 = self.log16.swizzle_dyn_within_blocks(a1);
    let la01 = self.log16.swizzle_dyn_within_blocks(a01);

    let prod = self.gf16_mul(la0, la01);
    let d = prod ^ self.sq_nu16.swizzle_dyn_within_blocks(a1);
    let linv = self.log_inv16.swizzle_dyn_within_blocks(d);
    let b0 = self.gf16_mul(la01, linv);
    let b1 = self.gf16_mul(la1, linv);

    self.sbo_lo.swizzle_dyn_within_blocks(b0) ^ self.sbo_hi.swizzle_dyn_within_blocks(b1)
  }

  #[inline(always)]
  fn mix_consts(&self) -> &MixConsts<S> {
    &self.mix
  }
}