Skip to main content

predsync_core/
hash.rs

1//! FNV-1a hashes, message IDs and predictor state hashes.
2
3const FNV1A32_OFFSET: u32 = 0x811C_9DC5;
4const FNV1A32_PRIME: u32 = 0x0100_0193;
5pub const FNV1A64_OFFSET: u64 = 0xCBF2_9CE4_8422_2325;
6const FNV1A64_PRIME: u64 = 0x0000_0100_0000_01B3;
7
8/// FNV-1a over 32-bit words.
9pub fn fnv1a32(data: &[u8]) -> u32 {
10    let mut hash = FNV1A32_OFFSET;
11    for &byte in data {
12        hash = (hash ^ u32::from(byte)).wrapping_mul(FNV1A32_PRIME);
13    }
14    hash
15}
16
17/// Add bytes to an in-progress FNV-1a 64-bit hash.
18pub fn fnv1a64_update(hash: u64, data: &[u8]) -> u64 {
19    let mut hash = hash;
20    for &byte in data {
21        hash = (hash ^ u64::from(byte)).wrapping_mul(FNV1A64_PRIME);
22    }
23    hash
24}
25
26/// FNV-1a over 64-bit words.
27pub fn fnv1a64(data: &[u8]) -> u64 {
28    fnv1a64_update(FNV1A64_OFFSET, data)
29}
30
31/// Fold a qualified message name into the one-byte wire message ID.
32pub fn message_id(qualified_name: &str) -> u8 {
33    let hash = fnv1a32(qualified_name.as_bytes());
34    (hash ^ (hash >> 8) ^ (hash >> 16) ^ (hash >> 24)) as u8
35}
36
37/// State hash over an ordered predictor window, oldest entry first.
38pub fn state_hash(samples: &[(u64, f64)]) -> u64 {
39    state_hash_u64_pair_slice(samples)
40}
41
42/// State hash over an ordered predictor window, oldest entry first.
43pub fn state_hash_u64_pair_slice(samples: &[(u64, f64)]) -> u64 {
44    let mut hash = fnv1a64_update(FNV1A64_OFFSET, &(samples.len() as u64).to_le_bytes());
45    for &(t, x) in samples {
46        hash = fnv1a64_update(hash, &t.to_le_bytes());
47        hash = fnv1a64_update(hash, &x.to_bits().to_le_bytes());
48    }
49    hash
50}
51
52/// State hash for an EWMA predictor.
53///
54/// The byte order is sample count, alpha bits, last sample tick and smoothed
55/// level bits, all little-endian. The Python reference model uses the same
56/// order so state hashes agree across implementations.
57pub fn ewma_state_hash(alpha: f64, last_t: u64, count: u64, level: f64) -> u64 {
58    let mut hash = fnv1a64_update(FNV1A64_OFFSET, &count.to_le_bytes());
59    hash = fnv1a64_update(hash, &alpha.to_bits().to_le_bytes());
60    hash = fnv1a64_update(hash, &last_t.to_le_bytes());
61    fnv1a64_update(hash, &level.to_bits().to_le_bytes())
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn fnv1a32_known_vectors() {
70        let cases: &[(&[u8], u32)] = &[
71            (b"", 0x811C_9DC5),
72            (b"a", 0xE40C_292C),
73            (b"foobar", 0xBF9C_F968),
74            (b"predsync", 0xB9CC_3325),
75            (b"Pose", 0xDFC7_76A4),
76        ];
77        for (data, expected) in cases {
78            assert_eq!(fnv1a32(data), *expected, "data {data:02x?}");
79        }
80    }
81
82    #[test]
83    fn message_id_fold_matches_vectors() {
84        let cases = [
85            ("pose", 0xC4),
86            ("demo.Pose", 0x51),
87            ("single_field_linear_startup", 0xA7),
88            ("diff", 0x82),
89        ];
90        for (name, expected) in cases {
91            assert_eq!(message_id(name), expected, "name {name}");
92        }
93    }
94
95    #[test]
96    fn fnv1a64_state_hash_matches_reference() {
97        let samples = [(0, 0.0f64), (1, 1.0), (2, 4.0)];
98        assert_eq!(state_hash(&samples), 0x7224_713F_CF5D_5E24);
99        assert_eq!(state_hash(&[]), 0xA8C7_F832_281A_39C5);
100    }
101}