-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMBE_factorization_method.sf
66 lines (46 loc) · 1.3 KB
/
MBE_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
#!/usr/bin/ruby
# Author: Trizen
# Date: 12 March 2022
# Edit: 15 March 2022
# https://github.com/trizen
# A new integer factorization method, using the binary exponentiation algorithm with modular exponentiation.
# We call it the "Modular Binary Exponentiation" (MBE) factorization method.
# Similar in flavor to the Pollard's p-1 method.
func f(a,b,n) {
var c = 1
b.bits.flip.each {|x|
c = powmod(a, c, n) if x
a = powmod(a, a, n)
}
return c
}
func MBE_factor(n, max_k = 10000) {
say "[*] Factoring #{n}"
for k in (1..max_k) {
#var a = idiv(n, k)
var a = irand(2,n) # randomised version
var c = 1
a.bits.flip.each {|x|
c = powmod(a, c, n) if x
a = powmod(a, a, n)
}
# Equivalently:
# c = f(a, a, n)
var g = gcd(c-1, n)
if (g.is_between(2, n-1)) {
say "[*] Found factor #{g} with k=#{k} -- took O(n^(1/#{n.log(1+k).round(-4)}))"
return g
}
}
return 1
}
for n in (1..5) {
var n = 2.of { 1e7.random_prime }.prod
MBE_factor(n)
say ''
}
__END__
[*] Factoring 3034271543203
[*] Found factor 604727 with k=177 -- took O(n^(1/5.5526))
[*] Factoring 43120971427631
[*] Found factor 5501281 with k=2 -- took O(n^(1/45.2935))