p3_blake3/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//! The blake3 hash function.

#![no_std]

use p3_symmetric::CryptographicHasher;

/// The blake3 hash function.
#[derive(Copy, Clone, Debug)]
pub struct Blake3;

impl CryptographicHasher<u8, [u8; 32]> for Blake3 {
    fn hash_iter<I>(&self, input: I) -> [u8; 32]
    where
        I: IntoIterator<Item = u8>,
    {
        const BUFLEN: usize = 512; // Tweakable parameter; determined by experiment
        let mut hasher = blake3::Hasher::new();
        p3_util::apply_to_chunks::<BUFLEN, _, _>(input, |buf| {
            hasher.update(buf);
        });
        hasher.finalize().into()
    }

    fn hash_iter_slices<'a, I>(&self, input: I) -> [u8; 32]
    where
        I: IntoIterator<Item = &'a [u8]>,
    {
        let mut hasher = blake3::Hasher::new();
        for chunk in input.into_iter() {
            hasher.update(chunk);
        }
        hasher.finalize().into()
    }
}