Skip to main content

rushdb_types/
hash.rs

1/// Hash domain separator for the explicit-null value marker.
2pub const NULL_HASH_DOMAIN: &[u8] = b"rushdb:explicit-null:v1";
3
4fn prefix64(bytes: &[u8; 32]) -> u64 {
5    let mut prefix = [0u8; 8];
6    prefix.copy_from_slice(&bytes[..8]);
7    u64::from_be_bytes(prefix)
8}
9
10/// Stable 64-bit digest of arbitrary bytes, independent of process state.
11#[must_use]
12pub fn stable_hash64(bytes: &[u8]) -> u64 {
13    prefix64(blake3::hash(bytes).as_bytes())
14}
15
16/// Domain-separated 64-bit hash of a type tag and payload.
17#[must_use]
18pub fn hash_domain(domain: &[u8], ty: u8, payload: &[u8]) -> u64 {
19    let mut hasher = blake3::Hasher::new();
20    hasher.update(domain);
21    hasher.update(&[ty]);
22    hasher.update(payload);
23    prefix64(hasher.finalize().as_bytes())
24}
25
26/// Hash of the payload-free explicit-null marker.
27#[must_use]
28pub fn null_value_hash() -> u64 {
29    stable_hash64(NULL_HASH_DOMAIN)
30}
31
32/// Value hash for a canonical payload under the given type tag.
33#[must_use]
34pub fn value_hash(ty: u8, canonical_payload: &[u8]) -> u64 {
35    hash_domain(b"rushdb:value:v1", ty, canonical_payload)
36}