1
0
mirror of https://github.com/danog/PrimeModule.git synced 2024-11-26 20:34:37 +01:00

Fix native factorization

This commit is contained in:
Daniil Gentili 2021-02-08 19:57:32 +01:00
parent 55de0454e9
commit 293b767097
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 25 additions and 26 deletions

View File

@ -27,15 +27,19 @@ class PrimeModule
} }
} }
$g = 0; $g = 0;
for ($i = 0; $i < 3; $i++) { $it = 0;
$q = (rand(0, 127) & 15) + 17; for ($i = 0; $i < 3 || $it < 1000; $i++) {
$x = rand(0, 1000000000) + 1; $t = ((rand(0, 127) & 15) + 17) % $what;
$x = rand(0, 1 << 31) % ($what - 1) + 1;
$y = $x; $y = $x;
$lim = 1 << ($i + 18); $lim = 1 << ($i + 18);
for ($j = 1; $j <= $lim; $j++) { for ($j = 1; $j <= $lim; $j++) {
list($a, $b, $c) = [$x, $x, $q]; ++$it;
while ($b != 0) { $a = $x;
if (($b & 1) != 0) { $b = $x;
$c = $t;
while ($b) {
if ($b & 1) {
$c += $a; $c += $a;
if ($c >= $what) { if ($c >= $what) {
$c -= $what; $c -= $what;
@ -48,23 +52,25 @@ class PrimeModule
$b >>= 1; $b >>= 1;
} }
$x = $c; $x = $c;
$z = ($x < $y) ? $y - $x : $x - $y; $z = ($x < $y) ? $what + $x - $y : $x - $y;
$g = self::gcd($z, $what); $g = self::gcd($z, $what);
if ($g != 1) { if ($g != 1) {
break; break;
} }
if (($j & ($j - 1)) === 0) { if (!($j & ($j - 1))) {
$y = $x; $y = $x;
} }
} }
if ($g > 1) { if ($g > 1 && $g < $what) {
break; break;
} }
} }
$p = $what;
return min($p, $g); if ($g > 1 && $g < $what) {
return $g;
}
return $what;
} }
public static function native($what) public static function native($what)
@ -80,10 +86,10 @@ class PrimeModule
public static function python_single($what) public static function python_single($what)
{ {
if (function_exists('shell_exec')) { if (function_exists('shell_exec')) {
$res = trim(shell_exec('timeout 10 python '.__DIR__.'/prime.py '.$what.' 2>&1')); $res = trim(shell_exec('timeout 10 python2 '.__DIR__.'/prime.py '.$what.' 2>&1'));
if ($res == '' || is_null($res) || !is_numeric($res)) { if ($res == '' || is_null($res) || !is_numeric($res)) {
copy(__DIR__.'/prime.py', getcwd().'/.prime.py'); copy(__DIR__.'/prime.py', getcwd().'/.prime.py');
$res = trim(shell_exec('timeout 10 python '.getcwd().'/.prime.py '.$what.' 2>&1')); $res = trim(shell_exec('timeout 10 python2 '.getcwd().'/.prime.py '.$what.' 2>&1'));
unlink(getcwd().'/.prime.py'); unlink(getcwd().'/.prime.py');
if ($res == '' || is_null($res) || !is_numeric($res)) { if ($res == '' || is_null($res) || !is_numeric($res)) {
return false; return false;

View File

@ -1,5 +1,5 @@
import sys
from math import log from math import log
from argparse import ArgumentParser
# Multiple Polynomial Quadratic Sieve # Multiple Polynomial Quadratic Sieve
def mpqs(n, verbose=False): def mpqs(n, verbose=False):
@ -308,7 +308,8 @@ def mod_sqrt(n, p):
#integer sqrt of n #integer sqrt of n
def isqrt(n): def isqrt(n):
c = n*4/3 n = int(n)
c = int(n*4/3)
d = c.bit_length() d = c.bit_length()
a = d>>1 a = d>>1
@ -321,10 +322,10 @@ def isqrt(n):
if x != y: if x != y:
x = y x = y
y = (x + n/x) >> 1 y = int(x + n/x) >> 1
while y < x: while y < x:
x = y x = y
y = (x + n/x) >> 1 y = int(x + n/x) >> 1
return x return x
# strong probable prime # strong probable prime
@ -496,12 +497,4 @@ def next_prime(n):
return i return i
i += o i += o
if __name__ == "__main__": print(mpqs(int(sys.argv[1])))
parser =ArgumentParser(description='Uses a MPQS to factor a composite number')
parser.add_argument('composite', metavar='number_to_factor', type=long,
help='the composite number to factor')
parser.add_argument('--verbose', dest='verbose', action='store_true',
help="enable verbose output")
args = parser.parse_args()
print mpqs(args.composite)