blake3_servil_core/convenience.rs
1//! The convenience API: one call per hash, every feature of BLAKE3 (the
2//! plain hash, the keyed hash, key derivation, extended output), any
3//! slice, the result returned, and a [`Hasher`] for a message fed in
4//! pieces, on the calling thread. It does without what makes hashing fast:
5//! other cores, batches, streams. Those are the
6//! [`throughput`](crate::throughput) calls, for power users.
7
8pub use crate::incremental::Hasher;
9
10use crate::{Hash, IV, Mode, OutputReader};
11
12/// The hash of `input`, on the calling thread.
13///
14/// ```
15/// # blake3_servil_core::initialize();
16/// let hash = blake3_servil_core::convenience::hash(b"foo");
17/// # let hash1 = hash;
18///
19/// let mut hasher = blake3_servil_core::convenience::Hasher::new(blake3_servil_core::Mode::Plain, None);
20/// hasher.update(b"foo");
21/// let hash = hasher.finalize();
22/// # let hash2 = hash;
23/// # assert_eq!(hash1, hash2);
24/// ```
25///
26/// For a keyed hash or key derivation, see [`hash_with`]; for output sizes
27/// other than 32 bytes, [`hash_xof`]. For speed (other cores, batches,
28/// streams, a message fed in pieces), the [`throughput`](crate::throughput)
29/// calls.
30///
31/// **Speed**, nonstop on an Apple M4 Max: about 55 ns for 64 bytes, 0.8 µs
32/// for 1 KiB, 4 GB/s at 16 KiB, and 6 GB/s from 1 MiB. A call after a pause
33/// or after other work takes longer (its code and data have left the
34/// CPU's caches): 64 bytes then take about 0.5 to 0.6 µs. On Apple M4 and
35/// later, threads that hash inputs of 16 KiB or more at the same moment
36/// share one accelerator: one runs at full speed and the others at about
37/// two thirds of it.
38///
39/// Call [`initialize`](crate::initialize) once, before the first hash in
40/// a process: it runs the self-test (see the crate's documentation), under
41/// 0.2 ms. A hash before it panics.
42// Inlined into its caller: its one call, out of line, sat apart from
43// hash_serial, one more code line to fetch from DRAM in a call after a pause
44// (i7-12700K, after idling, 64 B: -16.5%, 8 fresh layouts a side).
45#[inline]
46pub fn hash(input: &[u8]) -> Hash {
47 crate::hash_serial_cold(input, IV, 0)
48}
49
50/// [`hash`] in any [`Mode`]: the plain hash, the keyed hash, or key
51/// derivation.
52///
53/// ```
54/// # blake3_servil_core::initialize();
55/// use blake3_servil_core::{Mode, convenience};
56/// let key = [7u8; 32];
57/// let mac = convenience::hash_with(Mode::Keyed(&key), b"foo");
58/// let mut hasher = blake3_servil_core::convenience::Hasher::new(Mode::Keyed(&key), None);
59/// hasher.update(b"foo");
60/// assert_eq!(mac, hasher.finalize());
61/// ```
62#[inline]
63pub fn hash_with(mode: Mode, input: &[u8]) -> Hash {
64 let mut room = core::mem::MaybeUninit::uninit();
65 let (key, flags) = mode.key(&mut room);
66 crate::hash_serial_cold(input, key, flags)
67}
68
69/// The extended output of `input` in `mode`: an [`OutputReader`] that
70/// gives as many bytes as the program reads (the first 32 are
71/// [`hash_with`]'s).
72///
73/// ```
74/// # blake3_servil_core::initialize();
75/// use blake3_servil_core::{Mode, convenience};
76/// let mut out = [0u8; 100];
77/// convenience::hash_xof(Mode::Plain, b"foo").fill(&mut out);
78/// assert_eq!(&out[..32], convenience::hash(b"foo").as_bytes());
79/// ```
80pub fn hash_xof(mode: Mode, input: &[u8]) -> OutputReader {
81 let mut hasher = crate::Hasher::new(mode, None);
82 hasher.update(input);
83 hasher.finalize_xof()
84}