p3_symmetric/
hasher.rs

1/// A generic trait for cryptographic hashers that consume an arbitrary sequence of input items
2/// and produce a fixed-size output.
3///
4/// This trait abstracts over hash functions in a flexible way, supporting both field elements,
5/// scalars, or any other data type that implements `Clone`.
6pub trait CryptographicHasher<Item: Clone, Out>: Clone {
7    /// Hash an iterator of input items.
8    /// # Arguments
9    /// - `input`: An iterator over items to be hashed.
10    ///
11    /// # Returns
12    /// A fixed-size digest of type `Out`.
13    fn hash_iter<I>(&self, input: I) -> Out
14    where
15        I: IntoIterator<Item = Item>;
16
17    /// Hash an iterator of slices, by flattening it into a single stream of items.
18    ///
19    /// # Arguments
20    /// - `input`: An iterator over slices of items to hash.
21    ///
22    /// # Returns
23    /// A fixed-size digest of type `Out`.
24    fn hash_iter_slices<'a, I>(&self, input: I) -> Out
25    where
26        I: IntoIterator<Item = &'a [Item]>,
27        Item: 'a,
28    {
29        self.hash_iter(input.into_iter().flatten().cloned())
30    }
31
32    /// Hash a single slice of items.
33    ///
34    /// # Arguments
35    /// - `input`: A slice of items to hash.
36    ///
37    /// # Returns
38    /// A fixed-size digest of type `Out`.
39    fn hash_slice(&self, input: &[Item]) -> Out {
40        self.hash_iter_slices(core::iter::once(input))
41    }
42
43    /// Hash a single item.
44    ///
45    /// # Arguments
46    /// - `input`: A single item to hash.
47    ///
48    /// # Returns
49    /// A fixed-size digest of type `Out`.
50    fn hash_item(&self, input: Item) -> Out {
51        self.hash_slice(&[input])
52    }
53}