p3-monty-31 0.4.3

An implementation of a generic prime field F_p, where 2^30 < p < 2^31 using Montgomery arithmetic.
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
use alloc::vec::Vec;
use core::arch::aarch64::{self, int32x4_t, uint32x4_t};
use core::arch::asm;
use core::hint::unreachable_unchecked;
use core::iter::{Product, Sum};
use core::mem::transmute;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use p3_field::interleave::{interleave_u32, interleave_u64};
use p3_field::op_assign_macros::{
    impl_add_assign, impl_add_base_field, impl_div_methods, impl_mul_base_field, impl_mul_methods,
    impl_packed_value, impl_rng, impl_sub_assign, impl_sub_base_field, impl_sum_prod_base_field,
    ring_sum,
};
use p3_field::{
    Algebra, Field, InjectiveMonomial, PackedField, PackedFieldPow2, PackedValue,
    PermutationMonomial, PrimeCharacteristicRing, impl_packed_field_pow_2, uint32x4_mod_add,
    uint32x4_mod_sub,
};
use p3_util::reconstitute_from_base;
use rand::Rng;
use rand::distr::{Distribution, StandardUniform};

use super::utils::halve_neon;
use crate::{
    BinomialExtensionData, FieldParameters, MontyField31, PackedMontyParameters,
    RelativelyPrimePower,
};

const WIDTH: usize = 4;

pub trait MontyParametersNeon {
    const PACKED_P: uint32x4_t;
    const PACKED_MU: int32x4_t;
}

/// A trait to allow functions to be generic over scalar `MontyField31` and packed `PackedMontyField31Neon`.
trait IntoVec<P: PackedMontyParameters>: Copy {
    /// Convert the value to a NEON vector, broadcasting if it's a scalar.
    fn into_vec(self) -> uint32x4_t;
}

impl<P: PackedMontyParameters> IntoVec<P> for PackedMontyField31Neon<P> {
    #[inline(always)]
    fn into_vec(self) -> uint32x4_t {
        self.to_vector()
    }
}

impl<P: PackedMontyParameters> IntoVec<P> for MontyField31<P> {
    #[inline(always)]
    fn into_vec(self) -> uint32x4_t {
        // Broadcast the scalar value to all lanes of the vector.
        unsafe { aarch64::vdupq_n_u32(self.value) }
    }
}

/// Vectorized NEON implementation of `MontyField31` arithmetic.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(transparent)] // Needed to make `transmute`s safe.
#[must_use]
pub struct PackedMontyField31Neon<PMP: PackedMontyParameters>(pub [MontyField31<PMP>; WIDTH]);

impl<PMP: PackedMontyParameters> PackedMontyField31Neon<PMP> {
    /// Get an arch-specific vector representing the packed values.
    #[inline]
    #[must_use]
    pub(crate) fn to_vector(self) -> uint32x4_t {
        unsafe {
            // Safety: `MontyField31` is `repr(transparent)` so it can be transmuted to `u32`. It
            // follows that `[MontyField31; WIDTH]` can be transmuted to `[u32; WIDTH]`, which can be
            // transmuted to `uint32x4_t`, since arrays are guaranteed to be contiguous in memory.
            // Finally `PackedMontyField31Neon` is `repr(transparent)` so it can be transmuted to
            // `[MontyField31; WIDTH]`.
            transmute(self)
        }
    }

    /// Get an arch-specific vector representing the packed values.
    #[inline]
    #[must_use]
    pub(crate) fn to_signed_vector(self) -> int32x4_t {
        unsafe {
            // Safety: `MontyField31` is `repr(transparent)` so it can be transmuted to `u32` furthermore
            // the u32 is guaranteed to be less than `2^31` so it can be safely reinterpreted as an `i32`. It
            // follows that `[MontyField31; WIDTH]` can be transmuted to `[i32; WIDTH]`, which can be
            // transmuted to `int32x4_t`, since arrays are guaranteed to be contiguous in memory.
            // Finally `PackedMontyField31Neon` is `repr(transparent)` so it can be transmuted to
            // `[MontyField31; WIDTH]`.
            transmute(self)
        }
    }

    /// Make a packed field vector from an arch-specific vector.
    ///
    /// SAFETY: The caller must ensure that each element of `vector` represents a valid `MontyField31`.
    /// In particular, each element of vector must be in `0..P` (canonical form).
    #[inline]
    pub(crate) unsafe fn from_vector(vector: uint32x4_t) -> Self {
        unsafe {
            // Safety: It is up to the user to ensure that elements of `vector` represent valid
            // `MontyField31` values. We must only reason about memory representations. `uint32x4_t` can be
            // transmuted to `[u32; WIDTH]` (since arrays elements are contiguous in memory), which can
            // be transmuted to `[MontyField31; WIDTH]` (since `MontyField31` is `repr(transparent)`), which in
            // turn can be transmuted to `PackedMontyField31Neon` (since `PackedMontyField31Neon` is also
            // `repr(transparent)`).
            transmute(vector)
        }
    }

    /// Copy `value` to all positions in a packed vector. This is the same as
    /// `From<MontyField31>::from`, but `const`.
    #[inline]
    const fn broadcast(value: MontyField31<PMP>) -> Self {
        Self([value; WIDTH])
    }

    /// Fused DIF butterfly for forward FFT: computes `(x + y, (x - y) * roots)`.
    ///
    /// Saves 2 NEON ops per butterfly by skipping the modular reduction on
    /// `x - y`. The raw `vsubq_u32(x, y)` lies in `(-P, P)` as signed,
    /// which is already a valid input for Montgomery multiplication.
    #[inline(always)]
    pub(crate) fn forward_butterfly(self, y: Self, roots: Self) -> (Self, Self) {
        unsafe {
            let x_vec = self.to_vector();
            let y_vec = y.to_vector();

            // Canonical modular addition: result in [0, P).
            let sum = uint32x4_mod_add(x_vec, y_vec, PMP::PACKED_P);

            // Raw subtraction without reduction.
            //
            // Since x, y are in [0, P), the u32 result wraps to a value that,
            // when reinterpreted as i32, lies in (-P, P). This is exactly the
            // signed input range that Montgomery multiplication accepts.
            let diff = aarch64::vreinterpretq_s32_u32(aarch64::vsubq_u32(x_vec, y_vec));

            // Montgomery multiply:
            // - accepts signed inputs in (-P, P),
            // - produces canonical output in [0, P).
            let roots_s = roots.to_signed_vector();
            let product = mul::<PMP>(diff, roots_s);

            (Self::from_vector(sum), Self::from_vector(product))
        }
    }
}

impl<PMP: PackedMontyParameters> From<MontyField31<PMP>> for PackedMontyField31Neon<PMP> {
    #[inline]
    fn from(value: MontyField31<PMP>) -> Self {
        Self::broadcast(value)
    }
}

impl<PMP: PackedMontyParameters> Add for PackedMontyField31Neon<PMP> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        let lhs = self.to_vector();
        let rhs = rhs.to_vector();
        let res = uint32x4_mod_add(lhs, rhs, PMP::PACKED_P);
        unsafe {
            // Safety: `uint32x4_mod_add` returns values in canonical form when given values in canonical form.
            Self::from_vector(res)
        }
    }
}

impl<PMP: PackedMontyParameters> Sub for PackedMontyField31Neon<PMP> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        let lhs = self.to_vector();
        let rhs = rhs.to_vector();
        let res = uint32x4_mod_sub(lhs, rhs, PMP::PACKED_P);
        unsafe {
            // Safety: `uint32x4_mod_sub` returns values in canonical form when given values in canonical form.
            Self::from_vector(res)
        }
    }
}

impl<PMP: PackedMontyParameters> Neg for PackedMontyField31Neon<PMP> {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        let val = self.to_vector();
        let res = neg::<PMP>(val);
        unsafe {
            // Safety: `neg` returns values in canonical form when given values in canonical form.
            Self::from_vector(res)
        }
    }
}

impl<PMP: PackedMontyParameters> Mul for PackedMontyField31Neon<PMP> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        let lhs = self.to_signed_vector();
        let rhs = rhs.to_signed_vector();
        let res = mul::<PMP>(lhs, rhs);
        unsafe {
            // Safety: `mul` returns values in canonical form when given values in canonical form.
            Self::from_vector(res)
        }
    }
}

impl_add_assign!(PackedMontyField31Neon, (PackedMontyParameters, PMP));
impl_sub_assign!(PackedMontyField31Neon, (PackedMontyParameters, PMP));
impl_mul_methods!(PackedMontyField31Neon, (FieldParameters, FP));
ring_sum!(PackedMontyField31Neon, (FieldParameters, FP));
impl_rng!(PackedMontyField31Neon, (PackedMontyParameters, PMP));

impl<FP: FieldParameters> PrimeCharacteristicRing for PackedMontyField31Neon<FP> {
    type PrimeSubfield = MontyField31<FP>;

    const ZERO: Self = Self::broadcast(MontyField31::ZERO);
    const ONE: Self = Self::broadcast(MontyField31::ONE);
    const TWO: Self = Self::broadcast(MontyField31::TWO);
    const NEG_ONE: Self = Self::broadcast(MontyField31::NEG_ONE);

    #[inline]
    fn from_prime_subfield(f: Self::PrimeSubfield) -> Self {
        f.into()
    }

    #[inline]
    fn halve(&self) -> Self {
        let val = self.to_vector();
        let halved = halve_neon::<FP>(val);
        unsafe {
            // Safety: `halve_neon` returns values in canonical form when given values in canonical form.
            Self::from_vector(halved)
        }
    }

    #[inline]
    fn cube(&self) -> Self {
        let val = self.to_signed_vector();
        let res = cube::<FP>(val);
        unsafe {
            // Safety: `cube` returns values in canonical form when given values in canonical form.
            Self::from_vector(res)
        }
    }

    #[inline(always)]
    fn dot_product<const N: usize>(u: &[Self; N], v: &[Self; N]) -> Self {
        general_dot_product::<_, _, _, N>(u, v)
    }

    #[inline(always)]
    fn zero_vec(len: usize) -> Vec<Self> {
        // SAFETY: this is a repr(transparent) wrapper around an array.
        unsafe { reconstitute_from_base(MontyField31::<FP>::zero_vec(len * WIDTH)) }
    }

    #[inline(always)]
    fn exp_const_u64<const POWER: u64>(&self) -> Self {
        // We provide specialised code for the powers 3, 5, 7 as these turn up regularly.
        // The other powers could be specialised similarly but we ignore this for now.
        match POWER {
            0 => Self::ONE,
            1 => *self,
            2 => self.square(),
            3 => self.cube(),
            4 => self.square().square(),
            5 => {
                let val = self.to_signed_vector();
                unsafe {
                    // Safety: `exp_5` returns values in canonical form when given values in canonical form.
                    let res = exp_5::<FP>(val);
                    Self::from_vector(res)
                }
            }
            6 => self.square().cube(),
            7 => {
                let val = self.to_signed_vector();
                unsafe {
                    // Safety: `exp_7` returns values in canonical form when given values in canonical form.
                    let res = exp_7::<FP>(val);
                    Self::from_vector(res)
                }
            }
            _ => self.exp_u64(POWER),
        }
    }
}

impl_add_base_field!(
    PackedMontyField31Neon,
    MontyField31,
    (PackedMontyParameters, PMP)
);
impl_sub_base_field!(
    PackedMontyField31Neon,
    MontyField31,
    (PackedMontyParameters, PMP)
);
impl_mul_base_field!(
    PackedMontyField31Neon,
    MontyField31,
    (PackedMontyParameters, PMP)
);
impl_div_methods!(PackedMontyField31Neon, MontyField31, (FieldParameters, FP));
impl_sum_prod_base_field!(PackedMontyField31Neon, MontyField31, (FieldParameters, FP));

impl<FP: FieldParameters> Algebra<MontyField31<FP>> for PackedMontyField31Neon<FP> {}

impl<FP: FieldParameters + RelativelyPrimePower<D>, const D: u64> InjectiveMonomial<D>
    for PackedMontyField31Neon<FP>
{
}

impl<FP: FieldParameters + RelativelyPrimePower<D>, const D: u64> PermutationMonomial<D>
    for PackedMontyField31Neon<FP>
{
    fn injective_exp_root_n(&self) -> Self {
        FP::exp_root_d(*self)
    }
}

/// No-op. Prevents the compiler from deducing the value of the vector.
///
/// Similar to `core::hint::black_box`, it can be used to stop the compiler applying undesirable
/// "optimizations". Unlike the built-in `black_box`, it does not force the value to be written to
/// and then read from the stack.
#[inline]
#[must_use]
fn confuse_compiler(x: uint32x4_t) -> uint32x4_t {
    let y;
    unsafe {
        asm!(
            "/*{0:v}*/",
            inlateout(vreg) x => y,
            options(nomem, nostack, preserves_flags, pure),
        );
        // Below tells the compiler the semantics of this so it can still do constant folding, etc.
        // You may ask, doesn't it defeat the point of the inline asm block to tell the compiler
        // what it does? The answer is that we still inhibit the transform we want to avoid, so
        // apparently not. Idk, LLVM works in mysterious ways.
        if transmute::<uint32x4_t, [u32; 4]>(x) != transmute::<uint32x4_t, [u32; 4]>(y) {
            unreachable_unchecked();
        }
    }
    y
}

// MONTGOMERY MULTIPLICATION
//   This implementation is based on [1] but with changes. The reduction is as follows:
//
// Constants: P < 2^31
//            B = 2^32
//            μ = P^-1 mod B
// Input: -P^2 <= C <= P^2
// Output: -P < D < P such that D = C B^-1 (mod P)
// Define:
//   smod_B(a) = r, where -B/2 <= r <= B/2 - 1 and r = a (mod B).
// Algorithm:
//   1. Q := smod_B(μ C)
//   2. D := (C - Q P) / B
//
// We first show that the division in step 2. is exact. It suffices to show that C = Q P (mod B). By
// definition of Q, smod_B, and μ, we have Q P = smod_B(μ C) P = μ C P = P^-1 C P = C (mod B).
//
// We also have C - Q P = C (mod P), so thus D = C B^-1 (mod P).
//
// It remains to show that D is in the correct range. It suffices to show that -P B < C - Q P < P B.
// We know that -P^2 <= C <= P^2 and (-B / 2) P <= Q P <= (B/2 - 1) P. Then
// (1 - B/2) P - P^2 <= C - Q P <= (B/2) P + P^2. Now, P < B/2, so B/2 + P < B and
// (B/2) P + P^2 < P B; also B/2 - 1 + P < B, so -P B < (1 - B/2) P - P^2.
// Hence, -P B < C - Q P < P B as desired.
//
// [1] Modern Computer Arithmetic, Richard Brent and Paul Zimmermann, Cambridge University Press,
//     2010, algorithm 2.7.

#[inline]
#[must_use]
fn mulby_mu<MPNeon: MontyParametersNeon>(val: int32x4_t) -> int32x4_t {
    // We want this to compile to:
    //      mul      res.4s, val.4s, MU.4s
    // throughput: .25 cyc/vec (16 els/cyc)
    // latency: 3 cyc

    unsafe { aarch64::vmulq_s32(val, MPNeon::PACKED_MU) }
}

#[inline]
#[must_use]
fn get_c_hi(lhs: int32x4_t, rhs: int32x4_t) -> int32x4_t {
    // We want this to compile to:
    //      sqdmulh  c_hi.4s, lhs.4s, rhs.4s
    // throughput: .25 cyc/vec (16 els/cyc)
    // latency: 3 cyc

    unsafe {
        // Get bits 31, ..., 62 of C. Note that `sqdmulh` saturates when the product doesn't fit in
        // an `i63`, but this cannot happen here due to our bounds on `lhs` and `rhs`.
        aarch64::vqdmulhq_s32(lhs, rhs)
    }
}

#[inline]
#[must_use]
fn get_qp_hi<MPNeon: MontyParametersNeon>(lhs: int32x4_t, mu_rhs: int32x4_t) -> int32x4_t {
    // We want this to compile to:
    //      mul      q.4s, lhs.4s, mu_rhs.4s
    //      sqdmulh  qp_hi.4s, q.4s, P.4s
    // throughput: .5 cyc/vec (8 els/cyc)
    // latency: 6 cyc

    unsafe {
        // Form `Q`.
        let q = aarch64::vmulq_s32(lhs, mu_rhs);

        // Gets bits 31, ..., 62 of Q P. Again, saturation is not an issue because `P` is not
        // -2**31.
        aarch64::vqdmulhq_s32(q, aarch64::vreinterpretq_s32_u32(MPNeon::PACKED_P))
    }
}

/// Multiply MontyField31 field elements.
///
/// # Safety
/// Inputs must be signed 32-bit integers in the range [-P, P].
/// Outputs will be a unsigned 32-bit integers in canonical form [0, ..., P).
#[inline]
#[must_use]
fn mul<MPNeon: MontyParametersNeon>(lhs: int32x4_t, rhs: int32x4_t) -> uint32x4_t {
    // We want this to compile to:
    //      sqdmulh  c_hi.4s, lhs.4s, rhs.4s
    //      mul      mu_rhs.4s, rhs.4s, MU.4s
    //      mul      q.4s, lhs.4s, mu_rhs.4s
    //      sqdmulh  qp_hi.4s, q.4s, P.4s
    //      shsub    res.4s, c_hi.4s, qp_hi.4s
    //      cmgt     underflow.4s, qp_hi.4s, c_hi.4s
    //      mls      res.4s, underflow.4s, P.4s
    // throughput: 1.75 cyc/vec (2.29 els/cyc)
    // latency: (lhs->) 11 cyc, (rhs->) 14 cyc

    unsafe {
        let mu_rhs = mulby_mu::<MPNeon>(rhs);
        let d = mul_with_precomp::<MPNeon, true>(lhs, rhs, mu_rhs);

        // Safe as mul_with_precomp::<MPNeon, true> returns integers in [0, P)
        aarch64::vreinterpretq_u32_s32(d)
    }
}

/// Multiply MontyField31 field elements using precomputation.
///
/// Allows us to reuse `mu_rhs`.
///
/// # Safety
/// Both `lhs` and `rhs` must be signed 32-bit integers in the range [-P, P].
/// `mu_rhs` must be equal to `MPNeon::PACKED_MU * rhs mod 2^32`
///
/// Output will be signed 32-bit integers either in (-P, P) if CANONICAL is set to false
/// or in [0, P) if CANONICAL is set to true.
#[inline]
#[must_use]
fn mul_with_precomp<MPNeon: MontyParametersNeon, const CANONICAL: bool>(
    lhs: int32x4_t,
    rhs: int32x4_t,
    mu_rhs: int32x4_t,
) -> int32x4_t {
    // If CANONICAL:
    //  We want this to compile to:
    //      sqdmulh  c_hi.4s, lhs.4s, rhs.4s
    //      mul      q.4s, lhs.4s, mu_rhs.4s
    //      sqdmulh  qp_hi.4s, q.4s, P.4s
    //      shsub    res.4s, c_hi.4s, qp_hi.4s
    //      cmgt     underflow.4s, qp_hi.4s, c_hi.4s
    //      mls      res.4s, underflow.4s, P.4s
    //
    //      throughput: 1.5 cyc/vec (2.66 els/cyc)
    //      latency: 11 cyc
    //
    // If !CANONICAL:
    //  We want this to compile to:
    //      sqdmulh  c_hi.4s, lhs.4s, rhs.4s
    //      mul      q.4s, lhs.4s, mu_rhs.4s
    //      sqdmulh  qp_hi.4s, q.4s, P.4s
    //      shsub    res.4s, c_hi.4s, qp_hi.4s
    //
    //      throughput: 1 cyc/vec (4 els/cyc)
    //      latency: 8 cyc
    //
    unsafe {
        let c_hi = get_c_hi(lhs, rhs);
        let qp_hi = get_qp_hi::<MPNeon>(lhs, mu_rhs);
        let d = aarch64::vhsubq_s32(c_hi, qp_hi);

        // This branch will be removed by the compiler.
        if CANONICAL {
            // We reduce d to canonical form. d is negative iff `c_hi > qp_hi`, so if that's the
            // case then we add P. Note that if `c_hi > qp_hi` then `underflow` is -1, so we must
            // _subtract_ `underflow` * P.
            let underflow = aarch64::vcltq_s32(c_hi, qp_hi);

            // As underflow and MPNeon::PACKED_P are unsigned we use the unsigned version of multiply
            // and subtract. Note that on bits, the signed and unsigned versions are literally identical.
            let reduced = aarch64::vmlsq_u32(
                aarch64::vreinterpretq_u32_s32(d),
                confuse_compiler(underflow),
                MPNeon::PACKED_P,
            );

            // We convert back to int32x4_t to match the function output.
            aarch64::vreinterpretq_s32_u32(reduced)
        } else {
            d
        }
    }
}

/// Take cube of MontyField31 field elements.
///
/// # Safety
/// Inputs must be signed 32-bit integers in the range [-P, P].
/// Outputs will be a unsigned 32-bit integers in canonical form [0, ..., P).
#[inline]
#[must_use]
fn cube<MPNeon: MontyParametersNeon>(val: int32x4_t) -> uint32x4_t {
    // throughput: 2.75 cyc/vec (1.45 els/cyc)
    // latency: 22 cyc

    unsafe {
        let mu_val = mulby_mu::<MPNeon>(val);

        let val_2 = mul_with_precomp::<MPNeon, false>(val, val, mu_val);
        let val_3 = mul_with_precomp::<MPNeon, true>(val_2, val, mu_val);

        // Safe as mul_with_precomp::<MPNeon, true> returns integers in [0, P)
        aarch64::vreinterpretq_u32_s32(val_3)
    }
}

/// Take the fifth power of the MontyField31 field elements.
///
/// # Safety
/// Inputs must be signed 32-bit integers in the range [-P, P].
/// Outputs will be a unsigned 32-bit integers in canonical form [0, ..., P).
#[inline]
#[must_use]
fn exp_5<MPNeon: MontyParametersNeon>(val: int32x4_t) -> uint32x4_t {
    // throughput: 4 cyc/vec (1 els/cyc)
    // latency: 30 cyc

    unsafe {
        let mu_val = mulby_mu::<MPNeon>(val);

        let val_2 = mul_with_precomp::<MPNeon, false>(val, val, mu_val);

        // mu_val_2 and val_3 can be computed in parallel.
        let mu_val_2 = mulby_mu::<MPNeon>(val_2);
        let val_3 = mul_with_precomp::<MPNeon, false>(val_2, val, mu_val);

        let val_5 = mul_with_precomp::<MPNeon, true>(val_3, val_2, mu_val_2);

        // Safe as mul_with_precomp::<MPNeon, true> returns integers in [0, P)
        aarch64::vreinterpretq_u32_s32(val_5)
    }
}

/// Take the seventh power of the MontyField31 field elements.
///
/// # Safety
/// Inputs must be signed 32-bit integers in the range [-P, P].
/// Outputs will be a unsigned 32-bit integers in canonical form [0, ..., P).
#[inline]
#[must_use]
fn exp_7<MPNeon: MontyParametersNeon>(val: int32x4_t) -> uint32x4_t {
    // throughput: 5.25 cyc/vec (0.76 els/cyc)
    // latency: 33 cyc

    unsafe {
        let mu_val = mulby_mu::<MPNeon>(val);

        let val_2 = mul_with_precomp::<MPNeon, false>(val, val, mu_val);

        // mu_val_2, val_4 and val_3, mu_val_3 can be computed in parallel.
        let mu_val_2 = mulby_mu::<MPNeon>(val_2);
        let val_3 = mul_with_precomp::<MPNeon, false>(val_2, val, mu_val);

        let mu_val_3 = mulby_mu::<MPNeon>(val_3);
        let val_4 = mul_with_precomp::<MPNeon, false>(val_2, val_2, mu_val_2);

        let val_7 = mul_with_precomp::<MPNeon, true>(val_4, val_3, mu_val_3);

        // Safe as mul_with_precomp::<MPNeon, true> returns integers in [0, P)
        aarch64::vreinterpretq_u32_s32(val_7)
    }
}

/// Negate a vector of Monty31 field elements in canonical form.
/// If the inputs are not in canonical form, the result is undefined.
#[inline]
#[must_use]
fn neg<MPNeon: MontyParametersNeon>(val: uint32x4_t) -> uint32x4_t {
    // We want this to compile to:
    //      sub   t.4s, P.4s, val.4s
    //      cmeq  is_zero.4s, val.4s, #0
    //      bic   res.4s, t.4s, is_zero.4s
    // throughput: .75 cyc/vec (5.33 els/cyc)
    // latency: 4 cyc

    // This has the same throughput as `sub(0, val)` but slightly lower latency.

    //   We want to return (-val) mod P. This is equivalent to returning `0` if `val = 0` and
    // `P - val` otherwise, since `val` is in `0, ..., P - 1`.
    //   Let `t := P - val` and let `is_zero := (-1) mod 2^32` if `val = 0` and `0` otherwise.
    //   We return `r := t & ~is_zero`, which is `t` if `val > 0` and `0` otherwise, as desired.
    unsafe {
        // Safety: If this code got compiled then NEON intrinsics are available.
        let t = aarch64::vsubq_u32(MPNeon::PACKED_P, val);
        let is_zero = aarch64::vceqzq_u32(val);
        aarch64::vbicq_u32(t, is_zero)
    }
}

impl_packed_value!(
    PackedMontyField31Neon,
    MontyField31,
    WIDTH,
    (PackedMontyParameters, PMP)
);

unsafe impl<FP: FieldParameters> PackedField for PackedMontyField31Neon<FP> {
    type Scalar = MontyField31<FP>;

    #[inline]
    fn packed_linear_combination<const N: usize>(coeffs: &[Self::Scalar], vecs: &[Self]) -> Self {
        general_dot_product::<_, _, _, N>(coeffs, vecs)
    }
}

impl_packed_field_pow_2!(
    PackedMontyField31Neon, (FieldParameters, FP);
    [
        (1, interleave_u32),
        (2, interleave_u64),
    ],
    WIDTH
);

/// Compute the elementary function `l0*r0 + l1*r1` given four inputs
/// in canonical form.
///
/// If the inputs are not in canonical form, the result is undefined.
#[inline]
unsafe fn dot_product_2<P, LHS, RHS>(lhs: &[LHS; 2], rhs: &[RHS; 2]) -> PackedMontyField31Neon<P>
where
    P: FieldParameters + MontyParametersNeon,
    LHS: IntoVec<P>,
    RHS: IntoVec<P>,
{
    unsafe {
        // Accumulate the full 64-bit sum C = l0*r0 + l1*r1.

        // Low half (Lanes 0 & 1)
        let mut sum_l = aarch64::vmull_u32(
            aarch64::vget_low_u32(lhs[0].into_vec()),
            aarch64::vget_low_u32(rhs[0].into_vec()),
        );
        sum_l = aarch64::vmlal_u32(
            sum_l,
            aarch64::vget_low_u32(lhs[1].into_vec()),
            aarch64::vget_low_u32(rhs[1].into_vec()),
        );

        // High half (Lanes 2 & 3)
        let mut sum_h = aarch64::vmull_high_u32(lhs[0].into_vec(), rhs[0].into_vec());
        sum_h = aarch64::vmlal_high_u32(sum_h, lhs[1].into_vec(), rhs[1].into_vec());

        // Split C into 32-bit low halves per lane: c_lo = C mod 2^{32}
        let c_lo = aarch64::vuzp1q_u32(
            aarch64::vreinterpretq_u32_u64(sum_l),
            aarch64::vreinterpretq_u32_u64(sum_h),
        );

        // q ≡ c_lo ⋅ μ (mod 2^{32}), with μ = −P^{-1} (mod 2^{32}).
        let q = aarch64::vmulq_u32(c_lo, aarch64::vreinterpretq_u32_s32(P::PACKED_MU));

        // Compute d = (C - qâ‹…P) / B using multiply-subtract-long instructions.
        //
        // This combines the multiplication qâ‹…P and subtraction C - qâ‹…P in one step.
        let d_l = aarch64::vmlsl_u32(
            sum_l,
            aarch64::vget_low_u32(q),
            aarch64::vget_low_u32(P::PACKED_P),
        );
        let d_h = aarch64::vmlsl_high_u32(sum_h, q, P::PACKED_P);

        // Extract the high 32 bits (the division by B = 2^32) from d_l and d_h.
        let d = aarch64::vuzp2q_u32(
            aarch64::vreinterpretq_u32_u64(d_l),
            aarch64::vreinterpretq_u32_u64(d_h),
        );

        // Canonicalize d from (-P, P) to [0, P) branchlessly.
        //
        // The `vmlsq_u32` instruction computes `a - (b * c)`.
        // - If `d` is negative (interpreted as unsigned, it's >= 2^31), the mask is `-1` (all 1s),
        //   so we compute `d - (-1 * P) = d + P`.
        // - If `d` is non-negative, the mask is `0`, so we compute `d - (0 * P) = d`.
        //
        // Check if d >= 2^31 (i.e., negative when interpreted as signed).
        let underflow = aarch64::vcgeq_u32(d, aarch64::vdupq_n_u32(1u32 << 31));
        let canonical_res = aarch64::vmlsq_u32(d, underflow, P::PACKED_P);

        // Safety: The result is now in canonical form [0, P).
        PackedMontyField31Neon::from_vector(canonical_res)
    }
}

/// A general fast dot product implementation using NEON.
#[inline(always)]
fn general_dot_product<P, LHS, RHS, const N: usize>(
    lhs: &[LHS],
    rhs: &[RHS],
) -> PackedMontyField31Neon<P>
where
    P: FieldParameters + MontyParametersNeon,
    LHS: IntoVec<P> + Into<PackedMontyField31Neon<P>>,
    RHS: IntoVec<P> + Into<PackedMontyField31Neon<P>>,
{
    assert_eq!(lhs.len(), N);
    assert_eq!(rhs.len(), N);
    match N {
        0 => PackedMontyField31Neon::<P>::ZERO,
        1 => lhs[0].into() * rhs[0].into(),
        2 => unsafe { dot_product_2(&[lhs[0], lhs[1]], &[rhs[0], rhs[1]]) },
        3 => {
            let lhs_packed = [
                lhs[0].into(),
                lhs[1].into(),
                lhs[2].into(),
                PackedMontyField31Neon::<P>::ZERO,
            ];
            let rhs_packed = [
                rhs[0].into(),
                rhs[1].into(),
                rhs[2].into(),
                PackedMontyField31Neon::<P>::ZERO,
            ];
            unsafe { dot_product_4(&lhs_packed, &rhs_packed) }
        }
        4 => unsafe {
            dot_product_4(
                &[lhs[0], lhs[1], lhs[2], lhs[3]],
                &[rhs[0], rhs[1], rhs[2], rhs[3]],
            )
        },
        64 => {
            let sum_4s: [PackedMontyField31Neon<P>; 16] = core::array::from_fn(|i| {
                let start = i * 4;
                unsafe {
                    dot_product_4(
                        &[lhs[start], lhs[start + 1], lhs[start + 2], lhs[start + 3]],
                        &[rhs[start], rhs[start + 1], rhs[start + 2], rhs[start + 3]],
                    )
                }
            });
            PackedMontyField31Neon::<P>::sum_array::<16>(&sum_4s)
        }
        _ => {
            // Initialize accumulator with the first chunk of 4.
            let mut acc = unsafe {
                dot_product_4(
                    &[lhs[0], lhs[1], lhs[2], lhs[3]],
                    &[rhs[0], rhs[1], rhs[2], rhs[3]],
                )
            };

            // Loop over the rest of the full chunks of 4.
            for i in (4..N).step_by(4) {
                if i + 3 < N {
                    acc += unsafe {
                        dot_product_4(
                            &[lhs[i], lhs[i + 1], lhs[i + 2], lhs[i + 3]],
                            &[rhs[i], rhs[i + 1], rhs[i + 2], rhs[i + 3]],
                        )
                    };
                }
            }

            // Handle the remainder recursively by creating new arrays and calling self.
            match N % 4 {
                0 => acc,
                1 => {
                    let rem_start = N - 1;
                    let lhs_rem: [_; 1] = core::array::from_fn(|i| lhs[rem_start + i]);
                    let rhs_rem: [_; 1] = core::array::from_fn(|i| rhs[rem_start + i]);
                    acc + general_dot_product::<_, _, _, 1>(&lhs_rem, &rhs_rem)
                }
                2 => {
                    let rem_start = N - 2;
                    let lhs_rem: [_; 2] = core::array::from_fn(|i| lhs[rem_start + i]);
                    let rhs_rem: [_; 2] = core::array::from_fn(|i| rhs[rem_start + i]);
                    acc + general_dot_product::<_, _, _, 2>(&lhs_rem, &rhs_rem)
                }
                3 => {
                    let rem_start = N - 3;
                    let lhs_rem: [_; 3] = core::array::from_fn(|i| lhs[rem_start + i]);
                    let rhs_rem: [_; 3] = core::array::from_fn(|i| rhs[rem_start + i]);
                    acc + general_dot_product::<_, _, _, 3>(&lhs_rem, &rhs_rem)
                }
                _ => unreachable!(),
            }
        }
    }
}

/// Compute the elementary function `l0*r0 + l1*r1 + l2*r2 + l3*r3` given eight inputs
/// in canonical form.
///
/// If the inputs are not in canonical form, the result is undefined.
#[inline]
unsafe fn dot_product_4<P, LHS, RHS>(lhs: &[LHS; 4], rhs: &[RHS; 4]) -> PackedMontyField31Neon<P>
where
    P: FieldParameters + MontyParametersNeon,
    LHS: IntoVec<P>,
    RHS: IntoVec<P>,
{
    unsafe {
        // Accumulate the full 64-bit sum C = Σ lhs_i ⋅ rhs_i.

        // Low half (Lanes 0 & 1)
        let mut sum_l = aarch64::vmull_u32(
            aarch64::vget_low_u32(lhs[0].into_vec()),
            aarch64::vget_low_u32(rhs[0].into_vec()),
        );
        sum_l = aarch64::vmlal_u32(
            sum_l,
            aarch64::vget_low_u32(lhs[1].into_vec()),
            aarch64::vget_low_u32(rhs[1].into_vec()),
        );
        sum_l = aarch64::vmlal_u32(
            sum_l,
            aarch64::vget_low_u32(lhs[2].into_vec()),
            aarch64::vget_low_u32(rhs[2].into_vec()),
        );
        sum_l = aarch64::vmlal_u32(
            sum_l,
            aarch64::vget_low_u32(lhs[3].into_vec()),
            aarch64::vget_low_u32(rhs[3].into_vec()),
        );

        // High half (Lanes 2 & 3)
        let mut sum_h = aarch64::vmull_high_u32(lhs[0].into_vec(), rhs[0].into_vec());
        sum_h = aarch64::vmlal_high_u32(sum_h, lhs[1].into_vec(), rhs[1].into_vec());
        sum_h = aarch64::vmlal_high_u32(sum_h, lhs[2].into_vec(), rhs[2].into_vec());
        sum_h = aarch64::vmlal_high_u32(sum_h, lhs[3].into_vec(), rhs[3].into_vec());

        // Split C into 32-bit halves per lane:
        // - c_lo = C mod 2^{32},
        // - c_hi = C >> 32.
        let c_lo = aarch64::vuzp1q_u32(
            aarch64::vreinterpretq_u32_u64(sum_l),
            aarch64::vreinterpretq_u32_u64(sum_h),
        );
        let c_hi = aarch64::vuzp2q_u32(
            aarch64::vreinterpretq_u32_u64(sum_l),
            aarch64::vreinterpretq_u32_u64(sum_h),
        );

        // Since C < 4P^2 and P < 2^{31}, we have c_hi < 2P.
        // We want to compute: c_hi' ∈ [0,P) satisfying c_hi' = c_hi mod P.
        let c_hi_sub = aarch64::vsubq_u32(c_hi, P::PACKED_P);
        let c_hi_prime = aarch64::vminq_u32(c_hi, c_hi_sub);

        // q ≡ c_lo ⋅ μ (mod 2^{32}), with μ = −P^{-1} (mod 2^{32}).
        let q = aarch64::vmulq_u32(c_lo, aarch64::vreinterpretq_u32_s32(P::PACKED_MU));

        // Compute (qâ‹…P)_hi = high 32 bits of qâ‹…P per lane (exact unsigned widening multiply).
        let qp_l = aarch64::vmull_u32(aarch64::vget_low_u32(q), aarch64::vget_low_u32(P::PACKED_P));
        let qp_h = aarch64::vmull_high_u32(q, P::PACKED_P);
        let qp_hi = aarch64::vuzp2q_u32(
            aarch64::vreinterpretq_u32_u64(qp_l),
            aarch64::vreinterpretq_u32_u64(qp_h),
        );

        let d = aarch64::vsubq_u32(c_hi_prime, qp_hi);

        // Canonicalize d from (-P, P) to [0, P) branchlessly.
        //
        // The `vmlsq_u32` instruction computes `a - (b * c)`.
        // - If `d` is negative, the mask is `-1` (all 1s), so we compute `d - (-1 * P) = d + P`.
        // - If `d` is non-negative, the mask is `0`, so we compute `d - (0 * P) = d`.
        let underflow = aarch64::vcltq_u32(c_hi_prime, qp_hi);
        let canonical_res = aarch64::vmlsq_u32(d, underflow, P::PACKED_P);

        // Safety: The result is now in canonical form [0, P).
        PackedMontyField31Neon::from_vector(canonical_res)
    }
}

/// Multiplication in a quartic binomial extension field.
#[inline]
pub(crate) fn quartic_mul_packed<FP, const WIDTH: usize>(
    a: &[MontyField31<FP>; WIDTH],
    b: &[MontyField31<FP>; WIDTH],
    res: &mut [MontyField31<FP>; WIDTH],
) where
    FP: FieldParameters + BinomialExtensionData<WIDTH> + MontyParametersNeon,
{
    assert_eq!(WIDTH, 4);

    // Precompute wâ‹…b once (base-field multiply by the binomial constant).
    let packed_b = PackedMontyField31Neon([b[0], b[1], b[2], b[3]]);
    let w_b = FP::mul_w(packed_b).0;

    // Constant term = a0*b0 + w(a1*b3 + a2*b2 + a3*b1)
    // Linear term = a0*b1 + a1*b0 + w(a2*b3 + a3*b2)
    // Square term = a0*b2 + a1*b1 + a2*b0 + w(a3*b3)
    // Cubic term = a0*b3 + a1*b2 + a2*b1 + a3*b0
    let cols = [
        PackedMontyField31Neon([b[0], b[1], b[2], b[3]]),
        PackedMontyField31Neon([w_b[3], b[0], b[1], b[2]]),
        PackedMontyField31Neon([w_b[2], w_b[3], b[0], b[1]]),
        PackedMontyField31Neon([w_b[1], w_b[2], w_b[3], b[0]]),
    ];

    // Arrange a’s coefficients for the dot product.
    let a_coeffs = [a[0], a[1], a[2], a[3]];

    let result = unsafe { dot_product_4(&a_coeffs, &cols) };

    res.copy_from_slice(&result.0);
}

/// Multiplication in a quintic binomial extension field.
#[inline]
pub(crate) fn quintic_mul_packed<FP, const WIDTH: usize>(
    a: &[MontyField31<FP>; WIDTH],
    b: &[MontyField31<FP>; WIDTH],
    res: &mut [MontyField31<FP>; WIDTH],
) where
    FP: FieldParameters + BinomialExtensionData<WIDTH>,
{
    // TODO: This could be optimised further with a custom NEON implementation.
    assert_eq!(WIDTH, 5);
    let packed_b = PackedMontyField31Neon([b[1], b[2], b[3], b[4]]);
    let w_b = FP::mul_w(packed_b).0;
    let w_b1 = w_b[0];
    let w_b2 = w_b[1];
    let w_b3 = w_b[2];
    let w_b4 = w_b[3];

    // Constant term = a0*b0 + w(a1*b4 + a2*b3 + a3*b2 + a4*b1)
    // Linear term = a0*b1 + a1*b0 + w(a2*b4 + a3*b3 + a4*b2)
    // Square term = a0*b2 + a1*b1 + a2*b0 + w(a3*b4 + a4*b3)
    // Cubic term = a0*b3 + a1*b2 + a2*b1 + a3*b0 + w*a4*b4
    // Quartic term = a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0
    let lhs: [PackedMontyField31Neon<FP>; 5] = [
        a[0].into(),
        a[1].into(),
        a[2].into(),
        a[3].into(),
        a[4].into(),
    ];
    let rhs = [
        PackedMontyField31Neon([b[0], b[1], b[2], b[3]]),
        PackedMontyField31Neon([w_b4, b[0], b[1], b[2]]),
        PackedMontyField31Neon([w_b3, w_b4, b[0], b[1]]),
        PackedMontyField31Neon([w_b2, w_b3, w_b4, b[0]]),
        PackedMontyField31Neon([w_b1, w_b2, w_b3, w_b4]),
    ];

    let dot = PackedMontyField31Neon::dot_product(&lhs, &rhs).0;

    res[..4].copy_from_slice(&dot);
    res[4] =
        MontyField31::dot_product::<5>(a[..].try_into().unwrap(), &[b[4], b[3], b[2], b[1], b[0]]);
}

/// Multiplication in the quintic trinomial extension field (X^5 + X^2 - 1).
#[inline]
pub(crate) fn quintic_mul_packed_trinomial<FP: FieldParameters>(
    a: &[MontyField31<FP>; 5],
    b: &[MontyField31<FP>; 5],
    res: &mut [MontyField31<FP>; 5],
) {
    let b0_minus_b3 = b[0] - b[3];
    let b1_minus_b4 = b[1] - b[4];
    let b4_minus_b2 = b[4] - b[2];
    let b3_plus_b4_minus_b_1 = b[3] - b1_minus_b4;

    // Constant term = a0*b0 + a1*b4 + a2*b3 + a3*b2 + a4*b1 - a4*b4
    // Linear term = a0*b1 + a1*b0 + a2*b4 + a3*b3 + a4*b2
    // Square term = a0*b2 + a1*b1 - a1*b4 + a2*b0 - a2*b3 + a3*b4 - a3*b2 + a4*b3 - a4*b1 + a4*b4
    // Cubic term = a0*b3 + a1*b2 + a2*b1 - a2*b4 + a3*b0 - a3*b3 + a4*b4 - a4*b2
    // Quartic term = a0*b4 + a1*b3 + a2*b2 + a3*b1 - a3*b4 + a4*b0 - a4*b3
    let lhs: [PackedMontyField31Neon<FP>; 5] = [
        a[0].into(),
        a[1].into(),
        a[2].into(),
        a[3].into(),
        a[4].into(),
    ];
    let rhs = [
        PackedMontyField31Neon([b[0], b[1], b[2], b[3]]),
        PackedMontyField31Neon([b[4], b[0], b1_minus_b4, b[2]]),
        PackedMontyField31Neon([b[3], b[4], b0_minus_b3, b1_minus_b4]),
        PackedMontyField31Neon([b[2], b[3], b4_minus_b2, b0_minus_b3]),
        PackedMontyField31Neon([b1_minus_b4, b[2], b3_plus_b4_minus_b_1, b4_minus_b2]),
    ];

    let dot = PackedMontyField31Neon::dot_product(&lhs, &rhs).0;

    res[..4].copy_from_slice(&dot);
    res[4] = MontyField31::dot_product::<5>(
        &[a[0], a[1], a[2], a[3], a[4]],
        &[b[4], b[3], b[2], b1_minus_b4, b0_minus_b3],
    );
}

/// Multiplication in an octic binomial extension field.
#[inline]
pub(crate) fn octic_mul_packed<FP, const WIDTH: usize>(
    a: &[MontyField31<FP>; WIDTH],
    b: &[MontyField31<FP>; WIDTH],
    res: &mut [MontyField31<FP>; WIDTH],
) where
    FP: FieldParameters + BinomialExtensionData<WIDTH>,
{
    // TODO: This could be optimised further with a custom NEON implementation.
    assert_eq!(WIDTH, 8);
    let packed_b_lo = PackedMontyField31Neon([b[0], b[1], b[2], b[3]]);
    let packed_b_hi = PackedMontyField31Neon([b[4], b[5], b[6], b[7]]);
    let w_b_lo = FP::mul_w(packed_b_lo).0;
    let w_b_hi = FP::mul_w(packed_b_hi).0;

    // Constant coefficient = a0*b0 + w(a1*b7 + ... + a7*b1)
    // Linear coefficient = a0*b1 + a1*b0 + w(a2*b7 + ... + a7*b2)
    // Square coefficient = a0*b2 + .. + a2*b0 + w(a3*b7 + ... + a7*b3)
    // Cube coefficient = a0*b3 + .. + a3*b0 + w(a4*b7 + ... + a7*b4)
    // Quartic coefficient = a0*b4 + ... + a4*b0 + w(a5*b7 + ... + a7*b5)
    // Quintic coefficient = a0*b5 + ... + a5*b0 + w(a6*b7 + ... + a7*b6)
    // Sextic coefficient = a0*b6 + ... + a6*b0 + w*a7*b7
    // Final coefficient = a0*b7 + ... + a7*b0
    let lhs: [PackedMontyField31Neon<FP>; 8] = [
        a[0].into(),
        a[1].into(),
        a[2].into(),
        a[3].into(),
        a[4].into(),
        a[5].into(),
        a[6].into(),
        a[7].into(),
    ];
    let rhs_0 = [
        PackedMontyField31Neon([b[0], b[1], b[2], b[3]]),
        PackedMontyField31Neon([w_b_hi[3], b[0], b[1], b[2]]),
        PackedMontyField31Neon([w_b_hi[2], w_b_hi[3], b[0], b[1]]),
        PackedMontyField31Neon([w_b_hi[1], w_b_hi[2], w_b_hi[3], b[0]]),
        PackedMontyField31Neon([w_b_hi[0], w_b_hi[1], w_b_hi[2], w_b_hi[3]]),
        PackedMontyField31Neon([w_b_lo[3], w_b_hi[0], w_b_hi[1], w_b_hi[2]]),
        PackedMontyField31Neon([w_b_lo[2], w_b_lo[3], w_b_hi[0], w_b_hi[1]]),
        PackedMontyField31Neon([w_b_lo[1], w_b_lo[2], w_b_lo[3], w_b_hi[0]]),
    ];
    let rhs_1 = [
        PackedMontyField31Neon([b[4], b[5], b[6], b[7]]),
        PackedMontyField31Neon([b[3], b[4], b[5], b[6]]),
        PackedMontyField31Neon([b[2], b[3], b[4], b[5]]),
        PackedMontyField31Neon([b[1], b[2], b[3], b[4]]),
        PackedMontyField31Neon([b[0], b[1], b[2], b[3]]),
        PackedMontyField31Neon([w_b_hi[3], b[0], b[1], b[2]]),
        PackedMontyField31Neon([w_b_hi[2], w_b_hi[3], b[0], b[1]]),
        PackedMontyField31Neon([w_b_hi[1], w_b_hi[2], w_b_hi[3], b[0]]),
    ];

    let dot_0 = PackedMontyField31Neon::dot_product(&lhs, &rhs_0).0;
    let dot_1 = PackedMontyField31Neon::dot_product(&lhs, &rhs_1).0;

    res[..4].copy_from_slice(&dot_0);
    res[4..].copy_from_slice(&dot_1);
}

/// Multiplication by a base field element in a binomial extension field.
#[inline]
pub(crate) fn base_mul_packed<FP, const WIDTH: usize>(
    a: [MontyField31<FP>; WIDTH],
    b: MontyField31<FP>,
    res: &mut [MontyField31<FP>; WIDTH],
) where
    FP: FieldParameters + BinomialExtensionData<WIDTH>,
{
    match WIDTH {
        1 => res[0] = a[0] * b,
        4 => {
            let lhs = PackedMontyField31Neon([a[0], a[1], a[2], a[3]]);

            let out = lhs * b;

            res.copy_from_slice(&out.0[..4]);
        }
        5 => {
            let lhs = PackedMontyField31Neon([a[0], a[1], a[2], a[3]]);

            let out = lhs * b;
            res[4] = a[4] * b;

            res[..4].copy_from_slice(&out.0[..4]);
        }
        8 => {
            let lhs_lo = PackedMontyField31Neon([a[0], a[1], a[2], a[3]]);
            let lhs_hi = PackedMontyField31Neon([a[4], a[5], a[6], a[7]]);

            let out_lo = lhs_lo * b;
            let out_hi = lhs_hi * b;

            res[..4].copy_from_slice(&out_lo.0);
            res[4..].copy_from_slice(&out_hi.0);
        }
        _ => panic!("Unsupported binomial extension degree: {}", WIDTH),
    }
}

/// Raise MontyField31 field elements to a small constant power `D`.
///
/// Currently, `D` must be one of 3, 5, or 7, if other powers are needed we can easily add them.
///
/// # Safety
/// Inputs must be signed 32-bit integers in the range `[-P, P]`.
/// Outputs will be unsigned 32-bit integers in canonical form `[0, P)`.
#[inline(always)]
#[must_use]
pub(crate) fn exp_small<PMP, const D: u64>(val: int32x4_t) -> uint32x4_t
where
    PMP: PackedMontyParameters + FieldParameters,
{
    match D {
        3 => cube::<PMP>(val),
        5 => exp_5::<PMP>(val),
        7 => exp_7::<PMP>(val),
        _ => panic!("No exp function for given D"),
    }
}