-
Notifications
You must be signed in to change notification settings - Fork 0
/
siqs_factorization.sf
1195 lines (877 loc) · 31.4 KB
/
siqs_factorization.sf
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
#!/usr/bin/ruby
# This script factorizes a natural number given as a command line
# parameter into its prime factors. It first attempts to use trial
# division to find very small factors, then uses Brent's version of the
# Pollard rho algorithm [1] to find slightly larger factors. If any large
# factors remain, it uses the Self-Initializing Quadratic Sieve (SIQS) [2]
# to factorize those.
# [1] Brent, Richard P. 'An improved Monte Carlo factorization algorithm.'
# BIT Numerical Mathematics 20.2 (1980): 176-184.
# [2] Contini, Scott Patrick. 'Factoring integers with the self-
# initializing quadratic sieve.' (1997).
STDOUT.autoflush(true)
class SIQSPolynomial (coeff, a=nil, b=nil) {
method eval (x) {
var res = 0
coeff.each {|k|
res *= x
res += k
}
return res
}
}
class FactorBasePrime (p, t, lp) {
has soln1 = nil
has soln2 = nil
has ainv = nil
has p_log = p.log
}
func fibonacci_factorization (n, upper_bound) {
var bound = (5 * n.ilog2**2)
bound = upper_bound if (bound > upper_bound)
loop {
return nil if (bound <= 1)
var B = bound.consecutive_lcm
var F = B.fibmod(n)
var g = gcd(F, n)
if (g == n) {
bound >>= 1
next
}
if (g > 1) {
return [g]
}
return nil
}
}
func fast_fibonacci_factor (n, upto) {
var (P, Q) = (3, 1)
for k in (2 .. upto) {
var (U, V) = lucasUVmod(P, Q, k, n)
for t in (U, V, V-1, V+1, V-P) {
var g = gcd(t, n)
if (g.is_ntf(n)) {
return [g]
}
}
}
return nil
}
# Some tuning parameters
class SIQS (
MASK_LIMIT = 200, # show Cn if n > MASK_LIMIT, where n ~ log_10(N)
LOOK_FOR_SMALL_FACTORS = 1,
TRIAL_DIVISION_LIMIT = 1_000_000,
PHI_FINDER_ITERATIONS = 100_000,
FERMAT_ITERATIONS = 100_000,
NEAR_POWER_ITERATIONS = 1_000,
PELL_ITERATIONS = 100_000,
FLT_ITERATIONS = 200_000,
HOLF_ITERATIONS = 100_000,
MBE_ITERATIONS = 100,
DOP_FACTOR_LIMIT = 1000,
COP_FACTOR_LIMIT = 500,
MILLER_RABIN_ITERATIONS = 100,
LUCAS_MILLER_ITERATIONS = 50,
SIQS_TRIAL_DIVISION_EPS = 25,
SIQS_MIN_PRIME_POLYNOMIAL = 400,
SIQS_MAX_PRIME_POLYNOMIAL = 4000,
) {
method factor_base_primes (n, nf) {
var factor_base = []
1e7.each_prime {|p|
var t = n.sqrtmod(p) || next
var lp = p.log2.round.int
factor_base << FactorBasePrime(p, t, lp)
break if (factor_base.len >= nf)
}
return factor_base
}
method create_poly (a, b, n, factor_base, first) {
var b_orig = b
if (2*b > a) {
b = (a - b)
}
var g = SIQSPolynomial([a*a, 2*a*b, b*b - n], a, b_orig)
var h = SIQSPolynomial([a, b])
for fb in (factor_base) {
next if (fb.p `divides` a)
fb.ainv = a.invmod(fb.p) if first
fb.soln1 = ((fb.ainv * ( fb.t - b)) % fb.p)
fb.soln2 = ((fb.ainv * (-fb.t - b)) % fb.p)
}
return (g, h)
}
method find_first_poly (n, m, factor_base) {
var p_min_i = nil
var p_max_i = nil
factor_base.each_kv { |i,fb|
if (!defined(p_min_i) && (fb.p >= SIQS_MIN_PRIME_POLYNOMIAL)) {
p_min_i = i
}
if (!defined(p_max_i) && (fb.p > SIQS_MAX_PRIME_POLYNOMIAL)) {
p_max_i = i-1
break
}
}
# The following may happen if the factor base is small
if (!defined(p_max_i)) {
p_max_i = factor_base.end
}
if (!defined(p_min_i)) {
p_min_i = 5
}
if ((p_max_i - p_min_i) < 20) {
p_min_i = (p_min_i `min` 5)
}
var target0 = ((n.log + 2.log)/2 - m.log)
var target1 = (target0 - ((log(factor_base[p_min_i].p + factor_base[p_max_i].p) - 2.log) / 2))
# find q such that the product of factor_base[q_i] is approximately
# sqrt(2 * n) / m; try a few different sets to find a good one
var (best_q, best_a, best_ratio)
30.times {
var A = 1
var log_A = 0
var Q = Hash()
while (log_A < target1) {
var p_i = 0
while ((p_i == 0) || Q.exists(p_i)) {
p_i = p_min_i.irand(p_max_i) # inclusive on both sides
}
var fb = factor_base[p_i]
A *= fb.p
log_A += fb.p_log
Q{p_i} = fb
}
var ratio = exp(log_A - target0)
# ratio too small seems to be not good
if (!defined(best_ratio) ||
(( ratio >= 0.9) && (ratio < best_ratio)) ||
((best_ratio < 0.9) && (ratio > best_ratio))
) {
best_q = Q
best_a = A
best_ratio = ratio
}
}
var a = best_a
var b = 0
var arr = []
best_q.each_v { |fb|
var p = fb.p
var r = a/p
var gamma = ((fb.t * r.invmod(p)) % p)
if (gamma > (p >> 1)) {
gamma = (p - gamma)
}
var t = r*gamma
b += t
arr << t
}
var (g, h) = self.create_poly(a, b, n, factor_base, true)
return (g, h, arr)
}
method find_next_poly (n, factor_base, i, g, arr) {
# Compute the (i+1)-th polynomials for the Self-Initializing
# Quadratic Sieve, given that g is the i-th polynomial.
var v = i.valuation(2)
var z = ((((i >> (v+1)) & 1) == 0) ? -1 : 1)
var a = g.a
var b = ((g.b + 2*z*arr[v]) % a)
return self.create_poly(a, b, n, factor_base, false)
}
method siqs_sieve (factor_base, m) {
# Perform the sieving step of the SIQS. Return the sieve array.
var sieve_array = (2*m + 1 -> of(0))
factor_base.each {|fb|
fb.p > 100 || next
fb.soln1 \\ next
var p = fb.p
var lp = fb.lp
var end = 2*m
var i_start_1 = -((m + fb.soln1) // p) #/
var a_start_1 = (fb.soln1 + i_start_1*p)
for i in (RangeNum(a_start_1 + m, end, p)) {
sieve_array[i] += lp
}
var i_start_2 = -((m + fb.soln2) // p) #/
var a_start_2 = (fb.soln2 + i_start_2*p)
for i in (RangeNum(a_start_2 + m, end, p)) {
sieve_array[i] += lp
}
}
return sieve_array
}
method siqs_trial_divide (n, factor_base_info) {
# Determine whether the given number a can be fully factorized into
# primes from the factors base. If so, return the indices of the
# factors from the factor base. If not, return `nil`.
if (n.is_smooth_over_prod(factor_base_info{:prod})) {
var factor_index = factor_base_info{:index}
return n.factor_exp.map {|p|
[factor_index{p[0]}, p[1]]
}
}
return nil
}
method siqs_trial_division (n, sieve_array, factor_base_info, smooth_relations, g, h, m, req_relations) {
# Perform the trial division step of the Self-Initializing Quadratic Sieve.
var limit = ((m.log + n.log/2)/2.log - SIQS_TRIAL_DIVISION_EPS)
sieve_array.each_kv { |i,s|
next if (s < limit)
var x = (i - m)
var gx = g.eval(x).abs
var divisors_idx = self.siqs_trial_divide(gx, factor_base_info) \\ next
var u = h.eval(x)
var v = gx
smooth_relations << [u, v, divisors_idx]
if (smooth_relations.len >= req_relations) {
return true
}
}
return false
}
method build_matrix (factor_base, smooth_relations) {
# Build the matrix for the linear algebra step of the Quadratic Sieve.
var fb = factor_base.len
var matrix = []
smooth_relations.each {|sr|
var row = fb.of(0)
for u,v in (sr[2]) {
row[u] = v&1
}
matrix << row
}
return matrix
}
method build_matrix_opt(M) {
# Convert the given matrix M of 0s and 1s into a list of numbers m
# that correspond to the columns of the matrix.
# The j-th number encodes the j-th column of matrix M in binary:
# The i-th bit of m[i] is equal to M[i][j].
var m = M[0].len
var cols_binary = m.of('')
M.each {|mi|
mi.each_kv { |j, mij|
cols_binary[j] += mij
}
}
return (cols_binary.map {|s| Number(s.flip, 2) }, M.len, m)
}
method find_pivot_column_opt (M, j) {
# For a matrix produced by `build_matrix_opt`, return the row of
# the first non-zero entry in column j, or None if no such row exists.
var v = M[j]
if (v == 0) {
return nil
}
return v.valuation(2)
}
method solve_matrix_opt (M, n, m) {
# Perform the linear algebra step of the SIQS. Perform fast
# Gaussian elimination to determine pairs of perfect squares mod n.
# Use the optimizations described in [1].
# [1] Koç, Çetin K., and Sarath N. Arachchige. 'A Fast Algorithm for
# Gaussian Elimination over GF (2) and its Implementation on the
# GAPP.' Journal of Parallel and Distributed Computing 13.1
# (1991): 118-122.
var row_is_marked = n.of(false)
var pivots = m.of(-1)
for j in (m.range) {
var i = self.find_pivot_column_opt(M, j) \\ next
pivots[j] = i
row_is_marked[i] = true
for k in (m.range) {
if ((k != j) && M[k].bit(i)) {
M[k] ^= M[j]
}
}
}
var perf_squares = []
for i in (n.range) {
next if row_is_marked[i]
var perfect_sq_indices = [i]
for j in (m.range) {
if (M[j].bit(i)) {
perfect_sq_indices << pivots[j]
}
}
perf_squares << perfect_sq_indices
}
return perf_squares
}
method calc_sqrts (n, square_indices, smooth_relations) {
# Given on of the solutions returned by `solve_matrix_opt` and
# the corresponding smooth relations, calculate the pair [a, b], such
# that a^2 = b^2 (mod n).
var r1 = 1
var r2 = 1
square_indices.each { |i|
r1 *= smooth_relations[i][0] %= n
r2 *= smooth_relations[i][1]
}
return (r1, r2.isqrt)
}
method factor_from_square (n, square_indices, smooth_relations) {
# Given one of the solutions returned by `solve_matrix_opt`,
# return the factor f determined by f = gcd(a - b, n), where
# a, b are calculated from the solution such that a*a = b*b (mod n).
# Return f, a factor of n (possibly a trivial one).
var (sqrt1, sqrt2) = self.calc_sqrts(n, square_indices, smooth_relations)
return gcd(sqrt1 - sqrt2, n)
}
method siqs_find_more_factors_gcd(numbers) {
var res = Hash()
numbers.each_kv { |i,n|
res{n} = n
for k in (i+1 .. numbers.end) {
var m = numbers[k]
var f = gcd(n, m)
if ((f != 1) && (f != n) && (f != m)) {
if (!res.exists(f)) {
say "SIQS: GCD found non-trivial factor: #{f}"
res{f} = f
}
var t1 = n//f #/
var t2 = m//f #/
res{t1} = t1
res{t2} = t2
}
}
}
return res.values
}
method siqs_find_factors (n, perfect_squares, smooth_relations) {
# Perform the last step of the Self-Initializing Quadratic Field.
# Given the solutions returned by `solve_matrix_opt`, attempt to
# identify a number of (not necessarily prime) factors of n, and
# return them.
var factors = []
var rem = n
var non_prime_factors = Hash()
var prime_factors = Hash()
perfect_squares.each {|square_indices|
var fact = self.factor_from_square(n, square_indices, smooth_relations)
next if (fact <= 1)
next if (fact > rem)
if (fact.is_prime) {
if (!prime_factors.exists(fact)) {
say "SIQS: Prime factor found: #{fact}"
prime_factors{fact} = fact
}
rem = self.check_factor(rem, fact, factors)
break if rem.is_one
if (rem.is_prime) {
factors << rem
rem = 1
break
}
if (defined(var root = self.check_perfect_power(rem))) {
say "SIQS: Perfect power detected with root: #{root}"
factors << root
rem = 1
break
}
}
else {
if (!non_prime_factors.exists(fact)) {
say "SIQS: Composite factor found: #{fact}"
non_prime_factors{fact} = fact
}
}
}
if ((rem != 1) && non_prime_factors) {
non_prime_factors{rem} = rem
var primes = []
var composites = []
self.siqs_find_more_factors_gcd(non_prime_factors.values).each {|fact|
if (fact.is_prime) {
primes << fact
}
elsif (fact > 1) {
composites << fact
}
}
for fact in (primes + composites) {
if ((fact != rem) && (fact `divides` rem)) {
say "SIQS: Using non-trivial factor from GCD: #{fact}"
rem = self.check_factor(rem, fact, factors)
}
break if rem.is_one
break if rem.is_prime
}
}
if (rem != 1) {
factors << rem
}
return factors
}
method siqs_choose_range (n) {
# Choose m for sieving in [-m, m].
exp(sqrt(n.log * n.log.log) / 2) -> round.int
}
method siqs_choose_nf (n) {
# Choose parameters nf (sieve of factor base)
exp(sqrt(n.log * n.log.log))**(2.sqrt / 4) -> round.int
}
method siqs_factorize (n, nf) {
# Use the Self-Initializing Quadratic Sieve algorithm to identify
# one or more non-trivial factors of the given number n. Return the
# factors as a list.
var m = self.siqs_choose_range(n)
var factors = []
var factor_base = self.factor_base_primes(n, nf)
var factor_prod = factor_base.prod_by { .p }
var factor_index = Hash(
factor_base.map { .p } ~Z ^factor_base -> flat...
)
var factor_base_info = Hash(
base => factor_base,
prod => factor_prod,
index => factor_index,
)
var smooth_relations = []
var required_relations_ratio = 1
var success = false
var prev_cnt = 0
var i_poly = 0
var (g, h, arr)
while (!success) {
say "*** Step 1/2: Finding smooth relations ***"
say "SIQS sieving range: [-#{m}, #{m}]"
var required_relations = ((factor_base.len + 1) * required_relations_ratio).round.int
say "Target: #{required_relations} relations."
var enough_relations = false
while (!enough_relations) {
if (i_poly == 0) {
(g, h, arr) = self.find_first_poly(n, m, factor_base);
}
else {
(g, h) = self.find_next_poly(n, factor_base, i_poly, g, arr);
}
if (++i_poly >= (1 << arr.end)) {
i_poly = 0
}
var sieve_array = self.siqs_sieve(factor_base, m)
enough_relations = self.siqs_trial_division(
n, sieve_array, factor_base_info, smooth_relations,
g, h, m, required_relations
)
if ((smooth_relations.len >= required_relations) ||
(smooth_relations.len > prev_cnt)
) {
printf("Progress: %d/%d relations.\r", smooth_relations.len, required_relations)
prev_cnt = smooth_relations.len
}
}
say "\n\n*** Step 2/2: Linear Algebra ***"
say "Building matrix for linear algebra step..."
var M = self.build_matrix(factor_base, smooth_relations)
var (M_opt, M_n, M_m) = self.build_matrix_opt(M)
say "Finding perfect squares using Gaussian elimination...";
var perfect_squares = self.solve_matrix_opt(M_opt, M_n, M_m)
say "Finding factors from perfect squares...\n";
factors = self.siqs_find_factors(n, perfect_squares, smooth_relations)
if (factors.len >= 2) {
success = true
}
else {
say "Failed to find a solution. Finding more relations..."
required_relations_ratio += 0.05
}
}
return factors
}
method trial_division_small_primes (n) {
# Perform trial division on the given number n using all primes up
# to upper_bound. Initialize the global variable small_primes with a
# list of all primes <= upper_bound. Return (factors, rem), where
# factors is the list of identified prime factors of n, and rem is the
# remaining factor. If rem = 1, the function terminates early, without
# fully initializing small_primes.
say "[*] Trial division..."
var factors = n.trial_factor(TRIAL_DIVISION_LIMIT)
if (factors.last.is_prime) {
return (factors, 1)
}
var rem = factors.pop
return (factors, rem)
}
method check_perfect_power (n) {
# Check whether n is a perfect and return its perfect root.
# Returns undef otherwise.
if (n.is_power) {
var power = n.perfect_power
var root = n.perfect_root
say "`-> #{n} is #{root}^#{power}"
return root
}
return nil
}
method check_factor (Number n, Number i, Array factors) {
while (i `divides` n) {
n //= i #/
factors << i
if (n.is_prime) {
factors << n
return 1
}
}
return n
}
method store_factor (Ref rem, Number f, Array factors) {
f || return false
f < *rem || return false
f `divides` *rem || die "error: #{f} does not divide #{*rem}"
if (f.is_prime) {
say "`-> prime factor: #{f}"
*rem = self.check_factor(*rem, f, factors)
}
else {
say "`-> composite factor: #{f}"
*rem = idiv(*rem, f)
# Try to find a small factor of f
var f_factor = self.find_small_factors(f, factors)
if (f_factor < f) {
*rem *= f_factor
}
else {
# Use SIQS to factorize f
var F = self.find_prime_factors(f)
F.each {|p|
if (p `divides` *rem) {
*rem = self.check_factor(*rem, p, factors)
break if (*rem == 1)
}
}
}
}
return true
}
method find_small_factors (rem, factors) {
# Perform up to max_iter iterations of Brent's variant of the
# Pollard rho factorization algorithm to attempt to find small
# prime factors. Restart the algorithm each time a factor was found.
# Add all identified prime factors to factors, and return 1 if all
# prime factors were found, or otherwise the remaining factor.
var state = Hash(
cyclotomic_check => true,
fast_power_check => true,
fast_fibonacci_check => true,
)
var len = rem.len
var factorization_methods = [
func {
say "=> Miller-Rabin method..."
miller_factor(rem, (len > 1000) ? 15 : MILLER_RABIN_ITERATIONS)
},
func {
if (len < 3000) {
say "=> Lucas-Miller method (n+1)..."
lucas_factor(rem, +1, (len > 1000) ? 10 : LUCAS_MILLER_ITERATIONS)
}
},
func {
if (len < 3000) {
say "=> Lucas-Miller method (n-1)..."
lucas_factor(rem, -1, (len > 1000) ? 10 : LUCAS_MILLER_ITERATIONS)
}
},
func {
say "=> Phi finder method...";
phi_finder_factor(rem, PHI_FINDER_ITERATIONS)
},
func {
say "=> Fermat's method..."
fermat_factor(rem, FERMAT_ITERATIONS)
},
func {
say "=> HOLF method..."
holf_factor(rem, 2*HOLF_ITERATIONS)
},
func {
say "=> Pell factor..."
pell_factor(rem, PELL_ITERATIONS)
},
func {
say "=> Pollard p-1 (20K)..."
pm1_factor(rem, 20_000)
},
func {
say "=> Fermat's little theorem (base 2)..."
flt_factor(rem, 2, (len > 1000) ? 1e4 : FLT_ITERATIONS)
},
func {
var len_2 = (len * (log(10) / log(2)))
var iter = (((len_2 * MBE_ITERATIONS) > 1_000) ? int(1_000/len_2) : MBE_ITERATIONS)
if (iter > 0) {
say "=> MBE method (#{iter} iter)...";
mbe_factor(rem, iter)
}
},
func {
say "=> Fermat's little theorem (base 3)..."
flt_factor(rem, 3, (len > 1000) ? 1e4 : FLT_ITERATIONS)
},
func {
state{:fast_fibonacci_check} || return nil
say "=> Fast Fibonacci check..."
var f = fast_fibonacci_factor(rem, 5000)
f || do { state{:fast_fibonacci_check} = false }
f
},
func {
state{:cyclotomic_check} || return nil
say "=> Fast cyclotomic check..."
var f = cyclotomic_factor(rem)
f.len >= 2 || do { state{:cyclotomic_check} = false }
f
},
func {
say "=> Pollard rho (10M)..."
prho_factor(rem, 1e10.isqrt)
},
func {
say "=> Pollard p-1 (500K)..."
pm1_factor(rem, 500_000)
},
func {
say "=> ECM (600)..."
ecm_factor(rem, 600, 20)
},
func {
say "=> Williams p±1 (500K)..."
pp1_factor(rem, 500_000)
},
func {
if (len < 1000) {
say "=> Chebyshev p±1 (500K)..."
chebyshev_factor(rem, 500_000, 1e6.irand+2)
}
},
func {
say "=> Williams p±1 (1M)..."
pp1_factor(rem, 1_000_000)
},
func {
if (len < 1000) {
say "=> Chebyshev p±1 (1M)..."
chebyshev_factor(rem, 1_000_000, 1e6.irand+2)
}
},
func {
say "=> ECM (2K)..."
ecm_factor(rem, 2000, 10)
},
func {
say "=> Difference of powers..."
dop_factor(rem, DOP_FACTOR_LIMIT)
},
func {
if (len < 500) {
say "=> Fibonacci p±1 (500K)..."
fibonacci_factorization(rem, 500_000)
}
},
func {
say "=> Pollard-Brent rho (12M)..."
pbrent_factor(rem, 1e12.isqrt)
},
func {
say "=> Congruence of powers..."
cop_factor(rem, COP_FACTOR_LIMIT)
},
func {
say "=> Pollard p-1 (5M)..."
pm1_factor(rem, 5_000_000)
},
func {
say "=> Williams p±1 (3M)..."
pp1_factor(rem, 3_000_000)
},
func {
say "=> Pollard rho (13M)..."
prho_factor(rem, 1e13.isqrt)
},
func {
say "=> ECM (160K)..."
ecm_factor(rem, 160_000, 80)
},
]
loop {
break if (rem <= 1)
if (rem.is_prime) {
factors << rem
rem = 1
break
}
len = rem.len
if ((len >= 25) && (len <= 30)) { # factorize with SIQS directly
return rem
}
printf("\n[*] Factoring %s (%s digits)...\n\n", (len > MASK_LIMIT ? "C#{len}" : rem), len)
say "=> Perfect power check..."
var f = self.check_perfect_power(rem)
if (defined(f)) {
var pow = rem.perfect_power
var r = self.factorize(f)
factors << (r*pow)...
return 1
}
var found_factor = false
var end = factorization_methods.end
for (var i = 0; i <= end; ++i) {
var f = factorization_methods[i].call || next
f.each {|p|
if (self.store_factor(\rem, p, factors)) {
found_factor = true
}
}
if (found_factor) {
factorization_methods.unshift(factorization_methods.pop_at(i))
break
}
else {
factorization_methods.push(factorization_methods.pop_at(i))
--i