//! Hash-v1: portable XXH64, seed zero, over explicit machine byte encodings. use prometeu_bytecode::HeapRef; const P1: u64 = 0x9E3779B185EBCA87; const P2: u64 = 0xC2B2AE3D27D4EB4F; const P3: u64 = 0x165667B19E3779F9; const P4: u64 = 0x85EBCA77C2B2AE63; const P5: u64 = 0x27D4EB2F165667C5; fn word(bytes: &[u8]) -> u64 { u64::from_le_bytes(bytes[..8].try_into().expect("complete XXH64 lane")) } fn round(acc: u64, lane: u64) -> u64 { acc.wrapping_add(lane.wrapping_mul(P2)).rotate_left(31).wrapping_mul(P1) } fn merge(acc: u64, lane: u64) -> u64 { (acc ^ round(0, lane)).wrapping_mul(P1).wrapping_add(P4) } pub(crate) fn xxh64(mut bytes: &[u8]) -> u64 { let length = bytes.len() as u64; let mut hash = if bytes.len() >= 32 { let mut acc = [P1.wrapping_add(P2), P2, 0, 0u64.wrapping_sub(P1)]; while bytes.len() >= 32 { for (index, value) in acc.iter_mut().enumerate() { *value = round(*value, word(&bytes[index * 8..])); } bytes = &bytes[32..]; } let mut hash = acc[0] .rotate_left(1) .wrapping_add(acc[1].rotate_left(7)) .wrapping_add(acc[2].rotate_left(12)) .wrapping_add(acc[3].rotate_left(18)); for value in acc { hash = merge(hash, value); } hash } else { P5 }; hash = hash.wrapping_add(length); while bytes.len() >= 8 { hash ^= round(0, word(bytes)); hash = hash.rotate_left(27).wrapping_mul(P1).wrapping_add(P4); bytes = &bytes[8..]; } if bytes.len() >= 4 { hash ^= u64::from(u32::from_le_bytes(bytes[..4].try_into().unwrap())).wrapping_mul(P1); hash = hash.rotate_left(23).wrapping_mul(P2).wrapping_add(P3); bytes = &bytes[4..]; } for byte in bytes { hash ^= u64::from(*byte).wrapping_mul(P5); hash = hash.rotate_left(11).wrapping_mul(P1); } hash ^= hash >> 33; hash = hash.wrapping_mul(P2); hash ^= hash >> 29; hash = hash.wrapping_mul(P3); hash ^ (hash >> 32) } pub(crate) fn reference(value: Option) -> i64 { value.map_or(0, |reference| { let mut bytes = [0; 8]; bytes[..4].copy_from_slice(&reference.index.to_le_bytes()); bytes[4..].copy_from_slice(&reference.generation.to_le_bytes()); xxh64(&bytes) as i64 }) } #[cfg(test)] mod tests { use super::*; #[test] fn matches_all_independent_c_reference_vectors() { let mut count = 0; for row in include_str!("../tests/fixtures/hash-conformance.tsv").lines().skip(1) { let f: Vec<_> = row.split('\t').collect(); let expected = u64::from_str_radix(f[2], 16).unwrap(); let actual = match f[0] { "bytes" | "string" => { let bytes: Vec<_> = (0..f[1].len()) .step_by(2) .map(|i| u8::from_str_radix(&f[1][i..i + 2], 16).unwrap()) .collect(); xxh64(&bytes) } "i64" => xxh64(&f[1].parse::().unwrap().to_le_bytes()), "float" => { let bits = u64::from_str_radix(f[1], 16).unwrap(); let bits = if f64::from_bits(bits) == 0.0 { 0 } else { bits }; xxh64(&bits.to_le_bytes()) } "reference" => { let (index, generation) = f[1].split_once(',').unwrap(); reference(Some(HeapRef { index: index.parse().unwrap(), generation: generation.parse().unwrap(), })) as u64 } "null" => reference(None) as u64, kind => panic!("unknown vector {kind}"), }; assert_eq!(actual, expected, "{row}"); count += 1; } assert_eq!(count, 118); } #[test] fn sequential_handles_spread_and_bucket_collisions_do_not_imply_identity() { let mut buckets = [0; 256]; let mut first = [None; 256]; let mut collisions = 0; for index in 1..=16384 { let handle = HeapRef { index, generation: 1 }; let bucket = reference(Some(handle)) as u64 as usize & 255; buckets[bucket] += 1; if let Some(previous) = first[bucket] { assert_ne!(previous, handle); collisions += 1; } else { first[bucket] = Some(handle); } } assert!(buckets.iter().all(|&count| (32..=100).contains(&count)), "{buckets:?}"); assert_eq!(collisions, 16384 - 256); } }