Updated prime module
This commit is contained in:
parent
e86b935745
commit
429695db43
69
prime.php
69
prime.php
@ -73,52 +73,54 @@ class PrimeModule
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// taken from https://github.com/enricostara/telegram-mt-node/blob/master/lib/security/pq-finder.js
|
// taken from https://github.com/enricostara/telegram-mt-node/blob/master/lib/security/pq-finder.js
|
||||||
public function factorization($num) {
|
public function factorization($pq) {
|
||||||
$zero = new \phpseclib\Math\BigInteger(0);
|
$zero = new \phpseclib\Math\BigInteger(0);
|
||||||
$one = new \phpseclib\Math\BigInteger(1);
|
$one = new \phpseclib\Math\BigInteger(1);
|
||||||
$two = new \phpseclib\Math\BigInteger(2);
|
$two = new \phpseclib\Math\BigInteger(2);
|
||||||
$three = new \phpseclib\Math\BigInteger(3);
|
$three = new \phpseclib\Math\BigInteger(3);
|
||||||
|
|
||||||
$prime = new \phpseclib\Math\BigInteger();
|
$p = new \phpseclib\Math\BigInteger();
|
||||||
|
$q = new \phpseclib\Math\BigInteger();
|
||||||
for ($i = 0; $i < 3; $i++) {
|
while (!$pq->equals($p->multiply($q))) {
|
||||||
$q = new \phpseclib\Math\BigInteger((random_int(0, 128) & 15) + 17);
|
for ($i = 0; $i < 3; $i++) {
|
||||||
$x = new \phpseclib\Math\BigInteger(random_int(0, 1000000000) + 1);
|
$q = new \phpseclib\Math\BigInteger((random_int(0, 128) & 15) + 17);
|
||||||
$y = $x;
|
$x = new \phpseclib\Math\BigInteger(random_int(0, 1000000000) + 1);
|
||||||
$lim = 1 << ($i + 18);
|
$y = $x;
|
||||||
for ($j = 1; $j < $lim; $j++) {
|
$lim = 1 << ($i + 18);
|
||||||
$a = $x;
|
for ($j = 1; $j < $lim; $j++) {
|
||||||
$b = $x;
|
$a = $x;
|
||||||
$c = $q;
|
$b = $x;
|
||||||
while (!$b->equals($zero)) {
|
$c = $q;
|
||||||
if (b.repr[0] & 1) {
|
while (!$b->equals($zero)) {
|
||||||
c.addEquals(a);
|
if ($b->powMod($one, $two)->equals($zero)) {
|
||||||
if (c.gt(num)) {
|
$c = $c->add($a);
|
||||||
c = c.subtract(num);
|
if ($c->compare($pq) > 0) {
|
||||||
|
$c = $c->subtract($pq);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
$a = $a->add($a);
|
||||||
|
if ($a->compare($pq) > 0) {
|
||||||
|
$a = $a->subtract($pq);
|
||||||
|
}
|
||||||
|
$b = $b->rightShift(1);
|
||||||
}
|
}
|
||||||
a.addEquals(a);
|
$x = $c;
|
||||||
if (a.gt(num)) {
|
$z = ($y->compare($x) > 0) ? $y->subtract($x) : $x->subtract($y);
|
||||||
a = a.subtract(num);
|
$p = $z->gcd($pq);
|
||||||
|
if (!$p->equals($one)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (($j & ($j - 1)) === 0) {
|
||||||
|
$y = $x;
|
||||||
}
|
}
|
||||||
b = b.shiftRight(1);
|
|
||||||
}
|
}
|
||||||
$x = $c;
|
if (prime.gt(BigInteger.One())) {
|
||||||
$z = $y.gt(x) ? y.subtract(x) : x.subtract(y);
|
|
||||||
$prime = z.gcd(num, a, b);
|
|
||||||
if (!prime.eql(BigInteger.One())) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ((j & (j - 1)) === 0) {
|
|
||||||
$y = $x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (prime.gt(BigInteger.One())) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
$q = $pq->divide(prime)[0];
|
||||||
}
|
}
|
||||||
$cofactor = num.divide(prime)[0];
|
$_pq = ($q->compare($p) > 0) ? [$p, $q] : [$q, $p];
|
||||||
$_pq = cofactor.gt(prime) ? [prime, cofactor] : [cofactor, prime];
|
|
||||||
return _$pq;
|
return _$pq;
|
||||||
}
|
}
|
||||||
public function pollard_brent($n)
|
public function pollard_brent($n)
|
||||||
@ -177,6 +179,7 @@ class PrimeModule
|
|||||||
{
|
{
|
||||||
$factors = [];
|
$factors = [];
|
||||||
$n = new \phpseclib\Math\BigInteger(1724114033281923457);
|
$n = new \phpseclib\Math\BigInteger(1724114033281923457);
|
||||||
|
var_dump($this->factorization($n));
|
||||||
$one = new \phpseclib\Math\BigInteger(1);
|
$one = new \phpseclib\Math\BigInteger(1);
|
||||||
$two = new \phpseclib\Math\BigInteger(2);
|
$two = new \phpseclib\Math\BigInteger(2);
|
||||||
$limit = $n->root()->add($one);
|
$limit = $n->root()->add($one);
|
||||||
|
Loading…
Reference in New Issue
Block a user