MadelineProto/prime.php

305 lines
9.7 KiB
PHP
Raw Normal View History

2016-06-23 23:51:08 +02:00
<?php
2016-07-14 15:15:50 +02:00
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'libpy2php');
require_once 'libpy2php.php';
2016-07-20 20:12:32 +02:00
2016-07-19 11:56:05 +02:00
class PrimeModule
{
public function __construct()
{
2016-07-18 18:43:50 +02:00
$this->smallprimeset = array_unique($this->primesbelow(100000));
$this->_smallprimeset = 100000;
$this->smallprimes = $this->primesbelow(10000);
}
2016-07-29 17:19:34 +02:00
2016-07-19 11:56:05 +02:00
public function primesbelow($N)
{
2016-07-18 18:43:50 +02:00
$res = [];
2016-07-19 11:56:05 +02:00
for ($i = 2; $i <= $N; $i++) {
if ($i % 2 != 1 && $i != 2) {
continue;
}
2016-07-18 18:43:50 +02:00
$d = 3;
$x = sqrt($i);
2016-07-19 11:56:05 +02:00
while ($i % $d != 0 && $d < $x) {
$d += 2;
}
if ((($i % $d == 0 && $i != $d) * 1) == 0) {
$res[] = $i;
}
2016-07-14 15:15:50 +02:00
}
2016-07-19 11:56:05 +02:00
2016-07-18 18:43:50 +02:00
return $res;
2016-07-14 15:15:50 +02:00
}
2016-07-19 11:56:05 +02:00
public function isprime($n, $precision = 7)
2016-07-18 18:43:50 +02:00
{
if (($n == 1) || (($n % 2) == 0)) {
return false;
} elseif (($n < 1)) {
throw new Exception('Out of bounds, first argument must be > 0');
} elseif (($n < $this->_smallprimeset)) {
return in_array($n, $this->smallprimeset);
2016-06-23 23:51:08 +02:00
}
2016-07-18 18:43:50 +02:00
$d = ($n - 1);
$s = 0;
while (($d % 2) == 0) {
$d = floor($d / 2);
2016-07-20 20:12:32 +02:00
$s++;
2016-07-18 18:43:50 +02:00
}
2016-07-20 20:12:32 +02:00
$break = false;
2016-07-18 18:43:50 +02:00
foreach (pyjslib_range($precision) as $repeat) {
$a = rand(2, ($n - 2));
$x = posmod(pow($a, $d), $n);
if (($x == 1) || ($x == ($n - 1))) {
continue;
2016-06-23 23:51:08 +02:00
}
2016-07-20 20:12:32 +02:00
foreach (pyjslib_range($s - 1) as $r) {
2016-07-18 18:43:50 +02:00
$x = posmod(pow($x, 2), $n);
if (($x == 1)) {
return false;
}
if (($x == ($n - 1))) {
2016-07-20 20:12:32 +02:00
$break = true;
2016-07-18 18:43:50 +02:00
}
2016-06-23 23:51:08 +02:00
}
2016-07-29 17:19:34 +02:00
if (!$break) {
return false;
}
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
2016-07-18 18:43:50 +02:00
return true;
2016-06-23 23:51:08 +02:00
}
2016-08-06 00:43:43 +02:00
2016-08-04 23:34:00 +02:00
// taken from https://github.com/enricostara/telegram-mt-node/blob/master/lib/security/pq-finder.js
2016-08-05 01:04:03 +02:00
public function getpq($pq)
{
$p = 0;
$q = 0;
while ($pq != $p * $q && $p != 0) {
2016-08-05 00:59:38 +02:00
for ($i = 0; $i < 3; $i++) {
$q = new \phpseclib\Math\BigInteger((random_int(0, 128) & 15) + 17);
$x = new \phpseclib\Math\BigInteger(random_int(0, 1000000000) + 1);
$y = $x;
$lim = 1 << ($i + 18);
for ($j = 1; $j < $lim; $j++) {
$a = $x;
$b = $x;
$c = $q;
while (!$b->equals($zero)) {
if ($b->powMod($one, $two)->equals($zero)) {
2016-08-05 01:04:03 +02:00
$c = $c->add($a);
if ($c->compare($pq) > 0) {
2016-08-05 00:59:38 +02:00
$c = $c->subtract($pq);
2016-08-05 01:04:03 +02:00
}
2016-08-05 00:59:38 +02:00
}
$a = $a->add($a);
if ($a->compare($pq) > 0) {
$a = $a->subtract($pq);
2016-08-04 23:34:00 +02:00
}
2016-08-05 00:59:38 +02:00
$b = $b->rightShift(1);
}
$x = $c;
$z = ($y->compare($x) > 0) ? $y->subtract($x) : $x->subtract($y);
$p = $z->gcd($pq);
if (!$p->equals($one)) {
2016-08-05 01:04:03 +02:00
break;
2016-08-04 23:34:00 +02:00
}
2016-08-05 00:59:38 +02:00
if (($j & ($j - 1)) === 0) {
$y = $x;
2016-08-04 23:34:00 +02:00
}
}
2016-08-05 00:59:38 +02:00
if (prime.gt(BigInteger.One())) {
2016-08-04 23:34:00 +02:00
break;
}
}
2016-08-05 00:59:38 +02:00
$q = $pq->divide(prime)[0];
2016-08-04 23:34:00 +02:00
}
2016-08-05 00:59:38 +02:00
$_pq = ($q->compare($p) > 0) ? [$p, $q] : [$q, $p];
2016-08-05 01:04:03 +02:00
2016-08-05 01:03:45 +02:00
return $_pq;
2016-08-04 23:34:00 +02:00
}
2016-08-05 01:04:03 +02:00
2016-07-19 11:56:05 +02:00
public function pollard_brent($n)
2016-07-18 18:43:50 +02:00
{
$zero = new \phpseclib\Math\BigInteger(0);
$one = new \phpseclib\Math\BigInteger(1);
$two = new \phpseclib\Math\BigInteger(2);
$three = new \phpseclib\Math\BigInteger(3);
2016-07-30 17:17:15 +02:00
if ($n->powMod($one, $two)->toString() == '0') {
2016-07-18 18:43:50 +02:00
return 2;
}
2016-07-30 17:17:15 +02:00
if ($n->powMod($one, $three)->toString() == '0') {
2016-07-18 18:43:50 +02:00
return 3;
}
2016-07-20 20:23:26 +02:00
$big = new \phpseclib\Math\BigInteger();
$max = $n->subtract($one);
list($y, $c, $m) = [new \phpseclib\Math\BigInteger(87552211475113995), new \phpseclib\Math\BigInteger(330422027228888537), new \phpseclib\Math\BigInteger(226866727920975483)];
//[$big->random($one, $max), $big->random($one, $max), $big->random($one, $max)];
list($g, $r, $q) = [$one, $one, $one];
while ($g->equals($one)) {
2016-07-18 18:43:50 +02:00
$x = $y;
2016-08-03 22:07:58 +02:00
$params = ['y' => $y, 'two' => $two, 'c' => $c, 'one' => $one, 'n' => $n];
2016-08-03 22:07:40 +02:00
$r->loopforeach(function ($i, $params) {
2016-08-03 22:07:58 +02:00
$params['y'] = $params['y']->powMod($params['two'], $params['n'])->add($params['c'])->powMod($params['one'], $params['n']);
}, $params);
2016-08-03 22:07:40 +02:00
each($params);
$k = $zero;
while ($k->compare($r) == -1 && $g->equals($one)) {
2016-07-18 18:43:50 +02:00
$ys = $y;
2016-08-03 22:07:58 +02:00
$params = ['x' => $x, 'y' => $y, 'two' => $two, 'c' => $c, 'one' => $one, 'n' => $n, 'q' => $q];
2016-08-03 22:07:40 +02:00
$m->min($r->subtract($k))->loopforeach(function ($i, $params) {
2016-08-03 22:07:58 +02:00
$params['y'] = $params['y']->powMod($params['two'], $params['n'])->add($params['c'])->powMod($params['one'], $params['n']);
$params['q'] = $params['q']->multiply($params['x']->subtract($params['y'])->abs())->powMod($params['one'], $params['n']);
}, $params);
2016-08-03 22:07:40 +02:00
each($params);
$g = $q->gcd($n);
$k = $k->add($m);
2016-07-18 18:43:50 +02:00
}
$r = $r->multiply($two);
2016-06-23 23:51:08 +02:00
}
2016-08-03 22:07:40 +02:00
die;
if ($g->equals($n)) {
2016-07-18 18:43:50 +02:00
while (true) {
$ys = $ys->powMod($two, $n)->add($c)->powMod($one, $n);
$g = $x->subtract($ys)->abs()->gcd($n);
if ($g->compare($one) == 1) {
2016-07-18 18:43:50 +02:00
break;
}
2016-06-23 23:51:08 +02:00
}
}
2016-07-14 15:15:50 +02:00
2016-07-18 18:43:50 +02:00
return $g;
}
2016-07-19 11:56:05 +02:00
public function primefactors($pq, $sort = false)
2016-07-18 18:43:50 +02:00
{
2016-08-06 00:43:43 +02:00
if (function_exists('shell_exec')) {
2016-08-06 02:49:38 +02:00
try {
// Use the python version.
$res = explode(' ', shell_exec('python getpq.py '.$pq));
if (count($res) == 2) {
return $res;
}
2016-08-06 02:50:19 +02:00
} catch (ErrorException $e) {
}
}
// Else do factorization with wolfram alpha :)))))
2016-08-06 00:43:43 +02:00
$query = 'Do prime factorization of '.$pq;
$params = [
2016-08-06 00:43:43 +02:00
'async' => true,
'banners' => 'raw',
'debuggingdata' => false,
'format' => 'moutput',
'formattimeout' => 8,
'input' => $query,
'output' => 'JSON',
'proxycode' => json_decode(file_get_contents('http://www.wolframalpha.com/api/v1/code'), true)['code'],
];
2016-08-06 00:43:43 +02:00
$url = 'https://www.wolframalpha.com/input/json.jsp?'.http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2016-08-06 00:43:43 +02:00
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Referer: https://www.wolframalpha.com/input/?i='.urlencode($query)]);
curl_setopt($ch, CURLOPT_URL, $url);
2016-08-06 00:43:43 +02:00
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
2016-08-06 00:43:43 +02:00
foreach ($res['queryresult']['pods'] as $cur) {
if ($cur['id'] == 'Divisors') {
$res = explode(', ', preg_replace(["/{\d+, /", "/, \d+}$/"], '', $cur['subpods'][0]['moutput']));
break;
}
}
2016-08-06 00:43:43 +02:00
if (count($res) == 2) {
return $res;
}
2016-07-18 18:43:50 +02:00
$factors = [];
$one = new \phpseclib\Math\BigInteger(1);
$two = new \phpseclib\Math\BigInteger(2);
$limit = $n->root()->add($one);
2016-07-18 18:43:50 +02:00
foreach ($this->smallprimes as $checker) {
$checker = new \phpseclib\Math\BigInteger($checker);
if ($limit->compare($checker) == -1) {
2016-06-23 23:51:08 +02:00
break;
}
2016-07-30 17:17:15 +02:00
while ($n->modPow($one, $checker)->toString() == '0') {
2016-07-18 18:43:50 +02:00
$factors[] = $checker;
$n = $n->divide($checker)[0];
$limit = $n->root()->add($one);
if ($limit->compare($checker) == -1) {
2016-07-18 18:43:50 +02:00
break;
}
}
2016-06-23 23:51:08 +02:00
}
if ($n->compare($two) == -1) {
2016-07-18 18:43:50 +02:00
return $factors;
}
while ($n->compare($two) == 1) {
if ($n->isprime()) {
2016-07-18 18:43:50 +02:00
$factors[] = $n;
break;
}
$factor = $this->pollard_brent($n);
$factors[] = $this->primefactors($factor);
$n = floor($n / $factor);
}
if ($sort) {
$factors = sort($factors);
}
2016-06-23 23:51:08 +02:00
return $factors;
}
2016-07-19 11:56:05 +02:00
public function factorization($n)
2016-07-18 18:43:50 +02:00
{
$factors = [];
foreach (primefactors($n) as $p1) {
2016-07-19 11:56:05 +02:00
if (isset($factors[$p1])) {
2016-07-18 18:43:50 +02:00
$factors[$p1] += 1;
} else {
$factors[$p1] = 1;
}
2016-06-23 23:51:08 +02:00
}
2016-07-14 15:15:50 +02:00
2016-07-18 18:43:50 +02:00
return $factors;
2016-06-23 23:51:08 +02:00
}
2016-07-19 11:56:05 +02:00
public function totient($n)
2016-07-18 18:43:50 +02:00
{
$totients = [];
if (($n == 0)) {
return 1;
}
2016-07-19 11:56:05 +02:00
if (isset($totients[$n])) {
2016-07-18 18:43:50 +02:00
return $totients[$n];
}
$tot = 1;
foreach (factorization($n) as $p => $exp) {
$tot *= (($p - 1) * pow($p, ($exp - 1)));
}
$totients[$n] = $tot;
return $tot;
2016-06-23 23:51:08 +02:00
}
2016-07-19 11:56:05 +02:00
public function gcd($a, $b)
2016-07-18 18:43:50 +02:00
{
if (($a == $b)) {
return $a;
}
while (($b > 0)) {
2016-07-20 20:12:32 +02:00
list($a, $b) = [$b, posmod($a, $b)];
2016-07-18 18:43:50 +02:00
}
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $a;
}
2016-07-19 11:56:05 +02:00
public function lcm($a, $b)
2016-07-18 18:43:50 +02:00
{
2016-07-18 18:56:33 +02:00
return floor(abs(($a * $b)) / $this->gcd($a, $b));
2016-06-23 23:51:08 +02:00
}
2016-07-19 11:56:05 +02:00
}