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
9// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
10#[cfg(not(target_arch = "spirv"))]
11mod const_fn;
12// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
13#[cfg(not(target_arch = "spirv"))]
14mod platform;
15mod portable;
16mod single_block;
17// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
18#[cfg(not(target_arch = "spirv"))]
19mod single_chunk;
20
21// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
22#[cfg(not(target_arch = "spirv"))]
23pub use const_fn::{const_derive_key, const_hash, const_keyed_hash};
24// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
25#[cfg(not(target_arch = "spirv"))]
26pub use platform::{le_bytes_from_words_32, words_from_le_bytes_32, words_from_le_bytes_64};
27pub use single_block::single_block_hash_portable_words;
28// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
29#[cfg(not(target_arch = "spirv"))]
30pub use single_block::{
31    single_block_derive_key, single_block_hash, single_block_hash_many_exact,
32    single_block_keyed_hash, single_block_keyed_hash_many_exact,
33};
34// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
35#[cfg(not(target_arch = "spirv"))]
36pub use single_chunk::{single_chunk_derive_key, single_chunk_hash, single_chunk_keyed_hash};
37
38/// The number of bytes in a hash
39pub const OUT_LEN: usize = 32;
40/// The number of bytes in a key
41pub const KEY_LEN: usize = 32;
42/// The number of bytes in a block
43pub const BLOCK_LEN: usize = 64;
44
45/// The number of bytes in a chunk, 1024.
46///
47/// You don't usually need to think about this number, but it often comes up in benchmarks, because
48/// the maximum degree of parallelism used by the implementation equals the number of chunks.
49// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
50#[cfg(not(target_arch = "spirv"))]
51pub const CHUNK_LEN: usize = 1024;
52
53// While iterating the compression function within a chunk, the CV is
54// represented as words, to avoid doing two extra endianness conversions for
55// each compression in the portable implementation. But the hash_many interface
56// needs to hash both input bytes and parent nodes, so its better for its
57// output CVs to be represented as bytes.
58type CVWords = [u32; 8];
59// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
60#[cfg(not(target_arch = "spirv"))]
61type CVBytes = [u8; 32]; // little-endian
62
63// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
64#[cfg(not(target_arch = "spirv"))]
65type BlockBytes = [u8; BLOCK_LEN];
66type BlockWords = [u32; 16];
67
68const IV: &CVWords = &[
69    0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
70];
71
72const MSG_SCHEDULE: [[usize; 16]; 7] = [
73    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
74    [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8],
75    [3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1],
76    [10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6],
77    [12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4],
78    [9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7],
79    [11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13],
80];
81
82// These are the internal flags that we use to domain separate root/non-root,
83// chunk/parent, and chunk beginning/middle/end. These get set at the high end
84// of the block flags word in the compression function, so their values start
85// high and go down.
86const CHUNK_START: u8 = 1 << 0;
87const CHUNK_END: u8 = 1 << 1;
88// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
89#[cfg(not(target_arch = "spirv"))]
90const PARENT: u8 = 1 << 2;
91const ROOT: u8 = 1 << 3;
92// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
93#[cfg(not(target_arch = "spirv"))]
94const KEYED_HASH: u8 = 1 << 4;
95// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
96#[cfg(not(target_arch = "spirv"))]
97const DERIVE_KEY_CONTEXT: u8 = 1 << 5;
98// TODO: Workaround for https://github.com/Rust-GPU/rust-gpu/issues/312
99#[cfg(not(target_arch = "spirv"))]
100const DERIVE_KEY_MATERIAL: u8 = 1 << 6;