#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![warn(missing_docs)]
use core::hash::Hasher;
#[cfg(feature = "std")]
mod convenience;
mod seed;
#[cfg(feature = "std")]
pub use convenience::*;
const ARBITRARY0: u64 = 0x243f6a8885a308d3;
const ARBITRARY1: u64 = 0x13198a2e03707344;
const ARBITRARY2: u64 = 0xa4093822299f31d0;
const ARBITRARY3: u64 = 0x082efa98ec4e6c89;
const ARBITRARY4: u64 = 0x452821e638d01377;
const ARBITRARY5: u64 = 0xbe5466cf34e90c6c;
const ARBITRARY6: u64 = 0xc0ac29b7c97c50dd;
const ARBITRARY7: u64 = 0x3f84d5b5b5470917;
const ARBITRARY8: u64 = 0x9216d5d98979fb1b;
const ARBITRARY9: u64 = 0xd1310ba698dfb5ac;
#[inline(always)]
const fn folded_multiply(x: u64, y: u64) -> u64 {
#[cfg(target_pointer_width = "64")]
{
let full = (x as u128) * (y as u128);
let lo = full as u64;
let hi = (full >> 64) as u64;
lo ^ hi
}
#[cfg(target_pointer_width = "32")]
{
let lx = x as u32;
let ly = y as u32;
let hx = (x >> 32) as u32;
let hy = (y >> 32) as u32;
let afull = (lx as u64) * (hy as u64);
let bfull = (hx as u64) * (ly as u64);
afull ^ bfull.rotate_right(32)
}
}
pub mod fast {
use super::*;
pub use seed::fast::{FixedState, RandomState};
#[derive(Clone)]
pub struct FoldHasher {
accumulator: u64,
sponge: u128,
sponge_len: u8,
fold_seed: u64,
expand_seed: u64,
expand_seed2: u64,
expand_seed3: u64,
}
impl FoldHasher {
pub(crate) fn with_seed(per_hasher_seed: u64, global_seed: &[u64; 4]) -> FoldHasher {
FoldHasher {
accumulator: per_hasher_seed,
sponge: 0,
sponge_len: 0,
fold_seed: global_seed[0],
expand_seed: global_seed[1],
expand_seed2: global_seed[2],
expand_seed3: global_seed[3],
}
}
#[inline(always)]
fn write_num<T: Into<u128>>(&mut self, x: T) {
let bits: usize = 8 * core::mem::size_of::<T>();
if self.sponge_len as usize + bits > 128 {
let lo = self.sponge as u64;
let hi = (self.sponge >> 64) as u64;
self.accumulator = folded_multiply(lo ^ self.accumulator, hi ^ self.fold_seed);
self.sponge = x.into();
self.sponge_len = 0;
} else {
self.sponge |= x.into() << self.sponge_len;
self.sponge_len += bits as u8;
}
}
}
impl Hasher for FoldHasher {
#[inline(always)]
fn write(&mut self, bytes: &[u8]) {
let mut s0 = self.accumulator;
let mut s1 = self.expand_seed;
let len = bytes.len();
if len <= 16 {
if len >= 8 {
s0 ^= u64::from_ne_bytes(bytes[0..8].try_into().unwrap());
s1 ^= u64::from_ne_bytes(bytes[len - 8..].try_into().unwrap());
} else if len >= 4 {
s0 ^= u32::from_ne_bytes(bytes[0..4].try_into().unwrap()) as u64;
s1 ^= u32::from_ne_bytes(bytes[len - 4..].try_into().unwrap()) as u64;
} else if len > 0 {
let lo = bytes[0];
let mid = bytes[len / 2];
let hi = bytes[len - 1];
s0 ^= lo as u64;
s1 ^= ((hi as u64) << 8) | mid as u64;
}
self.accumulator = folded_multiply(s0, s1);
} else if len < 256 {
self.accumulator = hash_bytes_medium(bytes, s0, s1, self.fold_seed);
} else {
self.accumulator = hash_bytes_long(
bytes,
s0,
s1,
self.expand_seed2,
self.expand_seed3,
self.fold_seed,
);
}
}
#[inline(always)]
fn write_u8(&mut self, i: u8) {
self.write_num(i);
}
#[inline(always)]
fn write_u16(&mut self, i: u16) {
self.write_num(i);
}
#[inline(always)]
fn write_u32(&mut self, i: u32) {
self.write_num(i);
}
#[inline(always)]
fn write_u64(&mut self, i: u64) {
self.write_num(i);
}
#[inline(always)]
fn write_u128(&mut self, i: u128) {
let lo = i as u64;
let hi = (i >> 64) as u64;
self.accumulator = folded_multiply(lo ^ self.accumulator, hi ^ self.fold_seed);
}
#[inline(always)]
fn write_usize(&mut self, i: usize) {
#[cfg(target_pointer_width = "32")]
self.write_num(i as u32);
#[cfg(target_pointer_width = "64")]
self.write_num(i as u64);
}
#[inline(always)]
fn finish(&self) -> u64 {
if self.sponge_len > 0 {
let lo = self.sponge as u64;
let hi = (self.sponge >> 64) as u64;
folded_multiply(lo ^ self.accumulator, hi ^ self.fold_seed)
} else {
self.accumulator
}
}
}
}
pub mod quality {
use super::*;
pub use seed::quality::{FixedState, RandomState};
#[derive(Clone)]
pub struct FoldHasher {
pub(crate) inner: fast::FoldHasher,
}
impl Hasher for FoldHasher {
#[inline(always)]
fn write(&mut self, bytes: &[u8]) {
self.inner.write(bytes);
}
#[inline(always)]
fn write_u8(&mut self, i: u8) {
self.inner.write_u8(i);
}
#[inline(always)]
fn write_u16(&mut self, i: u16) {
self.inner.write_u16(i);
}
#[inline(always)]
fn write_u32(&mut self, i: u32) {
self.inner.write_u32(i);
}
#[inline(always)]
fn write_u64(&mut self, i: u64) {
self.inner.write_u64(i);
}
#[inline(always)]
fn write_u128(&mut self, i: u128) {
self.inner.write_u128(i);
}
#[inline(always)]
fn write_usize(&mut self, i: usize) {
self.inner.write_usize(i);
}
#[inline(always)]
fn finish(&self) -> u64 {
folded_multiply(self.inner.finish(), ARBITRARY0)
}
}
}
fn hash_bytes_medium(bytes: &[u8], mut s0: u64, mut s1: u64, fold_seed: u64) -> u64 {
let left_to_right = bytes.chunks_exact(16);
let mut right_to_left = bytes.rchunks_exact(16);
for lo in left_to_right {
let hi = right_to_left.next().unwrap();
let unconsumed_start = lo.as_ptr();
let unconsumed_end = hi.as_ptr_range().end;
if unconsumed_start >= unconsumed_end {
break;
}
let a = u64::from_ne_bytes(lo[0..8].try_into().unwrap());
let b = u64::from_ne_bytes(lo[8..16].try_into().unwrap());
let c = u64::from_ne_bytes(hi[0..8].try_into().unwrap());
let d = u64::from_ne_bytes(hi[8..16].try_into().unwrap());
s0 = folded_multiply(a ^ s0, c ^ fold_seed);
s1 = folded_multiply(b ^ s1, d ^ fold_seed);
}
s0 ^ s1
}
#[cold]
#[inline(never)]
fn hash_bytes_long(
bytes: &[u8],
mut s0: u64,
mut s1: u64,
mut s2: u64,
mut s3: u64,
fold_seed: u64,
) -> u64 {
let chunks = bytes.chunks_exact(64);
let remainder = chunks.remainder().len();
for chunk in chunks {
let a = u64::from_ne_bytes(chunk[0..8].try_into().unwrap());
let b = u64::from_ne_bytes(chunk[8..16].try_into().unwrap());
let c = u64::from_ne_bytes(chunk[16..24].try_into().unwrap());
let d = u64::from_ne_bytes(chunk[24..32].try_into().unwrap());
let e = u64::from_ne_bytes(chunk[32..40].try_into().unwrap());
let f = u64::from_ne_bytes(chunk[40..48].try_into().unwrap());
let g = u64::from_ne_bytes(chunk[48..56].try_into().unwrap());
let h = u64::from_ne_bytes(chunk[56..64].try_into().unwrap());
s0 = folded_multiply(a ^ s0, e ^ fold_seed);
s1 = folded_multiply(b ^ s1, f ^ fold_seed);
s2 = folded_multiply(c ^ s2, g ^ fold_seed);
s3 = folded_multiply(d ^ s3, h ^ fold_seed);
}
s0 ^= s2;
s1 ^= s3;
if remainder > 0 {
hash_bytes_medium(&bytes[bytes.len() - remainder.max(16)..], s0, s1, fold_seed)
} else {
s0 ^ s1
}
}