-
Notifications
You must be signed in to change notification settings - Fork 0
/
fermat_hybrid_factorization_method.sf
160 lines (119 loc) · 3.19 KB
/
fermat_hybrid_factorization_method.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
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 27 February 2018
# https://github.com/trizen
# A hybrid factorization algorithm, using:
# * Pollard's p-1 algorithm
# * Pollard's rho algorithm
# * A simple version of the continued-fraction factorization method
# * Fermat's factorization method
# See also:
# https://en.wikipedia.org/wiki/Quadratic_sieve
# https://en.wikipedia.org/wiki/Dixon%27s_factorization_method
# https://en.wikipedia.org/wiki/Fermat%27s_factorization_method
# https://en.wikipedia.org/wiki/Pollard%27s_p_%E2%88%92_1_algorithm
func fermat_hybrid_factorization (n) {
if ((n <= 1) or n.is_prime) {
return [n]
}
if (n.is_even) {
var v = n.valuation(2)
var t = n>>v
var factors = v.of(2)
if (t > 1) {
factors += __FUNC__(t)
}
return factors
}
var p = n.isqrt
var x = p
var q = (p*p - n)
var t = 1
var u = 1
var v = 1
var h = 1
var k = 4
var z = n.random_prime
var g = 1
var c = q+p
var a0 = 1
var a1 = (a0*a0 + c)
var a2 = (a1*a1 + c)
var c1 = p
var c2 = 1
var r = p+p
var (e1, e2) = (1, 0)
var (f1, f2) = (0, 1)
while (!q.is_square) {
# Trizen's random factorization method (for small factors)
u = powmod(q * u, k, n)
v = powmod(p * v, k, n)
k += 16
g = gcd(u - v, n)
if ((g > 1) && (g < n)) {
return (
__FUNC__(n/g) +
__FUNC__(g) -> sort
)
}
q += ((p++ << 1) + 1)
# Pollard's rho algorithm
g = gcd(n, a2-a1)
if ((g > 1) && (g < n)) {
return (
__FUNC__(n/g) +
__FUNC__(g) -> sort
)
}
a1 = ((a1*a1 + c) % n)
a2 = ((a2*a2 + c) % n)
a2 = ((a2*a2 + c) % n)
# Simple version of the continued-fraction factorization method
c1 = (r*c2 - c1)
c2 = ((n - c1*c1) // c2) #/
var a = ((x*f2 + e2) % n)
var b = (a*a % n)
if (b.is_square) {
g = gcd(a - b.isqrt, n)
if ((g > 1) && (g < n)) {
return (
__FUNC__(g) +
__FUNC__(n/g) -> sort
)
}
}
r = round((x + c1) / c2)
(f1, f2) = (f2, (r*f2 + f1) % n)
(e1, e2) = (e2, (r*e2 + e1) % n)
# Pollard's p-a algorithm (random variation)
t = z
h = h.next_prime
z = powmod(z, h, n)
g = gcd(z * powmod(t, n.irand, n) - 1, n)
if (g > 1) {
if (g == n) {
h = 1
z = n.random_prime
next
}
return (
__FUNC__(n/g) +
__FUNC__(g) -> sort
)
}
}
# Fermat's method
var s = q.isqrt
__FUNC__(p + s) +
__FUNC__(p - s) -> sort
}
if (ARGV) {
say fermat_hybrid_factorization(ARGV[0].to_i)
return 1
}
for k in (1..63) {
var n = irand(2, 1<<k)
var prime_factors = fermat_hybrid_factorization(n)
say "#{n} = #{prime_factors.join(' * ')}"
assert_eq(prime_factors.prod, n)
}