md5-many 0.1.0-alpha.4

High-throughput MD5 with portable SIMD and multi-buffer backends
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
#![no_std]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]
//! High-throughput MD5 with portable multi-buffer SIMD.
//!
//! `md5-many` keeps the standard single-stream MD5 API separate from its
//! SIMD batch engine. A single MD5 stream has a dependency from one 64-byte
//! block to the next, while independent messages can occupy independent SIMD
//! lanes. On x86-64 the batch scheduler combines 8-way AVX2 or 16-way AVX-512
//! native kernels with interleaved dual- and triple-batch kernels to expose
//! additional instruction-level parallelism.
//!
//! MD5 is cryptographically broken and must not be used for signatures,
//! password hashing, or adversarial integrity. This crate targets legacy
//! interoperability and non-adversarial checksumming workloads.

#[cfg(feature = "std")]
extern crate std;

mod consts;
mod incremental;
mod scalar;
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
mod scalar_aarch64;
#[cfg(target_arch = "x86_64")]
mod scalar_x86_64;
#[cfg(target_arch = "x86_64")]
mod scalar_x86_64_avx512;
#[cfg(target_arch = "x86_64")]
mod scalar_x86_64_dual;
mod simd;

#[cfg(feature = "digest")]
/// RustCrypto block-level compatibility API.
pub mod block_api;

#[cfg(feature = "digest")]
pub use digest::{self, Digest};

#[cfg(feature = "digest")]
digest::buffer_fixed!(
    /// Streaming MD5 hasher compatible with RustCrypto's `digest` traits.
    pub struct Md5(block_api::Md5Core);
    impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
);

/// A raw 128-bit MD5 digest.
pub type Md5Digest = [u8; 16];

pub use incremental::Md5State;

/// Compute the MD5 digest of a single byte slice.
///
/// On x86-64 this uses the optimized scalar compressor and may select an
/// XMM-width AVX-512VL single-stream backend on supported Intel CPUs.
/// Little-endian AArch64 uses a hand-scheduled integer compressor; other
/// targets use the portable Rust compressor. For independent-message SIMD
/// batching, use [`Md5Many`] or [`md5_many`].
#[must_use]
pub fn md5(input: &[u8]) -> Md5Digest {
    scalar::hash(input)
}

/// Runtime-dispatched batch MD5 engine.
///
/// Construct this once and reuse it so CPU feature detection is not repeated.
#[derive(Clone, Copy, Debug)]
pub struct Md5Many {
    level: fearless_simd::Level,
}

impl Md5Many {
    /// Detect the best SIMD level available on the current process.
    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[must_use]
    pub fn new() -> Self {
        Self::from_level(fearless_simd::Level::new())
    }

    /// Construct a batch engine from an already-detected Fearless SIMD level.
    #[must_use]
    pub const fn from_level(level: fearless_simd::Level) -> Self {
        Self { level }
    }

    /// Return the native number of `u32` SIMD lanes used by this engine.
    ///
    /// AVX-512 is 16 lanes, AVX2 is 8 lanes, and SSE4.2/NEON/WASM use 4 lanes.
    #[must_use]
    pub const fn lanes(self) -> usize {
        simd::lanes_with_level(self.level)
    }

    /// Hash many independent messages into `outputs`.
    ///
    /// Equal-length groups use specialized whole-stream SIMD kernels. Mixed
    /// groups process their common full-block prefix at SIMD speed and only
    /// diverge for the tail; skewed groups may be repartitioned to avoid
    /// wasting lanes on already-finished messages.
    ///
    /// # Panics
    ///
    /// Panics if `outputs` is shorter than `inputs`.
    pub fn hash_many(self, inputs: &[&[u8]], outputs: &mut [Md5Digest]) {
        simd::hash_many_with_level(self.level, inputs, outputs);
    }

    /// Increment several independent MD5 streams in parallel.
    ///
    /// Each `inputs[i]` is appended to `states[i]`. Complete 64-byte blocks
    /// are compacted into SIMD lanes even when chunk lengths differ, while
    /// partial blocks remain buffered in the corresponding [`Md5State`].
    ///
    /// # Panics
    ///
    /// Panics if `states` and `inputs` have different lengths.
    pub fn update_many(self, states: &mut [Md5State], inputs: &[&[u8]]) {
        incremental::update_many_with_level(self.level, states, inputs);
    }

    /// Finalize several incremental MD5 streams in parallel.
    ///
    /// Finalization is non-destructive: the states are not modified and may
    /// receive more data afterwards. Padding blocks are batched through the
    /// multi-buffer compressor where profitable.
    ///
    /// # Panics
    ///
    /// Panics if `outputs` is shorter than `states`.
    pub fn finalize_many(self, states: &[Md5State], outputs: &mut [Md5Digest]) {
        incremental::finalize_many_with_level(self.level, states, outputs);
    }
}

#[cfg(any(feature = "std", target_arch = "wasm32"))]
impl Default for Md5Many {
    fn default() -> Self {
        Self::new()
    }
}

/// Hash many independent messages using runtime SIMD detection.
///
/// For repeated calls, prefer constructing [`Md5Many`] once and reusing it.
///
/// # Panics
///
/// Panics if `outputs` is shorter than `inputs`.
#[cfg(any(feature = "std", target_arch = "wasm32"))]
pub fn md5_many(inputs: &[&[u8]], outputs: &mut [Md5Digest]) {
    Md5Many::new().hash_many(inputs, outputs);
}

#[cfg(test)]
mod tests {
    use super::*;
    use md5::Md5 as ReferenceMd5;

    fn reference(input: &[u8]) -> Md5Digest {
        let out = <ReferenceMd5 as md5::Digest>::digest(input);
        out.as_slice().try_into().expect("MD5 is 16 bytes")
    }

    #[test]
    fn rfc_vectors() {
        let vectors: &[(&[u8], [u8; 16])] = &[
            (b"", hex_literal::hex!("d41d8cd98f00b204e9800998ecf8427e")),
            (b"a", hex_literal::hex!("0cc175b9c0f1b6a831c399e269772661")),
            (
                b"abc",
                hex_literal::hex!("900150983cd24fb0d6963f7d28e17f72"),
            ),
            (
                b"message digest",
                hex_literal::hex!("f96b697d7cb7938d525a2f31aaf161d0"),
            ),
            (
                b"abcdefghijklmnopqrstuvwxyz",
                hex_literal::hex!("c3fcd3d76192e4007dfb496cca67e13b"),
            ),
        ];
        for (input, expected) in vectors {
            assert_eq!(md5(input), *expected);
        }
    }

    #[test]
    fn boundary_lengths_match_reference() {
        let mut data = [0u8; 257];
        for (index, byte) in data.iter_mut().enumerate() {
            *byte = index as u8;
        }
        for len in 0..=data.len() {
            assert_eq!(md5(&data[..len]), reference(&data[..len]), "len={len}");
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn many_equal_length_matches_reference() {
        let messages: [[u8; 192]; 13] = core::array::from_fn(|message| {
            core::array::from_fn(|byte| (message * 31 + byte * 17) as u8)
        });
        let inputs: [&[u8]; 13] = core::array::from_fn(|i| messages[i].as_slice());
        let mut outputs = [[0u8; 16]; 13];

        let engine = Md5Many::new();
        engine.hash_many(&inputs, &mut outputs);
        for (input, output) in inputs.iter().zip(outputs) {
            assert_eq!(output, reference(input));
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn many_equal_length_lane_counts_match_reference() {
        let storage: [[u8; 193]; 16] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 37 + i * 13) as u8));
        let engine = Md5Many::new();

        for active in 2..=16 {
            let inputs = &storage[..active];
            let inputs: std::vec::Vec<&[u8]> = inputs.iter().map(<[u8; 193]>::as_slice).collect();
            let mut outputs = std::vec![[0u8; 16]; active];
            engine.hash_many(&inputs, &mut outputs);
            for lane in 0..active {
                assert_eq!(
                    outputs[lane],
                    reference(inputs[lane]),
                    "active={active}, lane={lane}"
                );
            }
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn avx512_padding_boundaries_match_reference() {
        let engine = Md5Many::new();
        if engine.lanes() < 16 {
            return;
        }

        let storage: [[u8; 129]; 16] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 23 + i * 5) as u8));
        for len in [0, 1, 55, 56, 63, 64, 65, 119, 120, 127, 128, 129] {
            let inputs: [&[u8]; 16] = core::array::from_fn(|lane| &storage[lane][..len]);
            let mut outputs = [[0u8; 16]; 16];
            engine.hash_many(&inputs, &mut outputs);
            for lane in 0..16 {
                assert_eq!(
                    outputs[lane],
                    reference(inputs[lane]),
                    "lane={lane}, len={len}"
                );
            }
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn avx512_small_batch_crossover_matches_reference() {
        let engine = Md5Many::new();
        if engine.lanes() < 16 {
            return;
        }

        let storage: [[u8; 1216]; 8] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 41 + i * 7) as u8));

        for active in [2usize, 4, 8] {
            for len in [512usize, 513, 1024] {
                let inputs: std::vec::Vec<&[u8]> = storage[..active]
                    .iter()
                    .map(|input| &input[..len])
                    .collect();
                let mut outputs = std::vec![[0u8; 16]; active];
                engine.hash_many(&inputs, &mut outputs);
                for lane in 0..active {
                    assert_eq!(
                        outputs[lane],
                        reference(inputs[lane]),
                        "equal active={active}, lane={lane}, len={len}"
                    );
                }
            }

            let inputs: std::vec::Vec<&[u8]> = storage[..active]
                .iter()
                .enumerate()
                .map(|(lane, input)| &input[..512 + lane * 73])
                .collect();
            let mut outputs = std::vec![[0u8; 16]; active];
            engine.hash_many(&inputs, &mut outputs);
            for lane in 0..active {
                assert_eq!(
                    outputs[lane],
                    reference(inputs[lane]),
                    "mixed active={active}, lane={lane}"
                );
            }
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn x86_triple_batches_match_reference() {
        let detected = fearless_simd::Level::new();

        if let Some(avx2) = detected.as_avx2() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx2(avx2));
            for mixed in [false, true] {
                let storage: std::vec::Vec<std::vec::Vec<u8>> = (0..24)
                    .map(|lane| {
                        let len = if mixed { 4096 - lane * 8 } else { 4096 };
                        (0..len).map(|i| (lane * 31 + i * 11) as u8).collect()
                    })
                    .collect();
                let inputs: std::vec::Vec<&[u8]> =
                    storage.iter().map(std::vec::Vec::as_slice).collect();
                let mut outputs = std::vec![[0u8; 16]; inputs.len()];
                engine.hash_many(&inputs, &mut outputs);
                for (lane, (input, output)) in inputs.iter().zip(&outputs).enumerate() {
                    assert_eq!(*output, reference(input), "avx2 mixed={mixed} lane={lane}");
                }
            }
        }

        if let Some(avx512) = detected.as_avx512() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx512(avx512));
            for mixed in [false, true] {
                let storage: std::vec::Vec<std::vec::Vec<u8>> = (0..48)
                    .map(|lane| {
                        let len = if mixed { 4096 - lane * 4 } else { 4096 };
                        (0..len).map(|i| (lane * 17 + i * 13) as u8).collect()
                    })
                    .collect();
                let inputs: std::vec::Vec<&[u8]> =
                    storage.iter().map(std::vec::Vec::as_slice).collect();
                let mut outputs = std::vec![[0u8; 16]; inputs.len()];
                engine.hash_many(&inputs, &mut outputs);
                for (lane, (input, output)) in inputs.iter().zip(&outputs).enumerate() {
                    assert_eq!(
                        *output,
                        reference(input),
                        "avx512 mixed={mixed} lane={lane}"
                    );
                }
            }
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn x86_partial_batch_boundaries_match_reference() {
        fn check_counts(engine: Md5Many, counts: &[usize], base_len: usize) {
            let equal_storage: std::vec::Vec<std::vec::Vec<u8>> = (0..64)
                .map(|lane| (0..base_len).map(|i| (lane * 41 + i * 17) as u8).collect())
                .collect();
            let mixed_storage: std::vec::Vec<std::vec::Vec<u8>> = (0..64)
                .map(|lane| {
                    let len = base_len - (lane % 13) * 7;
                    (0..len).map(|i| (lane * 23 + i * 29) as u8).collect()
                })
                .collect();

            for &count in counts {
                for (kind, storage) in [("equal", &equal_storage), ("mixed", &mixed_storage)] {
                    let inputs: std::vec::Vec<&[u8]> = storage[..count]
                        .iter()
                        .map(std::vec::Vec::as_slice)
                        .collect();
                    let mut outputs = std::vec![[0u8; 16]; count];
                    engine.hash_many(&inputs, &mut outputs);
                    for (lane, (input, output)) in inputs.iter().zip(&outputs).enumerate() {
                        assert_eq!(
                            *output,
                            reference(input),
                            "{kind} count={count} lane={lane}"
                        );
                    }
                }
            }
        }

        let detected = fearless_simd::Level::new();
        if let Some(avx2) = detected.as_avx2() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx2(avx2));
            check_counts(
                engine,
                &[
                    2, 3, 8, 9, 15, 16, 17, 23, 24, 25, 26, 31, 32, 49, 50, 55, 56, 63, 64,
                ],
                257,
            );

            // Exercise both sides of the two-message one-block crossover.
            for len in [55usize, 56] {
                let storage = [std::vec![0x35u8; len], std::vec![0xa7u8; len]];
                let inputs = [storage[0].as_slice(), storage[1].as_slice()];
                let mut outputs = [[0u8; 16]; 2];
                engine.hash_many(&inputs, &mut outputs);
                for lane in 0..2 {
                    assert_eq!(
                        outputs[lane],
                        reference(inputs[lane]),
                        "len={len} lane={lane}"
                    );
                }
            }
        }

        if let Some(avx512) = detected.as_avx512() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx512(avx512));
            check_counts(
                engine,
                &[8, 9, 16, 17, 31, 32, 33, 47, 48, 49, 50, 56, 63, 64],
                2048,
            );
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn x86_underfilled_skew_batches_match_reference() {
        fn check(engine: Md5Many, count: usize, short_count: usize) {
            let storage: std::vec::Vec<std::vec::Vec<u8>> = (0..count)
                .map(|lane| {
                    let len = if lane < short_count { 129 } else { 4097 };
                    (0..len).map(|i| (lane * 47 + i * 19) as u8).collect()
                })
                .collect();
            let inputs: std::vec::Vec<&[u8]> =
                storage.iter().map(std::vec::Vec::as_slice).collect();
            let mut outputs = std::vec![[0u8; 16]; count];
            engine.hash_many(&inputs, &mut outputs);
            for (lane, (input, output)) in inputs.iter().zip(&outputs).enumerate() {
                assert_eq!(
                    *output,
                    reference(input),
                    "count={count} short_count={short_count} lane={lane}"
                );
            }
        }

        let detected = fearless_simd::Level::new();
        if let Some(avx2) = detected.as_avx2() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx2(avx2));
            for (count, short_count) in
                [(3, 2), (9, 4), (15, 7), (17, 8), (23, 8), (26, 8), (31, 15)]
            {
                check(engine, count, short_count);
            }
        }
        if let Some(avx512) = detected.as_avx512() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx512(avx512));
            for (count, short_count) in [
                (3, 2),
                (4, 2),
                (4, 3),
                (5, 3),
                (6, 4),
                (7, 5),
                (8, 6),
                (17, 8),
                (31, 15),
                (33, 16),
                (47, 16),
                (50, 16),
                (57, 16),
                (63, 31),
            ] {
                check(engine, count, short_count);
            }
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn many_padding_boundaries_match_reference() {
        let storage: [[u8; 129]; 8] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 19 + i * 7) as u8));

        for len in [0, 1, 55, 56, 63, 64, 65, 119, 120, 127, 128, 129] {
            let inputs: [&[u8]; 8] = core::array::from_fn(|lane| &storage[lane][..len]);
            let mut outputs = [[0u8; 16]; 8];
            Md5Many::new().hash_many(&inputs, &mut outputs);
            for lane in 0..8 {
                assert_eq!(
                    outputs[lane],
                    reference(inputs[lane]),
                    "lane={lane}, len={len}"
                );
            }
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn many_mixed_lengths_are_correct() {
        let a = b"".as_slice();
        let b = b"abc".as_slice();
        let c = [0x5au8; 65];
        let d = [0xa5u8; 130];
        let inputs: [&[u8]; 4] = [a, b, &c, &d];
        let mut outputs = [[0u8; 16]; 4];
        Md5Many::new().hash_many(&inputs, &mut outputs);
        for i in 0..inputs.len() {
            assert_eq!(outputs[i], reference(inputs[i]));
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn many_mixed_lengths_cross_multiple_block_counts() {
        let storage: [[u8; 321]; 8] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 43 + i * 11) as u8));
        let lengths = [0usize, 3, 55, 64, 119, 128, 193, 321];
        let inputs: [&[u8]; 8] = core::array::from_fn(|lane| &storage[lane][..lengths[lane]]);
        let mut outputs = [[0u8; 16]; 8];

        Md5Many::new().hash_many(&inputs, &mut outputs);
        for lane in 0..inputs.len() {
            assert_eq!(outputs[lane], reference(inputs[lane]), "lane={lane}");
        }
    }

    #[cfg(feature = "std")]
    #[test]
    fn large_dual_batches_and_skew_partition_match_reference() {
        let engine = Md5Many::new();

        // Covers repeated dual-batch scheduling beyond one native pair.
        let equal_storage: std::vec::Vec<std::vec::Vec<u8>> = (0..64)
            .map(|lane| (0..1025).map(|i| (lane * 29 + i * 7) as u8).collect())
            .collect();
        let equal_inputs: std::vec::Vec<&[u8]> =
            equal_storage.iter().map(std::vec::Vec::as_slice).collect();
        let mut outputs = std::vec![[0u8; 16]; equal_inputs.len()];
        engine.hash_many(&equal_inputs, &mut outputs);
        for (lane, (input, output)) in equal_inputs.iter().zip(&outputs).enumerate() {
            assert_eq!(*output, reference(input), "equal lane={lane}");
        }

        // Alternating short/long messages forces the no-alloc skew planner to
        // repartition each dual candidate, then scatter digests back to the
        // caller's original order.
        let mixed_storage: std::vec::Vec<std::vec::Vec<u8>> = (0..64)
            .map(|lane| {
                let len = if lane % 2 == 0 { 1024 } else { 16 * 1024 };
                (0..len).map(|i| (lane * 17 + i * 11) as u8).collect()
            })
            .collect();
        let mixed_inputs: std::vec::Vec<&[u8]> =
            mixed_storage.iter().map(std::vec::Vec::as_slice).collect();
        outputs.fill([0u8; 16]);
        engine.hash_many(&mixed_inputs, &mut outputs);
        for (lane, (input, output)) in mixed_inputs.iter().zip(&outputs).enumerate() {
            assert_eq!(*output, reference(input), "mixed lane={lane}");
        }
    }

    #[test]
    fn incremental_single_state_boundaries_match_reference() {
        let data: [u8; 321] = core::array::from_fn(|i| (i * 37 + 11) as u8);
        for len in 0..=data.len() {
            let mut state = Md5State::new();
            let split1 = core::cmp::min(len, 31);
            let split2 = core::cmp::min(len, 97);
            state.update(&data[..split1]);
            state.update(&data[split1..split2]);
            state.update(&data[split2..len]);
            assert_eq!(state.finalize(), reference(&data[..len]), "len={len}");
            assert_eq!(state.bytes_hashed(), len as u64);
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn incremental_many_mixed_chunks_match_reference() {
        let engine = Md5Many::new();
        let storage: std::vec::Vec<std::vec::Vec<u8>> = (0..37)
            .map(|lane| {
                let len = (lane * 431 + 57) % 5001;
                (0..len).map(|i| (lane * 53 + i * 19) as u8).collect()
            })
            .collect();
        let mut states = std::vec![Md5State::new(); storage.len()];
        let mut offsets = std::vec![0usize; storage.len()];
        let mut round = 0usize;

        while offsets
            .iter()
            .zip(&storage)
            .any(|(&offset, message)| offset < message.len())
        {
            let chunks: std::vec::Vec<&[u8]> = storage
                .iter()
                .enumerate()
                .map(|(lane, message)| {
                    let start = offsets[lane];
                    let remaining = message.len() - start;
                    let proposed = 1 + ((round * 17 + lane * 29) % 172);
                    let take = core::cmp::min(remaining, proposed);
                    &message[start..start + take]
                })
                .collect();

            engine.update_many(&mut states, &chunks);
            for lane in 0..storage.len() {
                offsets[lane] += chunks[lane].len();
            }
            round += 1;
        }

        let mut outputs = std::vec![[0u8; 16]; states.len()];
        engine.finalize_many(&states, &mut outputs);
        for lane in 0..storage.len() {
            assert_eq!(
                outputs[lane],
                reference(&storage[lane]),
                "mixed incremental lane={lane}"
            );
        }

        // Finalization is a snapshot rather than a consuming operation.
        let suffixes: std::vec::Vec<std::vec::Vec<u8>> = (0..states.len())
            .map(|lane| std::vec![(lane as u8).wrapping_mul(7); lane % 91])
            .collect();
        let suffix_refs: std::vec::Vec<&[u8]> =
            suffixes.iter().map(std::vec::Vec::as_slice).collect();
        engine.update_many(&mut states, &suffix_refs);
        engine.finalize_many(&states, &mut outputs);
        for lane in 0..storage.len() {
            let mut expected = storage[lane].clone();
            expected.extend_from_slice(&suffixes[lane]);
            assert_eq!(
                outputs[lane],
                reference(&expected),
                "post-finalize incremental lane={lane}"
            );
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    #[test]
    fn incremental_many_partial_block_completion_matches_reference() {
        let engine = Md5Many::new();
        let storage: [[u8; 129]; 16] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 23 + i * 41) as u8));
        let mut states = [Md5State::new(); 16];

        let first: [&[u8]; 16] = core::array::from_fn(|lane| &storage[lane][..31]);
        engine.update_many(&mut states, &first);
        let second: [&[u8]; 16] = core::array::from_fn(|lane| &storage[lane][31..64]);
        engine.update_many(&mut states, &second);
        let third: [&[u8]; 16] = core::array::from_fn(|lane| &storage[lane][64..129]);
        engine.update_many(&mut states, &third);

        let mut outputs = [[0u8; 16]; 16];
        engine.finalize_many(&states, &mut outputs);
        for lane in 0..16 {
            assert_eq!(outputs[lane], reference(&storage[lane]), "lane={lane}");
        }
    }

    #[cfg(feature = "std")]
    #[test]
    fn incremental_many_lockstep_then_mixed_matches_reference() {
        let engine = Md5Many::new();
        let lanes = engine.lanes();
        let storage: std::vec::Vec<std::vec::Vec<u8>> = (0..lanes)
            .map(|lane| (0..257).map(|i| (lane * 31 + i * 43 + 7) as u8).collect())
            .collect();
        let mut states = std::vec![Md5State::new(); lanes];

        // The first update is deliberately lockstep and leaves a partial
        // block in every stream. The second update diverges lane-by-lane and
        // must fall back to the general compaction scheduler without losing
        // the buffered prefix.
        let first: std::vec::Vec<&[u8]> = storage.iter().map(|message| &message[..47]).collect();
        engine.update_many(&mut states, &first);

        let second_lengths: std::vec::Vec<usize> =
            (0..lanes).map(|lane| 1 + ((lane * 37) % 173)).collect();
        let second: std::vec::Vec<&[u8]> = storage
            .iter()
            .zip(&second_lengths)
            .map(|(message, &len)| &message[47..47 + len])
            .collect();
        engine.update_many(&mut states, &second);

        let mut outputs = std::vec![[0u8; 16]; lanes];
        engine.finalize_many(&states, &mut outputs);
        for lane in 0..lanes {
            let len = 47 + second_lengths[lane];
            assert_eq!(
                outputs[lane],
                reference(&storage[lane][..len]),
                "lane={lane}, len={len}"
            );
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn incremental_avx512_stateful_kernel_matches_reference() {
        let detected = fearless_simd::Level::new();
        let Some(avx512) = detected.as_avx512() else {
            return;
        };

        let engine = Md5Many::from_level(fearless_simd::Level::Avx512(avx512));
        let storage: [[u8; 521]; 16] =
            core::array::from_fn(|lane| core::array::from_fn(|i| (lane * 59 + i * 17) as u8));
        let mut states = [Md5State::new(); 16];

        for (start, end) in [(0, 17), (17, 64), (64, 129), (129, 257), (257, 521)] {
            let inputs: [&[u8]; 16] = core::array::from_fn(|lane| &storage[lane][start..end]);
            engine.update_many(&mut states, &inputs);
        }

        let mut outputs = [[0u8; 16]; 16];
        engine.finalize_many(&states, &mut outputs);
        for lane in 0..16 {
            assert_eq!(outputs[lane], reference(&storage[lane]), "lane={lane}");
        }
    }

    #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
    #[test]
    fn incremental_x86_interleaved_groups_match_reference() {
        fn check(engine: Md5Many, count: usize) {
            let storage: std::vec::Vec<std::vec::Vec<u8>> = (0..count)
                .map(|lane| (0..521).map(|i| (lane * 71 + i * 23 + 5) as u8).collect())
                .collect();
            let mut states = std::vec![Md5State::new(); count];

            // Three exactly block-aligned updates exercise the native/dual/
            // triple stateful kernels directly. The remaining uneven chunks
            // verify that the same states transition back through buffered
            // incremental scheduling before interleaved finalization.
            for (start, end) in [
                (0, 64),
                (64, 128),
                (128, 192),
                (192, 239),
                (239, 320),
                (320, 521),
            ] {
                let inputs: std::vec::Vec<&[u8]> =
                    storage.iter().map(|message| &message[start..end]).collect();
                engine.update_many(&mut states, &inputs);
            }

            let mut outputs = std::vec![[0u8; 16]; count];
            engine.finalize_many(&states, &mut outputs);
            for lane in 0..count {
                assert_eq!(
                    outputs[lane],
                    reference(&storage[lane]),
                    "count={count} lane={lane}"
                );
            }
        }

        let detected = fearless_simd::Level::new();
        if let Some(avx2) = detected.as_avx2() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx2(avx2));
            check(engine, 16);
            check(engine, 24);
        }
        if let Some(avx512) = detected.as_avx512() {
            let engine = Md5Many::from_level(fearless_simd::Level::Avx512(avx512));
            check(engine, 32);
            check(engine, 48);
        }
    }

    #[cfg(any(feature = "std", target_arch = "wasm32"))]
    proptest::proptest! {
        #![proptest_config(proptest::test_runner::Config::with_cases(64))]

        #[test]
        fn random_batches_match_reference(
            messages in proptest::collection::vec(
                proptest::collection::vec(proptest::prelude::any::<u8>(), 0..513),
                0..65,
            )
        ) {
            let inputs: std::vec::Vec<&[u8]> = messages.iter().map(std::vec::Vec::as_slice).collect();
            let mut outputs = std::vec![[0u8; 16]; inputs.len()];
            Md5Many::new().hash_many(&inputs, &mut outputs);
            for (input, output) in inputs.iter().zip(outputs) {
                proptest::prop_assert_eq!(output, reference(input));
            }
        }
    }

    #[cfg(feature = "digest")]
    #[test]
    fn digest_trait_compatibility() {
        let mut hasher = Md5::new();
        hasher.update(b"fearless");
        hasher.update(b" md5");
        let got = hasher.finalize();
        assert_eq!(got.as_slice(), reference(b"fearless md5"));
    }

    #[cfg(feature = "digest")]
    #[test]
    fn digest_streaming_multiblock_matches_reference() {
        let data: [u8; 4097] = core::array::from_fn(|i| (i * 29 + 7) as u8);
        let mut hasher = Md5::new();
        hasher.update(&data[..13]);
        hasher.update(&data[13..4013]);
        hasher.update(&data[4013..]);
        let got = hasher.finalize();
        assert_eq!(got.as_slice(), reference(&data));
    }
}