bcrypt-rust 0.1.0

Pure-Rust bcrypt with runtime-dispatched SIMD backends for batch hashing
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
//! x86-64 AVX2 EksBlowfish: eight independent bcrypt hashes in lockstep,
//! one per 32-bit ymm lane.
//!
//! Ports [`super::scalar`] exactly — same OpenBSD structure, same
//! continuous salt-word stream across the P→S transition in
//! `expand_state`, same key-then-salt cost-loop order — with every 32-bit
//! operation widened to a lane-wise `__m256i` one. Scalar `wrapping_add`
//! becomes `_mm256_add_epi32` (wraps, which is what Blowfish wants); every
//! XOR becomes `_mm256_xor_si256`.
//!
//! # State layout: struct-of-arrays
//!
//! The four S-boxes are stored interleaved by lane — [`SBoxes8`] — so
//! entry `(b, i)` holds all eight lanes contiguously and expansion-chain
//! writes stay full-vector stores. Lookups are gathers: lane `l` of a
//! lookup reads `box_base[idx_l * 8 + l]`. Unlike NEON, AVX2 *has* a
//! gather instruction — which is not always the fast choice.
//!
//! # Three lookup flavors, measured not assumed
//!
//! * **Gather** — [`lookup8_gather`], one `vpgatherdd` per box. Fast on
//!   Intel (hardware gather); on AMD Zen 2–4 `vpgatherdd` is microcoded
//!   and loses ~2–3× to scalar loads.
//! * **Insert** — `lookup8_insert!`: store the prescaled byte-offset
//!   vector, eight scalar loads, load the result back. On Intel this runs
//!   ~10–20 % behind the hardware gather.
//! * **Extract** — `lookup8_extract!`: `vpextrd` each prescaled offset
//!   lane to a GPR, scalar load, `vpinsrd` rebuild — no stack round-trip
//!   and no store-forward link at all (the shape LLVM's optimizer
//!   usually — but not reliably — turns Insert into).
//!
//! With `std` and optimized code a one-time shootout picks per CPU: all
//! flavors hash the same fixed group at cost 4 (outputs asserted
//! identical — a mismatch is a kernel bug), three interleaved reps each,
//! min wins, cached in [`CACHED_FLAVOR`]. Without `std` there is no
//! clock, and in debug builds or under Miri timing measures the codegen
//! mode, not the µarch — those builds take **Insert**, the minimax
//! default: with no microcoded instructions it stays within ~10–15 %
//! of the winner wherever measured (on Zen 4 Gather wins by ~12 %);
//! Gather's standing swings with µarch and toolchain, so it is only
//! ever picked by measurement.
//!
//! [`bcrypt_lanes`] reads the cache once per call and branches once per
//! **group** into three monomorphized kernel paths
//! (`bcrypt8::<FLAVOR_GATHER>` / `::<FLAVOR_INSERT>` /
//! `::<FLAVOR_EXTRACT>`), so no per-lookup branch exists.
//!
//! # Rosetta note
//!
//! Rosetta 2 (the dev host's only way to run this) executes AVX2 but
//! translates `vpgatherdd` poorly, so the shootout is expected to pick a
//! scalar-load flavor there. Rosetta numbers are translation-bound:
//! correctness is the gate under Rosetta, not speed.
//!
//! # Lookup invariant
//!
//! Every index vector handed to the lookups is derived from a byte-masked
//! value (a `>> 24` or `& 0xff` of a `u32` lane) produced inside
//! [`f8_gather`] / `f8_insert!` / `f8_extract!`. The gather flavor
//! receives the byte itself (`0..=255`), so `idx * 8 + lane` stays inside
//! the addressed 256-entry box; the insert and extract flavors receive it
//! prescaled to a byte offset (`idx * 32`, via an `& 0x1fe0` mask), so
//! `off + lane * 4` stays inside the box's 8 KiB. Either way a lookup can
//! never leave the 32 KiB [`SBoxes8`]. The invariant is stated once here
//! and referenced at every unsafe dereference below.
//!
//! # Zeroization
//!
//! With the `zeroize` feature the kernel wipes its named key-material
//! buffers — the `State8` expansion state, the `kwv` transposed key
//! schedule and `cdata` — before returning, best-effort like the rest of
//! the crate: compiler spills and transposition-internal temporaries are
//! not chased.

use core::arch::x86_64::{
    __m256i, _mm256_add_epi32, _mm256_and_si256, _mm256_extract_epi32, _mm256_i32gather_epi32,
    _mm256_insert_epi32, _mm256_loadu_si256, _mm256_set1_epi32, _mm256_setr_epi32,
    _mm256_setzero_si256, _mm256_slli_epi32, _mm256_srli_epi32, _mm256_store_si256,
    _mm256_storeu_si256, _mm256_xor_si256,
};
use core::sync::atomic::{AtomicU8, Ordering};

use crate::consts::{P_INIT, S_INIT};

/// Passwords per kernel call — the eight 32-bit AVX2 lanes.
const LANES: usize = 8;

/// Eight lockstep S-boxes, interleaved by lane: entry `b * 256 + i` is the
/// `(b, i)` Blowfish S-box word of all eight lanes, lane `l` at u32 offset
/// `(b * 256 + i) * 8 + l`. 32 KiB, 64-byte aligned, so every entry is
/// 32-byte aligned by construction (the entry stride divides the struct
/// alignment) and aligned vector stores are legal on every entry.
#[repr(C, align(64))]
struct SBoxes8([[u32; 8]; 1024]);

impl SBoxes8 {
    /// Base pointer of box `b` — the address lane gathers index from.
    #[inline(always)]
    fn box_base(&self, b: usize) -> *const u32 {
        debug_assert!(b < 4);
        // SAFETY: `b < 4`, so `b * 2048` stays inside the 8192-word array.
        unsafe { self.0.as_ptr().cast::<u32>().add(b * 256 * 8) }
    }
}

/// The lockstep EksBlowfish state: 18 P-vectors plus the interleaved
/// S-boxes — the lane-wise image of scalar `State`.
struct State8 {
    p: [__m256i; 18],
    s: SBoxes8,
}

/// One scalar S-box load: `$base` (a box pointer) + `$byte_off`, with a
/// zero-instruction opaque `asm!` identity on the address. Uniform
/// `base + off` scalar loads are a gather pattern LLVM can re-form into a
/// microcoded `vpgather*q` (it did for the 16-lane sibling under rustc
/// 1.99 — the very instruction the scalar-load flavors exist to avoid).
/// The blocker keeps every load scalar — the same SLP blocker
/// `super::neon` uses. The caller wraps the invocation in `unsafe` and
/// guarantees the module-level lookup invariant (the address stays inside
/// the addressed box and is 4-aligned).
macro_rules! lane_load {
    ($base:expr, $byte_off:expr) => {{
        let mut a = $base as usize + $byte_off;
        core::arch::asm!(
            "/* {0} */",
            inout(reg) a,
            options(pure, nomem, nostack, preserves_flags),
        );
        *(a as *const u32)
    }};
}

/// Gather one S-box word per lane with the hardware gather: lane `l` loads
/// `box_base[idx_l * 8 + l]` — vindex `idx * 8 + {0..8}`, scale 4, i.e.
/// byte offset `(idx * 8 + lane) * 4`. `lane_off` is hoisted by the caller
/// (one constant per F evaluation).
///
/// # Safety
///
/// `box_base` must point at the first word of one 256-entry box of an
/// [`SBoxes8`], and every lane of `idx` must be in `0..=255` — the
/// module-level lookup invariant. Then `idx_l * 8 + l < 2048` and the
/// gather stays inside that box's 8 KiB.
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn lookup8_gather(box_base: *const i32, idx: __m256i, lane_off: __m256i) -> __m256i {
    let vindex = _mm256_add_epi32(_mm256_slli_epi32::<3>(idx), lane_off);
    // SAFETY: the contract above is forwarded from the caller; `vindex`
    // lanes are exactly the bounded `idx_l * 8 + l`.
    unsafe { _mm256_i32gather_epi32::<4>(box_base, vindex) }
}

/// Gather one S-box word per lane without `vpgatherdd`, as a MACRO so
/// every call site gets a textually inlined copy: store the *prescaled*
/// byte-offset vector to the stack, eight scalar loads in GPR code, load
/// the result back. `$off` lane `l` must already hold the byte offset
/// `idx_l * 32` (the [`SBoxes8`] entry stride) — prescaling in vector
/// form in `f8_insert!` removes the per-lane GPR `shl` chains the
/// word-index spelling needed, so every load here is displacement-only.
/// The flavor that wins where gathers are microcoded.
///
/// A macro because LLVM refuses to inline the scalar-load F flavors into
/// the unrolled zero chain: as fns they compiled to 16 outlined `callq`
/// per encipher, and `#[inline(always)]` on a `#[target_feature]` fn is
/// a hard error (rust#145574) — the `rounds16!` reasoning, one level
/// down.
///
/// # Safety
///
/// `$box_base` must evaluate to the first word of one 256-entry box of an
/// [`SBoxes8`], and every lane of `$off` must be a multiple of 32 in
/// `0..=8160` — the module-level lookup invariant, prescaled. Then
/// `off_l + l * 4 <= 8188` is a 4-aligned byte offset inside that box's
/// 8 KiB.
macro_rules! lookup8_insert {
    ($box_base:expr, $off:expr) => {{
        let base = ($box_base).cast::<u8>();
        let mut offs = [0u32; 8];
        // SAFETY: `offs` is a live 32-byte stack array, writable in full.
        unsafe { _mm256_storeu_si256(offs.as_mut_ptr().cast::<__m256i>(), $off) };
        let gathered = [
            // SAFETY: lookup invariant (prescaled) — `offs[l]` is a
            // multiple of 32 at most 8160, so `offs[l] + l * 4` is a
            // 4-aligned byte offset inside the box `base` points at
            // (repeated per load).
            unsafe { lane_load!(base, offs[0] as usize) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[1] as usize + 4) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[2] as usize + 8) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[3] as usize + 12) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[4] as usize + 16) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[5] as usize + 20) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[6] as usize + 24) },
            // SAFETY: lookup invariant — see lane 0.
            unsafe { lane_load!(base, offs[7] as usize + 28) },
        ];
        // SAFETY: `gathered` is a live 32-byte stack array, readable in
        // full.
        unsafe { _mm256_loadu_si256(gathered.as_ptr().cast::<__m256i>()) }
    }};
}

/// `lookup8_insert!` without the stack round-trip, as a MACRO (same
/// force-inline reasoning): `vpextrd` each prescaled offset lane to a
/// GPR, scalar load, `vpinsrd` rebuild into a vector register. Removes
/// the store-forward link entirely — the shape LLVM's optimizer usually,
/// but not reliably, turns Insert into.
///
/// # Safety
///
/// Same contract as `lookup8_insert!`.
macro_rules! lookup8_extract {
    ($box_base:expr, $off:expr) => {{
        let base = ($box_base).cast::<u8>();
        let off = $off;
        // SAFETY: lookup invariant (prescaled), repeated per lane — every
        // extracted lane is a multiple of 32 at most 8160, so `+ l * 4`
        // is a 4-aligned byte offset inside the box `base` points at.
        let g0 = unsafe { lane_load!(base, _mm256_extract_epi32::<0>(off) as usize) };
        // SAFETY: lookup invariant — see lane 0.
        let g1 = unsafe { lane_load!(base, _mm256_extract_epi32::<1>(off) as usize + 4) };
        // SAFETY: lookup invariant — see lane 0.
        let g2 = unsafe { lane_load!(base, _mm256_extract_epi32::<2>(off) as usize + 8) };
        // SAFETY: lookup invariant — see lane 0.
        let g3 = unsafe { lane_load!(base, _mm256_extract_epi32::<3>(off) as usize + 12) };
        // SAFETY: lookup invariant — see lane 0.
        let g4 = unsafe { lane_load!(base, _mm256_extract_epi32::<4>(off) as usize + 16) };
        // SAFETY: lookup invariant — see lane 0.
        let g5 = unsafe { lane_load!(base, _mm256_extract_epi32::<5>(off) as usize + 20) };
        // SAFETY: lookup invariant — see lane 0.
        let g6 = unsafe { lane_load!(base, _mm256_extract_epi32::<6>(off) as usize + 24) };
        // SAFETY: lookup invariant — see lane 0.
        let g7 = unsafe { lane_load!(base, _mm256_extract_epi32::<7>(off) as usize + 28) };
        let mut v = _mm256_setzero_si256();
        v = _mm256_insert_epi32::<0>(v, g0 as i32);
        v = _mm256_insert_epi32::<1>(v, g1 as i32);
        v = _mm256_insert_epi32::<2>(v, g2 as i32);
        v = _mm256_insert_epi32::<3>(v, g3 as i32);
        v = _mm256_insert_epi32::<4>(v, g4 as i32);
        v = _mm256_insert_epi32::<5>(v, g5 as i32);
        v = _mm256_insert_epi32::<6>(v, g6 as i32);
        _mm256_insert_epi32::<7>(v, g7 as i32)
    }};
}

/// The Blowfish round function, lane-wise, gather flavor:
/// `((S0[a] + S1[b]) ^ S2[c]) + S3[d]`, bytes taken MSB-first, every
/// addition wrapping (`_mm256_add_epi32` wraps, which is what the scalar
/// `wrapping_add` does). The byte masks below are what make the
/// [`lookup8_gather`] gathers safe — see the module-level invariant.
#[target_feature(enable = "avx2")]
#[inline]
fn f8_gather(s: &SBoxes8, x: __m256i) -> __m256i {
    let byte_mask = _mm256_set1_epi32(0xff);
    let a = _mm256_srli_epi32::<24>(x);
    let b = _mm256_and_si256(_mm256_srli_epi32::<16>(x), byte_mask);
    let c = _mm256_and_si256(_mm256_srli_epi32::<8>(x), byte_mask);
    let d = _mm256_and_si256(x, byte_mask);
    // Lane gather offsets `{0..8}`, hoisted out of the four lookups.
    let lane_off = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
    // SAFETY: every index lane is a byte — `a` by shifting the other bytes
    // away, `b`/`c`/`d` by the `& 0xff` mask right above — and each
    // `box_base` points at a real box of `s`, so the module-level lookup
    // invariant holds at all four gathers.
    let (va, vb, vc, vd) = unsafe {
        (
            lookup8_gather(s.box_base(0).cast::<i32>(), a, lane_off),
            lookup8_gather(s.box_base(1).cast::<i32>(), b, lane_off),
            lookup8_gather(s.box_base(2).cast::<i32>(), c, lane_off),
            lookup8_gather(s.box_base(3).cast::<i32>(), d, lane_off),
        )
    };
    _mm256_add_epi32(_mm256_xor_si256(_mm256_add_epi32(va, vb), vc), vd)
}

/// [`f8_gather`], insert flavor, as a MACRO — textually inlined at every
/// call site for the same reason `lookup8_insert!` is a macro (LLVM
/// outlined the scalar-load F as 16 `callq` per encipher;
/// `#[inline(always)]` on `#[target_feature]` is a hard error). Identical
/// math to [`f8_gather`] — see the module-level flavor notes. The byte
/// extraction folds the ×32 stride into the mask (`0x1fe0 = 0xff << 5`),
/// so the lookups receive finished byte offsets: `(x >> 19) & 0x1fe0` is
/// `(x >> 24) * 32`, and so on — 8 vector ops per F instead of 32 GPR
/// shifts.
macro_rules! f8_insert {
    ($s:expr, $x:expr) => {{
        let s = $s;
        let x = $x;
        let stride_mask = _mm256_set1_epi32(0x1fe0);
        let a = _mm256_and_si256(_mm256_srli_epi32::<19>(x), stride_mask);
        let b = _mm256_and_si256(_mm256_srli_epi32::<11>(x), stride_mask);
        let c = _mm256_and_si256(_mm256_srli_epi32::<3>(x), stride_mask);
        let d = _mm256_and_si256(_mm256_slli_epi32::<5>(x), stride_mask);
        // Every offset lane is a byte value shifted left by 5 — a
        // multiple of 32 at most 8160 — and each `box_base` points at a
        // real box of `s`, so the module-level lookup invariant holds
        // (prescaled) at all four lookups; that is the contract
        // `lookup8_insert!`'s internal unsafe blocks rely on.
        let (va, vb, vc, vd) = (
            lookup8_insert!(s.box_base(0), a),
            lookup8_insert!(s.box_base(1), b),
            lookup8_insert!(s.box_base(2), c),
            lookup8_insert!(s.box_base(3), d),
        );
        _mm256_add_epi32(_mm256_xor_si256(_mm256_add_epi32(va, vb), vc), vd)
    }};
}

/// `f8_insert!`, extract flavor: identical math through
/// `lookup8_extract!` — same prescaled extraction, no stack round-trip.
/// A macro for the same force-inline reason.
macro_rules! f8_extract {
    ($s:expr, $x:expr) => {{
        let s = $s;
        let x = $x;
        let stride_mask = _mm256_set1_epi32(0x1fe0);
        let a = _mm256_and_si256(_mm256_srli_epi32::<19>(x), stride_mask);
        let b = _mm256_and_si256(_mm256_srli_epi32::<11>(x), stride_mask);
        let c = _mm256_and_si256(_mm256_srli_epi32::<3>(x), stride_mask);
        let d = _mm256_and_si256(_mm256_slli_epi32::<5>(x), stride_mask);
        // Same argument as `f8_insert!` — prescaled byte offsets into
        // real boxes, so the module-level lookup invariant holds at all
        // four lookups; that is the contract `lookup8_extract!`'s
        // internal unsafe blocks rely on.
        let (va, vb, vc, vd) = (
            lookup8_extract!(s.box_base(0), a),
            lookup8_extract!(s.box_base(1), b),
            lookup8_extract!(s.box_base(2), c),
            lookup8_extract!(s.box_base(3), d),
        );
        _mm256_add_epi32(_mm256_xor_si256(_mm256_add_epi32(va, vb), vc), vd)
    }};
}

/// The flavor-dispatched F, as a MACRO so the scalar-load flavors get
/// textual inlining (see `f8_insert!`). `$flavor` is the caller's
/// const generic (one of [`FLAVOR_GATHER`] / [`FLAVOR_INSERT`] /
/// [`FLAVOR_EXTRACT`]), so the branch folds at monomorphization and no
/// per-lookup branch exists in any kernel path.
macro_rules! f8 {
    ($flavor:ident, $s:expr, $x:expr) => {{
        if $flavor == FLAVOR_GATHER {
            f8_gather($s, $x)
        } else if $flavor == FLAVOR_EXTRACT {
            f8_extract!($s, $x)
        } else {
            f8_insert!($s, $x)
        }
    }};
}

/// The 16 Feistel rounds of one lockstep encryption, as a macro so every
/// caller gets a *textually* inlined copy — the same reasoning as
/// `super::neon`'s `rounds16!` (an outlined `#[target_feature]` rounds fn
/// let LLVM keep the box-base math per call). Fully unrolled swap-free
/// round pairs: the pair ends in reference post-swap orientation, so the
/// whitening lands on the exchanged registers and the final swap-undo is
/// the one `mem::swap` after the pairs (register renaming; free).
macro_rules! rounds16 {
    ($state:expr, $flavor:ident, $l:ident, $r:ident) => {{
        macro_rules! pair {
            ($i:expr) => {{
                $l = _mm256_xor_si256($l, $state.p[$i]);
                $r = _mm256_xor_si256($r, f8!($flavor, &$state.s, $l));
                $r = _mm256_xor_si256($r, $state.p[$i + 1]);
                $l = _mm256_xor_si256($l, f8!($flavor, &$state.s, $r));
            }};
        }
        pair!(0);
        pair!(2);
        pair!(4);
        pair!(6);
        pair!(8);
        pair!(10);
        pair!(12);
        pair!(14);
        $l = _mm256_xor_si256($l, $state.p[16]);
        $r = _mm256_xor_si256($r, $state.p[17]);
        core::mem::swap(&mut $l, &mut $r);
    }};
}

/// One Blowfish block encryption, eight lanes in lockstep: 16 Feistel
/// rounds with the final swap undone, the output halves whitened by
/// P[16]/P[17] — identical control flow to scalar `encipher`.
#[target_feature(enable = "avx2")]
#[inline]
fn encipher8<const FLAVOR: u8>(
    state: &State8,
    mut l: __m256i,
    mut r: __m256i,
) -> (__m256i, __m256i) {
    rounds16!(state, FLAVOR, l, r);
    (l, r)
}

/// Lockstep `Blowfish_expandstate`: fresh state from the pi digits with the
/// key XORed into P, then 521 encryptions mixing salt words into the
/// running block and writing each ciphertext pair back over P (9 pairs)
/// and then S (512 pairs).
///
/// The salt counter `j` is one continuous stream across **both** loops —
/// the P loop consumes 18 words, so the first S-box pair XORs `salt[2]`
/// and `salt[3]`. Faithful to scalar `expand_state`; do not "tidy" the
/// counter into the S loop.
#[target_feature(enable = "avx2")]
#[inline]
fn expand_state_v8<const FLAVOR: u8>(
    state: &mut State8,
    swv: &[__m256i; 4],
    kwv: &[__m256i; 18],
) {
    for ((p, &init), &k) in state.p.iter_mut().zip(P_INIT.iter()).zip(kwv.iter()) {
        *p = _mm256_xor_si256(_mm256_set1_epi32(init as i32), k);
    }
    for (entry, &init) in state.s.0.iter_mut().zip(S_INIT.as_flattened().iter()) {
        *entry = [init; 8];
    }
    let (mut l, mut r) = (_mm256_setzero_si256(), _mm256_setzero_si256());
    let mut j = 0usize;
    for pair in 0..9 {
        l = _mm256_xor_si256(l, swv[j % 4]);
        j += 1;
        r = _mm256_xor_si256(r, swv[j % 4]);
        j += 1;
        rounds16!(state, FLAVOR, l, r);
        state.p[2 * pair] = l;
        state.p[2 * pair + 1] = r;
    }
    for b in 0..4 {
        for pair in 0..128 {
            l = _mm256_xor_si256(l, swv[j % 4]);
            j += 1;
            r = _mm256_xor_si256(r, swv[j % 4]);
            j += 1;
            rounds16!(state, FLAVOR, l, r);
            let e = b * 256 + 2 * pair;
            // SAFETY: entries `e`/`e + 1` are 32-byte aligned by
            // construction (struct align 64, entry stride 32) and
            // exclusively owned — aligned stores of the full vectors.
            unsafe {
                _mm256_store_si256(state.s.0[e].as_mut_ptr().cast::<__m256i>(), l);
                _mm256_store_si256(state.s.0[e + 1].as_mut_ptr().cast::<__m256i>(), r);
            }
        }
    }
}

/// The 521-encryption zero chain shared by both lockstep `expand0state`
/// variants: overwrite P (9 pairs) then S (512 pairs) exactly as
/// [`expand_state_v8`] does, minus the salt mixing.
#[target_feature(enable = "avx2")]
#[inline]
fn encrypt_zero_chain_v8<const FLAVOR: u8>(state: &mut State8) {
    let (mut l, mut r) = (_mm256_setzero_si256(), _mm256_setzero_si256());
    for pair in 0..9 {
        rounds16!(state, FLAVOR, l, r);
        state.p[2 * pair] = l;
        state.p[2 * pair + 1] = r;
    }
    for b in 0..4 {
        for pair in 0..128 {
            rounds16!(state, FLAVOR, l, r);
            let e = b * 256 + 2 * pair;
            // SAFETY: same argument as `expand_state_v8` — 32-byte aligned,
            // exclusively owned entries.
            unsafe {
                _mm256_store_si256(state.s.0[e].as_mut_ptr().cast::<__m256i>(), l);
                _mm256_store_si256(state.s.0[e + 1].as_mut_ptr().cast::<__m256i>(), r);
            }
        }
    }
}

/// Lockstep `Blowfish_expand0state(key)`: XOR the password words into P,
/// then run the zero chain.
#[target_feature(enable = "avx2")]
#[inline]
fn expand0state_v8<const FLAVOR: u8>(state: &mut State8, wv: &[__m256i; 18]) {
    for (p, &w) in state.p.iter_mut().zip(wv.iter()) {
        *p = _mm256_xor_si256(*p, w);
    }
    encrypt_zero_chain_v8::<FLAVOR>(state);
}

/// Lockstep `Blowfish_expand0state(salt)`: the salt is exactly 4 words, so
/// the P XOR cycles it (`i & 3`), then the same zero chain.
#[target_feature(enable = "avx2")]
#[inline]
fn expand0state_salt_v8<const FLAVOR: u8>(state: &mut State8, swv: &[__m256i; 4]) {
    for (i, p) in state.p.iter_mut().enumerate() {
        *p = _mm256_xor_si256(*p, swv[i & 3]);
    }
    encrypt_zero_chain_v8::<FLAVOR>(state);
}

/// Transpose the eight lanes' key words into 18 vectors: vector `i` holds
/// word `i` of lanes 0..=7. Runs once per group, so the plain `setr`
/// gathers below are fine — nothing here is on the cost-loop path.
#[target_feature(enable = "avx2")]
#[inline]
fn transpose_keys(key_words: &[[u32; 18]]) -> [__m256i; 18] {
    debug_assert_eq!(key_words.len(), LANES);
    let mut out = [_mm256_setzero_si256(); 18];
    for (i, v) in out.iter_mut().enumerate() {
        *v = _mm256_setr_epi32(
            key_words[0][i] as i32,
            key_words[1][i] as i32,
            key_words[2][i] as i32,
            key_words[3][i] as i32,
            key_words[4][i] as i32,
            key_words[5][i] as i32,
            key_words[6][i] as i32,
            key_words[7][i] as i32,
        );
    }
    out
}

/// Transpose the eight lanes' salt words into 4 vectors; see
/// [`transpose_keys`].
#[target_feature(enable = "avx2")]
#[inline]
fn transpose_salts(salt_words: &[[u32; 4]]) -> [__m256i; 4] {
    debug_assert_eq!(salt_words.len(), LANES);
    let mut out = [_mm256_setzero_si256(); 4];
    for (i, v) in out.iter_mut().enumerate() {
        *v = _mm256_setr_epi32(
            salt_words[0][i] as i32,
            salt_words[1][i] as i32,
            salt_words[2][i] as i32,
            salt_words[3][i] as i32,
            salt_words[4][i] as i32,
            salt_words[5][i] as i32,
            salt_words[6][i] as i32,
            salt_words[7][i] as i32,
        );
    }
    out
}

/// The whole lockstep bcrypt for one flavor: one key+salt expansion,
/// `2^cost` rounds of key-then-salt expansion (OpenBSD order), then 64
/// encryptions of the "OrpheanBeholderScryDoubt" constant and a big-endian
/// store per lane.
///
/// Every kernel function carries the same `#[target_feature(enable =
/// "avx2")]` scope — stdarch annotates the AVX2 intrinsics, and a
/// matching-scope caller both calls and inlines them without `unsafe` —
/// so the whole kernel compiles as one AVX2 unit behind the
/// [`bcrypt_lanes`] entry point.
///
/// # Safety
///
/// The same contract as [`bcrypt_lanes`]: all three slices are exactly
/// [`LANES`] long and `outs` is not aliased. The raw-pointer dereferences
/// inside are governed by the module-level lookup invariant: S-box
/// gathers index with byte-masked lanes only.
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn bcrypt8<const FLAVOR: u8>(
    cost: u32,
    key_words: &[[u32; 18]],
    salt_words: &[[u32; 4]],
    outs: &mut [[u8; 24]],
) {
    debug_assert!((4..=31).contains(&cost));
    debug_assert_eq!(key_words.len(), LANES);
    debug_assert_eq!(salt_words.len(), LANES);
    debug_assert_eq!(outs.len(), LANES);
    // `mut` serves only the `zeroize` wipe at the bottom of this fn.
    #[cfg_attr(not(feature = "zeroize"), allow(unused_mut))]
    let mut kwv = transpose_keys(key_words);
    let swv = transpose_salts(salt_words);
    let mut state = State8 {
        p: [_mm256_setzero_si256(); 18],
        s: SBoxes8([[0; 8]; 1024]),
    };
    expand_state_v8::<FLAVOR>(&mut state, &swv, &kwv);
    for _ in 0..(1u64 << cost) {
        // OpenBSD order: the password expansion first, the salt second.
        expand0state_v8::<FLAVOR>(&mut state, &kwv);
        expand0state_salt_v8::<FLAVOR>(&mut state, &swv);
    }
    // "OrpheanBeholderScryDoubt" as six broadcast words.
    let mut cdata = [
        _mm256_set1_epi32(0x4f72_7068),
        _mm256_set1_epi32(0x6561_6e42),
        _mm256_set1_epi32(0x6568_6f6c),
        _mm256_set1_epi32(0x6465_7253),
        _mm256_set1_epi32(0x6372_7944),
        _mm256_set1_epi32(0x6f75_6274),
    ];
    for _ in 0..64 {
        for pair in 0..3 {
            let (l, r) = encipher8::<FLAVOR>(&state, cdata[2 * pair], cdata[2 * pair + 1]);
            cdata[2 * pair] = l;
            cdata[2 * pair + 1] = r;
        }
    }
    // Split the lanes back out: word `w` of lane `l`'s output is lane `l`
    // of `cdata[w]`, stored big-endian.
    for (w, &cv) in cdata.iter().enumerate() {
        let mut words = [0u32; 8];
        // SAFETY: `words` is a live 32-byte stack array, writable in full.
        unsafe { _mm256_storeu_si256(words.as_mut_ptr().cast::<__m256i>(), cv) };
        for (lane, out) in outs.iter_mut().enumerate() {
            out[4 * w..4 * w + 4].copy_from_slice(&words[lane].to_be_bytes());
        }
    }
    #[cfg(feature = "zeroize")]
    {
        // SAFETY: a `__m256i` is eight `u32`s, so the `*mut u32` view
        // covers exactly the same exclusively-owned stack bytes as `state.p`.
        crate::wipe::secure_wipe_u32(unsafe {
            core::slice::from_raw_parts_mut(state.p.as_mut_ptr().cast::<u32>(), 18 * 8)
        });
        crate::wipe::secure_wipe_u32(state.s.0.as_flattened_mut());
        // SAFETY: same reinterpretation as above, for the six `cdata`
        // vectors: 48 exclusively-owned stack words, valid for writes.
        crate::wipe::secure_wipe_u32(unsafe {
            core::slice::from_raw_parts_mut(cdata.as_mut_ptr().cast::<u32>(), 6 * 8)
        });
        // `kwv` is the transposed key schedule — all eight lanes'
        // password-derived key words — so it is wiped with the state.
        // SAFETY: same reinterpretation as above, for the eighteen `kwv`
        // vectors: 144 exclusively-owned stack words, valid for writes.
        crate::wipe::secure_wipe_u32(unsafe {
            core::slice::from_raw_parts_mut(kwv.as_mut_ptr().cast::<u32>(), 18 * 8)
        });
    }
}

// ---------------------------------------------------------------------------
// Flavor selection
// ---------------------------------------------------------------------------

/// Which S-box lookup spelling the kernel runs — see the module-level
/// flavor notes.
#[derive(Copy, Clone)]
enum Flavor {
    /// `_mm256_i32gather_epi32` (`vpgatherdd`).
    Gather,
    /// Stack round-trip: store offsets, eight scalar loads, load back.
    Insert,
    /// `vpextrd` lane → GPR → scalar load → `vpinsrd` rebuild.
    Extract,
}

/// Sentinel meaning "the shootout has not run yet". Not a valid flavor.
const FLAVOR_UNINIT: u8 = 0;
const FLAVOR_GATHER: u8 = 1;
const FLAVOR_INSERT: u8 = 2;
const FLAVOR_EXTRACT: u8 = 3;

/// Cached [`Flavor`] as a `u8`, or [`FLAVOR_UNINIT`].
static CACHED_FLAVOR: AtomicU8 = AtomicU8::new(FLAVOR_UNINIT);

const fn flavor_to_u8(flavor: Flavor) -> u8 {
    match flavor {
        Flavor::Gather => FLAVOR_GATHER,
        Flavor::Insert => FLAVOR_INSERT,
        Flavor::Extract => FLAVOR_EXTRACT,
    }
}

/// Anything unknown maps to Insert — the conservative default — so the
/// cache can never produce a panic.
const fn flavor_from_u8(value: u8) -> Flavor {
    match value {
        FLAVOR_GATHER => Flavor::Gather,
        FLAVOR_EXTRACT => Flavor::Extract,
        _ => Flavor::Insert,
    }
}

/// The flavor for this process: one relaxed atomic load on the hot path.
/// Not a `OnceLock` for the same reason as `super::backend`: there is no
/// data to publish, and a racing duplicate pick computes the same class of
/// answer (a wrong pick costs a few percent, never correctness — the
/// shootout asserts all flavors agree before timing anything).
#[inline]
fn flavor() -> Flavor {
    let cached = CACHED_FLAVOR.load(Ordering::Relaxed);
    if cached == FLAVOR_UNINIT {
        pick_and_cache()
    } else {
        flavor_from_u8(cached)
    }
}

/// Pick and populate the cache. Outlined so [`flavor`] stays tiny.
#[cold]
#[inline(never)]
fn pick_and_cache() -> Flavor {
    let picked = pick_flavor();
    CACHED_FLAVOR.store(flavor_to_u8(picked), Ordering::Relaxed);
    picked
}

/// With a clock and optimized code: measure all flavors and keep the
/// winner. In debug builds or under Miri, timing unoptimized/interpreted
/// intrinsics measures the codegen mode, not the µarch — and without
/// `std` there is no clock at all. All three take the minimax default
/// [`Flavor::Insert`] (module-level flavor notes).
#[cfg(all(feature = "std", not(debug_assertions), not(miri)))]
fn pick_flavor() -> Flavor {
    shootout()
}

/// See the measuring variant above.
#[cfg(not(all(feature = "std", not(debug_assertions), not(miri))))]
fn pick_flavor() -> Flavor {
    Flavor::Insert
}

/// The one-time flavor shootout: all three flavors hash the same fixed
/// deterministic 8-password group at cost 4 — correctness first
/// (identical outputs; a mismatch is a kernel bug, not a tie), then three
/// interleaved timed reps each, min per side, fastest wins.
#[cfg(all(feature = "std", not(debug_assertions), not(miri)))]
fn shootout() -> Flavor {
    use std::time::Instant;

    // SplitMix64, fixed seed: deterministic inputs (not a CSPRNG, nor does
    // it need to be).
    let mut rng = 0xBC79_7A5A_F1A0_0001u64;
    let mut next_u32 = move || {
        rng = rng.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = rng;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        (z ^ (z >> 31)) as u32
    };
    let mut kws = [[0u32; 18]; LANES];
    let mut sws = [[0u32; 4]; LANES];
    for w in kws.as_flattened_mut().iter_mut().chain(sws.as_flattened_mut()) {
        *w = next_u32();
    }
    let mut outs = [[0u8; 24]; LANES];
    // SAFETY (this whole fn): the shootout is reached only through
    // `bcrypt_lanes`, which the dispatch table hands out solely for
    // `Backend::Avx2` on CPUs advertising AVX2 (or the arch-gated tests,
    // which check `is_available` first); the slices are exactly 8 lanes.
    // SAFETY: per the fn-level comment above — dispatch hands this fn out
        // only for AVX2-capable CPUs; the slices are exactly 8 lanes.
        unsafe { bcrypt8::<FLAVOR_GATHER>(4, &kws, &sws, &mut outs) };
    let reference = outs;
    // SAFETY: per the fn-level comment above — dispatch hands this fn out
        // only for AVX2-capable CPUs; the slices are exactly 8 lanes.
        unsafe { bcrypt8::<FLAVOR_INSERT>(4, &kws, &sws, &mut outs) };
    assert_eq!(
        outs, reference,
        "avx2 flavor shootout: gather and insert kernels diverged — a kernel bug, not timing"
    );
    // SAFETY: per the fn-level comment above — dispatch hands this fn out
        // only for AVX2-capable CPUs; the slices are exactly 8 lanes.
        unsafe { bcrypt8::<FLAVOR_EXTRACT>(4, &kws, &sws, &mut outs) };
    assert_eq!(
        outs, reference,
        "avx2 flavor shootout: gather and extract kernels diverged — a kernel bug, not timing"
    );
    let (mut best_gather, mut best_insert, mut best_extract) = (f64::MAX, f64::MAX, f64::MAX);
    for _ in 0..3 {
        let start = Instant::now();
        // SAFETY: per the fn-level comment above — dispatch hands this fn out
                // only for AVX2-capable CPUs; the slices are exactly 8 lanes.
            unsafe { bcrypt8::<FLAVOR_GATHER>(4, &kws, &sws, &mut outs) };
        best_gather = best_gather.min(start.elapsed().as_secs_f64());
        let start = Instant::now();
        // SAFETY: per the fn-level comment above — dispatch hands this fn out
                // only for AVX2-capable CPUs; the slices are exactly 8 lanes.
            unsafe { bcrypt8::<FLAVOR_INSERT>(4, &kws, &sws, &mut outs) };
        best_insert = best_insert.min(start.elapsed().as_secs_f64());
        let start = Instant::now();
        // SAFETY: per the fn-level comment above — dispatch hands this fn out
                // only for AVX2-capable CPUs; the slices are exactly 8 lanes.
            unsafe { bcrypt8::<FLAVOR_EXTRACT>(4, &kws, &sws, &mut outs) };
        best_extract = best_extract.min(start.elapsed().as_secs_f64());
        core::hint::black_box(&mut outs);
    }
    if best_gather <= best_insert && best_gather <= best_extract {
        Flavor::Gather
    } else if best_insert <= best_extract {
        Flavor::Insert
    } else {
        Flavor::Extract
    }
}

/// The flavor branch: resolved once per GROUP (one relaxed atomic load),
/// then one of three fully monomorphized kernel paths — no per-lookup
/// branch exists in any path.
///
/// # Safety
///
/// Forwards [`bcrypt_lanes`]' contract unchanged.
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn bcrypt_lanes_impl(
    cost: u32,
    key_words: &[[u32; 18]],
    salt_words: &[[u32; 4]],
    outs: &mut [[u8; 24]],
) {
    debug_assert!((4..=31).contains(&cost));
    debug_assert_eq!(key_words.len(), LANES);
    debug_assert_eq!(salt_words.len(), LANES);
    debug_assert_eq!(outs.len(), LANES);
    match flavor() {
        // SAFETY: forwards this fn's contract unchanged.
        Flavor::Gather => unsafe { bcrypt8::<FLAVOR_GATHER>(cost, key_words, salt_words, outs) },
        // SAFETY: forwards this fn's contract unchanged.
        Flavor::Insert => unsafe { bcrypt8::<FLAVOR_INSERT>(cost, key_words, salt_words, outs) },
        // SAFETY: forwards this fn's contract unchanged.
        Flavor::Extract => unsafe { bcrypt8::<FLAVOR_EXTRACT>(cost, key_words, salt_words, outs) },
    }
}

/// The AVX2 batch kernel: [`LANES`] independent bcrypt hashes in lockstep,
/// one per 32-bit vector lane.
///
/// Implements the [`super::BcryptLanesFn`] contract for
/// [`super::Backend::Avx2`]: `cost` is `4..=31` (validated upstream in
/// `crate::core`) and the three slices are exactly [`LANES`] long. Resolve
/// through [`super::bcrypt_lanes_fn`]; never call directly without an
/// availability check.
///
/// # Safety
///
/// * The CPU must support AVX2. The dispatch table hands this pointer out
///   only for [`super::Backend::Avx2`], whose `is_available` is the
///   documented check.
/// * `key_words`, `salt_words` and `outs` must each be exactly [`LANES`]
///   long (debug-asserted at entry), and `outs` must not be aliased for
///   the duration of the call.
#[target_feature(enable = "avx2")]
pub unsafe fn bcrypt_lanes(
    cost: u32,
    key_words: &[[u32; 18]],
    salt_words: &[[u32; 4]],
    outs: &mut [[u8; 24]],
) {
    debug_assert!((4..=31).contains(&cost));
    debug_assert_eq!(key_words.len(), LANES);
    debug_assert_eq!(salt_words.len(), LANES);
    debug_assert_eq!(outs.len(), LANES);
    // SAFETY: the caller upholds the `BcryptLanesFn` contract — AVX2 is
    // available on this CPU and the three slices are exactly 8 lanes each —
    // which is everything `bcrypt_lanes_impl` requires.
    unsafe { bcrypt_lanes_impl(cost, key_words, salt_words, outs) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::base64;
    use crate::eks::{Backend, expand_key_words, salt_words};

    /// Reduce a password to kernel key words exactly as `crate::core` will
    /// (mirrors the scalar tests' helper, plus the 72-byte cap
    /// `padded_key` applies: the stream cycles at `min(len + 1, 72)`).
    fn key_words_for(password: &[u8]) -> [u32; 18] {
        let mut key = [0u8; 72];
        let copied = password.len().min(72);
        key[..copied].copy_from_slice(&password[..copied]);
        expand_key_words(&key, (copied + 1).min(72))
    }

    /// Gate every kernel-driving test on real AVX2 availability: unlike
    /// NEON on aarch64, AVX2 is not in the x86-64 baseline, so a plain
    /// `cargo test` on an old x86-64 host must skip, not `SIGILL`. Miri
    /// skips too — `detect()` pins Scalar there by design.
    fn avx2_available() -> bool {
        !cfg!(miri) && Backend::Avx2.is_available()
    }

    /// Deterministic 8-lane key/salt words from an inline SplitMix64 with
    /// a fixed seed (arbitrary u32s are valid key and salt words — they
    /// are pure XOR inputs). Reproduces on rerun.
    fn fixed_words() -> ([[u32; 18]; LANES], [[u32; 4]; LANES]) {
        let mut rng = 0x853C_49E6_748F_EA9Bu64; // splitmix64 constant
        let mut next_u32 = move || {
            rng = rng.wrapping_add(0x9E37_79B9_7F4A_7C15);
            let mut z = rng;
            z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
            z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
            (z ^ (z >> 31)) as u32
        };
        let mut kws = [[0u32; 18]; LANES];
        let mut sws = [[0u32; 4]; LANES];
        for w in kws.as_flattened_mut().iter_mut().chain(sws.as_flattened_mut()) {
            *w = next_u32();
        }
        (kws, sws)
    }

    /// The flavor correctness check: all three flavors must produce
    /// byte-identical output on a fixed input. This is the test that pins
    /// the `vpgatherdd` index math against the scalar-load spellings —
    /// the shootout asserts the same invariant before it times anything.
    #[test]
    fn flavors_agree() {
        if !avx2_available() {
            return;
        }
        let (kws, sws) = fixed_words();
        let (mut gathered, mut inserted, mut extracted) =
            ([[0u8; 24]; LANES], [[0u8; 24]; LANES], [[0u8; 24]; LANES]);
        // SAFETY: availability checked above; the slices are exactly 8 lanes.
        unsafe {
            bcrypt8::<FLAVOR_GATHER>(4, &kws, &sws, &mut gathered);
            bcrypt8::<FLAVOR_INSERT>(4, &kws, &sws, &mut inserted);
            bcrypt8::<FLAVOR_EXTRACT>(4, &kws, &sws, &mut extracted);
        }
        assert_eq!(gathered, inserted, "gather and insert flavors diverged");
        assert_eq!(gathered, extracted, "gather and extract flavors diverged");
    }

    /// Eight different passwords and eight different salts, one per lane,
    /// must reproduce eight scalar hashes bit for bit — including the
    /// OpenBSD `U*U` vector in lane 1.
    #[test]
    fn lanes_match_scalar() {
        if !avx2_available() {
            return;
        }
        let vector_salt =
            base64::decode_16(b"CCCCCCCCCCCCCCCCCCCCC.").expect("test salt must decode");
        let passwords: [&[u8]; 8] = [
            b"",
            b"U*U",
            b"hunter2",
            &[0xAA; 72],
            b"correct horse",
            &[0x01],
            &[0x55; 71],
            &[0xFF; 100],
        ];
        let kws: [[u32; 18]; 8] = core::array::from_fn(|i| key_words_for(passwords[i]));
        let sws: [[u32; 4]; 8] = [
            salt_words(&[0x11; 16]),
            salt_words(&vector_salt),
            salt_words(&[0x33; 16]),
            salt_words(&[0x44; 16]),
            salt_words(&[0x55; 16]),
            salt_words(&[0x66; 16]),
            salt_words(&[0x77; 16]),
            salt_words(&[0x88; 16]),
        ];
        let mut outs = [[0u8; 24]; 8];
        // SAFETY: availability checked above; the slices are exactly 8 lanes.
        unsafe { bcrypt_lanes(5, &kws, &sws, &mut outs) };
        for lane in 0..LANES {
            let mut expected = [0u8; 24];
            crate::eks::scalar::bcrypt_lanes(
                5,
                &kws[lane..lane + 1],
                &sws[lane..lane + 1],
                core::slice::from_mut(&mut expected),
            );
            assert_eq!(outs[lane], expected, "lane {lane} diverged from scalar");
        }
    }

    /// The flavor pick runs (the shootout, in release+std builds) and
    /// caches a valid value; repeated reads agree.
    #[test]
    fn flavor_pick_is_cached() {
        if !avx2_available() {
            return;
        }
        let first = flavor();
        assert_ne!(CACHED_FLAVOR.load(Ordering::Relaxed), FLAVOR_UNINIT);
        assert_eq!(flavor_to_u8(first), CACHED_FLAVOR.load(Ordering::Relaxed));
    }
}