Skip to main content

ab_blake3/
lib.rs

1//! Optimized and more exotic APIs around BLAKE3: `const fn` and GPU-friendly ([rust-gpu])
2//!
3//! [rust-gpu]: https://github.com/rust-gpu/rust-gpu
4//!
5//! Does not require a standard library (`no_std`) or an allocator.
6
7#![no_std]
8
9mod const_fn;
10mod platform;
11mod portable;
12mod single_block;
13mod single_chunk;
14
15pub use const_fn::{const_derive_key, const_hash, const_keyed_hash};
16pub use platform::{le_bytes_from_words_32, words_from_le_bytes_32, words_from_le_bytes_64};
17pub use single_block::{
18    single_block_derive_key, single_block_hash, single_block_hash_many_exact,
19    single_block_hash_portable_words, single_block_keyed_hash, single_block_keyed_hash_many_exact,
20};
21pub use single_chunk::{single_chunk_derive_key, single_chunk_hash, single_chunk_keyed_hash};
22
23/// The number of bytes in a hash
24pub const OUT_LEN: usize = 32;
25/// The number of bytes in a key
26pub const KEY_LEN: usize = 32;
27/// The number of bytes in a block
28pub const BLOCK_LEN: usize = 64;
29
30/// The number of bytes in a chunk, 1024.
31///
32/// You don't usually need to think about this number, but it often comes up in benchmarks, because
33/// the maximum degree of parallelism used by the implementation equals the number of chunks.
34pub const CHUNK_LEN: usize = 1024;
35
36// While iterating the compression function within a chunk, the CV is
37// represented as words, to avoid doing two extra endianness conversions for
38// each compression in the portable implementation. But the hash_many interface
39// needs to hash both input bytes and parent nodes, so its better for its
40// output CVs to be represented as bytes.
41type CVWords = [u32; 8];
42type CVBytes = [u8; 32]; // little-endian
43
44type BlockBytes = [u8; BLOCK_LEN];
45type BlockWords = [u32; 16];
46
47const IV: &CVWords = &[
48    0x6A09_E667,
49    0xBB67_AE85,
50    0x3C6E_F372,
51    0xA54F_F53A,
52    0x510E_527F,
53    0x9B05_688C,
54    0x1F83_D9AB,
55    0x5BE0_CD19,
56];
57
58const MSG_SCHEDULE: [[usize; 16]; 7] = [
59    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
60    [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8],
61    [3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1],
62    [10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6],
63    [12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4],
64    [9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7],
65    [11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13],
66];
67
68// These are the internal flags that we use to domain separate root/non-root,
69// chunk/parent, and chunk beginning/middle/end. These get set at the high end
70// of the block flags word in the compression function, so their values start
71// high and go down.
72const CHUNK_START: u8 = 1 << 0;
73const CHUNK_END: u8 = 1 << 1;
74const PARENT: u8 = 1 << 2;
75const ROOT: u8 = 1 << 3;
76const KEYED_HASH: u8 = 1 << 4;
77const DERIVE_KEY_CONTEXT: u8 = 1 << 5;
78const DERIVE_KEY_MATERIAL: u8 = 1 << 6;