| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| //! Verified non-generic hashing primitives (`vx_core::hash`). | |
| //! | |
| //! Re-exports zero-cost [`Hash`], [`Hasher`], and [`BuildHasher`] traits from | |
| //! `::core::hash` and implements verified non-generic [`FnvHasher`] (64-bit | |
| //! FNV-1a) and [`SipHasher13`] (SipHash-1-3). | |
| #![forbid(unsafe_code)] | |
| pub use ::core::hash::{BuildHasher, Hash, Hasher}; | |
| /// 64-bit FNV-1a offset basis. | |
| pub const FNV1A_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325; | |
| /// 64-bit FNV-1a prime. | |
| pub const FNV1A_PRIME: u64 = 0x0000_0100_0000_01b3; | |
| const SIP_INIT_V0: u64 = 0x736f_6d65_7073_6575; | |
| const SIP_INIT_V1: u64 = 0x646f_7261_6e64_6f6d; | |
| const SIP_INIT_V2: u64 = 0x6c79_6765_6e65_7261; | |
| const SIP_INIT_V3: u64 = 0x7465_6462_7974_6573; | |
| /// Compute the 64-bit FNV-1a hash of `bytes`. | |
| #[curry::verified] | |
| pub fn fnv1a_hash(bytes: &[u8]) -> u64 { | |
| let mut hasher = FnvHasher::new(); | |
| hasher.write_bytes(bytes); | |
| hasher.finish_hash() | |
| } | |
| /// Deterministic 64-bit FNV-1a streaming hasher. | |
| #[repr(transparent)] | |
| #[derive(Clone, PartialEq, Eq)] | |
| pub struct FnvHasher { | |
| state: u64, | |
| } | |
| const _: () = assert!(::core::mem::size_of::<FnvHasher>() == 8); | |
| const _: () = assert!(::core::mem::align_of::<FnvHasher>() == 8); | |
| const _: () = assert!(::core::mem::offset_of!(FnvHasher, state) == 0); | |
| impl FnvHasher { | |
| /// Create a new FNV-1a hasher initialized with [`FNV1A_OFFSET_BASIS`]. | |
| #[inline(always)] | |
| pub const fn new() -> Self { | |
| Self { state: FNV1A_OFFSET_BASIS } | |
| } | |
| /// Create an FNV-1a hasher initialized with a caller-provided seed. | |
| #[inline(always)] | |
| pub const fn with_seed(seed: u64) -> Self { | |
| Self { state: seed } | |
| } | |
| /// Absorb `bytes` into the FNV-1a state. | |
| #[curry::verified] | |
| pub fn write_bytes(&mut self, bytes: &[u8]) { | |
| let mut h = self.state; | |
| let mut i: usize = 0; | |
| while i < bytes.len() { | |
| let Some(&b) = bytes.get(i) else { | |
| break; | |
| }; | |
| h ^= b as u64; | |
| h = h.wrapping_mul(FNV1A_PRIME); | |
| i += 1; | |
| } | |
| self.state = h; | |
| } | |
| /// Return the current 64-bit FNV-1a hash value. | |
| #[inline(always)] | |
| pub const fn finish_hash(&self) -> u64 { | |
| self.state | |
| } | |
| } | |
| impl Default for FnvHasher { | |
| #[inline(always)] | |
| fn default() -> Self { | |
| Self::new() | |
| } | |
| } | |
| impl Hasher for FnvHasher { | |
| #[inline(always)] | |
| fn finish(&self) -> u64 { | |
| self.finish_hash() | |
| } | |
| #[inline(always)] | |
| fn write(&mut self, bytes: &[u8]) { | |
| self.write_bytes(bytes); | |
| } | |
| } | |
| /// Internal 256-bit SipHash permutation state. | |
| #[repr(C)] | |
| #[derive(Copy, Clone, PartialEq, Eq)] | |
| struct SipState { | |
| v0: u64, | |
| v1: u64, | |
| v2: u64, | |
| v3: u64, | |
| } | |
| const _: () = assert!(::core::mem::size_of::<SipState>() == 32); | |
| const _: () = assert!(::core::mem::align_of::<SipState>() == 8); | |
| const _: () = assert!(::core::mem::offset_of!(SipState, v0) == 0); | |
| const _: () = assert!(::core::mem::offset_of!(SipState, v1) == 8); | |
| const _: () = assert!(::core::mem::offset_of!(SipState, v2) == 16); | |
| const _: () = assert!(::core::mem::offset_of!(SipState, v3) == 24); | |
| impl SipState { | |
| #[inline(always)] | |
| const fn new(k0: u64, k1: u64) -> Self { | |
| Self { | |
| v0: k0 ^ SIP_INIT_V0, | |
| v1: k1 ^ SIP_INIT_V1, | |
| v2: k0 ^ SIP_INIT_V2, | |
| v3: k1 ^ SIP_INIT_V3, | |
| } | |
| } | |
| /// Perform one SipRound ARX permutation over `(v0, v1, v2, v3)`. | |
| #[curry::verified] | |
| fn sip_round(&mut self) { | |
| self.v0 = self.v0.wrapping_add(self.v1); | |
| self.v1 = self.v1.rotate_left(13); | |
| self.v1 ^= self.v0; | |
| self.v0 = self.v0.rotate_left(32); | |
| self.v2 = self.v2.wrapping_add(self.v3); | |
| self.v3 = self.v3.rotate_left(16); | |
| self.v3 ^= self.v2; | |
| self.v0 = self.v0.wrapping_add(self.v3); | |
| self.v3 = self.v3.rotate_left(21); | |
| self.v3 ^= self.v0; | |
| self.v2 = self.v2.wrapping_add(self.v1); | |
| self.v1 = self.v1.rotate_left(17); | |
| self.v1 ^= self.v2; | |
| self.v2 = self.v2.rotate_left(32); | |
| } | |
| /// Compress one 64-bit message block `m` using SipHash-1-x (1 compression round). | |
| #[inline(always)] | |
| fn compress_block(&mut self, m: u64) { | |
| self.v3 ^= m; | |
| self.sip_round(); | |
| self.v0 ^= m; | |
| } | |
| } | |
| /// Keyed streaming SipHash-1-3 hasher (Aumasson & Bernstein, matching Rust's | |
| /// `SipHasher13` specification). | |
| #[repr(C)] | |
| #[derive(Clone, PartialEq, Eq)] | |
| pub struct SipHasher13 { | |
| state: SipState, | |
| tail: u64, | |
| ntail: usize, | |
| length: usize, | |
| } | |
| const _: () = assert!(::core::mem::size_of::<SipHasher13>() == 56); | |
| const _: () = assert!(::core::mem::align_of::<SipHasher13>() == 8); | |
| const _: () = assert!(::core::mem::offset_of!(SipHasher13, state) == 0); | |
| const _: () = assert!(::core::mem::offset_of!(SipHasher13, tail) == 32); | |
| const _: () = assert!(::core::mem::offset_of!(SipHasher13, ntail) == 40); | |
| const _: () = assert!(::core::mem::offset_of!(SipHasher13, length) == 48); | |
| impl SipHasher13 { | |
| /// Create a new `SipHasher13` initialized with keys `(0, 0)`. | |
| #[inline(always)] | |
| pub const fn new() -> Self { | |
| Self::new_with_keys(0, 0) | |
| } | |
| /// Create a new `SipHasher13` initialized with 128-bit key `(k0, k1)`. | |
| #[inline(always)] | |
| pub const fn new_with_keys(k0: u64, k1: u64) -> Self { | |
| Self { state: SipState::new(k0, k1), tail: 0, ntail: 0, length: 0 } | |
| } | |
| /// Absorb `bytes` into the SipHash-1-3 streaming state. | |
| #[curry::verified] | |
| pub fn write_bytes(&mut self, bytes: &[u8]) { | |
| self.length = self.length.wrapping_add(bytes.len()); | |
| let mut i: usize = 0; | |
| while i < bytes.len() { | |
| let Some(&b) = bytes.get(i) else { | |
| break; | |
| }; | |
| let shift = (self.ntail & 7) * 8; | |
| self.tail |= (b as u64) << shift; | |
| self.ntail += 1; | |
| if self.ntail == 8 { | |
| let block = self.tail; | |
| self.state.compress_block(block); | |
| self.tail = 0; | |
| self.ntail = 0; | |
| } | |
| i += 1; | |
| } | |
| } | |
| /// Finalize and return the 64-bit SipHash-1-3 digest without mutating `self`. | |
| #[curry::verified] | |
| pub fn finish_hash(&self) -> u64 { | |
| let mut state = self.state; | |
| let last_block = (((self.length & 0xff) as u64) << 56) | self.tail; | |
| state.compress_block(last_block); | |
| state.v2 ^= 0xff; | |
| state.sip_round(); | |
| state.sip_round(); | |
| state.sip_round(); | |
| state.v0 ^ state.v1 ^ state.v2 ^ state.v3 | |
| } | |
| } | |
| impl Default for SipHasher13 { | |
| #[inline(always)] | |
| fn default() -> Self { | |
| Self::new() | |
| } | |
| } | |
| impl Hasher for SipHasher13 { | |
| #[inline(always)] | |
| fn finish(&self) -> u64 { | |
| self.finish_hash() | |
| } | |
| #[inline(always)] | |
| fn write(&mut self, bytes: &[u8]) { | |
| self.write_bytes(bytes); | |
| } | |
| } | |
| /// One-shot SipHash-1-3 convenience helper over `bytes` with key `(k0, k1)`. | |
| #[inline(always)] | |
| pub fn siphash13(k0: u64, k1: u64, bytes: &[u8]) -> u64 { | |
| let mut hasher = SipHasher13::new_with_keys(k0, k1); | |
| hasher.write_bytes(bytes); | |
| hasher.finish_hash() | |
| } |