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// Multiple tests to compare delegation blake with external implementation.
26// To run - please execute the run_tests inside the main workload method.
27// Then compile the zksync_os (dump_bin.sh) - and run it (cargo test from zksync_os_runner)
28#[cfg(feature = "blake2s_tests")]
29pub mod blake2s_tests {
30    pub fn run_tests() {
31        test_empty();
32        test_single_byte();
33        test_one_different_byte();
34        test_single_input();
35        test_increasing();
36        test_large();
37    }
38
39    // Compare delegated vs external implementations
40    #[track_caller]
41    fn compare_blakes(input: &[u8]) {
42        use crate::MiniDigest;
43        let output = crate::blake2s::Blake2s256::digest(&input);
44        use crate::blake2_ext::Digest;
45        let expected = crate::blake2_ext::Blake2s256::digest(&input);
46        assert_eq!(&output, &*expected);
47    }
48
49    pub fn test_empty() {
50        let input = [];
51        compare_blakes(&input);
52    }
53
54    pub fn test_single_byte() {
55        let input = [1];
56        compare_blakes(&input);
57    }
58
59    pub fn test_one_different_byte() {
60        for i in 0..255u8 {
61            let mut input = [0u8; 256];
62            input[42] = i;
63            compare_blakes(&input);
64        }
65    }
66
67    pub fn test_single_input() {
68        let input = [0, 1, 2, 3, 4, 5];
69        compare_blakes(&input);
70    }
71
72    pub fn test_increasing() {
73        let mut input = [0u8; 200];
74        for i in 0..200 {
75            input[i] = i as u8;
76            compare_blakes(&input[0..i]);
77        }
78    }
79
80    pub fn test_large() {
81        let mut input = [0u8; 20_000];
82        for i in 0..20_000 {
83            input[i] = i as u8;
84        }
85        compare_blakes(&input);
86    }
87}
88
89#[cfg(test)]
90mod test;