README.md (7015B)
1 # keccak-fast 2 3 Drop-in-speed replacement for `coruus/keccak-tiny`. SHA-3/SHAKE one-shots, 4 streaming absorb and squeeze, TurboSHAKE128/256, batch entry points and fused 5 iterated chains. Plain C11, libc only, no build flags, runtime-dispatched, 6 thread-safe. 7 8 ## Use 9 10 ```c 11 #include <finwo/keccak-fast.h> 12 13 kf_shake256(out, sizeof out, in, inlen); /* 0, or -1 */ 14 kf_shake256_batch(count, in, inlen, out, outlen); /* equal-length, packed */ 15 kf_shake256_chain(count, x, 32, tail, 8, 1, 0, 256); 16 17 kf_shake256_init(&ctx); /* streaming, see below */ 18 kf_shake256_absorb(&ctx, buf, n); 19 kf_shake256_squeeze(&ctx, out, sizeof out); 20 ``` 21 22 `<algo>`: `shake128`, `shake256`, `sha3_224`, `sha3_256`, `sha3_384`, 23 `sha3_512`, `turboshake128`, `turboshake256`. 24 25 - `kf_<algo>`: one-shot, a `kf_hash_fn` pointer, so a call is one indirect jump 26 - `kf_<algo>_init`, `_absorb`, `_squeeze`: streaming, one message at a time 27 - `kf_<algo>_batch`: `count` equal-length messages, packed back to back 28 - `kf_<algo>_chain`: iterated, see below 29 30 TurboSHAKE is the same sponge over Keccak-p[1600,12] with domain `0x1F`, about 31 1.9x SHAKE, on a separately registered 12-round permutation. 32 33 ## Streaming 34 35 The sponge lives in the caller's `kf_ctx`, 224 bytes, so the read buffer is the 36 only memory a caller spends. 37 38 ```c 39 kf_ctx ctx; 40 uint8_t buf[65536]; 41 size_t n; 42 43 kf_sha3_256_init(&ctx); 44 while ((n = fread(buf, 1, sizeof buf, f)) != 0) { 45 kf_sha3_256_absorb(&ctx, buf, n); 46 } 47 kf_sha3_256_squeeze(&ctx, md, sizeof md); 48 ``` 49 50 - `kf_<algo>_init`: binds the permutation and zeroes the sponge, and resets a 51 context that was already used 52 - `kf_<algo>_absorb`: more input, as many calls as the caller likes. A partial 53 block waits in the sponge, there is no staging buffer 54 - `kf_<algo>_squeeze`: pads and steps the sponge on its first call, then emits. 55 Call it again on SHAKE or TurboSHAKE for more XOF output. A SHA3 context 56 stops at `bits / 8` 57 58 Padding happens once, on the first squeeze, so the digest does not depend on how 59 the input was split. `-1` covers a null ctx, a null pointer with a length, 60 absorb after squeezing, and a SHA3 squeeze past its cap. 61 62 ## Chains 63 64 `kf_<algo>_chain` iterates a hash into itself, `count` chains at a time: 65 66 ``` 67 x_r = H(x_r || tail_r || ctr) repeated `iters` times 68 ``` 69 70 - `tail_r`: `tail_len` caller-defined bytes, constant across iterations, packed 71 back to back like the batch inputs 72 - `ctr`: `ctr_len` bytes (0 or 1) from `ctr0`, incremented each iteration 73 - returns `-1` unless `outlen + tail_len + ctr_len <= rate - 1`, i.e. one 74 permutation per iteration 75 76 Covers plain iterated hashing, counter-only chains, tweaked chains, and both at 77 once. No wider counter: forces an endianness choice into every kernel, and 78 composes anyway by calling again with an updated tail. 79 80 Tightest fit is SHA3-512: rate 72, 7 bytes beside the digest. 81 82 Vector backends hold state in registers across iterations, `_batch` loop would 83 pay transpose + scalar absorb per step. 84 85 ## Replacing keccak-tiny 86 87 ```c 88 #include <finwo/keccak-fast-compat.h> /* was <coruus/keccak-tiny.h> */ 89 ``` 90 91 Maps the six `keccak-tiny` names onto their `kf_` equivalents, claims 92 `KECCAK_FIPS202_H` so either include order works. Signatures are identical. 93 94 Not carried over: `&sha3_256` and friends, the `kf_` names are dispatch 95 pointers, not functions. 96 97 ## Build 98 99 ``` 100 dep add finwo/keccak-fast 101 ``` 102 103 `export.mk` lists the sources, `.dep.export` installs `<finwo/keccak-fast.h>` 104 and `<finwo/keccak-fast-compat.h>`. 105 106 ## Backends 107 108 Lanes and priority per track: 109 110 | backend | one-shot | batch | chain | stream | 111 | ---------- | -------- | ------- | ------------- | ------ | 112 | scalar | 100 | x1, 100 | x1, 100 | 100 | 113 | scalar+bmi | 115 | x1, 114 | x1, 114 | 115 | 114 | arm-sha3 | 101 | x2, 182 | x2, 194 | 101 | 115 | AVX2 | - | x4, 218 | x4 fused, 265 | - | 116 | AVX-512 | - | x8, 447 | x8 fused, 786 | - | 117 118 Backends register from constructors, highest available priority wins. Priority 119 is measured, not ranked: scalar is 100, the rest are throughput relative to it 120 on the same track (265 = 2.65x scalar). One-shot scored on absorb MiB/s, batch 121 and chain on hashes per second, from `make bench` at SHA3-256. An x1 batch has 122 no vector kernel and falls through to the one-shot loop. The stream track steps 123 one sponge at a time, so it runs the single-state permutation and inherits the 124 one-shot scores. The lane-parallel backends have no single-lane kernel and 125 register nothing, for the same reason they have no one-shot: 4 or 8 lanes with 126 one live message pay the transpose for nothing. 127 128 Numbers vary by core, so they encode ordering, not a promise. `arm-sha3` is the 129 one to watch: scalar parity for one-shot on a Cortex-A72, ~1.4x on Apple 130 silicon. Re-run `make bench` on a new target before trusting that ordering. 131 132 `kf_backend_name()`, `kf_batch_name()`, `kf_chain_name()` and `kf_stream_name()` 133 report the active backends. `KECCAK_FAST_NO_BMI`, `KECCAK_FAST_NO_AVX2`, 134 `KECCAK_FAST_NO_AVX512` and `KECCAK_FAST_NO_ARM_SHA3` drop those backends. 135 136 - `scalar`: pinned to cpu baseline (no extensions: 137 `no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2`), so a consumer's `-march=native` 138 cannot vectorize it or emit BMI. 25 lanes in named locals, theta fused into 139 the previous round's chi, round loop unrolled 4x (beats no unroll and 24x). 140 - `scalar+bmi`: same permutation with `bmi`, turning chi's `not`+`and` into 141 `andn`, gcc also gets `rorx` for the rotates. ~20% over the donor. 142 - `arm-sha3`: ARMv8.2 `eor3`, `bcax`, `rax1`, `xar` under a scoped 143 `target("sha3")` region, no build flags. Self-contained: own permutation and 144 sponge, 25 lanes in named locals so they stay in NEON registers. Runtime 145 availability only (`hw.optional.arm.FEAT_SHA3` on Darwin, `HWCAP_SHA3` on 146 Linux). x2 kernels pack two messages per 128-bit register, ~2x scalar. 147 - `AVX2`, `AVX-512`: 4 or 8 messages one per lane, 25 state words in 25 vector 148 registers, transposed in 4x4 or 8x8 blocks at the sponge boundary. Theta and 149 chi unrolled by hand, so `-O2` emits what `-O3` does. 150 151 Chain kernels transpose on entry and exit only, rebuilding the message in the 152 transposed domain between permutations. A digest length not a multiple of 8 153 leaves one straddling word, masked rather than copied. `scalar`, `scalar+bmi` 154 and `arm-sha3` rebuild the block in memory instead: batching, no register 155 residency. 156 157 ## Dev 158 159 ``` 160 make test # KAT, batch, chain, stream and compat differentials (TAP) 161 make bench # per-algo table: every backend, every mode, plus the donor 162 ``` 163 164 `bench/` fetches the donor via `dep install`. `OPT=-O2|-O3` (arch flags 165 rejected), `BENCH_CPU=<n>` selects the core. 166 167 ## License 168 169 See `LICENSE.md`. The sponge is derived from `coruus/keccak-tiny` (David Leon 170 Gil, CC0); the scalar permutation follows XKCP's public-domain `opt64` 171 structure. The `arm-sha3` permutation is an independent implementation over 172 the ARMv8.2 SHA3 instructions. 173