ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
#[cfg(feature = "std")]
use core::cell::Cell;
use core::{
  hash::{BuildHasher, Hash, Hasher},
  slice::from_raw_parts,
};
#[cfg(feature = "std")]
use std::{hash::RandomState, thread_local};

#[cfg(feature = "std")]
use crate::const_fn::GOLDEN_RATIO;
use crate::{
  backend::{ActiveBackend, DigestBackend, tree::cold},
  const_fn::K_SHORT_LI,
  state::{fold, rd64_ptr, read_0_16},
};

#[cfg(feature = "std")]
#[inline(always)]
pub(crate) fn random_or_zero_seed() -> u64 {
  thread_local! {
    static THREAD_SEED: Cell<u64> = Cell::new(RandomState::new().hash_one(GOLDEN_RATIO));
  }
  THREAD_SEED.with(|cell| {
    let next = cell.get().wrapping_add(GOLDEN_RATIO);
    cell.set(next);
    fold(next, K_SHORT_LI[1])
  })
}

#[cfg(not(feature = "std"))]
#[inline(always)]
pub(crate) const fn random_or_zero_seed() -> u64 {
  0
}

#[inline(always)]
pub(crate) const fn init_build_seed(seed: u64) -> u64 {
  seed.wrapping_add(K_SHORT_LI[0]) ^ K_SHORT_LI[0]
}

#[inline(always)]
unsafe fn write_byte_17_64<B: DigestBackend>(
  ptr: *const u8,
  len: usize,
  acc: u64,
  backend: B,
) -> u64 {
  if len > 64 {
    cold();
    // SAFETY: forwarded from caller contract.
    return unsafe { write_byte_gt64(ptr, len, acc, backend) };
  }
  // SAFETY: caller ensures 17 <= len <= 64 and valid ptr for len readable bytes.
  unsafe {
    let mut s0 = fold(
      rd64_ptr(ptr, 0) ^ acc,
      rd64_ptr(ptr, len - 16) ^ K_SHORT_LI[7],
    );
    let mut s1 = fold(
      rd64_ptr(ptr, 8) ^ acc.rotate_left(23),
      rd64_ptr(ptr, len - 8) ^ K_SHORT_LI[8].wrapping_add(len as u64),
    );
    if len > 32 {
      s0 = fold(
        rd64_ptr(ptr, 16) ^ s0,
        rd64_ptr(ptr, len - 32) ^ K_SHORT_LI[9],
      );
      s1 = fold(
        rd64_ptr(ptr, 24) ^ s1,
        rd64_ptr(ptr, len - 24) ^ K_SHORT_LI[10],
      );
    }
    s0 ^ s1.rotate_left(29)
  }
}

#[cold]
#[inline(never)]
unsafe fn write_byte_gt64<B: DigestBackend>(
  ptr: *const u8,
  len: usize,
  acc: u64,
  backend: B,
) -> u64 {
  // SAFETY: caller ensures ptr points to at least len readable bytes.
  backend.hash64(unsafe { from_raw_parts(ptr, len) }, acc)
}

#[derive(Clone, Debug)]
#[repr(transparent)]
pub(crate) struct HasherCore(pub(crate) u64);

impl HasherCore {
  #[inline(always)]
  pub(crate) const fn with_seed(seed: u64) -> Self {
    Self(seed ^ K_SHORT_LI[0])
  }

  #[inline(always)]
  fn write_small(&mut self, val: u64, tweak: u64) {
    self.0 = fold(self.0 ^ val, tweak);
  }

  #[inline(always)]
  pub(crate) fn write_u8_val(&mut self, val: u8) {
    self.write_small(val as u64, K_SHORT_LI[2]);
  }

  #[inline(always)]
  pub(crate) fn write_u16_val(&mut self, val: u16) {
    self.write_small(val as u64, K_SHORT_LI[4]);
  }

  #[inline(always)]
  pub(crate) fn write_u32_val(&mut self, val: u32) {
    self.write_small(val as u64, K_SHORT_LI[5]);
  }

  #[inline(always)]
  pub(crate) fn write_wide(&mut self, val: u64) {
    self.0 = fold(self.0 ^ val, K_SHORT_LI[1]);
  }

  #[inline(always)]
  pub(crate) fn write_u128_val(&mut self, val: u128) {
    self.0 = fold((val as u64) ^ self.0, ((val >> 64) as u64) ^ K_SHORT_LI[6]);
  }

  #[inline(always)]
  pub(crate) fn write_byte_with<B: DigestBackend>(&mut self, data: &[u8], backend: B) {
    let len = data.len();
    let ptr = data.as_ptr();
    let acc = self.0 ^ (len as u64);
    if len > 16 {
      // SAFETY: data provides valid ptr for len > 16 readable bytes.
      self.0 = unsafe { write_byte_17_64(ptr, len, acc, backend) };
      return;
    }
    // SAFETY: 0 <= len <= 16 keeps read_0_16 in bounds.
    let (w0, w1) = unsafe { read_0_16(ptr, len) };
    self.0 = fold(w0 ^ acc, w1 ^ K_SHORT_LI[7].wrapping_add(len as u64));
  }

  #[inline(always)]
  pub(crate) const fn finish_u64(&self) -> u64 {
    self.0
  }

  #[inline(always)]
  pub(crate) fn finish_u128(&self) -> u128 {
    let lo = self.0;
    let hi = fold(lo ^ K_SHORT_LI[10], K_SHORT_LI[11]);
    (lo as u128) | ((hi as u128) << 64)
  }
}

macro_rules! impl_ihasher {
  ($ty:ty, $core_field:tt) => {
    impl Hasher for $ty {
      #[inline(always)]
      fn write(&mut self, data: &[u8]) {
        let backend = self.backend();
        self.$core_field.write_byte_with(data, backend);
      }

      #[inline(always)]
      fn write_u8(&mut self, i: u8) {
        self.$core_field.write_u8_val(i);
      }

      #[inline(always)]
      fn write_u16(&mut self, i: u16) {
        self.$core_field.write_u16_val(i);
      }

      #[inline(always)]
      fn write_u32(&mut self, i: u32) {
        self.$core_field.write_u32_val(i);
      }

      #[inline(always)]
      fn write_u64(&mut self, i: u64) {
        self.$core_field.write_wide(i);
      }

      #[inline(always)]
      fn write_u128(&mut self, i: u128) {
        self.$core_field.write_u128_val(i);
      }

      #[inline(always)]
      fn write_usize(&mut self, i: usize) {
        self.$core_field.write_wide(i as u64);
      }

      #[inline(always)]
      fn finish(&self) -> u64 {
        self.$core_field.finish_u64()
      }
    }
  };
}

#[cfg(feature = "cpu")]
pub(crate) use impl_ihasher;

/// Single-register 8-byte streaming `core::hash::Hasher` optimized for maps and incremental inputs.
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct IHasher(HasherCore);

impl IHasher {
  /// Creates an `IHasher` initialized with seed `0`.
  #[must_use]
  #[inline]
  pub const fn new() -> Self {
    Self::with_seed(0)
  }

  /// Creates an `IHasher` initialized with a specific `seed`.
  #[must_use]
  #[inline]
  pub(crate) const fn with_seed(seed: u64) -> Self {
    Self(HasherCore::with_seed(seed))
  }

  /// Finalizes the accumulated state as a 128-bit digest.
  #[must_use]
  #[inline]
  pub fn finish_u128(&self) -> u128 {
    self.0.finish_u128()
  }

  #[inline(always)]
  const fn backend(&self) -> ActiveBackend {
    ActiveBackend
  }
}

impl Default for IHasher {
  #[inline]
  fn default() -> Self {
    Self::new()
  }
}

impl_ihasher!(IHasher, 0);

/// `core::hash::BuildHasher` factory producing [`IHasher`] instances with per-thread seeds.
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct IBuildHasher(u64);

impl IBuildHasher {
  /// Creates an `IBuildHasher` with per-thread randomized seeding when `std` is enabled.
  #[must_use]
  #[inline]
  pub fn new() -> Self {
    Self::default()
  }

  /// Creates a deterministic `IBuildHasher` pinned to a specific `seed`.
  #[must_use]
  #[inline]
  pub(crate) const fn with_seed(seed: u64) -> Self {
    Self(init_build_seed(seed))
  }
}

impl Default for IBuildHasher {
  #[inline]
  fn default() -> Self {
    Self::with_seed(random_or_zero_seed())
  }
}

impl BuildHasher for IBuildHasher {
  type Hasher = IHasher;

  #[inline(always)]
  fn build_hasher(&self) -> Self::Hasher {
    IHasher(HasherCore(self.0))
  }

  #[inline(always)]
  fn hash_one<T: Hash>(&self, x: T) -> u64 {
    let mut hasher = IHasher(HasherCore(self.0));
    x.hash(&mut hasher);
    hasher.finish()
  }
}