keccak-fast.c

Minimal SIMD keccak implementation
git clone git://git.finwo.net/lib/keccak-fast.c
Log | Files | Refs | README | LICENSE

commit d4bd67ae9f9f8f3dc63b0d979581270a59576142
parent dcf793165ac78044388b6301e22f9cd6067ae500
Author: finwo <finwo@pm.me>
Date:   Thu, 12 Nov 2026 23:58:24 +0100

Add streaming init, absorb and squeeze over a single-state stream track

Diffstat:
MMakefile | 8++++++--
MREADME.md | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Mbench/bench.c | 124++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/backend/arm_sha3.c | 18++++++++++++++++--
Msrc/backend/scalar.c | 14++++++++++++++
Msrc/backend/scalar_bmi.c | 14++++++++++++++
Msrc/keccak-fast-internal.h | 5+++++
Msrc/keccak-fast.c | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/keccak-fast.h | 43+++++++++++++++++++++++++++++++++++++++++++
Mtest/batch.c | 4++--
Mtest/kat.c | 4++--
Atest/stream.c | 288+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 files changed, 662 insertions(+), 48 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,17 +5,20 @@ CFLAGS += -std=c11 -I src LIB := src/keccak-fast.c $(wildcard src/backend/*.c) OUT := build -BINS := $(OUT)/kat $(OUT)/batch $(OUT)/chain $(OUT)/compat +BINS := $(OUT)/kat $(OUT)/batch $(OUT)/chain $(OUT)/compat $(OUT)/stream -.PHONY: default test bench clean +.PHONY: default default: test +.PHONY: test test: $(BINS) @$(OUT)/kat @$(OUT)/batch @$(OUT)/chain @$(OUT)/compat + @$(OUT)/stream +.PHONY: bench bench: @$(MAKE) --no-print-directory --directory bench run @@ -25,6 +28,7 @@ $(OUT)/%: test/%.c $(LIB) | $(OUT) $(OUT): @mkdir -p $(OUT) +.PHONY: clean clean: rm -rf $(OUT) @$(MAKE) --no-print-directory --directory bench clean diff --git a/README.md b/README.md @@ -1,8 +1,9 @@ # keccak-fast Drop-in-speed replacement for `coruus/keccak-tiny`. SHA-3/SHAKE one-shots, -TurboSHAKE128/256, batch entry points and fused iterated chains. Plain C11, -libc only, no build flags, runtime-dispatched, thread-safe. +streaming absorb and squeeze, TurboSHAKE128/256, batch entry points and fused +iterated chains. Plain C11, libc only, no build flags, runtime-dispatched, +thread-safe. ## Use @@ -12,18 +13,52 @@ libc only, no build 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); + +kf_shake256_init(&ctx); /* streaming, see below */ +kf_shake256_absorb(&ctx, buf, n); +kf_shake256_squeeze(&ctx, out, sizeof out); ``` `<algo>`: `shake128`, `shake256`, `sha3_224`, `sha3_256`, `sha3_384`, `sha3_512`, `turboshake128`, `turboshake256`. - `kf_<algo>`: one-shot, a `kf_hash_fn` pointer, so a call is one indirect jump +- `kf_<algo>_init`, `_absorb`, `_squeeze`: streaming, one message at a time - `kf_<algo>_batch`: `count` equal-length messages, packed back to back - `kf_<algo>_chain`: iterated, see below TurboSHAKE is the same sponge over Keccak-p[1600,12] with domain `0x1F`, about 1.9x SHAKE, on a separately registered 12-round permutation. +## Streaming + +The sponge lives in the caller's `kf_ctx`, 224 bytes, so the read buffer is the +only memory a caller spends. + +```c +kf_ctx ctx; +uint8_t buf[65536]; +size_t n; + +kf_sha3_256_init(&ctx); +while ((n = fread(buf, 1, sizeof buf, f)) != 0) { + kf_sha3_256_absorb(&ctx, buf, n); +} +kf_sha3_256_squeeze(&ctx, md, sizeof md); +``` + +- `kf_<algo>_init`: binds the permutation and zeroes the sponge, and resets a + context that was already used +- `kf_<algo>_absorb`: more input, as many calls as the caller likes. A partial + block waits in the sponge, there is no staging buffer +- `kf_<algo>_squeeze`: pads and steps the sponge on its first call, then emits. + Call it again on SHAKE or TurboSHAKE for more XOF output. A SHA3 context + stops at `bits / 8` + +Padding happens once, on the first squeeze, so the digest does not depend on how +the input was split. `-1` covers a null ctx, a null pointer with a length, +absorb after squeezing, and a SHA3 squeeze past its cap. + ## Chains `kf_<algo>_chain` iterates a hash into itself, `count` chains at a time: @@ -72,27 +107,31 @@ and `<finwo/keccak-fast-compat.h>`. Lanes and priority per track: -| backend | one-shot | batch | chain | -| ---------- | -------- | ------- | ------------- | -| scalar | 100 | x1, 100 | x1, 100 | -| scalar+bmi | 115 | x1, 114 | x1, 114 | -| arm-sha3 | 101 | x2, 182 | x2, 194 | -| AVX2 | - | x4, 218 | x4 fused, 265 | -| AVX-512 | - | x8, 447 | x8 fused, 786 | +| backend | one-shot | batch | chain | stream | +| ---------- | -------- | ------- | ------------- | ------ | +| scalar | 100 | x1, 100 | x1, 100 | 100 | +| scalar+bmi | 115 | x1, 114 | x1, 114 | 115 | +| arm-sha3 | 101 | x2, 182 | x2, 194 | 101 | +| AVX2 | - | x4, 218 | x4 fused, 265 | - | +| AVX-512 | - | x8, 447 | x8 fused, 786 | - | Backends register from constructors, highest available priority wins. Priority is measured, not ranked: scalar is 100, the rest are throughput relative to it on the same track (265 = 2.65x scalar). One-shot scored on absorb MiB/s, batch and chain on hashes per second, from `make bench` at SHA3-256. An x1 batch has -no vector kernel and falls through to the one-shot loop. +no vector kernel and falls through to the one-shot loop. The stream track steps +one sponge at a time, so it runs the single-state permutation and inherits the +one-shot scores. The lane-parallel backends have no single-lane kernel and +register nothing, for the same reason they have no one-shot: 4 or 8 lanes with +one live message pay the transpose for nothing. Numbers vary by core, so they encode ordering, not a promise. `arm-sha3` is the one to watch: scalar parity for one-shot on a Cortex-A72, ~1.4x on Apple silicon. Re-run `make bench` on a new target before trusting that ordering. -`kf_backend_name()`, `kf_batch_name()` and `kf_chain_name()` report the active -backends. `KECCAK_FAST_NO_BMI`, `KECCAK_FAST_NO_AVX2`, `KECCAK_FAST_NO_AVX512` -and `KECCAK_FAST_NO_SHA3` drop those backends. +`kf_backend_name()`, `kf_batch_name()`, `kf_chain_name()` and `kf_stream_name()` +report the active backends. `KECCAK_FAST_NO_BMI`, `KECCAK_FAST_NO_AVX2`, +`KECCAK_FAST_NO_AVX512` and `KECCAK_FAST_NO_ARM_SHA3` drop those backends. - `scalar`: pinned to cpu baseline (no extensions: `no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2`), so a consumer's `-march=native` @@ -118,7 +157,7 @@ residency. ## Dev ``` -make test # KAT, batch, chain and compat differentials (TAP) +make test # KAT, batch, chain, stream and compat differentials (TAP) make bench # per-algo table: every backend, every mode, plus the donor ``` diff --git a/bench/bench.c b/bench/bench.c @@ -28,10 +28,15 @@ int kf_scalar_turboshake256(uint8_t *, size_t, const uint8_t *, size_t); KF_CHAIN_PROTO(kf_scalar_chain); KF_CHAIN_PROTO(kf_scalar_chain12); +void kf_scalar_perm(uint64_t st[25]); +void kf_scalar_perm12(uint64_t st[25]); + #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ !defined(KECCAK_FAST_NO_BMI) KF_CHAIN_PROTO(kf_bmi_chain); KF_CHAIN_PROTO(kf_bmi_chain12); +void kf_bmi_perm(uint64_t st[25]); +void kf_bmi_perm12(uint64_t st[25]); #endif #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ @@ -52,7 +57,7 @@ void kf_avx512_chain(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, void kf_avx512_chain12(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, uint8_t, uint32_t, size_t, uint8_t); #endif -#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_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); @@ -60,6 +65,8 @@ void kf_armsha3_chain(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, uint8_t, uint32_t, size_t, uint8_t); void kf_armsha3_chain12(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, uint8_t, uint32_t, size_t, uint8_t); +void kf_armsha3_perm(uint64_t st[25]); +void kf_armsha3_perm12(uint64_t st[25]); #endif #ifndef BENCH_FLAGS @@ -69,11 +76,15 @@ void kf_armsha3_chain12(size_t, uint8_t *, size_t, const uint8_t *, size_t, #define BENCH_COUNT 64 #define BENCH_LINKS 256 #define BENCH_STREAM 16384 +#define BENCH_CHUNK 4096 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); typedef int (*kf_chain_api)(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, uint8_t, uint32_t); +typedef int (*kf_stream_init_fn)(kf_ctx *); +typedef int (*kf_stream_absorb_fn)(kf_ctx *, const uint8_t *, size_t); +typedef int (*kf_stream_squeeze_fn)(kf_ctx *, uint8_t *, size_t); static volatile uint64_t g_sink; @@ -128,6 +139,24 @@ static double time_chain(kf_chain_api fn, size_t count, uint8_t *x, ((double)iters * (double)count * (double)BENCH_LINKS); } +static double time_stream(kf_stream_init_fn init, kf_stream_absorb_fn absorb, + kf_stream_squeeze_fn squeeze, uint8_t *out, + size_t outlen, const uint8_t *in, size_t inlen, + size_t iters) { + uint64_t t0 = now_ns(); + for (size_t i = 0; i < iters; i++) { + kf_ctx ctx; + init(&ctx); + for (size_t off = 0; off < inlen; off += BENCH_CHUNK) { + size_t k = inlen - off < BENCH_CHUNK ? inlen - off : BENCH_CHUNK; + absorb(&ctx, in + off, k); + } + squeeze(&ctx, out, outlen); + g_sink += out[0]; + } + return (double)(now_ns() - t0) / (double)iters; +} + enum { ONE_NONE = 0, ONE_SCALAR, ONE_DISPATCH, ONE_DONOR }; typedef struct { @@ -138,6 +167,8 @@ typedef struct { kf_chain_fn chain; kf_chain_fn chain12; int lanes; + kf_perm_fn sperm; + kf_perm_fn sperm12; } row_t; typedef struct { @@ -148,6 +179,9 @@ typedef struct { kf_hash_fn *dispatched; kf_batch_fn batch_fn; kf_chain_api chain_fn; + kf_stream_init_fn sinit; + kf_stream_absorb_fn sabsorb; + kf_stream_squeeze_fn ssqueeze; size_t inlen; size_t outlen; size_t tail_len; @@ -191,18 +225,20 @@ static void report_algo(const algo_t *a, const row_t *rows, int nrows, kf_chain_fn sc = kf_chain_kernel; kf_chain_fn sc12 = kf_chain_kernel12; int scl = kf_chain_lanes; + kf_perm_fn ss = kf_stream_perm; + kf_perm_fn ss12 = kf_stream_perm12; printf("%s, rate %zu, %zu byte block\n", a->name, a->rate, a->inlen); - printf(" %-12s %10s %10s %10s %10s %10s\n", "backend", "one-shot", "batch", - "chain", "absorb", "squeeze"); - printf(" %-12s %10s %10s %10s %10s %10s\n", "", "Mh/s", "Mh/s", "Mh/s", - "MiB/s", "MiB/s"); + printf(" %-12s %10s %10s %10s %10s %10s %10s\n", "backend", "one-shot", + "batch", "chain", "absorb", "squeeze", "stream"); + printf(" %-12s %10s %10s %10s %10s %10s %10s\n", "", "Mh/s", "Mh/s", "Mh/s", + "MiB/s", "MiB/s", "MiB/s"); for (int i = 0; i < nrows; i++) { const row_t *r = &rows[i]; kf_fn one = NULL; double v_one = -1.0, v_bat = -1.0, v_chn = -1.0; - double v_abs = -1.0, v_sqz = -1.0; + double v_abs = -1.0, v_sqz = -1.0, v_str = -1.0; if (r->one == ONE_SCALAR) one = a->scalar_one; if (r->one == ONE_DISPATCH) one = *a->dispatched; @@ -257,12 +293,30 @@ static void report_algo(const algo_t *a, const row_t *rows, int nrows, v_chn = 1000.0 / best; } + if (r->sperm != NULL) { + kf_stream_perm = r->sperm; + kf_stream_perm12 = r->sperm12; + double p = time_stream(a->sinit, a->sabsorb, a->ssqueeze, out, a->outlen, + in, BENCH_STREAM, 20); + if (p <= 0.0) p = 1.0; + size_t it = (size_t)((double)target_ns / p); + if (it < 5) it = 5; + double best = -1.0; + for (int k = 0; k < reps; k++) { + double t = time_stream(a->sinit, a->sabsorb, a->ssqueeze, out, + a->outlen, in, BENCH_STREAM, it); + if (best < 0.0 || t < best) best = t; + } + v_str = mibs(best); + } + printf(" %-12s", r->label); cell(v_one); cell(v_bat); cell(v_chn); cell(v_abs); cell(v_sqz); + cell(v_str); printf("\n"); } @@ -272,6 +326,8 @@ static void report_algo(const algo_t *a, const row_t *rows, int nrows, kf_chain_kernel = sc; kf_chain_kernel12 = sc12; kf_chain_lanes = scl; + kf_stream_perm = ss; + kf_stream_perm12 = ss12; printf("\n"); } @@ -294,16 +350,20 @@ static void explain(void) { printf("number of permutations per hash and the spread across them is\n"); printf("per-call overhead alone. The chain tail is sized so its block\n"); printf("matches the one-shot input length.\n\n"); - printf("The last two columns move %d KiB on a single stream instead, and\n", + printf("The last three columns move %d KiB on a single stream instead.\n", BENCH_STREAM / 1024); - printf("are one-shot only. Batch and chain exist to step a whole group, so\n"); - printf("the question they answer is hashes per second, not bytes per second\n"); - printf("down one stream.\n\n"); + printf("absorb and squeeze are one-shot only, stream is the same bytes\n"); + printf("through the streaming interface. Batch and chain exist to step a\n"); + printf("whole group, so the question they answer is hashes per second, not\n"); + printf("bytes per second down one stream.\n\n"); printf(" absorb long input, short digest. What a checksum over a file\n"); printf(" sees, and the number that matters for plain SHA3.\n"); printf(" squeeze short input, long output. The sponge is seeded once and\n"); printf(" then stepped, which is what an XOF is for and the number\n"); - printf(" that matters for the SHAKE variants.\n\n"); + printf(" that matters for the SHAKE variants.\n"); + printf(" stream %d byte chunks through kf_<algo>_absorb, then squeezed.\n", + BENCH_CHUNK); + printf("\n"); printf("Both directions cost rate bytes per permutation, so rate is the\n"); printf("ceiling and the tables are ordered by it.\n"); printf("\n"); @@ -332,16 +392,18 @@ int main(void) { } printf("best of %d interleaved reps, about %llu ms per rep\n", reps, (unsigned long long)(target_ns / 1000000ULL)); - printf("dispatched: core %s, batch %s, chain %s\n\n", kf_backend_name(), - kf_batch_name(), kf_chain_name()); + printf("dispatched: core %s, batch %s, chain %s, stream %s\n\n", + kf_backend_name(), kf_batch_name(), kf_chain_name(), kf_stream_name()); explain(); row_t rows[6]; int nr = 0; - rows[nr++] = (row_t){"donor", ONE_DONOR, NULL, NULL, NULL, NULL, 1}; + rows[nr++] = + (row_t){"donor", ONE_DONOR, NULL, NULL, NULL, NULL, 1, NULL, NULL}; rows[nr++] = (row_t){"scalar", ONE_SCALAR, NULL, NULL, - kf_scalar_chain, kf_scalar_chain12, 1}; + kf_scalar_chain, kf_scalar_chain12, 1, + kf_scalar_perm, kf_scalar_perm12}; #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ !defined(KECCAK_FAST_NO_BMI) #if defined(__clang__) @@ -350,41 +412,51 @@ int main(void) { if (__builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2")) #endif rows[nr++] = (row_t){kf_backend_name(), ONE_DISPATCH, NULL, NULL, - kf_bmi_chain, kf_bmi_chain12, 1}; + kf_bmi_chain, kf_bmi_chain12, 1, + kf_bmi_perm, kf_bmi_perm12}; #endif #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ !defined(KECCAK_FAST_NO_AVX2) if (__builtin_cpu_supports("avx2")) rows[nr++] = (row_t){"avx2 x4", ONE_NONE, kf_avx2_batch, - kf_avx2_batch12, kf_avx2_chain, kf_avx2_chain12, 4}; + kf_avx2_batch12, kf_avx2_chain, kf_avx2_chain12, 4, + NULL, NULL}; #endif #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ !defined(KECCAK_FAST_NO_AVX512) if (__builtin_cpu_supports("avx512f")) rows[nr++] = (row_t){"avx512 x8", ONE_NONE, kf_avx512_batch, - kf_avx512_batch12, kf_avx512_chain, kf_avx512_chain12, 8}; + kf_avx512_batch12, kf_avx512_chain, kf_avx512_chain12, 8, + NULL, NULL}; #endif -#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) if (kf_armsha3_available()) rows[nr++] = (row_t){"arm-sha3 x2", ONE_DISPATCH, kf_armsha3_batch, - kf_armsha3_batch12, kf_armsha3_chain, kf_armsha3_chain12, 2}; + kf_armsha3_batch12, kf_armsha3_chain, kf_armsha3_chain12, 2, + kf_armsha3_perm, kf_armsha3_perm12}; #endif const algo_t algos[] = { {"SHAKE128", 168, NULL, kf_scalar_shake128, &kf_shake128, - kf_shake128_batch, kf_shake128_chain, 64, 32, 31, 1, 1}, + kf_shake128_batch, kf_shake128_chain, kf_shake128_init, + kf_shake128_absorb, kf_shake128_squeeze, 64, 32, 31, 1, 1}, {"TurboSHAKE128", 168, NULL, kf_scalar_turboshake128, &kf_turboshake128, - kf_turboshake128_batch, kf_turboshake128_chain, 64, 32, 31, 1, 1}, + kf_turboshake128_batch, kf_turboshake128_chain, kf_turboshake128_init, + kf_turboshake128_absorb, kf_turboshake128_squeeze, 64, 32, 31, 1, 1}, {"SHA3-256", 136, kct_sha3_256, kf_scalar_sha3_256, &kf_sha3_256, - kf_sha3_256_batch, kf_sha3_256_chain, 64, 32, 31, 1, 0}, + kf_sha3_256_batch, kf_sha3_256_chain, kf_sha3_256_init, + kf_sha3_256_absorb, kf_sha3_256_squeeze, 64, 32, 31, 1, 0}, {"SHAKE256", 136, kct_shake256, kf_scalar_shake256, &kf_shake256, - kf_shake256_batch, kf_shake256_chain, 64, 32, 31, 1, 1}, + kf_shake256_batch, kf_shake256_chain, kf_shake256_init, + kf_shake256_absorb, kf_shake256_squeeze, 64, 32, 31, 1, 1}, {"TurboSHAKE256", 136, NULL, kf_scalar_turboshake256, &kf_turboshake256, - kf_turboshake256_batch, kf_turboshake256_chain, 64, 32, 31, 1, 1}, + kf_turboshake256_batch, kf_turboshake256_chain, kf_turboshake256_init, + kf_turboshake256_absorb, kf_turboshake256_squeeze, 64, 32, 31, 1, 1}, {"SHA3-512", 72, NULL, kf_scalar_sha3_512, &kf_sha3_512, - kf_sha3_512_batch, kf_sha3_512_chain, 64, 64, 6, 1, 0}, + kf_sha3_512_batch, kf_sha3_512_chain, kf_sha3_512_init, + kf_sha3_512_absorb, kf_sha3_512_squeeze, 64, 64, 6, 1, 0}, }; for (size_t i = 0; i < sizeof algos / sizeof algos[0]; i++) { diff --git a/src/backend/arm_sha3.c b/src/backend/arm_sha3.c @@ -8,7 +8,7 @@ #include "../keccak-fast-internal.h" -#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) #if defined(__APPLE__) #include <sys/sysctl.h> @@ -131,6 +131,14 @@ defturboshake(256) #undef defsha3 #undef defturboshake +void kf_armsha3_perm(uint64_t st[25]) { + keccakf_armsha3(st); +} + +void kf_armsha3_perm12(uint64_t st[25]) { + keccak12_armsha3(st); +} + void kf_armsha3_batch(uint64_t states[][25], size_t count) { size_t i = 0; for (; i + 2 <= count; i += 2) { @@ -238,6 +246,12 @@ __attribute__((constructor)) static void kf_armsha3_register(void) { kf_chain_lanes = 2; kf_chain_label = "arm-sha3"; } + if (kf_stream_priority <= 101) { + kf_stream_perm = kf_armsha3_perm; + kf_stream_perm12 = kf_armsha3_perm12; + kf_stream_priority = 101; + kf_stream_label = "arm-sha3"; + } } -#endif /* aarch64 && !KECCAK_FAST_NO_SHA3 */ +#endif /* aarch64 && !KECCAK_FAST_NO_ARM_SHA3 */ diff --git a/src/backend/scalar.c b/src/backend/scalar.c @@ -316,6 +316,14 @@ defturboshake(256) #undef defsha3 #undef defturboshake +void kf_scalar_perm(uint64_t st[25]) { + keccakf(st); +} + +void kf_scalar_perm12(uint64_t st[25]) { + keccak12(st); +} + void kf_scalar_batch(uint64_t states[][25], size_t count) { for (size_t i = 0; i < count; i++) { keccakf(states[i]); @@ -377,6 +385,12 @@ __attribute__((constructor)) static void kf_scalar_register(void) { kf_chain_lanes = 1; kf_chain_label = "scalar"; } + if (kf_stream_priority <= 100) { + kf_stream_perm = kf_scalar_perm; + kf_stream_perm12 = kf_scalar_perm12; + kf_stream_priority = 100; + kf_stream_label = "scalar"; + } } #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) diff --git a/src/backend/scalar_bmi.c b/src/backend/scalar_bmi.c @@ -318,6 +318,14 @@ defturboshake(256) #undef defsha3 #undef defturboshake +void kf_bmi_perm(uint64_t st[25]) { + keccakf(st); +} + +void kf_bmi_perm12(uint64_t st[25]) { + keccak12(st); +} + void kf_bmi_batch(uint64_t states[][25], size_t count) { for (size_t i = 0; i < count; i++) { keccakf(states[i]); @@ -386,6 +394,12 @@ __attribute__((constructor)) static void kf_bmi_register(void) { kf_chain_lanes = 1; kf_chain_label = "scalar+bmi"; } + if (kf_stream_priority <= 115) { + kf_stream_perm = kf_bmi_perm; + kf_stream_perm12 = kf_bmi_perm12; + kf_stream_priority = 115; + kf_stream_label = "scalar+bmi"; + } } #if defined(__clang__) diff --git a/src/keccak-fast-internal.h b/src/keccak-fast-internal.h @@ -24,6 +24,11 @@ extern kf_chain_fn kf_chain_kernel12; extern int kf_chain_priority; extern int kf_chain_lanes; +extern kf_perm_fn kf_stream_perm; +extern kf_perm_fn kf_stream_perm12; +extern int kf_stream_priority; +extern const char *kf_stream_label; + extern int kf_backend_priority; extern const char *kf_backend_label; extern const char *kf_batch_label; diff --git a/src/keccak-fast.c b/src/keccak-fast.c @@ -32,6 +32,11 @@ int kf_chain_priority = 0; int kf_chain_lanes = 1; const char *kf_chain_label = "none"; +kf_perm_fn kf_stream_perm = NULL; +kf_perm_fn kf_stream_perm12 = NULL; +int kf_stream_priority = 0; +const char *kf_stream_label = "none"; + const char *kf_backend_name(void) { return kf_backend_label; } @@ -44,11 +49,127 @@ const char *kf_chain_name(void) { return kf_chain_label; } +const char *kf_stream_name(void) { + return kf_stream_label; +} + static void kf_wipe(void *p, size_t n) { static void *(*const volatile fn)(void *, int, size_t) = memset; fn(p, 0, n); } +#define KF_FL_PADDED 0x01 + +static int kf_stream_init(kf_ctx *ctx, size_t rate, uint8_t delim, size_t cap, + int twelve) { + kf_perm_fn perm = twelve ? kf_stream_perm12 : kf_stream_perm; + if ((ctx == NULL) || (perm == NULL) || (rate >= 200)) return -1; + memset(ctx, 0, sizeof *ctx); + ctx->perm = perm; + ctx->rate = (uint8_t)rate; + ctx->cap = (uint16_t)cap; + ctx->delim = delim; + return 0; +} + +static int kf_stream_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen) { + if ((ctx == NULL) || ((in == NULL) && (inlen != 0)) || (ctx->flags != 0)) { + return -1; + } + + uint8_t *b = (uint8_t *)ctx->st; + const size_t rate = ctx->rate; + size_t pt = ctx->pt; + size_t n = inlen; + + while (n >= rate - pt) { + size_t k = rate - pt; + for (size_t i = 0; i < k; i++) b[pt + i] ^= in[i]; + in += k; + n -= k; + pt = 0; + ctx->perm(ctx->st); + } + for (size_t i = 0; i < n; i++) b[pt + i] ^= in[i]; + ctx->pt = (uint8_t)(pt + n); + return 0; +} + +static int kf_stream_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen) { + if ((ctx == NULL) || (out == NULL)) return -1; + if ((ctx->cap != 0) && (outlen > (size_t)ctx->cap - ctx->emitted)) return -1; + + uint8_t *b = (uint8_t *)ctx->st; + const size_t rate = ctx->rate; + size_t pt = ctx->pt; + size_t n = outlen; + + if (!(ctx->flags & KF_FL_PADDED)) { + b[pt] ^= ctx->delim; + b[rate - 1] ^= 0x80; + ctx->perm(ctx->st); + ctx->flags |= KF_FL_PADDED; + pt = 0; + } + while (n != 0) { + size_t k = rate - pt; + if (k > n) k = n; + memcpy(out, b + pt, k); + out += k; + n -= k; + pt += k; + if (pt == rate) { + ctx->perm(ctx->st); + pt = 0; + } + } + ctx->pt = (uint8_t)pt; + if (ctx->cap != 0) ctx->emitted += (uint32_t)outlen; + return 0; +} + +#define KF_DEFSHAKE_STREAM(bits) \ + int kf_shake##bits##_init(kf_ctx *ctx) { \ + return kf_stream_init(ctx, KF_RATE(bits), KF_DOMAIN_SHAKE, 0, 0); \ + } \ + int kf_shake##bits##_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen) { \ + return kf_stream_absorb(ctx, in, inlen); \ + } \ + int kf_shake##bits##_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen) { \ + return kf_stream_squeeze(ctx, out, outlen); \ + } +#define KF_DEFSHA3_STREAM(bits) \ + int kf_sha3_##bits##_init(kf_ctx *ctx) { \ + return kf_stream_init(ctx, KF_RATE(bits), KF_DOMAIN_SHA3, (bits) / 8, 0); \ + } \ + int kf_sha3_##bits##_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen) { \ + return kf_stream_absorb(ctx, in, inlen); \ + } \ + int kf_sha3_##bits##_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen) { \ + return kf_stream_squeeze(ctx, out, outlen); \ + } +#define KF_DEFTURBOSHAKE_STREAM(bits) \ + int kf_turboshake##bits##_init(kf_ctx *ctx) { \ + return kf_stream_init(ctx, KF_RATE(bits), KF_DOMAIN_SHAKE, 0, 1); \ + } \ + int kf_turboshake##bits##_absorb(kf_ctx *ctx, const uint8_t *in, \ + size_t inlen) { \ + return kf_stream_absorb(ctx, in, inlen); \ + } \ + int kf_turboshake##bits##_squeeze(kf_ctx *ctx, uint8_t *out, \ + size_t outlen) { \ + return kf_stream_squeeze(ctx, out, outlen); \ + } + +KF_DEFSHAKE_STREAM(128) +KF_DEFSHAKE_STREAM(256) +KF_DEFSHA3_STREAM(224) +KF_DEFSHA3_STREAM(256) +KF_DEFSHA3_STREAM(384) +KF_DEFSHA3_STREAM(512) +KF_DEFTURBOSHAKE_STREAM(128) +KF_DEFTURBOSHAKE_STREAM(256) + 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, diff --git a/src/keccak-fast.h b/src/keccak-fast.h @@ -16,6 +16,48 @@ extern kf_hash_fn kf_sha3_512; extern kf_hash_fn kf_turboshake128; extern kf_hash_fn kf_turboshake256; +/* Streaming sponge: caller-allocated, fields are internal. */ +typedef void (*kf_perm_fn)(uint64_t st[25]); + +typedef struct kf_ctx { + kf_perm_fn perm; /* bound at init */ + uint64_t st[25]; /* partial block lives in st[0..rate-1] */ + uint32_t emitted; /* bytes squeezed, counted only when cap is not 0 */ + uint16_t cap; /* SHA3: bits/8. XOF: 0 */ + uint8_t rate; + uint8_t pt; /* cursor in the current block */ + uint8_t delim; + uint8_t flags; +} kf_ctx; + +/* squeeze pads on its first call, then emits; repeat it for XOF output. + * -1: null ctx, null pointer with a length, absorb after squeeze, a SHA3 + * squeeze past its cap. */ +int kf_shake128_init(kf_ctx *ctx); +int kf_shake128_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_shake128_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_shake256_init(kf_ctx *ctx); +int kf_shake256_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_shake256_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_sha3_224_init(kf_ctx *ctx); +int kf_sha3_224_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_sha3_224_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_sha3_256_init(kf_ctx *ctx); +int kf_sha3_256_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_sha3_256_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_sha3_384_init(kf_ctx *ctx); +int kf_sha3_384_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_sha3_384_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_sha3_512_init(kf_ctx *ctx); +int kf_sha3_512_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_sha3_512_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_turboshake128_init(kf_ctx *ctx); +int kf_turboshake128_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_turboshake128_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); +int kf_turboshake256_init(kf_ctx *ctx); +int kf_turboshake256_absorb(kf_ctx *ctx, const uint8_t *in, size_t inlen); +int kf_turboshake256_squeeze(kf_ctx *ctx, uint8_t *out, size_t outlen); + int kf_shake128_batch(size_t count, const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen); int kf_shake256_batch(size_t count, const uint8_t *in, size_t inlen, @@ -62,5 +104,6 @@ int kf_turboshake256_chain(size_t count, uint8_t *x, size_t outlen, const char *kf_backend_name(void); const char *kf_batch_name(void); const char *kf_chain_name(void); +const char *kf_stream_name(void); #endif diff --git a/test/batch.c b/test/batch.c @@ -21,7 +21,7 @@ void kf_avx2_batch12(uint64_t states[][25], size_t count); 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) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_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); @@ -86,7 +86,7 @@ int main(void) { kernels[nk++] = (kernel){"avx512 x8", kf_avx512_batch, kf_avx512_batch12, 8}; #endif -#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) if (kf_armsha3_available()) kernels[nk++] = (kernel){"arm-sha3 x2", kf_armsha3_batch, kf_armsha3_batch12, 2}; diff --git a/test/kat.c b/test/kat.c @@ -27,7 +27,7 @@ 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) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_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); @@ -178,7 +178,7 @@ int main(void) { kf_bmi_sha3_512, kf_bmi_turboshake128, kf_bmi_turboshake256}; #endif -#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_SHA3) +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) if (kf_armsha3_available()) bes[nb++] = (backend){"arm-sha3", kf_armsha3_shake128, kf_armsha3_shake256, kf_armsha3_sha3_224, diff --git a/test/stream.c b/test/stream.c @@ -0,0 +1,288 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "keccak-fast-internal.h" +#include "keccak-fast.h" + +void kf_scalar_perm(uint64_t st[25]); +void kf_scalar_perm12(uint64_t st[25]); + +#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ + !defined(KECCAK_FAST_NO_BMI) +void kf_bmi_perm(uint64_t st[25]); +void kf_bmi_perm12(uint64_t st[25]); +#endif + +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) +int kf_armsha3_available(void); +void kf_armsha3_perm(uint64_t st[25]); +void kf_armsha3_perm12(uint64_t st[25]); +#endif + +typedef int (*init_fn)(kf_ctx *); +typedef int (*absorb_fn)(kf_ctx *, const uint8_t *, size_t); +typedef int (*squeeze_fn)(kf_ctx *, uint8_t *, size_t); + +typedef struct { + const char *name; + kf_hash_fn one; + init_fn init; + absorb_fn absorb; + squeeze_fn squeeze; + size_t rate; + size_t cap; +} algo; + +typedef struct { + const char *label; + kf_perm_fn perm; + kf_perm_fn perm12; +} kernel; + +#define INLEN_MAX 40000 +#define OUT_MAX 512 + +static uint8_t in[INLEN_MAX + 8]; +static uint8_t want[OUT_MAX]; +static uint8_t got[OUT_MAX]; +static uint8_t ref[OUT_MAX]; + +static unsigned test_no; +static unsigned fails; + +static void check(const char *what, int ok) { + test_no++; + printf("%s %u - %s\n", ok ? "ok" : "not ok", test_no, what); + if (!ok) fails++; +} + +static void fill(uint8_t *p, size_t n, unsigned seed) { + for (size_t i = 0; i < n; i++) p[i] = (uint8_t)(i * 31 + seed * 7 + 1); +} + +/* absorb msg in chunks of `chunk`, then squeeze outlen in one call */ +static int feed(const algo *a, const uint8_t *msg, size_t inlen, size_t chunk, + uint8_t *out, size_t outlen) { + kf_ctx ctx; + if (a->init(&ctx) != 0) return -1; + for (size_t off = 0; off < inlen; off += chunk) { + size_t k = inlen - off < chunk ? inlen - off : chunk; + if (a->absorb(&ctx, msg + off, k) != 0) return -1; + } + if (a->absorb(&ctx, msg, 0) != 0) return -1; + return a->squeeze(&ctx, out, outlen); +} + +/* absorb inlen at once, then squeeze outlen in chunks of `chunk` */ +static int feed_sq(const algo *a, uint8_t *out, size_t inlen, size_t chunk, + size_t outlen) { + kf_ctx ctx; + if (a->init(&ctx) != 0) return -1; + if (a->absorb(&ctx, in, inlen) != 0) return -1; + for (size_t off = 0; off < outlen; off += chunk) { + size_t k = outlen - off < chunk ? outlen - off : chunk; + if (a->squeeze(&ctx, out + off, k) != 0) return -1; + } + return 0; +} + +int main(void) { + const algo algos[] = { + {"shake128", kf_shake128, kf_shake128_init, kf_shake128_absorb, + kf_shake128_squeeze, 168, 0}, + {"shake256", kf_shake256, kf_shake256_init, kf_shake256_absorb, + kf_shake256_squeeze, 136, 0}, + {"sha3_224", kf_sha3_224, kf_sha3_224_init, kf_sha3_224_absorb, + kf_sha3_224_squeeze, 144, 28}, + {"sha3_256", kf_sha3_256, kf_sha3_256_init, kf_sha3_256_absorb, + kf_sha3_256_squeeze, 136, 32}, + {"sha3_384", kf_sha3_384, kf_sha3_384_init, kf_sha3_384_absorb, + kf_sha3_384_squeeze, 104, 48}, + {"sha3_512", kf_sha3_512, kf_sha3_512_init, kf_sha3_512_absorb, + kf_sha3_512_squeeze, 72, 64}, + {"turboshake128", kf_turboshake128, kf_turboshake128_init, + kf_turboshake128_absorb, kf_turboshake128_squeeze, 168, 0}, + {"turboshake256", kf_turboshake256, kf_turboshake256_init, + kf_turboshake256_absorb, kf_turboshake256_squeeze, 136, 0}, + }; + const size_t nalgo = sizeof algos / sizeof algos[0]; + + kernel kernels[3]; + int nk = 0; + + kernels[nk++] = (kernel){"scalar", kf_scalar_perm, kf_scalar_perm12}; +#if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ + !defined(KECCAK_FAST_NO_BMI) +#if defined(__clang__) + if (__builtin_cpu_supports("bmi")) +#else + if (__builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2")) +#endif + kernels[nk++] = (kernel){"scalar+bmi", kf_bmi_perm, kf_bmi_perm12}; +#endif +#if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) + if (kf_armsha3_available()) + kernels[nk++] = (kernel){"arm-sha3", kf_armsha3_perm, kf_armsha3_perm12}; +#endif + + fill(in, sizeof in, 5); + + printf("TAP version 13\n"); + printf("# streaming sponge\n"); + + for (int k = 0; k < nk; k++) { + kf_stream_perm = kernels[k].perm; + kf_stream_perm12 = kernels[k].perm12; + + for (size_t a = 0; a < nalgo; a++) { + const algo *al = &algos[a]; + size_t rate = al->rate; + size_t olen = al->cap ? al->cap : 100; + size_t ils[8]; + size_t chs[7]; + char nm[160]; + + ils[0] = 0; + ils[1] = 1; + ils[2] = rate - 1; + ils[3] = rate; + ils[4] = rate + 1; + ils[5] = 2 * rate + 3; + ils[6] = 4096; + ils[7] = INLEN_MAX; + chs[0] = 1; + chs[1] = 3; + chs[2] = rate - 1; + chs[3] = rate; + chs[4] = rate + 1; + chs[5] = 4096; + chs[6] = 0; /* whole message in one absorb */ + + for (int i = 0; i < 8; i++) { + size_t inlen = ils[i]; + if (al->one(want, olen, in, inlen) != 0) { + snprintf(nm, sizeof nm, "%s %s one-shot %zu", kernels[k].label, + al->name, inlen); + check(nm, 0); + continue; + } + for (int c = 0; c < 7; c++) { + size_t chunk = chs[c] ? chs[c] : (inlen ? inlen : 1); + int rc = feed(al, in, inlen, chunk, got, olen); + snprintf(nm, sizeof nm, "%s %s in=%zu chunk=%zu", kernels[k].label, + al->name, inlen, chunk); + check(nm, rc == 0 && memcmp(want, got, olen) == 0); + } + } + + /* squeeze splits, including past the first output block */ + size_t sqs[4] = {olen, 1, 7, rate + 5}; + for (int s = 0; s < 4; s++) { + int rc = al->one(want, olen, in, 100); + rc |= feed_sq(al, got, 100, sqs[s], olen); + snprintf(nm, sizeof nm, "%s %s squeeze=%zu split=%zu", + kernels[k].label, al->name, olen, sqs[s]); + check(nm, rc == 0 && memcmp(want, got, olen) == 0); + } + + if (al->cap == 0) { + int rc1; + int rc2 = al->one(want, 400, in, 1000); + kf_ctx ctx; + + rc1 = feed(al, in, 1000, rate - 1, got, 400); + snprintf(nm, sizeof nm, "%s %s xof 400 of one block", + kernels[k].label, al->name); + check(nm, rc1 == 0 && rc2 == 0 && memcmp(want, got, 400) == 0); + + rc1 = al->init(&ctx); + rc1 |= al->absorb(&ctx, in, 1000); + for (size_t i = 0; i < 400; i++) { + if (al->squeeze(&ctx, got + i, 1) != 0) { + rc1 = -1; + break; + } + } + snprintf(nm, sizeof nm, "%s %s xof byte at a time", kernels[k].label, + al->name); + check(nm, rc1 == 0 && rc2 == 0 && memcmp(want, got, 400) == 0); + } + + /* a context copied mid-stream keeps going */ + { + kf_ctx c1, c2; + int rc = al->init(&c1); + rc |= al->absorb(&c1, in, 1234); + c2 = c1; + rc |= al->absorb(&c1, in + 1234, 2222); + rc |= al->squeeze(&c1, got, olen); + rc |= al->absorb(&c2, in + 1234, 2222); + rc |= al->squeeze(&c2, want, olen); + snprintf(nm, sizeof nm, "%s %s ctx copy mid-stream", + kernels[k].label, al->name); + check(nm, rc == 0 && memcmp(want, got, olen) == 0 && + al->one(ref, olen, in, 3456) == 0 && + memcmp(ref, got, olen) == 0); + } + } + } + + kf_stream_perm = kf_scalar_perm; + kf_stream_perm12 = kf_scalar_perm12; + + { + kf_ctx ctx; + uint8_t fresh[64]; + int rc; + + check("rejects null ctx on init", kf_sha3_256_init(NULL) == -1); + check("init binds a permutation", kf_shake256_init(&ctx) == 0); + + rc = kf_shake256_init(&ctx); + rc |= kf_shake256_absorb(&ctx, NULL, 4); + check("rejects null input with a length", rc == -1); + + rc = kf_shake256_init(&ctx); + rc |= kf_shake256_absorb(&ctx, in, 64); + rc |= kf_shake256_squeeze(&ctx, NULL, 32); + check("rejects null output", rc == -1); + + rc = kf_shake256_init(&ctx); + rc |= kf_shake256_absorb(&ctx, in, 64); + rc |= kf_shake256_squeeze(&ctx, got, 32); + rc |= kf_shake256_absorb(&ctx, in, 8); + check("rejects absorb after squeeze", rc == -1); + + check("sha3 rejects outlen past the cap", + kf_sha3_224_init(&ctx) == 0 && kf_sha3_224_absorb(&ctx, in, 9) == 0 && + kf_sha3_224_squeeze(&ctx, got, 29) == -1); + + rc = kf_sha3_224_init(&ctx); + rc |= kf_sha3_224_absorb(&ctx, in, 9); + rc |= kf_sha3_224_squeeze(&ctx, got, 27); + rc |= kf_sha3_224_squeeze(&ctx, got + 27, 1); + check("sha3 accepts an exact split cap", rc == 0); + + check("sha3 rejects one byte past a reached cap", + kf_sha3_224_squeeze(&ctx, got, 1) == -1); + + rc = kf_shake256_init(&ctx); + rc |= kf_shake256_absorb(&ctx, in, 40); + rc |= kf_shake256_squeeze(&ctx, got, 32); + kf_shake256_init(&ctx); + kf_shake256_absorb(&ctx, in, 40); + kf_shake256_squeeze(&ctx, fresh, 32); + check("re-init resets a used ctx", + rc == 0 && memcmp(got, fresh, 32) == 0 && + kf_shake256(ref, 32, in, 40) == 0 && + memcmp(ref, fresh, 32) == 0); + + check("dispatched track has a name", kf_stream_name()[0] != '\0'); + } + + printf("1..%u\n", test_no); + if (fails) printf("# %u of %u assertions failed\n", fails, test_no); + printf("# stream backend: %s\n", kf_stream_name()); + return fails != 0; +}