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));
break;
case 'long':
assert(is_long($value));
assert(is_int($value));
fwrite($bytes_io, $this->struct->pack('<q', $value));
break;
case 'int128':

View File

@ -23,7 +23,7 @@ function newcrc32($data)
*/
function hex_dump(...$what)
{
foreach($what as $w){
foreach ($what as $w) {
var_dump(bin2hex($w));
}
}
@ -39,8 +39,9 @@ function hex_dump(...$what)
function len($input)
{
if (is_array($input)) {
return count($input);
return count($input);
}
return strlen($input);
}
@ -71,11 +72,15 @@ function vis($bs)
}
/**
* 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;
if($resto < 0) $resto += abs($b);
if ($resto < 0) {
$resto += abs($b);
}
return $resto;
}
@ -108,7 +113,7 @@ function long_to_bytes($n, $blocksize = 0)
$s = null;
$n = long($n);
while (($n > 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];

View File

@ -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]
}*/
}
}