hash-fast 0.1.0

Fast non-cryptographic hashing: the FxHash-family Hasher for maps and dedup tables, no_std, oracle-verified, avalanche-tested
Documentation
//! Naive reference forms: the forever-oracle. An independent
//! byte-assembly formulation of the same recurrence (bytes assembled
//! into a chunk through a shift-or loop instead of `from_le_bytes`),
//! plus the streaming-equivalence reference. Do not optimize this
//! module; do not delete it.

/// The recurrence constant (mirrors the crate's `SEED`).
const ORACLE_SEED: u64 = 0x51_7c_c1_b7_27_22_0a_95;

/// Byte-assembly hash: processes the input through the same
/// multiply-xor recurrence, assembling each 8-byte chunk with a
/// shift-or loop (not `from_le_bytes`). Byte-order and chunk-boundary
/// handling are re-derived here so a mistake in the optimized path
/// cannot be mirrored.
#[must_use]
// indexing is modulo-guarded: idx and idx+8 < len by the while condition
#[allow(clippy::indexing_slicing)]
pub fn naive_hash64(bytes: &[u8]) -> u64 {
    // indexing is modulo-guarded (idx and idx+8 < len by the while
    // conditions); the slicing denial applies to the crate, so the
    // guard is stated here
    #[allow(clippy::indexing_slicing)]
    let mut state: u64 = 0;
    let mut idx = 0usize;
    while idx + 8 <= bytes.len() {
        let mut chunk: u64 = 0;
        for (shift, b) in bytes[idx..idx + 8].iter().enumerate() {
            chunk |= u64::from(*b) << (8 * shift);
        }
        state = (state.rotate_left(5) ^ chunk).wrapping_mul(ORACLE_SEED);
        idx += 8;
    }
    while idx < bytes.len() {
        state = (state.rotate_left(5) ^ u64::from(bytes[idx])).wrapping_mul(ORACLE_SEED);
        idx += 1;
    }
    // the finalizer, re-derived step by step
    state ^= state >> 33;
    state = state.wrapping_mul(0xFF51_AFD7_ED55_8CCD);
    state ^= state >> 33;
    state = state.wrapping_mul(0xC4CE_B9FE_1A85_EC53);
    state ^= state >> 33;
    state
}

/// Streaming reference: hash `a ++ b` assembled from two slices. The
/// optimized `Hasher` must produce the same value from
/// `write(a); write(b)` as from one `write(a ++ b)`; this function is
/// the spec the split-write property compares against.
#[must_use]
pub fn naive_stream_hash(a: &[u8], b: &[u8]) -> u64 {
    // the oracle joins via a Vec: test-side allocation is the honest
    // reference formulation for the split-write property
    let mut joined = alloc::vec::Vec::with_capacity(a.len().saturating_add(b.len()));
    joined.extend_from_slice(a);
    joined.extend_from_slice(b);
    naive_hash64(&joined)
}