ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
use crate::const_fn::{T0_LI, T1_LI, T2_LI, T3_LI};

/// 128-bit AES state represented in canonical little-endian byte order.
pub(crate) type State = [u8; 16];

pub(crate) const BLOCK_LEN: usize = 16;
pub(crate) const SHORT_MAX: usize = 128;
pub(crate) const MID4_MAX: usize = 512;
pub(crate) const MID_MAX: usize = 2048;

/// Folded 64x64->128 widening multiply XOR mixing both halves.
#[inline(always)]
pub(crate) const fn fold(a: u64, b: u64) -> u64 {
  let prod = (a as u128).wrapping_mul(b as u128);
  (prod as u64) ^ ((prod >> 64) as u64)
}

/// Reads an unaligned little-endian `u32` at `ptr + off`.
///
/// # Safety
///
/// Caller must ensure `ptr.add(off)` points to at least 4 readable bytes.
#[inline(always)]
pub(crate) unsafe fn rd32_ptr(ptr: *const u8, off: usize) -> u32 {
  // SAFETY: caller ensures ptr + off points to at least 4 readable bytes.
  u32::from_le(unsafe { ptr.add(off).cast::<u32>().read_unaligned() })
}

/// Reads an unaligned little-endian `u64` at `ptr + off`.
///
/// # Safety
///
/// Caller must ensure `ptr.add(off)` points to at least 8 readable bytes.
#[inline(always)]
pub(crate) unsafe fn rd64_ptr(ptr: *const u8, off: usize) -> u64 {
  // SAFETY: caller ensures ptr + off points to at least 8 readable bytes.
  u64::from_le(unsafe { ptr.add(off).cast::<u64>().read_unaligned() })
}

/// Exact non-OOB read of 1..=3 bytes packed into the low 24 bits of a `u32`.
///
/// # Safety
///
/// Caller must ensure `1 <= len < 4` readable bytes starting at `ptr`.
#[inline(always)]
pub(crate) unsafe fn read_1_3(ptr: *const u8, len: usize) -> u32 {
  debug_assert!(len > 0 && len < 4);
  // SAFETY: caller guarantees 1 <= len < 4 readable bytes at ptr.
  unsafe { (*ptr as u32) | ((*ptr.add(len >> 1) as u32) << 8) | ((*ptr.add(len - 1) as u32) << 16) }
}

/// Exact non-OOB read of 0..=16 bytes into two little-endian `u64` lanes.
///
/// # Safety
///
/// Caller must ensure `len <= 16` and `ptr` points to at least `len` readable bytes.
#[inline(always)]
pub(crate) unsafe fn read_0_16(ptr: *const u8, len: usize) -> (u64, u64) {
  debug_assert!(len <= BLOCK_LEN);
  // SAFETY: caller guarantees ptr points to at least len readable bytes.
  unsafe {
    if len >= 8 {
      (rd64_ptr(ptr, 0), rd64_ptr(ptr, len - 8))
    } else if len >= 4 {
      let lo = rd32_ptr(ptr, 0) as u64;
      let hi = rd32_ptr(ptr, len - 4) as u64;
      (lo | (hi << 32), 0)
    } else if len > 0 {
      (read_1_3(ptr, len) as u64, 0)
    } else {
      (0, 0)
    }
  }
}

/// Packs `(lo, hi)` 128-bit digest halves into the 32-byte little-endian root digest.
#[inline(always)]
pub(crate) fn pack_root(lo: State, hi: State) -> [u8; 32] {
  let mut root_li = [0u8; 32];
  root_li[..BLOCK_LEN].copy_from_slice(&lo);
  root_li[BLOCK_LEN..].copy_from_slice(&hi);
  root_li
}

/// Bit-exact scalar AES encipher round: `MixColumns(ShiftRows(SubBytes(state))) ^ rk`.
#[inline]
pub(crate) const fn aes_enc(state: State, rk: State) -> State {
  let y0 = T0_LI[state[0] as usize]
    ^ T1_LI[state[5] as usize]
    ^ T2_LI[state[10] as usize]
    ^ T3_LI[state[15] as usize];
  let y1 = T0_LI[state[4] as usize]
    ^ T1_LI[state[9] as usize]
    ^ T2_LI[state[14] as usize]
    ^ T3_LI[state[3] as usize];
  let y2 = T0_LI[state[8] as usize]
    ^ T1_LI[state[13] as usize]
    ^ T2_LI[state[2] as usize]
    ^ T3_LI[state[7] as usize];
  let y3 = T0_LI[state[12] as usize]
    ^ T1_LI[state[1] as usize]
    ^ T2_LI[state[6] as usize]
    ^ T3_LI[state[11] as usize];

  let b0 = y0.to_le_bytes();
  let b1 = y1.to_le_bytes();
  let b2 = y2.to_le_bytes();
  let b3 = y3.to_le_bytes();
  [
    b0[0] ^ rk[0],
    b0[1] ^ rk[1],
    b0[2] ^ rk[2],
    b0[3] ^ rk[3],
    b1[0] ^ rk[4],
    b1[1] ^ rk[5],
    b1[2] ^ rk[6],
    b1[3] ^ rk[7],
    b2[0] ^ rk[8],
    b2[1] ^ rk[9],
    b2[2] ^ rk[10],
    b2[3] ^ rk[11],
    b3[0] ^ rk[12],
    b3[1] ^ rk[13],
    b3[2] ^ rk[14],
    b3[3] ^ rk[15],
  ]
}