Skip to main content

primitives/hashing/
hashing_utils.rs

1use blake3;
2use hybrid_array::Array;
3
4use crate::{
5    algebra::field::FieldExtension,
6    constants::CollisionResistanceBytes,
7    types::SessionId,
8};
9
10pub type Digest = Array<u8, CollisionResistanceBytes>;
11
12/// A generic trait for hashing with a tag.
13pub trait HashWith: AsRef<[u8]> + From<Digest> {
14    /// Hash the current value with the given tag, returning a new value.
15    fn hash_with(&self, tag: &[u8]) -> Self {
16        hash(&[self.as_ref(), tag]).into()
17    }
18}
19
20impl<T: AsRef<[u8]> + From<Digest>> HashWith for T {}
21
22/// Utility function to hash a list of byte slices into a fixed-size digest. Uses BLAKE3.
23pub fn hash(slices: &[&[u8]]) -> Digest {
24    let mut hasher = blake3::Hasher::new();
25    for slice in slices {
26        hasher.update(slice);
27    }
28    Into::<[u8; 32]>::into(hasher.finalize()).into()
29}
30
31/// Utility function to hash a list of byte slices into the provided output buffer. Uses BLAKE3.
32pub fn hash_into<T: AsRef<[u8]>, I: IntoIterator<Item = T>>(slices: I, out: &mut [u8]) {
33    let mut hasher = blake3::Hasher::new();
34    for slice in slices {
35        hasher.update(slice.as_ref());
36    }
37    hasher.finalize_xof().fill(out.as_mut());
38}
39
40/// Hashes the given session ID and seed into a field element of type F.
41pub fn hash_to_field<T: AsRef<[u8]>, F: FieldExtension>(session_id: &SessionId, seed: &T) -> F {
42    let mut hasher = blake3::Hasher::new();
43    let mut output = Array::<u8, F::UniformBytes>::default();
44
45    hasher.update(session_id.as_ref());
46    hasher.update(seed.as_ref());
47    hasher.finalize_xof().fill(&mut output);
48
49    F::from_uniform_bytes(&output)
50}
51
52/// Utility function to flatten a list of byte slices into a single contiguous vector.
53pub fn flatten_slices<T: AsRef<[u8]>>(slices: &[T]) -> Vec<u8> {
54    let total_len = slices.iter().map(|slice| slice.as_ref().len()).sum();
55
56    let mut flattened = Vec::with_capacity(total_len);
57    slices.iter().for_each(|slice| {
58        flattened.extend_from_slice(slice.as_ref());
59    });
60
61    flattened
62}
63
64#[cfg(test)]
65mod tests {
66    use crate::hashing::hash_into;
67
68    #[test]
69    fn test_hash_into_different_results() {
70        let (mut seed0, mut seed1, mut seed2, mut seed3) = ([0; 16], [0; 16], [0; 16], [0; 16]);
71        hash_into([b"0", b"1"], &mut seed0);
72        hash_into([b"0", b"12".as_slice()], &mut seed1);
73        hash_into([b"01", b"12"], &mut seed2);
74        hash_into([b"01", b"1".as_slice()], &mut seed3);
75
76        assert_ne!(seed0, seed1);
77        assert_ne!(seed0, seed2);
78        assert_ne!(seed0, seed3);
79        assert_ne!(seed1, seed2);
80        assert_ne!(seed1, seed3);
81        assert_ne!(seed2, seed3);
82    }
83}