-
Notifications
You must be signed in to change notification settings - Fork 34
/
ecm_factorization_method.pl
executable file
·162 lines (116 loc) · 4.61 KB
/
ecm_factorization_method.pl
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
#!/usr/bin/perl
# The elliptic-curve factorization method (ECM), due to Hendrik Lenstra.
# Algorithm presented in the YouTube video:
# https://www.youtube.com/watch?v=2JlpeQWtGH8
# See also:
# https://en.wikipedia.org/wiki/Lenstra_elliptic-curve_factorization
use 5.020;
use strict;
use warnings;
use Math::GMPz qw();
use experimental qw(signatures);
use ntheory qw(is_prime_power logint);
use Math::Prime::Util::GMP qw(primes vecprod random_nbit_prime);
sub ecm ($N, $zrange = 200, $plimit = 20000) {
# Check for perfect powers
if (is_prime_power($N, \my $p)) {
return $p;
}
# Make sure `N` is a Math::GMPz object
if (ref($N) ne 'Math::GMPz') {
$N = Math::GMPz->new("$N");
}
# Primes up to `plimit`
my @primes = @{primes($plimit)};
# Temporary mpz objects
my $t = Math::GMPz::Rmpz_init();
my $t1 = Math::GMPz::Rmpz_init();
my $t2 = Math::GMPz::Rmpz_init();
foreach my $z (-$zrange .. $zrange) {
my $x = Math::GMPz::Rmpz_init_set_ui(0);
my $y = Math::GMPz::Rmpz_init_set_ui(1);
foreach my $p (@primes) {
my ($xn, $yn);
my ($sx, $sy, $k) = ($x, $y, $p**logint($plimit, $p));
my $first = 1;
while ($k) {
if ($k & 1) {
if ($first) {
($xn, $yn) = ($sx, $sy);
$first = 0;
}
else {
Math::GMPz::Rmpz_sub($t, $sx, $xn);
if (!Math::GMPz::Rmpz_invert($t2, $t, $N)) {
Math::GMPz::Rmpz_gcd($t2, $t, $N);
Math::GMPz::Rmpz_cmp($t2, $N) ? return $t2 : last;
}
my $u = $t2;
# u * (sy - yn)
Math::GMPz::Rmpz_sub($t, $sy, $yn);
Math::GMPz::Rmpz_mul($t, $t, $u);
Math::GMPz::Rmpz_mod($t2, $t, $N);
my $L = $t2;
# L^2 - xn - sx
Math::GMPz::Rmpz_mul($t, $L, $L);
Math::GMPz::Rmpz_sub($t, $t, $xn);
Math::GMPz::Rmpz_sub($t, $t, $sx);
Math::GMPz::Rmpz_mod($t, $t, $N);
my $x_sum = Math::GMPz::Rmpz_init_set($t);
Math::GMPz::Rmpz_sub($t, $xn, $x_sum);
Math::GMPz::Rmpz_mul($t, $t, $L);
Math::GMPz::Rmpz_sub($t, $t, $yn);
Math::GMPz::Rmpz_mod($t, $t, $N);
$yn = Math::GMPz::Rmpz_init_set($t);
$xn = $x_sum;
}
}
Math::GMPz::Rmpz_mul_2exp($t, $sy, 1);
if (!Math::GMPz::Rmpz_invert($t2, $t, $N)) {
Math::GMPz::Rmpz_gcd($t2, $t, $N);
Math::GMPz::Rmpz_cmp($t2, $N) ? return $t2 : last;
}
my $u = $t2;
# u * (3 * sx^2 + z) % N
Math::GMPz::Rmpz_mul($t, $sx, $sx);
Math::GMPz::Rmpz_mul_ui($t, $t, 3);
$z < 0
? Math::GMPz::Rmpz_sub_ui($t, $t, -$z)
: Math::GMPz::Rmpz_add_ui($t, $t, $z);
Math::GMPz::Rmpz_mul($t, $t, $u);
Math::GMPz::Rmpz_mod($t2, $t, $N);
my $L = $t2;
# (L*L - 2*sx) % N
Math::GMPz::Rmpz_mul($t, $L, $L);
Math::GMPz::Rmpz_submul_ui($t, $sx, 2);
Math::GMPz::Rmpz_mod($t, $t, $N);
my $x2 = Math::GMPz::Rmpz_init_set($t);
# (L * (sx - x2) - sy) % N
Math::GMPz::Rmpz_sub($t, $sx, $x2);
Math::GMPz::Rmpz_mul($t, $t, $L);
Math::GMPz::Rmpz_sub($t, $t, $sy);
Math::GMPz::Rmpz_mod($t, $t, $N);
$sy = Math::GMPz::Rmpz_init_set($t);
$sx = $x2;
# Failure when t = 0
return $N if !Math::GMPz::Rmpz_sgn($t);
$k >>= 1;
}
($x, $y) = ($xn, $yn);
}
}
return $N; # failed to factorize N
}
# Factoring the 7th Fermat number: 2^128 + 1
say ecm(Math::GMPz->new(2)**128 + 1, 100, 8000); # takes ~1 second
say "\n=> More tests:";
foreach my $k (10 .. 40) {
my $n = Math::GMPz->new(vecprod(map { random_nbit_prime($k) } 1 .. 2));
my $p = ecm($n, logint($n, 2), logint($n, 2)**2);
if ($p > 1 and $p < $n) {
say "$n = $p * ", $n / $p;
}
else {
say "Failed to factor $n";
}
}