sha3-kernel-hasher 0.4.1

Pure-Rust SHA3-512 (FIPS 202), full Keccak-f[1600] permutation, no_std+alloc dual-mode for kernel drivers. Passes NIST CAVP test vectors, cross-validated vs RustCrypto sha3. Real AVX-512x8 batch hashing (~5.5x, hash_many); single-hash SIMD is opt-in, not a speedup — see README 5.1.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! AVX-512 vectorized Keccak-f\[1600\] permutation.
//!
//! # Design
//!
//! The 5×5 lane state is held as five `__m512i` registers, one per row
//! `y` (row `y` occupies lanes 0..5 of its register; lanes 5..7 are
//! padding). This layout is chosen because θ's column-parity XOR and
//! χ's row-local shifts both become single vector ops in it; only π
//! (which mixes freely across all 25 positions — see the derivation
//! below) needs more than one source register per destination row.
//!
//! Every constant table here (`RHO_ROW`, `THETA_*_IDX`, `PI_MASKS`/
//! `PI_IDXS`, `CHI_SHIFT*_IDX`) is derived mechanically from this crate's
//! existing, independently-tested scalar tables (`RHO`, `PI` in
//! `keccak.rs`) rather than re-derived by hand, specifically to avoid a
//! transcription bug in security-critical constants. There's no
//! build-time codegen here; instead `tests::table_consistency` asserts
//! every table below against a value computed straight from
//! `super::keccak::{RHO, PI}` at test time, so a future edit to either
//! the scalar tables or this module's tables that goes out of sync fails
//! loudly.
//!
//! # Why π needs more than a single shuffle — and why it's not a gather
//!
//! For destination row `z`, the source indices `PI[5z..5z+5]` land in up
//! to **five different source rows** (e.g. destination row 0 draws from
//! `PI[0..5] = [0, 6, 12, 18, 24]`, i.e. rows 0, 1, 2, 3, 4 — one lane
//! from each). A 2-input shuffle (`permutex2var`) can't express a gather
//! from five distinct sources.
//!
//! An earlier version of this module handled that by storing the
//! rotated rows to a padded `[u64; 40]` scratch buffer and using
//! `_mm512_i64gather_epi64` to pull each destination row's five
//! cross-row lanes back in — bit-correct, but measured **~25-30% slower
//! end-to-end throughput than the scalar implementation**
//! (`cargo bench --bench sha3_bench`: ~135 MiB/s vector vs. ~180 MiB/s
//! scalar on a 1 MiB payload on this container's Xeon). Gather is a
//! high-latency instruction on real hardware, and paying it five times a
//! round outweighed whatever the wider registers saved.
//!
//! This version instead computes each destination row as, for every
//! source row `y` in turn, a `permutexvar` that reorders `row[y]` into
//! destination lane positions, combined into an accumulator with a
//! `mask_blend` that keeps only the lanes actually sourced from row `y`
//! (`PI_MASKS`/`PI_IDXS`, both statically known from `PI`). That's 25
//! permutes + 25 blends per round instead of 5 stores + 5 gathers — more
//! total instructions, but every one is a single-cycle-throughput
//! shuffle/blend instead of a multi-cycle-latency gather.
//!
//! # Safety
//!
//! [`keccak_f1600_avx512`] is `unsafe` and must only be called after
//! `is_x86_feature_detected!("avx512f")` has returned `true` — it is
//! marked `#[target_feature(enable = "avx512f")]`, so calling it on a
//! CPU without AVX-512F is undefined behavior (most likely `SIGILL`).
//! [`keccak_f1600_dispatch`] is the safe entry point that performs this
//! check; nothing outside this module should call the unsafe function
//! directly.

use crate::keccak::{PI, RHO};
use std::arch::x86_64::*;

/// Rotation-amount vectors for the ρ step, one per row `y`, in the
/// crate's natural lane order (`ROT[y][x] == RHO[5*y + x]`). Lanes 5..8
/// are unused padding (rotate-by-0 is harmless since nothing reads
/// those lanes downstream).
const ROT_ROW: [[u64; 8]; 5] = [
    [
        RHO[0] as u64,
        RHO[1] as u64,
        RHO[2] as u64,
        RHO[3] as u64,
        RHO[4] as u64,
        0,
        0,
        0,
    ],
    [
        RHO[5] as u64,
        RHO[6] as u64,
        RHO[7] as u64,
        RHO[8] as u64,
        RHO[9] as u64,
        0,
        0,
        0,
    ],
    [
        RHO[10] as u64,
        RHO[11] as u64,
        RHO[12] as u64,
        RHO[13] as u64,
        RHO[14] as u64,
        0,
        0,
        0,
    ],
    [
        RHO[15] as u64,
        RHO[16] as u64,
        RHO[17] as u64,
        RHO[18] as u64,
        RHO[19] as u64,
        0,
        0,
        0,
    ],
    [
        RHO[20] as u64,
        RHO[21] as u64,
        RHO[22] as u64,
        RHO[23] as u64,
        RHO[24] as u64,
        0,
        0,
        0,
    ],
];

/// θ step permutation index: lane `x` selects `C[(x + 4) % 5]` (i.e.
/// `C[x-1]`). Padding lanes point at lane 0 (value discarded).
const THETA_MINUS1_IDX: [u64; 8] = [4, 0, 1, 2, 3, 0, 0, 0];
/// θ step permutation index: lane `x` selects `C[(x + 1) % 5]`.
const THETA_PLUS1_IDX: [u64; 8] = [1, 2, 3, 4, 0, 0, 0, 0];

/// χ step permutation index: lane `x` selects row-lane `(x + 1) % 5`.
const CHI_SHIFT1_IDX: [u64; 8] = [1, 2, 3, 4, 0, 0, 0, 0];
/// χ step permutation index: lane `x` selects row-lane `(x + 2) % 5`.
const CHI_SHIFT2_IDX: [u64; 8] = [2, 3, 4, 0, 1, 0, 0, 0];

/// Ternary-logic immediate computing `a ^ (!b & c)` — Keccak's χ
/// combining function. Verified against a brute-force truth table in
/// `tests::ternarylogic_truth_table_is_chi`.
const CHI_TERNARY_IMM: i32 = 0xD2;

/// π's per-(destination row, source row) selection tables — see the
/// module-level "Why π needs more than a single shuffle" section above
/// for the full rationale and measured before/after numbers.
///
/// Mask bit `x` (0..5) of `PI_MASKS[z][y]` is set iff destination lane
/// `x` of row `z` sources from row `y` (i.e. `PI[5*z+x] / 5 == y`).
/// `PI_IDXS[z][y][x]` holds that source lane (`PI[5*z+x] % 5`) when the
/// mask bit is set, `0` (don't-care — masked out) otherwise. Both tables
/// are checked against `PI` directly in `tests::table_consistency`.
const fn source_row(j: usize) -> usize {
    PI[j] / 5
}
const fn source_lane(j: usize) -> usize {
    PI[j] % 5
}

const fn pi_mask(z: usize, y: usize) -> u8 {
    let mut mask = 0u8;
    let mut x = 0;
    while x < 5 {
        if source_row(5 * z + x) == y {
            mask |= 1 << x;
        }
        x += 1;
    }
    mask
}

const fn pi_idx(z: usize, y: usize) -> [u64; 8] {
    let mut idx = [0u64; 8];
    let mut x = 0;
    while x < 5 {
        if source_row(5 * z + x) == y {
            idx[x] = source_lane(5 * z + x) as u64;
        }
        x += 1;
    }
    idx
}

const fn build_pi_masks() -> [[u8; 5]; 5] {
    let mut out = [[0u8; 5]; 5];
    let mut z = 0;
    while z < 5 {
        let mut y = 0;
        while y < 5 {
            out[z][y] = pi_mask(z, y);
            y += 1;
        }
        z += 1;
    }
    out
}

const fn build_pi_idxs() -> [[[u64; 8]; 5]; 5] {
    let mut out = [[[0u64; 8]; 5]; 5];
    let mut z = 0;
    while z < 5 {
        let mut y = 0;
        while y < 5 {
            out[z][y] = pi_idx(z, y);
            y += 1;
        }
        z += 1;
    }
    out
}

const PI_MASKS: [[u8; 5]; 5] = build_pi_masks();
const PI_IDXS: [[[u64; 8]; 5]; 5] = build_pi_idxs();

/// Round constants for ι, one per round (same values as `keccak::RC`,
/// re-derived in `tests::table_consistency` rather than trusted blind).
use crate::keccak::round_constants;

#[inline]
unsafe fn load(v: &[u64; 8]) -> __m512i {
    _mm512_loadu_si512(v.as_ptr() as *const i32 as *const _)
}

/// Vectorized Keccak-f\[1600\] permutation.
///
/// # Safety
///
/// The caller must have confirmed `is_x86_feature_detected!("avx512f")`
/// returns `true` before calling this function. Calling it on hardware
/// without AVX-512F support is undefined behavior.
#[target_feature(enable = "avx512f")]
pub unsafe fn keccak_f1600_avx512(state: &mut [u64; 25]) {
    // Load the 5 rows, padding lanes 5..8 with zero.
    let mut padded_in = [0u64; 40];
    for y in 0..5 {
        padded_in[y * 8..y * 8 + 5].copy_from_slice(&state[y * 5..y * 5 + 5]);
    }
    let mut row: [__m512i; 5] = [
        load(padded_in[0..8].try_into().unwrap()),
        load(padded_in[8..16].try_into().unwrap()),
        load(padded_in[16..24].try_into().unwrap()),
        load(padded_in[24..32].try_into().unwrap()),
        load(padded_in[32..40].try_into().unwrap()),
    ];

    let theta_minus1 = load(&THETA_MINUS1_IDX);
    let theta_plus1 = load(&THETA_PLUS1_IDX);
    let chi_shift1 = load(&CHI_SHIFT1_IDX);
    let chi_shift2 = load(&CHI_SHIFT2_IDX);
    let rot: [__m512i; 5] = [
        load(&ROT_ROW[0]),
        load(&ROT_ROW[1]),
        load(&ROT_ROW[2]),
        load(&ROT_ROW[3]),
        load(&ROT_ROW[4]),
    ];
    // Flattened [z][y] -> pi_idx_vecs[z * 5 + y], loaded once up front since
    // every value is a compile-time constant.
    let mut pi_idx_vecs: [__m512i; 25] = [_mm512_setzero_si512(); 25];
    for z in 0..5 {
        for y in 0..5 {
            pi_idx_vecs[z * 5 + y] = load(&PI_IDXS[z][y]);
        }
    }

    for &rc in round_constants().iter() {
        // ===== θ (theta) =====
        let mut c = row[0];
        c = _mm512_xor_si512(c, row[1]);
        c = _mm512_xor_si512(c, row[2]);
        c = _mm512_xor_si512(c, row[3]);
        c = _mm512_xor_si512(c, row[4]);

        let c_minus1 = _mm512_permutexvar_epi64(theta_minus1, c);
        let c_plus1 = _mm512_permutexvar_epi64(theta_plus1, c);
        let c_plus1_rot1 = _mm512_rol_epi64::<1>(c_plus1);
        let d = _mm512_xor_si512(c_minus1, c_plus1_rot1);

        for r in row.iter_mut() {
            *r = _mm512_xor_si512(*r, d);
        }

        // ===== ρ (rho) =====
        let mut rotated = [_mm512_setzero_si512(); 5];
        for y in 0..5 {
            rotated[y] = _mm512_rolv_epi64(row[y], rot[y]);
        }

        // ===== π (pi) — permutexvar + mask_blend per (destination row,
        // source row) pair, no memory round-trip (see the rustdoc above
        // PI_MASKS/PI_IDXS for why this replaced a gather-based version).
        let mut new_row = [_mm512_setzero_si512(); 5];
        for z in 0..5 {
            let mut acc = _mm512_setzero_si512();
            for y in 0..5 {
                let candidate = _mm512_permutexvar_epi64(pi_idx_vecs[z * 5 + y], rotated[y]);
                acc = _mm512_mask_blend_epi64(PI_MASKS[z][y], acc, candidate);
            }
            new_row[z] = acc;
        }

        // ===== χ (chi) =====
        for z in 0..5 {
            let a = new_row[z];
            let b = _mm512_permutexvar_epi64(chi_shift1, a);
            let c = _mm512_permutexvar_epi64(chi_shift2, a);
            row[z] = _mm512_ternarylogic_epi64::<CHI_TERNARY_IMM>(a, b, c);
        }

        // ===== ι (iota) =====
        let mut rc_lanes = [0u64; 8];
        rc_lanes[0] = rc;
        let rc_vec = load(&rc_lanes);
        row[0] = _mm512_xor_si512(row[0], rc_vec);
    }

    let mut padded_out = [0u64; 40];
    for y in 0..5 {
        _mm512_storeu_si512(
            padded_out[y * 8..y * 8 + 8].as_mut_ptr() as *mut i32 as *mut _,
            row[y],
        );
        state[y * 5..y * 5 + 5].copy_from_slice(&padded_out[y * 8..y * 8 + 5]);
    }
}

/// Safe dispatch: runs the AVX-512 permutation if the CPU supports it,
/// otherwise falls back to the scalar implementation. This is the only
/// sanctioned way to reach [`keccak_f1600_avx512`] from outside tests.
#[inline]
pub fn keccak_f1600_dispatch(state: &mut [u64; 25], use_avx512: bool) {
    if use_avx512 && is_x86_feature_detected!("avx512f") {
        // SAFETY: is_x86_feature_detected!("avx512f") was just checked.
        unsafe { keccak_f1600_avx512(state) }
    } else {
        crate::keccak::keccak_f1600(state);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::keccak::{keccak_f1600, PI, RC, RHO};

    fn avx512_available() -> bool {
        is_x86_feature_detected!("avx512f")
    }

    #[test]
    fn ternarylogic_truth_table_is_chi() {
        // Brute-force every (a,b,c) bit combination and confirm the
        // 0xD2 immediate computes a ^ (!b & c), bit for bit, via the
        // real instruction (not just hand arithmetic).
        if !avx512_available() {
            eprintln!("skipping: no avx512f on this host");
            return;
        }
        unsafe {
            for a_bit in 0u64..2 {
                for b_bit in 0u64..2 {
                    for c_bit in 0u64..2 {
                        let a = if a_bit == 1 { u64::MAX } else { 0 };
                        let b = if b_bit == 1 { u64::MAX } else { 0 };
                        let c = if c_bit == 1 { u64::MAX } else { 0 };
                        let expected = a ^ (!b & c);

                        let av = _mm512_set1_epi64(a as i64);
                        let bv = _mm512_set1_epi64(b as i64);
                        let cv = _mm512_set1_epi64(c as i64);
                        let rv = _mm512_ternarylogic_epi64::<CHI_TERNARY_IMM>(av, bv, cv);
                        let mut out = [0u64; 8];
                        _mm512_storeu_si512(out.as_mut_ptr() as *mut i32 as *mut _, rv);
                        assert_eq!(
                            out[0], expected,
                            "a={a_bit} b={b_bit} c={c_bit}: ternarylogic(0xD2) mismatch"
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn table_consistency() {
        // Every constant table in this module must be re-derivable from
        // keccak.rs's own RHO/PI/RC — this test fails loudly if either
        // side is edited without the other.
        for y in 0..5 {
            for x in 0..5 {
                assert_eq!(
                    ROT_ROW[y][x],
                    RHO[5 * y + x] as u64,
                    "ROT_ROW[{y}][{x}] must equal RHO[{}]",
                    5 * y + x
                );
            }
        }
        // PI_MASKS/PI_IDXS: for every destination row z and lane x, exactly
        // one source row y must have its mask bit x set (every destination
        // lane comes from exactly one source lane), and that row's index
        // table must carry the correct source lane at position x.
        for z in 0..5 {
            for x in 0..5 {
                let j = PI[5 * z + x];
                let expected_row = j / 5;
                let expected_lane = (j % 5) as u64;

                let mut rows_claiming_this_lane = 0;
                for y in 0..5 {
                    let bit_set = (PI_MASKS[z][y] >> x) & 1 == 1;
                    if bit_set {
                        rows_claiming_this_lane += 1;
                        assert_eq!(
                            y,
                            expected_row,
                            "PI_MASKS[{z}][{y}] claims lane {x}, but PI[{}] says the \
                             source row is {expected_row}",
                            5 * z + x
                        );
                        assert_eq!(
                            PI_IDXS[z][y][x],
                            expected_lane,
                            "PI_IDXS[{z}][{y}][{x}] must equal source_lane(PI[{}]) = {expected_lane}",
                            5 * z + x
                        );
                    }
                }
                assert_eq!(
                    rows_claiming_this_lane, 1,
                    "PI_MASKS[{z}][*] must have exactly one row claiming destination lane {x}"
                );
            }
        }
        assert_eq!(
            round_constants(),
            RC,
            "round_constants() must equal keccak::RC"
        );
    }

    #[test]
    fn matches_scalar_on_zero_state() {
        if !avx512_available() {
            eprintln!("skipping: no avx512f on this host");
            return;
        }
        let mut scalar_state = [0u64; 25];
        keccak_f1600(&mut scalar_state);

        let mut vector_state = [0u64; 25];
        unsafe { keccak_f1600_avx512(&mut vector_state) };

        assert_eq!(
            vector_state, scalar_state,
            "AVX-512 permutation of the zero state must match the scalar implementation"
        );
        // Cross-check against the same XKCP known-answer values the
        // scalar test already asserts.
        assert_eq!(vector_state[0], 0xF1258F7940E1DDE7);
        assert_eq!(vector_state[1], 0x84D5CCF933C0478A);
        assert_eq!(vector_state[2], 0xD598261EA65AA9EE);
        assert_eq!(vector_state[3], 0xBD1547306F80494D);
        assert_eq!(vector_state[4], 0x8B284E056253D057);
    }

    #[test]
    fn matches_scalar_on_random_states() {
        if !avx512_available() {
            eprintln!("skipping: no avx512f on this host");
            return;
        }
        // Deterministic xorshift64 PRNG — no external dependency needed
        // for this property test, and reproducible across runs.
        let mut seed: u64 = 0x9E3779B97F4A7C15;
        let mut next = || {
            seed ^= seed << 13;
            seed ^= seed >> 7;
            seed ^= seed << 17;
            seed
        };

        for trial in 0..2000 {
            let mut state = [0u64; 25];
            for lane in state.iter_mut() {
                *lane = next();
            }
            let mut scalar_state = state;
            keccak_f1600(&mut scalar_state);

            let mut vector_state = state;
            unsafe { keccak_f1600_avx512(&mut vector_state) };

            assert_eq!(
                vector_state, scalar_state,
                "trial {trial}: AVX-512 and scalar permutations diverged on random input {state:?}"
            );
        }
    }

    #[test]
    fn matches_scalar_after_multiple_permutations() {
        // Feeding the permutation's own output back in repeatedly is a
        // stronger test than independent random states: it catches a
        // bug that only manifests on states with structure the
        // permutation itself produces (e.g. specific bit patterns from
        // the round constants), not just uniform-random noise.
        if !avx512_available() {
            eprintln!("skipping: no avx512f on this host");
            return;
        }
        let mut scalar_state = [0u64; 25];
        let mut vector_state = [0u64; 25];
        for iteration in 0..50 {
            keccak_f1600(&mut scalar_state);
            unsafe { keccak_f1600_avx512(&mut vector_state) };
            assert_eq!(
                vector_state, scalar_state,
                "diverged after {iteration} chained permutations"
            );
        }
    }

    #[test]
    fn dispatch_respects_use_avx512_flag() {
        // With use_avx512: false, dispatch must take the scalar path
        // regardless of what the CPU supports — verified by comparing
        // against a direct scalar call on the same input.
        let mut direct_scalar = [1u64; 25];
        keccak_f1600(&mut direct_scalar);

        let mut via_dispatch_off = [1u64; 25];
        keccak_f1600_dispatch(&mut via_dispatch_off, false);

        assert_eq!(direct_scalar, via_dispatch_off);
    }

    #[test]
    fn dispatch_avx512_true_matches_scalar_output() {
        // Whether or not this host has avx512f, dispatch(true) must
        // produce the same *output* as scalar (it falls back safely
        // when unsupported, or produces an equal result when it does
        // run the vector path — either way the observable behavior is
        // identical).
        let mut expected = [7u64; 25];
        keccak_f1600(&mut expected);

        let mut actual = [7u64; 25];
        keccak_f1600_dispatch(&mut actual, true);

        assert_eq!(expected, actual);
    }
}