Skip to content

Commit e5fb6c9

Browse files
mkannwischerhanno-becker
authored andcommitted
AArch64: Add MLK_SYS_AARCH64_FAST_SHA3 for fast SHA3 CPUs
Introduce MLK_SYS_AARCH64_FAST_SHA3 to signal AArch64 CPUs that implement SHA3 instructions on all Neon execution units. When set, the FIPS202 backend auto-selection picks the pure-Neon v84a Keccak-f1600 and Keccak-f1600x2 implementations, matching the existing Apple-CPU path. Set the flag for the new Graviton5 (c9g) benchmark entry. - Resolves #1783 Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
1 parent f85c1c4 commit e5fb6c9

4 files changed

Lines changed: 32 additions & 25 deletions

File tree

.github/workflows/bench.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ jobs:
130130
- name: Graviton5
131131
ec2_instance_type: c9g.medium
132132
ec2_ami: ubuntu-latest (aarch64)
133-
archflags: -march=armv9-a+sha3
133+
archflags: -march=armv9-a+sha3 -DMLK_SYS_AARCH64_FAST_SHA3
134134
cflags: "-flto -DMLK_FORCE_AARCH64"
135135
ldflags: "-flto"
136136
perf: PERF

dev/fips202/aarch64/auto.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
/*
2525
* Keccak-f1600
2626
*
27-
* - On Arm-based Apple CPUs, we pick a pure Neon implementation.
27+
* - On Arm-based Apple CPUs, or if MLK_SYS_AARCH64_FAST_SHA3 is set,
28+
* we pick a pure Neon implementation.
2829
* - Otherwise, unless MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER is set,
2930
* we use lazy-rotation scalar assembly from @[HYBRID].
3031
* - Otherwise, if MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER is set, we
3132
* fall back to the standard C implementation.
3233
*/
33-
#if defined(__ARM_FEATURE_SHA3) && defined(__APPLE__)
34+
#if defined(__ARM_FEATURE_SHA3) && \
35+
(defined(__APPLE__) || defined(MLK_SYS_AARCH64_FAST_SHA3))
3436
#include "x1_v84a.h"
3537
#elif !defined(MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER)
3638
#include "x1_scalar.h"
@@ -42,20 +44,21 @@
4244
* The optimal implementation is highly CPU-specific; see @[HYBRID].
4345
*
4446
* For now, if v8.4-A is not implemented, we fall back to Keccak-f1600.
45-
* If v8.4-A is implemented and we are on an Apple CPU, we use a plain
46-
* Neon-based implementation.
47-
* If v8.4-A is implemented and we are not on an Apple CPU, we use a
48-
* scalar/Neon/Neon hybrid.
49-
* The reason for this distinction is that Apple CPUs appear to implement
50-
* the SHA3 instructions on all SIMD units, while Arm CPUs prior to Cortex-X4
51-
* don't, and ordinary Neon instructions are still needed.
47+
* If v8.4-A is implemented and we are on an Apple CPU or
48+
* MLK_SYS_AARCH64_FAST_SHA3 is set, we use a plain Neon-based
49+
* implementation.
50+
* Otherwise, if v8.4-A is implemented, we use a scalar/Neon/Neon hybrid.
51+
* The reason for this distinction is that Apple CPUs (and CPUs flagged with
52+
* MLK_SYS_AARCH64_FAST_SHA3) implement the SHA3 instructions on all SIMD
53+
* units, while Arm CPUs prior to Cortex-X4 don't, and ordinary Neon
54+
* instructions are still needed.
5255
*/
5356
#if defined(__ARM_FEATURE_SHA3)
5457
/*
55-
* For Apple-M cores, we use a plain implementation leveraging SHA3
56-
* instructions only.
58+
* For Apple-M cores (and CPUs flagged with MLK_SYS_AARCH64_FAST_SHA3), we
59+
* use a plain implementation leveraging SHA3 instructions only.
5760
*/
58-
#if defined(__APPLE__)
61+
#if defined(__APPLE__) || defined(MLK_SYS_AARCH64_FAST_SHA3)
5962
#include "x2_v84a.h"
6063
#else
6164
#include "x4_v8a_v84a_scalar.h"

mlkem/src/fips202/native/aarch64/auto.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
/*
2525
* Keccak-f1600
2626
*
27-
* - On Arm-based Apple CPUs, we pick a pure Neon implementation.
27+
* - On Arm-based Apple CPUs, or if MLK_SYS_AARCH64_FAST_SHA3 is set,
28+
* we pick a pure Neon implementation.
2829
* - Otherwise, unless MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER is set,
2930
* we use lazy-rotation scalar assembly from @[HYBRID].
3031
* - Otherwise, if MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER is set, we
3132
* fall back to the standard C implementation.
3233
*/
33-
#if defined(__ARM_FEATURE_SHA3) && defined(__APPLE__)
34+
#if defined(__ARM_FEATURE_SHA3) && \
35+
(defined(__APPLE__) || defined(MLK_SYS_AARCH64_FAST_SHA3))
3436
#include "x1_v84a.h"
3537
#elif !defined(MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER)
3638
#include "x1_scalar.h"
@@ -42,20 +44,21 @@
4244
* The optimal implementation is highly CPU-specific; see @[HYBRID].
4345
*
4446
* For now, if v8.4-A is not implemented, we fall back to Keccak-f1600.
45-
* If v8.4-A is implemented and we are on an Apple CPU, we use a plain
46-
* Neon-based implementation.
47-
* If v8.4-A is implemented and we are not on an Apple CPU, we use a
48-
* scalar/Neon/Neon hybrid.
49-
* The reason for this distinction is that Apple CPUs appear to implement
50-
* the SHA3 instructions on all SIMD units, while Arm CPUs prior to Cortex-X4
51-
* don't, and ordinary Neon instructions are still needed.
47+
* If v8.4-A is implemented and we are on an Apple CPU or
48+
* MLK_SYS_AARCH64_FAST_SHA3 is set, we use a plain Neon-based
49+
* implementation.
50+
* Otherwise, if v8.4-A is implemented, we use a scalar/Neon/Neon hybrid.
51+
* The reason for this distinction is that Apple CPUs (and CPUs flagged with
52+
* MLK_SYS_AARCH64_FAST_SHA3) implement the SHA3 instructions on all SIMD
53+
* units, while Arm CPUs prior to Cortex-X4 don't, and ordinary Neon
54+
* instructions are still needed.
5255
*/
5356
#if defined(__ARM_FEATURE_SHA3)
5457
/*
55-
* For Apple-M cores, we use a plain implementation leveraging SHA3
56-
* instructions only.
58+
* For Apple-M cores (and CPUs flagged with MLK_SYS_AARCH64_FAST_SHA3), we
59+
* use a plain implementation leveraging SHA3 instructions only.
5760
*/
58-
#if defined(__APPLE__)
61+
#if defined(__APPLE__) || defined(MLK_SYS_AARCH64_FAST_SHA3)
5962
#include "x2_v84a.h"
6063
#else
6164
#include "x4_v8a_v84a_scalar.h"

scripts/autogen

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,7 @@ def get_config_options():
28822882
"MLK_FORCE_RISCV64",
28832883
"MLK_FORCE_RISCV32",
28842884
"MLK_SYS_AARCH64_SLOW_BARREL_SHIFTER",
2885+
"MLK_SYS_AARCH64_FAST_SHA3",
28852886
"MLKEM_DEBUG", # TODO: Rename?
28862887
"MLK_BREAK_PCT", # Use in PCT breakage test]
28872888
"MLK_CHECK_APIS",

0 commit comments

Comments
 (0)