libcrux_platform/
lib.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! High-level functions to detect available CPU features
//! at runtime on supported processor architectures and
//! operation systems

#![no_std]

// Use std for tests
#[cfg(test)]
#[macro_use]
extern crate std;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod x86;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use x86::{self as cpu_id, Feature};

#[cfg(all(target_arch = "aarch64", target_os = "linux"))]
mod linux_arm;
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
mod macos_arm;

#[cfg(test)]
mod test;

// TODO: Check for z14 or z15
pub fn simd128_support() -> bool {
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    {
        use macos_arm::*;
        adv_simd()
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        cpu_id::supported(Feature::sse2)
            && cpu_id::supported(Feature::sse3)
            && cpu_id::supported(Feature::sse4_1)
            && cpu_id::supported(Feature::avx)
    }

    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    {
        use linux_arm::*;
        adv_simd()
    }

    #[cfg(not(any(
        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
        target_arch = "x86",
        target_arch = "x86_64"
    )))]
    {
        false
    }
}

pub fn simd256_support() -> bool {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    return cpu_id::supported(Feature::avx2);

    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
    false
}

pub fn x25519_support() -> bool {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    return cpu_id::supported(Feature::bmi2) && cpu_id::supported(Feature::adx);

    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
    false
}

pub fn bmi2_adx_support() -> bool {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    return cpu_id::supported(Feature::bmi2) && cpu_id::supported(Feature::adx);

    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
    false
}

/// Check whether p(cl)mull is supported
pub fn pmull_support() -> bool {
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    {
        use crate::macos_arm::*;
        pmull()
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        cpu_id::supported(Feature::pclmulqdq)
    }

    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    {
        use crate::linux_arm::*;
        pmull()
    }

    #[cfg(not(any(
        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
        target_arch = "x86",
        target_arch = "x86_64"
    )))]
    {
        false
    }
}

/// Check whether advanced SIMD features are supported
pub fn adv_simd_support() -> bool {
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    {
        use macos_arm::*;
        adv_simd()
    }

    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    {
        use linux_arm::*;
        adv_simd()
    }

    #[cfg(not(all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos"))))]
    {
        false
    }
}

/// Check whether AES is supported
pub fn aes_ni_support() -> bool {
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    {
        use crate::macos_arm::*;
        aes()
    }

    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    {
        use crate::linux_arm::*;
        aes()
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        cpu_id::supported(Feature::avx)
            && cpu_id::supported(Feature::sse)
            && cpu_id::supported(Feature::aes)
            && cpu_id::supported(Feature::pclmulqdq)
            && cpu_id::supported(Feature::movbe)
    }

    #[cfg(not(any(
        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
        target_arch = "x86",
        target_arch = "x86_64"
    )))]
    {
        false
    }
}

/// Check whether SHA256 is supported
pub fn sha256_support() -> bool {
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    {
        use crate::macos_arm::*;
        sha256()
    }

    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    {
        use crate::linux_arm::*;
        sha256()
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        cpu_id::supported(Feature::sha)
    }

    #[cfg(not(any(
        all(target_arch = "aarch64", any(target_os = "linux", target_os = "macos")),
        target_arch = "x86",
        target_arch = "x86_64"
    )))]
    {
        false
    }
}