-
Notifications
You must be signed in to change notification settings - Fork 0
/
long_extras.c
2819 lines (2363 loc) · 65.4 KB
/
long_extras.c
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
/*============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/****************************************************************************
long_extras.c: extra functions for longs and unsigned longs
Copyright (C) 2007, 2008 William Hart
Copyright (C) 2008, Peter Shrimpton
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <math.h>
#include <gmp.h>
#include "flint.h"
#include "long_extras.h"
#include "longlong_wrapper.h"
#include "longlong.h"
#include "memory-manager.h"
#ifndef __TINYC__
#include "QS/tinyQS.h"
#endif
#define MIN_HOLF 0xFFFFFUL
#define MAX_HOLF 0x1FFFFFFFFFUL
#define HOLF_MULTIPLIER 480
#define HOLF_ITERS 50000
/*
Generate a random integer in the range [0, limit)
If limit == 0, return a random limb
*/
const unsigned int z_primes[] =
{
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,
191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,
281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,
389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,
491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,
607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,
719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,
829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,
953,967,971,977,983,991,997
};
unsigned long z_randint(unsigned long limit)
{
#if FLINT_BITS == 32
static uint64_t randval = 4035456057U;
randval = ((uint64_t)randval*(uint64_t)1025416097U+(uint64_t)286824430U)%(uint64_t)4294967311U;
if (limit == 0L) return (unsigned long) randval;
return (unsigned long)randval%limit;
#else
static unsigned long randval = 4035456057U;
static unsigned long randval2 = 6748392731U;
randval = ((unsigned long)randval*(unsigned long)1025416097U+(unsigned long)286824428U)%(unsigned long)4294967311U;
randval2 = ((unsigned long)randval2*(unsigned long)1647637699U+(unsigned long)286824428U)%(unsigned long)4294967357U;
if (limit == 0L) return (unsigned long) (randval+(randval2<<32));
return (unsigned long)(randval+(randval2<<32))%limit;
#endif
}
/*
Generate a random integer with up to the given number of bits [0, FLINT_BITS]
*/
unsigned long z_randbits(unsigned long bits)
{
return z_randint(l_shift(1L, bits));
}
/*
Generates a random prime of _bits_ bits.
If proved is o (false) the prime is not proven prime, otherwise it is.
*/
unsigned long z_randprime(unsigned long bits, int proved)
{
unsigned long limit, rand;
if (bits < 2)
{
printf("FLINT Exception: attempt to generate prime < 2!\n");
abort();
}
if (bits == FLINT_BITS)
{
do
{
rand = z_randbits(bits);
#if FLINT_BITS == 32
} while (rand > 4294967290UL);
#else
} while (rand > 18446744073709551556UL);
#endif
rand = z_nextprime(rand, proved);
} else
{
do
{
rand = z_randbits(bits);
rand = z_nextprime(rand, proved);
} while ((rand >> bits) > 0L);
}
return rand;
}
/*
Computes a double floating point approximate inverse,
i.e. 53 bits of 1 / n
*/
double z_precompute_inverse(unsigned long n)
{
return (double) 1 / (double) n;
}
/*
Returns a % n given a precomputed approx inverse ninv
Operation is *unsigned*
Requires that n be no more than FLINT_D_BITS bits and _a_ be less than n^2
*/
unsigned long z_mod_precomp(unsigned long a, unsigned long n, double ninv)
{
if (a < n) return a;
unsigned long quot = (unsigned long) ((double) a * ninv);
unsigned long rem = a - quot*n;
if (rem >= n) return rem - n;
else return rem;
}
/*
Returns a / n given a precomputed approx inverse ninv
Operation is *unsigned*
Requires that n be no more than FLINT_BITS-1 bits but there are no
restrictions on _a_
*/
unsigned long z_div_64_precomp(unsigned long a, unsigned long n, double ninv)
{
if (a < n) return 0;
unsigned long quot = (unsigned long) ((double) a * ninv);
long rem = a - quot*n;
if (rem < (long)(-n)) quot -= (unsigned long) ((double) (-rem) * ninv);
else if (rem >= (long) n) quot += (unsigned long) ((double) rem * ninv);
else if (rem < 0L) return quot - 1;
else return quot;
rem = a - quot*n;
if (rem >= (long) n) return quot + 1;
else if (rem < 0L) return quot - 1;
else return quot;
}
/*
Returns a % n given a precomputed approx inverse ninv
Operation is *unsigned*
Requires that n be no more than FLINT_BITS-1 bits but there are no
restrictions on _a_
*/
unsigned long z_mod_64_precomp(unsigned long a, unsigned long n, double ninv)
{
if (a < n) return a;
unsigned long quot = (unsigned long) ((double) a * ninv);
long rem = a - quot*n;
if (rem < (long)(-n)) quot -= (unsigned long) ((double) (-rem) * ninv);
else if (rem >= (long) n) quot += (unsigned long) ((double) rem * ninv);
else if (rem < 0L) return rem + n;
else return rem;
rem = a - quot*n;
if (rem >= (long) n) return rem - n;
else if (rem < 0L) return rem + n;
else return rem;
}
/*
Computes a_hi a_lo mod n given an approximate inverse
Assumes n is no more than FLINT_BITS-1 bits, but there are no
restrictions on a_hi or a_lo
Operation is unsigned.
*/
unsigned long z_ll_mod_precomp(unsigned long a_hi, unsigned long a_lo,
unsigned long n, double ninv)
{
unsigned long t1;
unsigned long norm, q, r, orig_n;
if (a_hi >= n)
{
if (((n>>(FLINT_BITS/2)) == 0) && (a_hi >= n*n)) a_hi = a_hi%n;
else a_hi = z_mod2_precomp(a_hi, n, ninv);
}
#if UDIV_NEEDS_NORMALIZATION
count_lead_zeros(norm, n);
udiv_qrnnd(q, r, (a_hi<<norm) + (a_lo>>(FLINT_BITS-norm)), a_lo<<norm, n<<norm);
r >>= norm;
#else
udiv_qrnnd(q, r, a_hi, a_lo, n);
#endif
return r;
}
/*
I don't trust the code below. It is for precomputed inverses of 32 bits.
It should work without modification up to 32 bits, but does not.
The code is a brutalisation of code found in GMP. */
#if FLINT_BITS == 64
/*
Computes a 32 bit approximation to 1/xl - 1
Here xl is thought of as being between 0.5 and (2^32 - 1)/2^32
This is basically a 33 bit approximation to 1/xl without the leading 1
The product of this and a 64 bit integer will give the quotient which
will at worst be 1 too small
*/
#define invert_limb32(invxl, xl) \
do { \
invxl = ((~(((unsigned long)xl)<<32))/xl); \
} while (0)
/*
-1 if high bit set
0 otherwise
*/
#define LIMB_HIGHBIT_TO_MASK32(n) \
(((n) & (1<<31)) ? (~0) : (0))
/*uint32_t z_mod32_precomp(unsigned long n64, uint32_t d, uint32_t di)
{
uint32_t xh, xl, nh, nl, nmask, nadj, q1;
unsigned long x;
nh = ((uint32_t) (n64 >> 32));
nl = ((uint32_t) n64);
nmask = LIMB_HIGHBIT_TO_MASK32(nl); // nmask = -1 if high bit of nl is set, else nmask = 0
nadj = nl + (nmask & d); // nadj = nl if high bit of nl was 0, else it is nl + d - 2^32
// di may be too small, so that if we compute q = floor( (2^32+di)*nh + (2^32+di)*nl/2^32 )
// we will not be more than 1 out (after shifting by 32) even if we only take notice of
// the top bit of nl
// thus if that top bit is 0 we can use q = (2^32+di)*nh + nl
// if the top bit is 1 then q = (2^32+di)*nh + nl may be 2 too small after shifting by 32
// we need to add di/2 to q to ensure that we are no more than one out after shifting by 32
x = (unsigned long) di * (unsigned long) (nh - nmask); // x = di * nh if high bit of nl was 0
// else x = di * (nh + 1)
x += (((unsigned long)nh)<<32) + (unsigned long) nadj; // x = di * nh + n64 if high bit of nl was 0
// else x = di * (nh + 1) + 2^32 * nh - 2^32 + nl + d
q1 = ~(x>>32); // if high bit of nl is 0 then we want q = (2^32+di)*nh >> 32
// else we get q = (2^32+di)*nh + nl + (di + d) - 2^32
// and q1 = 2^32 - q - 1
x = (unsigned long) q1 * (unsigned long) d;
x += n64; // now x = n64 + 2^32*d - q*d - d
xh = (uint32_t) (x >> 32);
xl = (uint32_t) x;
xh -= d; // makes up for the 2^32, and now xh is -1 or 0
return xl + ((xh) & d); // if xh is -1 we have gone too far, so add d, else we are done
} */
uint32_t z_mod32_precomp(unsigned long n64, uint32_t d, uint32_t di)
{
uint32_t xl, nh;
unsigned long q, x;
nh = ((uint32_t) (n64 >> 32));
q = (unsigned long) nh * (unsigned long) di;
q += (((unsigned long)nh)<<32); // Compensate, di is 2^FLINT_BITS too small
// As we only use nh which is only 32 bits, the quotient may be incorrect in
// the last 2 bits, i.e. it may be up to 3 too small
// we adjust for this below
x = (q>>32)*(unsigned long)d;
x = n64 - x;
if (x>>32)
{
x -= (unsigned long) d;
if (x>>32) x -= (unsigned long) d;
}
xl = (uint32_t) x;
if (xl >= d) xl -= d;
return xl;
}
uint32_t z_precompute_inverse32(unsigned long n)
{
unsigned long norm;
count_lead_zeros(norm, n);
uint32_t ninv;
invert_limb32(ninv, (n<<(norm-32)));
return ninv;
}
unsigned long z_mulmod32_precomp(unsigned long a, unsigned long b,
unsigned long n, uint32_t ninv)
{
unsigned long norm;
unsigned long prod = a*b;
count_lead_zeros(norm, n);
norm -= 32;
unsigned long res = (unsigned long) z_mod32_precomp(prod<<norm, n<<norm, ninv);
res >>= norm;
return res;
}
#endif
/*
Computes a*b mod n, given a precomputed inverse ninv
Assumes a an b are both in [0,n).
Requires that n be no more than FLINT_D_BITS bits
*/
unsigned long z_mulmod_precomp(unsigned long a, unsigned long b,
unsigned long n, double ninv)
{
unsigned long quot = (unsigned long) ((double) a * (double) b * ninv);
long rem = a*b - quot*n;
if (rem < 0)
{
rem += n;
if (rem < 0) return rem + n;
} else if (rem >= n) return rem - n;
return rem;
}
/*
Computes a*b mod n, given a precomputed inverse ninv
Assumes a an b are both in [0,n). There is no restriction on a*b,
i.e. it can be two limbs
*/
unsigned long z_mulmod_64_precomp(unsigned long a, unsigned long b, unsigned long n,
double ninv)
{
unsigned long p1, p2;
umul_ppmm(p2, p1, a, b);
return z_ll_mod_precomp(p2, p1, n, ninv);
}
/*
Returns a^exp modulo n
Assumes a is reduced mod n
Requires that n be no more than FLINT_D_BITS bits
There are no restrictions on exp, which can also be negative.
*/
unsigned long z_powmod(unsigned long a, long exp, unsigned long n)
{
double ninv = z_precompute_inverse(n);
unsigned long x, y;
unsigned long e;
if (exp < 0)
e = (unsigned long) -exp;
else
e = exp;
x = 1;
y = a;
while (e) {
if (e & 1) x = z_mulmod_precomp(x, y, n, ninv);
y = z_mulmod_precomp(y, y, n, ninv);
e = e >> 1;
}
if (exp < 0) x = z_invert(x, n);
return x;
}
/*
Returns a^exp modulo n
Assumes a is reduced mod n
Requires that n be no more than FLINT_BITS-1 bits
There are no restrictions on exp, which can also be negative.
*/
unsigned long z_powmod_64(unsigned long a, long exp, unsigned long n)
{
double ninv = z_precompute_inverse(n);
unsigned long x, y;
unsigned long e;
if (exp < 0)
e = (unsigned long) -exp;
else
e = exp;
x = 1;
y = a;
while (e) {
if (e & 1) x = z_mulmod_64_precomp(x, y, n, ninv);
y = z_mulmod_64_precomp(y, y, n, ninv);
e = e >> 1;
}
if (exp < 0) x = z_invert(x, n);
return x;
}
/*
Returns a^exp modulo n given a precomputed inverse
Assumes a is reduced mod n
Requires that n be no more than FLINT_D_BITS bits
There are no restrictions on exp, which may also be negative
*/
unsigned long z_powmod_precomp(unsigned long a, long exp,
unsigned long n, double ninv)
{
unsigned long x, y;
unsigned long e;
if (exp < 0)
e = (unsigned long) -exp;
else
e = exp;
x = 1;
y = a;
while (e) {
if (e & 1) x = z_mulmod_precomp(x, y, n, ninv);
y = z_mulmod_precomp(y, y, n, ninv);
e = e >> 1;
}
if (exp < 0) x = z_invert(x, n);
return x;
}
/*
Returns a^exp modulo n given a precomputed inverse
Assumes a is reduced mod n
Requires that n be no more than FLINT_BITS-1 bits
There are no restrictions on exp, which may also be negative
*/
unsigned long z_powmod_64_precomp(unsigned long a, long exp,
unsigned long n, double ninv)
{
unsigned long x, y;
unsigned long e;
if (exp < 0)
e = (unsigned long) -exp;
else
e = exp;
x = 1;
y = a;
while (e) {
if (e & 1) x = z_mulmod_64_precomp(x, y, n, ninv);
y = z_mulmod_64_precomp(y, y, n, ninv);
e = e >> 1;
}
if (exp < 0) x = z_invert(x, n);
return x;
}
/*
Computes the Legendre symbol of _a_ modulo p
Assumes p is a prime of no more than FLINT_BITS-1 bits and that _a_
is reduced modulo p
*/
int z_legendre_precomp(unsigned long a, unsigned long p, double pinv)
{
if (a == 0) return 0;
if (z_powmod2_precomp(a, (p-1)/2, p, pinv) == p-1) return -1;
else return 1;
}
/*
Calculates the jacobi symbol (x/y)
Assumes that gcd(x,y) = 1 and y is odd
*/
int z_jacobi(long x, unsigned long y)
{
unsigned long b, temp;
long a;
int s, exp;
pre_inv2_t inv;
a = x;
b = y;
s = 1;
if (a < 0)
{
if (((b-1)/2)%2 == 1)
{
s = -s;
}
a = -a;
}
a = a % b;
if (a == 0)
{
if (b == 1) return 1;
else return 0;
}
while (b != 1)
{
a = a % b;
exp = z_remove(&a, 2);
if (((exp*(b*b-1))/8)%2 == 1) // we are only interested in values mod 8,
{ //so overflows don't matter here
s = -s;
}
temp = a;
a = b;
b = temp;
if (((a-1)*(b-1)/4)%2 == 1) // we are only interested in values mod 4,
{ //so overflows don't matter here
s = -s;
}
}
return s;
}
/*
Computes a square root of _a_ modulo p.
Assumes p is a prime of no more than FLINT_BITS-1 bits,
that _a_ is reduced modulo p.
Returns 0 if _a_ is a quadratic non-residue modulo p.
*/
unsigned long z_sqrtmod(unsigned long a, unsigned long p)
{
unsigned int r, k, m;
unsigned long p1, b, g, bpow, gpow, res;
double pinv;
if ((a==0) || (a==1))
{
return a;
}
pinv = z_precompute_inverse(p);
if (z_legendre_precomp(a, p, pinv) == -1) return 0;
if ((p&3)==3)
{
return z_powmod2_precomp(a, (p+1)/4, p, pinv);
}
r = 0;
p1 = p-1;
do {
p1>>=1UL;
r++;
} while ((p1&1UL) == 0);
b = z_powmod2_precomp(a, p1, p, pinv);
for (k=2UL; ;k++)
{
if (z_legendre_precomp(k, p, pinv) == -1) break;
}
g = z_powmod2_precomp(k, p1, p, pinv);
res = z_powmod2_precomp(a, (p1+1)/2, p, pinv);
if (b == 1UL)
{
return res;
}
while (b != 1)
{
bpow = b;
for (m = 1; (m <= r-1) && (bpow != 1); m++)
{
bpow = z_mulmod2_precomp(bpow, bpow, p, pinv);
}
gpow = g;
int i;
for (i = 1; i < r-m; i++)
{
gpow = z_mulmod2_precomp(gpow, gpow, p, pinv);
}
res = z_mulmod2_precomp(res, gpow, p, pinv);
gpow = z_mulmod2_precomp(gpow, gpow, p, pinv);
b = z_mulmod2_precomp(b, gpow, p, pinv);
gpow = g;
r = m;
}
return res;
}
/*
Computes a cube root of _a_ mod p for a prime p and returns a cube
root of unity if the cube roots of _a_ are distinct else the cube
root is set to 1
If _a_ is not a cube modulo p then 0 is returned
This function assumes _a_ is reduced modulo p
Requires p be no more than FLINT_BITS-1 bits
*/
unsigned long z_cuberootmod(unsigned long * cuberoot1, unsigned long a,
unsigned long p)
{
unsigned long x;
double pinv;
if (a == 0) return 0;
pinv = z_precompute_inverse(p);
if ((p % 3) == 2)
{
*cuberoot1 = 1;
return z_powmod2_precomp(a, 2*((p+1)/3)-1, p, pinv);
}
unsigned long e=0;
unsigned long q = p-1;
unsigned long l;
unsigned long n = 2;
unsigned long z, y, r, temp, temp2, b, m, s, t;
r = 1;
while ((q%3) == 0)
{
q = q/3;
e++;
}
l = q%3;
x = z_powmod2_precomp(a, (q-l)/3, p, pinv);
temp = z_powmod2_precomp(a, l, p, pinv);
temp2 = z_powmod2_precomp(x, 3UL, p, pinv);
b = z_mulmod2_precomp(temp, temp2, p, pinv);
if (l == 2) x = z_mulmod2_precomp(a, x, p, pinv);
while(z_powmod2_precomp(n, (p-1)/3, p, pinv)==1) n++;
z = z_powmod2_precomp(n, q, p, pinv);
y = z;
r = e;
while (b!=1)
{
s = z_powmod2_precomp(b, 3UL, p, pinv);
m = 1;
while(s!=1)
{
s = z_powmod2_precomp(s, 3UL, p, pinv);
m++;
}
if(m>=r) return(0);
t = z_powmod2_precomp(y, z_pow(3UL, r-m-1UL), p, pinv);
y = z_powmod2_precomp(t, 3UL, p, pinv);
r = m;
x = z_mulmod2_precomp(t, x, p, pinv);
b = z_mulmod2_precomp(y, b, p, pinv);
}
if (r==1) *cuberoot1 = y;
else *cuberoot1 = z_powmod2_precomp(y, z_pow(3UL, r-1), p, pinv);
if (l==2) return(x);
else return(z_invert(x, p));
}
/*
returns a^exp
*/
unsigned long z_pow(unsigned long a, unsigned long exp)
{
if (exp == 0) return 1;
if (a == 1) return 1;
unsigned long power = a;
unsigned long i;
for (i = 1; i < exp; i++)
power *= a;
return power;
}
/*
Checks to see if n is fermat pseudoprime with base i
Assumes n doesn't divide i
*/
int z_ispseudoprime_fermat(unsigned long const n, unsigned long const i)
{
/*
By Fermats little thm if i^n-1 is not congruent to 1 then n is not prime
*/
return (z_powmod2(i, n-1, n) == 1);
}
/*
Tests whether n is an a-Strong Pseudo Prime
Assumes d is set to the largest odd factor of n-1
Assumes n is at most FLINT_D_BITS bits
Requires _a_ to be reduced mod n
*/
static inline
int SPRP(unsigned long a, unsigned long d, unsigned long n, double ninv)
{
unsigned long t = d;
unsigned long y;
y = z_powmod_precomp(a, t , n, ninv);
while ((t != n-1) && (y != 1) && (y != n-1))
{
y = z_mulmod_precomp(y, y, n, ninv);
t <<= 1;
}
if ((y != n-1) && ((t&1) == 0)) return 0;
return 1;
}
/*
Tests whether n is an a-Strong Pseudo Prime
Assumes d is set to the largest odd factor of n-1
Assumes n is at most FLINT_BITS-1 bits
Requires _a_ to be reduced mod n
*/
static inline
int SPRP_64(unsigned long a, unsigned long d, unsigned long n, double ninv)
{
unsigned long t = d;
unsigned long y;
y = z_powmod_64_precomp(a, t , n, ninv);
while ((t != n-1) && (y != 1) && (y != n-1))
{
y = z_mulmod_64_precomp(y, y, n, ninv);
t <<= 1;
}
if ((y != n-1) && ((t&1) == 0)) return 0;
return 1;
}
/*
Miller-Rabin primality test. If reps is set to 5 a couple of
pseudoprimes on average will pass the test out of each 10^11 tests.
Every increase of reps by 1 decreases the chance or composites
passing by a factor of 4.
Requires n be no more than FLINT_BITS-1 bits
*/
int z_miller_rabin_precomp(unsigned long n, double ninv, unsigned long reps)
{
unsigned long d = n-1, a, t, y;
do {
d>>=1UL;
} while ((d&1UL) == 0);
unsigned long i;
for (i = 0; i < reps; i++)
{
a = z_randint(n-2)+1UL;
t = d;
y = z_powmod2_precomp(a, t , n, ninv);
while ((t != n-1) && (y != 1UL) && (y != n-1))
{
y = z_mulmod2_precomp(y, y, n, ninv);
t <<= 1UL;
}
if ((y != n-1) && ((t&1UL) == 0UL)) return 0;
}
return 1;
}
/*
This is a deterministic prime test up to 10^16.
This test is intended to be run after checking for divisibility by
primes up to 257 say, but returns a correct result if n is odd and n > 2.
Requires n is no more than FLINT_BITS-1 bits
*/
int z_isprobab_prime_precomp(unsigned long n, double ninv)
{
#if FLINT_BITS == 64
if (n >= 10000000000000000UL) return z_isprobab_prime_BPSW(n);
#endif
unsigned long d = n-1;
do {
d>>=1;
} while ((d&1) == 0);
if (n < 2047)
{
if (SPRP(2UL, d, n, ninv)) return 1;
else return 0;
}
if (n < 9080191UL)
{
if (SPRP(31UL, d, n, ninv) && SPRP(73UL, d, n, ninv)) return 1;
else return 0;
}
#if FLINT_BITS == 64
if (n < 4759123141UL)
{
if (SPRP(2UL, d, n, ninv) && SPRP(7UL, d, n, ninv) && SPRP(61UL, d, n, ninv)) return 1;
else return 0;
}
if (n < 1122004669633UL)
{
if (SPRP(2UL, d, n, ninv) && SPRP(13UL, d, n, ninv) && SPRP(23UL, d, n, ninv) && SPRP(1662803UL, d, n, ninv))
if (n != 46856248255981UL) return 1;
return 0;
}
if (SPRP_64(2UL, d, n, ninv) && SPRP_64(3UL, d, n, ninv) && SPRP_64(7UL, d, n, ninv) && SPRP_64(61UL, d, n, ninv) && SPRP_64(24251UL, d, n, ninv))
if (n != 46856248255981UL) return 1;
return 0;
#else
if (SPRP(2UL, d, n, ninv) && SPRP(7UL, d, n, ninv) && SPRP(61UL, d, n, ninv)) return 1;
else return 0;
#endif
}
/*
This is currently only a deterministic primality test if FLINT can factor n - 1.
Note that this is currently the case if n - 1 has less than FLINT_BITS - 10 bits
after removal of factors below 1000.
It is assumed that n is greater than 2 and odd.
*/
int z_isprime_precomp(unsigned long n, double ninv)
{
#if FLINT_BITS == 64
if (n >= 10000000000000000UL)
{
return z_isprime_pocklington(n, -1L);
}
#endif
unsigned long d = n-1;
do {
d>>=1;
} while ((d&1) == 0);
if (n < 2047)
{
if (SPRP(2UL, d, n, ninv)) return 1;
else return 0;
}
if (n < 9080191UL)
{
if (SPRP(31UL, d, n, ninv) && SPRP(73UL, d, n, ninv)) return 1;
else return 0;
}
#if FLINT_BITS == 64
if (n < 4759123141UL)
{
if (SPRP(2UL, d, n, ninv) && SPRP(7UL, d, n, ninv) && SPRP(61UL, d, n, ninv)) return 1;
else return 0;
}
if (n < 1122004669633UL)
{
if (SPRP(2UL, d, n, ninv) && SPRP(13UL, d, n, ninv) && SPRP(23UL, d, n, ninv) && SPRP(1662803UL, d, n, ninv))
if (n != 46856248255981UL) return 1;
return 0;
}
if (SPRP_64(2UL, d, n, ninv) && SPRP_64(3UL, d, n, ninv) && SPRP_64(7UL, d, n, ninv) && SPRP_64(61UL, d, n, ninv) && SPRP_64(24251UL, d, n, ninv))
if (n != 46856248255981UL) return 1;
return 0;
#else
if (SPRP(2UL, d, n, ninv) && SPRP(7UL, d, n, ninv) && SPRP(61UL, d, n, ninv)) return 1;
else return 0;
#endif
}
/*
This is a very dense representation of the prime indicator
function for odd primes < 4096. In that range,
oddprime_lt_4096 is probably about the fastest primality
test we can get.
One might be tempted to do something clever, like pack everything
according to its value mod 30. In practice, that either requires
branching, or some very dirty tricks. In the end, this is 2-5
times faster than everything I tried.
*/
static inline int z_oddprime_lt_4096(unsigned long n) {
static uint64_t oddprime_indicator[] =
{
0x816d129a64b4cb6eUL,0x2196820d864a4c32UL,0xa48961205a0434c9UL,
0x4a2882d129861144UL,0x834992132424030UL, 0x148a48844225064bUL,
0xb40b4086c304205UL, 0x65048928125108a0UL,0x80124496804c3098UL,
0xc02104c941124221UL,0x804490000982d32UL, 0x220825b082689681UL,
0x9004265940a28948UL,0x6900924430434006UL,0x12410da408088210UL,
0x86122d22400c060UL, 0x110d301821b0484UL, 0x14916022c044a002UL,
0x92094d204a6400cUL, 0x4ca2100800522094UL,0xa48b081051018200UL,
0x34c108144309a25UL, 0x2084490880522502UL,0x241140a218003250UL,
0xa41a00101840128UL, 0x2926000836004512UL,0x10100480c0618283UL,
0xc20c26584822006dUL,0x4520582024894810UL,0x10c0250219002488UL,
0x802832ca01140868UL,0x60901300264b0400UL
};
unsigned long q = n/2;
unsigned long x = (q&63);
return (oddprime_indicator[q/64]&(((uint64_t)1)<<x))>>x;
}
/*
This is a deterministic prime test up to 10^16.
Requires n to be at most FLINT_BITS-1 bits
For numbers greater than 10^16 there are no known
counterexamples to the conjecture that a composite
will never be declared prime.
*/
int z_isprobab_prime(unsigned long n)
{
if (n <= 1UL) return 0;
if (n == 2UL) return 1;
if ((n & 1UL) == 0) return 0;
if (n < 4096UL) return z_oddprime_lt_4096(n);
double ninv;
ninv = z_precompute_inverse(n);
return z_isprobab_prime_precomp(n, ninv);
}