Skip to main content

airbender_crypto/blake2s/
mod.rs

1#[cfg(not(any(
2    all(feature = "single_round_with_control", target_arch = "riscv32"),
3    feature = "proving"
4)))]
5mod naive;
6
7#[cfg(not(any(
8    all(feature = "single_round_with_control", target_arch = "riscv32"),
9    feature = "proving"
10)))]
11pub use naive::Blake2s256;
12
13#[cfg(any(
14    all(feature = "single_round_with_control", target_arch = "riscv32"),
15    feature = "proving"
16))]
17mod delegated_extended;
18
19#[cfg(any(
20    all(feature = "single_round_with_control", target_arch = "riscv32"),
21    feature = "proving"
22))]
23pub use delegated_extended::{initialize_blake2s_delegation_context, Blake2s256};
24
25#[cfg(feature = "single_round_with_control")]
26mod path_hasher;
27
28#[cfg(feature = "single_round_with_control")]
29pub use path_hasher::Blake2sPathHasher;
30
31// Multiple tests to compare delegation blake with external implementation.
32// To run - please execute the run_tests inside the main workload method.
33// Then compile the zksync_os (dump_bin.sh) - and run it (cargo test from zksync_os_runner)
34#[cfg(feature = "blake2s_tests")]
35pub mod blake2s_tests {
36    pub fn run_tests() {
37        test_empty();
38        test_single_byte();
39        test_one_different_byte();
40        test_single_input();
41        test_increasing();
42        test_large();
43    }
44
45    // Compare delegated vs external implementations
46    #[track_caller]
47    fn compare_blakes(input: &[u8]) {
48        use crate::MiniDigest;
49        let output = crate::blake2s::Blake2s256::digest(&input);
50        use crate::blake2_ext::Digest;
51        let expected = crate::blake2_ext::Blake2s256::digest(&input);
52        assert_eq!(&output, &*expected);
53    }
54
55    pub fn test_empty() {
56        let input = [];
57        compare_blakes(&input);
58    }
59
60    pub fn test_single_byte() {
61        let input = [1];
62        compare_blakes(&input);
63    }
64
65    pub fn test_one_different_byte() {
66        for i in 0..255u8 {
67            let mut input = [0u8; 256];
68            input[42] = i;
69            compare_blakes(&input);
70        }
71    }
72
73    pub fn test_single_input() {
74        let input = [0, 1, 2, 3, 4, 5];
75        compare_blakes(&input);
76    }
77
78    pub fn test_increasing() {
79        let mut input = [0u8; 200];
80        for i in 0..200 {
81            input[i] = i as u8;
82            compare_blakes(&input[0..i]);
83        }
84    }
85
86    pub fn test_large() {
87        let mut input = [0u8; 20_000];
88        for i in 0..20_000 {
89            input[i] = i as u8;
90        }
91        compare_blakes(&input);
92    }
93}
94
95#[cfg(test)]
96mod test;