keccak-fast.c

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

commit 5e9612ee1bc0a7497edd0da6f7629976cf374b3c
parent c672ce3e90bab90b496ae6dceafca6cd00a0537b
Author: finwo <finwo@pm.me>
Date:   Tue, 13 Oct 2026 16:36:38 +0200

Add keccak-tiny compatibility header

Diffstat:
M.dep.export | 1+
MMakefile | 3++-
MREADME.md | 170++++++++++++++++++++++++++++++++++++++-----------------------------------------
Asrc/keccak-fast-compat.h | 18++++++++++++++++++
Atest/compat.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 159 insertions(+), 89 deletions(-)

diff --git a/.dep.export b/.dep.export @@ -1 +1,2 @@ include/finwo/keccak-fast.h src/keccak-fast.h +include/finwo/keccak-fast-compat.h src/keccak-fast-compat.h diff --git a/Makefile b/Makefile @@ -5,7 +5,7 @@ CFLAGS += -std=c11 -I src LIB := src/keccak-fast.c $(wildcard src/backend/*.c) OUT := build -BINS := $(OUT)/kat $(OUT)/batch $(OUT)/chain +BINS := $(OUT)/kat $(OUT)/batch $(OUT)/chain $(OUT)/compat .PHONY: default test bench clean default: test @@ -14,6 +14,7 @@ test: $(BINS) @$(OUT)/kat @$(OUT)/batch @$(OUT)/chain + @$(OUT)/compat bench: @$(MAKE) --no-print-directory --directory bench run diff --git a/README.md b/README.md @@ -14,13 +14,15 @@ 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`, -`kf_sha3_384`, `kf_sha3_512`, `kf_turboshake128`, `kf_turboshake256`) are -`kf_hash_fn` table entries: a call is one indirect jump. Batch variants are -`kf_<algo>_batch`, taking `count` equal-length messages packed back to back. +`<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>_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 faster than SHAKE; its batch entries use a separately registered 12-round -permutation. +1.9x SHAKE, on a separately registered 12-round permutation. ## Chains @@ -30,24 +32,32 @@ permutation. 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. +- `tail_r`: `tail_len` caller-defined bytes, constant across iterations, packed + back to back like the batch inputs +- `ctr`: `ctr_len` bytes (0 or 1) from `ctr0`, incremented each iteration +- returns `-1` unless `outlen + tail_len + ctr_len <= rate - 1`, i.e. one + permutation per iteration + +Covers plain iterated hashing, counter-only chains, tweaked chains, and both at +once. No wider counter: forces an endianness choice into every kernel, and +composes anyway by calling again with an updated tail. + +Tightest fit is SHA3-512: rate 72, 7 bytes beside the digest. + +Vector backends hold state in registers across iterations, `_batch` loop would +pay transpose + scalar absorb per step. + +## Replacing keccak-tiny + +```c +#include <finwo/keccak-fast-compat.h> /* was <coruus/keccak-tiny.h> */ +``` + +Maps the six `keccak-tiny` names onto their `kf_` equivalents, claims +`KECCAK_FIPS202_H` so either include order works. Signatures are identical. + +Not carried over: `&sha3_256` and friends, the `kf_` names are dispatch +pointers, not functions. ## Build @@ -55,77 +65,61 @@ dispatched separately from the batch one. dep add finwo/keccak-fast ``` -`export.mk` lists the sources; `.dep.export` installs `<finwo/keccak-fast.h>`. +`export.mk` lists the sources, `.dep.export` installs `<finwo/keccak-fast.h>` +and `<finwo/keccak-fast-compat.h>`. ## Backends -| 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. - -The scalar backend is pinned to the x86-64 baseline -(`no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2`) so a consumer's `-march=native` -cannot vectorize it or emit BMI. Its permutation keeps the 25 lanes in named -locals, fuses theta into the previous round's chi, and unrolls the round loop -4x; that beats both no unroll and a full 24x unroll. `scalar+bmi` is the same -permutation with `bmi` enabled, which turns chi's `not`+`and` into `andn`; gcc -also gets `rorx` for the rotates. About 20% faster than the donor. - -`arm-sha3` uses the ARMv8.2 SHA3 instructions (`eor3`, `bcax`, `rax1`, `xar`) -under a scoped `target("sha3")` region, so no build flags are needed. It is -self-contained: its own permutation and sponge, nothing shared with the scalar -backend, and its 25 lanes live in named locals so the register allocator keeps -them in NEON registers. Availability is checked at runtime only -(`hw.optional.arm.FEAT_SHA3` on Darwin, `HWCAP_SHA3` on Linux); the x2 batch -kernel packs two messages one per 64-bit half of each 128-bit register. About -1.4x scalar for one-shots on Apple silicon and ~2x for the x2 batch; on some -cores the one-shot lands at parity and the pair kernel carries the win. - -The AVX2 and AVX-512 batch kernels hold the 4 or 8 messages of a group one per -vector lane, so the 25 state words live in 25 vector registers. The state is -transposed in 8x8 (or 4x4) blocks with unpack and shuffle instructions at the -sponge boundary. Their round bodies unroll theta and chi by hand so `-O2` emits -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. +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 | + +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. + +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. + +- `scalar`: pinned to cpu baseline (no extensions: + `no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2`), so a consumer's `-march=native` + cannot vectorize it or emit BMI. 25 lanes in named locals, theta fused into + the previous round's chi, round loop unrolled 4x (beats no unroll and 24x). +- `scalar+bmi`: same permutation with `bmi`, turning chi's `not`+`and` into + `andn`, gcc also gets `rorx` for the rotates. ~20% over the donor. +- `arm-sha3`: ARMv8.2 `eor3`, `bcax`, `rax1`, `xar` under a scoped + `target("sha3")` region, no build flags. Self-contained: own permutation and + sponge, 25 lanes in named locals so they stay in NEON registers. Runtime + availability only (`hw.optional.arm.FEAT_SHA3` on Darwin, `HWCAP_SHA3` on + Linux). x2 kernels pack two messages per 128-bit register, ~2x scalar. +- `AVX2`, `AVX-512`: 4 or 8 messages one per lane, 25 state words in 25 vector + registers, transposed in 4x4 or 8x8 blocks at the sponge boundary. Theta and + chi unrolled by hand, so `-O2` emits what `-O3` does. + +Chain kernels transpose on entry and exit only, rebuilding the message in the +transposed domain between permutations. A digest length not a multiple of 8 +leaves one straddling word, masked rather than copied. `scalar`, `scalar+bmi` +and `arm-sha3` rebuild the block in memory instead: batching, no register +residency. ## Dev ``` -make test # KAT, batch and chain differentials (TAP) -make bench # per-hash table: every backend, plus the donor +make test # KAT, batch, chain and compat differentials (TAP) +make bench # per-algo table: every backend, every mode, plus the donor ``` `bench/` fetches the donor via `dep install`. `OPT=-O2|-O3` (arch flags diff --git a/src/keccak-fast-compat.h b/src/keccak-fast-compat.h @@ -0,0 +1,18 @@ +#ifndef FINWO_KECCAK_FAST_COMPAT_H +#define FINWO_KECCAK_FAST_COMPAT_H + +/* Claims keccak-tiny's include guard, so either order of inclusion works */ +#ifndef KECCAK_FIPS202_H +#define KECCAK_FIPS202_H +#endif + +#include "keccak-fast.h" + +#define shake128 kf_shake128 +#define shake256 kf_shake256 +#define sha3_224 kf_sha3_224 +#define sha3_256 kf_sha3_256 +#define sha3_384 kf_sha3_384 +#define sha3_512 kf_sha3_512 + +#endif diff --git a/test/compat.c b/test/compat.c @@ -0,0 +1,56 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "keccak-fast-compat.h" + +static unsigned test_no; +static unsigned fails; + +static void check(const char *what, int (*fn)(uint8_t *, size_t, + const uint8_t *, size_t), + const uint8_t *in, size_t inlen, const char *hex) { + uint8_t got[64]; + uint8_t want[64]; + size_t outlen = strlen(hex) / 2; + int ok; + + for (size_t i = 0; i < outlen; i++) { + unsigned v = 0; + sscanf(hex + i * 2, "%2x", &v); + want[i] = (uint8_t)v; + } + memset(got, 0, sizeof got); + ok = fn(got, outlen, in, inlen) == 0 && memcmp(got, want, outlen) == 0; + + test_no++; + printf("%s %u - %s\n", ok ? "ok" : "not ok", test_no, what); + if (!ok) fails++; +} + +int main(void) { + const uint8_t *abc = (const uint8_t *)"abc"; + + printf("TAP version 13\n"); + printf("# keccak-tiny compatibility\n"); + + check("shake128 abc", shake128, abc, 3, + "5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8"); + check("shake256 abc", shake256, abc, 3, + "483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739" + "d5a15bef186a5386c75744c0527e1faa9f8726e462a12a4feb06bd8801e751e4"); + check("sha3_224 abc", sha3_224, abc, 3, + "e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf"); + check("sha3_256 abc", sha3_256, abc, 3, + "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"); + check("sha3_384 abc", sha3_384, abc, 3, + "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b2" + "98d88cea927ac7f539f1edf228376d25"); + check("sha3_512 abc", sha3_512, abc, 3, + "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e" + "10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"); + + printf("1..%u\n", test_no); + if (fails) printf("# %u of %u assertions failed\n", fails, test_no); + return fails != 0; +}