p3_blake3/
lib.rs

1//! The blake3 hash function.
2
3#![no_std]
4
5use p3_symmetric::CryptographicHasher;
6
7/// The blake3 hash function.
8#[derive(Copy, Clone, Debug)]
9pub struct Blake3;
10
11impl CryptographicHasher<u8, [u8; 32]> for Blake3 {
12    fn hash_iter<I>(&self, input: I) -> [u8; 32]
13    where
14        I: IntoIterator<Item = u8>,
15    {
16        const BUFLEN: usize = 512; // Tweakable parameter; determined by experiment
17        let mut hasher = blake3::Hasher::new();
18        p3_util::apply_to_chunks::<BUFLEN, _, _>(input, |buf| {
19            hasher.update(buf);
20        });
21        hasher.finalize().into()
22    }
23
24    fn hash_iter_slices<'a, I>(&self, input: I) -> [u8; 32]
25    where
26        I: IntoIterator<Item = &'a [u8]>,
27    {
28        let mut hasher = blake3::Hasher::new();
29        for chunk in input.into_iter() {
30            hasher.update(chunk);
31        }
32        hasher.finalize().into()
33    }
34}