commit 3ea97f4b05389e9e113b39cdb89bb78b37c458fe
parent 25923c67731412c7b0191d0b1dbea6bb18f1d943
Author: finwo <finwo@pm.me>
Date: Sun, 11 Oct 2026 20:08:38 +0200
Stricter backend separation
Diffstat:
12 files changed, 901 insertions(+), 521 deletions(-)
diff --git a/bench/bench.c b/bench/bench.c
@@ -11,35 +11,28 @@
#include "keccak-fast.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
+int kct_sha3_256(uint8_t *, size_t, const uint8_t *, size_t);
+int kct_shake256(uint8_t *, size_t, const uint8_t *, size_t);
-static int have_armsha3(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;
+int kf_scalar_sha3_256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_shake256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_turboshake256(uint8_t *, size_t, const uint8_t *, size_t);
+
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_AVX2)
+void kf_avx2_batch(uint64_t states[][25], size_t count);
+void kf_avx2_batch12(uint64_t states[][25], size_t count);
#endif
-}
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_AVX512)
+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)
+int kf_armsha3_available(void);
+void kf_armsha3_batch(uint64_t states[][25], size_t count);
+void kf_armsha3_batch12(uint64_t states[][25], size_t count);
#endif
-
-int kct_sha3_256(uint8_t *, size_t, const uint8_t *, size_t);
-int kct_shake256(uint8_t *, size_t, const uint8_t *, size_t);
#ifndef BENCH_FLAGS
#define BENCH_FLAGS ""
@@ -131,6 +124,10 @@ static void case_all(const char *title, const oneshot_t *os, int nos,
if (biters < 10) biters = 10;
}
+ kf_batch_perm_fn saved_perm = kf_batch_perm;
+ kf_batch_perm_fn saved_perm12 = kf_batch_perm12;
+ int saved_lanes = kf_batch_lanes;
+
for (int r = 0; r < reps; r++) {
if (donor != NULL) {
double d = time_once(donor, out, outlen, in, inlen, iters);
@@ -149,6 +146,10 @@ static void case_all(const char *title, const oneshot_t *os, int nos,
}
}
+ kf_batch_perm = saved_perm;
+ kf_batch_perm12 = saved_perm12;
+ kf_batch_lanes = saved_lanes;
+
printf("%s\n", title);
if (donor != NULL) {
printf(" %-12s %9.2f ns/hash %9.3f Mh/s\n", "donor", bd, 1000.0 / bd);
@@ -209,7 +210,7 @@ int main(void) {
batch[nb++] = (batch_t){"avx512 x8", kf_avx512_batch, kf_avx512_batch12, 8};
#endif
#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
- if (have_armsha3())
+ if (kf_armsha3_available())
batch[nb++] = (batch_t){"arm-sha3 x2", kf_armsha3_batch, kf_armsha3_batch12, 2};
#endif
diff --git a/src/backend/arm_sha3.c b/src/backend/arm_sha3.c
@@ -100,26 +100,25 @@ static inline int kf_hash12(uint8_t *out, size_t outlen, const uint8_t *in,
}
#define defshake(bits) \
- KF_LINKAGE int KF_FN(shake##bits)(uint8_t *out, size_t outlen, \
- const uint8_t *in, size_t inlen) { \
+ 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) { \
+ 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) { \
+ 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)
@@ -158,7 +157,7 @@ void kf_armsha3_batch12(uint64_t states[][25], size_t count) {
#pragma GCC pop_options
#endif
-static int kf_armsha3_available(void) {
+int kf_armsha3_available(void) {
#if defined(__APPLE__)
int v = 0;
size_t n = sizeof v;
@@ -177,7 +176,7 @@ __attribute__((constructor)) static void kf_armsha3_register(void) {
if (!kf_armsha3_available()) {
return;
}
- if (kf_backend_priority <= KF_PRIO_SHA3) {
+ if (kf_backend_priority <= 10) {
kf_shake128 = kf_armsha3_shake128;
kf_shake256 = kf_armsha3_shake256;
kf_sha3_224 = kf_armsha3_sha3_224;
@@ -186,15 +185,15 @@ __attribute__((constructor)) static void kf_armsha3_register(void) {
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_priority = 10;
kf_backend_label = "arm-sha3";
}
- if (kf_batch_priority <= KF_PRIO_SHA3) {
+ if (kf_batch_priority <= 10) {
kf_batch_perm = kf_armsha3_batch;
kf_batch_perm12 = kf_armsha3_batch12;
- kf_batch_priority = KF_PRIO_SHA3;
+ kf_batch_priority = 10;
kf_batch_lanes = 2;
- kf_batch_label = "arm-sha3 x2";
+ kf_batch_label = "arm-sha3";
}
}
diff --git a/src/backend/avx2.c b/src/backend/avx2.c
@@ -1,5 +1,6 @@
#include <stddef.h>
#include <stdint.h>
+#include <string.h>
#include "../keccak-fast-internal.h"
@@ -193,8 +194,11 @@ void kf_avx2_batch(uint64_t states[][25], size_t count) {
for (; i + 4 <= count; i += 4) {
kf_permute_x4(&states[i]);
}
- for (; i < count; i++) {
- kf_scalar_permute(states[i]);
+ if (i < count) {
+ uint64_t tmp[4][25] = {0};
+ memcpy(tmp, &states[i], (count - i) * sizeof tmp[0]);
+ kf_permute_x4(tmp);
+ memcpy(&states[i], tmp, (count - i) * sizeof tmp[0]);
}
}
@@ -204,17 +208,22 @@ void kf_avx2_batch12(uint64_t states[][25], size_t count) {
for (; i + 4 <= count; i += 4) {
kf_permute_x4_12(&states[i]);
}
- if (i < count) kf_scalar_batch12(&states[i], count - i);
+ if (i < count) {
+ uint64_t tmp[4][25] = {0};
+ memcpy(tmp, &states[i], (count - i) * sizeof tmp[0]);
+ kf_permute_x4_12(tmp);
+ memcpy(&states[i], tmp, (count - i) * sizeof tmp[0]);
+ }
}
__attribute__((constructor)) static void kf_avx2_register(void) {
if (!__builtin_cpu_supports("avx2")) {
return;
}
- if (kf_batch_priority <= KF_PRIO_AVX2) {
+ if (kf_batch_priority <= 20) {
kf_batch_perm = kf_avx2_batch;
kf_batch_perm12 = kf_avx2_batch12;
- kf_batch_priority = KF_PRIO_AVX2;
+ kf_batch_priority = 20;
kf_batch_lanes = 4;
kf_batch_label = "avx2";
}
diff --git a/src/backend/avx512.c b/src/backend/avx512.c
@@ -1,5 +1,6 @@
#include <stddef.h>
#include <stdint.h>
+#include <string.h>
#include "../keccak-fast-internal.h"
@@ -202,8 +203,11 @@ void kf_avx512_batch(uint64_t states[][25], size_t count) {
for (; i + 8 <= count; i += 8) {
kf_permute_x8(&states[i]);
}
- for (; i < count; i++) {
- kf_scalar_permute(states[i]);
+ if (i < count) {
+ uint64_t tmp[8][25] = {0};
+ memcpy(tmp, &states[i], (count - i) * sizeof tmp[0]);
+ kf_permute_x8(tmp);
+ memcpy(&states[i], tmp, (count - i) * sizeof tmp[0]);
}
}
@@ -213,17 +217,22 @@ void kf_avx512_batch12(uint64_t states[][25], size_t count) {
for (; i + 8 <= count; i += 8) {
kf_permute_x8_12(&states[i]);
}
- if (i < count) kf_scalar_batch12(&states[i], count - i);
+ if (i < count) {
+ uint64_t tmp[8][25] = {0};
+ memcpy(tmp, &states[i], (count - i) * sizeof tmp[0]);
+ kf_permute_x8_12(tmp);
+ memcpy(&states[i], tmp, (count - i) * sizeof tmp[0]);
+ }
}
__attribute__((constructor)) static void kf_avx512_register(void) {
if (!__builtin_cpu_supports("avx512f")) {
return;
}
- if (kf_batch_priority <= KF_PRIO_AVX512) {
+ if (kf_batch_priority <= 30) {
kf_batch_perm = kf_avx512_batch;
kf_batch_perm12 = kf_avx512_batch12;
- kf_batch_priority = KF_PRIO_AVX512;
+ kf_batch_priority = 30;
kf_batch_lanes = 8;
kf_batch_label = "avx512";
}
diff --git a/src/backend/scalar-impl.h b/src/backend/scalar-impl.h
@@ -1,109 +0,0 @@
-#ifndef FINWO_SCALAR_IMPL_H
-#define FINWO_SCALAR_IMPL_H
-
-#include <stddef.h>
-#include <stdint.h>
-#include <string.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)
-
-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};
-
-#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
-
-#include "scalar-perm.h"
-
-#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(a, 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);
-}
-
-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);
-}
-
-#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); \
- }
-
-defshake(128)
-defshake(256)
-defsha3(224)
-defsha3(256)
-defsha3(384)
-defsha3(512)
-defturboshake(128)
-defturboshake(256)
-
-#undef defshake
-#undef defsha3
-#undef defturboshake
-
-#endif /* FINWO_SCALAR_IMPL_H */
diff --git a/src/backend/scalar-perm.h b/src/backend/scalar-perm.h
@@ -1,206 +0,0 @@
-#ifndef FINWO_SCALAR_PERM_H
-#define FINWO_SCALAR_PERM_H
-
-/* Keccak-p[1600] with the 25 lanes held in named locals rather than an indexed
- array, so the scheduler can keep them in registers; theta is fused into the
- previous round's chi (prepareTheta) and the round loop is unrolled 4x. */
-
-#define KFP_LOAD(P, S) \
- P##0 = S[0]; \
- P##1 = S[1]; \
- P##2 = S[2]; \
- P##3 = S[3]; \
- P##4 = S[4]; \
- P##5 = S[5]; \
- P##6 = S[6]; \
- P##7 = S[7]; \
- P##8 = S[8]; \
- P##9 = S[9]; \
- P##10 = S[10]; \
- P##11 = S[11]; \
- P##12 = S[12]; \
- P##13 = S[13]; \
- P##14 = S[14]; \
- P##15 = S[15]; \
- P##16 = S[16]; \
- P##17 = S[17]; \
- P##18 = S[18]; \
- P##19 = S[19]; \
- P##20 = S[20]; \
- P##21 = S[21]; \
- P##22 = S[22]; \
- P##23 = S[23]; \
- P##24 = S[24]; \
-
-#define KFP_STORE(S, P) \
- S[0] = P##0; \
- S[1] = P##1; \
- S[2] = P##2; \
- S[3] = P##3; \
- S[4] = P##4; \
- S[5] = P##5; \
- S[6] = P##6; \
- S[7] = P##7; \
- S[8] = P##8; \
- S[9] = P##9; \
- S[10] = P##10; \
- S[11] = P##11; \
- S[12] = P##12; \
- S[13] = P##13; \
- S[14] = P##14; \
- S[15] = P##15; \
- S[16] = P##16; \
- S[17] = P##17; \
- S[18] = P##18; \
- S[19] = P##19; \
- S[20] = P##20; \
- S[21] = P##21; \
- S[22] = P##22; \
- S[23] = P##23; \
- S[24] = P##24; \
-
-#define KFP_INIT_C(SRC) \
- c0 = SRC##0 ^ SRC##5 ^ SRC##10 ^ SRC##15 ^ SRC##20; \
- c1 = SRC##1 ^ SRC##6 ^ SRC##11 ^ SRC##16 ^ SRC##21; \
- c2 = SRC##2 ^ SRC##7 ^ SRC##12 ^ SRC##17 ^ SRC##22; \
- c3 = SRC##3 ^ SRC##8 ^ SRC##13 ^ SRC##18 ^ SRC##23; \
- c4 = SRC##4 ^ SRC##9 ^ SRC##14 ^ SRC##19 ^ SRC##24; \
-
-#define KFP_R(SRC, DST, RCIDX) \
- d0 = c4 ^ rol(c1, 1); \
- d1 = c0 ^ rol(c2, 1); \
- d2 = c1 ^ rol(c3, 1); \
- d3 = c2 ^ rol(c4, 1); \
- d4 = c3 ^ rol(c0, 1); \
- SRC##0 ^= d0; \
- b0 = SRC##0; \
- SRC##6 ^= d1; \
- b1 = rol(SRC##6, 44); \
- SRC##12 ^= d2; \
- b2 = rol(SRC##12, 43); \
- SRC##18 ^= d3; \
- b3 = rol(SRC##18, 21); \
- SRC##24 ^= d4; \
- b4 = rol(SRC##24, 14); \
- DST##0 = b0 ^ ((~b1) & b2); \
- DST##1 = b1 ^ ((~b2) & b3); \
- DST##2 = b2 ^ ((~b3) & b4); \
- DST##3 = b3 ^ ((~b4) & b0); \
- DST##4 = b4 ^ ((~b0) & b1); \
- DST##0 ^= RC[RCIDX]; \
- c0 = DST##0; \
- c1 = DST##1; \
- c2 = DST##2; \
- c3 = DST##3; \
- c4 = DST##4; \
- SRC##3 ^= d3; \
- b5 = rol(SRC##3, 28); \
- SRC##9 ^= d4; \
- b6 = rol(SRC##9, 20); \
- SRC##10 ^= d0; \
- b7 = rol(SRC##10, 3); \
- SRC##16 ^= d1; \
- b8 = rol(SRC##16, 45); \
- SRC##22 ^= d2; \
- b9 = rol(SRC##22, 61); \
- DST##5 = b5 ^ ((~b6) & b7); \
- DST##6 = b6 ^ ((~b7) & b8); \
- DST##7 = b7 ^ ((~b8) & b9); \
- DST##8 = b8 ^ ((~b9) & b5); \
- DST##9 = b9 ^ ((~b5) & b6); \
- c0 ^= DST##5; \
- c1 ^= DST##6; \
- c2 ^= DST##7; \
- c3 ^= DST##8; \
- c4 ^= DST##9; \
- SRC##1 ^= d1; \
- b10 = rol(SRC##1, 1); \
- SRC##7 ^= d2; \
- b11 = rol(SRC##7, 6); \
- SRC##13 ^= d3; \
- b12 = rol(SRC##13, 25); \
- SRC##19 ^= d4; \
- b13 = rol(SRC##19, 8); \
- SRC##20 ^= d0; \
- b14 = rol(SRC##20, 18); \
- DST##10 = b10 ^ ((~b11) & b12); \
- DST##11 = b11 ^ ((~b12) & b13); \
- DST##12 = b12 ^ ((~b13) & b14); \
- DST##13 = b13 ^ ((~b14) & b10); \
- DST##14 = b14 ^ ((~b10) & b11); \
- c0 ^= DST##10; \
- c1 ^= DST##11; \
- c2 ^= DST##12; \
- c3 ^= DST##13; \
- c4 ^= DST##14; \
- SRC##4 ^= d4; \
- b15 = rol(SRC##4, 27); \
- SRC##5 ^= d0; \
- b16 = rol(SRC##5, 36); \
- SRC##11 ^= d1; \
- b17 = rol(SRC##11, 10); \
- SRC##17 ^= d2; \
- b18 = rol(SRC##17, 15); \
- SRC##23 ^= d3; \
- b19 = rol(SRC##23, 56); \
- DST##15 = b15 ^ ((~b16) & b17); \
- DST##16 = b16 ^ ((~b17) & b18); \
- DST##17 = b17 ^ ((~b18) & b19); \
- DST##18 = b18 ^ ((~b19) & b15); \
- DST##19 = b19 ^ ((~b15) & b16); \
- c0 ^= DST##15; \
- c1 ^= DST##16; \
- c2 ^= DST##17; \
- c3 ^= DST##18; \
- c4 ^= DST##19; \
- SRC##2 ^= d2; \
- b20 = rol(SRC##2, 62); \
- SRC##8 ^= d3; \
- b21 = rol(SRC##8, 55); \
- SRC##14 ^= d4; \
- b22 = rol(SRC##14, 39); \
- SRC##15 ^= d0; \
- b23 = rol(SRC##15, 41); \
- SRC##21 ^= d1; \
- b24 = rol(SRC##21, 2); \
- DST##20 = b20 ^ ((~b21) & b22); \
- DST##21 = b21 ^ ((~b22) & b23); \
- DST##22 = b22 ^ ((~b23) & b24); \
- DST##23 = b23 ^ ((~b24) & b20); \
- DST##24 = b24 ^ ((~b20) & b21); \
- c0 ^= DST##20; \
- c1 ^= DST##21; \
- c2 ^= DST##22; \
- c3 ^= DST##23; \
- c4 ^= DST##24; \
-
-#define KFP_R2(SRC, DST, I) KFP_R(SRC, DST, I) KFP_R(DST, SRC, I + 1)
-#define KFP_R4(SRC, DST, I) KFP_R2(SRC, DST, I) KFP_R2(SRC, DST, I + 2)
-#define KFP_R12(SRC, DST, I) \
- KFP_R4(SRC, DST, I) KFP_R4(SRC, DST, I + 4) KFP_R4(SRC, DST, I + 8)
-
-#define KFP_DECL(P) \
- uint64_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 KFP_BODY(ROUNDS) \
- uint64_t *s = (uint64_t *)state; \
- KFP_DECL(a); \
- KFP_DECL(e); \
- KFP_DECL(b); \
- uint64_t c0, c1, c2, c3, c4, d0, d1, d2, d3, d4; \
- KFP_LOAD(a, s) \
- KFP_INIT_C(a) \
- ROUNDS \
- KFP_STORE(s, a)
-
-static inline void keccakf(void *state) {
- KFP_BODY(for (int i = 0; i < 24; i += 4) { KFP_R4(a, e, i) })
-}
-
-static inline void keccak12(void *state) {
- KFP_BODY(for (int i = 12; i < 24; i += 4) { KFP_R4(a, e, i) })
-}
-
-#endif /* FINWO_SCALAR_PERM_H */
diff --git a/src/backend/scalar.c b/src/backend/scalar.c
@@ -11,8 +11,7 @@
#include "../keccak-fast-internal.h"
-/* The fallback must run on any x86-64: no AVX (clang vectorizes it under
- * -march=native and runs 1.9x slower) and no BMI (that is scalar_bmi.c). */
+/* no-avx: clang vectorizes this 1.9x slower; no-bmi: that is scalar_bmi.c */
#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma clang attribute push( \
__attribute__((target("no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2"))), \
@@ -21,14 +20,302 @@
#pragma GCC target("no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2")
#endif
-#define KF_PREFIX kf_scalar_
-#define KF_LINKAGE
-#include "scalar-impl.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};
+
+#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
+
+#define KFP_LOAD(P, S) \
+ P##0 = S[0]; \
+ P##1 = S[1]; \
+ P##2 = S[2]; \
+ P##3 = S[3]; \
+ P##4 = S[4]; \
+ P##5 = S[5]; \
+ P##6 = S[6]; \
+ P##7 = S[7]; \
+ P##8 = S[8]; \
+ P##9 = S[9]; \
+ P##10 = S[10]; \
+ P##11 = S[11]; \
+ P##12 = S[12]; \
+ P##13 = S[13]; \
+ P##14 = S[14]; \
+ P##15 = S[15]; \
+ P##16 = S[16]; \
+ P##17 = S[17]; \
+ P##18 = S[18]; \
+ P##19 = S[19]; \
+ P##20 = S[20]; \
+ P##21 = S[21]; \
+ P##22 = S[22]; \
+ P##23 = S[23]; \
+ P##24 = S[24]; \
+
+#define KFP_STORE(S, P) \
+ S[0] = P##0; \
+ S[1] = P##1; \
+ S[2] = P##2; \
+ S[3] = P##3; \
+ S[4] = P##4; \
+ S[5] = P##5; \
+ S[6] = P##6; \
+ S[7] = P##7; \
+ S[8] = P##8; \
+ S[9] = P##9; \
+ S[10] = P##10; \
+ S[11] = P##11; \
+ S[12] = P##12; \
+ S[13] = P##13; \
+ S[14] = P##14; \
+ S[15] = P##15; \
+ S[16] = P##16; \
+ S[17] = P##17; \
+ S[18] = P##18; \
+ S[19] = P##19; \
+ S[20] = P##20; \
+ S[21] = P##21; \
+ S[22] = P##22; \
+ S[23] = P##23; \
+ S[24] = P##24; \
+
+#define KFP_INIT_C(SRC) \
+ c0 = SRC##0 ^ SRC##5 ^ SRC##10 ^ SRC##15 ^ SRC##20; \
+ c1 = SRC##1 ^ SRC##6 ^ SRC##11 ^ SRC##16 ^ SRC##21; \
+ c2 = SRC##2 ^ SRC##7 ^ SRC##12 ^ SRC##17 ^ SRC##22; \
+ c3 = SRC##3 ^ SRC##8 ^ SRC##13 ^ SRC##18 ^ SRC##23; \
+ c4 = SRC##4 ^ SRC##9 ^ SRC##14 ^ SRC##19 ^ SRC##24; \
+
+#define KFP_R(SRC, DST, RCIDX) \
+ d0 = c4 ^ rol(c1, 1); \
+ d1 = c0 ^ rol(c2, 1); \
+ d2 = c1 ^ rol(c3, 1); \
+ d3 = c2 ^ rol(c4, 1); \
+ d4 = c3 ^ rol(c0, 1); \
+ SRC##0 ^= d0; \
+ b0 = SRC##0; \
+ SRC##6 ^= d1; \
+ b1 = rol(SRC##6, 44); \
+ SRC##12 ^= d2; \
+ b2 = rol(SRC##12, 43); \
+ SRC##18 ^= d3; \
+ b3 = rol(SRC##18, 21); \
+ SRC##24 ^= d4; \
+ b4 = rol(SRC##24, 14); \
+ DST##0 = b0 ^ ((~b1) & b2); \
+ DST##1 = b1 ^ ((~b2) & b3); \
+ DST##2 = b2 ^ ((~b3) & b4); \
+ DST##3 = b3 ^ ((~b4) & b0); \
+ DST##4 = b4 ^ ((~b0) & b1); \
+ DST##0 ^= RC[RCIDX]; \
+ c0 = DST##0; \
+ c1 = DST##1; \
+ c2 = DST##2; \
+ c3 = DST##3; \
+ c4 = DST##4; \
+ SRC##3 ^= d3; \
+ b5 = rol(SRC##3, 28); \
+ SRC##9 ^= d4; \
+ b6 = rol(SRC##9, 20); \
+ SRC##10 ^= d0; \
+ b7 = rol(SRC##10, 3); \
+ SRC##16 ^= d1; \
+ b8 = rol(SRC##16, 45); \
+ SRC##22 ^= d2; \
+ b9 = rol(SRC##22, 61); \
+ DST##5 = b5 ^ ((~b6) & b7); \
+ DST##6 = b6 ^ ((~b7) & b8); \
+ DST##7 = b7 ^ ((~b8) & b9); \
+ DST##8 = b8 ^ ((~b9) & b5); \
+ DST##9 = b9 ^ ((~b5) & b6); \
+ c0 ^= DST##5; \
+ c1 ^= DST##6; \
+ c2 ^= DST##7; \
+ c3 ^= DST##8; \
+ c4 ^= DST##9; \
+ SRC##1 ^= d1; \
+ b10 = rol(SRC##1, 1); \
+ SRC##7 ^= d2; \
+ b11 = rol(SRC##7, 6); \
+ SRC##13 ^= d3; \
+ b12 = rol(SRC##13, 25); \
+ SRC##19 ^= d4; \
+ b13 = rol(SRC##19, 8); \
+ SRC##20 ^= d0; \
+ b14 = rol(SRC##20, 18); \
+ DST##10 = b10 ^ ((~b11) & b12); \
+ DST##11 = b11 ^ ((~b12) & b13); \
+ DST##12 = b12 ^ ((~b13) & b14); \
+ DST##13 = b13 ^ ((~b14) & b10); \
+ DST##14 = b14 ^ ((~b10) & b11); \
+ c0 ^= DST##10; \
+ c1 ^= DST##11; \
+ c2 ^= DST##12; \
+ c3 ^= DST##13; \
+ c4 ^= DST##14; \
+ SRC##4 ^= d4; \
+ b15 = rol(SRC##4, 27); \
+ SRC##5 ^= d0; \
+ b16 = rol(SRC##5, 36); \
+ SRC##11 ^= d1; \
+ b17 = rol(SRC##11, 10); \
+ SRC##17 ^= d2; \
+ b18 = rol(SRC##17, 15); \
+ SRC##23 ^= d3; \
+ b19 = rol(SRC##23, 56); \
+ DST##15 = b15 ^ ((~b16) & b17); \
+ DST##16 = b16 ^ ((~b17) & b18); \
+ DST##17 = b17 ^ ((~b18) & b19); \
+ DST##18 = b18 ^ ((~b19) & b15); \
+ DST##19 = b19 ^ ((~b15) & b16); \
+ c0 ^= DST##15; \
+ c1 ^= DST##16; \
+ c2 ^= DST##17; \
+ c3 ^= DST##18; \
+ c4 ^= DST##19; \
+ SRC##2 ^= d2; \
+ b20 = rol(SRC##2, 62); \
+ SRC##8 ^= d3; \
+ b21 = rol(SRC##8, 55); \
+ SRC##14 ^= d4; \
+ b22 = rol(SRC##14, 39); \
+ SRC##15 ^= d0; \
+ b23 = rol(SRC##15, 41); \
+ SRC##21 ^= d1; \
+ b24 = rol(SRC##21, 2); \
+ DST##20 = b20 ^ ((~b21) & b22); \
+ DST##21 = b21 ^ ((~b22) & b23); \
+ DST##22 = b22 ^ ((~b23) & b24); \
+ DST##23 = b23 ^ ((~b24) & b20); \
+ DST##24 = b24 ^ ((~b20) & b21); \
+ c0 ^= DST##20; \
+ c1 ^= DST##21; \
+ c2 ^= DST##22; \
+ c3 ^= DST##23; \
+ c4 ^= DST##24; \
+
+#define KFP_R2(SRC, DST, I) KFP_R(SRC, DST, I) KFP_R(DST, SRC, I + 1)
+#define KFP_R4(SRC, DST, I) KFP_R2(SRC, DST, I) KFP_R2(SRC, DST, I + 2)
+
+#define KFP_DECL(P) \
+ uint64_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 KFP_BODY(ROUNDS) \
+ uint64_t *s = (uint64_t *)state; \
+ KFP_DECL(a); \
+ KFP_DECL(e); \
+ KFP_DECL(b); \
+ uint64_t c0, c1, c2, c3, c4, d0, d1, d2, d3, d4; \
+ KFP_LOAD(a, s) \
+ KFP_INIT_C(a) \
+ ROUNDS \
+ KFP_STORE(s, a)
-void kf_scalar_permute(uint64_t state[25]) {
- keccakf(state);
+static inline void keccakf(void *state) {
+ KFP_BODY(for (int i = 0; i < 24; i += 4) { KFP_R4(a, e, i) })
}
+static inline void keccak12(void *state) {
+ KFP_BODY(for (int i = 12; i < 24; i += 4) { KFP_R4(a, e, i) })
+}
+
+#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(a, 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);
+}
+
+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);
+}
+
+#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)
+#define KF_PREFIX kf_scalar_
+
+#define defshake(bits) \
+ 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) \
+ 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) \
+ 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); \
+ }
+
+defshake(128)
+defshake(256)
+defsha3(224)
+defsha3(256)
+defsha3(384)
+defsha3(512)
+defturboshake(128)
+defturboshake(256)
+
+#undef defshake
+#undef defsha3
+#undef defturboshake
+
void kf_scalar_batch(uint64_t states[][25], size_t count) {
for (size_t i = 0; i < count; i++) {
keccakf(states[i]);
@@ -42,7 +329,7 @@ void kf_scalar_batch12(uint64_t states[][25], size_t count) {
}
__attribute__((constructor)) static void kf_scalar_register(void) {
- if (kf_backend_priority <= KF_PRIO_SCALAR) {
+ if (kf_backend_priority <= 0) {
kf_shake128 = kf_scalar_shake128;
kf_shake256 = kf_scalar_shake256;
kf_sha3_224 = kf_scalar_sha3_224;
@@ -51,13 +338,13 @@ __attribute__((constructor)) static void kf_scalar_register(void) {
kf_sha3_512 = kf_scalar_sha3_512;
kf_turboshake128 = kf_scalar_turboshake128;
kf_turboshake256 = kf_scalar_turboshake256;
- kf_backend_priority = KF_PRIO_SCALAR;
+ kf_backend_priority = 0;
kf_backend_label = "scalar";
}
- if (kf_batch_priority <= KF_PRIO_SCALAR) {
+ if (kf_batch_priority <= 0) {
kf_batch_perm = kf_scalar_batch;
kf_batch_perm12 = kf_scalar_batch12;
- kf_batch_priority = KF_PRIO_SCALAR;
+ kf_batch_priority = 0;
kf_batch_lanes = 1;
kf_batch_label = "scalar";
}
diff --git a/src/backend/scalar_bmi.c b/src/backend/scalar_bmi.c
@@ -1,5 +1,9 @@
-/* Runtime BMI1/BMI2 variant of the scalar backend. Same source as scalar.c,
- * but the target enables `andn` (replaces `not`+`and` in chi) and `rorx`. */
+/* Keccak-f[1600] and the FIPS 202 sponge of coruus/keccak-tiny, renamed
+ * kf_bmi_* for the dispatcher.
+ *
+ * Implementor: David Leon Gil. License: CC0, attribution kindly requested.
+ * Blame taken too, but not liability.
+ */
#include <stddef.h>
#include <stdint.h>
@@ -18,9 +22,301 @@
#pragma GCC target("no-avx,no-avx2,no-avx512f,bmi,bmi2")
#endif
+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};
+
+#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
+
+#define KFP_LOAD(P, S) \
+ P##0 = S[0]; \
+ P##1 = S[1]; \
+ P##2 = S[2]; \
+ P##3 = S[3]; \
+ P##4 = S[4]; \
+ P##5 = S[5]; \
+ P##6 = S[6]; \
+ P##7 = S[7]; \
+ P##8 = S[8]; \
+ P##9 = S[9]; \
+ P##10 = S[10]; \
+ P##11 = S[11]; \
+ P##12 = S[12]; \
+ P##13 = S[13]; \
+ P##14 = S[14]; \
+ P##15 = S[15]; \
+ P##16 = S[16]; \
+ P##17 = S[17]; \
+ P##18 = S[18]; \
+ P##19 = S[19]; \
+ P##20 = S[20]; \
+ P##21 = S[21]; \
+ P##22 = S[22]; \
+ P##23 = S[23]; \
+ P##24 = S[24]; \
+
+#define KFP_STORE(S, P) \
+ S[0] = P##0; \
+ S[1] = P##1; \
+ S[2] = P##2; \
+ S[3] = P##3; \
+ S[4] = P##4; \
+ S[5] = P##5; \
+ S[6] = P##6; \
+ S[7] = P##7; \
+ S[8] = P##8; \
+ S[9] = P##9; \
+ S[10] = P##10; \
+ S[11] = P##11; \
+ S[12] = P##12; \
+ S[13] = P##13; \
+ S[14] = P##14; \
+ S[15] = P##15; \
+ S[16] = P##16; \
+ S[17] = P##17; \
+ S[18] = P##18; \
+ S[19] = P##19; \
+ S[20] = P##20; \
+ S[21] = P##21; \
+ S[22] = P##22; \
+ S[23] = P##23; \
+ S[24] = P##24; \
+
+#define KFP_INIT_C(SRC) \
+ c0 = SRC##0 ^ SRC##5 ^ SRC##10 ^ SRC##15 ^ SRC##20; \
+ c1 = SRC##1 ^ SRC##6 ^ SRC##11 ^ SRC##16 ^ SRC##21; \
+ c2 = SRC##2 ^ SRC##7 ^ SRC##12 ^ SRC##17 ^ SRC##22; \
+ c3 = SRC##3 ^ SRC##8 ^ SRC##13 ^ SRC##18 ^ SRC##23; \
+ c4 = SRC##4 ^ SRC##9 ^ SRC##14 ^ SRC##19 ^ SRC##24; \
+
+#define KFP_R(SRC, DST, RCIDX) \
+ d0 = c4 ^ rol(c1, 1); \
+ d1 = c0 ^ rol(c2, 1); \
+ d2 = c1 ^ rol(c3, 1); \
+ d3 = c2 ^ rol(c4, 1); \
+ d4 = c3 ^ rol(c0, 1); \
+ SRC##0 ^= d0; \
+ b0 = SRC##0; \
+ SRC##6 ^= d1; \
+ b1 = rol(SRC##6, 44); \
+ SRC##12 ^= d2; \
+ b2 = rol(SRC##12, 43); \
+ SRC##18 ^= d3; \
+ b3 = rol(SRC##18, 21); \
+ SRC##24 ^= d4; \
+ b4 = rol(SRC##24, 14); \
+ DST##0 = b0 ^ ((~b1) & b2); \
+ DST##1 = b1 ^ ((~b2) & b3); \
+ DST##2 = b2 ^ ((~b3) & b4); \
+ DST##3 = b3 ^ ((~b4) & b0); \
+ DST##4 = b4 ^ ((~b0) & b1); \
+ DST##0 ^= RC[RCIDX]; \
+ c0 = DST##0; \
+ c1 = DST##1; \
+ c2 = DST##2; \
+ c3 = DST##3; \
+ c4 = DST##4; \
+ SRC##3 ^= d3; \
+ b5 = rol(SRC##3, 28); \
+ SRC##9 ^= d4; \
+ b6 = rol(SRC##9, 20); \
+ SRC##10 ^= d0; \
+ b7 = rol(SRC##10, 3); \
+ SRC##16 ^= d1; \
+ b8 = rol(SRC##16, 45); \
+ SRC##22 ^= d2; \
+ b9 = rol(SRC##22, 61); \
+ DST##5 = b5 ^ ((~b6) & b7); \
+ DST##6 = b6 ^ ((~b7) & b8); \
+ DST##7 = b7 ^ ((~b8) & b9); \
+ DST##8 = b8 ^ ((~b9) & b5); \
+ DST##9 = b9 ^ ((~b5) & b6); \
+ c0 ^= DST##5; \
+ c1 ^= DST##6; \
+ c2 ^= DST##7; \
+ c3 ^= DST##8; \
+ c4 ^= DST##9; \
+ SRC##1 ^= d1; \
+ b10 = rol(SRC##1, 1); \
+ SRC##7 ^= d2; \
+ b11 = rol(SRC##7, 6); \
+ SRC##13 ^= d3; \
+ b12 = rol(SRC##13, 25); \
+ SRC##19 ^= d4; \
+ b13 = rol(SRC##19, 8); \
+ SRC##20 ^= d0; \
+ b14 = rol(SRC##20, 18); \
+ DST##10 = b10 ^ ((~b11) & b12); \
+ DST##11 = b11 ^ ((~b12) & b13); \
+ DST##12 = b12 ^ ((~b13) & b14); \
+ DST##13 = b13 ^ ((~b14) & b10); \
+ DST##14 = b14 ^ ((~b10) & b11); \
+ c0 ^= DST##10; \
+ c1 ^= DST##11; \
+ c2 ^= DST##12; \
+ c3 ^= DST##13; \
+ c4 ^= DST##14; \
+ SRC##4 ^= d4; \
+ b15 = rol(SRC##4, 27); \
+ SRC##5 ^= d0; \
+ b16 = rol(SRC##5, 36); \
+ SRC##11 ^= d1; \
+ b17 = rol(SRC##11, 10); \
+ SRC##17 ^= d2; \
+ b18 = rol(SRC##17, 15); \
+ SRC##23 ^= d3; \
+ b19 = rol(SRC##23, 56); \
+ DST##15 = b15 ^ ((~b16) & b17); \
+ DST##16 = b16 ^ ((~b17) & b18); \
+ DST##17 = b17 ^ ((~b18) & b19); \
+ DST##18 = b18 ^ ((~b19) & b15); \
+ DST##19 = b19 ^ ((~b15) & b16); \
+ c0 ^= DST##15; \
+ c1 ^= DST##16; \
+ c2 ^= DST##17; \
+ c3 ^= DST##18; \
+ c4 ^= DST##19; \
+ SRC##2 ^= d2; \
+ b20 = rol(SRC##2, 62); \
+ SRC##8 ^= d3; \
+ b21 = rol(SRC##8, 55); \
+ SRC##14 ^= d4; \
+ b22 = rol(SRC##14, 39); \
+ SRC##15 ^= d0; \
+ b23 = rol(SRC##15, 41); \
+ SRC##21 ^= d1; \
+ b24 = rol(SRC##21, 2); \
+ DST##20 = b20 ^ ((~b21) & b22); \
+ DST##21 = b21 ^ ((~b22) & b23); \
+ DST##22 = b22 ^ ((~b23) & b24); \
+ DST##23 = b23 ^ ((~b24) & b20); \
+ DST##24 = b24 ^ ((~b20) & b21); \
+ c0 ^= DST##20; \
+ c1 ^= DST##21; \
+ c2 ^= DST##22; \
+ c3 ^= DST##23; \
+ c4 ^= DST##24; \
+
+#define KFP_R2(SRC, DST, I) KFP_R(SRC, DST, I) KFP_R(DST, SRC, I + 1)
+#define KFP_R4(SRC, DST, I) KFP_R2(SRC, DST, I) KFP_R2(SRC, DST, I + 2)
+
+#define KFP_DECL(P) \
+ uint64_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 KFP_BODY(ROUNDS) \
+ uint64_t *s = (uint64_t *)state; \
+ KFP_DECL(a); \
+ KFP_DECL(e); \
+ KFP_DECL(b); \
+ uint64_t c0, c1, c2, c3, c4, d0, d1, d2, d3, d4; \
+ KFP_LOAD(a, s) \
+ KFP_INIT_C(a) \
+ ROUNDS \
+ KFP_STORE(s, a)
+
+static inline void keccakf(void *state) {
+ KFP_BODY(for (int i = 0; i < 24; i += 4) { KFP_R4(a, e, i) })
+}
+
+static inline void keccak12(void *state) {
+ KFP_BODY(for (int i = 12; i < 24; i += 4) { KFP_R4(a, e, i) })
+}
+
+#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(a, 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);
+}
+
+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);
+}
+
+#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)
#define KF_PREFIX kf_bmi_
-#define KF_LINKAGE static
-#include "scalar-impl.h"
+
+#define defshake(bits) \
+ 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) \
+ 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) \
+ 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); \
+ }
+
+defshake(128)
+defshake(256)
+defsha3(224)
+defsha3(256)
+defsha3(384)
+defsha3(512)
+defturboshake(128)
+defturboshake(256)
+
+#undef defshake
+#undef defsha3
+#undef defturboshake
void kf_bmi_batch(uint64_t states[][25], size_t count) {
for (size_t i = 0; i < count; i++) {
@@ -42,7 +338,7 @@ __attribute__((constructor)) static void kf_bmi_register(void) {
#endif
return;
}
- if (kf_backend_priority <= KF_PRIO_BMI) {
+ if (kf_backend_priority <= 10) {
kf_shake128 = kf_bmi_shake128;
kf_shake256 = kf_bmi_shake256;
kf_sha3_224 = kf_bmi_sha3_224;
@@ -51,13 +347,13 @@ __attribute__((constructor)) static void kf_bmi_register(void) {
kf_sha3_512 = kf_bmi_sha3_512;
kf_turboshake128 = kf_bmi_turboshake128;
kf_turboshake256 = kf_bmi_turboshake256;
- kf_backend_priority = KF_PRIO_BMI;
+ kf_backend_priority = 10;
kf_backend_label = "scalar+bmi";
}
- if (kf_batch_priority <= KF_PRIO_BMI) {
+ if (kf_batch_priority <= 10) {
kf_batch_perm = kf_bmi_batch;
kf_batch_perm12 = kf_bmi_batch12;
- kf_batch_priority = KF_PRIO_BMI;
+ kf_batch_priority = 10;
kf_batch_lanes = 1;
kf_batch_label = "scalar+bmi";
}
diff --git a/src/keccak-fast-internal.h b/src/keccak-fast-internal.h
@@ -8,14 +8,6 @@
typedef void (*kf_batch_perm_fn)(uint64_t states[][25], size_t count);
-#define KF_LANES_MAX 8
-
-#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
-
extern kf_batch_perm_fn kf_batch_perm;
extern kf_batch_perm_fn kf_batch_perm12;
extern int kf_batch_priority;
@@ -25,45 +17,4 @@ extern int kf_backend_priority;
extern const char *kf_backend_label;
extern const char *kf_batch_label;
-int kf_scalar_shake128(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_shake256(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_sha3_224(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_sha3_256(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_sha3_384(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_sha3_512(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_turboshake128(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-int kf_scalar_turboshake256(uint8_t *out, size_t outlen, const uint8_t *in,
- size_t inlen);
-void kf_scalar_permute(uint64_t state[25]);
-void kf_scalar_batch(uint64_t states[][25], size_t count);
-void kf_scalar_batch12(uint64_t states[][25], size_t count);
-
-#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
- !defined(KECCAK_FAST_NO_BMI)
-void kf_bmi_batch(uint64_t states[][25], size_t count);
-void kf_bmi_batch12(uint64_t states[][25], size_t count);
-#endif
-#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
- !defined(KECCAK_FAST_NO_AVX2)
-void kf_avx2_batch(uint64_t states[][25], size_t count);
-void kf_avx2_batch12(uint64_t states[][25], size_t count);
-#endif
-#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
- !defined(KECCAK_FAST_NO_AVX512)
-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
diff --git a/src/keccak-fast.c b/src/keccak-fast.c
@@ -8,23 +8,23 @@
#define KF_DOMAIN_SHAKE 0x1f
#define KF_DOMAIN_SHA3 0x06
-kf_hash_fn kf_shake128 = kf_scalar_shake128;
-kf_hash_fn kf_shake256 = kf_scalar_shake256;
-kf_hash_fn kf_sha3_224 = kf_scalar_sha3_224;
-kf_hash_fn kf_sha3_256 = kf_scalar_sha3_256;
-kf_hash_fn kf_sha3_384 = kf_scalar_sha3_384;
-kf_hash_fn kf_sha3_512 = kf_scalar_sha3_512;
-kf_hash_fn kf_turboshake128 = kf_scalar_turboshake128;
-kf_hash_fn kf_turboshake256 = kf_scalar_turboshake256;
-
-int kf_backend_priority = KF_PRIO_SCALAR;
-const char *kf_backend_label = "scalar";
-
-kf_batch_perm_fn kf_batch_perm = kf_scalar_batch;
-kf_batch_perm_fn kf_batch_perm12 = kf_scalar_batch12;
-int kf_batch_priority = KF_PRIO_SCALAR;
+kf_hash_fn kf_shake128 = NULL;
+kf_hash_fn kf_shake256 = NULL;
+kf_hash_fn kf_sha3_224 = NULL;
+kf_hash_fn kf_sha3_256 = NULL;
+kf_hash_fn kf_sha3_384 = NULL;
+kf_hash_fn kf_sha3_512 = NULL;
+kf_hash_fn kf_turboshake128 = NULL;
+kf_hash_fn kf_turboshake256 = NULL;
+
+int kf_backend_priority = 0;
+const char *kf_backend_label = "none";
+
+kf_batch_perm_fn kf_batch_perm = NULL;
+kf_batch_perm_fn kf_batch_perm12 = NULL;
+int kf_batch_priority = 0;
int kf_batch_lanes = 1;
-const char *kf_batch_label = "scalar";
+const char *kf_batch_label = "none";
const char *kf_backend_name(void) {
return kf_backend_label;
@@ -43,10 +43,12 @@ static void kf_sponge_group(kf_batch_perm_fn perm, const uint8_t *in,
size_t inlen, uint8_t *out, size_t outlen,
size_t base, size_t count, size_t rate,
uint8_t delim) {
- uint64_t states[KF_LANES_MAX][25] = {0};
- uint8_t *a = (uint8_t *)states;
- const uint8_t *p[KF_LANES_MAX];
- uint8_t *o[KF_LANES_MAX];
+ uint64_t states[count][25];
+ uint8_t *a = (uint8_t *)states;
+ const uint8_t *p[count];
+ uint8_t *o[count];
+
+ memset(states, 0, sizeof states);
for (size_t i = 0; i < count; i++) {
p[i] = in + (base + i) * inlen;
@@ -93,7 +95,6 @@ static int kf_batch_impl(kf_hash_fn one, kf_batch_perm_fn perm, size_t count,
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= 200)) return -1;
int lanes = kf_batch_lanes;
- if (lanes > KF_LANES_MAX) lanes = KF_LANES_MAX;
size_t base = 0;
if (lanes > 1) {
diff --git a/test/batch.c b/test/batch.c
@@ -3,10 +3,37 @@
#include <string.h>
#include "keccak-fast.h"
+#include "keccak-fast-internal.h"
typedef int (*kf_fn)(uint8_t *, size_t, const uint8_t *, size_t);
typedef int (*kf_batch_fn)(size_t, const uint8_t *, size_t, uint8_t *, size_t);
+void kf_scalar_batch(uint64_t states[][25], size_t count);
+void kf_scalar_batch12(uint64_t states[][25], size_t count);
+
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_AVX2)
+void kf_avx2_batch(uint64_t states[][25], size_t count);
+void kf_avx2_batch12(uint64_t states[][25], size_t count);
+#endif
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_AVX512)
+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)
+int kf_armsha3_available(void);
+void kf_armsha3_batch(uint64_t states[][25], size_t count);
+void kf_armsha3_batch12(uint64_t states[][25], size_t count);
+#endif
+
+typedef struct {
+ const char *label;
+ kf_batch_perm_fn perm;
+ kf_batch_perm_fn perm12;
+ int lanes;
+} kernel;
+
static int fails;
static int testn;
@@ -44,41 +71,69 @@ int main(void) {
static uint8_t want[17 * 64];
static uint8_t got[17 * 64];
+ kernel kernels[4];
+ int nk = 0;
+
+ kernels[nk++] = (kernel){"lanes=1", kf_scalar_batch, kf_scalar_batch12, 1};
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_AVX2)
+ if (__builtin_cpu_supports("avx2"))
+ kernels[nk++] = (kernel){"avx2 x4", kf_avx2_batch, kf_avx2_batch12, 4};
+#endif
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_AVX512)
+ if (__builtin_cpu_supports("avx512f"))
+ kernels[nk++] =
+ (kernel){"avx512 x8", kf_avx512_batch, kf_avx512_batch12, 8};
+#endif
+#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
+ if (kf_armsha3_available())
+ kernels[nk++] =
+ (kernel){"arm-sha3 x2", kf_armsha3_batch, kf_armsha3_batch12, 2};
+#endif
+
printf("1..%zu\n", (sizeof algos / sizeof algos[0]) *
(sizeof counts / sizeof counts[0]) *
- (sizeof inlens / sizeof inlens[0]));
-
- for (size_t a = 0; a < sizeof algos / sizeof algos[0]; a++) {
- for (size_t ci = 0; ci < sizeof counts / sizeof counts[0]; ci++) {
- size_t count = counts[ci];
- for (size_t li = 0; li < sizeof inlens / sizeof inlens[0]; li++) {
- size_t inlen = inlens[li];
- size_t outlen = algos[a].outlen;
- char name[64];
-
- for (size_t m = 0; m < count; m++) {
- for (size_t j = 0; j < inlen; j++) {
- in[m * inlen + j] = (uint8_t)(j * 31 + m * 7 + 1);
+ (sizeof inlens / sizeof inlens[0]) * (size_t)nk);
+
+ for (int k = 0; k < nk; k++) {
+ kf_batch_perm = kernels[k].perm;
+ kf_batch_perm12 = kernels[k].perm12;
+ kf_batch_lanes = kernels[k].lanes;
+
+ for (size_t a = 0; a < sizeof algos / sizeof algos[0]; a++) {
+ for (size_t ci = 0; ci < sizeof counts / sizeof counts[0]; ci++) {
+ size_t count = counts[ci];
+ for (size_t li = 0; li < sizeof inlens / sizeof inlens[0]; li++) {
+ size_t inlen = inlens[li];
+ size_t outlen = algos[a].outlen;
+ char name[96];
+
+ for (size_t m = 0; m < count; m++) {
+ for (size_t j = 0; j < inlen; j++) {
+ in[m * inlen + j] = (uint8_t)(j * 31 + m * 7 + 1);
+ }
}
- }
- for (size_t m = 0; m < count; m++) {
- int rc = algos[a].one(want + m * outlen, outlen, in + m * inlen,
- inlen);
- if (rc != 0) {
- printf("# %s one-shot returned %d\n", algos[a].name, rc);
+ for (size_t m = 0; m < count; m++) {
+ int rc = algos[a].one(want + m * outlen, outlen, in + m * inlen,
+ inlen);
+ if (rc != 0) {
+ printf("# %s one-shot returned %d\n", algos[a].name, rc);
+ }
}
- }
- memset(got, 0, count * outlen);
- int rc = algos[a].batch(count, in, inlen, got, outlen);
- snprintf(name, sizeof name, "%s count=%zu inlen=%zu", algos[a].name,
- count, inlen);
- check(name, rc == 0 && memcmp(want, got, count * outlen) == 0);
+ memset(got, 0, count * outlen);
+ int rc = algos[a].batch(count, in, inlen, got, outlen);
+ snprintf(name, sizeof name, "%s %s count=%zu inlen=%zu",
+ kernels[k].label, algos[a].name, count, inlen);
+ check(name, rc == 0 && memcmp(want, got, count * outlen) == 0);
+ }
}
}
}
- printf("# batch backend: %s\n", kf_batch_name());
+ printf("# dispatched backend: %s, batch: %s\n", kf_backend_name(),
+ kf_batch_name());
if (fails != 0) printf("# %d failed\n", fails);
return fails != 0;
diff --git a/test/kat.c b/test/kat.c
@@ -6,6 +6,51 @@
typedef int (*kf_fn)(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_shake128(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_shake256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_sha3_224(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_sha3_256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_sha3_384(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_sha3_512(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_turboshake128(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_scalar_turboshake256(uint8_t *, size_t, const uint8_t *, size_t);
+
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_BMI)
+int kf_bmi_shake128(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_shake256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_sha3_224(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_sha3_256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_sha3_384(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_sha3_512(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_turboshake128(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_bmi_turboshake256(uint8_t *, size_t, const uint8_t *, size_t);
+#endif
+
+#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
+int kf_armsha3_available(void);
+int kf_armsha3_shake128(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_shake256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_sha3_224(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_sha3_256(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_sha3_384(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_sha3_512(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_turboshake128(uint8_t *, size_t, const uint8_t *, size_t);
+int kf_armsha3_turboshake256(uint8_t *, size_t, const uint8_t *, size_t);
+#endif
+
+typedef struct {
+ const char *label;
+ kf_fn shake128;
+ kf_fn shake256;
+ kf_fn sha3_224;
+ kf_fn sha3_256;
+ kf_fn sha3_384;
+ kf_fn sha3_512;
+ kf_fn turboshake128;
+ kf_fn turboshake256;
+} backend;
+
static int fails;
static int testn;
@@ -17,8 +62,8 @@ static void hex2bin(const char *hex, uint8_t *out) {
}
}
-static void check(const char *name, kf_fn fn, const uint8_t *in, size_t inlen,
- const char *hex) {
+static void check(const char *be, const char *name, kf_fn fn,
+ const uint8_t *in, size_t inlen, const char *hex) {
uint8_t got[64] = {0};
uint8_t want[64] = {0};
size_t outlen = strlen(hex) / 2;
@@ -26,84 +71,126 @@ static void check(const char *name, kf_fn fn, const uint8_t *in, size_t inlen,
hex2bin(hex, want);
testn++;
if (rc == 0 && memcmp(got, want, outlen) == 0) {
- printf("ok %d - %s\n", testn, name);
+ printf("ok %d - %s %s\n", testn, be, name);
} else {
fails++;
- printf("not ok %d - %s\n", testn, name);
+ printf("not ok %d - %s %s\n", testn, be, name);
}
}
-int main(void) {
- static uint8_t qbf[200];
- for (size_t i = 0; i < sizeof qbf; i++) qbf[i] = (uint8_t)i;
+static void run(const backend *b, const uint8_t *qbf) {
const uint8_t *abc = (const uint8_t *)"abc";
const uint8_t *nil = NULL;
- printf("1..22\n");
-
- check("shake128 empty", kf_shake128, nil, 0,
+ check(b->label, "shake128 empty", b->shake128, nil, 0,
"7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26");
- check("shake128 abc", kf_shake128, abc, 3,
+ check(b->label, "shake128 abc", b->shake128, abc, 3,
"5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8");
- check("shake256 empty", kf_shake256, nil, 0,
+ check(b->label, "shake256 empty", b->shake256, nil, 0,
"46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f"
"d75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be");
- check("shake256 abc", kf_shake256, abc, 3,
+ check(b->label, "shake256 abc", b->shake256, abc, 3,
"483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739"
"d5a15bef186a5386c75744c0527e1faa9f8726e462a12a4feb06bd8801e751e4");
- check("sha3_224 empty", kf_sha3_224, nil, 0,
+ check(b->label, "sha3_224 empty", b->sha3_224, nil, 0,
"6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7");
- check("sha3_224 abc", kf_sha3_224, abc, 3,
+ check(b->label, "sha3_224 abc", b->sha3_224, abc, 3,
"e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf");
- check("sha3_256 empty", kf_sha3_256, nil, 0,
+ check(b->label, "sha3_256 empty", b->sha3_256, nil, 0,
"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a");
- check("sha3_256 abc", kf_sha3_256, abc, 3,
+ check(b->label, "sha3_256 abc", b->sha3_256, abc, 3,
"3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532");
- check("sha3_256 200", kf_sha3_256, qbf, 200,
+ check(b->label, "sha3_256 200", b->sha3_256, qbf, 200,
"5f728f63bf5ee48c77f453c0490398fa645b8d4c4e56be9a41cfec344d6ca899");
- check("sha3_384 empty", kf_sha3_384, nil, 0,
+ check(b->label, "sha3_384 empty", b->sha3_384, nil, 0,
"0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"
"c3713831264adb47fb6bd1e058d5f004");
- check("sha3_384 abc", kf_sha3_384, abc, 3,
+ check(b->label, "sha3_384 abc", b->sha3_384, abc, 3,
"ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b2"
"98d88cea927ac7f539f1edf228376d25");
- check("sha3_512 empty", kf_sha3_512, nil, 0,
+ check(b->label, "sha3_512 empty", b->sha3_512, nil, 0,
"a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"
"15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26");
- check("sha3_512 abc", kf_sha3_512, abc, 3,
+ check(b->label, "sha3_512 abc", b->sha3_512, abc, 3,
"b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e"
"10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0");
- check("turboshake128 empty", kf_turboshake128, nil, 0,
+ check(b->label, "turboshake128 empty", b->turboshake128, nil, 0,
"1e415f1c5983aff2169217277d17bb538cd945a397ddec541f1ce41af2c1b74c");
- check("turboshake128 abc", kf_turboshake128, abc, 3,
+ check(b->label, "turboshake128 abc", b->turboshake128, abc, 3,
"dcf1646dfe993a8eb6b782d1faaca6d82416a5dcf1de98ee3c6dbc5e1dc63018");
- check("turboshake128 200", kf_turboshake128, qbf, 200,
+ check(b->label, "turboshake128 200", b->turboshake128, qbf, 200,
"5b96f9aac16a9047cd03b83c950fb6b074a31ea05f0aaa3aa3a7f98deaab289c");
- check("turboshake256 empty", kf_turboshake256, nil, 0,
+ check(b->label, "turboshake256 empty", b->turboshake256, nil, 0,
"367a329dafea871c7802ec67f905ae13c57695dc2c6663c61035f59a18f8e7db"
"11edc0e12e91ea60eb6b32df06dd7f002fbafabb6e13ec1cc20d995547600db0");
- check("turboshake256 abc", kf_turboshake256, abc, 3,
+ check(b->label, "turboshake256 abc", b->turboshake256, abc, 3,
"63824b1431a7372e85edc022c9d7afdd027472fcfa33c887d6f5aaf8dc5d4db6"
"8afbcb5714b49b7ffd8dd115dd5bd5436f837236845a230d6969a4083a113617");
- check("turboshake256 200", kf_turboshake256, qbf, 200,
+ check(b->label, "turboshake256 200", b->turboshake256, qbf, 200,
"c27b5422da9177efaf2a335b6ea79cb57961a8cc3ca8f870f17ebbbfd662086a"
"fe62a10085fb7c413fcc0f586aa938d152254a77aa0d682eb846c82199428a71");
- {
- uint8_t out[64];
- int bad1 = kf_sha3_256(out, 33, abc, 3);
- int bad2 = kf_sha3_256(NULL, 32, abc, 3);
- int bad3 = kf_shake256(out, 32, NULL, 3);
- printf("%s %d - sha3_256 rejects long output\n", bad1 == -1 ? "ok" : "not ok",
- ++testn);
- printf("%s %d - rejects NULL output\n", bad2 == -1 ? "ok" : "not ok",
- ++testn);
- printf("%s %d - rejects NULL input\n", bad3 == -1 ? "ok" : "not ok",
- ++testn);
- if (bad1 != -1 || bad2 != -1 || bad3 != -1) fails++;
- }
+ uint8_t out[64];
+ int bad1 = b->sha3_256(out, 33, abc, 3);
+ int bad2 = b->sha3_256(NULL, 32, abc, 3);
+ int bad3 = b->shake256(out, 32, NULL, 3);
+ printf("%s %d - %s sha3_256 rejects long output\n",
+ bad1 == -1 ? "ok" : "not ok", ++testn, b->label);
+ printf("%s %d - %s rejects NULL output\n", bad2 == -1 ? "ok" : "not ok",
+ ++testn, b->label);
+ printf("%s %d - %s rejects NULL input\n", bad3 == -1 ? "ok" : "not ok",
+ ++testn, b->label);
+ if (bad1 != -1 || bad2 != -1 || bad3 != -1) fails++;
+}
+
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_BMI)
+static int have_bmi(void) {
+#if defined(__clang__)
+ return __builtin_cpu_supports("bmi");
+#else
+ return __builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2");
+#endif
+}
+#endif
+
+int main(void) {
+ static uint8_t qbf[200];
+ backend bes[4];
+ int nb = 0;
+
+ for (size_t i = 0; i < sizeof qbf; i++) qbf[i] = (uint8_t)i;
+
+ bes[nb++] = (backend){"dispatch", kf_shake128, kf_shake256,
+ kf_sha3_224, kf_sha3_256, kf_sha3_384, kf_sha3_512,
+ kf_turboshake128, kf_turboshake256};
+ bes[nb++] = (backend){"scalar", kf_scalar_shake128, kf_scalar_shake256,
+ kf_scalar_sha3_224, kf_scalar_sha3_256,
+ kf_scalar_sha3_384, kf_scalar_sha3_512,
+ kf_scalar_turboshake128, kf_scalar_turboshake256};
+#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \
+ !defined(KECCAK_FAST_NO_BMI)
+ if (have_bmi())
+ bes[nb++] = (backend){"scalar+bmi", kf_bmi_shake128, kf_bmi_shake256,
+ kf_bmi_sha3_224, kf_bmi_sha3_256, kf_bmi_sha3_384,
+ kf_bmi_sha3_512, kf_bmi_turboshake128,
+ kf_bmi_turboshake256};
+#endif
+#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3)
+ if (kf_armsha3_available())
+ bes[nb++] = (backend){"arm-sha3", kf_armsha3_shake128,
+ kf_armsha3_shake256, kf_armsha3_sha3_224,
+ kf_armsha3_sha3_256, kf_armsha3_sha3_384,
+ kf_armsha3_sha3_512, kf_armsha3_turboshake128,
+ kf_armsha3_turboshake256};
+#endif
+
+ printf("1..%d\n", 22 * nb);
+ for (int i = 0; i < nb; i++) run(&bes[i], qbf);
+
+ printf("# dispatched backend: %s\n", kf_backend_name());
if (fails != 0) printf("# %d failed\n", fails);
return fails != 0;