commonware-cryptography 2026.10.0

Generate keys, sign arbitrary messages, and deterministically verify signatures.
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
//! Bounded deterministic fuzzing helpers for the vendored Reed-Solomon implementation.

#[cfg(target_arch = "aarch64")]
use super::engine::Neon;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use super::engine::{Avx2, Avx512};
use super::{
    Decoder, Error, Plan,
    engine::{
        CANTOR_BASIS, DefaultEngine, Engine, GF_MODULUS, GF_ORDER, GF_POLYNOMIAL, GfElement, Naive,
        SHARD_CHUNK_BYTES, Scalar, ShardsRefMut,
    },
    rate::{DefaultRate, HighRate, LowRate, Rate, RateDecoder, RateEncoder},
};
use arbitrary::Unstructured;
use fixedbitset::FixedBitSet;

/// Shard lengths in bytes for rate cases: one 2-byte element, one 64-byte chunk, and one element
/// either side of one and two chunks.
const SHARD_SIZES: [usize; 6] = [2, 62, 64, 66, 126, 130];

/// Instantiate each supported engine, including implementations below the default CPU priority.
macro_rules! each_engine {
    ($runner:ident ( $($arg:expr),* )) => {{
        $runner::<Scalar>($($arg,)* Scalar::new);
        $runner::<DefaultEngine>($($arg,)* DefaultEngine::new);
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        {
            if std::arch::is_x86_feature_detected!("avx512f")
                && std::arch::is_x86_feature_detected!("gfni")
            {
                $runner::<Avx512>($($arg,)* Avx512::new);
            }
            if std::arch::is_x86_feature_detected!("avx2") {
                $runner::<Avx2>($($arg,)* Avx2::new);
            }
        }
        #[cfg(target_arch = "aarch64")]
        if std::arch::is_aarch64_feature_detected!("neon") {
            $runner::<Neon>($($arg,)* Neon::new);
        }
    }};
}

/// Bounded comparisons of engines and engine-independent kernels against references.
#[derive(Clone, Copy, Debug, arbitrary::Arbitrary)]
pub enum EnginePlan {
    /// Compare multiplication, including empty and multi-chunk operands.
    Mul,
    /// Compare FFT and IFFT with offsets, truncation, and sentinel shards.
    Transform,
    /// Compare short Walsh locator convolution with the full-field evaluator.
    Locator,
    /// Compare the fused formal derivative with the unfused pass schedule.
    Derivative,
}

impl EnginePlan {
    /// Run the selected check.
    ///
    /// `Mul` and `Transform` compare every engine supported by this host. `Locator` and
    /// `Derivative` check engine-independent kernels once.
    pub fn run(self, u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
        match self {
            Self::Mul => fuzz_mul(u),
            Self::Transform => fuzz_transform(u),
            Self::Locator => fuzz_locator(u),
            Self::Derivative => fuzz_derivative(u),
        }
    }
}

/// Checks multiplication on fuzzed chunks with `compare_mul`.
///
/// Draws `log_m` from logarithm edge cases, including `GF_MODULUS`, or at random. Draws 0, 1,
/// 2, 3, or 65 chunks, or 4 to 16.
fn fuzz_mul(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
    let log_m = match u.int_in_range(0..=7)? {
        0 => 0,
        1 => 1,
        2 => 2,
        3 => GF_MODULUS / 2,
        4 => GF_MODULUS / 2 + 1,
        5 => GF_MODULUS - 1,
        6 => GF_MODULUS,
        _ => u.arbitrary()?,
    };
    let mul_chunks = match u.int_in_range(0..=5)? {
        0 => 0,
        1 => 1,
        2 => 2,
        3 => 3,
        4 => 65,
        _ => u.int_in_range(4..=16)?,
    };
    let mut mul_input = vec![[0; SHARD_CHUNK_BYTES]; mul_chunks];
    let mut input = Input::new(u.bytes(u.len())?);
    fill_chunks(&mut mul_input, &mut input);
    compare_mul(&mul_input, log_m);
    Ok(())
}

/// Checks FFT and IFFT on a fuzzed block with `compare_transform`.
///
/// The block of `size = 2^k` shards, `k` in `0..=6`, starts at shard `pos` and is followed by up
/// to three suffix shards. `skew_delta` is an edge value or random in `0..=GF_ORDER - size`.
fn fuzz_transform(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
    let size = 1usize << u.int_in_range(0..=6)?;
    let pos = u.int_in_range(0..=5)?;
    let suffix = u.int_in_range(0..=3)?;
    let truncated_size = u.int_in_range(0..=size)?;
    let max_skew = GF_ORDER - size;
    let skew_delta = match u.int_in_range(0..=5)? {
        0 => 0,
        1 => 1.min(max_skew),
        2 => 7.min(max_skew),
        3 => max_skew,
        4 => 17.min(max_skew),
        _ => usize::from(u.arbitrary::<u16>()?) % (max_skew + 1),
    };
    let shard_chunks = match u.int_in_range(0..=5)? {
        0 => 0,
        1 => 1,
        2 => 2,
        3 => 17,
        4 => 65,
        _ => u.int_in_range(3..=8)?,
    };
    let shard_count = pos + size + suffix;
    let mut transform_input = vec![[0; SHARD_CHUNK_BYTES]; shard_count * shard_chunks];
    let mut input = Input::new(u.bytes(u.len())?);
    fill_chunks(&mut transform_input, &mut input);
    compare_transform(
        &transform_input,
        shard_count,
        shard_chunks,
        pos,
        size,
        truncated_size,
        skew_delta,
    );
    Ok(())
}

/// Checks the short Walsh locator on a fuzzed power-of-two domain `n <= 2^15` and truncation
/// `end` in `1..=n`.
fn fuzz_locator(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
    let n = 1usize << u.int_in_range(0..=15)?;
    let end = u.int_in_range(1..=n)?;
    compare_short_locator(n, end, u.arbitrary()?);
    Ok(())
}

/// Checks that `eval_poly_short` on `n` entries matches `Naive::eval_poly` modulo `GF_MODULUS`
/// on the first `n` outputs and never writes entry `n`.
///
/// Entries `0..end` hold values derived from `seed`, and the rest are zero. `n` must be a power
/// of two below `GF_ORDER`, and `end <= n`.
fn compare_short_locator(n: usize, end: usize, seed: u16) {
    // The convolution is linear modulo GF_MODULUS, so inputs need not be 0/1 flags. Mix zero,
    // GF_MODULUS (the other encoding of zero), and seeded residues.
    let mut short = [0; GF_ORDER];
    for (i, value) in short[..end].iter_mut().enumerate() {
        *value = match (i ^ usize::from(seed)) % 5 {
            0 => 0,
            1 => GF_MODULUS,
            _ => (i as u16).wrapping_mul(257) ^ seed,
        };
    }
    let mut full = short;
    Naive::eval_poly(&mut full, end);

    // The sentinel detects a write past the domain.
    short[n] = 0xa5a5;
    super::engine::utils::eval_poly_short(&mut short[..n], end);
    for i in 0..n {
        assert_eq!(
            short[i] % GF_MODULUS,
            full[i] % GF_MODULUS,
            "n={n} end={end} seed={seed} i={i}"
        );
    }
    assert_eq!(short[n], 0xa5a5, "n={n} end={end} wrote past the domain");
}

/// Shard counts for derivative cases. They bracket the four- and sixteen-shard leaf blocks and
/// each pass width.
const DERIVATIVE_COUNTS: [usize; 21] = [
    0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129,
];
/// Chunks per shard for derivative cases. They bracket the AVX-512 leaf's 8-chunk (512-byte)
/// minimum and its 64-chunk (4 KiB) exclusion.
const DERIVATIVE_CHUNKS: [usize; 10] = [0, 1, 3, 7, 8, 9, 17, 63, 64, 65];

/// Checks `formal_derivative` with `compare_derivative` on a fuzzed shard count and chunks per
/// shard.
///
/// The count comes from `DERIVATIVE_COUNTS` or from `0..=129`. The chunks per shard come from
/// `DERIVATIVE_CHUNKS`.
fn fuzz_derivative(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
    let count = if u.arbitrary::<bool>()? {
        DERIVATIVE_COUNTS[u.int_in_range(0..=20)?]
    } else {
        u.int_in_range(0..=129)?
    };
    let chunks = DERIVATIVE_CHUNKS[u.int_in_range(0..=9)?];
    let mut input = vec![[0; SHARD_CHUNK_BYTES]; count * chunks];
    fill_chunks(&mut input, &mut Input::new(u.bytes(u.len())?));
    compare_derivative(&input, count, chunks);
    Ok(())
}

/// Checks `formal_derivative` on `count` shards of `chunks` chunks each against the reference
/// pass schedule.
///
/// `input` holds the shards back to back, so its length must be `count * chunks`.
fn compare_derivative(input: &[[u8; SHARD_CHUNK_BYTES]], count: usize, chunks: usize) {
    // The reference runs the unclamped pass schedule over a zero-padded power-of-two block.
    // Padding shards only absorb higher padding shards and stay zero, so the first `count`
    // shards match the schedule with passes clamped at `count`.
    let padded_count = count.max(1).next_power_of_two();
    let mut expected = vec![[0; SHARD_CHUNK_BYTES]; padded_count * chunks];
    expected[..input.len()].copy_from_slice(input);
    let mut actual = input.to_vec();

    let mut reference = ShardsRefMut::new(padded_count, chunks, &mut expected);
    for i in 1..padded_count {
        let width = 1 << i.trailing_zeros();
        super::engine::utils::xor_within(&mut reference, i - width, i, width);
    }
    super::engine::utils::formal_derivative(&mut ShardsRefMut::new(count, chunks, &mut actual));
    assert_eq!(
        actual,
        expected[..input.len()],
        "count={count}, chunks={chunks}"
    );
}

/// Encoding rate exercised by the shared checks.
#[derive(Clone, Copy, Debug, arbitrary::Arbitrary)]
pub enum RateKind {
    /// More original shards than recovery shards.
    High,
    /// More recovery shards than original shards.
    Low,
    /// Switch between high and low rates when resetting.
    Default,
}

/// Bounded rate comparisons and backend-independent encoding/decoding contracts.
#[derive(Clone, Copy, Debug, arbitrary::Arbitrary)]
pub enum RatePlan {
    /// Compare encoding with the naive engine and decode its recovery shards.
    MatchesNaive(RateKind),
    /// Check roundtrips, automatic reuse, and reset with the same backend.
    Contract(RateKind),
    /// Check the public recovery decoder's reuse and missing recovery shards.
    Recovery,
    /// Compare prepared recovery against ordinary decoding across sizes, reuse, and mismatches.
    PreparedRecovery,
    /// Compare the rate-specific shortened locator against full-field arithmetic.
    Locator,
}

impl RatePlan {
    /// Run rate checks on every supported engine, exercise the public recovery decoder, or
    /// compare a rate's shortened erasure locator with the naive evaluator.
    pub fn run(self, u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
        if matches!(self, Self::Locator) {
            return fuzz_rate_locator(u);
        }
        let small = u.int_in_range(2..=4)?;
        let large = u.int_in_range(9..=12)?;
        let shard_bytes_a = SHARD_SIZES[u.int_in_range(0..=5)?];
        let shard_bytes_b = SHARD_SIZES[u.int_in_range(0..=5)?];

        let (counts_a, counts_b) = match self {
            Self::MatchesNaive(RateKind::High) | Self::Contract(RateKind::High) => {
                ((large, small), (large - 1, small + 1))
            }
            Self::MatchesNaive(RateKind::Low) | Self::Contract(RateKind::Low) => {
                ((small, large), (small + 1, large - 1))
            }
            _ => ((large, small), (small, large)),
        };
        let mut input = Input::new(u.bytes(u.len())?);
        let case_a = RateCase::new(counts_a.0, counts_a.1, shard_bytes_a, &mut input);
        let case_b = RateCase::new(counts_b.0, counts_b.1, shard_bytes_b, &mut input);

        match self {
            Self::MatchesNaive(kind) => {
                each_engine!(rate_matches_naive(kind, &case_a, &case_b))
            }
            Self::Contract(kind) => {
                rate_contract::<Naive>(kind, &case_a, &case_b, Naive::new);
                each_engine!(rate_contract(kind, &case_a, &case_b));
            }
            Self::Recovery => exercise_recovery_reuse(&case_a, &case_b),
            Self::PreparedRecovery => exercise_prepared_reuse(&case_a, &case_b),
            Self::Locator => unreachable!("returned before case generation"),
        }
        Ok(())
    }
}

/// Checks a rate's shortened erasure locator with `compare_rate_locator` on fuzzed counts in
/// `1..=64`, a fuzzed rate, and a fuzzed received mask over the decoding domain.
fn fuzz_rate_locator(u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
    let high = u.arbitrary::<bool>()?;
    let original_count = u.int_in_range(1usize..=64)?;
    let recovery_count = u.int_in_range(1usize..=64)?;
    let chunk = if high { recovery_count } else { original_count }.next_power_of_two();
    let end = chunk + if high { original_count } else { recovery_count };
    let mut received = FixedBitSet::with_capacity(end);
    for i in 0..end {
        if u.arbitrary::<bool>()? {
            received.insert(i);
        }
    }
    compare_rate_locator(high, original_count, recovery_count, &received);
    Ok(())
}

/// Checks a rate's shortened erasure locator against `Naive::eval_poly` over the full field.
///
/// `high` selects the high-rate layout (`rate_high::eval_erasures`) or the low-rate layout
/// (`rate_low::eval_walsh`). The decoding domain `0..end` places recovery shards (high rate) or
/// originals (low rate) from 0 and the other kind from `chunk`, the next power of two of the
/// first kind's count. `received` marks received positions in `0..end`. Only outputs below `end`
/// are compared, modulo `GF_MODULUS`.
fn compare_rate_locator(
    high: bool,
    original_count: usize,
    recovery_count: usize,
    received: &FixedBitSet,
) {
    // The reference marks erased positions over the full field. High rate places recovery
    // shards from 0 and originals from `chunk`, erases the padding between them, and truncates
    // at `end`. Low rate places originals from 0 and recovery shards from `chunk`, keeps the
    // padding known, and erases every position from `end`.
    let chunk = if high { recovery_count } else { original_count }.next_power_of_two();
    let end = chunk + if high { original_count } else { recovery_count };
    let mut expected = [0; GF_ORDER];
    if high {
        for i in 0..recovery_count {
            expected[i] = u16::from(!received[i]);
        }
        expected[recovery_count..chunk].fill(1);
        for i in chunk..end {
            expected[i] = u16::from(!received[i]);
        }
    } else {
        for i in 0..original_count {
            expected[i] = u16::from(!received[i]);
        }
        for i in chunk..end {
            expected[i] = u16::from(!received[i]);
        }
        expected[end..].fill(1);
    }
    Naive::eval_poly(&mut expected, if high { end } else { GF_ORDER });
    let mut actual = [0; GF_ORDER];
    let domain = &mut actual[..end.next_power_of_two()];
    if high {
        super::rate::rate_high::eval_erasures::<Scalar>(
            domain,
            original_count,
            recovery_count,
            received,
        );
    } else {
        super::rate::rate_low::eval_walsh::<Scalar>(
            domain,
            original_count,
            recovery_count,
            received,
        );
    }
    for i in 0..end {
        assert_eq!(
            actual[i] % GF_MODULUS,
            expected[i] % GF_MODULUS,
            "high={high} original={original_count} recovery={recovery_count} i={i}"
        );
    }
}

/// Cyclic byte source over fuzz input. Reads zeros when the input is empty.
struct Input<'a> {
    /// Fuzz bytes to cycle through.
    bytes: &'a [u8],
    /// Number of bytes read. The next read takes index `offset % bytes.len()`.
    offset: usize,
}

impl<'a> Input<'a> {
    /// Creates a source that starts at the first byte of `bytes`.
    const fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, offset: 0 }
    }

    /// Returns the next byte, wrapping to the start after the last.
    ///
    /// Returns 0 if the input is empty.
    const fn byte(&mut self) -> u8 {
        if self.bytes.is_empty() {
            return 0;
        }
        let byte = self.bytes[self.offset % self.bytes.len()];
        self.offset += 1;
        byte
    }
}

/// Fills every chunk from `input`, adding chunk and lane offsets so the bytes still vary when a
/// short input repeats.
fn fill_chunks(chunks: &mut [[u8; SHARD_CHUNK_BYTES]], input: &mut Input<'_>) {
    for (chunk_index, chunk) in chunks.iter_mut().enumerate() {
        let (low, high) = chunk.split_at_mut(SHARD_CHUNK_BYTES / 2);
        for lane in 0..SHARD_CHUNK_BYTES / 2 {
            low[lane] = input
                .byte()
                .wrapping_add((chunk_index as u8).wrapping_mul(29))
                .wrapping_add(lane as u8);
            high[lane] = input
                .byte()
                .wrapping_add((chunk_index as u8).wrapping_mul(53))
                .wrapping_sub(lane as u8);
        }
    }
}

/// Checks `Naive` multiplication by the element with logarithm `log_m` against
/// `independent_mul`, then checks every engine against `Naive`.
///
/// Each chunk holds 32 elements, with low bytes in its first half and high bytes in its second.
fn compare_mul(input: &[[u8; SHARD_CHUNK_BYTES]], log_m: GfElement) {
    let mut expected = input.to_vec();
    for chunk in &mut expected {
        let (low, high) = chunk.split_at_mut(SHARD_CHUNK_BYTES / 2);
        for lane in 0..SHARD_CHUNK_BYTES / 2 {
            let value = GfElement::from(low[lane]) | (GfElement::from(high[lane]) << 8);
            let product = independent_mul(value, log_m);
            low[lane] = product as u8;
            high[lane] = (product >> 8) as u8;
        }
    }

    let mut naive = input.to_vec();
    Naive::new().mul(&mut naive, log_m);
    assert_eq!(naive, expected, "Naive mul differs for log {log_m}");

    each_engine!(check_mul(input, log_m, &naive));
}

/// Checks that engine `E` multiplies `input` by the element with logarithm `log_m` to give
/// `expected`.
fn check_mul<E: Engine>(
    input: &[[u8; SHARD_CHUNK_BYTES]],
    log_m: GfElement,
    expected: &[[u8; SHARD_CHUNK_BYTES]],
    new_engine: fn() -> E,
) {
    let mut actual = input.to_vec();
    new_engine().mul(&mut actual, log_m);
    assert_eq!(
        actual,
        expected,
        "{} mul differs for log {log_m}",
        core::any::type_name::<E>()
    );
}

/// Multiplies `value` by the element with logarithm `log_m` without the engine tables.
///
/// Elements are Cantor-basis coordinates. The oracle converts `value` to the polynomial basis,
/// multiplies by x^log_m reduced modulo GF_POLYNOMIAL, and converts back. It builds x^log_m by
/// square-and-multiply, so a table bug cannot cancel against the same bug in the engines.
fn independent_mul(value: GfElement, log_m: GfElement) -> GfElement {
    let mut coefficient = 1;
    let mut alpha = 2;
    let mut exponent = usize::from(log_m) % usize::from(GF_MODULUS);
    while exponent > 0 {
        if exponent & 1 == 1 {
            coefficient = polynomial_mul(coefficient, alpha);
        }
        alpha = polynomial_mul(alpha, alpha);
        exponent >>= 1;
    }
    independent_mul_coefficient(value, coefficient)
}

/// Multiplies Cantor-basis `value` by polynomial-basis `coefficient`.
fn independent_mul_coefficient(value: GfElement, coefficient: GfElement) -> GfElement {
    polynomial_to_cantor(polynomial_mul(cantor_to_polynomial(value), coefficient))
}

/// Converts Cantor-basis coordinates `value` to the polynomial basis.
///
/// Bit `i` of `value` selects `CANTOR_BASIS[i]`, written in the polynomial basis.
fn cantor_to_polynomial(value: GfElement) -> GfElement {
    let mut polynomial = 0;
    for (bit, basis) in CANTOR_BASIS.iter().enumerate() {
        if value & (1 << bit) != 0 {
            polynomial ^= basis;
        }
    }
    polynomial
}

/// Converts polynomial-basis `value` to Cantor-basis coordinates, inverting
/// `cantor_to_polynomial`.
fn polynomial_to_cantor(value: GfElement) -> GfElement {
    // Invert the basis change by Gauss-Jordan elimination over GF(2). Row `r` keeps
    // `values[r]` equal to the XOR of `CANTOR_BASIS[j]` over the set bits `j` of
    // `coordinates[r]`. Once `values[r] == 1 << r`, `coordinates[r]` holds the Cantor
    // coordinates of x^r. A pivot always exists because the basis is linearly independent.
    static INVERSE: std::sync::OnceLock<[GfElement; 16]> = std::sync::OnceLock::new();
    let inverse = INVERSE.get_or_init(|| {
        let mut values = CANTOR_BASIS;
        let mut coordinates = core::array::from_fn(|index| 1 << index);
        for pivot in 0..16 {
            let row = (pivot..16)
                .find(|row| values[*row] & (1 << pivot) != 0)
                .unwrap();
            values.swap(pivot, row);
            coordinates.swap(pivot, row);
            for row in 0..16 {
                if row != pivot && values[row] & (1 << pivot) != 0 {
                    values[row] ^= values[pivot];
                    coordinates[row] ^= coordinates[pivot];
                }
            }
        }
        debug_assert_eq!(values, core::array::from_fn(|index| 1 << index));
        coordinates
    });

    let mut coordinates = 0;
    for (bit, coordinate) in inverse.iter().enumerate() {
        if value & (1 << bit) != 0 {
            coordinates ^= coordinate;
        }
    }
    coordinates
}

/// Carry-less multiply, then reduce the degree-30 product modulo GF_POLYNOMIAL from the top bit.
fn polynomial_mul(left: GfElement, right: GfElement) -> GfElement {
    let mut product = 0u32;
    for bit in 0..16 {
        if right & (1 << bit) != 0 {
            product ^= u32::from(left) << bit;
        }
    }
    for bit in (16..=30).rev() {
        if product & (1 << bit) != 0 {
            product ^= (GF_POLYNOMIAL as u32) << (bit - 16);
        }
    }
    product as GfElement
}

/// Checks every engine's FFT and IFFT against `Naive` on shards `pos..pos + size` of `input`.
///
/// `input` holds `shard_count` shards of `shard_chunks` chunks each. Shards outside the block
/// must stay unchanged. FFT compares the first `truncated_size` block outputs. IFFT first zeroes
/// the block's input shards from `pos + truncated_size`, then compares the whole block.
///
/// # Panics
///
/// Panics if `size` is not a power of two at most `GF_ORDER`, `truncated_size > size`, the block
/// extends past `shard_count`, or `size > 1` and `skew_delta > GF_ORDER - size`.
fn compare_transform(
    input: &[[u8; SHARD_CHUNK_BYTES]],
    shard_count: usize,
    shard_chunks: usize,
    pos: usize,
    size: usize,
    truncated_size: usize,
    skew_delta: usize,
) {
    assert!(size.is_power_of_two());
    assert!(size <= GF_ORDER);
    assert!(truncated_size <= size);
    assert!(pos.checked_add(size).is_some_and(|end| end <= shard_count));
    assert!(size == 1 || skew_delta <= GF_ORDER - size);

    for inverse in [false, true] {
        let mut operation_input = input.to_vec();
        if inverse {
            for shard in pos + truncated_size..pos + size {
                operation_input[shard * shard_chunks..(shard + 1) * shard_chunks]
                    .fill([0; SHARD_CHUNK_BYTES]);
            }
        }

        let mut expected = operation_input.clone();
        apply_transform(
            &Naive::new(),
            &mut expected,
            shard_count,
            shard_chunks,
            pos,
            size,
            truncated_size,
            skew_delta,
            inverse,
        );

        for shard in 0..shard_count {
            if shard < pos || shard >= pos + size {
                let chunks = shard * shard_chunks..(shard + 1) * shard_chunks;
                assert_eq!(
                    expected[chunks.clone()],
                    input[chunks],
                    "Naive changed sentinel shard {shard}"
                );
            }
        }
        each_engine!(check_transform(
            &operation_input,
            input,
            &expected,
            shard_count,
            shard_chunks,
            pos,
            size,
            truncated_size,
            skew_delta,
            inverse
        ));
    }
}

/// Checks one FFT or IFFT of `operation_input` on engine `E` against `expected`.
///
/// `operation_input` is `input`, with the block's input suffix zeroed for IFFT. Shards outside the
/// block must match `input`. Block shards are compared where the transform specifies them, which
/// is the first `truncated_size` for FFT and the whole block for IFFT.
#[allow(clippy::too_many_arguments)]
fn check_transform<E: Engine>(
    operation_input: &[[u8; SHARD_CHUNK_BYTES]],
    input: &[[u8; SHARD_CHUNK_BYTES]],
    expected: &[[u8; SHARD_CHUNK_BYTES]],
    shard_count: usize,
    shard_chunks: usize,
    pos: usize,
    size: usize,
    truncated_size: usize,
    skew_delta: usize,
    inverse: bool,
    new_engine: fn() -> E,
) {
    let name = core::any::type_name::<E>();
    let mut actual = operation_input.to_vec();
    apply_transform(
        &new_engine(),
        &mut actual,
        shard_count,
        shard_chunks,
        pos,
        size,
        truncated_size,
        skew_delta,
        inverse,
    );
    for shard in 0..shard_count {
        let chunks = shard * shard_chunks..(shard + 1) * shard_chunks;
        if shard < pos || shard >= pos + size {
            assert_eq!(
                actual[chunks.clone()],
                input[chunks],
                "{name} changed sentinel shard {shard}"
            );
        } else if inverse || shard < pos + truncated_size {
            // FFT specifies a prefix. IFFT specifies the entire block after zeroing its input
            // suffix.
            assert_eq!(
                actual[chunks.clone()],
                expected[chunks],
                "{name} {} differs at shard {shard} for pos={pos} size={size} truncated={truncated_size} skew={skew_delta}",
                if inverse { "ifft" } else { "fft" }
            );
        }
    }
}

/// Runs the FFT of `engine`, or the IFFT if `inverse` is set, on shards `pos..pos + size`.
///
/// `data` holds `shard_count` shards of `shard_chunks` chunks each.
#[allow(clippy::too_many_arguments)]
fn apply_transform<E: Engine>(
    engine: &E,
    data: &mut [[u8; SHARD_CHUNK_BYTES]],
    shard_count: usize,
    shard_chunks: usize,
    pos: usize,
    size: usize,
    truncated_size: usize,
    skew_delta: usize,
    inverse: bool,
) {
    let mut shards = ShardsRefMut::new(shard_count, shard_chunks, data);
    if inverse {
        engine.ifft(&mut shards, pos, size, truncated_size, skew_delta);
    } else {
        engine.fft(&mut shards, pos, size, truncated_size, skew_delta);
    }
}

/// Rate encoding and decoding case with fuzzed shard contents and erasure offsets.
#[derive(Clone)]
struct RateCase {
    original_count: usize,
    recovery_count: usize,
    shard_bytes: usize,
    /// `original_count` original shards of `shard_bytes` bytes each.
    originals: Vec<Vec<u8>>,
    /// First original index dropped in the round with `start_delta` 0.
    original_start: usize,
    /// First recovery index provided in the round with `start_delta` 0.
    recovery_start: usize,
}

impl RateCase {
    /// Creates a case, drawing the erasure offsets and then the shard bytes from `input`.
    ///
    /// Both counts must be nonzero.
    fn new(
        original_count: usize,
        recovery_count: usize,
        shard_bytes: usize,
        input: &mut Input<'_>,
    ) -> Self {
        let original_start = usize::from(input.byte()) % original_count;
        let recovery_start = usize::from(input.byte()) % recovery_count;
        let mut originals = vec![vec![0; shard_bytes]; original_count];
        for (shard_index, shard) in originals.iter_mut().enumerate() {
            for (byte_index, byte) in shard.iter_mut().enumerate() {
                *byte = input
                    .byte()
                    .wrapping_add((shard_index as u8).wrapping_mul(31))
                    .wrapping_add((byte_index as u8).wrapping_mul(17));
            }
        }
        Self {
            original_count,
            recovery_count,
            shard_bytes,
            originals,
            original_start,
            recovery_start,
        }
    }

    /// Number of originals dropped and recovery shards provided in each round, so every
    /// decode receives exactly `original_count` shards. With at least two recovery shards, the
    /// cap leaves at least one recovery shard unprovided.
    fn missing_count(&self) -> usize {
        self.original_count.min(self.recovery_count - 1).max(1)
    }

    /// Returns a mask over originals that marks `missing_count()` consecutive indices as missing,
    /// starting at `original_start + start_delta` and wrapping at `original_count`.
    fn missing_originals(&self, start_delta: usize) -> Vec<bool> {
        let mut missing = vec![false; self.original_count];
        for offset in 0..self.missing_count() {
            missing[(self.original_start + start_delta + offset) % self.original_count] = true;
        }
        missing
    }

    /// Returns a mask over recovery shards that marks `missing_count()` consecutive indices as
    /// provided, starting at `recovery_start + start_delta` and wrapping at `recovery_count`.
    fn provided_recoveries(&self, start_delta: usize) -> Vec<bool> {
        let mut provided = vec![false; self.recovery_count];
        for offset in 0..self.missing_count() {
            provided[(self.recovery_start + start_delta + offset) % self.recovery_count] = true;
        }
        provided
    }
}

/// Encodes `case` with rate `R` on `engine` and returns the recovery shards.
fn encode<R, E>(case: &RateCase, engine: E) -> Vec<Vec<u8>>
where
    R: Rate<E>,
    E: Engine,
{
    let mut encoder = R::encoder(
        case.original_count,
        case.recovery_count,
        case.shard_bytes,
        engine,
        None,
    )
    .unwrap();
    encode_with::<R, E>(&mut encoder, case)
}

/// Adds every original of `case` to `encoder`, encodes, and returns the recovery shards.
///
/// `encoder` must be configured for the counts and shard size of `case`.
fn encode_with<R, E>(encoder: &mut R::RateEncoder, case: &RateCase) -> Vec<Vec<u8>>
where
    R: Rate<E>,
    E: Engine,
{
    for original in &case.originals {
        encoder.add_original_shard(original).unwrap();
    }
    encoder
        .encode()
        .unwrap()
        .recovery_iter()
        .map(<[u8]>::to_vec)
        .collect()
}

/// Runs one decode round and checks that the result holds exactly the missing originals.
///
/// Provides the originals and recovery shards selected by `start_delta`. `decoder` must be
/// configured for `case`, and `recovery` holds the recovery shards encoded from it.
fn decode_with<R, E>(
    decoder: &mut R::RateDecoder,
    case: &RateCase,
    recovery: &[Vec<u8>],
    start_delta: usize,
) where
    R: Rate<E>,
    E: Engine,
{
    let missing = case.missing_originals(start_delta);
    let provided_recovery = case.provided_recoveries(start_delta);
    for (index, original) in case.originals.iter().enumerate() {
        if !missing[index] {
            decoder.add_original_shard(index, original).unwrap();
        }
    }
    for (index, shard) in recovery.iter().enumerate() {
        if provided_recovery[index] {
            decoder.add_recovery_shard(index, shard).unwrap();
        }
    }

    let result = decoder.decode(false).unwrap().unwrap();
    for (index, original) in case.originals.iter().enumerate() {
        if missing[index] {
            assert_eq!(result.original(index), Some(original.as_slice()));
        } else {
            assert_eq!(result.original(index), None);
        }
    }
}

/// Checks encode and decode roundtrips with rate `R` on one engine, including reuse and reset.
///
/// The encoder encodes `case_a`, resets, and encodes `case_b`. The decoder decodes two rounds of
/// `case_a` without a reset, then resets and decodes two rounds of `case_b`.
fn check_rate_contract<R, E>(case_a: &RateCase, case_b: &RateCase, new_engine: fn() -> E)
where
    R: Rate<E>,
    E: Engine,
{
    let mut encoder = R::encoder(
        case_a.original_count,
        case_a.recovery_count,
        case_a.shard_bytes,
        new_engine(),
        None,
    )
    .unwrap();
    let recovery_a = encode_with::<R, E>(&mut encoder, case_a);
    encoder
        .reset(
            case_b.original_count,
            case_b.recovery_count,
            case_b.shard_bytes,
        )
        .unwrap();
    let recovery_b = encode_with::<R, E>(&mut encoder, case_b);

    let mut decoder = R::decoder(
        case_a.original_count,
        case_a.recovery_count,
        case_a.shard_bytes,
        new_engine(),
        None,
    )
    .unwrap();
    decode_with::<R, E>(&mut decoder, case_a, &recovery_a, 0);
    decode_with::<R, E>(&mut decoder, case_a, &recovery_a, 1);
    decoder
        .reset(
            case_b.original_count,
            case_b.recovery_count,
            case_b.shard_bytes,
        )
        .unwrap();
    decode_with::<R, E>(&mut decoder, case_b, &recovery_b, 1);
    decode_with::<R, E>(&mut decoder, case_b, &recovery_b, 0);
}

/// Checks that rate `R` decodes `case_a` from the reference recovery shards `expected_a`, then
/// resets and decodes `case_b` from `expected_b`.
fn decode_reference<R, E>(
    case_a: &RateCase,
    case_b: &RateCase,
    expected_a: &[Vec<u8>],
    expected_b: &[Vec<u8>],
    new_engine: fn() -> E,
) where
    R: Rate<E>,
    E: Engine,
{
    let mut decoder = R::decoder(
        case_a.original_count,
        case_a.recovery_count,
        case_a.shard_bytes,
        new_engine(),
        None,
    )
    .unwrap();
    decode_with::<R, E>(&mut decoder, case_a, expected_a, 0);
    decoder
        .reset(
            case_b.original_count,
            case_b.recovery_count,
            case_b.shard_bytes,
        )
        .unwrap();
    decode_with::<R, E>(&mut decoder, case_b, expected_b, 1);
}

/// Runs `check_rate_contract` on engine `E` with the rate selected by `kind`.
fn rate_contract<E: Engine>(
    kind: RateKind,
    case_a: &RateCase,
    case_b: &RateCase,
    new_engine: fn() -> E,
) {
    match kind {
        RateKind::High => check_rate_contract::<HighRate<E>, E>(case_a, case_b, new_engine),
        RateKind::Low => check_rate_contract::<LowRate<E>, E>(case_a, case_b, new_engine),
        RateKind::Default => check_rate_contract::<DefaultRate<E>, E>(case_a, case_b, new_engine),
    }
}

/// Runs `compare_rate` on engine `E` with the rate selected by `kind`.
fn rate_matches_naive<E: Engine>(
    kind: RateKind,
    case_a: &RateCase,
    case_b: &RateCase,
    new_engine: fn() -> E,
) {
    match kind {
        RateKind::High => {
            compare_rate::<HighRate<E>, HighRate<Naive>, E>(case_a, case_b, new_engine)
        }
        RateKind::Low => compare_rate::<LowRate<E>, LowRate<Naive>, E>(case_a, case_b, new_engine),
        RateKind::Default => {
            compare_rate::<DefaultRate<E>, DefaultRate<Naive>, E>(case_a, case_b, new_engine)
        }
    }
}

/// Checks that rate `R` on engine `E` encodes `case_a` and, after a reset, `case_b` to the same
/// recovery shards as `Reference` on `Naive`, then decodes both with `decode_reference`.
fn compare_rate<R, Reference, E>(case_a: &RateCase, case_b: &RateCase, new_engine: fn() -> E)
where
    R: Rate<E>,
    Reference: Rate<Naive>,
    E: Engine,
{
    let expected_a = encode::<Reference, _>(case_a, Naive::new());
    let expected_b = encode::<Reference, _>(case_b, Naive::new());
    let mut encoder = R::encoder(
        case_a.original_count,
        case_a.recovery_count,
        case_a.shard_bytes,
        new_engine(),
        None,
    )
    .unwrap();
    assert_eq!(encode_with::<R, E>(&mut encoder, case_a), expected_a);
    encoder
        .reset(
            case_b.original_count,
            case_b.recovery_count,
            case_b.shard_bytes,
        )
        .unwrap();
    assert_eq!(encode_with::<R, E>(&mut encoder, case_b), expected_b);
    decode_reference::<R, E>(case_a, case_b, &expected_a, &expected_b, new_engine);
}

/// Runs one `decode_with_recovery` round on the public `Decoder` and checks that it restores
/// exactly the missing originals and the unprovided recovery shards.
///
/// `start_delta` selects the round's erasures. `recovery` holds the recovery shards of `case`.
fn recovery_round(
    decoder: &mut Decoder,
    case: &RateCase,
    recovery: &[Vec<u8>],
    start_delta: usize,
) {
    let missing = case.missing_originals(start_delta);
    let provided_recovery = case.provided_recoveries(start_delta);
    for (index, original) in case.originals.iter().enumerate() {
        if !missing[index] {
            decoder.add_original_shard(index, original).unwrap();
        }
    }
    for (index, shard) in recovery.iter().enumerate() {
        if provided_recovery[index] {
            decoder.add_recovery_shard(index, shard).unwrap();
        }
    }

    let result = decoder.decode_with_recovery().unwrap().unwrap();
    for (index, original) in case.originals.iter().enumerate() {
        let expected = missing[index].then_some(original.as_slice());
        assert_eq!(result.original(index), expected);
    }
    for (index, shard) in recovery.iter().enumerate() {
        let expected = (!provided_recovery[index]).then_some(shard.as_slice());
        assert_eq!(result.recovery(index), expected);
    }
}

/// Checks the public `Decoder` over two rounds of `case_a` without a reset, then one round of
/// `case_b` after a reset.
fn exercise_recovery_reuse(case_a: &RateCase, case_b: &RateCase) {
    let expected_a = encode::<DefaultRate<Naive>, _>(case_a, Naive::new());
    let expected_b = encode::<DefaultRate<Naive>, _>(case_b, Naive::new());
    let mut decoder = Decoder::new(
        case_a.original_count,
        case_a.recovery_count,
        case_a.shard_bytes,
    )
    .unwrap();

    recovery_round(&mut decoder, case_a, &expected_a, 0);
    recovery_round(&mut decoder, case_a, &expected_a, 1);
    decoder
        .reset(
            case_b.original_count,
            case_b.recovery_count,
            case_b.shard_bytes,
        )
        .unwrap();
    recovery_round(&mut decoder, case_b, &expected_b, 0);
}

/// Builds the `Plan` for the erasures of round `start_delta` and runs
/// `prepared_round_with_plan` with it.
fn prepared_round(
    decoder: &mut Decoder,
    case: &RateCase,
    recovery: &[Vec<u8>],
    start_delta: usize,
) {
    let missing = case.missing_originals(start_delta);
    let provided = case.provided_recoveries(start_delta);
    let originals = (0..case.original_count).filter(|&i| !missing[i]);
    let recoveries = (0..case.recovery_count).filter(|&i| provided[i]);
    let plan = Plan::new(
        case.original_count,
        case.recovery_count,
        originals,
        recoveries,
    )
    .unwrap();
    prepared_round_with_plan(decoder, case, recovery, start_delta, &plan);
}

/// Runs one prepared decode round with `plan` and checks it against ordinary decoding of the
/// same shards.
///
/// First checks that a plan with a different original count, a plan with one more recovery
/// index, and a plan with one provided recovery index swapped for an unprovided one each return
/// `Error::PlanMismatch` and leave the submitted shards usable. `case` must have at least two
/// recovery shards, so one stays unprovided for the second and third plans.
fn prepared_round_with_plan(
    decoder: &mut Decoder,
    case: &RateCase,
    recovery: &[Vec<u8>],
    start_delta: usize,
    plan: &Plan,
) {
    let missing = case.missing_originals(start_delta);
    let provided = case.provided_recoveries(start_delta);
    let mut reference =
        Decoder::new(case.original_count, case.recovery_count, case.shard_bytes).unwrap();
    for (index, shard) in case.originals.iter().enumerate() {
        if !missing[index] {
            decoder.add_original_shard(index, shard).unwrap();
            reference.add_original_shard(index, shard).unwrap();
        }
    }
    for (index, shard) in recovery.iter().enumerate() {
        if provided[index] {
            decoder.add_recovery_shard(index, shard).unwrap();
            reference.add_recovery_shard(index, shard).unwrap();
        }
    }

    // A wrong count and a changed mask must leave the submitted shards usable for a retry.
    let other_counts = Plan::new(
        case.original_count + 1,
        case.recovery_count,
        0..case.original_count + 1,
        [],
    )
    .unwrap();
    assert!(matches!(
        decoder.decode_with_plan(&other_counts),
        Err(Error::PlanMismatch)
    ));
    let additional = provided.iter().position(|present| !present).unwrap();
    let changed = Plan::new(
        case.original_count,
        case.recovery_count,
        (0..case.original_count).filter(|&i| !missing[i]),
        (0..case.recovery_count).filter(|&i| provided[i] || i == additional),
    )
    .unwrap();
    assert!(matches!(
        decoder.decode_with_recovery_plan(&changed),
        Err(Error::PlanMismatch)
    ));

    // A plan with the same counts and one provided recovery index swapped for an unprovided one
    // must also mismatch.
    let swapped = provided.iter().position(|&present| present).unwrap();
    let exchanged = Plan::new(
        case.original_count,
        case.recovery_count,
        (0..case.original_count).filter(|&i| !missing[i]),
        (0..case.recovery_count).filter(|&i| (provided[i] && i != swapped) || i == additional),
    )
    .unwrap();
    assert!(matches!(
        decoder.decode_with_recovery_plan(&exchanged),
        Err(Error::PlanMismatch)
    ));
    let result = decoder.decode_with_recovery_plan(plan).unwrap().unwrap();
    let ordinary = reference.decode_with_recovery().unwrap().unwrap();
    for (index, shard) in case.originals.iter().enumerate() {
        assert_eq!(result.original(index), ordinary.original(index));
        assert_eq!(
            result.original(index),
            missing[index].then_some(shard.as_slice())
        );
    }
    for (index, shard) in recovery.iter().enumerate() {
        assert_eq!(result.recovery(index), ordinary.recovery(index));
        assert_eq!(
            result.recovery(index),
            (!provided[index]).then_some(shard.as_slice())
        );
    }
}

/// Checks prepared recovery across decoder reuse: two rounds of `case_a`, a plan reused for
/// 2-byte shards, a plan with every original present, and two rounds of `case_b` after a reset.
fn exercise_prepared_reuse(case_a: &RateCase, case_b: &RateCase) {
    let recovery_a = encode::<DefaultRate<Naive>, _>(case_a, Naive::new());
    let recovery_b = encode::<DefaultRate<Naive>, _>(case_b, Naive::new());
    let mut decoder = Decoder::new(
        case_a.original_count,
        case_a.recovery_count,
        case_a.shard_bytes,
    )
    .unwrap();

    // Decode two received masks in sequence without a reset.
    prepared_round(&mut decoder, case_a, &recovery_a, 0);
    prepared_round(&mut decoder, case_a, &recovery_a, 1);

    // A plan does not depend on shard bytes. Reuse the first mask's plan for 2-byte shards.
    let missing = case_a.missing_originals(0);
    let provided = case_a.provided_recoveries(0);
    let shared = Plan::new(
        case_a.original_count,
        case_a.recovery_count,
        (0..case_a.original_count).filter(|&i| !missing[i]),
        (0..case_a.recovery_count).filter(|&i| provided[i]),
    )
    .unwrap();
    let mut short = case_a.clone();
    short.shard_bytes = 2;
    for original in &mut short.originals {
        original.truncate(2);
    }
    let short_recovery = encode::<DefaultRate<Naive>, _>(&short, Naive::new());
    decoder
        .reset(short.original_count, short.recovery_count, 2)
        .unwrap();
    prepared_round_with_plan(&mut decoder, &short, &short_recovery, 0, &shared);

    // A matching plan with every original present returns None.
    decoder
        .reset(
            case_a.original_count,
            case_a.recovery_count,
            case_a.shard_bytes,
        )
        .unwrap();
    let all = Plan::new(
        case_a.original_count,
        case_a.recovery_count,
        0..case_a.original_count,
        [],
    )
    .unwrap();
    for (i, original) in case_a.originals.iter().enumerate() {
        decoder.add_original_shard(i, original).unwrap();
    }
    assert!(decoder.decode_with_recovery_plan(&all).unwrap().is_none());

    // Reset to the second shape and decode both of its masks.
    decoder
        .reset(
            case_b.original_count,
            case_b.recovery_count,
            case_b.shard_bytes,
        )
        .unwrap();
    prepared_round(&mut decoder, case_b, &recovery_b, 0);
    prepared_round(&mut decoder, case_b, &recovery_b, 1);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reed_solomon::{engine::GF_BITS, rate::rate_low::DIRECT_EVALUATION_LIMIT};
    use commonware_invariants::minifuzz;

    /// Builds a `RateCase` whose shard bytes and erasure offsets derive from `seed`.
    fn fixed_case(
        original_count: usize,
        recovery_count: usize,
        shard_bytes: usize,
        seed: u8,
    ) -> RateCase {
        RateCase::new(
            original_count,
            recovery_count,
            shard_bytes,
            &mut Input::new(&[seed, seed.wrapping_mul(17), seed.wrapping_add(91)]),
        )
    }

    /// Checks that `E::eval_poly` matches `Naive` on `input` truncated at `truncated_size`.
    ///
    /// `_new_engine` only fits the `each_engine!` calling convention.
    fn compare_eval_poly<E: Engine>(
        input: &[GfElement; GF_ORDER],
        truncated_size: usize,
        _new_engine: fn() -> E,
    ) {
        let mut expected: Box<[GfElement; GF_ORDER]> =
            input.to_vec().into_boxed_slice().try_into().unwrap();
        Naive::eval_poly(expected.as_mut(), truncated_size);
        let mut actual: Box<[GfElement; GF_ORDER]> =
            input.to_vec().into_boxed_slice().try_into().unwrap();
        E::eval_poly(actual.as_mut(), truncated_size);
        assert_eq!(actual, expected);
    }

    #[test]
    fn minifuzz_derivative() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(64)
            .test(|u| EnginePlan::Derivative.run(u));
    }

    #[test]
    fn derivative_boundary_schedule() {
        // Chunk counts around 8 and 64 straddle the AVX-512 leaf guards, which Miri never
        // takes. Miri keeps every shard count with fewer chunk counts.
        let chunk_counts: &[usize] = if cfg!(miri) {
            &[0, 1, 8]
        } else {
            &[0, 1, 7, 8, 9, 63, 64, 65]
        };
        for count in [0, 1, 3, 4, 5, 15, 16, 17, 31, 32, 33, 64, 65] {
            for &chunks in chunk_counts {
                let mut input = vec![[0; SHARD_CHUNK_BYTES]; count * chunks];
                fill_chunks(&mut input, &mut Input::new(&[3, 0xff, 19, 0x80]));
                compare_derivative(&input, count, chunks);
            }
        }
    }

    #[test]
    fn minifuzz_mul() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(64)
            .test(|u| EnginePlan::Mul.run(u));
    }

    #[test]
    fn minifuzz_transform() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(64)
            .test(|u| EnginePlan::Transform.run(u));
    }

    #[test]
    fn minifuzz_engine_locator() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(32)
            .test(|u| EnginePlan::Locator.run(u));
    }

    #[test]
    fn locator_every_short_level() {
        // Build each cached short Walsh kernel and check it at the domain edges.
        for bits in 0..GF_BITS {
            let n = 1usize << bits;
            for end in [1, n / 2 + 1, n] {
                compare_short_locator(n, end, 0x5a5a);
            }
        }
    }

    #[test]
    fn minifuzz_rate_locator() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(32)
            .test(|u| RatePlan::Locator.run(u));
    }

    #[test]
    fn locator_domain_boundaries() {
        // Straddle several Walsh size changes and the switch to the full field above 32768. The
        // low-rate check calls `eval_walsh` directly, which ignores DIRECT_EVALUATION_LIMIT.
        for high in [false, true] {
            for end in [
                2, 3, 4, 7, 8, 15, 16, 127, 128, 129, 511, 512, 513, 32767, 32768, 32769, 65535,
                65536,
            ] {
                // Chunk size one places the other group immediately after it.
                let (original_count, recovery_count) =
                    if high { (end - 1, 1) } else { (1, end - 1) };
                for pattern in 0..3 {
                    let mut received = FixedBitSet::with_capacity(end);
                    for i in 0..end {
                        if pattern == 0 || (pattern == 1 && i % 3 != 0) {
                            received.insert(i);
                        }
                    }
                    compare_rate_locator(high, original_count, recovery_count, &received);
                }
            }
        }
    }

    #[test]
    fn minifuzz_rate_matches_naive() {
        for kind in [RateKind::High, RateKind::Low, RateKind::Default] {
            minifuzz::Builder::default()
                .with_seed(0)
                .with_search_limit(32)
                .test(|u| RatePlan::MatchesNaive(kind).run(u));
        }
    }

    #[test]
    fn minifuzz_rate_contract() {
        for kind in [RateKind::High, RateKind::Low, RateKind::Default] {
            minifuzz::Builder::default()
                .with_seed(0)
                .with_search_limit(32)
                .test(|u| RatePlan::Contract(kind).run(u));
        }
    }

    #[test]
    fn minifuzz_recovery() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(32)
            .test(|u| RatePlan::Recovery.run(u));
    }

    #[test]
    fn minifuzz_prepared_recovery() {
        minifuzz::Builder::default()
            .with_seed(0)
            .with_search_limit(32)
            .test(|u| RatePlan::PreparedRecovery.run(u));
    }

    #[test]
    fn prepared_received_mask_reuse() {
        // Reuse larger masks for both partial and full final words in each rate.
        for (original_count, recovery_count) in [(3, 5), (5, 3), (16, 48), (48, 16)] {
            let large = fixed_case(original_count * 4, recovery_count * 4, 2, 21);
            let small = fixed_case(original_count, recovery_count, 2, 23);
            exercise_prepared_reuse(&large, &small);
        }
    }

    #[test]
    fn prepared_low_locator_boundaries() {
        let high = fixed_case(11, 3, 2, 23);

        // Three originals pad to a chunk of 4, so the decoding domain ends at 4 + recovery_count.
        // Straddle DIRECT_EVALUATION_LIMIT, then take the transform path with a larger domain.
        let limit = DIRECT_EVALUATION_LIMIT - 4;
        for recovery_count in [limit - 1, limit, limit + 1, 510] {
            let low = fixed_case(3, recovery_count, 66, 21);
            exercise_prepared_reuse(&low, &high);
        }
    }

    #[test]
    fn engines_match_naive_on_field_and_log_edges() {
        let mut chunks = vec![[0; SHARD_CHUNK_BYTES]; 3];
        for (chunk_index, chunk) in chunks.iter_mut().enumerate() {
            let (low, high) = chunk.split_at_mut(SHARD_CHUNK_BYTES / 2);
            for lane in 0..SHARD_CHUNK_BYTES / 2 {
                let value = match (chunk_index * 32 + lane) % 8 {
                    0 => 0,
                    1 => 1,
                    2 => 0x00ff,
                    3 => 0x0100,
                    4 => 0x7fff,
                    5 => 0x8000,
                    6 => 0xfffe,
                    _ => 0xffff,
                };
                low[lane] = value as u8;
                high[lane] = (value >> 8) as u8;
            }
        }
        for log_m in [0, 1, 2, 0x7fff, 0x8000, 0xfffe, 0xffff] {
            compare_mul(&[], log_m);
            compare_mul(&chunks, log_m);
        }
        let mut multi_vector = vec![[0; SHARD_CHUNK_BYTES]; 65];
        fill_chunks(&mut multi_vector, &mut Input::new(&[3, 0xff, 19, 0x80, 0]));
        compare_mul(&multi_vector, 12_345);
    }

    #[test]
    fn log_tables_match_independent_field_multiplication() {
        let tables = super::super::engine::tables::get_exp_log();
        let mut coefficient = 1;
        for log_m in 0..=GF_MODULUS {
            if log_m == GF_MODULUS {
                assert_eq!(coefficient, 1, "generator did not close at GF_MODULUS");
            } else if log_m > 0 {
                assert_ne!(coefficient, 1, "generator cycled early at log {log_m}");
            }
            for shift in [0, 4, 8, 12] {
                let bit_products: [GfElement; 4] = core::array::from_fn(|bit| {
                    independent_mul_coefficient(1 << (shift + bit), coefficient)
                });
                for nibble in 0..16 {
                    let value = nibble << shift;
                    let expected = bit_products
                        .iter()
                        .enumerate()
                        .filter(|(bit, _)| nibble & (1 << bit) != 0)
                        .fold(0, |product, (_, bit_product)| product ^ bit_product);
                    assert_eq!(
                        super::super::engine::tables::mul(value, log_m, &tables.exp, &tables.log,),
                        expected,
                        "table mul differs for value={value} log={log_m}",
                    );
                }
            }
            coefficient = polynomial_mul(coefficient, 2);
        }
    }

    #[test]
    fn engines_match_naive_on_offset_truncated_and_skewed_transforms() {
        // Size-4 cases pick butterfly branches through `skew_delta`. The transform reads
        // `log_m01`, `log_m02`, and `log_m23` from `skew[skew_delta..skew_delta + 3]`, and
        // `skew` holds `GF_MODULUS` (a zero coefficient) exactly at indices `2^k - 1`.
        // `skew_delta` 4 zeroes none and reaches the fused AVX-512 path. 0 zeroes `log_m01`
        // and `log_m02`, 1 zeroes `log_m01` and `log_m23`, 2 zeroes `log_m02`, and 3 and 7
        // zero `log_m01`.
        for (pos, size, truncated_size, skew_delta, shard_chunks) in [
            (0, 1, 0, GF_ORDER - 1, 1),
            (1, 2, 1, 0, 2),
            (2, 4, 1, 3, 2),
            (3, 4, 3, 7, 1),
            (2, 8, 5, 31, 3),
            (5, 16, 16, GF_ORDER - 16, 2),
            (1, 4, 4, 4, 0),
            (1, 4, 0, 4, 1),
            (1, 4, 4, 0, 1),
            (2, 4, 3, 1, 65),
            (3, 4, 2, 2, 1),
            (2, 4, 4, 4, 65),
            (1, 16, 13, 0, 1),
            (2, 64, 37, 17, 17),
        ] {
            let shard_count = pos + size + 2;
            let mut input = vec![[0; SHARD_CHUNK_BYTES]; shard_count * shard_chunks];
            fill_chunks(&mut input, &mut Input::new(&[3, 0xff, 19, 0x80, 0]));
            compare_transform(
                &input,
                shard_count,
                shard_chunks,
                pos,
                size,
                truncated_size,
                skew_delta,
            );
        }
    }

    #[test]
    fn maximum_domain_transform_regression() {
        let mut input = vec![[0; SHARD_CHUNK_BYTES]; GF_ORDER];
        fill_chunks(&mut input, &mut Input::new(&[3, 0xff, 19, 0x80, 0]));
        compare_transform(&input, GF_ORDER, 1, 0, GF_ORDER, 1, 0);
    }

    #[test]
    fn optimized_eval_poly_matches_naive() {
        for truncated_size in [0, 1, 257] {
            let mut input: Box<[GfElement; GF_ORDER]> =
                vec![0; GF_ORDER].into_boxed_slice().try_into().unwrap();
            for (index, value) in input.iter_mut().enumerate().take(truncated_size) {
                *value = [0, 1, 2, 0x7fff, 0x8000, 0xfffe, 0xffff][index % 7];
            }
            each_engine!(compare_eval_poly(&input, truncated_size));
        }
    }

    #[test]
    fn rate_engines_match_naive_with_partial_chunks_and_mixed_erasures() {
        let high_a = fixed_case(11, 3, 66, 3);
        let high_b = fixed_case(9, 4, 130, 5);
        each_engine!(rate_matches_naive(RateKind::High, &high_a, &high_b));

        let low_a = fixed_case(3, 11, 126, 7);
        let low_b = fixed_case(4, 9, 62, 11);
        each_engine!(rate_matches_naive(RateKind::Low, &low_a, &low_b));
    }

    #[test]
    fn default_rate_switches_and_recovery_decoder_reuses_work() {
        let high = fixed_case(11, 3, 66, 13);
        let low = fixed_case(3, 11, 130, 17);
        each_engine!(rate_matches_naive(RateKind::Default, &high, &low));
        exercise_recovery_reuse(&high, &low);
    }
}