libcrux/jasmin/
sha3.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::digest::{
    digest_size,
    Algorithm::{Sha3_224, Sha3_256, Sha3_384, Sha3_512},
};

#[cfg(simd256)]
macro_rules! sha3_simd256 {
    ($name:ident, $alg:expr, $avx2_fun:expr, $ref_fun:expr) => {
        pub fn $name(input: &[u8]) -> [u8; digest_size($alg)] {
            let mut digest = [0u8; digest_size($alg)];
            let r = if libcrux_platform::simd256_support() {
                unsafe {
                    $avx2_fun(
                        digest.as_mut_ptr(),
                        input.as_ptr() as _,
                        input.len().try_into().unwrap(),
                    )
                }
            } else {
                unsafe {
                    $ref_fun(
                        digest.as_mut_ptr(),
                        input.as_ptr() as _,
                        input.len().try_into().unwrap(),
                    )
                }
            };
            if r != 0 {
                [0u8; digest_size($alg)]
            } else {
                digest
            }
        }
    };
}

#[cfg(simd256)]
sha3_simd256!(
    sha3_224,
    Sha3_224,
    libjade_sys::jade_hash_sha3_224_amd64_avx2,
    libjade_sys::jade_hash_sha3_224_amd64_ref
);

#[cfg(simd256)]
sha3_simd256!(
    sha3_256,
    Sha3_256,
    libjade_sys::jade_hash_sha3_256_amd64_avx2,
    libjade_sys::jade_hash_sha3_256_amd64_ref
);

#[cfg(simd256)]
sha3_simd256!(
    sha3_384,
    Sha3_384,
    libjade_sys::jade_hash_sha3_384_amd64_avx2,
    libjade_sys::jade_hash_sha3_384_amd64_ref
);

#[cfg(simd256)]
sha3_simd256!(
    sha3_512,
    Sha3_512,
    libjade_sys::jade_hash_sha3_512_amd64_avx2,
    libjade_sys::jade_hash_sha3_512_amd64_ref
);

#[cfg(not(simd256))]
macro_rules! sha3_ref {
    ($name:ident, $alg:expr, $ref_fun:expr) => {
        pub fn $name(input: &[u8]) -> [u8; digest_size($alg)] {
            let mut digest = [0u8; digest_size($alg)];
            let r = unsafe {
                $ref_fun(
                    digest.as_mut_ptr(),
                    input.as_ptr() as _,
                    input.len().try_into().unwrap(),
                )
            };
            if r != 0 {
                [0u8; digest_size($alg)]
            } else {
                digest
            }
        }
    };
}

#[cfg(not(simd256))]
sha3_ref!(
    sha3_224,
    Sha3_224,
    libjade_sys::jade_hash_sha3_224_amd64_ref
);

#[cfg(not(simd256))]
sha3_ref!(
    sha3_256,
    Sha3_256,
    libjade_sys::jade_hash_sha3_256_amd64_ref
);

#[cfg(not(simd256))]
sha3_ref!(
    sha3_384,
    Sha3_384,
    libjade_sys::jade_hash_sha3_384_amd64_ref
);

#[cfg(not(simd256))]
sha3_ref!(
    sha3_512,
    Sha3_512,
    libjade_sys::jade_hash_sha3_512_amd64_ref
);