tape-sha256 0.1.0

Pure-Rust, SIMD-accelerated multi-buffer SHA-256 for hashing many independent messages at once
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
//! Multi-buffer SHA-256: hash many independent messages at once
//!
//! One hash cannot be vectorised, since each of the 64 rounds depends on the
//! last. Independent messages can, though: run them in lockstep, one per SIMD
//! lane, and every round becomes an elementwise vector operation.
//!
//! Worth reaching for when you have many messages and need none of them early.
//! The canonical case is a Merkle tree, where the whole bottom level can be
//! hashed in one pass.
//!
//! ```
//! use tape_sha256::hash_many;
//!
//! let msgs: Vec<&[u8]> = vec![b"one", b"two", b"three"];
//! let mut out = vec![[0u8; 32]; msgs.len()];
//! hash_many(&msgs, &mut out);
//! ```
//!
//! When every leaf shares a domain-separation prefix, hash_many_prefixed
//! avoids materialising `prefix || body` per message:
//!
//! ```
//! use tape_sha256::hash_many_prefixed;
//!
//! const LEAF: &[u8] = b"\x00SOLANA_MERKLE_SHREDS_LEAF";
//! let leaves: Vec<&[u8]> = vec![&[1u8; 64], &[2u8; 64]];
//! let mut out = vec![[0u8; 32]; leaves.len()];
//! hash_many_prefixed(LEAF, &leaves, &mut out);
//! ```
//!
//! # Correctness
//!
//! Output is bit-identical to any conforming SHA-256. Every backend is gated
//! against the independent `sha2` crate over lengths covering all block and
//! padding edge cases, and each SIMD backend is cross-checked against the
//! portable one.
//!
//! # Backend selection
//!
//! By default the best kernel for the running CPU is picked and falls back
//! gracefully. The `scalar`, `avx2`, `avx512`, and `neon` features pin one
//! kernel at build time instead, skipping detection; a pinned kernel the CPU
//! lacks will fault, so pin only what the whole fleet supports.
//!
//! # Using both threads of a core
//!
//! These entry points are stateless, so two threads may call them concurrently
//! on disjoint halves of a batch. Doing that on the two SMT siblings of one
//! physical core measured 1.5x the single-thread rate on Zen 5 (9.83 vs 14.84
//! us for 64 Merkle leaves), because a single thread at ~2 instructions per
//! cycle is limited by its own dependency chains rather than by issue width,
//! and the sibling fills the gaps.
//!
//! This crate never spawns or pins a thread. Thread topology is the caller's
//! policy, not a hashing library's. Split the batch, hand each half to a thread
//! you have pinned, and join:
//!
//! ```no_run
//! use tape_sha256::hash_many;
//!
//! # let msgs: Vec<&[u8]> = vec![];
//! # let mut out = vec![[0u8; 32]; 0];
//! let (m0, m1) = msgs.split_at(msgs.len() / 2);
//! let (o0, o1) = out.split_at_mut(msgs.len() / 2);
//! std::thread::scope(|s| {
//!     s.spawn(|| hash_many(m0, o0));
//!     s.spawn(|| hash_many(m1, o1));
//! });
//! ```
//!
//! How much this is worth depends heavily on the microarchitecture.
//!
//! Three further caveats decide whether this is worth anything:
//!
//! The two threads must be siblings of the *same* physical core. Pinned to
//! different cores this is ordinary multicore parallelism, which spends a core
//! to get it; the 1.5x claim is specifically about using a sibling that would
//! otherwise idle. Sibling pairs are listed in
//! `/sys/devices/system/cpu/cpu<N>/topology/thread_siblings_list`. Getting the
//! pairing wrong degrades silently, so it is worth asserting.
//!
//! The sibling has to actually be idle. Under a fully loaded process there is
//! no spare thread to claim and the gain goes away. Use an equal split of the
//! same kernel: an uneven or mixed-kernel split leaves a straggler and measured
//! worse than a single thread.

#[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
mod avx2;
#[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
mod avx512;
#[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
mod avx512x2;
mod batch;
mod core;
mod lanes;
#[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
mod neon;
#[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
mod neon_sha2;
#[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
mod shani;
#[cfg(all(
    target_arch = "wasm32",
    target_feature = "simd128",
    not(feature = "scalar")
))]
mod simd128;

pub use batch::Message;

use {
    batch::{drive, drive_pairs, drive_slices},
    lanes::Scalar,
};

/// Number of messages the active backend hashes per pass
///
/// Batches at least this large amortise the transpose fully; smaller ones
/// still work but leave lanes idle.
pub fn lane_width() -> usize {
    dispatch::lane_width()
}

/// Name of the active backend, for logging and benchmarks
pub fn backend() -> &'static str {
    dispatch::backend()
}

/// Hashes every message in `msgs`, writing one digest per message to `out`
///
/// # Panics
///
/// Panics if `msgs.len() != out.len()`.
pub fn hash_many(msgs: &[&[u8]], out: &mut [[u8; 32]]) {
    dispatch::hash_slices(&[], msgs, out);
}

/// Hashes `prefix || body` for each body in `bodies`
///
/// Equivalent to concatenating and calling hash_many, but without building the
/// concatenation.
///
/// # Panics
///
/// Panics if `bodies.len() != out.len()`.
pub fn hash_many_prefixed(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
    dispatch::hash_slices(prefix, bodies, out);
}

/// Hashes `prefix || left || right` for each pair, without joining them first
///
/// For Merkle interior nodes, where a parent hashes a domain prefix and two
/// child hashes held in separate buffers. Saves the caller a staging buffer
/// and the join.
///
/// # Panics
///
/// Panics unless `left.len() == right.len() == out.len()`.
pub fn hash_pairs(prefix: &[u8], left: &[&[u8]], right: &[&[u8]], out: &mut [[u8; 32]]) {
    dispatch::hash_pairs(prefix, left, right, out);
}

/// Hashes pre-built messages, for callers that want per-message prefixes
///
/// # Panics
///
/// Panics if `msgs.len() != out.len()`.
pub fn hash_messages(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
    dispatch::hash(msgs, out);
}

/// Backends exposed for differential testing and benchmarking
///
/// Not stable API; callers should use hash_many and let dispatch pick. Each
/// dispatchable backend has two entry points: one taking pre-built messages,
/// and a `_slices` form that builds them on the stack so the wrappers need no
/// allocation. Both `serial` and `portable8` are dispatchable — which one the
/// no-SIMD fallback picks is per-architecture, see `dispatch::PORTABLE`.
#[doc(hidden)]
pub mod backends {
    use super::*;

    // Concrete scalar group functions; see GroupFn for why kernels are
    // instantiated once here and called through pointers.
    pub(crate) fn scalar1(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        batch::hash_lanes::<Scalar<1>>(msgs, out)
    }
    pub(crate) fn scalar8(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        batch::hash_lanes::<Scalar<8>>(msgs, out)
    }
    /// Portable 8-lane kernel
    ///
    /// The reference every SIMD backend is checked against, and the no-SIMD
    /// fallback on register-rich targets.
    pub fn portable8(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        unsafe { drive(8, scalar8, msgs, out) }
    }

    pub fn portable8_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        unsafe { drive_slices(8, scalar8, prefix, bodies, out) }
    }

    /// Single-lane kernel: ordinary serial SHA-256 through the same core
    ///
    /// Shows the lane machinery is not what makes output correct, and is the
    /// baseline speedups are quoted against.
    pub fn serial(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        unsafe { drive(1, scalar1, msgs, out) }
    }

    pub fn serial_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        unsafe { drive_slices(1, scalar1, prefix, bodies, out) }
    }

    /// 4-lane AArch64 NEON kernel
    ///
    /// NEON is baseline on AArch64, so this is always safe to call there.
    #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
    pub fn neon4(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        unsafe { drive(4, crate::neon::group, msgs, out) }
    }

    #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
    pub fn neon4_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        unsafe { drive_slices(4, crate::neon::group, prefix, bodies, out) }
    }

    /// 4-lane wasm simd128 kernel
    ///
    /// Exists only in builds with `-C target-feature=+simd128`; an engine
    /// that lacks simd128 rejects the module at instantiation, so a call
    /// that runs at all is safe.
    #[cfg(all(
        target_arch = "wasm32",
        target_feature = "simd128",
        not(feature = "scalar")
    ))]
    pub fn simd128_4(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        unsafe { drive(4, crate::simd128::group, msgs, out) }
    }

    #[cfg(all(
        target_arch = "wasm32",
        target_feature = "simd128",
        not(feature = "scalar")
    ))]
    pub fn simd128_4_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        unsafe { drive_slices(4, crate::simd128::group, prefix, bodies, out) }
    }

    /// Two-wave 8-lane wasm simd128 kernel, for benchmarking against the
    /// single wave; never dispatched to
    #[cfg(all(
        target_arch = "wasm32",
        target_feature = "simd128",
        not(feature = "scalar")
    ))]
    pub fn simd128_8_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        unsafe { drive_slices(8, crate::simd128::group8, prefix, bodies, out) }
    }

    /// Four-wave 16-lane wasm simd128 kernel; never dispatched to
    #[cfg(all(
        target_arch = "wasm32",
        target_feature = "simd128",
        not(feature = "scalar")
    ))]
    pub fn simd128_16_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        unsafe { drive_slices(16, crate::simd128::group16, prefix, bodies, out) }
    }

    /// Multi-stream kernel on the ARMv8 SHA-256 crypto extension
    ///
    /// # Safety
    ///
    /// The running CPU must support the `sha2` extension.
    #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
    pub unsafe fn neon_sha2x4(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        drive(
            crate::neon_sha2::STREAMS,
            crate::neon_sha2::group,
            msgs,
            out,
        )
    }

    /// # Safety
    ///
    /// The running CPU must support the `sha2` extension.
    #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
    pub unsafe fn neon_sha2x4_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        drive_slices(
            crate::neon_sha2::STREAMS,
            crate::neon_sha2::group,
            prefix,
            bodies,
            out,
        )
    }

    /// Multi-stream kernel on the x86 SHA-NI extension
    ///
    /// # Safety
    ///
    /// The running CPU must support SHA-NI, SSSE3, and SSE4.1.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn shani_x4(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        drive(crate::shani::STREAMS, crate::shani::group, msgs, out)
    }

    /// # Safety
    ///
    /// The running CPU must support SHA-NI, SSSE3, and SSE4.1.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn shani_x4_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        drive_slices(
            crate::shani::STREAMS,
            crate::shani::group,
            prefix,
            bodies,
            out,
        )
    }

    /// 8-lane AVX2 kernel
    ///
    /// # Safety
    ///
    /// The running CPU must support AVX2.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn avx2_8(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        drive(8, crate::avx2::group, msgs, out)
    }

    /// # Safety
    ///
    /// The running CPU must support AVX2.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn avx2_8_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        drive_slices(8, crate::avx2::group, prefix, bodies, out)
    }

    /// 16-lane AVX-512 kernel
    ///
    /// # Safety
    ///
    /// The running CPU must support AVX-512F and AVX-512BW.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn avx512_16(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        drive(16, crate::avx512::group, msgs, out)
    }

    /// # Safety
    ///
    /// The running CPU must support AVX-512F and AVX-512BW.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn avx512_16_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        drive_slices(16, crate::avx512::group, prefix, bodies, out)
    }

    /// 32-message AVX-512 interlace: two 16-lane compressions with their
    /// rounds interlaced in one thread
    ///
    /// # Safety
    ///
    /// The running CPU must support AVX-512F and AVX-512BW.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn avx512_16x2(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        drive(crate::avx512x2::WIDTH, crate::avx512x2::group2, msgs, out)
    }

    /// # Safety
    ///
    /// The running CPU must support AVX-512F and AVX-512BW.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    pub unsafe fn avx512_16x2_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        drive_slices(
            crate::avx512x2::WIDTH,
            crate::avx512x2::group2,
            prefix,
            bodies,
            out,
        )
    }
}

mod dispatch {
    use super::*;

    /// The kernel to fall back on where no SIMD exists
    ///
    /// Multi-buffer costs a real general-purpose register per lane in scalar
    /// code rather than riding along free in a vector lane, so the width that
    /// wins is set by the register file. x86-64 has 16 GPRs and cannot absorb
    /// even two lanes: eight measured 1.9x slower than one on Zen 4 (418.78 vs
    /// 218.30 us). aarch64 has 31, and eight lanes measure 13% faster than one
    /// (93.7 vs 106.5).
    ///
    /// One lane is the default because it is the safe side of that trade: it
    /// gives up ~13% where the registers exist, while the wide kernel loses
    /// 1.9x where they do not, and most targets are nearer x86-64 than aarch64
    /// (32-bit x86 has 8 GPRs; wasm is JIT-ed onto whatever the host has).
    /// aarch64 is listed because it was measured, not because it is 64-bit.
    ///
    /// Unreachable on an aarch64 build without `scalar`, since NEON is
    /// baseline there and nothing falls through to it.
    #[allow(dead_code)]
    const PORTABLE: Kernel = if cfg!(target_arch = "aarch64") {
        Kernel::Portable8
    } else {
        Kernel::Portable1
    };

    /// Which kernel this build and CPU resolve to
    ///
    /// Entry points, reported name, and lane width all read from a single
    /// select, so they cannot drift. An earlier version kept two detection
    /// ladders and derived the width by string-matching the backend name,
    /// where a rename would silently have changed the reported width.
    // Which variants exist depends on target and features; the rest are only
    // match arms, which dead_code would otherwise flag.
    #[allow(dead_code)]
    #[derive(Clone, Copy, PartialEq, Eq)]
    pub(crate) enum Kernel {
        Portable1,
        Portable8,
        #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
        Neon4,
        #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
        NeonSha2x4,
        #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
        ShaNiX4,
        #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
        Avx2_8,
        #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
        Avx512_16,
        #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
        Avx512_16x2,
        #[cfg(all(
            target_arch = "wasm32",
            target_feature = "simd128",
            not(feature = "scalar")
        ))]
        Simd128_4,
    }

    impl Kernel {
        pub(crate) fn name(self) -> &'static str {
            match self {
                Kernel::Portable1 => "portable-1",
                Kernel::Portable8 => "portable-8",
                #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
                Kernel::Neon4 => "neon-4",
                #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
                Kernel::NeonSha2x4 => "neon-sha2-x4",
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::ShaNiX4 => "shani-x4",
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::Avx2_8 => "avx2-8",
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::Avx512_16 => "avx512-16",
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::Avx512_16x2 => "avx512-2x16",
                #[cfg(all(
                    target_arch = "wasm32",
                    target_feature = "simd128",
                    not(feature = "scalar")
                ))]
                Kernel::Simd128_4 => "simd128-4",
            }
        }

        pub(crate) fn width(self) -> usize {
            match self {
                Kernel::Portable1 => 1,
                Kernel::Portable8 => 8,
                #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
                Kernel::Neon4 => 4,
                #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
                Kernel::NeonSha2x4 => crate::neon_sha2::STREAMS,
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::ShaNiX4 => crate::shani::STREAMS,
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::Avx2_8 => 8,
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::Avx512_16 => 16,
                #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
                Kernel::Avx512_16x2 => crate::avx512x2::WIDTH,
                #[cfg(all(
                    target_arch = "wasm32",
                    target_feature = "simd128",
                    not(feature = "scalar")
                ))]
                Kernel::Simd128_4 => 4,
            }
        }
    }

    /// Effective AMD CPU family, or 0 on anything else
    ///
    /// 0x19 is Zen 3 / Zen 4, 0x1a is Zen 5; the AVX-512 dispatch below is
    /// per-family because the kernels rank differently on each. Cached:
    /// `select` runs on every public call, and two `cpuid`s per call would
    /// be measurable against a single small message.
    #[cfg(all(
        target_arch = "x86_64",
        not(any(feature = "scalar", feature = "avx2", feature = "avx512"))
    ))]
    fn amd_family() -> u32 {
        use std::sync::OnceLock;
        static FAMILY: OnceLock<u32> = OnceLock::new();
        *FAMILY.get_or_init(|| {
            use std::arch::x86_64::__cpuid;
            // SAFETY: leaves 0 and 1 exist on every x86_64 CPU. The blocks
            // are redundant from 1.95, where `__cpuid` became safe, but the
            // MSRV toolchain still requires them.
            #[allow(unused_unsafe)]
            let v = unsafe { __cpuid(0) };
            // "AuthenticAMD" in the ebx/edx/ecx registers.
            if (v.ebx, v.edx, v.ecx) != (0x6874_7541, 0x6974_6e65, 0x444d_4163) {
                return 0;
            }
            #[allow(unused_unsafe)]
            let eax = unsafe { __cpuid(1) }.eax;
            // AMD reports base family 0xf and puts the rest in the extended
            // field, so the effective family is their sum.
            ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff)
        })
    }

    /// Selects the best kernel for this target and CPU
    #[inline]
    #[allow(clippy::needless_return)]
    pub(crate) fn select() -> Kernel {
        #[cfg(feature = "scalar")]
        return PORTABLE;

        #[cfg(all(target_arch = "x86_64", feature = "avx512", not(feature = "scalar")))]
        return Kernel::Avx512_16;
        #[cfg(all(target_arch = "x86_64", feature = "avx2", not(feature = "scalar")))]
        return Kernel::Avx2_8;
        #[cfg(all(target_arch = "aarch64", feature = "neon", not(feature = "scalar")))]
        return Kernel::Neon4;

        #[cfg(not(any(
            feature = "scalar",
            all(target_arch = "x86_64", any(feature = "avx2", feature = "avx512")),
            all(target_arch = "aarch64", feature = "neon"),
        )))]
        {
            #[cfg(target_arch = "x86_64")]
            {
                use std::sync::OnceLock;
                // Each feature probe is itself a cached atomic load, but the
                // ladder runs on every public call and stacks half a dozen
                // of them; resolve the kernel once instead.
                static KERNEL: OnceLock<Kernel> = OnceLock::new();
                return *KERNEL.get_or_init(|| {
                    let sha = have_shani();
                    if is_x86_feature_detected!("avx512f") && is_x86_feature_detected!("avx512bw") {
                        // AVX-512 capability alone does not decide; the family
                        // does, and each arm here is a measurement. Zen 5 runs
                        // the 2x16 interlace (12.7 vs 14.6 us single-wave on
                        // EPYC 9B45): its native 512-bit datapath leaves
                        // dependency stalls the second wave fills.
                        //
                        // Zen 4 double-pumps 512-bit ops, so the interlace measured
                        // exactly neutral there (23.69 vs 23.72 on EPYC 9B14)
                        // and the SHA-NI streams beat every 16-lane kernel
                        // instead (21.05).
                        //
                        // Intel keeps the single wave for a different reason:
                        // the interlace wins the cycles (998 vs 1053 per block
                        // step on Emerald Rapids, 1004 vs 1048 on Granite Rapids)
                        // and loses the clock, since doubling 512-bit density
                        // costs 7 to 11% of frequency.
                        let fam = amd_family();
                        if fam == 0x1a {
                            return Kernel::Avx512_16x2;
                        }
                        if !(sha && fam == 0x19) {
                            return Kernel::Avx512_16;
                        }
                    }
                    // Without AVX-512 (or on Zen 4), the dedicated SHA unit beats
                    // 8-lane integer multi-buffer by a wide margin.
                    // Measured on Zen 3, AVX2 loses even to serial SHA-NI.
                    if sha {
                        return Kernel::ShaNiX4;
                    }
                    if is_x86_feature_detected!("avx2") {
                        return Kernel::Avx2_8;
                    }
                    PORTABLE
                });
            }
            #[cfg(target_arch = "aarch64")]
            {
                // The dedicated SHA-256 unit beats integer multi-buffer, so
                // prefer it and fall back to NEON lanes.
                if std::arch::is_aarch64_feature_detected!("sha2") {
                    return Kernel::NeonSha2x4;
                }
                Kernel::Neon4
            }
            #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
            {
                // Wasm has no runtime probing; simd128 is a build-time fact.
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                {
                    Kernel::Simd128_4
                }
                #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
                {
                    PORTABLE
                }
            }
        }
    }

    pub(crate) fn hash(msgs: &[Message<'_>], out: &mut [[u8; 32]]) {
        match select() {
            Kernel::Portable1 => backends::serial(msgs, out),
            Kernel::Portable8 => backends::portable8(msgs, out),
            #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
            Kernel::Neon4 => backends::neon4(msgs, out),
            #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
            Kernel::NeonSha2x4 => unsafe { backends::neon_sha2x4(msgs, out) },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            Kernel::ShaNiX4 => unsafe { backends::shani_x4(msgs, out) },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            Kernel::Avx2_8 => unsafe { backends::avx2_8(msgs, out) },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            k @ (Kernel::Avx512_16 | Kernel::Avx512_16x2) => unsafe {
                let (w, group) = avx512_parts(k);
                let cut = avx512_tail_cut(msgs.len());
                let (head, tail) = msgs.split_at(cut);
                let (out_head, out_tail) = out.split_at_mut(cut);
                drive(w, group, head, out_head);
                if !tail.is_empty() {
                    backends::shani_x4(tail, out_tail);
                }
            },
            #[cfg(all(
                target_arch = "wasm32",
                target_feature = "simd128",
                not(feature = "scalar")
            ))]
            Kernel::Simd128_4 => backends::simd128_4(msgs, out),
        }
    }

    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    #[inline]
    fn have_shani() -> bool {
        is_x86_feature_detected!("sha")
            && is_x86_feature_detected!("ssse3")
            && is_x86_feature_detected!("sse4.1")
    }

    /// Where to split an avx512 batch so its remainder runs on SHA-NI
    ///
    /// A remainder under 13 messages costs a flat 16-lane group mostly
    /// running empty lanes, while SHA-NI streams pay only for the messages
    /// present; at 13 or more the flat group is cheaper again. Routed when
    /// the CPU has SHA-NI. Returns the batch length when no split should
    /// happen.
    ///
    /// The cut is at wave (16-lane) granularity for both AVX-512 kernels:
    /// the interlace processes 32 per chunk but degrades internally to
    /// 16-lane groups for a partial chunk, so its routable straggler is
    /// also `len % 16` -- cutting at `len % 32` would strand a 17..=28
    /// remainder as 16 plus the exact mostly-empty group this exists to
    /// avoid.
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    #[inline]
    fn avx512_tail_cut(len: usize) -> usize {
        let rem = len % 16;
        if (1..=12).contains(&rem) && have_shani() {
            len - rem
        } else {
            len
        }
    }

    /// Chunk width and group function for an AVX-512 kernel choice
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
    fn avx512_parts(k: Kernel) -> (usize, batch::GroupFn) {
        match k {
            Kernel::Avx512_16x2 => (
                crate::avx512x2::WIDTH,
                crate::avx512x2::group2 as batch::GroupFn,
            ),
            _ => (16, crate::avx512::group as batch::GroupFn),
        }
    }

    pub(crate) fn hash_slices(prefix: &[u8], bodies: &[&[u8]], out: &mut [[u8; 32]]) {
        match select() {
            Kernel::Portable1 => backends::serial_slices(prefix, bodies, out),
            Kernel::Portable8 => backends::portable8_slices(prefix, bodies, out),
            #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
            Kernel::Neon4 => backends::neon4_slices(prefix, bodies, out),
            #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
            Kernel::NeonSha2x4 => unsafe { backends::neon_sha2x4_slices(prefix, bodies, out) },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            Kernel::ShaNiX4 => unsafe { backends::shani_x4_slices(prefix, bodies, out) },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            Kernel::Avx2_8 => unsafe { backends::avx2_8_slices(prefix, bodies, out) },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            k @ (Kernel::Avx512_16 | Kernel::Avx512_16x2) => unsafe {
                let (w, group) = avx512_parts(k);
                let cut = avx512_tail_cut(bodies.len());
                let (head, tail) = bodies.split_at(cut);
                let (out_head, out_tail) = out.split_at_mut(cut);
                drive_slices(w, group, prefix, head, out_head);
                if !tail.is_empty() {
                    backends::shani_x4_slices(prefix, tail, out_tail);
                }
            },
            #[cfg(all(
                target_arch = "wasm32",
                target_feature = "simd128",
                not(feature = "scalar")
            ))]
            Kernel::Simd128_4 => backends::simd128_4_slices(prefix, bodies, out),
        }
    }

    pub(crate) fn hash_pairs(prefix: &[u8], left: &[&[u8]], right: &[&[u8]], out: &mut [[u8; 32]]) {
        let k = select();
        let width = k.width();
        match k {
            Kernel::Portable1 => unsafe {
                drive_pairs(width, backends::scalar1, prefix, left, right, out)
            },
            Kernel::Portable8 => unsafe {
                drive_pairs(width, backends::scalar8, prefix, left, right, out)
            },
            #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
            Kernel::Neon4 => unsafe {
                drive_pairs(width, crate::neon::group, prefix, left, right, out)
            },
            #[cfg(all(target_arch = "aarch64", not(feature = "scalar")))]
            Kernel::NeonSha2x4 => unsafe {
                drive_pairs(width, crate::neon_sha2::group, prefix, left, right, out)
            },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            Kernel::ShaNiX4 => unsafe {
                drive_pairs(width, crate::shani::group, prefix, left, right, out)
            },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            Kernel::Avx2_8 => unsafe {
                drive_pairs(width, crate::avx2::group, prefix, left, right, out)
            },
            #[cfg(all(target_arch = "x86_64", not(feature = "scalar")))]
            k @ (Kernel::Avx512_16 | Kernel::Avx512_16x2) => unsafe {
                let (w, group) = avx512_parts(k);
                let cut = avx512_tail_cut(left.len());
                drive_pairs(
                    w,
                    group,
                    prefix,
                    &left[..cut],
                    &right[..cut],
                    &mut out[..cut],
                );
                if cut < left.len() {
                    drive_pairs(
                        crate::shani::STREAMS,
                        crate::shani::group,
                        prefix,
                        &left[cut..],
                        &right[cut..],
                        &mut out[cut..],
                    );
                }
            },
            #[cfg(all(
                target_arch = "wasm32",
                target_feature = "simd128",
                not(feature = "scalar")
            ))]
            Kernel::Simd128_4 => unsafe {
                drive_pairs(width, crate::simd128::group, prefix, left, right, out)
            },
        }
    }

    pub(crate) fn lane_width() -> usize {
        select().width()
    }

    pub(crate) fn backend() -> &'static str {
        select().name()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_message() {
        let mut out = [[0u8; 32]; 1];
        hash_many(&[b""], &mut out);
        // FIPS 180-4 known answer for the empty string.
        let expect = hex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
        assert_eq!(out[0], expect);
    }

    #[test]
    fn abc() {
        let mut out = [[0u8; 32]; 1];
        hash_many(&[b"abc"], &mut out);
        let expect = hex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
        assert_eq!(out[0], expect);
    }

    #[test]
    fn prefix_matches_concatenation() {
        let prefix = b"\x00SOLANA_MERKLE_SHREDS_LEAF";
        let bodies: Vec<Vec<u8>> = (0..8usize).map(|i| vec![i as u8; 100 + i * 37]).collect();
        let body_refs: Vec<&[u8]> = bodies.iter().map(|b| b.as_slice()).collect();

        let mut via_prefix = vec![[0u8; 32]; bodies.len()];
        hash_many_prefixed(prefix, &body_refs, &mut via_prefix);

        let joined: Vec<Vec<u8>> = bodies
            .iter()
            .map(|b| [prefix.as_slice(), b].concat())
            .collect();
        let joined_refs: Vec<&[u8]> = joined.iter().map(|b| b.as_slice()).collect();
        let mut via_concat = vec![[0u8; 32]; bodies.len()];
        hash_many(&joined_refs, &mut via_concat);

        assert_eq!(via_prefix, via_concat);
    }

    fn hex(s: &str) -> [u8; 32] {
        let mut out = [0u8; 32];
        for (i, b) in out.iter_mut().enumerate() {
            *b = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).unwrap();
        }
        out
    }
}