Bugfixes to prime factorization

This commit is contained in:
Daniil Gentili 2018-03-13 15:30:34 +01:00
parent f73b4645af
commit f94d4804dd

View File

@ -72,14 +72,75 @@ trait AuthKeyHandler
* Compute p and q
*/
$pq = new \phpseclib\Math\BigInteger($pq_bytes, 256);
$q = new \phpseclib\Math\BigInteger(0);
$p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::auto_single($pq->__toString()));
$q = $pq->divide($p)[0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
if (!$p->equals($this->zero)) {
$q = $pq->divide($p)[0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
}
}
if (!$pq->equals($p->multiply($q))) {
throw new \danog\MadelineProto\SecurityException("couldn't compute p and q. Original pq: {$pq}, computed p: {$p}, computed q: {$q}, computed pq: ".$p->multiply($q));
\danog\MadelineProto\Logger::log("Automatic factorization failed, trying native CPP module", \danog\MadelineProto\Logger::ERROR);
$p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::native_single_cpp($pq->__toString()));
if (!$p->equals($this->zero)) {
$q = $pq->divide($p) [0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
}
}
if (!$pq->equals($p->multiply($q))) {
\danog\MadelineProto\Logger::log("Automatic factorization failed, trying alt py module", \danog\MadelineProto\Logger::ERROR);
$p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::python_single_alt($pq->__toString()));
if (!$p->equals($this->zero)) {
$q = $pq->divide($p) [0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
}
}
if (!$pq->equals($p->multiply($q))) {
\danog\MadelineProto\Logger::log("Automatic factorization failed, trying py module", \danog\MadelineProto\Logger::ERROR);
$p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::python_single($pq->__toString()));
if (!$p->equals($this->zero)) {
$q = $pq->divide($p) [0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
}
}
if (!$pq->equals($p->multiply($q))) {
\danog\MadelineProto\Logger::log("Automatic factorization failed, trying native module", \danog\MadelineProto\Logger::ERROR);
$p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::native_single($pq->__toString()));
if (!$p->equals($this->zero)) {
$q = $pq->divide($p) [0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
}
}
if (!$pq->equals($p->multiply($q))) {
\danog\MadelineProto\Logger::log("Automatic factorization failed, trying wolfram module", \danog\MadelineProto\Logger::ERROR);
$p = new \phpseclib\Math\BigInteger(\danog\PrimeModule::wolfram_single($pq->__toString()));
if (!$p->equals($this->zero)) {
$q = $pq->divide($p) [0];
if ($p->compare($q) > 0) {
list($p, $q) = [$q, $p];
}
}
if (!$pq->equals($p->multiply($q))) {
throw new \danog\MadelineProto\SecurityException("couldn't compute p and q. Original pq: {$pq}, computed p: {$p}, computed q: {$q}, computed pq: ".$p->multiply($q));
}
}
}
}
}
}
\danog\MadelineProto\Logger::log('Factorization '.$pq.' = '.$p.' * '.$q, \danog\MadelineProto\Logger::VERBOSE);
/*
* ***********************************************************************