ffnt 0.11.0

Finite fields for number theory
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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use std::{
    fmt::{self, Display},
    iter::{Product, Sum},
    num::{IntErrorKind, TryFromIntError},
    ops::{
        Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
    },
    str::FromStr,
};

use crate::{ParseIntError, Z32};

/// Element of a finite field with a 64 bit characteristic `P`
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Z64<const P: u64>(u64);

impl<const P: u64> Z64<P> {
    const INFO: Z64Info = Z64Info::new(P);

    /// Minimum field element, i.e. 0
    pub const MIN: Z64<P> = {
        assert!(P > 0);
        Self::new_unchecked(0)
    };
    /// Maximum field element, i.e. `P - 1`
    pub const MAX: Z64<P> = {
        assert!(P > 1);
        Self::new_unchecked(P - 1)
    };

    /// Create a new field element corresponding to some integer
    ///
    /// The integer is reduced modulo the field characteristic `P`
    pub const fn new(z: i64) -> Self {
        let res = remi(z, P, Self::info().red_struct);
        debug_assert!(res >= 0);
        let res = res as u64;
        Self::new_unchecked(res)
    }

    /// Create a new field element corresponding to some integer
    /// without modular reduction
    ///
    /// # Safety
    ///
    /// The argument should be less than `P`
    pub const fn new_unchecked(z: u64) -> Self {
        assert!(P > 0);
        debug_assert!(z < P);
        Self(z)
    }

    /// The multiplicative inverse `1/z` of a field element `z`.
    ///
    /// # Panics
    ///
    /// Panics if `z` is not invertible. If the characteristic `P` is
    /// a prime power this happens only if `z` is zero.
    pub const fn inv(&self) -> Self {
        self.try_inv()
            .expect("Number has no multiplicative inverse")
    }

    /// The multiplicative inverse `1/z` of a field element `z` or
    /// `None` if the inverse does not exist
    pub const fn try_inv(&self) -> Option<Self> {
        let res = extended_gcd(self.0, Self::modulus());
        if res.gcd != 1 {
            return None;
        }
        let s = res.bezout[0];
        let inv = if s < 0 {
            debug_assert!(s + Self::modulus() as i64 >= 0);
            s + Self::modulus() as i64
        } else {
            s
        } as u64;
        let inv = Self::new_unchecked(inv);
        Some(inv)
    }

    /// Check if a field element `z` has a multiplicative inverse `1/z`
    ///
    /// If you know that the characteristic is a prime it is usually
    /// better to check if `z` is zero.
    pub const fn has_inv(&self) -> bool {
        gcd(self.0, Self::modulus()) == 1
    }

    const fn info() -> &'static Z64Info {
        &Self::INFO
    }

    /// The field characteristic `P`
    pub const fn modulus() -> u64 {
        P
    }

    #[allow(missing_docs)]
    pub const fn modulus_inv() -> SpInverse64 {
        Self::info().p_inv
    }

    /// `z` to some integer power `exp`
    pub fn powi(self, exp: i64) -> Self {
        if exp < 0 {
            self.powu((-exp) as u64).inv()
        } else {
            self.powu(exp as u64)
        }
    }

    /// `z` to some integer power `exp`
    pub fn powu(mut self, mut exp: u64) -> Self {
        assert!(P > 1);
        let mut res = Self::new_unchecked(1);
        while exp > 0 {
            if exp & 1 != 0 {
                res *= self
            };
            self *= self;
            exp /= 2;
        }
        res
    }

    #[cfg(any(feature = "rand", feature = "num-traits"))]
    pub(crate) const fn repr(self) -> u64 {
        self.0
    }
}

impl<const P: u64, const Q: u32> From<Z32<Q>> for Z64<P> {
    fn from(z: Z32<Q>) -> Self {
        u32::from(z).into()
    }
}

impl<const P: u64> From<Z64<P>> for u128 {
    fn from(i: Z64<P>) -> Self {
        i.0 as _
    }
}

impl<const P: u64> From<Z64<P>> for i128 {
    fn from(i: Z64<P>) -> Self {
        i.0 as _
    }
}

impl<const P: u64> From<Z64<P>> for u64 {
    fn from(i: Z64<P>) -> Self {
        i.0
    }
}

impl<const P: u64> From<Z64<P>> for i64 {
    fn from(i: Z64<P>) -> Self {
        i.0 as i64
    }
}

impl<const P: u64> TryFrom<Z64<P>> for u32 {
    type Error = TryFromIntError;

    fn try_from(i: Z64<P>) -> Result<Self, Self::Error> {
        i.0.try_into()
    }
}

impl<const P: u64> TryFrom<Z64<P>> for i32 {
    type Error = TryFromIntError;

    fn try_from(i: Z64<P>) -> Result<Self, Self::Error> {
        i.0.try_into()
    }
}

impl<const P: u64> TryFrom<Z64<P>> for u16 {
    type Error = TryFromIntError;

    fn try_from(i: Z64<P>) -> Result<Self, Self::Error> {
        i.0.try_into()
    }
}

impl<const P: u64> TryFrom<Z64<P>> for i16 {
    type Error = TryFromIntError;

    fn try_from(i: Z64<P>) -> Result<Self, Self::Error> {
        i.0.try_into()
    }
}

impl<const P: u64> TryFrom<Z64<P>> for u8 {
    type Error = TryFromIntError;

    fn try_from(i: Z64<P>) -> Result<Self, Self::Error> {
        i.0.try_into()
    }
}

impl<const P: u64> TryFrom<Z64<P>> for i8 {
    type Error = TryFromIntError;

    fn try_from(i: Z64<P>) -> Result<Self, Self::Error> {
        i.0.try_into()
    }
}

impl<const P: u64> From<u128> for Z64<P> {
    fn from(u: u128) -> Self {
        (u.rem_euclid(P as u128) as u64).into()
    }
}

impl<const P: u64> From<i128> for Z64<P> {
    fn from(i: i128) -> Self {
        (i.rem_euclid(P as i128) as u64).into()
    }
}

impl<const P: u64> From<u64> for Z64<P> {
    fn from(u: u64) -> Self {
        let num = remu(u, Self::modulus(), Self::info().red_struct) as u64;
        Self::new_unchecked(num)
    }
}

impl<const P: u64> From<i64> for Z64<P> {
    fn from(i: i64) -> Self {
        Self::new(i)
    }
}

impl<const P: u64> From<i32> for Z64<P> {
    fn from(i: i32) -> Self {
        Self::from(i as i64)
    }
}

impl<const P: u64> From<u32> for Z64<P> {
    fn from(u: u32) -> Self {
        Self::from(u as u64)
    }
}

impl<const P: u64> From<i16> for Z64<P> {
    fn from(i: i16) -> Self {
        Self::from(i as i64)
    }
}

impl<const P: u64> From<u16> for Z64<P> {
    fn from(u: u16) -> Self {
        Self::from(u as u64)
    }
}

impl<const P: u64> From<i8> for Z64<P> {
    fn from(i: i8) -> Self {
        Self::from(i as i64)
    }
}

impl<const P: u64> From<u8> for Z64<P> {
    fn from(u: u8) -> Self {
        Self::from(u as u64)
    }
}

impl<'a, const P: u64> TryFrom<&'a str> for Z64<P> {
    type Error = ParseIntError;

    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl<const P: u64> FromStr for Z64<P> {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let z = s.parse()?;
        if z >= P {
            return Err(IntErrorKind::PosOverflow.into());
        }
        // # Safety
        // we just checked that z < P
        Ok(Self::new_unchecked(z))
    }
}

impl<const P: u64> Display for Z64<P> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<const P: u64> AddAssign for Z64<P> {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<const P: u64> AddAssign<&Z64<P>> for Z64<P> {
    fn add_assign(&mut self, rhs: &Self) {
        *self = *self + *rhs;
    }
}

impl<const P: u64> SubAssign for Z64<P> {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<const P: u64> SubAssign<&Z64<P>> for Z64<P> {
    fn sub_assign(&mut self, rhs: &Self) {
        *self -= *rhs;
    }
}

impl<const P: u64> MulAssign for Z64<P> {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<const P: u64> MulAssign<&Z64<P>> for Z64<P> {
    fn mul_assign(&mut self, rhs: &Self) {
        *self = *self * *rhs;
    }
}

impl<const P: u64> DivAssign for Z64<P> {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<const P: u64> DivAssign<&Z64<P>> for Z64<P> {
    fn div_assign(&mut self, rhs: &Self) {
        *self = *self / *rhs;
    }
}

impl<const P: u64> Add for Z64<P> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let res = correct_excess((self.0 + rhs.0) as i64, Self::modulus());
        debug_assert!(res >= 0);
        let res = res as u64;
        Self::new_unchecked(res)
    }
}

impl<const P: u64> Add for &Z64<P> {
    type Output = Z64<P>;

    fn add(self, rhs: Self) -> Self::Output {
        *self + *rhs
    }
}

impl<const P: u64> Add<Z64<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn add(self, rhs: Z64<P>) -> Self::Output {
        *self + rhs
    }
}

impl<const P: u64> Add<&Z64<P>> for Z64<P> {
    type Output = Z64<P>;

    fn add(self, rhs: &Z64<P>) -> Self::Output {
        self + *rhs
    }
}

impl<const P: u64> Sub for Z64<P> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let res =
            correct_deficit(self.0 as i64 - rhs.0 as i64, Self::modulus());
        debug_assert!(res >= 0);
        let res = res as u64;
        Self::new_unchecked(res)
    }
}

impl<const P: u64> Sub for &Z64<P> {
    type Output = Z64<P>;

    fn sub(self, rhs: Self) -> Self::Output {
        *self - *rhs
    }
}

impl<const P: u64> Sub<Z64<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn sub(self, rhs: Z64<P>) -> Self::Output {
        *self - rhs
    }
}

impl<const P: u64> Sub<&Z64<P>> for Z64<P> {
    type Output = Z64<P>;

    fn sub(self, rhs: &Z64<P>) -> Self::Output {
        self - *rhs
    }
}

impl<const P: u64> Neg for Z64<P> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::default() - self
    }
}

impl<const P: u64> Mul for Z64<P> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        let num = mul_mod(self.0, rhs.0, Self::modulus(), Self::modulus_inv());
        Self::new_unchecked(num)
    }
}

impl<const P: u64> Mul for &Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: Self) -> Self::Output {
        *self * *rhs
    }
}

impl<const P: u64> Mul<Z64<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: Z64<P>) -> Self::Output {
        *self * rhs
    }
}

impl<const P: u64> Mul<&Z64<P>> for Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: &Z64<P>) -> Self::Output {
        self * *rhs
    }
}

impl<const P: u64> Div for Z64<P> {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)]
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.inv()
    }
}

const fn mul_mod(a: u64, b: u64, n: u64, ninv: SpInverse64) -> u64 {
    let res = normalised_mul_mod(
        a,
        (b as i64) << ninv.shamt,
        ((n as i64) << ninv.shamt) as u64,
        ninv.inv,
    ) >> ninv.shamt;
    res as u64
}

impl<const P: u64> Div for &Z64<P> {
    type Output = Z64<P>;

    fn div(self, rhs: Self) -> Self::Output {
        *self / *rhs
    }
}

impl<const P: u64> Div<Z64<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn div(self, rhs: Z64<P>) -> Self::Output {
        *self / rhs
    }
}

impl<const P: u64> Div<&Z64<P>> for Z64<P> {
    type Output = Z64<P>;

    fn div(self, rhs: &Z64<P>) -> Self::Output {
        self / *rhs
    }
}

/// Fallible division
pub trait TryDiv<Rhs = Self> {
    /// Result type of successfull division
    type Output;

    /// Tries to divide by the argument.
    ///
    /// `a.try_div(b)` returns `Some(a / b)` if the division
    /// is successful and `None` otherwise.
    #[must_use]
    fn try_div(self, rhs: Rhs) -> Option<Self::Output>;
}

impl<const P: u64> TryDiv for Z64<P> {
    type Output = Self;

    fn try_div(self, rhs: Self) -> Option<Self::Output> {
        rhs.try_inv().map(|i| self * i)
    }
}

impl<const P: u64> TryDiv for &Z64<P> {
    type Output = Z64<P>;

    fn try_div(self, rhs: Self) -> Option<Self::Output> {
        (*self).try_div(*rhs)
    }
}

impl<const P: u64> TryDiv<Z64<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn try_div(self, rhs: Z64<P>) -> Option<Self::Output> {
        (*self).try_div(rhs)
    }
}

impl<const P: u64> TryDiv<&Z64<P>> for Z64<P> {
    type Output = Z64<P>;

    fn try_div(self, rhs: &Z64<P>) -> Option<Self::Output> {
        self.try_div(*rhs)
    }
}

impl<const P: u64> Sum for Z64<P> {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::new_unchecked(0), |a, b| a + b)
    }
}

impl<const P: u64> Product for Z64<P> {
    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::new_unchecked(1), |a, b| a * b)
    }
}

const fn normalised_mul_mod(a: u64, b: i64, n: u64, ninv: u64) -> i64 {
    let u = a as u128 * b as u128;
    let h = (u >> (SP_NBITS - 2)) as u64;
    let q = u128_mul_high(h, ninv) >> POST_SHIFT;
    let l = u as u64;
    let r = l.wrapping_sub(q.wrapping_mul(n));
    debug_assert!(r < 2 * n);
    correct_excess(r as i64, n)
}

const fn remu(z: u64, p: u64, red: ReduceStruct) -> i64 {
    let q = u128_mul_high(z, red.ninv);
    let r = (z - q.wrapping_mul(p)) as i64;
    correct_excess(r, p)
}

const fn remi(z: i64, p: u64, red: ReduceStruct) -> i64 {
    let zu = (z as u64) & ((1u64 << (u64::BITS - 1)) - 1);
    let r = remu(zu, p, red);
    let s = i64_sign_mask(z) & (red.sgn as i64);
    correct_deficit(r - s, p)
}

const fn u128_mul_high(a: u64, b: u64) -> u64 {
    u128_get_high(a as u128 * b as u128)
}

const fn u128_get_high(u: u128) -> u64 {
    (u >> u64::BITS) as u64
}

const fn correct_excess(a: i64, p: u64) -> i64 {
    let n = p as i64;
    (a - n) + (i64_sign_mask(a - n) & n)
}

const fn correct_deficit(a: i64, p: u64) -> i64 {
    a + (i64_sign_mask(a) & (p as i64))
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct ExtendedGCDResult {
    gcd: u64,
    bezout: [i64; 2],
}

const fn extended_gcd(a: u64, b: u64) -> ExtendedGCDResult {
    let mut old_r = a;
    let mut r = b;
    let mut old_s = 1;
    let mut s = 0;
    let mut old_t = 0;
    let mut t = 1;

    while r != 0 {
        let quotient = old_r / r;
        (old_r, r) = (r, old_r - quotient * r);
        (old_s, s) = (s, old_s - quotient as i64 * s);
        (old_t, t) = (t, old_t - quotient as i64 * t);
    }
    ExtendedGCDResult {
        gcd: old_r,
        bezout: [old_s, old_t],
    }
}

const fn gcd(mut a: u64, mut b: u64) -> u64 {
    while b != 0 {
        (a, b) = (b, a % b)
    }
    a
}

const SP_NBITS: u32 = u64::BITS - 2;
const PRE_SHIFT2: u32 = 2 * SP_NBITS + 1;
const POST_SHIFT: u32 = 1;

const fn used_bits(z: u64) -> u32 {
    u64::BITS - z.leading_zeros()
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct Z64Info {
    p: u64,
    p_inv: SpInverse64,
    red_struct: ReduceStruct,
}

impl Z64Info {
    const fn new(p: u64) -> Self {
        assert!(p > 1);
        assert!(used_bits(p) <= SP_NBITS);

        let p_inv = prep_mul_mod(p);
        let red_struct = prep_rem(p);
        Self {
            p,
            p_inv,
            red_struct,
        }
    }
}

const fn prep_mul_mod(p: u64) -> SpInverse64 {
    let shamt = p.leading_zeros() - (u64::BITS - SP_NBITS);
    let inv = normalised_prep_mul_mod(p << shamt);
    SpInverse64 { inv, shamt }
}

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct ReduceStruct {
    ninv: u64,
    sgn: u64,
}

const fn prep_rem(p: u64) -> ReduceStruct {
    let mut q = (1 << (u64::BITS - 1)) / p;
    // r = 2^63 % p
    let r = (1 << (u64::BITS - 1)) - q * p;

    q *= 2;
    q += correct_excess_quo(2 * r as i64, p as i64).0;

    ReduceStruct { ninv: q, sgn: r }
}

const fn correct_excess_quo(a: i64, n: i64) -> (u64, i64) {
    if a >= n { (1, a - n) } else { (0, a) }
}

const fn i64_sign_mask(i: i64) -> i64 {
    i >> (u64::BITS - 1)
}

const fn u64_sign_mask(i: u64) -> i64 {
    i64_sign_mask(i as i64)
}

#[allow(missing_docs)]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct SpInverse64 {
    inv: u64,
    shamt: u32,
}

// Adapted from NTL's sp_NormalizedPrepMulMod
//
// Floating-point arithmetic replaced be u128 / i128 to allow `const`.
// The performance impact is not a huge concern since this function
// is only evaluated at compile time and only once for each prime field order.
// This is unlike NTL, where each change triggers a recalculation?
//
// This only works since this function is `const` and can be therefore
// used to compute individual `const INFO` inside `Z64<P>` for each
// `P`. The alternatives `lazy_static!` or `OnceCell` would not be
// recomputed, but instead incorrectly shared between `Z64<P>` with
// different `P`!
const fn normalised_prep_mul_mod(n: u64) -> u64 {
    // NOTE: this is an initial approximation
    //       the true quotient is <= 2^SP_NBITS
    const MAX: u128 = 1u128 << (2 * SP_NBITS - 1);
    let init_quot_approx = MAX / n as u128;

    let approx_rem = MAX - n as u128 * init_quot_approx;

    let approx_rem = (approx_rem << (PRE_SHIFT2 - 2 * SP_NBITS + 1)) - 1;

    let approx_rem_low = approx_rem as u64;
    let s1 = (approx_rem >> u64::BITS) as u64;
    let s2 = approx_rem_low >> (u64::BITS - 1);
    let approx_rem_high = s1.wrapping_add(s2);

    let approx_rem_low = approx_rem_low as i64;
    let approx_rem_high = approx_rem_high as i64;

    let bpl = 1i128 << u64::BITS;

    let fr = approx_rem_low as i128 + approx_rem_high as i128 * bpl;

    // now convert fr*ninv to a long
    // but we have to be careful: fr may be negative.
    // the result should still give floor(r/n) pm 1,
    // and is computed in a way that avoids branching

    let mut q1 = (fr / n as i128) as i64;
    if q1 < 0 {
        // This counteracts the round-to-zero behavior of conversion
        // to i64.  It should be compiled into branch-free code.
        q1 -= 1
    }

    let mut q1 = q1 as u64;
    let approx_rem_low = approx_rem_low as u64;
    let sub = q1.wrapping_mul(n);

    let approx_rem = approx_rem_low.wrapping_sub(sub);

    q1 += (1
        + u64_sign_mask(approx_rem)
        + u64_sign_mask(approx_rem.wrapping_sub(n))) as u64;

    ((init_quot_approx as u64) << (PRE_SHIFT2 - 2 * SP_NBITS + 1))
        .wrapping_add(q1)

    // NTL_PRE_SHIFT1 is 0, so no further shift required
}

/// [Z64] variant for fast repeated multiplication
///
/// Use this variant to speed up repeated multiplication by the same value:
/// ```
/// # use ffnt::{Z64, z64::Z64FastMul};
/// const P: u64 = 10007;
/// let mut numbers = Vec::from_iter((1..1000).map(Z64::<P>::from));
///
/// let factor: Z64FastMul<P> = 12.into();
/// for number in &mut numbers {
///     // same as `number *= Z64::from(12)`, but faster
///     *number *= &factor;
/// }
/// ```
///
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Z64FastMul<const P: u64> {
    val: Z64<P>,
    val_over_mod_approx: u64,
}

impl<const P: u64> From<Z64<P>> for Z64FastMul<P> {
    fn from(val: Z64<P>) -> Self {
        let val_over_mod_approx = Self::prep_mul_mod_precon(val.0);
        Self {
            val,
            val_over_mod_approx,
        }
    }
}

impl<const P: u64> Z64FastMul<P> {
    fn prep_mul_mod_precon(val: u64) -> u64 {
        let p_inv = Z64::<P>::INFO.p_inv;
        normalized_prep_mul_mod_precon(
            val << p_inv.shamt,
            P << p_inv.shamt,
            p_inv.inv,
        ) << (u64::BITS - SP_NBITS)
    }
}

fn normalized_prep_mul_mod_precon(val: u64, p: u64, p_inv: u64) -> u64 {
    let h = val << 2;
    let q = u128_mul_high(h, p_inv) >> POST_SHIFT;
    let l = val << SP_NBITS;
    let r = l.wrapping_sub(q.wrapping_mul(p)); // r in [0..2*p)
    debug_assert!(r < 2 * p);

    q.saturating_add_signed(1 + i64_sign_mask(r as i64 - p as i64)) // NOTE: not shifted
}

impl<const P: u64> Mul<Z64FastMul<P>> for Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: Z64FastMul<P>) -> Self::Output {
        let res = mul_mod_precon(self.0, rhs.val.0, P, rhs.val_over_mod_approx);
        Z64::new_unchecked(res as u64)
    }
}

impl<const P: u64> Mul<Z64<P>> for Z64FastMul<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: Z64<P>) -> Self::Output {
        rhs * self
    }
}

impl<const P: u64> Mul<Z64FastMul<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: Z64FastMul<P>) -> Self::Output {
        *self * rhs
    }
}

impl<const P: u64> Mul<Z64<P>> for &Z64FastMul<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: Z64<P>) -> Self::Output {
        *self * rhs
    }
}

impl<const P: u64> Mul<&Z64FastMul<P>> for Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: &Z64FastMul<P>) -> Self::Output {
        self * *rhs
    }
}

impl<const P: u64> Mul<&Z64<P>> for Z64FastMul<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: &Z64<P>) -> Self::Output {
        self * *rhs
    }
}

impl<'a, const P: u64> Mul<&'a Z64FastMul<P>> for &Z64<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: &'a Z64FastMul<P>) -> Self::Output {
        *self * *rhs
    }
}

impl<'a, const P: u64> Mul<&'a Z64<P>> for &Z64FastMul<P> {
    type Output = Z64<P>;

    fn mul(self, rhs: &'a Z64<P>) -> Self::Output {
        *self * *rhs
    }
}

impl<const P: u64> MulAssign<Z64FastMul<P>> for Z64<P> {
    fn mul_assign(&mut self, rhs: Z64FastMul<P>) {
        *self = *self * rhs
    }
}

impl<const P: u64> MulAssign<&Z64FastMul<P>> for Z64<P> {
    fn mul_assign(&mut self, rhs: &Z64FastMul<P>) {
        *self = *self * rhs
    }
}

fn mul_mod_precon(lhs: u64, rhs: u64, p: u64, rhs_over_mod_approx: u64) -> i64 {
    let q = u128_mul_high(lhs, rhs_over_mod_approx);
    let lhs_times_rhs = lhs.wrapping_mul(rhs);
    let q_times_p = q.wrapping_mul(p);
    let r = lhs_times_rhs.wrapping_sub(q_times_p);
    correct_excess(r as i64, p)
}

macro_rules! impl_fastmul_from {
    ( $( $t:ty ),* ) => {
        $(
            impl<const P: u64> From<$t> for Z64FastMul<P> {
                fn from(t: $t) -> Self {
                    Self::from(Z64::from(t))
                }
            }
        )*
    }
}

impl_fastmul_from!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);

impl<const P: u64> From<Z64FastMul<P>> for Z64<P> {
    fn from(z: Z64FastMul<P>) -> Self {
        z.val
    }
}

#[cfg(test)]
mod tests {

    use ::rand::{Rng, SeedableRng};
    use once_cell::sync::Lazy;
    use rug::{Integer, ops::Pow};

    use super::*;

    const PRIMES: [u64; 3] = [3, 443619635352171979, 1152921504606846883];

    #[test]
    fn z64_has_inv() {
        type Z = Z64<6>;
        assert!(!Z::from(0).has_inv());
        assert!(Z::from(1).has_inv());
        assert!(!Z::from(2).has_inv());
        assert!(!Z::from(3).has_inv());
        assert!(!Z::from(4).has_inv());
        assert!(Z::from(5).has_inv());
        assert_eq!(Z::from(6), Z::from(0));
    }

    #[test]
    #[should_panic]
    fn z64_inv0() {
        type Z = Z64<6>;
        Z::from(0).inv();
    }

    #[test]
    #[should_panic]
    fn z64_inv2() {
        type Z = Z64<6>;
        Z::from(2).inv();
    }

    #[test]
    fn z64_constr() {
        let z: Z64<3> = 2.into();
        assert_eq!(u64::from(z), 2);
        let z: Z64<3> = (-1).into();
        assert_eq!(u64::from(z), 2);
        let z: Z64<3> = 5.into();
        assert_eq!(u64::from(z), 2);

        let z: Z64<3> = 0.into();
        assert_eq!(u64::from(z), 0);
        let z: Z64<3> = 3.into();
        assert_eq!(u64::from(z), 0);

        let z: Z64<3> = 2u32.into();
        assert_eq!(u64::from(z), 2);
        let z: Z64<3> = 5u32.into();
        assert_eq!(u64::from(z), 2);

        let z: Z64<3> = 0u32.into();
        assert_eq!(u64::from(z), 0);
        let z: Z64<3> = 3u32.into();
        assert_eq!(u64::from(z), 0);
    }

    static POINTS: Lazy<[i64; 1000]> = Lazy::new(|| {
        let mut pts = [0; 1000];
        let mut rng = rand_xoshiro::Xoshiro256StarStar::seed_from_u64(0);
        for pt in &mut pts {
            *pt = rng.random();
        }
        pts
    });

    #[test]
    fn tst_conv() {
        for pt in *POINTS {
            let z: Z64<{ PRIMES[0] }> = pt.into();
            let z: i64 = z.into();
            assert_eq!(z, pt.rem_euclid(PRIMES[0] as i64));
        }

        for pt in *POINTS {
            let z: Z64<{ PRIMES[1] }> = pt.into();
            let z: i64 = z.into();
            assert_eq!(z, pt.rem_euclid(PRIMES[1] as i64));
        }

        for pt in *POINTS {
            let z: Z64<{ PRIMES[2] }> = pt.into();
            let z: i64 = z.into();
            assert_eq!(z, pt.rem_euclid(PRIMES[2] as i64));
        }
    }

    #[test]
    fn tst_add() {
        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[0] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[0] }> = pt2.into();
                let pt2 = pt2 as i128;
                let sum1: i64 = (z1 + z2).into();
                let sum2 = (pt1 + pt2).rem_euclid(PRIMES[0] as i128) as i64;
                assert_eq!(sum1, sum2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[1] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[1] }> = pt2.into();
                let pt2 = pt2 as i128;
                let sum1: i64 = (z1 + z2).into();
                let sum2 = (pt1 + pt2).rem_euclid(PRIMES[1] as i128) as i64;
                assert_eq!(sum1, sum2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[2] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[2] }> = pt2.into();
                let pt2 = pt2 as i128;
                let sum1: i64 = (z1 + z2).into();
                let sum2 = (pt1 + pt2).rem_euclid(PRIMES[2] as i128) as i64;
                assert_eq!(sum1, sum2);
            }
        }
    }

    #[test]
    fn tst_sub() {
        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[0] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[0] }> = pt2.into();
                let pt2 = pt2 as i128;
                let sum1: i64 = (z1 - z2).into();
                let sum2 = (pt1 - pt2).rem_euclid(PRIMES[0] as i128) as i64;
                assert_eq!(sum1, sum2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[1] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[1] }> = pt2.into();
                let pt2 = pt2 as i128;
                let sum1: i64 = (z1 - z2).into();
                let sum2 = (pt1 - pt2).rem_euclid(PRIMES[1] as i128) as i64;
                assert_eq!(sum1, sum2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[2] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[2] }> = pt2.into();
                let pt2 = pt2 as i128;
                let sum1: i64 = (z1 - z2).into();
                let sum2 = (pt1 - pt2).rem_euclid(PRIMES[2] as i128) as i64;
                assert_eq!(sum1, sum2);
            }
        }
    }

    #[test]
    fn tst_mul() {
        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[0] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[0] }> = pt2.into();
                let pt2 = pt2 as i128;
                let prod1: i64 = (z1 * z2).into();
                let prod2 = (pt1 * pt2).rem_euclid(PRIMES[0] as i128) as i64;
                assert_eq!(prod1, prod2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[1] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[1] }> = pt2.into();
                let pt2 = pt2 as i128;
                let prod1: i64 = (z1 * z2).into();
                let prod2 = (pt1 * pt2).rem_euclid(PRIMES[1] as i128) as i64;
                assert_eq!(prod1, prod2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[2] }> = pt1.into();
            let pt1 = pt1 as i128;
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[2] }> = pt2.into();
                let pt2 = pt2 as i128;
                let prod1: i64 = (z1 * z2).into();
                let prod2 = (pt1 * pt2).rem_euclid(PRIMES[2] as i128) as i64;
                assert_eq!(prod1, prod2);
            }
        }
    }

    #[test]
    fn tst_fastmul() {
        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[0] }> = pt1.into();
            let fast_z1 = Z64FastMul::from(z1);
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[0] }> = pt2.into();
                assert_eq!(z1 * z2, fast_z1 * z2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[1] }> = pt1.into();
            let fast_z1 = Z64FastMul::from(z1);
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[1] }> = pt2.into();
                assert_eq!(z1 * z2, fast_z1 * z2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[2] }> = pt1.into();
            let fast_z1 = Z64FastMul::from(z1);
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[2] }> = pt2.into();
                assert_eq!(z1 * z2, fast_z1 * z2);
            }
        }
    }

    #[test]
    fn tst_div() {
        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[0] }> = pt1.into();
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[0] }> = pt2.into();
                if i64::from(z2) == 0 {
                    continue;
                }
                let div = z1 / z2;
                assert_eq!(z1, div * z2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[1] }> = pt1.into();
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[1] }> = pt2.into();
                if i64::from(z2) == 0 {
                    continue;
                }
                let div = z1 / z2;
                assert_eq!(z1, div * z2);
            }
        }

        for pt1 in *POINTS {
            let z1: Z64<{ PRIMES[2] }> = pt1.into();
            for pt2 in *POINTS {
                let z2: Z64<{ PRIMES[2] }> = pt2.into();
                if i64::from(z2) == 0 {
                    continue;
                }
                let div = z1 / z2;
                assert_eq!(z1, div * z2);
            }
        }
    }

    #[test]
    fn tst_pow() {
        let mut rng = rand_xoshiro::Xoshiro256StarStar::seed_from_u64(2849);
        for pt1 in *POINTS {
            let base = Integer::from(pt1);
            for _ in 0..100 {
                let exp: u8 = rng.random();
                let pow = base.clone().pow(exp as u32);
                // ensure remainder is positive and less than the mod
                let ref_pow0 =
                    (pow.clone() % PRIMES[0] + PRIMES[0]) % PRIMES[0];
                let ref_pow0: u64 = ref_pow0.try_into().unwrap();
                let z: Z64<{ PRIMES[0] }> = pt1.into();
                let pow0: u64 = z.powu(exp as u64).into();
                assert_eq!(pow0, ref_pow0);

                let ref_pow0 =
                    (pow.clone() % PRIMES[1] + PRIMES[1]) % PRIMES[1];
                let ref_pow0: u64 = ref_pow0.try_into().unwrap();
                let z: Z64<{ PRIMES[1] }> = pt1.into();
                let pow0: u64 = z.powu(exp as u64).into();
                assert_eq!(pow0, ref_pow0);

                let ref_pow0 = (pow % PRIMES[2] + PRIMES[2]) % PRIMES[2];
                let ref_pow0: u64 = ref_pow0.try_into().unwrap();
                let z: Z64<{ PRIMES[2] }> = pt1.into();
                let pow0: u64 = z.powu(exp as u64).into();
                assert_eq!(pow0, ref_pow0);
            }
        }
    }

    #[test]
    fn tst_from() {
        const P: u64 = 1152921504606846883;
        let z = 9723086427719839101u64;
        assert_eq!(u64::from(Z64::<P>::from(z)), 499714390865064037u64);
    }
}