1# SPDX-FileCopyrightText: 2018 Helder Eijs <helderijs@gmail.com>
2# SPDX-License-Identifier: BSD-2-Clause
3
4from __future__ import annotations
5
6import os
7from typing import Callable
8
9from Crypto.Util._raw_api import load_pycryptodome_raw_lib
10
11_raw_cpuid_lib = load_pycryptodome_raw_lib(
12 "Crypto.Util._cpuid_c",
13 """
14 int have_aes_ni(void);
15 int have_clmul(void);
16 int have_avx2(void);
17 int have_bmi1(void);
18 int have_bmi2(void);
19 int have_sha_ni(void);
20 int have_adx(void);
21 int have_aes_armv8(void);
22 int have_pmull(void);
23 int have_sha256_armv8(void);
24 int have_sha512_armv8(void);
25 int have_sha3_armv8(void);
26 """,
27)
28
29
30def have_aes_ni() -> int:
31 return _raw_cpuid_lib.have_aes_ni()
32
33
34def have_clmul() -> int:
35 return _raw_cpuid_lib.have_clmul()
36
37
38def have_avx2() -> int:
39 return _raw_cpuid_lib.have_avx2()
40
41
42def have_bmi1() -> int:
43 return _raw_cpuid_lib.have_bmi1()
44
45
46def have_bmi2() -> int:
47 return _raw_cpuid_lib.have_bmi2()
48
49
50def have_sha_ni() -> int:
51 return _raw_cpuid_lib.have_sha_ni()
52
53
54def have_adx() -> int:
55 return _raw_cpuid_lib.have_adx()
56
57
58def have_aes_armv8() -> int:
59 return _raw_cpuid_lib.have_aes_armv8()
60
61
62def have_pmull() -> int:
63 return _raw_cpuid_lib.have_pmull()
64
65
66def have_sha256_armv8() -> int:
67 return _raw_cpuid_lib.have_sha256_armv8()
68
69
70def have_sha512_armv8() -> int:
71 return _raw_cpuid_lib.have_sha512_armv8()
72
73
74def have_sha3_armv8() -> int:
75 return _raw_cpuid_lib.have_sha3_armv8()
76
77
78def load_optional_lib(name: str, cdecl: str, cpu_supported: Callable[[], int]):
79 """Load the functions in ``cdecl`` from the module ``name``,
80 compiled for instructions that the CPU may not have.
81
82 Return None if ``cpu_supported()`` is false (the CPU does not have them),
83 or if the module was not compiled in.
84 """
85
86 if not cpu_supported():
87 return None
88 try:
89 return load_pycryptodome_raw_lib(name, cdecl)
90 except OSError:
91 return None
92
93
94def load_avx2_bmi2_lib(name: str, cdecl: str):
95 """Load the functions in ``cdecl`` from the module ``name``,
96 compiled for AVX2, BMI1 and BMI2.
97
98 Return None if the CPU does not support all three instruction sets,
99 or if the module was not compiled in.
100 """
101
102 if not (have_avx2() and have_bmi1() and have_bmi2()):
103 return None
104 try:
105 return load_pycryptodome_raw_lib(name, cdecl)
106 except OSError:
107 return None
108
109
110def available_cores() -> int:
111 """Return the number of CPU cores this process can run on."""
112
113 count: int | None
114 # Python 3.13+: it takes into account CPU affinity and -X cpu_count
115 if hasattr(os, "process_cpu_count"):
116 count = os.process_cpu_count()
117 elif hasattr(os, "sched_getaffinity"):
118 count = len(os.sched_getaffinity(0))
119 elif hasattr(os, "cpu_count"):
120 count = os.cpu_count()
121 else:
122 import multiprocessing
123
124 try:
125 count = multiprocessing.cpu_count()
126 except NotImplementedError:
127 count = None
128 return count or 1