commit 0316ebb211739fbe432b9218ec2beab5691c5d84
parent 3ea97f4b05389e9e113b39cdb89bb78b37c458fe
Author: finwo <finwo@pm.me>
Date: Tue, 13 Oct 2026 16:15:53 +0200
Add fused iterated sponge kernels and score priorities on measured throughput
Diffstat:
9 files changed, 470 insertions(+), 31 deletions(-)
diff --git a/README.md b/README.md
@@ -1,8 +1,8 @@
# keccak-fast
Drop-in-speed replacement for `coruus/keccak-tiny`. SHA-3/SHAKE one-shots,
-TurboSHAKE128/256, and batch entry points. Plain C11, libc only, no build
-flags, runtime-dispatched, thread-safe.
+TurboSHAKE128/256, batch entry points and fused iterated chains. Plain C11,
+libc only, no build flags, runtime-dispatched, thread-safe.
## Use
@@ -11,6 +11,7 @@ flags, runtime-dispatched, thread-safe.
kf_shake256(out, sizeof out, in, inlen); /* 0, or -1 */
kf_shake256_batch(count, in, inlen, out, outlen); /* equal-length, packed */
+kf_shake256_chain(count, x, 32, tail, 8, 1, 0, 256);
```
The one-shots (`kf_shake128`, `kf_shake256`, `kf_sha3_224`, `kf_sha3_256`,
@@ -21,6 +22,33 @@ TurboSHAKE is the same sponge over Keccak-p[1600,12] with domain `0x1F`, about
1.9x faster than SHAKE; its batch entries use a separately registered 12-round
permutation.
+## Chains
+
+`kf_<algo>_chain` iterates a hash into itself, `count` chains at a time:
+
+```
+x_r = H(x_r || tail_r || ctr) repeated `iters` times
+```
+
+`tail_r` is `tail_len` caller-defined bytes, constant across iterations and
+packed back to back like the batch inputs. `ctr` is `ctr_len` bytes, 0 or 1,
+starting at `ctr0` and incremented every iteration, so all four of plain
+iterated hashing, counter-only chains, tweaked chains and both together are
+expressible. A wider counter is deliberately not offered: it would force an
+endianness choice into every kernel, and the construction composes by calling
+again with an updated tail.
+
+Each iteration must be a single permutation, so `outlen + tail_len + ctr_len`
+has to fit in `rate - 1`; anything larger returns `-1`. At the tight end that
+is SHA3-512, where `rate` is 72 and a 64-byte digest leaves 7 bytes.
+
+The win is that the vector backends keep the state in registers across
+iterations. The batch entry points only expose a permutation, so a feedback
+loop built on them pays a full transpose and a scalar byte-wise absorb per
+step; the chain kernels rewrite only the words below the digest and permute
+again. `kf_chain_name()` reports the active kernel, which is tracked and
+dispatched separately from the batch one.
+
## Build
```
@@ -31,17 +59,35 @@ dep add finwo/keccak-fast
## Backends
-| backend | one-shot | batch |
-| ---------- | -------- | ----- |
-| 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, arm-sha3 10, avx2 20, avx512 30). `kf_backend_name()`
-and `kf_batch_name()` report the active ones. `KECCAK_FAST_NO_BMI`,
+| backend | one-shot | batch | chain |
+| ---------- | -------- | ----- | -------- |
+| scalar | yes | - | x1 |
+| scalar+bmi | yes | - | x1 |
+| arm-sha3 | yes | x2 | x2 |
+| AVX2 | no | x4 | x4 fused |
+| AVX-512 | no | x8 | x8 fused |
+
+Backends register from constructors and the highest available priority wins.
+Priority is measured, not ranked: scalar is 100 and every other number is that
+backend's throughput relative to scalar on the same track, so 265 means 2.65x
+scalar. One-shot is scored on absorb MiB/s, batch and chain on hashes per
+second, both from `make bench` at SHA3-256.
+
+| backend | one-shot | batch | chain |
+| ---------- | -------- | ----- | ----- |
+| scalar | 100 | 100 | 100 |
+| scalar+bmi | 115 | 114 | 114 |
+| arm-sha3 | 101 | 182 | 194 |
+| AVX2 | - | 218 | 265 |
+| AVX-512 | - | 447 | 786 |
+
+These are a fixed approximation of a quantity that varies by core, so they
+encode the ordering rather than a promise. `arm-sha3` is the one to watch: it
+measures at parity with scalar for one-shot on a Cortex-A72 and about 1.4x on
+Apple silicon, so 101 is conservative and the pair kernels carry its win.
+Re-run `make bench` on a new target before trusting the one-shot ordering
+there. `kf_backend_name()`, `kf_batch_name()` and `kf_chain_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.
@@ -67,12 +113,18 @@ 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
sponge boundary. Their round bodies unroll theta and chi by hand so `-O2` emits
-the same code as `-O3`.
+the same code as `-O3`. Their chain kernels transpose once on entry and once on
+exit rather than per iteration, rebuilding the message in the transposed domain
+between permutations; a digest whose length is not a multiple of 8 leaves one
+word straddling the boundary, which is masked rather than copied. The scalar,
+`scalar+bmi` and `arm-sha3` chain kernels reuse their own permutations and
+rebuild the block in memory, so they gain correctness and batching but not the
+register residency.
## Dev
```
-make test # KAT and batch differential (TAP)
+make test # KAT, batch and chain differentials (TAP)
make bench # per-hash table: every backend, plus the donor
```
diff --git a/src/backend/arm_sha3.c b/src/backend/arm_sha3.c
@@ -151,6 +151,42 @@ void kf_armsha3_batch12(uint64_t states[][25], size_t count) {
}
}
+#define KF_DEFARMCHAIN(NAME, PERM2, PERM1) \
+ void NAME(size_t count, uint8_t *x, size_t outlen, const uint8_t *tail, \
+ size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters, \
+ size_t rate, uint8_t delim) { \
+ uint64_t st[2][25]; \
+ size_t i = 0; \
+ for (; i + 2 <= count; i += 2) { \
+ for (uint32_t it = 0; it < iters; it++) { \
+ for (size_t l = 0; l < 2; l++) { \
+ const uint8_t *tr = tail_len ? tail + (i + l) * tail_len : NULL; \
+ kf_chain_fill(st[l], x + (i + l) * outlen, outlen, tr, tail_len, \
+ ctr_len, (uint8_t)(ctr0 + it), rate, delim); \
+ } \
+ PERM2(st); \
+ for (size_t l = 0; l < 2; l++) { \
+ memcpy(x + (i + l) * outlen, st[l], outlen); \
+ } \
+ } \
+ } \
+ for (; i < count; i++) { \
+ uint8_t *xr = x + i * outlen; \
+ const uint8_t *tr = tail_len ? tail + i * tail_len : NULL; \
+ for (uint32_t it = 0; it < iters; it++) { \
+ kf_chain_fill(st[0], xr, outlen, tr, tail_len, ctr_len, \
+ (uint8_t)(ctr0 + it), rate, delim); \
+ PERM1(st[0]); \
+ memcpy(xr, st[0], outlen); \
+ } \
+ } \
+ }
+
+KF_DEFARMCHAIN(kf_armsha3_chain, keccakf2_armsha3, keccakf_armsha3)
+KF_DEFARMCHAIN(kf_armsha3_chain12, keccak12_2_armsha3, keccak12_armsha3)
+
+#undef KF_DEFARMCHAIN
+
#if defined(__clang__)
#pragma clang attribute pop
#elif defined(__GNUC__)
@@ -176,7 +212,7 @@ __attribute__((constructor)) static void kf_armsha3_register(void) {
if (!kf_armsha3_available()) {
return;
}
- if (kf_backend_priority <= 10) {
+ if (kf_backend_priority <= 101) {
kf_shake128 = kf_armsha3_shake128;
kf_shake256 = kf_armsha3_shake256;
kf_sha3_224 = kf_armsha3_sha3_224;
@@ -185,16 +221,23 @@ __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 = 10;
+ kf_backend_priority = 101;
kf_backend_label = "arm-sha3";
}
- if (kf_batch_priority <= 10) {
+ if (kf_batch_priority <= 182) {
kf_batch_perm = kf_armsha3_batch;
kf_batch_perm12 = kf_armsha3_batch12;
- kf_batch_priority = 10;
+ kf_batch_priority = 182;
kf_batch_lanes = 2;
kf_batch_label = "arm-sha3";
}
+ if (kf_chain_priority <= 194) {
+ kf_chain_kernel = kf_armsha3_chain;
+ kf_chain_kernel12 = kf_armsha3_chain12;
+ kf_chain_priority = 194;
+ kf_chain_lanes = 2;
+ kf_chain_label = "arm-sha3";
+ }
}
#endif /* aarch64 && !KECCAK_FAST_NO_SHA3 */
diff --git a/src/backend/avx2.c b/src/backend/avx2.c
@@ -216,17 +216,102 @@ void kf_avx2_batch12(uint64_t states[][25], size_t count) {
}
}
+__attribute__((target("avx2"), always_inline))
+static inline void kf_avx2_xin(uint64_t s[4][25], __m256i v[25]) {
+ __m256i t[4];
+ KF_LOAD4_ALL();
+}
+
+__attribute__((target("avx2"), always_inline))
+static inline void kf_avx2_xout(uint64_t s[4][25], __m256i v[25]) {
+ __m256i t[4];
+ uint64_t tail[4];
+ KF_STORE4_ALL();
+}
+
+/* The digest stays in v; only the words below it are rewritten per iteration */
+#define KF_DEF_AVX2_CHAIN_X4(NAME, R0) \
+ __attribute__((target("avx2"))) static void NAME( \
+ uint64_t s[4][25], uint64_t tmpl[4][25], size_t full_words, \
+ uint64_t keep_mask, size_t ctr_word, unsigned ctr_shift, size_t ctr_len, \
+ uint8_t ctr0, uint32_t iters) { \
+ __m256i v[25]; \
+ __m256i tv[25]; \
+ __m256i km = _mm256_set1_epi64x((long long)keep_mask); \
+ kf_avx2_xin(s, v); \
+ kf_avx2_xin(tmpl, tv); \
+ for (uint32_t it = 0;;) { \
+ for (int r = R0; r < 24; r++) kf_avx2_round_x4(v, RC[r]); \
+ if (++it >= iters) break; \
+ size_t w = full_words; \
+ if (keep_mask) { \
+ v[w] = _mm256_or_si256(_mm256_and_si256(v[w], km), tv[w]); \
+ w++; \
+ } \
+ for (; w < 25; w++) v[w] = tv[w]; \
+ if (ctr_len) { \
+ uint64_t c = (uint64_t)(uint8_t)(ctr0 + it) << ctr_shift; \
+ v[ctr_word] = \
+ _mm256_or_si256(v[ctr_word], _mm256_set1_epi64x((long long)c)); \
+ } \
+ } \
+ kf_avx2_xout(s, v); \
+ }
+
+KF_DEF_AVX2_CHAIN_X4(kf_avx2_chain_x4, 0)
+KF_DEF_AVX2_CHAIN_X4(kf_avx2_chain_x4_12, 12)
+
+#define KF_DEF_AVX2_CHAIN(NAME, X4) \
+ __attribute__((target("avx2"))) void NAME( \
+ size_t count, uint8_t *x, size_t outlen, const uint8_t *tail, \
+ size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters, \
+ size_t rate, uint8_t delim) { \
+ uint64_t s[4][25]; \
+ uint64_t tmpl[4][25]; \
+ size_t fw = outlen / 8; \
+ size_t part = outlen % 8; \
+ uint64_t km = part ? (((uint64_t)1 << (8 * part)) - 1) : 0; \
+ size_t cw = (outlen + tail_len) / 8; \
+ unsigned cs = (unsigned)(8 * ((outlen + tail_len) % 8)); \
+ for (size_t i = 0; i < count; i += 4) { \
+ size_t n = count - i < 4 ? count - i : 4; \
+ for (size_t l = 0; l < 4; l++) { \
+ int live = l < n; \
+ const uint8_t *tr = \
+ tail_len ? tail + (live ? (i + l) : 0) * tail_len : NULL; \
+ kf_chain_fill(s[l], live ? x + (i + l) * outlen : NULL, outlen, tr, \
+ tail_len, ctr_len, ctr0, rate, delim); \
+ kf_chain_fill(tmpl[l], NULL, outlen, tr, tail_len, ctr_len, 0, rate, \
+ delim); \
+ } \
+ X4(s, tmpl, fw, km, cw, cs, ctr_len, ctr0, iters); \
+ for (size_t l = 0; l < n; l++) { \
+ memcpy(x + (i + l) * outlen, s[l], outlen); \
+ } \
+ } \
+ }
+
+KF_DEF_AVX2_CHAIN(kf_avx2_chain, kf_avx2_chain_x4)
+KF_DEF_AVX2_CHAIN(kf_avx2_chain12, kf_avx2_chain_x4_12)
+
__attribute__((constructor)) static void kf_avx2_register(void) {
if (!__builtin_cpu_supports("avx2")) {
return;
}
- if (kf_batch_priority <= 20) {
+ if (kf_batch_priority <= 218) {
kf_batch_perm = kf_avx2_batch;
kf_batch_perm12 = kf_avx2_batch12;
- kf_batch_priority = 20;
+ kf_batch_priority = 218;
kf_batch_lanes = 4;
kf_batch_label = "avx2";
}
+ if (kf_chain_priority <= 265) {
+ kf_chain_kernel = kf_avx2_chain;
+ kf_chain_kernel12 = kf_avx2_chain12;
+ kf_chain_priority = 265;
+ kf_chain_lanes = 4;
+ kf_chain_label = "avx2";
+ }
}
#endif
diff --git a/src/backend/avx512.c b/src/backend/avx512.c
@@ -225,17 +225,101 @@ void kf_avx512_batch12(uint64_t states[][25], size_t count) {
}
}
+__attribute__((target("avx512f"), always_inline))
+static inline void kf_avx512_xin(uint64_t s[8][25], __m512i v[25]) {
+ __m512i t[8];
+ KF_LOAD8_ALL();
+}
+
+__attribute__((target("avx512f"), always_inline))
+static inline void kf_avx512_xout(uint64_t s[8][25], __m512i v[25]) {
+ __m512i t[8];
+ uint64_t tail[8];
+ KF_STORE8_ALL();
+}
+
+#define KF_DEF_AVX512_CHAIN_X8(NAME, R0) \
+ __attribute__((target("avx512f"))) static void NAME( \
+ uint64_t s[8][25], uint64_t tmpl[8][25], size_t full_words, \
+ uint64_t keep_mask, size_t ctr_word, unsigned ctr_shift, size_t ctr_len, \
+ uint8_t ctr0, uint32_t iters) { \
+ __m512i v[25]; \
+ __m512i tv[25]; \
+ __m512i km = _mm512_set1_epi64((long long)keep_mask); \
+ kf_avx512_xin(s, v); \
+ kf_avx512_xin(tmpl, tv); \
+ for (uint32_t it = 0;;) { \
+ for (int r = R0; r < 24; r++) kf_avx512_round_x8(v, RC[r]); \
+ if (++it >= iters) break; \
+ size_t w = full_words; \
+ if (keep_mask) { \
+ v[w] = _mm512_or_si512(_mm512_and_si512(v[w], km), tv[w]); \
+ w++; \
+ } \
+ for (; w < 25; w++) v[w] = tv[w]; \
+ if (ctr_len) { \
+ uint64_t c = (uint64_t)(uint8_t)(ctr0 + it) << ctr_shift; \
+ v[ctr_word] = \
+ _mm512_or_si512(v[ctr_word], _mm512_set1_epi64((long long)c)); \
+ } \
+ } \
+ kf_avx512_xout(s, v); \
+ }
+
+KF_DEF_AVX512_CHAIN_X8(kf_avx512_chain_x8, 0)
+KF_DEF_AVX512_CHAIN_X8(kf_avx512_chain_x8_12, 12)
+
+#define KF_DEF_AVX512_CHAIN(NAME, X8) \
+ __attribute__((target("avx512f"))) void NAME( \
+ size_t count, uint8_t *x, size_t outlen, const uint8_t *tail, \
+ size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters, \
+ size_t rate, uint8_t delim) { \
+ uint64_t s[8][25]; \
+ uint64_t tmpl[8][25]; \
+ size_t fw = outlen / 8; \
+ size_t part = outlen % 8; \
+ uint64_t km = part ? (((uint64_t)1 << (8 * part)) - 1) : 0; \
+ size_t cw = (outlen + tail_len) / 8; \
+ unsigned cs = (unsigned)(8 * ((outlen + tail_len) % 8)); \
+ for (size_t i = 0; i < count; i += 8) { \
+ size_t n = count - i < 8 ? count - i : 8; \
+ for (size_t l = 0; l < 8; l++) { \
+ int live = l < n; \
+ const uint8_t *tr = \
+ tail_len ? tail + (live ? (i + l) : 0) * tail_len : NULL; \
+ kf_chain_fill(s[l], live ? x + (i + l) * outlen : NULL, outlen, tr, \
+ tail_len, ctr_len, ctr0, rate, delim); \
+ kf_chain_fill(tmpl[l], NULL, outlen, tr, tail_len, ctr_len, 0, rate, \
+ delim); \
+ } \
+ X8(s, tmpl, fw, km, cw, cs, ctr_len, ctr0, iters); \
+ for (size_t l = 0; l < n; l++) { \
+ memcpy(x + (i + l) * outlen, s[l], outlen); \
+ } \
+ } \
+ }
+
+KF_DEF_AVX512_CHAIN(kf_avx512_chain, kf_avx512_chain_x8)
+KF_DEF_AVX512_CHAIN(kf_avx512_chain12, kf_avx512_chain_x8_12)
+
__attribute__((constructor)) static void kf_avx512_register(void) {
if (!__builtin_cpu_supports("avx512f")) {
return;
}
- if (kf_batch_priority <= 30) {
+ if (kf_batch_priority <= 447) {
kf_batch_perm = kf_avx512_batch;
kf_batch_perm12 = kf_avx512_batch12;
- kf_batch_priority = 30;
+ kf_batch_priority = 447;
kf_batch_lanes = 8;
kf_batch_label = "avx512";
}
+ if (kf_chain_priority <= 786) {
+ kf_chain_kernel = kf_avx512_chain;
+ kf_chain_kernel12 = kf_avx512_chain12;
+ kf_chain_priority = 786;
+ kf_chain_lanes = 8;
+ kf_chain_label = "avx512";
+ }
}
#endif
diff --git a/src/backend/scalar.c b/src/backend/scalar.c
@@ -328,8 +328,30 @@ void kf_scalar_batch12(uint64_t states[][25], size_t count) {
}
}
+#define KF_DEFSCALARCHAIN(NAME, PERM) \
+ void NAME(size_t count, uint8_t *x, size_t outlen, const uint8_t *tail, \
+ size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters, \
+ size_t rate, uint8_t delim) { \
+ uint64_t st[25]; \
+ for (size_t r = 0; r < count; r++) { \
+ uint8_t *xr = x + r * outlen; \
+ const uint8_t *tr = tail_len ? tail + r * tail_len : NULL; \
+ for (uint32_t it = 0; it < iters; it++) { \
+ kf_chain_fill(st, xr, outlen, tr, tail_len, ctr_len, \
+ (uint8_t)(ctr0 + it), rate, delim); \
+ PERM(st); \
+ memcpy(xr, st, outlen); \
+ } \
+ } \
+ }
+
+KF_DEFSCALARCHAIN(kf_scalar_chain, keccakf)
+KF_DEFSCALARCHAIN(kf_scalar_chain12, keccak12)
+
+#undef KF_DEFSCALARCHAIN
+
__attribute__((constructor)) static void kf_scalar_register(void) {
- if (kf_backend_priority <= 0) {
+ if (kf_backend_priority <= 100) {
kf_shake128 = kf_scalar_shake128;
kf_shake256 = kf_scalar_shake256;
kf_sha3_224 = kf_scalar_sha3_224;
@@ -338,16 +360,23 @@ __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 = 0;
+ kf_backend_priority = 100;
kf_backend_label = "scalar";
}
- if (kf_batch_priority <= 0) {
+ if (kf_batch_priority <= 100) {
kf_batch_perm = kf_scalar_batch;
kf_batch_perm12 = kf_scalar_batch12;
- kf_batch_priority = 0;
+ kf_batch_priority = 100;
kf_batch_lanes = 1;
kf_batch_label = "scalar";
}
+ if (kf_chain_priority <= 100) {
+ kf_chain_kernel = kf_scalar_chain;
+ kf_chain_kernel12 = kf_scalar_chain12;
+ kf_chain_priority = 100;
+ kf_chain_lanes = 1;
+ kf_chain_label = "scalar";
+ }
}
#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
diff --git a/src/backend/scalar_bmi.c b/src/backend/scalar_bmi.c
@@ -330,6 +330,28 @@ void kf_bmi_batch12(uint64_t states[][25], size_t count) {
}
}
+#define KF_DEFBMICHAIN(NAME, PERM) \
+ void NAME(size_t count, uint8_t *x, size_t outlen, const uint8_t *tail, \
+ size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters, \
+ size_t rate, uint8_t delim) { \
+ uint64_t st[25]; \
+ for (size_t r = 0; r < count; r++) { \
+ uint8_t *xr = x + r * outlen; \
+ const uint8_t *tr = tail_len ? tail + r * tail_len : NULL; \
+ for (uint32_t it = 0; it < iters; it++) { \
+ kf_chain_fill(st, xr, outlen, tr, tail_len, ctr_len, \
+ (uint8_t)(ctr0 + it), rate, delim); \
+ PERM(st); \
+ memcpy(xr, st, outlen); \
+ } \
+ } \
+ }
+
+KF_DEFBMICHAIN(kf_bmi_chain, keccakf)
+KF_DEFBMICHAIN(kf_bmi_chain12, keccak12)
+
+#undef KF_DEFBMICHAIN
+
__attribute__((constructor)) static void kf_bmi_register(void) {
#if defined(__clang__)
if (!__builtin_cpu_supports("bmi")) {
@@ -338,7 +360,7 @@ __attribute__((constructor)) static void kf_bmi_register(void) {
#endif
return;
}
- if (kf_backend_priority <= 10) {
+ if (kf_backend_priority <= 115) {
kf_shake128 = kf_bmi_shake128;
kf_shake256 = kf_bmi_shake256;
kf_sha3_224 = kf_bmi_sha3_224;
@@ -347,16 +369,23 @@ __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 = 10;
+ kf_backend_priority = 115;
kf_backend_label = "scalar+bmi";
}
- if (kf_batch_priority <= 10) {
+ if (kf_batch_priority <= 114) {
kf_batch_perm = kf_bmi_batch;
kf_batch_perm12 = kf_bmi_batch12;
- kf_batch_priority = 10;
+ kf_batch_priority = 114;
kf_batch_lanes = 1;
kf_batch_label = "scalar+bmi";
}
+ if (kf_chain_priority <= 114) {
+ kf_chain_kernel = kf_bmi_chain;
+ kf_chain_kernel12 = kf_bmi_chain12;
+ kf_chain_priority = 114;
+ kf_chain_lanes = 1;
+ kf_chain_label = "scalar+bmi";
+ }
}
#if defined(__clang__)
diff --git a/src/keccak-fast-internal.h b/src/keccak-fast-internal.h
@@ -3,18 +3,49 @@
#include <stddef.h>
#include <stdint.h>
+#include <string.h>
#include "keccak-fast.h"
typedef void (*kf_batch_perm_fn)(uint64_t states[][25], size_t count);
+typedef void (*kf_chain_fn)(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len,
+ size_t ctr_len, uint8_t ctr0, uint32_t iters,
+ size_t rate, uint8_t delim);
+
extern kf_batch_perm_fn kf_batch_perm;
extern kf_batch_perm_fn kf_batch_perm12;
extern int kf_batch_priority;
extern int kf_batch_lanes;
+extern kf_chain_fn kf_chain_kernel;
+extern kf_chain_fn kf_chain_kernel12;
+extern int kf_chain_priority;
+extern int kf_chain_lanes;
+
extern int kf_backend_priority;
extern const char *kf_backend_label;
extern const char *kf_batch_label;
+extern const char *kf_chain_label;
+
+static inline void kf_chain_fill(uint64_t st[25], const uint8_t *x,
+ size_t outlen, const uint8_t *tail,
+ size_t tail_len, size_t ctr_len, uint8_t ctr,
+ size_t rate, uint8_t delim) {
+ uint8_t *b = (uint8_t *)st;
+ memset(st, 0, 200);
+ if (x != NULL) {
+ memcpy(b, x, outlen);
+ }
+ if (tail_len != 0) {
+ memcpy(b + outlen, tail, tail_len);
+ }
+ if (ctr_len != 0) {
+ b[outlen + tail_len] = ctr;
+ }
+ b[outlen + tail_len + ctr_len] ^= delim;
+ b[rate - 1] ^= 0x80;
+}
#endif
diff --git a/src/keccak-fast.c b/src/keccak-fast.c
@@ -26,6 +26,12 @@ int kf_batch_priority = 0;
int kf_batch_lanes = 1;
const char *kf_batch_label = "none";
+kf_chain_fn kf_chain_kernel = NULL;
+kf_chain_fn kf_chain_kernel12 = NULL;
+int kf_chain_priority = 0;
+int kf_chain_lanes = 1;
+const char *kf_chain_label = "none";
+
const char *kf_backend_name(void) {
return kf_backend_label;
}
@@ -34,6 +40,10 @@ const char *kf_batch_name(void) {
return kf_batch_label;
}
+const char *kf_chain_name(void) {
+ return kf_chain_label;
+}
+
static void kf_wipe(void *p, size_t n) {
static void *(*const volatile fn)(void *, int, size_t) = memset;
fn(p, 0, n);
@@ -137,3 +147,52 @@ KF_DEFSHA3_BATCH(384)
KF_DEFSHA3_BATCH(512)
KF_DEFTURBOSHAKE_BATCH(128)
KF_DEFTURBOSHAKE_BATCH(256)
+
+static int kf_chain_impl(kf_chain_fn kernel, size_t count, uint8_t *x,
+ size_t outlen, const uint8_t *tail, size_t tail_len,
+ size_t ctr_len, uint8_t ctr0, uint32_t iters,
+ size_t rate, uint8_t delim) {
+ if ((x == NULL) || ((tail == NULL) && (tail_len != 0))) return -1;
+ if ((outlen == 0) || (ctr_len > 1) || (rate >= 200)) return -1;
+ if (outlen + tail_len + ctr_len > rate - 1) return -1;
+ if ((kernel == NULL) || (kf_chain_lanes <= 0)) return -1;
+ if ((count == 0) || (iters == 0)) return 0;
+ kernel(count, x, outlen, tail, tail_len, ctr_len, ctr0, iters, rate, delim);
+ return 0;
+}
+
+#define KF_DEFSHAKE_CHAIN(bits) \
+ int kf_shake##bits##_chain(size_t count, uint8_t *x, size_t outlen, \
+ const uint8_t *tail, size_t tail_len, \
+ size_t ctr_len, uint8_t ctr0, uint32_t iters) { \
+ return kf_chain_impl(kf_chain_kernel, count, x, outlen, tail, tail_len, \
+ ctr_len, ctr0, iters, KF_RATE(bits), \
+ KF_DOMAIN_SHAKE); \
+ }
+#define KF_DEFSHA3_CHAIN(bits) \
+ int kf_sha3_##bits##_chain(size_t count, uint8_t *x, size_t outlen, \
+ const uint8_t *tail, size_t tail_len, \
+ size_t ctr_len, uint8_t ctr0, uint32_t iters) { \
+ if (outlen > ((bits) / 8)) return -1; \
+ return kf_chain_impl(kf_chain_kernel, count, x, outlen, tail, tail_len, \
+ ctr_len, ctr0, iters, KF_RATE(bits), \
+ KF_DOMAIN_SHA3); \
+ }
+#define KF_DEFTURBOSHAKE_CHAIN(bits) \
+ int kf_turboshake##bits##_chain(size_t count, uint8_t *x, size_t outlen, \
+ const uint8_t *tail, size_t tail_len, \
+ size_t ctr_len, uint8_t ctr0, \
+ uint32_t iters) { \
+ return kf_chain_impl(kf_chain_kernel12, count, x, outlen, tail, tail_len, \
+ ctr_len, ctr0, iters, KF_RATE(bits), \
+ KF_DOMAIN_SHAKE); \
+ }
+
+KF_DEFSHAKE_CHAIN(128)
+KF_DEFSHAKE_CHAIN(256)
+KF_DEFSHA3_CHAIN(224)
+KF_DEFSHA3_CHAIN(256)
+KF_DEFSHA3_CHAIN(384)
+KF_DEFSHA3_CHAIN(512)
+KF_DEFTURBOSHAKE_CHAIN(128)
+KF_DEFTURBOSHAKE_CHAIN(256)
diff --git a/src/keccak-fast.h b/src/keccak-fast.h
@@ -33,7 +33,34 @@ int kf_turboshake128_batch(size_t count, const uint8_t *in, size_t inlen,
int kf_turboshake256_batch(size_t count, const uint8_t *in, size_t inlen,
uint8_t *out, size_t outlen);
+/* x_r = H(x_r || tail_r || ctr), ctr one byte from ctr0, +1 per iteration */
+int kf_shake128_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len, size_t ctr_len,
+ uint8_t ctr0, uint32_t iters);
+int kf_shake256_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len, size_t ctr_len,
+ uint8_t ctr0, uint32_t iters);
+int kf_sha3_224_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len, size_t ctr_len,
+ uint8_t ctr0, uint32_t iters);
+int kf_sha3_256_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len, size_t ctr_len,
+ uint8_t ctr0, uint32_t iters);
+int kf_sha3_384_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len, size_t ctr_len,
+ uint8_t ctr0, uint32_t iters);
+int kf_sha3_512_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len, size_t ctr_len,
+ uint8_t ctr0, uint32_t iters);
+int kf_turboshake128_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len,
+ size_t ctr_len, uint8_t ctr0, uint32_t iters);
+int kf_turboshake256_chain(size_t count, uint8_t *x, size_t outlen,
+ const uint8_t *tail, size_t tail_len,
+ size_t ctr_len, uint8_t ctr0, uint32_t iters);
+
const char *kf_backend_name(void);
const char *kf_batch_name(void);
+const char *kf_chain_name(void);
#endif