Skip to main content

bb_ir/
wire.rs

1//! Wire codec hash helper.
2//!
3//! Type identity for graph values rides on `&'static TypeNode`
4//! (`crate::types`). This module owns the `compute_wire_hash` FNV-1a
5//! helper used to derive stable wire-envelope hashes from a
6//! denotation + version pair. Concrete types stamp the result on
7//! their `TypeNode::wire_hash` field.
8
9/// FNV-1a 64-bit hash of `(denotation, "@", version)`. Available
10/// as a `const fn` so callers can derive stable identifiers at
11/// compile time (e.g. for graph-derived edge identity).
12pub const fn compute_wire_hash(denotation: &str, version: i64) -> u64 {
13    const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
14    const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
15
16    let mut hash = FNV_OFFSET;
17    let bytes = denotation.as_bytes();
18    let mut i = 0;
19    while i < bytes.len() {
20        hash ^= bytes[i] as u64;
21        hash = hash.wrapping_mul(FNV_PRIME);
22        i += 1;
23    }
24    hash ^= b'@' as u64;
25    hash = hash.wrapping_mul(FNV_PRIME);
26
27    let v = version.to_le_bytes();
28    let mut j = 0;
29    while j < v.len() {
30        hash ^= v[j] as u64;
31        hash = hash.wrapping_mul(FNV_PRIME);
32        j += 1;
33    }
34    hash
35}
36