ab_blake3/
lib.rs

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