ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
use fearless_simd::Level;

#[cfg(feature = "std")]
use super::{HashMap, HashSet};
use super::{
  IBuildHasher, IHasher, Stream,
  vperm::{CpuBackend, compress_hi, compress_lo, detect_level},
};
use crate::{
  arch::soft::{short_128, short_256},
  state::SHORT_MAX,
  stream::fill_xof_root,
};

pub(super) trait LevelSource: Copy {
  fn resolve(self) -> Level;
}

impl LevelSource for Level {
  #[inline(always)]
  fn resolve(self) -> Level {
    self
  }
}

#[derive(Clone, Copy)]
pub(super) struct DetectLevel;

impl LevelSource for DetectLevel {
  #[inline(always)]
  fn resolve(self) -> Level {
    detect_level()
  }
}

#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub(super) fn hash128_with<const SEED0: bool>(
  data: &[u8],
  seed: u64,
  src: impl LevelSource,
) -> u128 {
  if data.len() <= SHORT_MAX {
    return short_128::<SEED0>(data, seed);
  }
  compress_lo::<SEED0>(data, seed, src.resolve())
}

#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub(super) fn hash256_with<const SEED0: bool>(
  data: &[u8],
  seed: u64,
  src: impl LevelSource,
) -> [u8; 32] {
  if data.len() <= SHORT_MAX {
    return short_256::<SEED0>(data, seed);
  }
  compress_hi::<SEED0>(data, seed, src.resolve())
}

#[inline(always)]
pub(super) fn hash_xof_with<const SEED0: bool>(
  data: &[u8],
  seed: u64,
  out: &mut [u8],
  src: impl LevelSource,
) {
  if out.is_empty() {
    return;
  }
  let lvl = src.resolve();
  let root = hash256_with::<SEED0>(data, seed, lvl);
  fill_xof_root(root, out, CpuBackend(lvl));
}

/// Computes a seeded 32-bit hash without AES hardware intrinsics, using an explicit `fearless_simd::Level`.
#[must_use]
#[inline]
pub fn hash32_with_level(data: &[u8], seed: u64, level: Level) -> u32 {
  hash64_with_level(data, seed, level) as u32
}

/// Computes a seeded 64-bit hash without AES hardware intrinsics, using an explicit `fearless_simd::Level`.
#[must_use]
#[inline]
pub fn hash64_with_level(data: &[u8], seed: u64, level: Level) -> u64 {
  hash128_with_level(data, seed, level) as u64
}

/// Computes a seeded 128-bit hash without AES hardware intrinsics, using an explicit `fearless_simd::Level`.
#[must_use]
#[inline]
pub fn hash128_with_level(data: &[u8], seed: u64, level: Level) -> u128 {
  hash128_with::<false>(data, seed, level)
}

/// Computes a seeded 256-bit hash without AES hardware intrinsics, using an explicit `fearless_simd::Level`.
#[must_use]
#[inline]
pub fn hash256_with_level(data: &[u8], seed: u64, level: Level) -> [u8; 32] {
  hash256_with::<false>(data, seed, level)
}

/// Computes a seeded arbitrary-length XOF digest without AES hardware intrinsics, using `level`.
#[inline]
pub fn hash_xof_with_level(data: &[u8], seed: u64, out: &mut [u8], level: Level) {
  hash_xof_with::<false>(data, seed, out, level);
}

/// Computes a seeded 32-bit hash using pure-CPU software AES via `fearless_simd`.
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash32(data: &[u8], seed: u64) -> u32 {
  hash64(data, seed) as u32
}

/// Computes a seeded 64-bit hash using pure-CPU software AES via `fearless_simd`.
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash64(data: &[u8], seed: u64) -> u64 {
  hash128(data, seed) as u64
}

/// Computes a seeded 128-bit hash using pure-CPU software AES via `fearless_simd`.
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash128(data: &[u8], seed: u64) -> u128 {
  hash128_with::<false>(data, seed, DetectLevel)
}

/// Computes a seeded 256-bit hash using pure-CPU software AES via `fearless_simd`.
#[must_use]
#[cfg_attr(not(debug_assertions), inline(always))]
#[cfg_attr(debug_assertions, inline)]
pub fn hash256(data: &[u8], seed: u64) -> [u8; 32] {
  hash256_with::<false>(data, seed, DetectLevel)
}

/// Fills `out` with a seeded arbitrary-length XOF digest using pure-CPU software AES.
#[inline]
pub fn hash_xof(data: &[u8], seed: u64, out: &mut [u8]) {
  hash_xof_with::<false>(data, seed, out, DetectLevel);
}

/// Creates a pure-CPU stream seeded by `cpu::seed::hash256(data, seed)`.
#[must_use]
#[inline]
pub fn stream(data: &[u8], seed: u64) -> Stream {
  stream_with_level(data, seed, detect_level())
}

/// Creates a pure-CPU stream seeded with an explicit `fearless_simd::Level`.
#[must_use]
#[inline]
pub fn stream_with_level(data: &[u8], seed: u64, level: Level) -> Stream {
  Stream::from_root_with_level(hash256_with_level(data, seed, level), level)
}

/// Creates a pure-CPU keyed cipher stream directly from `seed`.
#[must_use]
#[inline]
pub fn keystream(seed: u64) -> Stream {
  stream(&[], seed)
}

/// Creates a pure-CPU keyed cipher stream directly from `seed` with an explicit SIMD level.
#[must_use]
#[inline]
pub fn keystream_with_level(seed: u64, level: Level) -> Stream {
  stream_with_level(&[], seed, level)
}

/// Creates a pure-CPU `IHasher` initialized with `seed`.
#[must_use]
#[inline]
pub fn hasher(seed: u64) -> IHasher {
  IHasher::with_seed(seed)
}

/// Creates a pure-CPU `IHasher` pinned to a specific `seed` and `fearless_simd::Level`.
#[must_use]
#[inline]
pub const fn hasher_with_level(seed: u64, level: Level) -> IHasher {
  IHasher::with_seed_and_level(seed, level)
}

/// Creates a deterministic pure-CPU `IBuildHasher` pinned to `seed`.
#[must_use]
#[inline]
pub fn build_hasher(seed: u64) -> IBuildHasher {
  IBuildHasher::with_seed(seed)
}

/// Creates a deterministic pure-CPU `IBuildHasher` with an explicit SIMD level.
#[must_use]
#[inline]
pub const fn build_hasher_with_level(seed: u64, level: Level) -> IBuildHasher {
  IBuildHasher::with_seed_and_level(seed, level)
}

/// Creates an empty pure-CPU `HashMap` pinned to a deterministic `seed`.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_map<K, V>(seed: u64) -> HashMap<K, V> {
  HashMap::with_hasher(build_hasher(seed))
}

/// Creates an empty pure-CPU `HashMap` pinned to `seed` and an explicit SIMD level.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_map_with_level<K, V>(seed: u64, level: Level) -> HashMap<K, V> {
  HashMap::with_hasher(build_hasher_with_level(seed, level))
}

/// Creates an empty pure-CPU `HashSet` pinned to a deterministic `seed`.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_set<T>(seed: u64) -> HashSet<T> {
  HashSet::with_hasher(build_hasher(seed))
}

/// Creates an empty pure-CPU `HashSet` pinned to `seed` and an explicit SIMD level.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
#[inline(always)]
pub fn hash_set_with_level<T>(seed: u64, level: Level) -> HashSet<T> {
  HashSet::with_hasher(build_hasher_with_level(seed, level))
}