1
0
mirror of https://github.com/danog/PrimeModule.git synced 2024-11-30 04:39:09 +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;
for ($i = 0; $i < 3; $i++) {
$q = (rand(0, 127) & 15) + 17;
$x = rand(0, 1000000000) + 1;
$it = 0;
for ($i = 0; $i < 3 || $it < 1000; $i++) {
$t = ((rand(0, 127) & 15) + 17) % $what;
$x = rand(0, 1 << 31) % ($what - 1) + 1;
$y = $x;
$lim = 1 << ($i + 18);
for ($j = 1; $j <= $lim; $j++) {
list($a, $b, $c) = [$x, $x, $q];
while ($b != 0) {
if (($b & 1) != 0) {
++$it;
$a = $x;
$b = $x;
$c = $t;
while ($b) {
if ($b & 1) {
$c += $a;
if ($c >= $what) {
$c -= $what;
@ -48,23 +52,25 @@ class PrimeModule
$b >>= 1;
}
$x = $c;
$z = ($x < $y) ? $y - $x : $x - $y;
$z = ($x < $y) ? $what + $x - $y : $x - $y;
$g = self::gcd($z, $what);
if ($g != 1) {
break;
}
if (($j & ($j - 1)) === 0) {
if (!($j & ($j - 1))) {
$y = $x;
}
}
if ($g > 1) {
if ($g > 1 && $g < $what) {
break;
}
}
$p = $what;
return min($p, $g);
if ($g > 1 && $g < $what) {
return $g;
}
return $what;
}
public static function native($what)
@ -80,10 +86,10 @@ class PrimeModule
public static function python_single($what)
{
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)) {
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');
if ($res == '' || is_null($res) || !is_numeric($res)) {
return false;

View File

@ -1,5 +1,5 @@
import sys
from math import log
from argparse import ArgumentParser
# Multiple Polynomial Quadratic Sieve
def mpqs(n, verbose=False):
@ -308,7 +308,8 @@ def mod_sqrt(n, p):
#integer sqrt of n
def isqrt(n):
c = n*4/3
n = int(n)
c = int(n*4/3)
d = c.bit_length()
a = d>>1
@ -321,10 +322,10 @@ def isqrt(n):
if x != y:
x = y
y = (x + n/x) >> 1
y = int(x + n/x) >> 1
while y < x:
x = y
y = (x + n/x) >> 1
y = int(x + n/x) >> 1
return x
# strong probable prime
@ -496,12 +497,4 @@ def next_prime(n):
return i
i += o
if __name__ == "__main__":
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)
print(mpqs(int(sys.argv[1])))