fgf 3.0.0

Faster Galois Fields: SIMD and portable finite-field arithmetic (binary towers and prime fields) and vector kernels
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
//! GF(2^8) kernel dispatch for every degree-eight representation.
//!
//! Every degree-eight representation under [`Binary`] shares one dispatch: the
//! split-nibble scalar operations every architecture backend uses for
//! sub-lane tails, and one routing table that diverges by representation
//! only where the hardware does. [`Gf8Data`](crate::kernel::tables::Gf8Data)
//! carries what dispatch needs — AES nativity, the compile-time banks, the
//! isomorphism onto the AES field, and the reduction byte — and
//! [`Prepared`] is what survives below it: a nibble table and an affine map
//! qword, with no representation type attached. `GF2P8MULB` implements only
//! the AES field, so a native representation takes the `GF2P8MULB` bodies
//! and every other one its `VGF2P8AFFINEQB` map; each
//! `R::AES_NATIVE` comparison below is a constant that folds away in each
//! monomorphization, selecting between two non-generic kernel bodies.
//!
//! The raw-element operations resolve their coefficients against `R`'s banks
//! on access through the providers in this module, so they stay
//! allocation-free while no kernel body depends on `R`.
//!
//! # Backends
//!
//! Degree-eight towers join this dispatch through placeholder banks: they
//! report `Scalar` and route every bulk operation through the typed scalar
//! fallback. `AArch64` and Wasm use representation-specific nibble tables for
//! single-row scaling. Their blocked and elementwise routes serve only
//! AES-native representations; the others compose the single-row kernels.

use crate::field::{Binary, Elem};
use crate::kernel::tables::{Gf8Data, ScaleTable};
use crate::kernel::{Backend, FieldKernels, KernelDispatch, RawDispatch, backend, scalar};

#[cfg(all(feature = "simd", target_arch = "aarch64"))]
use crate::kernel::aarch64;
#[cfg(all(feature = "simd", target_arch = "wasm32"))]
use crate::kernel::wasm32;
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
use crate::kernel::x86;

// --------------------------------------------------------------------------
// Shared preparation and portable tails.
// --------------------------------------------------------------------------

/// `dst ^= coeff * src` via split-nibble lookup.
///
/// Two table reads and two XORs per byte, versus a log/antilog pair with a
/// modular reduction. Also the tail handler for every vector kernel, which is
/// why it takes the already-built table rather than a coefficient.
pub fn mul_add_nibble(dst: &mut [u8], table: &ScaleTable, src: &[u8]) {
    debug_assert_eq!(dst.len(), src.len());
    for (d, &s) in dst.iter_mut().zip(src) {
        *d ^= table.lo[(s & 0x0f) as usize] ^ table.hi[(s >> 4) as usize];
    }
}

/// `dst *= coeff` via split-nibble lookup.
pub fn mul_assign_nibble(dst: &mut [u8], table: &ScaleTable) {
    for d in dst.iter_mut() {
        *d = table.lo[(*d & 0x0f) as usize] ^ table.hi[(*d >> 4) as usize];
    }
}

/// `dst = coeff * src` via split-nibble lookup.
///
/// Tail handler for the fused out-of-place kernels, mirroring
/// [`mul_add_nibble`].
pub fn mul_into_nibble(dst: &mut [u8], table: &ScaleTable, src: &[u8]) {
    debug_assert_eq!(dst.len(), src.len());
    for (d, &s) in dst.iter_mut().zip(src) {
        *d = table.lo[(s & 0x0f) as usize] ^ table.hi[(s >> 4) as usize];
    }
}

/// The backend-ready form of a GF(2^8) coefficient, representation-erased.
///
/// One preparation gives every backend what it can consume: the affine GFNI
/// and AVX-512 kernels read the `VGF2P8AFFINEQB` matrix qword, the native
/// AES kernels the coefficient byte, and the shuffle backends and sub-lane
/// scalar tails the nibble tables. Every entry comes from the
/// representation's compile-time banks, so no kernel recovers a field fact
/// from the coefficient byte — or from a representation type: `Prepared` is
/// the plain value the hot bodies see.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Prepared {
    pub(crate) table: &'static ScaleTable,
    pub(crate) affine: u64,
}

impl Prepared {
    /// Resolve `coeff` against its representation's banks.
    ///
    /// The representation is inferred from the element; callers never name
    /// the crate-private bank facet.
    #[allow(private_bounds)]
    #[inline]
    #[must_use]
    pub fn new<R: Gf8Data>(coeff: Elem<Binary<8, R>>) -> Self {
        let raw = coeff.to_raw() as usize;
        Self {
            table: &R::SCALE[raw],
            affine: R::AFFINE[raw],
        }
    }

    /// The raw coefficient byte this was built from.
    #[allow(dead_code)]
    #[inline]
    #[must_use]
    pub fn byte(self) -> u8 {
        self.table.coeff
    }

    /// The zero coefficient's prepared form: an all-zero map and table. The
    /// staging arrays the blocked scatter kernels fill group-wise start here;
    /// every slot is overwritten before it is read.
    #[allow(dead_code)]
    #[inline]
    pub(crate) fn zero() -> Self {
        Self {
            table: &crate::kernel::tables::ZERO_TABLE,
            affine: 0,
        }
    }

    /// Whether the coefficient is zero: the case the blocked scatter staging
    /// drops before grouping.
    #[allow(dead_code)]
    #[inline]
    pub(crate) fn is_zero(self) -> bool {
        self.table.coeff == 0
    }
}

/// Random access to resolved coefficients for the blocked scatter and gather
/// entries.
///
/// A prepared call holds its coefficients as a `[Prepared]` slice; the
/// dispatch layer's raw-element operations resolve each element against its
/// representation's banks on access, which keeps those one-shot forms
/// allocation-free without making any kernel body depend on the
/// representation. Hot loops that need one facet of a coefficient read it
/// through [`Coeffs::byte`], [`Coeffs::affine`] or [`Coeffs::table`], so a
/// raw-element provider answers from the element itself instead of building
/// a full [`Prepared`].
#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
pub(crate) trait Coeffs {
    /// Number of coefficients.
    fn count(&self) -> usize;
    /// The prepared form of coefficient `index`.
    fn resolved(&self, index: usize) -> Prepared;
    /// The raw byte of coefficient `index`: the native AES multiplier and
    /// the zero test.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[inline]
    fn byte(&self, index: usize) -> u8 {
        self.resolved(index).byte()
    }
    /// The `VGF2P8AFFINEQB` matrix qword of coefficient `index`.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[inline]
    fn affine(&self, index: usize) -> u64 {
        self.resolved(index).affine
    }
    /// The nibble tables of coefficient `index`, for sub-lane scalar tails.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[inline]
    fn table(&self, index: usize) -> &'static ScaleTable {
        self.resolved(index).table
    }
}

#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
impl Coeffs for [Prepared] {
    #[inline]
    fn count(&self) -> usize {
        self.len()
    }

    #[inline]
    fn resolved(&self, index: usize) -> Prepared {
        self[index]
    }
}

#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
impl Coeffs for alloc::vec::Vec<Prepared> {
    #[inline]
    fn count(&self) -> usize {
        self.len()
    }

    #[inline]
    fn resolved(&self, index: usize) -> Prepared {
        self[index]
    }
}

#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
impl<const N: usize> Coeffs for [Prepared; N] {
    #[inline]
    fn count(&self) -> usize {
        N
    }

    #[inline]
    fn resolved(&self, index: usize) -> Prepared {
        self[index]
    }
}

/// Raw elements resolved against their representation's banks on access.
#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
struct Resolved<'a, R: Gf8Data>(&'a [Elem<Binary<8, R>>]);

#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
impl<R: Gf8Data> Coeffs for Resolved<'_, R> {
    #[inline]
    fn count(&self) -> usize {
        self.0.len()
    }

    #[inline]
    fn resolved(&self, index: usize) -> Prepared {
        Prepared::new(self.0[index])
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[inline]
    fn byte(&self, index: usize) -> u8 {
        self.0[index].to_raw()
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[inline]
    fn affine(&self, index: usize) -> u64 {
        R::AFFINE[self.0[index].to_raw() as usize]
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    #[inline]
    fn table(&self, index: usize) -> &'static ScaleTable {
        &R::SCALE[self.0[index].to_raw() as usize]
    }
}

/// Raw per-term elements as a [`Matrix`](crate::kernel::Matrix) of prepared
/// coefficients, resolved on access.
///
/// The public operation has already checked that every term supplies one
/// coefficient per destination row; an out-of-range access still panics
/// through slice indexing.
#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
// The unified element spelling nests over the term geometry here.
// Term slices stay slices so the dispatch layer serves raw-coefficient
// operations without allocating.
#[allow(clippy::type_complexity)]
struct TermsResolved<'a, R: Gf8Data>(&'a [(&'a [Elem<Binary<8, R>>], &'a [u8])]);

#[cfg(all(
    feature = "simd",
    any(
        target_arch = "x86",
        target_arch = "x86_64",
        target_arch = "aarch64",
        target_arch = "wasm32"
    )
))]
impl<R: Gf8Data> crate::kernel::Matrix<Prepared> for TermsResolved<'_, R> {
    #[inline]
    fn len(&self) -> usize {
        self.0.len()
    }

    #[inline]
    fn coefficient(&self, term: usize, row: usize) -> Prepared {
        Prepared::new(self.0[term].0[row])
    }

    #[inline]
    fn source(&self, term: usize) -> &[u8] {
        self.0[term].1
    }
}

/// Raw flat row-major elements as a [`Matrix`](crate::kernel::Matrix) of
/// prepared coefficients, resolved on access.
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
struct FlatResolved<'a, R: Gf8Data> {
    values: &'a [Elem<Binary<8, R>>],
    nrows: usize,
    sources: &'a [&'a [u8]],
}

#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
impl<R: Gf8Data> crate::kernel::Matrix<Prepared> for FlatResolved<'_, R> {
    #[inline]
    fn len(&self) -> usize {
        self.sources.len()
    }

    #[inline]
    fn coefficient(&self, term: usize, row: usize) -> Prepared {
        Prepared::new(self.values[term * self.nrows + row])
    }

    #[inline]
    fn source(&self, term: usize) -> &[u8] {
        self.sources[term]
    }
}

impl<R: Gf8Data> FieldKernels for Binary<8, R> {
    #[inline]
    fn backend() -> Backend {
        // Non-polynomial presentations — ordered bases and towers — never
        // touch the vector units: every bulk operation, including tails,
        // runs the typed scalar fallback, so polynomial arithmetic must not
        // observe their coordinates.
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return Backend::Scalar;
        }
        backend()
    }

    #[inline]
    fn has_vector_elementwise() -> bool {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return false;
        }
        match backend() {
            Backend::V4x | Backend::V3GfniCrypto | Backend::V3 | Backend::V2 => {
                cfg!(all(
                    feature = "simd",
                    any(target_arch = "x86", target_arch = "x86_64")
                ))
            }
            Backend::NeonAes | Backend::Neon | Backend::Wasm128 => R::AES_NATIVE,
            _ => false,
        }
    }
}

impl<R: Gf8Data> KernelDispatch for Binary<8, R> {
    type Prepared = Prepared;

    #[inline]
    fn prepare(_proof: RawDispatch, coeff: Elem<Binary<8, R>>) -> Self::Prepared {
        Prepared::new(coeff)
    }

    #[inline]
    fn prepared_coeff(_proof: RawDispatch, prepared: &Self::Prepared) -> Elem<Binary<8, R>> {
        <Elem<Binary<8, R>>>::from_raw(prepared.table.coeff)
    }

    #[inline]
    fn add_assign(_proof: RawDispatch, dst: &mut [u8], src: &[u8]) {
        crate::kernel::xor(dst, src);
    }

    #[inline]
    fn add_gather_offsets(_proof: RawDispatch, region: &[u8], dst: &mut [u8], offsets: &[u32]) {
        crate::kernel::xor_gather(region, dst, offsets);
    }

    #[inline]
    fn sub_assign(_proof: RawDispatch, dst: &mut [u8], src: &[u8]) {
        crate::kernel::xor(dst, src);
    }

    fn mul_add(_proof: RawDispatch, dst: &mut [u8], coeff: &Self::Prepared, src: &[u8]) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            let coeff = <Elem<Binary<8, R>>>::from_raw(coeff.table.coeff);
            return scalar::mul_add::<Binary<8, R>>(dst, coeff, src);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                x86::gf8::mul_add_avx512(crate::kernel::x86_v4x_token(), dst, *coeff, src);
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_add_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        *coeff,
                        src,
                    );
                } else {
                    x86::gf8::mul_add_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        *coeff,
                        src,
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 => {
                x86::gf8::mul_add_avx2(crate::kernel::x86_v3_token(), dst, coeff.table, src);
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 => {
                x86::gf8::mul_add_ssse3(crate::kernel::x86_v2_token(), dst, coeff.table, src);
            }
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon | Backend::NeonAes => {
                aarch64::gf8::mul_add_neon(crate::kernel::neon_token(), dst, coeff.table, src);
            }
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 => {
                wasm32::gf8::mul_add_simd128(crate::kernel::wasm128_token(), dst, coeff.table, src);
            }
            _ => mul_add_nibble(dst, coeff.table, src),
        }
    }

    fn mul_assign(_proof: RawDispatch, dst: &mut [u8], coeff: &Self::Prepared) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            let coeff = <Elem<Binary<8, R>>>::from_raw(coeff.table.coeff);
            return scalar::mul_assign::<Binary<8, R>>(dst, coeff);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                x86::gf8::mul_assign_avx512(crate::kernel::x86_v4x_token(), dst, *coeff);
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                // The affine in-place form selects the nibble shuffle for
                // long buffers; the native AES multiply leads at every size.
                if R::AES_NATIVE {
                    x86::gf8::mul_assign_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        *coeff,
                    );
                } else if dst.len() < 65_536 {
                    x86::gf8::mul_assign_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        *coeff,
                    );
                } else {
                    x86::gf8::mul_assign_avx2(crate::kernel::x86_v3_token(), dst, coeff.table);
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 => {
                x86::gf8::mul_assign_avx2(crate::kernel::x86_v3_token(), dst, coeff.table);
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 => {
                x86::gf8::mul_assign_ssse3(crate::kernel::x86_v2_token(), dst, coeff.table);
            }
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon | Backend::NeonAes => {
                aarch64::gf8::mul_assign_neon(crate::kernel::neon_token(), dst, coeff.table);
            }
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 => {
                wasm32::gf8::mul_assign_simd128(crate::kernel::wasm128_token(), dst, coeff.table);
            }
            _ => mul_assign_nibble(dst, coeff.table),
        }
    }

    fn mul_into(_proof: RawDispatch, dst: &mut [u8], coeff: &Self::Prepared, src: &[u8]) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            let coeff = <Elem<Binary<8, R>>>::from_raw(coeff.table.coeff);
            dst.copy_from_slice(src);
            return scalar::mul_assign::<Binary<8, R>>(dst, coeff);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                // Past the streaming threshold the destination will not be
                // read back soon: the AVX2 non-temporal body owns that shape.
                // Below it the 64-byte temporal form leads.
                if crate::kernel::x86::wants_nt_store(dst.len()) {
                    if R::AES_NATIVE {
                        x86::gf8::mul_into_gfni::<true>(
                            crate::kernel::x86_v3_gfni_token(),
                            dst,
                            *coeff,
                            src,
                        );
                    } else {
                        x86::gf8::mul_into_gfni::<false>(
                            crate::kernel::x86_v3_gfni_token(),
                            dst,
                            *coeff,
                            src,
                        );
                    }
                } else {
                    x86::gf8::mul_into_avx512(crate::kernel::x86_v4x_token(), dst, *coeff, src);
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_into_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        *coeff,
                        src,
                    );
                } else {
                    x86::gf8::mul_into_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        *coeff,
                        src,
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 => {
                x86::gf8::mul_into_avx2(crate::kernel::x86_v3_token(), dst, coeff.table, src);
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 => {
                x86::gf8::mul_into_ssse3(crate::kernel::x86_v2_token(), dst, coeff.table, src);
            }
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon | Backend::NeonAes => {
                aarch64::gf8::mul_into_neon(crate::kernel::neon_token(), dst, coeff.table, src);
            }
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 => {
                wasm32::gf8::mul_into_simd128(
                    crate::kernel::wasm128_token(),
                    dst,
                    coeff.table,
                    src,
                );
            }
            _ => mul_into_nibble(dst, coeff.table, src),
        }
    }

    fn mul_add_scatter(
        _proof: RawDispatch,
        rows: &mut [u8],
        row_len: usize,
        coeffs: &[Elem<Binary<8, R>>],
        src: &[u8],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_add_scatter::<Binary<8, R>>(rows, row_len, coeffs, src);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => x86::gf8::mul_add_scatter_avx512(
                crate::kernel::x86_v4x_token(),
                rows,
                row_len,
                &Resolved(coeffs),
                src,
            ),
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_add_scatter_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        rows,
                        row_len,
                        &Resolved(coeffs),
                        src,
                    );
                } else {
                    x86::gf8::mul_add_scatter_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        rows,
                        row_len,
                        &Resolved(coeffs),
                        src,
                    );
                }
            }
            // The blocked shuffle scatter is measured for the AES encoding;
            // other representations compose the single-coefficient kernel
            // per row.
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 if R::AES_NATIVE => x86::gf8::mul_add_scatter_avx2(
                crate::kernel::x86_v3_token(),
                rows,
                row_len,
                &Resolved(coeffs),
                src,
            ),
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 if R::AES_NATIVE => x86::gf8::mul_add_scatter_ssse3(
                crate::kernel::x86_v2_token(),
                rows,
                row_len,
                &Resolved(coeffs),
                src,
            ),
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon | Backend::NeonAes if R::AES_NATIVE => {
                aarch64::gf8::mul_add_scatter_neon(
                    crate::kernel::neon_token(),
                    rows,
                    row_len,
                    &Resolved(coeffs),
                    src,
                )
            }
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 if R::AES_NATIVE => wasm32::gf8::mul_add_scatter_simd128(
                crate::kernel::wasm128_token(),
                rows,
                row_len,
                &Resolved(coeffs),
                src,
            ),
            Backend::Scalar => scalar::mul_add_scatter::<Self>(rows, row_len, coeffs, src),
            _ => {
                for (row, &coeff) in rows.chunks_exact_mut(row_len).zip(coeffs) {
                    Self::mul_add(RawDispatch, row, &Prepared::new(coeff), src);
                }
            }
        }
    }

    #[cfg(feature = "alloc")]
    fn mul_add_scatter_plan(
        _proof: RawDispatch,
        rows: &mut [u8],
        row_len: usize,
        values: &[Elem<Binary<8, R>>],
        coeffs: &[Self::Prepared],
        src: &[u8],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_add_scatter::<Binary<8, R>>(rows, row_len, values, src);
        }
        let _ = (values, coeffs);
        // Prepared affine scatter: the grouped rows read stored maps instead
        // of re-deriving them per row.
        #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
        if !R::AES_NATIVE && Self::backend() == Backend::V4x {
            return x86::gf8::mul_add_scatter_avx512(
                crate::kernel::x86_v4x_token(),
                rows,
                row_len,
                coeffs,
                src,
            );
        }
        Self::mul_add_scatter(RawDispatch, rows, row_len, values, src);
    }

    fn mul_add_gather(
        _proof: RawDispatch,
        dst: &mut [u8],
        coeffs: &[Elem<Binary<8, R>>],
        srcs: &[&[u8]],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_add_gather::<Binary<8, R>>(dst, coeffs, srcs);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                x86::gf8::mul_add_gather_avx512(
                    crate::kernel::x86_v4x_token(),
                    dst,
                    &Resolved(coeffs),
                    srcs,
                );
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_add_gather_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        &Resolved(coeffs),
                        srcs,
                    );
                } else {
                    x86::gf8::mul_add_gather_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        &Resolved(coeffs),
                        srcs,
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 if R::AES_NATIVE => {
                x86::gf8::mul_add_gather_avx2(
                    crate::kernel::x86_v3_token(),
                    dst,
                    &Resolved(coeffs),
                    srcs,
                );
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 if R::AES_NATIVE => {
                x86::gf8::mul_add_gather_ssse3(
                    crate::kernel::x86_v2_token(),
                    dst,
                    &Resolved(coeffs),
                    srcs,
                );
            }
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon | Backend::NeonAes if R::AES_NATIVE => aarch64::gf8::mul_add_gather_neon(
                crate::kernel::neon_token(),
                dst,
                &Resolved(coeffs),
                srcs,
            ),
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 if R::AES_NATIVE => wasm32::gf8::mul_add_gather_simd128(
                crate::kernel::wasm128_token(),
                dst,
                &Resolved(coeffs),
                srcs,
            ),
            Backend::Scalar => scalar::mul_add_gather::<Self>(dst, coeffs, srcs),
            _ => {
                for (&coeff, &src) in coeffs.iter().zip(srcs) {
                    Self::mul_add(RawDispatch, dst, &Prepared::new(coeff), src);
                }
            }
        }
    }

    #[cfg(feature = "alloc")]
    fn mul_add_gather_plan(
        _proof: RawDispatch,
        dst: &mut [u8],
        values: &[Elem<Binary<8, R>>],
        coeffs: &[Self::Prepared],
        srcs: &[&[u8]],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_add_gather::<Binary<8, R>>(dst, values, srcs);
        }
        let _ = (values, coeffs);
        // Prepared affine gather: the blocked tile reads stored maps instead
        // of re-deriving them per tile.
        #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
        if !R::AES_NATIVE && Self::backend() == Backend::V4x {
            return x86::gf8::mul_add_gather_avx512(
                crate::kernel::x86_v4x_token(),
                dst,
                coeffs,
                srcs,
            );
        }
        Self::mul_add_gather(RawDispatch, dst, values, srcs);
    }

    fn mul_into_gather(
        _proof: RawDispatch,
        dst: &mut [u8],
        coeffs: &[Elem<Binary<8, R>>],
        srcs: &[&[u8]],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            let mut pairs = coeffs.iter().copied().zip(srcs.iter().copied());
            let Some((first, src)) = pairs.next() else {
                dst.fill(0);
                return;
            };
            Self::mul_into(RawDispatch, dst, &Prepared::new(first), src);
            for (coeff, src) in pairs {
                Self::mul_add(RawDispatch, dst, &Prepared::new(coeff), src);
            }
            return;
        }
        // One destination pass instead of the fill-then-accumulate pair: the
        // one-row overwrite matrix holds the tile in registers from zero.
        #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
        {
            let terms = FlatResolved {
                values: coeffs,
                nrows: 1,
                sources: srcs,
            };
            match Self::backend() {
                #[cfg(feature = "simd512")]
                Backend::V4x => {
                    return x86::gf8::mul_into_matrix_avx512_with(
                        crate::kernel::x86_v4x_token(),
                        dst,
                        dst.len(),
                        1,
                        &terms,
                    );
                }
                Backend::V3GfniCrypto if !R::AES_NATIVE => {
                    return x86::gf8::mul_into_matrix_gfni_with::<false, _>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        dst.len(),
                        1,
                        &terms,
                    );
                }
                _ => {}
            }
        }
        if R::AES_NATIVE {
            dst.fill(0);
            Self::mul_add_gather(RawDispatch, dst, coeffs, srcs);
            return;
        }
        let mut pairs = coeffs.iter().copied().zip(srcs.iter().copied());
        let Some((first, src)) = pairs.next() else {
            dst.fill(0);
            return;
        };
        Self::mul_into(RawDispatch, dst, &Prepared::new(first), src);
        for (coeff, src) in pairs {
            Self::mul_add(RawDispatch, dst, &Prepared::new(coeff), src);
        }
    }

    #[cfg(feature = "alloc")]
    fn mul_into_gather_plan(
        _proof: RawDispatch,
        dst: &mut [u8],
        values: &[Elem<Binary<8, R>>],
        coeffs: &[Self::Prepared],
        srcs: &[&[u8]],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return Self::mul_into_gather_with(RawDispatch, dst, coeffs, srcs);
        }
        if R::AES_NATIVE {
            return Self::mul_into_gather(RawDispatch, dst, values, srcs);
        }
        // Prepared overwrite gather: one destination pass through the
        // one-row overwrite matrix instead of the trait default's
        // into-then-AXPY shape.
        #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
        match Self::backend() {
            #[cfg(feature = "simd512")]
            Backend::V4x => {
                let terms = crate::kernel::FlatMatrix {
                    coefficients: coeffs,
                    nrows: 1,
                    sources: srcs,
                };
                return x86::gf8::mul_into_matrix_avx512_with(
                    crate::kernel::x86_v4x_token(),
                    dst,
                    dst.len(),
                    1,
                    &terms,
                );
            }
            Backend::V3GfniCrypto => {
                let terms = FlatResolved {
                    values,
                    nrows: 1,
                    sources: srcs,
                };
                return x86::gf8::mul_into_matrix_gfni_with::<false, _>(
                    crate::kernel::x86_v3_gfni_token(),
                    dst,
                    dst.len(),
                    1,
                    &terms,
                );
            }
            _ => {}
        }
        Self::mul_into_gather_with(RawDispatch, dst, coeffs, srcs);
    }

    fn mul_add_matrix(
        _proof: RawDispatch,
        rows: &mut [u8],
        row_len: usize,
        nrows: usize,
        terms: &[(&[Elem<Binary<8, R>>], &[u8])],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_add_matrix::<Binary<8, R>>(rows, row_len, nrows, terms);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                x86::gf8::mul_add_matrix_avx512_with(
                    crate::kernel::x86_v4x_token(),
                    rows,
                    row_len,
                    nrows,
                    &TermsResolved(terms),
                );
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_add_matrix_gfni_with::<true, _>(
                        crate::kernel::x86_v3_gfni_token(),
                        rows,
                        row_len,
                        nrows,
                        &TermsResolved(terms),
                    );
                } else {
                    x86::gf8::mul_add_matrix_gfni_with::<false, _>(
                        crate::kernel::x86_v3_gfni_token(),
                        rows,
                        row_len,
                        nrows,
                        &TermsResolved(terms),
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 if R::AES_NATIVE => {
                x86::gf8::mul_add_matrix_avx2_with(
                    crate::kernel::x86_v3_token(),
                    rows,
                    row_len,
                    nrows,
                    &TermsResolved(terms),
                );
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 if R::AES_NATIVE => {
                x86::gf8::mul_add_matrix_ssse3_with(
                    crate::kernel::x86_v2_token(),
                    rows,
                    row_len,
                    nrows,
                    &TermsResolved(terms),
                );
            }
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon | Backend::NeonAes if R::AES_NATIVE => {
                aarch64::gf8::mul_add_matrix_neon(
                    crate::kernel::neon_token(),
                    rows,
                    row_len,
                    nrows,
                    &TermsResolved(terms),
                );
            }
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 if R::AES_NATIVE => {
                wasm32::gf8::mul_add_matrix_simd128(
                    crate::kernel::wasm128_token(),
                    rows,
                    row_len,
                    nrows,
                    &TermsResolved(terms),
                );
            }
            Backend::Scalar => scalar::mul_add_matrix::<Self>(rows, row_len, nrows, terms),
            _ => {
                for &(coeffs, src) in terms {
                    for (row, &coeff) in rows.chunks_exact_mut(row_len).take(nrows).zip(coeffs) {
                        Self::mul_add(RawDispatch, row, &Prepared::new(coeff), src);
                    }
                }
            }
        }
    }

    #[cfg(feature = "alloc")]
    fn mul_add_matrix_plan(
        _proof: RawDispatch,
        rows: &mut [u8],
        row_len: usize,
        nrows: usize,
        values: &[Elem<Binary<8, R>>],
        coeffs: &[Self::Prepared],
        srcs: &[&[u8]],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            for (term, &src) in srcs.iter().enumerate() {
                let start = term * nrows;
                for (row, &coeff) in rows
                    .chunks_exact_mut(row_len)
                    .take(nrows)
                    .zip(&values[start..start + nrows])
                {
                    scalar::mul_add::<Binary<8, R>>(row, coeff, src);
                }
            }
            return;
        }
        #[cfg(not(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64"))))]
        let _ = values;
        #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
        {
            let terms = FlatResolved {
                values,
                nrows,
                sources: srcs,
            };
            match Self::backend() {
                // The prepared tables carry their affine maps, so the blocked
                // 64-byte bodies read them directly.
                #[cfg(feature = "simd512")]
                Backend::V4x => {
                    let prepared = crate::kernel::FlatMatrix {
                        coefficients: coeffs,
                        nrows,
                        sources: srcs,
                    };
                    return x86::gf8::mul_add_matrix_avx512_with(
                        crate::kernel::x86_v4x_token(),
                        rows,
                        row_len,
                        nrows,
                        &prepared,
                    );
                }
                Backend::V3GfniCrypto => {
                    if R::AES_NATIVE {
                        return x86::gf8::mul_add_matrix_gfni_with::<true, _>(
                            crate::kernel::x86_v3_gfni_token(),
                            rows,
                            row_len,
                            nrows,
                            &terms,
                        );
                    }
                    return x86::gf8::mul_add_matrix_gfni_with::<false, _>(
                        crate::kernel::x86_v3_gfni_token(),
                        rows,
                        row_len,
                        nrows,
                        &terms,
                    );
                }
                Backend::V3 if R::AES_NATIVE => {
                    return x86::gf8::mul_add_matrix_avx2_with(
                        crate::kernel::x86_v3_token(),
                        rows,
                        row_len,
                        nrows,
                        &terms,
                    );
                }
                Backend::V2 if R::AES_NATIVE => {
                    return x86::gf8::mul_add_matrix_ssse3_with(
                        crate::kernel::x86_v2_token(),
                        rows,
                        row_len,
                        nrows,
                        &terms,
                    );
                }
                _ => {}
            }
        }
        for (term, &src) in srcs.iter().enumerate() {
            let start = term * nrows;
            for (row, coeff) in rows
                .chunks_exact_mut(row_len)
                .take(nrows)
                .zip(&coeffs[start..start + nrows])
            {
                Self::mul_add(RawDispatch, row, coeff, src);
            }
        }
    }

    fn mul_into_matrix(
        _proof: RawDispatch,
        rows: &mut [u8],
        row_len: usize,
        nrows: usize,
        terms: &[(&[Elem<Binary<8, R>>], &[u8])],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            for row in rows.chunks_exact_mut(row_len).take(nrows) {
                row.fill(0);
            }
            return scalar::mul_add_matrix::<Binary<8, R>>(rows, row_len, nrows, terms);
        }
        // Overwrite seeds accumulators from zero in registers: one write pass,
        // no destination read, no separate fill.
        #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
        if Self::backend() == Backend::V4x {
            return x86::gf8::mul_into_matrix_avx512_with(
                crate::kernel::x86_v4x_token(),
                rows,
                row_len,
                nrows,
                &TermsResolved(terms),
            );
        }
        #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
        if Self::backend() == Backend::V3GfniCrypto {
            if R::AES_NATIVE {
                return x86::gf8::mul_into_matrix_gfni_with::<true, _>(
                    crate::kernel::x86_v3_gfni_token(),
                    rows,
                    row_len,
                    nrows,
                    &TermsResolved(terms),
                );
            }
            return x86::gf8::mul_into_matrix_gfni_with::<false, _>(
                crate::kernel::x86_v3_gfni_token(),
                rows,
                row_len,
                nrows,
                &TermsResolved(terms),
            );
        }
        for row in rows.chunks_exact_mut(row_len).take(nrows) {
            row.fill(0);
        }
        Self::mul_add_matrix(RawDispatch, rows, row_len, nrows, terms);
    }

    #[cfg(feature = "alloc")]
    fn mul_into_matrix_plan(
        _proof: RawDispatch,
        rows: &mut [u8],
        row_len: usize,
        nrows: usize,
        values: &[Elem<Binary<8, R>>],
        coeffs: &[Self::Prepared],
        srcs: &[&[u8]],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            for row in rows.chunks_exact_mut(row_len).take(nrows) {
                row.fill(0);
            }
            for (term, &src) in srcs.iter().enumerate() {
                let start = term * nrows;
                for (row, &coeff) in rows
                    .chunks_exact_mut(row_len)
                    .take(nrows)
                    .zip(&values[start..start + nrows])
                {
                    scalar::mul_add::<Binary<8, R>>(row, coeff, src);
                }
            }
            return;
        }
        #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
        if Self::backend() == Backend::V4x {
            let prepared = crate::kernel::FlatMatrix {
                coefficients: coeffs,
                nrows,
                sources: srcs,
            };
            return x86::gf8::mul_into_matrix_avx512_with(
                crate::kernel::x86_v4x_token(),
                rows,
                row_len,
                nrows,
                &prepared,
            );
        }
        #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
        if Self::backend() == Backend::V3GfniCrypto {
            let terms = FlatResolved {
                values,
                nrows,
                sources: srcs,
            };
            if R::AES_NATIVE {
                return x86::gf8::mul_into_matrix_gfni_with::<true, _>(
                    crate::kernel::x86_v3_gfni_token(),
                    rows,
                    row_len,
                    nrows,
                    &terms,
                );
            }
            return x86::gf8::mul_into_matrix_gfni_with::<false, _>(
                crate::kernel::x86_v3_gfni_token(),
                rows,
                row_len,
                nrows,
                &terms,
            );
        }
        for row in rows.chunks_exact_mut(row_len).take(nrows) {
            row.fill(0);
        }
        Self::mul_add_matrix_plan(RawDispatch, rows, row_len, nrows, values, coeffs, srcs);
    }

    fn mul_add_matrix_at(
        _proof: RawDispatch,
        dst: &mut [u8],
        row_len: usize,
        row_starts: &[usize],
        terms: &[(&[Elem<Binary<8, R>>], &[u8])],
    ) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return Self::mul_add_matrix_at_rows(RawDispatch, dst, row_len, row_starts, terms);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => x86::gf8::mul_add_matrix_at_avx512_with(
                crate::kernel::x86_v4x_token(),
                dst,
                row_len,
                row_starts,
                &TermsResolved(terms),
            ),
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_add_matrix_at_gfni_with::<true, _>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        row_len,
                        row_starts,
                        &TermsResolved(terms),
                    );
                } else {
                    x86::gf8::mul_add_matrix_at_gfni_with::<false, _>(
                        crate::kernel::x86_v3_gfni_token(),
                        dst,
                        row_len,
                        row_starts,
                        &TermsResolved(terms),
                    );
                }
            }
            // The shuffle and non-x86 backends have no blocked scattered
            // kernel.
            _ => Self::mul_add_matrix_at_rows(RawDispatch, dst, row_len, row_starts, terms),
        }
    }

    fn mul_elementwise(_proof: RawDispatch, dst: &mut [u8], a: &[u8], b: &[u8]) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_elementwise::<Binary<8, R>>(dst, a, b);
        }
        match Self::backend() {
            // `GF2P8MULB` multiplies two vectors directly in the AES
            // encoding; every other representation conjugates it by the
            // isomorphism onto `0x11B` (two affine maps around one native
            // multiply).
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                if R::AES_NATIVE {
                    x86::gf8::mul_elementwise_avx512::<true>(
                        crate::kernel::x86_v4x_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        a,
                        b,
                    );
                } else {
                    x86::gf8::mul_elementwise_avx512::<false>(
                        crate::kernel::x86_v4x_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        a,
                        b,
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_elementwise_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        a,
                        b,
                    );
                } else {
                    x86::gf8::mul_elementwise_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        a,
                        b,
                    );
                }
            }
            // No fixed coefficient means no nibble table, so the shuffle
            // backends use eight branchless shift/reduce rounds that thread
            // the representation's reduction byte.
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 => {
                x86::gf8::mul_elementwise_avx2(
                    crate::kernel::x86_v3_token(),
                    R::REDUCTION_LOW,
                    dst,
                    a,
                    b,
                );
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 => {
                x86::gf8::mul_elementwise_ssse3(
                    crate::kernel::x86_v2_token(),
                    R::REDUCTION_LOW,
                    dst,
                    a,
                    b,
                );
            }
            // Two varying operands are the one shape PMULL wins: two
            // `vmull_p8`s and a reduction network against eight bit-serial
            // rounds. The capability is cached in the backend, not probed per
            // call.
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::NeonAes if R::AES_NATIVE => {
                aarch64::gf8::mul_elementwise_neon_aes(crate::kernel::neon_aes_token(), dst, a, b);
            }
            #[cfg(all(feature = "simd", target_arch = "aarch64"))]
            Backend::Neon if R::AES_NATIVE => {
                aarch64::gf8::mul_elementwise_neon(crate::kernel::neon_token(), dst, a, b);
            }
            #[cfg(all(feature = "simd", target_arch = "wasm32"))]
            Backend::Wasm128 if R::AES_NATIVE => {
                wasm32::gf8::mul_elementwise_simd128(crate::kernel::wasm128_token(), dst, a, b);
            }
            _ => scalar::mul_elementwise::<Self>(dst, a, b),
        }
    }

    fn mul_elementwise_assign(_proof: RawDispatch, dst: &mut [u8], src: &[u8]) {
        if const { !R::DESCRIPTION.is_polynomial_root() } {
            return scalar::mul_elementwise_assign::<Binary<8, R>>(dst, src);
        }
        match Self::backend() {
            #[cfg(all(feature = "simd512", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V4x => {
                if R::AES_NATIVE {
                    x86::gf8::mul_elementwise_assign_avx512::<true>(
                        crate::kernel::x86_v4x_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        src,
                    );
                } else {
                    x86::gf8::mul_elementwise_assign_avx512::<false>(
                        crate::kernel::x86_v4x_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        src,
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3GfniCrypto => {
                if R::AES_NATIVE {
                    x86::gf8::mul_elementwise_assign_gfni::<true>(
                        crate::kernel::x86_v3_gfni_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        src,
                    );
                } else {
                    x86::gf8::mul_elementwise_assign_gfni::<false>(
                        crate::kernel::x86_v3_gfni_token(),
                        R::ISOMORPHISM,
                        R::REDUCTION_LOW,
                        dst,
                        src,
                    );
                }
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V3 => {
                x86::gf8::mul_elementwise_assign_avx2(
                    crate::kernel::x86_v3_token(),
                    R::REDUCTION_LOW,
                    dst,
                    src,
                );
            }
            #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
            Backend::V2 => {
                x86::gf8::mul_elementwise_assign_ssse3(
                    crate::kernel::x86_v2_token(),
                    R::REDUCTION_LOW,
                    dst,
                    src,
                );
            }
            _ => scalar::mul_elementwise_assign::<Self>(dst, src),
        }
    }
}