From f94d4804dd79507dee46631c67ee682784c8712b Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 13 Mar 2018 15:30:34 +0100 Subject: [PATCH] Bugfixes to prime factorization --- .../MTProtoTools/AuthKeyHandler.php | 69 +++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 49515209..5f437ad1 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -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); /* * ***********************************************************************