Merge pull request #3 from danog/analysis-z4wP3D

Applied fixes from StyleCI
This commit is contained in:
Daniil Gentili 2016-07-19 11:56:19 +02:00 committed by GitHub
commit 4f02410de0
3 changed files with 56 additions and 32 deletions

2
TL.php
View File

@ -112,7 +112,7 @@ class TL
fwrite($bytes_io, $this->struct->pack('<i', $value)); fwrite($bytes_io, $this->struct->pack('<i', $value));
break; break;
case 'long': case 'long':
assert(is_long($value)); assert(is_int($value));
fwrite($bytes_io, $this->struct->pack('<q', $value)); fwrite($bytes_io, $this->struct->pack('<q', $value));
break; break;
case 'int128': case 'int128':

View File

@ -41,6 +41,7 @@ function len($input)
if (is_array($input)) { if (is_array($input)) {
return count($input); return count($input);
} }
return strlen($input); return strlen($input);
} }
@ -71,11 +72,15 @@ function vis($bs)
} }
/** /**
* posmod(numeric,numeric) : numeric * posmod(numeric,numeric) : numeric
* Works just like the % (modulus) operator, only returns always a postive number * Works just like the % (modulus) operator, only returns always a postive number.
*/ */
function posmod($a, $b) { function posmod($a, $b)
{
$resto = $a % $b; $resto = $a % $b;
if($resto < 0) $resto += abs($b); if ($resto < 0) {
$resto += abs($b);
}
return $resto; return $resto;
} }
@ -108,7 +113,7 @@ function long_to_bytes($n, $blocksize = 0)
$s = null; $s = null;
$n = long($n); $n = long($n);
while (($n > 0)) { while (($n > 0)) {
$s = $GLOBALS["struct"]->pack('I', $n & 4294967295) . $s; $s = $GLOBALS['struct']->pack('I', $n & 4294967295).$s;
$n = $n >> 32; $n = $n >> 32;
} }
foreach (pyjslib_range(strlen($s)) as $i) { foreach (pyjslib_range(strlen($s)) as $i) {
@ -118,7 +123,7 @@ function long_to_bytes($n, $blocksize = 0)
} }
$s = substr($s, $i); $s = substr($s, $i);
if ($blocksize > 0 && strlen($s) % $blocksize) { if ($blocksize > 0 && strlen($s) % $blocksize) {
$s = pack("@" . $blocksize - (strlen($s) % $blocksize)) . $s; $s = pack('@'.$blocksize - (strlen($s) % $blocksize)).$s;
} }
return $s; return $s;
@ -135,12 +140,13 @@ function bytes_to_long($s)
$length = strlen($s); $length = strlen($s);
if ($length % 4) { if ($length % 4) {
$extra = (4 - ($length % 4)); $extra = (4 - ($length % 4));
$s = pack("@" . $extra) . $s; $s = pack('@'.$extra).$s;
$length += $extra; $length += $extra;
} }
foreach (pyjslib_range(0, $length, 4) as $i) { 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; return $acc;
} }
function string2bin($string) function string2bin($string)
@ -261,6 +267,7 @@ class Session
pyjslib_printnl('Retry call method'); pyjslib_printnl('Retry call method');
continue; continue;
} }
return $this->tl->deserialize(fopen_and_write('php://memory', 'rw+b', $server_answer)); 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); $pq = bytes_to_long($pq_bytes);
var_dump($this->PrimeModule->pollard_brent(2118588165281151121)); var_dump($this->PrimeModule->pollard_brent(2118588165281151121));
//$this->PrimeModule->primefactors(1724114033281923457) //$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); list($p, $q) = $this->PrimeModule->primefactors($pq);
if ($p > $q) { if ($p > $q) {
list($p, $q) = [$q, $p]; list($p, $q) = [$q, $p];

View File

@ -2,26 +2,36 @@
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'libpy2php'); set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'libpy2php');
require_once 'libpy2php.php'; require_once 'libpy2php.php';
class PrimeModule { class PrimeModule
function __construct() { {
public function __construct()
{
$this->smallprimeset = array_unique($this->primesbelow(100000)); $this->smallprimeset = array_unique($this->primesbelow(100000));
$this->_smallprimeset = 100000; $this->_smallprimeset = 100000;
$this->smallprimes = $this->primesbelow(10000); $this->smallprimes = $this->primesbelow(10000);
} }
function primesbelow($N) {
$res = []; public function primesbelow($N)
for ($i = 2; $i <= $N; $i++)
{ {
if($i % 2 != 1 && $i != 2) continue; $res = [];
for ($i = 2; $i <= $N; $i++) {
if ($i % 2 != 1 && $i != 2) {
continue;
}
$d = 3; $d = 3;
$x = sqrt($i); $x = sqrt($i);
while ($i % $d != 0 && $d < $x) $d += 2; while ($i % $d != 0 && $d < $x) {
if((($i % $d == 0 && $i != $d) * 1) == 0) $res[] = $i; $d += 2;
} }
if ((($i % $d == 0 && $i != $d) * 1) == 0) {
$res[] = $i;
}
}
return $res; return $res;
} }
function isprime($n, $precision = 7) public function isprime($n, $precision = 7)
{ {
if (($n == 1) || (($n % 2) == 0)) { if (($n == 1) || (($n % 2) == 0)) {
return false; return false;
@ -55,7 +65,8 @@ class PrimeModule {
return true; return true;
} }
function pollard_brent($n)
public function pollard_brent($n)
{ {
if ((($n % 2) == 0)) { if ((($n % 2) == 0)) {
return 2; return 2;
@ -94,7 +105,8 @@ class PrimeModule {
return $g; return $g;
} }
function primefactors($n, $sort = false)
public function primefactors($n, $sort = false)
{ {
$factors = []; $factors = [];
$limit = ((int) (pow($n, 0.5)) + 1); $limit = ((int) (pow($n, 0.5)) + 1);
@ -129,7 +141,8 @@ class PrimeModule {
return $factors; return $factors;
} }
function factorization($n)
public function factorization($n)
{ {
$factors = []; $factors = [];
foreach (primefactors($n) as $p1) { foreach (primefactors($n) as $p1) {
@ -142,7 +155,8 @@ class PrimeModule {
return $factors; return $factors;
} }
function totient($n)
public function totient($n)
{ {
$totients = []; $totients = [];
if (($n == 0)) { if (($n == 0)) {
@ -159,7 +173,8 @@ class PrimeModule {
return $tot; return $tot;
} }
function gcd($a, $b)
public function gcd($a, $b)
{ {
if (($a == $b)) { if (($a == $b)) {
return $a; return $a;
@ -170,10 +185,12 @@ class PrimeModule {
return $a; return $a;
} }
function lcm($a, $b)
public function lcm($a, $b)
{ {
return floor(abs(($a * $b)) / $this->gcd($a, $b)); return floor(abs(($a * $b)) / $this->gcd($a, $b));
} }
/* /*
function pqPrimeLeemon ($what) { function pqPrimeLeemon ($what) {
$minBits = 64; $minBits = 64;
@ -248,5 +265,4 @@ function pqPrimeLeemon ($what) {
return [bytesFromLeemonBigInt(P), bytesFromLeemonBigInt(Q), it] return [bytesFromLeemonBigInt(P), bytesFromLeemonBigInt(Q), it]
}*/ }*/
} }