Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
1710 lines (1565 loc) · 48.5 KB

File metadata and controls

1710 lines (1565 loc) · 48.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include <valarray>
#include <iterator>
#include <symengine/prime_sieve.h>
#include <symengine/ntheory.h>
#include <symengine/rational.h>
#include <symengine/add.h>
#include <symengine/mul.h>
#include <symengine/pow.h>
#ifdef HAVE_SYMENGINE_ECM
#include <ecm.h>
#endif // HAVE_SYMENGINE_ECM
#ifdef HAVE_SYMENGINE_PRIMESIEVE
#include <primesieve.hpp>
#endif // HAVE_SYMENGINE_PRIMESIEVE
#ifdef HAVE_SYMENGINE_ARB
#include <mpfr.h>
#include <flint/fmpq.h>
#include <arb.h>
#include <bernoulli.h>
#endif // HAVE_SYMENGINE_ARB
#ifndef HAVE_SYMENGINE_GMP
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random.hpp>
#endif // !HAVE_SYMENGINE_GMP
namespace SymEngine
{
// Basic number theoretic functions
RCP<const Integer> gcd(const Integer &a, const Integer &b)
{
integer_class g;
mp_gcd(g, a.as_integer_class(), b.as_integer_class());
return integer(std::move(g));
}
void gcd_ext(const Ptr<RCP<const Integer>> &g, const Ptr<RCP<const Integer>> &s,
const Ptr<RCP<const Integer>> &t, const Integer &a,
const Integer &b)
{
integer_class g_, s_, t_;
mp_gcdext(g_, s_, t_, a.as_integer_class(), b.as_integer_class());
*g = integer(std::move(g_));
*s = integer(std::move(s_));
*t = integer(std::move(t_));
}
RCP<const Integer> lcm(const Integer &a, const Integer &b)
{
integer_class c;
mp_lcm(c, a.as_integer_class(), b.as_integer_class());
return integer(std::move(c));
}
int mod_inverse(const Ptr<RCP<const Integer>> &b, const Integer &a,
const Integer &m)
{
int ret_val;
integer_class inv_t;
ret_val = mp_invert(inv_t, a.as_integer_class(), m.as_integer_class());
*b = integer(std::move(inv_t));
return ret_val;
}
RCP<const Integer> mod(const Integer &n, const Integer &d)
{
return integer(n.as_integer_class() % d.as_integer_class());
}
RCP<const Integer> quotient(const Integer &n, const Integer &d)
{
return integer(n.as_integer_class() / d.as_integer_class());
}
void quotient_mod(const Ptr<RCP<const Integer>> &q,
const Ptr<RCP<const Integer>> &r, const Integer &n,
const Integer &d)
{
integer_class _q, _r;
mp_tdiv_qr(_q, _r, n.as_integer_class(), d.as_integer_class());
*q = integer(std::move(_q));
*r = integer(std::move(_r));
}
RCP<const Integer> mod_f(const Integer &n, const Integer &d)
{
integer_class q;
mp_fdiv_r(q, n.as_integer_class(), d.as_integer_class());
return integer(std::move(q));
}
RCP<const Integer> quotient_f(const Integer &n, const Integer &d)
{
integer_class q;
mp_fdiv_q(q, n.as_integer_class(), d.as_integer_class());
return integer(std::move(q));
}
void quotient_mod_f(const Ptr<RCP<const Integer>> &q,
const Ptr<RCP<const Integer>> &r, const Integer &n,
const Integer &d)
{
integer_class _q, _r;
mp_fdiv_qr(_q, _r, n.as_integer_class(), d.as_integer_class());
*q = integer(std::move(_q));
*r = integer(std::move(_r));
}
RCP<const Integer> fibonacci(unsigned long n)
{
integer_class f;
mp_fib_ui(f, n);
return integer(std::move(f));
}
void fibonacci2(const Ptr<RCP<const Integer>> &g,
const Ptr<RCP<const Integer>> &s, unsigned long n)
{
integer_class g_t;
integer_class s_t;
mp_fib2_ui(g_t, s_t, n);
*g = integer(std::move(g_t));
*s = integer(std::move(s_t));
}
RCP<const Integer> lucas(unsigned long n)
{
integer_class f;
mp_lucnum_ui(f, n);
return integer(std::move(f));
}
void lucas2(const Ptr<RCP<const Integer>> &g, const Ptr<RCP<const Integer>> &s,
unsigned long n)
{
integer_class g_t;
integer_class s_t;
mp_lucnum2_ui(g_t, s_t, n);
*g = integer(std::move(g_t));
*s = integer(std::move(s_t));
}
// Binomial Coefficient
RCP<const Integer> binomial(const Integer &n, unsigned long k)
{
integer_class f;
mp_bin_ui(f, n.as_integer_class(), k);
return integer(std::move(f));
}
// Factorial
RCP<const Integer> factorial(unsigned long n)
{
integer_class f;
mp_fac_ui(f, n);
return integer(std::move(f));
}
// Returns true if `b` divides `a` without reminder
bool divides(const Integer &a, const Integer &b)
{
return mp_divisible_p(a.as_integer_class(), b.as_integer_class()) != 0;
}
// Prime functions
int probab_prime_p(const Integer &a, unsigned reps)
{
return mp_probab_prime_p(a.as_integer_class(), reps);
}
RCP<const Integer> nextprime(const Integer &a)
{
integer_class c;
mp_nextprime(c, a.as_integer_class());
return integer(std::move(c));
}
namespace
{
// Factoring by Trial division using primes only
int _factor_trial_division_sieve(integer_class &factor, const integer_class &N)
{
integer_class sqrtN = mp_sqrt(N);
unsigned long limit = mp_get_ui(sqrtN);
if (limit > std::numeric_limits<unsigned>::max())
throw SymEngineException("N too large to factor");
Sieve::iterator pi(numeric_cast<unsigned>(limit));
unsigned p;
while ((p = pi.next_prime()) <= limit) {
if (N % p == 0) {
factor = p;
return 1;
}
}
return 0;
}
// Factor using lehman method.
int _factor_lehman_method(integer_class &rop, const integer_class &n)
{
if (n < 21)
throw SymEngineException("Require n >= 21 to use lehman method");
int ret_val = 0;
integer_class u_bound;
mp_root(u_bound, n, 3);
u_bound = u_bound + 1;
Sieve::iterator pi(numeric_cast<unsigned>(mp_get_ui(u_bound)));
unsigned p;
while ((p = pi.next_prime()) <= mp_get_ui(u_bound)) {
if (n % p == 0) {
rop = n / p;
ret_val = 1;
break;
}
}
if (not ret_val) {
integer_class k, a, b, l;
k = 1;
while (k <= u_bound) {
a = mp_sqrt(4 * k * n);
mp_root(b, n, 6);
mp_root(l, k, 2);
b = b / (4 * l);
b = b + a;
while (a <= b) {
l = a * a - 4 * k * n;
if (mp_perfect_square_p(l)) {
b = a + mp_sqrt(l);
mp_gcd(rop, n, b);
ret_val = 1;
break;
}
a = a + 1;
}
if (ret_val)
break;
k = k + 1;
}
}
return ret_val;
}
} // anonymous namespace
int factor_lehman_method(const Ptr<RCP<const Integer>> &f, const Integer &n)
{
int ret_val;
integer_class rop;
ret_val = _factor_lehman_method(rop, n.as_integer_class());
*f = integer(std::move(rop));
return ret_val;
}
namespace
{
// Factor using Pollard's p-1 method
int _factor_pollard_pm1_method(integer_class &rop, const integer_class &n,
const integer_class &c, unsigned B)
{
if (n < 4 or B < 3)
throw SymEngineException(
"Require n > 3 and B > 2 to use Pollard's p-1 method");
integer_class m, _c;
_c = c;
Sieve::iterator pi(B);
unsigned p;
while ((p = pi.next_prime()) <= B) {
m = 1;
// calculate log(p, B), this can be improved
while (m <= B / p) {
m = m * p;
}
mp_powm(_c, _c, m, n);
}
_c = _c - 1;
mp_gcd(rop, _c, n);
if (rop == 1 or rop == n)
return 0;
else
return 1;
}
} // anonymous namespace
int factor_pollard_pm1_method(const Ptr<RCP<const Integer>> &f,
const Integer &n, unsigned B, unsigned retries)
{
int ret_val = 0;
integer_class rop, nm4, c;
mp_randstate state;
nm4 = n.as_integer_class() - 4;
for (unsigned i = 0; i < retries and ret_val == 0; ++i) {
state.urandomint(c, nm4);
c += 2;
ret_val = _factor_pollard_pm1_method(rop, n.as_integer_class(), c, B);
}
if (ret_val != 0)
*f = integer(std::move(rop));
return ret_val;
}
namespace
{
// Factor using Pollard's rho method
int _factor_pollard_rho_method(integer_class &rop, const integer_class &n,
const integer_class &a, const integer_class &s,
unsigned steps = 10000)
{
if (n < 5)
throw SymEngineException("Require n > 4 to use pollard's-rho method");
integer_class u, v, g, m;
u = s;
v = s;
for (unsigned i = 0; i < steps; ++i) {
u = (u * u + a) % n;
v = (v * v + a) % n;
v = (v * v + a) % n;
m = u - v;
mp_gcd(g, m, n);
if (g == n)
return 0;
if (g == 1)
continue;
rop = g;
return 1;
}
return 0;
}
} // namespace
int factor_pollard_rho_method(const Ptr<RCP<const Integer>> &f,
const Integer &n, unsigned retries)
{
int ret_val = 0;
integer_class rop, nm1, nm4, a, s;
mp_randstate state;
nm1 = n.as_integer_class() - 1;
nm4 = n.as_integer_class() - 4;
for (unsigned i = 0; i < retries and ret_val == 0; ++i) {
state.urandomint(a, nm1);
state.urandomint(s, nm4);
s += 1;
ret_val = _factor_pollard_rho_method(rop, n.as_integer_class(), a, s);
}
if (ret_val != 0)
*f = integer(std::move(rop));
return ret_val;
}
// Factorization
int factor(const Ptr<RCP<const Integer>> &f, const Integer &n, double B1)
{
int ret_val = 0;
integer_class _n, _f;
_n = n.as_integer_class();
#ifdef HAVE_SYMENGINE_ECM
if (mp_perfect_power_p(_n)) {
unsigned long int i = 1;
integer_class m, rem;
rem = 1; // Any non zero number
m = 2; // set `m` to 2**i, i = 1 at the begining
// calculate log2n, this can be improved
for (; m < _n; ++i)
m = m * 2;
// eventually `rem` = 0 zero as `n` is a perfect power. `f_t` will
// be set to a factor of `n` when that happens
while (i > 1 and rem != 0) {
mp_rootrem(_f, rem, _n, i);
--i;
}
ret_val = 1;
} else {
if (mp_probab_prime_p(_n, 25) > 0) { // most probably, n is a prime
ret_val = 0;
_f = _n;
} else {
for (int i = 0; i < 10 and not ret_val; ++i)
ret_val = ecm_factor(get_mpz_t(_f), get_mpz_t(_n), B1, nullptr);
mp_demote(_f);
if (not ret_val)
throw SymEngineException(
"ECM failed to factor the given number");
}
}
#else
// B1 is discarded if gmp-ecm is not installed
ret_val = _factor_trial_division_sieve(_f, _n);
#endif // HAVE_SYMENGINE_ECM
*f = integer(std::move(_f));
return ret_val;
}
int factor_trial_division(const Ptr<RCP<const Integer>> &f, const Integer &n)
{
int ret_val;
integer_class factor;
ret_val = _factor_trial_division_sieve(factor, n.as_integer_class());
if (ret_val == 1)
*f = integer(std::move(factor));
return ret_val;
}
void prime_factors(std::vector<RCP<const Integer>> &prime_list,
const Integer &n)
{
integer_class sqrtN;
integer_class _n = n.as_integer_class();
if (_n == 0)
return;
if (_n < 0)
_n *= -1;
sqrtN = mp_sqrt(_n);
auto limit = mp_get_ui(sqrtN);
if (not mp_fits_ulong_p(sqrtN)
or limit > std::numeric_limits<unsigned>::max())
throw SymEngineException("N too large to factor");
Sieve::iterator pi(numeric_cast<unsigned>(limit));
unsigned p;
while ((p = pi.next_prime()) <= limit) {
while (_n % p == 0) {
prime_list.push_back(integer(p));
_n = _n / p;
}
if (_n == 1)
break;
}
if (not(_n == 1))
prime_list.push_back(integer(std::move(_n)));
}
void prime_factor_multiplicities(map_integer_uint &primes_mul, const Integer &n)
{
integer_class sqrtN;
integer_class _n = n.as_integer_class();
unsigned count;
if (_n == 0)
return;
if (_n < 0)
_n *= -1;
sqrtN = mp_sqrt(_n);
auto limit = mp_get_ui(sqrtN);
if (not mp_fits_ulong_p(sqrtN)
or limit > std::numeric_limits<unsigned>::max())
throw SymEngineException("N too large to factor");
Sieve::iterator pi(numeric_cast<unsigned>(limit));
unsigned p;
while ((p = pi.next_prime()) <= limit) {
count = 0;
while (_n % p == 0) { // when a prime factor is found, we divide
++count; // _n by that prime as much as we can
_n = _n / p;
}
if (count > 0) {
insert(primes_mul, integer(p), count);
if (_n == 1)
break;
}
}
if (not(_n == 1))
insert(primes_mul, integer(std::move(_n)), 1);
}
RCP<const Number> bernoulli(unsigned long n)
{
#ifdef HAVE_SYMENGINE_ARB
fmpq_t res;
fmpq_init(res);
bernoulli_fmpq_ui(res, n);
mpq_t a;
mpq_init(a);
fmpq_get_mpq(a, res);
rational_class b(a);
fmpq_clear(res);
mpq_clear(a);
return Rational::from_mpq(std::move(b));
#else
// TODO: implement a faster algorithm
std::vector<rational_class> v(n + 1);
for (unsigned m = 0; m <= n; ++m) {
v[m] = rational_class(1u, m + 1);
for (unsigned j = m; j >= 1; --j) {
v[j - 1] = j * (v[j - 1] - v[j]);
}
}
return Rational::from_mpq(v[0]);
#endif
}
RCP<const Number> harmonic(unsigned long n, long m)
{
rational_class res(0);
if (m == 1) {
for (unsigned i = 1; i <= n; ++i) {
res += rational_class(1u, i);
}
return Rational::from_mpq(res);
} else {
for (unsigned i = 1; i <= n; ++i) {
if (m > 0) {
rational_class t(1u, i);
#if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
mp_pow_ui(get_den(t), get_den(t), m);
#else
mp_pow_ui(t, t, m);
#endif
res += t;
} else {
integer_class t(i);
mp_pow_ui(t, t, static_cast<unsigned long>(-m));
res += t;
}
}
return Rational::from_mpq(res);
}
}
// References : Cohen H., A course in computational algebraic number theory
// (1996), page 21.
bool crt(const Ptr<RCP<const Integer>> &R,
const std::vector<RCP<const Integer>> &rem,
const std::vector<RCP<const Integer>> &mod)
{
if (mod.size() > rem.size())
throw SymEngineException("Too few remainders");
if (mod.size() == 0)
throw SymEngineException("Moduli vector cannot be empty");
integer_class m, r, g, s, t;
m = mod[0]->as_integer_class();
r = rem[0]->as_integer_class();
for (unsigned i = 1; i < mod.size(); ++i) {
mp_gcdext(g, s, t, m, mod[i]->as_integer_class());
// g = s * m + t * mod[i]
t = rem[i]->as_integer_class() - r;
if (not mp_divisible_p(t, g))
return false;
r += m * s * (t / g); // r += m * (m**-1 mod[i]/g)* (rem[i] - r) / g
m *= mod[i]->as_integer_class() / g;
mp_fdiv_r(r, r, m);
}
*R = integer(std::move(r));
return true;
}
namespace
{
// Crt over a cartesian product of vectors (Assuming that moduli are pairwise
// relatively prime).
void _crt_cartesian(std::vector<RCP<const Integer>> &R,
const std::vector<std::vector<RCP<const Integer>>> &rem,
const std::vector<RCP<const Integer>> &mod)
{
if (mod.size() > rem.size())
throw SymEngineException("Too few remainders");
if (mod.size() == 0)
throw SymEngineException("Moduli vector cannot be empty");
integer_class m, _m, r, s, t;
m = mod[0]->as_integer_class();
R = rem[0];
for (unsigned i = 1; i < mod.size(); ++i) {
std::vector<RCP<const Integer>> rem2;
mp_invert(s, m, mod[i]->as_integer_class());
_m = m;
m *= mod[i]->as_integer_class();
for (auto &elem : R) {
for (auto &_k : rem[i]) {
r = elem->as_integer_class();
r += _m * s * (_k->as_integer_class() - r);
mp_fdiv_r(r, r, m);
rem2.push_back(integer(r));
}
}
R = rem2;
}
}
// Tests whether n is a prime power and finds a prime p and e such that n =
// p**e.
bool _prime_power(integer_class &p, integer_class &e, const integer_class &n)
{
if (n < 2)
return false;
integer_class _n = n, temp;
e = 1;
unsigned i = 2;
while (mp_perfect_power_p(_n) and _n >= 2) {
if (mp_root(temp, _n, i)) {
e *= i;
_n = temp;
} else {
++i;
}
}
if (mp_probab_prime_p(_n, 25)) {
p = _n;
return true;
}
return false;
}
// Computes a primitive root modulo p**e or 2*p**e where p is an odd prime.
// References : Cohen H., A course in computational algebraic number theory
// (2009), pages 25-27.
void _primitive_root(integer_class &g, const integer_class &p,
const integer_class &e, bool even = false)
{
std::vector<RCP<const Integer>> primes;
prime_factors(primes, *integer(p - 1));
integer_class t;
g = 2;
while (g < p) {
bool root = true;
for (const auto &it : primes) {
t = it->as_integer_class();
t = (p - 1) / t;
mp_powm(t, g, t, p);
if (t == 1) { // If g**(p-1)/q is 1 then g is not a primitive root.
root = false;
break;
}
}
if (root)
break;
++g;
}
if (e > 1) {
t = p * p;
integer_class pm1 = p - 1;
mp_powm(t, g, pm1, t);
if (t == 1) { // If g**(p-1) mod (p**2) == 1 then g + p is a primitive
// root.
g += p;
}
}
if (even and g % 2 == 0) {
mp_pow_ui(t, p, mp_get_ui(e));
g += t; // If g is even then root of 2*p**e is g + p**e.
}
}
} // anonymous namespace
bool primitive_root(const Ptr<RCP<const Integer>> &g, const Integer &n)
{
integer_class _n = n.as_integer_class();
if (_n < 0)
_n = -_n;
if (_n <= 1)
return false;
if (_n < 5) {
*g = integer(_n - 1);
return true;
}
bool even = false;
if (_n % 2 == 0) {
if (_n % 4 == 0) {
return false; // If n mod 4 == 0 and n > 4, then no primitive roots.
}
_n /= 2;
even = true;
}
integer_class p, e;
if (not _prime_power(p, e, _n))
return false;
_primitive_root(_n, p, e, even);
*g = integer(std::move(_n));
return true;
}
namespace
{
// Computes primitive roots modulo p**e or 2*p**e where p is an odd prime.
// References :
// [1] Cohen H., A course in computational algebraic number theory (1996), pages
// 25-27.
// [2] Hackman P., Elementary number theory (2009), page 28.
void _primitive_root_list(std::vector<RCP<const Integer>> &roots,
const integer_class &p, const integer_class &e,
bool even = false)
{
integer_class g, h, d, t, pe2, n, pm1;
_primitive_root(g, p, integer_class(1),
false); // Find one primitive root for p.
h = 1;
pm1 = p - 1;
// Generate other primitive roots for p. h = g**i and gcd(i, p-1) = 1.
// Ref[2]
mp_pow_ui(n, p, mp_get_ui(e));
for (unsigned long i = 1; i < p; ++i) {
h *= g;
h %= p;
mp_gcd(d, pm1, integer_class(i));
if (d == 1) {
if (e == 1) {
if (even and h % 2 == 0)
roots.push_back(integer(h + n));
else
roots.push_back(integer(h));
} else {
integer_class pp = p * p;
// Find d such that (h + d*p)**(p-1) mod (p**2) == 1. Ref[1]
// h**(p-1) - 1 = d*p*h**(p-2)
// d = (h - h**(2-p)) / p
t = 2 - p;
mp_powm(d, h, t, pp);
d = ((h - d) / p + p) % p;
t = h;
// t = h + i * p + j * p * p and i != d
mp_pow_ui(pe2, p, mp_get_ui(e) - 2);
for (unsigned long j = 0; j < pe2; ++j) {
for (unsigned long i = 0; i < p; ++i) {
if (i != d) {
if (even and t % 2 == 0)
roots.push_back(integer(t + n));
else
roots.push_back(integer(t));
}
t += p;
}
}
}
}
}
} //_primitive_root_list
} // anonymous namespace
void primitive_root_list(std::vector<RCP<const Integer>> &roots,
const Integer &n)
{
integer_class _n = n.as_integer_class();
if (_n < 0)
_n = -_n;
if (_n <= 1)
return;
if (_n < 5) {
roots.push_back(integer(_n - 1));
return;
}
bool even = false;
if (_n % 2 == 0) {
if (_n % 4 == 0) {
return; // If n%4 == 0 and n > 4, then no primitive roots.
}
_n /= 2;
even = true;
}
integer_class p, e;
if (not _prime_power(p, e, _n))
return;
_primitive_root_list(roots, p, e, even);
std::sort(roots.begin(), roots.end(), SymEngine::RCPIntegerKeyLess());
return;
}
RCP<const Integer> totient(const RCP<const Integer> &n)
{
if (n->is_zero())
return integer(1);
integer_class phi = n->as_integer_class(), p;
if (phi < 0)
phi = -phi;
map_integer_uint prime_mul;
prime_factor_multiplicities(prime_mul, *n);
for (const auto &it : prime_mul) {
p = it.first->as_integer_class();
mp_divexact(phi, phi, p);
// phi is exactly divisible by p.
phi *= p - 1;
}
return integer(std::move(phi));
}
RCP<const Integer> carmichael(const RCP<const Integer> &n)
{
if (n->is_zero())
return integer(1);
map_integer_uint prime_mul;
integer_class lambda, t, p;
unsigned multiplicity;
prime_factor_multiplicities(prime_mul, *n);
lambda = 1;
for (const auto &it : prime_mul) {
p = it.first->as_integer_class();
multiplicity = it.second;
if (p == 2
and multiplicity
> 2) { // For powers of 2 greater than 4 divide by 2.
multiplicity--;
}
t = p - 1;
mp_lcm(lambda, lambda, t);
mp_pow_ui(t, p, multiplicity - 1);
// lambda and p are relatively prime.
lambda = lambda * t;
}
return integer(std::move(lambda));
}
// References : Cohen H., A course in computational algebraic number theory
// (1996), page 25.
bool multiplicative_order(const Ptr<RCP<const Integer>> &o,
const RCP<const Integer> &a,
const RCP<const Integer> &n)
{
integer_class order, p, t;
integer_class _a = a->as_integer_class(),
_n = mp_abs(n->as_integer_class());
mp_gcd(t, _a, _n);
if (t != 1)
return false;
RCP<const Integer> lambda = carmichael(n);
map_integer_uint prime_mul;
prime_factor_multiplicities(prime_mul, *lambda);
_a %= _n;
order = lambda->as_integer_class();
for (const auto &it : prime_mul) {
p = it.first->as_integer_class();
mp_pow_ui(t, p, it.second);
mp_divexact(order, order, t);
mp_powm(t, _a, order, _n);
while (t != 1) {
mp_powm(t, t, p, _n);
order *= p;
}
}
*o = integer(std::move(order));
return true;
}
int legendre(const Integer &a, const Integer &n)
{
return mp_legendre(a.as_integer_class(), n.as_integer_class());
}
int jacobi(const Integer &a, const Integer &n)
{
return mp_jacobi(a.as_integer_class(), n.as_integer_class());
}
int kronecker(const Integer &a, const Integer &n)
{
return mp_kronecker(a.as_integer_class(), n.as_integer_class());
}
namespace
{
bool _sqrt_mod_tonelli_shanks(integer_class &rop, const integer_class &a,
const integer_class &p)
{
mp_randstate state;
integer_class n, y, b, q, pm1, t(1);
pm1 = p - 1;
unsigned e, m;
e = numeric_cast<unsigned>(mp_scan1(pm1));
q = pm1 >> e; // p - 1 = 2**e*q
while (t != -1) {
state.urandomint(n, p);
t = mp_legendre(n, p);
}
mp_powm(y, n, q, p); // y = n**q mod p
mp_powm(b, a, q, p); // b = a**q mod p
t = (q + 1) / 2;
mp_powm(rop, a, t, p); // rop = a**((q + 1) / 2) mod p
while (b != 1) {
m = 0;
t = b;
while (t != 1) {
mp_powm(t, t, integer_class(2), p);
++m; // t = t**2 = b**2**(m)
}
if (m == e)
return false;
mp_pow_ui(q, integer_class(2), e - m - 1); // q = 2**(e - m - 1)
mp_powm(t, y, q, p); // t = y**(2**(e - m - 1))
mp_powm(y, t, integer_class(2), p); // y = t**2
e = m;
rop = (rop * t) % p;
b = (b * y) % p;
}
return true;
}
bool _sqrt_mod_prime(integer_class &rop, const integer_class &a,
const integer_class &p)
{
if (p == 2) {
rop = a % p;
return true;
}
int l = mp_legendre(a, p);
integer_class t;
if (l == -1) {
return false;
} else if (l == 0) {
rop = 0;
} else if (p % 4 == 3) {
t = (p + 1) / 4;
mp_powm(rop, a, t, p);
} else if (p % 8 == 5) {
t = (p - 1) / 4;
mp_powm(t, a, t, p);
if (t == 1) {
t = (p + 3) / 8;
mp_powm(rop, a, t, p);
} else {
t = (p - 5) / 8;
integer_class t1 = 4 * a;
mp_powm(t, t1, t, p);
rop = (2 * a * t) % p;
}
} else {
if (p < 10000) { // If p < 10000, brute force is faster.
integer_class sq = integer_class(1), _a;
mp_fdiv_r(_a, a, p);
for (unsigned i = 1; i < p; ++i) {
if (sq == _a) {
rop = i;
return true;
}
sq += 2 * i + 1;
mp_fdiv_r(sq, sq, p);
}
return false;
} else {
return _sqrt_mod_tonelli_shanks(rop, a, p);
}
}
return true;
}
// References : Menezes, Alfred J., Paul C. Van Oorschot, and Scott A. Vanstone.
// Handbook of applied cryptography. CRC press, 2010. pages 104 - 108
// Calculates log = x mod q**k where g**x == a mod p and order(g, p) = n.
void _discrete_log(integer_class &log, const integer_class &a,
const integer_class &g, const integer_class &n,
const integer_class &q, const unsigned &k,
const integer_class &p)
{
log = 0;
integer_class gamma = a, alpha, _n, t, beta, qj(1), m, l;
_n = n / q;
mp_powm(alpha, g, _n, p);
mp_sqrtrem(m, t, q);
if (t != 0)
++m; // m = ceiling(sqrt(q)).
map_integer_uint
table; // Table for lookup in baby-step giant-step algorithm
integer_class alpha_j(1), d, s;
s = -m;
mp_powm(s, alpha, s, p);
for (unsigned j = 0; j < m; ++j) {
insert(table, integer(alpha_j), j);
alpha_j = (alpha_j * alpha) % p;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.