commit 5365e8ba2caa7a1810f2e05ee9af90c30a82d6f2
parent c0d81db877f2dca748dcf5ac22da1134a325152f
Author: finwo <finwo@pm.me>
Date: Sun, 11 Oct 2026 04:08:03 +0200
Add arm-sha3 backend
Diffstat:
6 files changed, 486 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
@@ -35,13 +35,15 @@ dep add finwo/keccak-fast
| ---------- | -------- | ----- |
| scalar | yes | — |
| scalar+bmi | yes | — |
+| arm-sha3 | yes | x2 |
| AVX2 | no | x4 |
| AVX-512 | no | x8 |
Backends register from constructors; the highest available priority wins
-(scalar 0, scalar+bmi 10, avx2 20, avx512 30). `kf_backend_name()` and
-`kf_batch_name()` report the active ones. `KECCAK_FAST_NO_BMI`,
-`KECCAK_FAST_NO_AVX2` and `KECCAK_FAST_NO_AVX512` drop those backends.
+(scalar 0, scalar+bmi 10, arm-sha3 10, avx2 20, avx512 30). `kf_backend_name()`
+and `kf_batch_name()` report the active ones. `KECCAK_FAST_NO_BMI`,
+`KECCAK_FAST_NO_AVX2`, `KECCAK_FAST_NO_AVX512` and `KECCAK_FAST_NO_SHA3` drop
+those backends.
The scalar backend is pinned to the x86-64 baseline
(`no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2`) so a consumer's `-march=native`
@@ -51,6 +53,16 @@ locals, fuses theta into the previous round's chi, and unrolls the round loop
permutation with `bmi` enabled, which turns chi's `not`+`and` into `andn`; gcc
also gets `rorx` for the rotates. About 20% faster than the donor.
+`arm-sha3` uses the ARMv8.2 SHA3 instructions (`eor3`, `bcax`, `rax1`, `xar`)
+under a scoped `target("sha3")` region, so no build flags are needed. It is
+self-contained: its own permutation and sponge, nothing shared with the scalar
+backend, and its 25 lanes live in named locals so the register allocator keeps
+them in NEON registers. Availability is checked at runtime only
+(`hw.optional.arm.FEAT_SHA3` on Darwin, `HWCAP_SHA3` on Linux); the x2 batch
+kernel packs two messages one per 64-bit half of each 128-bit register. About
+1.4x scalar for one-shots on Apple silicon and ~2x for the x2 batch; on some
+cores the one-shot lands at parity and the pair kernel carries the win.
+
The AVX2 and AVX-512 batch kernels hold the 4 or 8 messages of a group one per
vector lane, so the 25 state words live in 25 vector registers. The state is
transposed in 8x8 (or 4x4) blocks with unpack and shuffle instructions at the
@@ -61,7 +73,7 @@ the same code as `-O3`.
```
make test # KAT and batch differential (TAP)
-make bench # per-hash table: scalar, scalar+bmi, avx2, avx512 (+ donor)
+make bench # per-hash table: every backend, plus the donor
```
`bench/` fetches the donor via `dep install`. `OPT=-O2|-O3` (arch flags
@@ -71,5 +83,6 @@ rejected), `BENCH_CPU=<n>` selects the core.
See `LICENSE.md`. The sponge is derived from `coruus/keccak-tiny` (David Leon
Gil, CC0); the scalar permutation follows XKCP's public-domain `opt64`
-structure.
+structure. The `arm-sha3` permutation is an independent implementation over
+the ARMv8.2 SHA3 instructions.
diff --git a/bench/bench.c b/bench/bench.c
@@ -179,6 +179,9 @@ int main(void) {
!defined(KECCAK_FAST_NO_AVX512)
batch[nb++] = (batch_t){"avx512 x8", kf_avx512_batch, kf_avx512_batch12, 8};
#endif
+#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
+ batch[nb++] = (batch_t){"arm-sha3 x2", kf_armsha3_batch, kf_armsha3_batch12, 2};
+#endif
case_all("SHA3-256 32->32, per hash", sha3, 2, batch, nb, kct_sha3_256,
kf_sha3_256_batch, 64, 32, 32, in, out, reps, target_ns);
diff --git a/export.mk b/export.mk
@@ -3,3 +3,4 @@ SRC+={{module.dirname}}/src/backend/scalar.c
SRC+={{module.dirname}}/src/backend/scalar_bmi.c
SRC+={{module.dirname}}/src/backend/avx2.c
SRC+={{module.dirname}}/src/backend/avx512.c
+SRC+={{module.dirname}}/src/backend/arm_sha3.c
diff --git a/src/backend/arm-sha3-perm.h b/src/backend/arm-sha3-perm.h
@@ -0,0 +1,249 @@
+#ifndef FINWO_ARM_SHA3_PERM_H
+#define FINWO_ARM_SHA3_PERM_H
+
+/* Keccak-p[1600] using the ARMv8.2 SHA3 instructions (EOR3, BCAX, RAX1, XAR).
+ *
+ * The including translation unit must have <arm_neon.h> in scope, define
+ * RC[24], and be compiled with the sha3 target enabled (arm_sha3.c does this
+ * with a scoped target region). The 25 lanes live in named locals, not an
+ * indexed array, so the register allocator keeps them in NEON registers (an
+ * array gets spilled by gcc). Each 128-bit register holds one 64-bit lane in
+ * its low half; the x2 entry points pack two independent states one per half.
+ *
+ * theta is fused into rho: XAR(t, D, 64 - rot) computes ROL(t ^ D, rot), so
+ * each lane is theta-added and rho-rotated in a single instruction. */
+
+#define KFA3_DECL(P) \
+ uint64x2_t P##0, P##1, P##2, P##3, P##4, P##5, P##6, P##7, P##8, P##9, \
+ P##10, P##11, P##12, P##13, P##14, P##15, P##16, P##17, P##18, P##19, \
+ P##20, P##21, P##22, P##23, P##24
+
+#define KFA3_LOAD1(P, S) \
+ P##0 = vdupq_n_u64((S)[0]); \
+ P##1 = vdupq_n_u64((S)[1]); \
+ P##2 = vdupq_n_u64((S)[2]); \
+ P##3 = vdupq_n_u64((S)[3]); \
+ P##4 = vdupq_n_u64((S)[4]); \
+ P##5 = vdupq_n_u64((S)[5]); \
+ P##6 = vdupq_n_u64((S)[6]); \
+ P##7 = vdupq_n_u64((S)[7]); \
+ P##8 = vdupq_n_u64((S)[8]); \
+ P##9 = vdupq_n_u64((S)[9]); \
+ P##10 = vdupq_n_u64((S)[10]); \
+ P##11 = vdupq_n_u64((S)[11]); \
+ P##12 = vdupq_n_u64((S)[12]); \
+ P##13 = vdupq_n_u64((S)[13]); \
+ P##14 = vdupq_n_u64((S)[14]); \
+ P##15 = vdupq_n_u64((S)[15]); \
+ P##16 = vdupq_n_u64((S)[16]); \
+ P##17 = vdupq_n_u64((S)[17]); \
+ P##18 = vdupq_n_u64((S)[18]); \
+ P##19 = vdupq_n_u64((S)[19]); \
+ P##20 = vdupq_n_u64((S)[20]); \
+ P##21 = vdupq_n_u64((S)[21]); \
+ P##22 = vdupq_n_u64((S)[22]); \
+ P##23 = vdupq_n_u64((S)[23]); \
+ P##24 = vdupq_n_u64((S)[24]);
+
+#define KFA3_STORE1(S, P) \
+ (S)[0] = vgetq_lane_u64(P##0, 0); \
+ (S)[1] = vgetq_lane_u64(P##1, 0); \
+ (S)[2] = vgetq_lane_u64(P##2, 0); \
+ (S)[3] = vgetq_lane_u64(P##3, 0); \
+ (S)[4] = vgetq_lane_u64(P##4, 0); \
+ (S)[5] = vgetq_lane_u64(P##5, 0); \
+ (S)[6] = vgetq_lane_u64(P##6, 0); \
+ (S)[7] = vgetq_lane_u64(P##7, 0); \
+ (S)[8] = vgetq_lane_u64(P##8, 0); \
+ (S)[9] = vgetq_lane_u64(P##9, 0); \
+ (S)[10] = vgetq_lane_u64(P##10, 0); \
+ (S)[11] = vgetq_lane_u64(P##11, 0); \
+ (S)[12] = vgetq_lane_u64(P##12, 0); \
+ (S)[13] = vgetq_lane_u64(P##13, 0); \
+ (S)[14] = vgetq_lane_u64(P##14, 0); \
+ (S)[15] = vgetq_lane_u64(P##15, 0); \
+ (S)[16] = vgetq_lane_u64(P##16, 0); \
+ (S)[17] = vgetq_lane_u64(P##17, 0); \
+ (S)[18] = vgetq_lane_u64(P##18, 0); \
+ (S)[19] = vgetq_lane_u64(P##19, 0); \
+ (S)[20] = vgetq_lane_u64(P##20, 0); \
+ (S)[21] = vgetq_lane_u64(P##21, 0); \
+ (S)[22] = vgetq_lane_u64(P##22, 0); \
+ (S)[23] = vgetq_lane_u64(P##23, 0); \
+ (S)[24] = vgetq_lane_u64(P##24, 0);
+
+#define KFA3_LOAD2(P, S) \
+ P##0 = vcombine_u64(vcreate_u64((S)[0][0]), vcreate_u64((S)[1][0])); \
+ P##1 = vcombine_u64(vcreate_u64((S)[0][1]), vcreate_u64((S)[1][1])); \
+ P##2 = vcombine_u64(vcreate_u64((S)[0][2]), vcreate_u64((S)[1][2])); \
+ P##3 = vcombine_u64(vcreate_u64((S)[0][3]), vcreate_u64((S)[1][3])); \
+ P##4 = vcombine_u64(vcreate_u64((S)[0][4]), vcreate_u64((S)[1][4])); \
+ P##5 = vcombine_u64(vcreate_u64((S)[0][5]), vcreate_u64((S)[1][5])); \
+ P##6 = vcombine_u64(vcreate_u64((S)[0][6]), vcreate_u64((S)[1][6])); \
+ P##7 = vcombine_u64(vcreate_u64((S)[0][7]), vcreate_u64((S)[1][7])); \
+ P##8 = vcombine_u64(vcreate_u64((S)[0][8]), vcreate_u64((S)[1][8])); \
+ P##9 = vcombine_u64(vcreate_u64((S)[0][9]), vcreate_u64((S)[1][9])); \
+ P##10 = vcombine_u64(vcreate_u64((S)[0][10]), vcreate_u64((S)[1][10])); \
+ P##11 = vcombine_u64(vcreate_u64((S)[0][11]), vcreate_u64((S)[1][11])); \
+ P##12 = vcombine_u64(vcreate_u64((S)[0][12]), vcreate_u64((S)[1][12])); \
+ P##13 = vcombine_u64(vcreate_u64((S)[0][13]), vcreate_u64((S)[1][13])); \
+ P##14 = vcombine_u64(vcreate_u64((S)[0][14]), vcreate_u64((S)[1][14])); \
+ P##15 = vcombine_u64(vcreate_u64((S)[0][15]), vcreate_u64((S)[1][15])); \
+ P##16 = vcombine_u64(vcreate_u64((S)[0][16]), vcreate_u64((S)[1][16])); \
+ P##17 = vcombine_u64(vcreate_u64((S)[0][17]), vcreate_u64((S)[1][17])); \
+ P##18 = vcombine_u64(vcreate_u64((S)[0][18]), vcreate_u64((S)[1][18])); \
+ P##19 = vcombine_u64(vcreate_u64((S)[0][19]), vcreate_u64((S)[1][19])); \
+ P##20 = vcombine_u64(vcreate_u64((S)[0][20]), vcreate_u64((S)[1][20])); \
+ P##21 = vcombine_u64(vcreate_u64((S)[0][21]), vcreate_u64((S)[1][21])); \
+ P##22 = vcombine_u64(vcreate_u64((S)[0][22]), vcreate_u64((S)[1][22])); \
+ P##23 = vcombine_u64(vcreate_u64((S)[0][23]), vcreate_u64((S)[1][23])); \
+ P##24 = vcombine_u64(vcreate_u64((S)[0][24]), vcreate_u64((S)[1][24]));
+
+#define KFA3_STORE2(S, P) \
+ (S)[0][0] = vgetq_lane_u64(P##0, 0); \
+ (S)[1][0] = vgetq_lane_u64(P##0, 1); \
+ (S)[0][1] = vgetq_lane_u64(P##1, 0); \
+ (S)[1][1] = vgetq_lane_u64(P##1, 1); \
+ (S)[0][2] = vgetq_lane_u64(P##2, 0); \
+ (S)[1][2] = vgetq_lane_u64(P##2, 1); \
+ (S)[0][3] = vgetq_lane_u64(P##3, 0); \
+ (S)[1][3] = vgetq_lane_u64(P##3, 1); \
+ (S)[0][4] = vgetq_lane_u64(P##4, 0); \
+ (S)[1][4] = vgetq_lane_u64(P##4, 1); \
+ (S)[0][5] = vgetq_lane_u64(P##5, 0); \
+ (S)[1][5] = vgetq_lane_u64(P##5, 1); \
+ (S)[0][6] = vgetq_lane_u64(P##6, 0); \
+ (S)[1][6] = vgetq_lane_u64(P##6, 1); \
+ (S)[0][7] = vgetq_lane_u64(P##7, 0); \
+ (S)[1][7] = vgetq_lane_u64(P##7, 1); \
+ (S)[0][8] = vgetq_lane_u64(P##8, 0); \
+ (S)[1][8] = vgetq_lane_u64(P##8, 1); \
+ (S)[0][9] = vgetq_lane_u64(P##9, 0); \
+ (S)[1][9] = vgetq_lane_u64(P##9, 1); \
+ (S)[0][10] = vgetq_lane_u64(P##10, 0); \
+ (S)[1][10] = vgetq_lane_u64(P##10, 1); \
+ (S)[0][11] = vgetq_lane_u64(P##11, 0); \
+ (S)[1][11] = vgetq_lane_u64(P##11, 1); \
+ (S)[0][12] = vgetq_lane_u64(P##12, 0); \
+ (S)[1][12] = vgetq_lane_u64(P##12, 1); \
+ (S)[0][13] = vgetq_lane_u64(P##13, 0); \
+ (S)[1][13] = vgetq_lane_u64(P##13, 1); \
+ (S)[0][14] = vgetq_lane_u64(P##14, 0); \
+ (S)[1][14] = vgetq_lane_u64(P##14, 1); \
+ (S)[0][15] = vgetq_lane_u64(P##15, 0); \
+ (S)[1][15] = vgetq_lane_u64(P##15, 1); \
+ (S)[0][16] = vgetq_lane_u64(P##16, 0); \
+ (S)[1][16] = vgetq_lane_u64(P##16, 1); \
+ (S)[0][17] = vgetq_lane_u64(P##17, 0); \
+ (S)[1][17] = vgetq_lane_u64(P##17, 1); \
+ (S)[0][18] = vgetq_lane_u64(P##18, 0); \
+ (S)[1][18] = vgetq_lane_u64(P##18, 1); \
+ (S)[0][19] = vgetq_lane_u64(P##19, 0); \
+ (S)[1][19] = vgetq_lane_u64(P##19, 1); \
+ (S)[0][20] = vgetq_lane_u64(P##20, 0); \
+ (S)[1][20] = vgetq_lane_u64(P##20, 1); \
+ (S)[0][21] = vgetq_lane_u64(P##21, 0); \
+ (S)[1][21] = vgetq_lane_u64(P##21, 1); \
+ (S)[0][22] = vgetq_lane_u64(P##22, 0); \
+ (S)[1][22] = vgetq_lane_u64(P##22, 1); \
+ (S)[0][23] = vgetq_lane_u64(P##23, 0); \
+ (S)[1][23] = vgetq_lane_u64(P##23, 1); \
+ (S)[0][24] = vgetq_lane_u64(P##24, 0); \
+ (S)[1][24] = vgetq_lane_u64(P##24, 1);
+
+/* ROL(t ^ D, ROT) via XAR = ROR(a ^ b, imm); 64 - ROT in [2, 63]. */
+#define KFA3_RHOPI(P, DEST, ROT, D) \
+ do { \
+ u = P##DEST; \
+ P##DEST = vxarq_u64(t, (D), 64 - (ROT)); \
+ t = u; \
+ } while (0);
+
+#define KFA3_CHI(P, Y0, Y1, Y2, Y3, Y4) \
+ do { \
+ uint64x2_t r0 = P##Y0, r1 = P##Y1, r2 = P##Y2, r3 = P##Y3, r4 = P##Y4; \
+ P##Y0 = vbcaxq_u64(r0, r2, r1); \
+ P##Y1 = vbcaxq_u64(r1, r3, r2); \
+ P##Y2 = vbcaxq_u64(r2, r4, r3); \
+ P##Y3 = vbcaxq_u64(r3, r0, r4); \
+ P##Y4 = vbcaxq_u64(r4, r1, r0); \
+ } while (0);
+
+#define KFA3_ROUND(P, RCIDX) \
+ do { \
+ uint64x2_t c0, c1, c2, c3, c4, d0, d1, d2, d3, d4, t, u; \
+ c0 = veor3q_u64(veor3q_u64(P##0, P##5, P##10), P##15, P##20); \
+ c1 = veor3q_u64(veor3q_u64(P##1, P##6, P##11), P##16, P##21); \
+ c2 = veor3q_u64(veor3q_u64(P##2, P##7, P##12), P##17, P##22); \
+ c3 = veor3q_u64(veor3q_u64(P##3, P##8, P##13), P##18, P##23); \
+ c4 = veor3q_u64(veor3q_u64(P##4, P##9, P##14), P##19, P##24); \
+ d0 = vrax1q_u64(c4, c1); \
+ d1 = vrax1q_u64(c0, c2); \
+ d2 = vrax1q_u64(c1, c3); \
+ d3 = vrax1q_u64(c2, c4); \
+ d4 = vrax1q_u64(c3, c0); \
+ t = P##1; \
+ KFA3_RHOPI(P, 10, 1, d1) \
+ KFA3_RHOPI(P, 7, 3, d0) \
+ KFA3_RHOPI(P, 11, 6, d2) \
+ KFA3_RHOPI(P, 17, 10, d1) \
+ KFA3_RHOPI(P, 18, 15, d2) \
+ KFA3_RHOPI(P, 3, 21, d3) \
+ KFA3_RHOPI(P, 5, 28, d3) \
+ KFA3_RHOPI(P, 16, 36, d0) \
+ KFA3_RHOPI(P, 8, 45, d1) \
+ KFA3_RHOPI(P, 21, 55, d3) \
+ KFA3_RHOPI(P, 24, 2, d1) \
+ KFA3_RHOPI(P, 4, 14, d4) \
+ KFA3_RHOPI(P, 15, 27, d4) \
+ KFA3_RHOPI(P, 23, 41, d0) \
+ KFA3_RHOPI(P, 19, 56, d3) \
+ KFA3_RHOPI(P, 13, 8, d4) \
+ KFA3_RHOPI(P, 12, 25, d3) \
+ KFA3_RHOPI(P, 2, 43, d2) \
+ KFA3_RHOPI(P, 20, 62, d2) \
+ KFA3_RHOPI(P, 14, 18, d0) \
+ KFA3_RHOPI(P, 22, 39, d4) \
+ KFA3_RHOPI(P, 9, 61, d2) \
+ KFA3_RHOPI(P, 6, 20, d4) \
+ KFA3_RHOPI(P, 1, 44, d1) \
+ P##0 = veorq_u64(P##0, d0); \
+ KFA3_CHI(P, 0, 1, 2, 3, 4) \
+ KFA3_CHI(P, 5, 6, 7, 8, 9) \
+ KFA3_CHI(P, 10, 11, 12, 13, 14) \
+ KFA3_CHI(P, 15, 16, 17, 18, 19) \
+ KFA3_CHI(P, 20, 21, 22, 23, 24) \
+ P##0 = veorq_u64(P##0, vdupq_n_u64(RC[RCIDX])); \
+ } while (0);
+
+static inline void keccakf_armsha3(void *state) {
+ uint64_t *s = (uint64_t *)state;
+ KFA3_DECL(v);
+ KFA3_LOAD1(v, s)
+ for (int i = 0; i < 24; i++) KFA3_ROUND(v, i);
+ KFA3_STORE1(s, v)
+}
+
+static inline void keccak12_armsha3(void *state) {
+ uint64_t *s = (uint64_t *)state;
+ KFA3_DECL(v);
+ KFA3_LOAD1(v, s)
+ for (int i = 12; i < 24; i++) KFA3_ROUND(v, i);
+ KFA3_STORE1(s, v)
+}
+
+static inline void keccakf2_armsha3(uint64_t s[2][25]) {
+ KFA3_DECL(v);
+ KFA3_LOAD2(v, s)
+ for (int i = 0; i < 24; i++) KFA3_ROUND(v, i);
+ KFA3_STORE2(s, v)
+}
+
+static inline void keccak12_2_armsha3(uint64_t s[2][25]) {
+ KFA3_DECL(v);
+ KFA3_LOAD2(v, s)
+ for (int i = 12; i < 24; i++) KFA3_ROUND(v, i);
+ KFA3_STORE2(s, v)
+}
+
+#endif /* FINWO_ARM_SHA3_PERM_H */
diff --git a/src/backend/arm_sha3.c b/src/backend/arm_sha3.c
@@ -0,0 +1,209 @@
+/* ARMv8.2 SHA3 backend. Self-contained: own permutation (arm-sha3-perm.h)
+ * and own copy of the FIPS 202 sponge. Nothing here is shared with, or leaks
+ * into, the scalar backend. Availability is decided at runtime only. */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "../keccak-fast-internal.h"
+
+#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
+
+#if defined(__APPLE__)
+#include <sys/sysctl.h>
+#elif defined(__linux__)
+#include <sys/auxv.h>
+#include <asm/hwcap.h>
+#ifndef HWCAP_SHA3
+#define HWCAP_SHA3 (1 << 17)
+#endif
+#endif
+
+/* The sha3 target is scoped to the permutation, sponge and batch kernels so
+ * the runtime gate below compiles at the x86-64 baseline with no SHA3
+ * instructions of its own. */
+#if defined(__clang__)
+#pragma clang attribute push(__attribute__((target("sha3"))), apply_to = function)
+#elif defined(__GNUC__)
+#pragma GCC push_options
+#pragma GCC target("+sha3")
+#endif
+
+#include <arm_neon.h>
+
+static const uint64_t RC[24] = {
+ 1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
+ 0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
+ 0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
+ 0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
+ 0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
+ 0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
+
+#include "arm-sha3-perm.h"
+
+#define KF_CAT2(a, b) a##b
+#define KF_CAT(a, b) KF_CAT2(a, b)
+#define KF_FN(name) KF_CAT(KF_PREFIX, name)
+
+#if !defined(__STDC_LIB_EXT1__)
+static inline int memset_s(void *dest, size_t destsz, int ch, size_t count) {
+ (void)destsz;
+ memset(dest, ch, count);
+ return 0;
+}
+#endif
+
+#define _(S) do { S } while (0)
+#define FOR(i, ST, L, S) \
+ _(for (size_t i = 0; i < L; i += ST) { S; })
+#define mkapply_ds(NAME, S) \
+ static inline void NAME(uint8_t *dst, const uint8_t *src, size_t len) { \
+ FOR(i, 1, len, S); \
+ }
+#define mkapply_sd(NAME, S) \
+ static inline void NAME(const uint8_t *src, uint8_t *dst, size_t len) { \
+ FOR(i, 1, len, S); \
+ }
+
+mkapply_ds(xorin, dst[i] ^= src[i])
+mkapply_sd(setout, dst[i] = src[i])
+
+#define Plen 200
+
+#define foldP(I, L, F, PERM) \
+ while (L >= rate) { \
+ F(a, I, rate); \
+ PERM(a); \
+ I += rate; \
+ L -= rate; \
+ }
+
+#define KF_HASH_BODY(PERM) \
+ uint8_t a[Plen] = {0}; \
+ foldP(in, inlen, xorin, PERM); \
+ a[inlen] ^= delim; \
+ a[rate - 1] ^= 0x80; \
+ xorin(a, in, inlen); \
+ PERM(a); \
+ foldP(out, outlen, setout, PERM); \
+ setout(a, out, outlen); \
+ memset_s(a, 200, 0, 200); \
+ return 0
+
+static inline int kf_hash24(uint8_t *out, size_t outlen, const uint8_t *in,
+ size_t inlen, size_t rate, uint8_t delim) {
+ if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
+ return -1;
+ }
+ KF_HASH_BODY(keccakf_armsha3);
+}
+
+static inline int kf_hash12(uint8_t *out, size_t outlen, const uint8_t *in,
+ size_t inlen, size_t rate, uint8_t delim) {
+ if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
+ return -1;
+ }
+ KF_HASH_BODY(keccak12_armsha3);
+}
+
+#define defshake(bits) \
+ KF_LINKAGE int KF_FN(shake##bits)(uint8_t *out, size_t outlen, \
+ const uint8_t *in, size_t inlen) { \
+ return kf_hash24(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
+ }
+#define defsha3(bits) \
+ KF_LINKAGE int KF_FN(sha3_##bits)(uint8_t *out, size_t outlen, \
+ const uint8_t *in, size_t inlen) { \
+ if (outlen > (bits / 8)) { \
+ return -1; \
+ } \
+ return kf_hash24(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \
+ }
+#define defturboshake(bits) \
+ KF_LINKAGE int KF_FN(turboshake##bits)(uint8_t *out, size_t outlen, \
+ const uint8_t *in, size_t inlen) { \
+ return kf_hash12(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
+ }
+
+#define KF_PREFIX kf_armsha3_
+#define KF_LINKAGE
+defshake(128)
+defshake(256)
+defsha3(224)
+defsha3(256)
+defsha3(384)
+defsha3(512)
+defturboshake(128)
+defturboshake(256)
+#undef defshake
+#undef defsha3
+#undef defturboshake
+
+void kf_armsha3_batch(uint64_t states[][25], size_t count) {
+ size_t i = 0;
+ for (; i + 2 <= count; i += 2) {
+ keccakf2_armsha3(&states[i]);
+ }
+ for (; i < count; i++) {
+ keccakf_armsha3(states[i]);
+ }
+}
+
+void kf_armsha3_batch12(uint64_t states[][25], size_t count) {
+ size_t i = 0;
+ for (; i + 2 <= count; i += 2) {
+ keccak12_2_armsha3(&states[i]);
+ }
+ for (; i < count; i++) {
+ keccak12_armsha3(states[i]);
+ }
+}
+
+#if defined(__clang__)
+#pragma clang attribute pop
+#elif defined(__GNUC__)
+#pragma GCC pop_options
+#endif
+
+static int kf_armsha3_available(void) {
+#if defined(__APPLE__)
+ int v = 0;
+ size_t n = sizeof v;
+ if (sysctlbyname("hw.optional.arm.FEAT_SHA3", &v, &n, NULL, 0) != 0) {
+ return 0;
+ }
+ return v != 0;
+#elif defined(__linux__)
+ return (getauxval(AT_HWCAP) & HWCAP_SHA3) != 0;
+#else
+ return 0;
+#endif
+}
+
+__attribute__((constructor)) static void kf_armsha3_register(void) {
+ if (!kf_armsha3_available()) {
+ return;
+ }
+ if (kf_backend_priority <= KF_PRIO_SHA3) {
+ kf_shake128 = kf_armsha3_shake128;
+ kf_shake256 = kf_armsha3_shake256;
+ kf_sha3_224 = kf_armsha3_sha3_224;
+ kf_sha3_256 = kf_armsha3_sha3_256;
+ kf_sha3_384 = kf_armsha3_sha3_384;
+ kf_sha3_512 = kf_armsha3_sha3_512;
+ kf_turboshake128 = kf_armsha3_turboshake128;
+ kf_turboshake256 = kf_armsha3_turboshake256;
+ kf_backend_priority = KF_PRIO_SHA3;
+ kf_backend_label = "arm-sha3";
+ }
+ if (kf_batch_priority <= KF_PRIO_SHA3) {
+ kf_batch_perm = kf_armsha3_batch;
+ kf_batch_perm12 = kf_armsha3_batch12;
+ kf_batch_priority = KF_PRIO_SHA3;
+ kf_batch_lanes = 2;
+ kf_batch_label = "arm-sha3 x2";
+ }
+}
+
+#endif /* aarch64 && !KECCAK_FAST_NO_SHA3 */
diff --git a/src/keccak-fast-internal.h b/src/keccak-fast-internal.h
@@ -12,6 +12,7 @@ typedef void (*kf_batch_perm_fn)(uint64_t states[][25], size_t count);
#define KF_PRIO_SCALAR 0
#define KF_PRIO_BMI 10
+#define KF_PRIO_SHA3 10
#define KF_PRIO_AVX2 20
#define KF_PRIO_AVX512 30
@@ -60,4 +61,9 @@ void kf_avx512_batch(uint64_t states[][25], size_t count);
void kf_avx512_batch12(uint64_t states[][25], size_t count);
#endif
+#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
+void kf_armsha3_batch(uint64_t states[][25], size_t count);
+void kf_armsha3_batch12(uint64_t states[][25], size_t count);
+#endif
+
#endif