diff --git a/TL.php b/TL.php index 0655f563..ae7e73df 100644 --- a/TL.php +++ b/TL.php @@ -112,7 +112,7 @@ class TL fwrite($bytes_io, $this->struct->pack('struct->pack(' 0)) { - $s = $GLOBALS["struct"]->pack('I', $n & 4294967295) . $s; + $s = $GLOBALS['struct']->pack('I', $n & 4294967295).$s; $n = $n >> 32; } foreach (pyjslib_range(strlen($s)) as $i) { @@ -118,7 +123,7 @@ function long_to_bytes($n, $blocksize = 0) } $s = substr($s, $i); if ($blocksize > 0 && strlen($s) % $blocksize) { - $s = pack("@" . $blocksize - (strlen($s) % $blocksize)) . $s; + $s = pack('@'.$blocksize - (strlen($s) % $blocksize)).$s; } return $s; @@ -135,12 +140,13 @@ function bytes_to_long($s) $length = strlen($s); if ($length % 4) { $extra = (4 - ($length % 4)); - $s = pack("@" . $extra) . $s; + $s = pack('@'.$extra).$s; $length += $extra; } foreach (pyjslib_range(0, $length, 4) as $i) { - $acc = ($acc << 32) + $GLOBALS["struct"]->unpack('>I', substr($s, $i, 4))[0]; + $acc = ($acc << 32) + $GLOBALS['struct']->unpack('>I', substr($s, $i, 4))[0]; } + return $acc; } function string2bin($string) @@ -261,6 +267,7 @@ class Session pyjslib_printnl('Retry call method'); continue; } + return $this->tl->deserialize(fopen_and_write('php://memory', 'rw+b', $server_answer)); } } @@ -276,7 +283,8 @@ class Session $pq = bytes_to_long($pq_bytes); var_dump($this->PrimeModule->pollard_brent(2118588165281151121)); //$this->PrimeModule->primefactors(1724114033281923457) - var_dump($this->PrimeModule->primefactors(378221), $this->PrimeModule->primefactors(15));die; + var_dump($this->PrimeModule->primefactors(378221), $this->PrimeModule->primefactors(15)); + die; list($p, $q) = $this->PrimeModule->primefactors($pq); if ($p > $q) { list($p, $q) = [$q, $p]; diff --git a/prime.php b/prime.php index e1e238a3..37a6d5a4 100644 --- a/prime.php +++ b/prime.php @@ -2,26 +2,36 @@ set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'libpy2php'); require_once 'libpy2php.php'; -class PrimeModule { - function __construct() { +class PrimeModule +{ + public function __construct() + { $this->smallprimeset = array_unique($this->primesbelow(100000)); $this->_smallprimeset = 100000; $this->smallprimes = $this->primesbelow(10000); } - function primesbelow($N) { + + public function primesbelow($N) + { $res = []; - for ($i = 2; $i <= $N; $i++) - { - if($i % 2 != 1 && $i != 2) continue; + for ($i = 2; $i <= $N; $i++) { + if ($i % 2 != 1 && $i != 2) { + continue; + } $d = 3; $x = sqrt($i); - while ($i % $d != 0 && $d < $x) $d += 2; - if((($i % $d == 0 && $i != $d) * 1) == 0) $res[] = $i; + while ($i % $d != 0 && $d < $x) { + $d += 2; + } + if ((($i % $d == 0 && $i != $d) * 1) == 0) { + $res[] = $i; + } } + return $res; } - function isprime($n, $precision = 7) + public function isprime($n, $precision = 7) { if (($n == 1) || (($n % 2) == 0)) { return false; @@ -55,7 +65,8 @@ class PrimeModule { return true; } - function pollard_brent($n) + + public function pollard_brent($n) { if ((($n % 2) == 0)) { return 2; @@ -94,10 +105,11 @@ class PrimeModule { return $g; } - function primefactors($n, $sort = false) + + public function primefactors($n, $sort = false) { $factors = []; - $limit = ((int)(pow($n, 0.5)) + 1); + $limit = ((int) (pow($n, 0.5)) + 1); foreach ($this->smallprimes as $checker) { if (($checker > $limit)) { break; @@ -105,7 +117,7 @@ class PrimeModule { while (($n % $checker) == 0) { $factors[] = $checker; $n = floor($n / $checker); - $limit = ((int)(pow($n, 0.5)) + 1); + $limit = ((int) (pow($n, 0.5)) + 1); if (($checker > $limit)) { break; } @@ -129,11 +141,12 @@ class PrimeModule { return $factors; } - function factorization($n) + + public function factorization($n) { $factors = []; foreach (primefactors($n) as $p1) { - if(isset($factors[$p1])) { + if (isset($factors[$p1])) { $factors[$p1] += 1; } else { $factors[$p1] = 1; @@ -142,13 +155,14 @@ class PrimeModule { return $factors; } - function totient($n) + + public function totient($n) { $totients = []; if (($n == 0)) { return 1; } - if(isset($totients[$n])) { + if (isset($totients[$n])) { return $totients[$n]; } $tot = 1; @@ -159,7 +173,8 @@ class PrimeModule { return $tot; } - function gcd($a, $b) + + public function gcd($a, $b) { if (($a == $b)) { return $a; @@ -170,10 +185,12 @@ class PrimeModule { return $a; } - function lcm($a, $b) + + public function lcm($a, $b) { return floor(abs(($a * $b)) / $this->gcd($a, $b)); } + /* function pqPrimeLeemon ($what) { $minBits = 64; @@ -248,5 +265,4 @@ function pqPrimeLeemon ($what) { return [bytesFromLeemonBigInt(P), bytesFromLeemonBigInt(Q), it] }*/ - -} \ No newline at end of file +}