1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-02 09:38:06 +01:00

Fix bad merge

This commit is contained in:
Daniil Gentili 2019-06-14 12:56:49 +02:00
parent 3097d2940b
commit 0aff25ea25
5 changed files with 241 additions and 238 deletions

View File

@ -15,12 +15,13 @@
namespace phpseclib\Crypt\Common;
use phpseclib\Crypt\DSA;
use phpseclib\Exception\UnsupportedFormatException;
use phpseclib\Exception\NoKeyLoadedException;
use phpseclib\Math\BigInteger;
use phpseclib\Crypt\Hash;
use phpseclib\Crypt\RSA;
use phpseclib\Exception\NoKeyLoadedException;
use phpseclib\Exception\UnsupportedFormatException;
use phpseclib\Math\BigInteger;
use phpseclib\Crypt\DSA;
use phpseclib\Crypt\ECDSA;
/**
* Base Class for all stream cipher classes
@ -139,7 +140,7 @@ abstract class AsymmetricKey
protected static function initialize_static_variables()
{
if (!isset(self::$zero)) {
self::$zero = new BigInteger(0);
self::$zero= new BigInteger(0);
self::$one = new BigInteger(1);
}
@ -199,7 +200,7 @@ abstract class AsymmetricKey
* @param string $method optional
* @return mixed
*/
protected static function validatePlugin($format, $type, $method = null)
protected static function validatePlugin($format, $type, $method = NULL)
{
$type = strtolower($type);
if (!isset(self::$plugins[static::ALGORITHM][$format][$type])) {
@ -223,12 +224,12 @@ abstract class AsymmetricKey
{
if (!isset(self::$plugins[static::ALGORITHM][$format])) {
self::$plugins[static::ALGORITHM][$format] = [];
foreach (new \DirectoryIterator(__DIR__.'/../'.static::ALGORITHM.'/'.$format.'/') as $file) {
foreach (new \DirectoryIterator(__DIR__ . '/../' . static::ALGORITHM . '/' . $format . '/') as $file) {
if ($file->getExtension() != 'php') {
continue;
}
$name = $file->getBasename('.php');
$type = 'phpseclib\Crypt\\'.static::ALGORITHM.'\\'.$format.'\\'.$name;
$type = 'phpseclib\Crypt\\' . static::ALGORITHM . '\\' . $format . '\\' . $name;
$reflect = new \ReflectionClass($type);
if ($reflect->isTrait()) {
continue;
@ -309,7 +310,7 @@ abstract class AsymmetricKey
// this test can be satisfied by either of the following:
// http://php.net/manual/en/book.sodium.php
// https://github.com/paragonie/sodium_compat
'libsodium' => function_exists('sodium_crypto_sign_keypair'),
'libsodium' => function_exists('sodium_crypto_sign_keypair')
];
return static::$engines;
@ -325,7 +326,7 @@ abstract class AsymmetricKey
static::$engines = [
'PHP' => true,
'OpenSSL' => false,
'libsodium' => false,
'libsodium' => false
];
}
@ -373,10 +374,10 @@ abstract class AsymmetricKey
$h1 = $this->bits2octets($h1);
$this->hmac->setKey($k);
$k = $this->hmac->hash($v."\0".$x.$h1);
$k = $this->hmac->hash($v . "\0" . $x . $h1);
$this->hmac->setKey($k);
$v = $this->hmac->hash($v);
$k = $this->hmac->hash($v."\1".$x.$h1);
$k = $this->hmac->hash($v . "\1" . $x . $h1);
$this->hmac->setKey($k);
$v = $this->hmac->hash($v);
@ -386,14 +387,14 @@ abstract class AsymmetricKey
$t = '';
while (strlen($t) < $qlen) {
$v = $this->hmac->hash($v);
$t = $t.$v;
$t = $t . $v;
}
$k = $this->bits2int($t);
if (!$k->equals(self::$zero) && $k->compare($this->q) < 0) {
break;
}
$k = $this->hmac->hash($v."\0");
$k = $this->hmac->hash($v . "\0");
$this->hmac->setKey($k);
$v = $this->hmac->hash($v);
}
@ -451,7 +452,7 @@ abstract class AsymmetricKey
$z1 = $this->bits2int($in);
$z2 = $z1->subtract($this->q);
return $z2->compare(self::$zero) < 0 ?
$this->int2octets($z1) :
$this->int2octets($z2);
$this->int2octets($z1) :
$this->int2octets($z2);
}
}

View File

@ -54,11 +54,12 @@
namespace phpseclib\Crypt;
use phpseclib\Common\Functions\Strings;
use phpseclib\Crypt\Common\BlockCipher;
use phpseclib\Exception\BadDecryptionException;
use phpseclib\Common\Functions\Strings;
use phpseclib\Exception\BadModeException;
use phpseclib\Exception\InsufficientSetupException;
use phpseclib\Exception\BadDecryptionException;
/**
* Pure-PHP implementation of Rijndael.
@ -211,7 +212,7 @@ class Rijndael extends BlockCipher
$this->key_length = $length >> 3;
break;
default:
throw new \LengthException('Key size of '.$length.' bits is not supported by this algorithm. Only keys of sizes 128, 160, 192, 224 or 256 bits are supported');
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes 128, 160, 192, 224 or 256 bits are supported');
}
parent::setKeyLength($length);
@ -237,7 +238,7 @@ class Rijndael extends BlockCipher
case 32:
break;
default:
throw new \LengthException('Key of size '.strlen($key).' not supported by this algorithm. Only keys of sizes 16, 20, 24, 28 or 32 are supported');
throw new \LengthException('Key of size ' . strlen($key) . ' not supported by this algorithm. Only keys of sizes 16, 20, 24, 28 or 32 are supported');
}
parent::setKey($key);
@ -261,7 +262,7 @@ class Rijndael extends BlockCipher
case 256:
break;
default:
throw new \LengthException('Key size of '.$length.' bits is not supported by this algorithm. Only keys of sizes 128, 160, 192, 224 or 256 bits are supported');
throw new \LengthException('Key size of ' . $length . ' bits is not supported by this algorithm. Only keys of sizes 128, 160, 192, 224 or 256 bits are supported');
}
$this->Nb = $length >> 5;
@ -285,29 +286,29 @@ class Rijndael extends BlockCipher
switch ($engine) {
case self::ENGINE_LIBSODIUM:
return function_exists('sodium_crypto_aead_aes256gcm_is_available') &&
sodium_crypto_aead_aes256gcm_is_available() &&
$this->mode == self::MODE_GCM &&
$this->key_length == 32 &&
$this->nonce && strlen($this->nonce) == 12 &&
$this->block_size == 16;
sodium_crypto_aead_aes256gcm_is_available() &&
$this->mode == self::MODE_GCM &&
$this->key_length == 32 &&
$this->nonce && strlen($this->nonce) == 12 &&
$this->block_size == 16;
case self::ENGINE_OPENSSL_GCM:
if (!extension_loaded('openssl')) {
return false;
}
$methods = openssl_get_cipher_methods();
return $this->mode == self::MODE_GCM &&
version_compare(PHP_VERSION, '7.1.0', '>=') &&
in_array('aes-'.$this->getKeyLength().'-gcm', $methods) &&
$this->block_size == 16;
version_compare(PHP_VERSION, '7.1.0', '>=') &&
in_array('aes-' . $this->getKeyLength() . '-gcm', $methods) &&
$this->block_size == 16;
case self::ENGINE_OPENSSL:
if ($this->block_size != 16) {
return false;
}
self::$cipher_name_openssl_ecb = 'aes-'.($this->key_length << 3).'-ecb';
$this->cipher_name_openssl = 'aes-'.($this->key_length << 3).'-'.$this->openssl_translate_mode();
self::$cipher_name_openssl_ecb = 'aes-' . ($this->key_length << 3) . '-ecb';
$this->cipher_name_openssl = 'aes-' . ($this->key_length << 3) . '-' . $this->openssl_translate_mode();
break;
case self::ENGINE_MCRYPT:
$this->cipher_name_mcrypt = 'rijndael-'.($this->block_size << 3);
$this->cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3);
if ($this->key_length % 8) { // is it a 160/224-bit key?
// mcrypt is not usable for them, only for 128/192/256-bit keys
return false;
@ -330,10 +331,10 @@ class Rijndael extends BlockCipher
if (empty($tables)) {
$tables = &$this->getTables();
}
$t0 = $tables[0];
$t1 = $tables[1];
$t2 = $tables[2];
$t3 = $tables[3];
$t0 = $tables[0];
$t1 = $tables[1];
$t2 = $tables[2];
$t3 = $tables[3];
$sbox = $tables[4];
$state = [];
@ -367,10 +368,10 @@ class Rijndael extends BlockCipher
while ($i < $Nb) {
$temp[$i] = $t0[$state[$i] >> 24 & 0x000000FF] ^
$t1[$state[$j] >> 16 & 0x000000FF] ^
$t2[$state[$k] >> 8 & 0x000000FF] ^
$t3[$state[$l] & 0x000000FF] ^
$w[++$wc];
$t1[$state[$j] >> 16 & 0x000000FF] ^
$t2[$state[$k] >> 8 & 0x000000FF] ^
$t3[$state[$l] & 0x000000FF] ^
$w[++$wc];
++$i;
$j = ($j + 1) % $Nb;
$k = ($k + 1) % $Nb;
@ -381,10 +382,10 @@ class Rijndael extends BlockCipher
// subWord
for ($i = 0; $i < $Nb; ++$i) {
$state[$i] = $sbox[$state[$i] & 0x000000FF] |
($sbox[$state[$i] >> 8 & 0x000000FF] << 8) |
($sbox[$state[$i] >> 16 & 0x000000FF] << 16) |
($sbox[$state[$i] >> 24 & 0x000000FF] << 24);
$state[$i] = $sbox[$state[$i] & 0x000000FF] |
($sbox[$state[$i] >> 8 & 0x000000FF] << 8) |
($sbox[$state[$i] >> 16 & 0x000000FF] << 16) |
($sbox[$state[$i] >> 24 & 0x000000FF] << 24);
}
// shiftRows + addRoundKey
@ -394,10 +395,10 @@ class Rijndael extends BlockCipher
$l = $c[3];
while ($i < $Nb) {
$temp[$i] = ($state[$i] & 0xFF000000) ^
($state[$j] & 0x00FF0000) ^
($state[$k] & 0x0000FF00) ^
($state[$l] & 0x000000FF) ^
$w[$i];
($state[$j] & 0x00FF0000) ^
($state[$k] & 0x0000FF00) ^
($state[$l] & 0x000000FF) ^
$w[$i];
++$i;
$j = ($j + 1) % $Nb;
$k = ($k + 1) % $Nb;
@ -420,16 +421,16 @@ class Rijndael extends BlockCipher
if (empty($invtables)) {
$invtables = &$this->getInvTables();
}
$dt0 = $invtables[0];
$dt1 = $invtables[1];
$dt2 = $invtables[2];
$dt3 = $invtables[3];
$dt0 = $invtables[0];
$dt1 = $invtables[1];
$dt2 = $invtables[2];
$dt3 = $invtables[3];
$isbox = $invtables[4];
$state = [];
$words = unpack('N*', $in);
$c = $this->c;
$c = $this->c;
$dw = $this->dw;
$Nb = $this->Nb;
$Nr = $this->Nr;
@ -449,10 +450,10 @@ class Rijndael extends BlockCipher
while ($i < $Nb) {
$temp[$i] = $dt0[$state[$i] >> 24 & 0x000000FF] ^
$dt1[$state[$j] >> 16 & 0x000000FF] ^
$dt2[$state[$k] >> 8 & 0x000000FF] ^
$dt3[$state[$l] & 0x000000FF] ^
$dw[++$wc];
$dt1[$state[$j] >> 16 & 0x000000FF] ^
$dt2[$state[$k] >> 8 & 0x000000FF] ^
$dt3[$state[$l] & 0x000000FF] ^
$dw[++$wc];
++$i;
$j = ($j + 1) % $Nb;
$k = ($k + 1) % $Nb;
@ -469,14 +470,14 @@ class Rijndael extends BlockCipher
while ($i < $Nb) {
$word = ($state[$i] & 0xFF000000) |
($state[$j] & 0x00FF0000) |
($state[$k] & 0x0000FF00) |
($state[$l] & 0x000000FF);
($state[$j] & 0x00FF0000) |
($state[$k] & 0x0000FF00) |
($state[$l] & 0x000000FF);
$temp[$i] = $dw[$i] ^ ($isbox[$word & 0x000000FF] |
($isbox[$word >> 8 & 0x000000FF] << 8) |
($isbox[$word >> 16 & 0x000000FF] << 16) |
($isbox[$word >> 24 & 0x000000FF] << 24));
$temp[$i] = $dw[$i] ^ ($isbox[$word & 0x000000FF] |
($isbox[$word >> 8 & 0x000000FF] << 8) |
($isbox[$word >> 16 & 0x000000FF] << 16) |
($isbox[$word >> 24 & 0x000000FF] << 24));
++$i;
$j = ($j + 1) % $Nb;
$k = ($k + 1) % $Nb;
@ -502,7 +503,7 @@ class Rijndael extends BlockCipher
0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000,
0x2F000000, 0x5E000000, 0xBC000000, 0x63000000, 0xC6000000,
0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000,
0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000,
0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000
];
if (isset($this->kl['key']) && $this->key === $this->kl['key'] && $this->key_length === $this->kl['key_length'] && $this->block_size === $this->kl['block_size']) {
@ -569,9 +570,9 @@ class Rijndael extends BlockCipher
while ($j < $this->Nb) {
$dw = $this->subWord($this->w[$row][$j]);
$temp[$j] = $dt0[$dw >> 24 & 0x000000FF] ^
$dt1[$dw >> 16 & 0x000000FF] ^
$dt2[$dw >> 8 & 0x000000FF] ^
$dt3[$dw & 0x000000FF];
$dt1[$dw >> 16 & 0x000000FF] ^
$dt2[$dw >> 8 & 0x000000FF] ^
$dt3[$dw & 0x000000FF];
$j++;
}
$this->dw[$row] = $temp;
@ -587,15 +588,15 @@ class Rijndael extends BlockCipher
// Converting to 1-dim key arrays (both ascending)
$this->dw = array_reverse($this->dw);
$w = array_pop($this->w);
$w = array_pop($this->w);
$dw = array_pop($this->dw);
foreach ($this->w as $r => $wr) {
foreach ($wr as $c => $wc) {
$w[] = $wc;
$w[] = $wc;
$dw[] = $this->dw[$r][$c];
}
}
$this->w = $w;
$this->w = $w;
$this->dw = $dw;
}
@ -613,10 +614,10 @@ class Rijndael extends BlockCipher
list(, , , , $sbox) = self::getTables();
}
return $sbox[$word & 0x000000FF] |
($sbox[$word >> 8 & 0x000000FF] << 8) |
($sbox[$word >> 16 & 0x000000FF] << 16) |
($sbox[$word >> 24 & 0x000000FF] << 24);
return $sbox[$word & 0x000000FF] |
($sbox[$word >> 8 & 0x000000FF] << 8) |
($sbox[$word >> 16 & 0x000000FF] << 16) |
($sbox[$word >> 24 & 0x000000FF] << 24);
}
/**
@ -669,13 +670,13 @@ class Rijndael extends BlockCipher
0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733,
0x9B9BB62D, 0x1E1E223C, 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5,
0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, 0x4242C684, 0x6868B8D0,
0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C,
0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C
]);
foreach ($t3 as $t3i) {
$t0[] = (($t3i << 24) & 0xFF000000) | (($t3i >> 8) & 0x00FFFFFF);
$t0[] = (($t3i << 24) & 0xFF000000) | (($t3i >> 8) & 0x00FFFFFF);
$t1[] = (($t3i << 16) & 0xFFFF0000) | (($t3i >> 16) & 0x0000FFFF);
$t2[] = (($t3i << 8) & 0xFFFFFF00) | (($t3i >> 24) & 0x000000FF);
$t2[] = (($t3i << 8) & 0xFFFFFF00) | (($t3i >> 24) & 0x000000FF);
}
$tables = [
@ -701,8 +702,8 @@ class Rijndael extends BlockCipher
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16,
],
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
]
];
}
return $tables;
@ -753,13 +754,13 @@ class Rijndael extends BlockCipher
0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A,
0xD2DF599C, 0xF2733F55, 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678,
0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, 0x3C498B28, 0x0D9541FF,
0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0,
0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0
]);
foreach ($dt3 as $dt3i) {
$dt0[] = (($dt3i << 24) & 0xFF000000) | (($dt3i >> 8) & 0x00FFFFFF);
$dt0[] = (($dt3i << 24) & 0xFF000000) | (($dt3i >> 8) & 0x00FFFFFF);
$dt1[] = (($dt3i << 16) & 0xFFFF0000) | (($dt3i >> 16) & 0x0000FFFF);
$dt2[] = (($dt3i << 8) & 0xFFFFFF00) | (($dt3i >> 24) & 0x000000FF);
$dt2[] = (($dt3i << 8) & 0xFFFFFF00) | (($dt3i >> 24) & 0x000000FF);
};
$tables = [
@ -785,8 +786,8 @@ class Rijndael extends BlockCipher
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D,
],
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
]
];
}
return $tables;
@ -800,17 +801,17 @@ class Rijndael extends BlockCipher
*/
protected function setupInlineCrypt()
{
$w = $this->w;
$w = $this->w;
$dw = $this->dw;
$init_encrypt = '';
$init_decrypt = '';
$Nr = $this->Nr;
$Nb = $this->Nb;
$c = $this->c;
$c = $this->c;
// Generating encrypt code:
$init_encrypt .= '
$init_encrypt.= '
static $tables;
if (empty($tables)) {
$tables = &$this->getTables();
@ -822,8 +823,8 @@ class Rijndael extends BlockCipher
$sbox = $tables[4];
';
$s = 'e';
$e = 's';
$s = 'e';
$e = 's';
$wc = $Nb - 1;
// Preround: addRoundKey
@ -836,9 +837,9 @@ class Rijndael extends BlockCipher
for ($round = 1; $round < $Nr; ++$round) {
list($s, $e) = [$e, $s];
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .=
$encrypt_block.=
'$'.$e.$i.' =
$t0[($'.$s.$i.' >> 24) & 0xff] ^
$t0[($'.$s.$i .' >> 24) & 0xff] ^
$t1[($'.$s.(($i + $c[1]) % $Nb).' >> 16) & 0xff] ^
$t2[($'.$s.(($i + $c[2]) % $Nb).' >> 8) & 0xff] ^
$t3[ $'.$s.(($i + $c[3]) % $Nb).' & 0xff] ^
@ -848,7 +849,7 @@ class Rijndael extends BlockCipher
// Finalround: subWord + shiftRows + addRoundKey
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .=
$encrypt_block.=
'$'.$e.$i.' =
$sbox[ $'.$e.$i.' & 0xff] |
($sbox[($'.$e.$i.' >> 8) & 0xff] << 8) |
@ -857,8 +858,8 @@ class Rijndael extends BlockCipher
}
$encrypt_block .= '$in = pack("N*"'."\n";
for ($i = 0; $i < $Nb; ++$i) {
$encrypt_block .= ',
($'.$e.$i.' & '.((int) 0xFF000000).') ^
$encrypt_block.= ',
($'.$e.$i .' & '.((int)0xFF000000).') ^
($'.$e.(($i + $c[1]) % $Nb).' & 0x00FF0000 ) ^
($'.$e.(($i + $c[2]) % $Nb).' & 0x0000FF00 ) ^
($'.$e.(($i + $c[3]) % $Nb).' & 0x000000FF ) ^
@ -867,7 +868,7 @@ class Rijndael extends BlockCipher
$encrypt_block .= ');';
// Generating decrypt code:
$init_decrypt .= '
$init_decrypt.= '
static $invtables;
if (empty($invtables)) {
$invtables = &$this->getInvTables();
@ -879,8 +880,8 @@ class Rijndael extends BlockCipher
$isbox = $invtables[4];
';
$s = 'e';
$e = 's';
$s = 'e';
$e = 's';
$wc = $Nb - 1;
// Preround: addRoundKey
@ -893,9 +894,9 @@ class Rijndael extends BlockCipher
for ($round = 1; $round < $Nr; ++$round) {
list($s, $e) = [$e, $s];
for ($i = 0; $i < $Nb; ++$i) {
$decrypt_block .=
$decrypt_block.=
'$'.$e.$i.' =
$dt0[($'.$s.$i.' >> 24) & 0xff] ^
$dt0[($'.$s.$i .' >> 24) & 0xff] ^
$dt1[($'.$s.(($Nb + $i - $c[1]) % $Nb).' >> 16) & 0xff] ^
$dt2[($'.$s.(($Nb + $i - $c[2]) % $Nb).' >> 8) & 0xff] ^
$dt3[ $'.$s.(($Nb + $i - $c[3]) % $Nb).' & 0xff] ^
@ -905,7 +906,7 @@ class Rijndael extends BlockCipher
// Finalround: subWord + shiftRows + addRoundKey
for ($i = 0; $i < $Nb; ++$i) {
$decrypt_block .=
$decrypt_block.=
'$'.$e.$i.' =
$isbox[ $'.$e.$i.' & 0xff] |
($isbox[($'.$e.$i.' >> 8) & 0xff] << 8) |
@ -914,8 +915,8 @@ class Rijndael extends BlockCipher
}
$decrypt_block .= '$in = pack("N*"'."\n";
for ($i = 0; $i < $Nb; ++$i) {
$decrypt_block .= ',
($'.$e.$i.' & '.((int) 0xFF000000).') ^
$decrypt_block.= ',
($'.$e.$i. ' & '.((int)0xFF000000).') ^
($'.$e.(($Nb + $i - $c[1]) % $Nb).' & 0x00FF0000 ) ^
($'.$e.(($Nb + $i - $c[2]) % $Nb).' & 0x0000FF00 ) ^
($'.$e.(($Nb + $i - $c[3]) % $Nb).' & 0x000000FF ) ^
@ -925,11 +926,11 @@ class Rijndael extends BlockCipher
$this->inline_crypt = $this->createInlineCryptFunction(
[
'init_crypt' => '',
'init_encrypt' => $init_encrypt,
'init_decrypt' => $init_decrypt,
'encrypt_block' => $encrypt_block,
'decrypt_block' => $decrypt_block,
'init_crypt' => '',
'init_encrypt' => $init_encrypt,
'init_decrypt' => $init_decrypt,
'encrypt_block' => $encrypt_block,
'decrypt_block' => $decrypt_block
]
);
}
@ -954,7 +955,7 @@ class Rijndael extends BlockCipher
case self::ENGINE_OPENSSL_GCM:
return openssl_encrypt(
$plaintext,
'aes-'.$this->getKeyLength().'-gcm',
'aes-' . $this->getKeyLength() . '-gcm',
$this->key,
OPENSSL_RAW_DATA,
$this->nonce,
@ -987,7 +988,7 @@ class Rijndael extends BlockCipher
if (strlen($this->oldtag) != 16) {
break;
}
$plaintext = sodium_crypto_aead_aes256gcm_decrypt($ciphertext.$this->oldtag, $this->aad, $this->nonce, $this->key);
$plaintext = sodium_crypto_aead_aes256gcm_decrypt($ciphertext . $this->oldtag, $this->aad, $this->nonce, $this->key);
if ($plaintext === false) {
$this->oldtag = false;
throw new BadDecryptionException('Error decrypting ciphertext with libsodium');
@ -999,7 +1000,7 @@ class Rijndael extends BlockCipher
}
$plaintext = openssl_decrypt(
$ciphertext,
'aes-'.$this->getKeyLength().'-gcm',
'aes-' . $this->getKeyLength() . '-gcm',
$this->key,
OPENSSL_RAW_DATA,
$this->nonce,

View File

@ -81,7 +81,7 @@ class BigInteger implements \Serializable
{
self::$engines = [];
$fqmain = 'phpseclib\\Math\\BigInteger\\Engines\\'.$main;
$fqmain = 'phpseclib\\Math\\BigInteger\\Engines\\' . $main;
if (!class_exists($fqmain) || !method_exists($fqmain, 'isValidEngine')) {
throw new \InvalidArgumentException("$main is not a valid engine");
}
@ -135,7 +135,7 @@ class BigInteger implements \Serializable
['GMP'],
['PHP64', ['OpenSSL']],
['BCMath', ['OpenSSL']],
['PHP32', ['OpenSSL']],
['PHP32', ['OpenSSL']]
];
foreach ($engines as $engine) {
try {
@ -230,7 +230,7 @@ class BigInteger implements \Serializable
* @param bool $twos_compliment
* @return string
*/
public function toBits($twos_compliment = false)
function toBits($twos_compliment = false)
{
return $this->value->toBits($twos_compliment);
}
@ -252,7 +252,7 @@ class BigInteger implements \Serializable
* @param BigInteger $y
* @return BigInteger
*/
public function subtract(BigInteger $y)
function subtract(BigInteger $y)
{
return new static($this->value->subtract($y->value));
}
@ -298,7 +298,7 @@ class BigInteger implements \Serializable
list($q, $r) = $this->value->divide($y->value);
return [
new static($q),
new static($r),
new static($r)
];
}
@ -332,7 +332,7 @@ class BigInteger implements \Serializable
return [
'gcd' => new static($gcd),
'x' => new static($x),
'y' => new static($y),
'y' => new static($y)
];
}
@ -357,7 +357,7 @@ class BigInteger implements \Serializable
*/
public function abs()
{
return new static($this->value->abs());
return new static($this->value->abs());
}
/**
@ -598,7 +598,7 @@ class BigInteger implements \Serializable
*/
return [
'min' => new static($min),
'max' => new static($max),
'max' => new static($max)
];
}
@ -733,10 +733,10 @@ class BigInteger implements \Serializable
* @param BigInteger[] $nums
* @return BigInteger
*/
public static function min(BigInteger...$nums)
public static function min(BigInteger ...$nums)
{
$class = self::$mainEngine;
$nums = array_map(function ($num) {return $num->value;}, $nums);
$nums = array_map(function($num) { return $num->value; }, $nums);
return new static($class::min(...$nums));
}
@ -746,10 +746,10 @@ class BigInteger implements \Serializable
* @param BigInteger[] $nums
* @return BigInteger
*/
public static function max(BigInteger...$nums)
public static function max(BigInteger ...$nums)
{
$class = self::$mainEngine;
$nums = array_map(function ($num) {return $num->value;}, $nums);
$nums = array_map(function($num) { return $num->value; }, $nums);
return new static($class::max(...$nums));
}
@ -841,7 +841,7 @@ class BigInteger implements \Serializable
public function createRecurringModuloFunction()
{
$func = $this->value->createRecurringModuloFunction();
return function (BigInteger $x) use ($func) {
return function(BigInteger $x) use ($func) {
return new static($func($x->value));
};
}
@ -856,8 +856,8 @@ class BigInteger implements \Serializable
*/
public function bitwise_split($split)
{
return array_map(function ($val) {
return array_map(function($val) {
return new static($val);
}, $this->value->bitwise_split($split));
}
}
}

View File

@ -16,10 +16,10 @@
namespace phpseclib\Math\BigInteger\Engines;
use ParagonIE\ConstantTime\Hex;
use phpseclib\Common\Functions\Strings;
use phpseclib\Crypt\Random;
use phpseclib\Exception\BadConfigurationException;
use phpseclib\Crypt\Random;
use phpseclib\Math\BigInteger;
use phpseclib\Common\Functions\Strings;
/**
* Base Engine.
@ -75,17 +75,17 @@ abstract class Engine implements \Serializable
{
if (!isset(static::$primes)) {
static::$primes = [
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617,
619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997,
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617,
619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997
];
static::$zero = new static(0);
static::$one = new static(1);
@ -100,7 +100,7 @@ abstract class Engine implements \Serializable
switch ($base) {
case -256:
case 256:
case 256:
if ($base == -256 && (ord($x[0]) & 0x80)) {
$this->value = ~$x;
$this->is_negative = true;
@ -117,7 +117,7 @@ abstract class Engine implements \Serializable
}
break;
case -16:
case 16:
case 16:
if ($base > 0 && $x[0] == '-') {
$this->is_negative = true;
$x = substr($x, 1);
@ -140,7 +140,7 @@ abstract class Engine implements \Serializable
}
break;
case -10:
case 10:
case 10:
// (?<!^)(?:-).*: find any -'s that aren't at the beginning and then any characters that follow that
// (?<=^|-)0*: find any 0's that are preceded by the start of the string or by a - (ie. octals)
// [^-0-9].*: find any non-numeric characters and then any characters that follow that
@ -151,7 +151,7 @@ abstract class Engine implements \Serializable
static::initialize($base);
break;
case -2:
case 2:
case 2:
if ($base > 0 && $x[0] == '-') {
$this->is_negative = true;
$x = substr($x, 1);
@ -180,7 +180,7 @@ abstract class Engine implements \Serializable
*/
public static function setModExpEngine($engine)
{
$fqengine = '\\phpseclib\\Math\\BigInteger\\Engines\\'.static::ENGINE_DIR.'\\'.$engine;
$fqengine = '\\phpseclib\\Math\\BigInteger\\Engines\\' . static::ENGINE_DIR . '\\' . $engine;
if (!class_exists($fqengine) || !method_exists($fqengine, 'isValidEngine')) {
throw new \InvalidArgumentException("$engine is not a valid engine");
}
@ -212,7 +212,7 @@ abstract class Engine implements \Serializable
}
if (ord($bytes[0]) & 0x80) {
$bytes = chr(0).$bytes;
$bytes = chr(0) . $bytes;
}
return $comparison < 0 ? ~$bytes : $bytes;
@ -246,7 +246,7 @@ abstract class Engine implements \Serializable
$result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0');
if ($twos_compliment && $this->compare(new static()) > 0 && $this->precision <= 0) {
return '0'.$result;
return '0' . $result;
}
return $result;
@ -340,8 +340,8 @@ abstract class Engine implements \Serializable
public function __debugInfo()
{
return [
'value' => '0x'.$this->toHex(true),
'engine' => basename(static::class),
'value' => '0x' . $this->toHex(true),
'engine' => basename(static::class)
];
}
@ -388,7 +388,7 @@ abstract class Engine implements \Serializable
*/
protected static function setBitmask($bits)
{
return new static(chr((1 << ($bits & 0x7)) - 1).str_repeat(chr(0xFF), $bits >> 3), 256);
return new static(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256);
}
/**
@ -420,7 +420,7 @@ abstract class Engine implements \Serializable
}
// generate as many leading 1's as we need to.
$leading_ones = chr((1 << ($new_bits & 0x7)) - 1).str_repeat(chr(0xFF), $new_bits >> 3);
$leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3);
self::base256_lshift($leading_ones, $current_bits);
@ -454,7 +454,7 @@ abstract class Engine implements \Serializable
$carry = $temp >> 8;
}
$carry = ($carry != 0) ? chr($carry) : '';
$x = $carry.$x.str_repeat(chr(0), $num_bytes);
$x = $carry . $x . str_repeat(chr(0), $num_bytes);
}
/**
@ -482,13 +482,13 @@ abstract class Engine implements \Serializable
for ($i = 0; $temp >> $i; ++$i) {
}
$precision = 8 * strlen($bits) - 8 + $i;
$mask = chr((1 << ($precision & 0x7)) - 1).str_repeat(chr(0xFF), $precision >> 3);
$mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3);
}
if ($shift < 0) {
$shift += $precision;
$shift+= $precision;
}
$shift %= $precision;
$shift%= $precision;
if (!$shift) {
return clone $this;
@ -527,14 +527,14 @@ abstract class Engine implements \Serializable
$max = str_repeat(chr(0xFF), $bytes);
$msb = $bits & 7;
if ($msb) {
$min = chr(1 << ($msb - 1)).$min;
$max = chr((1 << $msb) - 1).$max;
$min = chr(1 << ($msb - 1)) . $min;
$max = chr((1 << $msb) - 1) . $max;
} else {
$min[0] = chr(0x80);
}
return [
'min' => new static($min, 256),
'max' => new static($max, 256),
'max' => new static($max, 256)
];
}
@ -756,21 +756,21 @@ abstract class Engine implements \Serializable
$size = strlen(ltrim($max->toBytes(), chr(0)));
/*
doing $random % $max doesn't work because some numbers will be more likely to occur than others.
eg. if $max is 140 and $random's max is 255 then that'd mean both $random = 5 and $random = 145
would produce 5 whereas the only value of random that could produce 139 would be 139. ie.
not all numbers would be equally likely. some would be more likely than others.
doing $random % $max doesn't work because some numbers will be more likely to occur than others.
eg. if $max is 140 and $random's max is 255 then that'd mean both $random = 5 and $random = 145
would produce 5 whereas the only value of random that could produce 139 would be 139. ie.
not all numbers would be equally likely. some would be more likely than others.
creating a whole new random number until you find one that is within the range doesn't work
because, for sufficiently small ranges, the likelihood that you'd get a number within that range
would be pretty small. eg. with $random's max being 255 and if your $max being 1 the probability
would be pretty high that $random would be greater than $max.
creating a whole new random number until you find one that is within the range doesn't work
because, for sufficiently small ranges, the likelihood that you'd get a number within that range
would be pretty small. eg. with $random's max being 255 and if your $max being 1 the probability
would be pretty high that $random would be greater than $max.
phpseclib works around this using the technique described here:
phpseclib works around this using the technique described here:
http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string
*/
$random_max = new static(chr(1).str_repeat("\0", $size), 256);
http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string
*/
$random_max = new static(chr(1) . str_repeat("\0", $size), 256);
$random = new static(Random::string($size), 256);
list($max_multiple) = $random_max->divide($max);
@ -848,18 +848,18 @@ abstract class Engine implements \Serializable
// see HAC 4.49 "Note (controlling the error probability)"
// @codingStandardsIgnoreStart
if ($length >= 163) {$t = 2;} // floor(1300 / 8)
else if ($length >= 106) {$t = 3;} // floor( 850 / 8)
else if ($length >= 81) {$t = 4;} // floor( 650 / 8)
else if ($length >= 68) {$t = 5;} // floor( 550 / 8)
else if ($length >= 56) {$t = 6;} // floor( 450 / 8)
else if ($length >= 50) {$t = 7;} // floor( 400 / 8)
else if ($length >= 43) {$t = 8;} // floor( 350 / 8)
else if ($length >= 37) {$t = 9;} // floor( 300 / 8)
else if ($length >= 31) {$t = 12;} // floor( 250 / 8)
else if ($length >= 25) {$t = 15;} // floor( 200 / 8)
else if ($length >= 18) {$t = 18;} // floor( 150 / 8)
else { $t = 27;}
if ($length >= 163) { $t = 2; } // floor(1300 / 8)
else if ($length >= 106) { $t = 3; } // floor( 850 / 8)
else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8)
else if ($length >= 68 ) { $t = 5; } // floor( 550 / 8)
else if ($length >= 56 ) { $t = 6; } // floor( 450 / 8)
else if ($length >= 50 ) { $t = 7; } // floor( 400 / 8)
else if ($length >= 43 ) { $t = 8; } // floor( 350 / 8)
else if ($length >= 37 ) { $t = 9; } // floor( 300 / 8)
else if ($length >= 31 ) { $t = 12; } // floor( 250 / 8)
else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8)
else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8)
else { $t = 27; }
// @codingStandardsIgnoreEnd
return $t;
@ -880,7 +880,7 @@ abstract class Engine implements \Serializable
return false;
}
$n = clone $this;
$n = clone $this;
$n_1 = $n->subtract(static::$one);
$n_2 = $n->subtract(static::$two);
@ -1068,11 +1068,11 @@ abstract class Engine implements \Serializable
$class = static::class;
$fqengine = !method_exists(static::$modexpEngine, 'reduce') ?
'\\phpseclib\\Math\\BigInteger\\Engines\\'.static::ENGINE_DIR.'\\DefaultEngine' :
static::$modexpEngine;
'\\phpseclib\\Math\\BigInteger\\Engines\\' . static::ENGINE_DIR . '\\DefaultEngine' :
static::$modexpEngine;
if (method_exists($fqengine, 'generateCustomReduction')) {
$func = $fqengine::generateCustomReduction($this, static::class);
$this->reduce = eval('return function('.static::class.' $x) use ($func, $class) {
$this->reduce = eval('return function(' . static::class . ' $x) use ($func, $class) {
$r = new $class();
$r->value = $func($x->value);
return $r;
@ -1080,7 +1080,7 @@ abstract class Engine implements \Serializable
return clone $this->reduce;
}
$n = $this->value;
$this->reduce = eval('return function('.static::class.' $x) use ($n, $fqengine, $class) {
$this->reduce = eval('return function(' . static::class . ' $x) use ($n, $fqengine, $class) {
$r = new $class();
$r->value = $fqengine::reduce($x->value, $n, $class);
return $r;
@ -1124,9 +1124,9 @@ abstract class Engine implements \Serializable
}
return [
'gcd' => $u,
'gcd'=> $u,
'x' => $a,
'y' => $b,
'y' => $b
];
}
@ -1208,6 +1208,7 @@ abstract class Engine implements \Serializable
$length = max(strlen($left), strlen($right));
$left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
$right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
return $this->normalize(new static($left ^ $right, -256));

View File

@ -34,7 +34,7 @@ abstract class PHP extends Engine
* multiply() or whatever, we'll just work directly on arrays, taking them in as parameters and returning them.
*
* @access protected
*/
*/
/**
* $result[self::VALUE] contains the value.
*/
@ -85,7 +85,7 @@ abstract class PHP extends Engine
static::$isValidEngine = static::isValidEngine();
}
if (!static::$isValidEngine) {
throw new BadConfigurationException(static::class.' is not setup correctly on this system');
throw new BadConfigurationException(static::class . ' is not setup correctly on this system');
}
$this->value = [];
@ -102,7 +102,7 @@ abstract class PHP extends Engine
{
switch (abs($base)) {
case 16:
$x = (strlen($this->value) & 1) ? '0'.$this->value : $this->value;
$x = (strlen($this->value) & 1) ? '0' . $this->value : $this->value;
$temp = new static(Hex::decode($x), 256);
$this->value = $temp->value;
break;
@ -164,7 +164,7 @@ abstract class PHP extends Engine
$result = '';
while (count($temp->value)) {
list($temp, $mod) = $temp->divide($divisor);
$result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', static::MAX10LEN, '0', STR_PAD_LEFT).$result;
$result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', static::MAX10LEN, '0', STR_PAD_LEFT) . $result;
}
$result = ltrim($result, '0');
if (empty($result)) {
@ -172,7 +172,7 @@ abstract class PHP extends Engine
}
if ($this->is_negative) {
$result = '-'.$result;
$result = '-' . $result;
}
return $result;
@ -198,8 +198,8 @@ abstract class PHP extends Engine
$result = implode('', array_map('chr', $result));
return $this->precision > 0 ?
str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) :
$result;
str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) :
$result;
}
/**
@ -219,12 +219,12 @@ abstract class PHP extends Engine
if ($x_size == 0) {
return [
self::VALUE => $y_value,
self::SIGN => $y_negative,
self::SIGN => $y_negative
];
} elseif ($y_size == 0) {
return [
self::VALUE => $x_value,
self::SIGN => $x_negative,
self::SIGN => $x_negative
];
}
@ -233,13 +233,13 @@ abstract class PHP extends Engine
if ($x_value == $y_value) {
return [
self::VALUE => [],
self::SIGN => false,
self::SIGN => false
];
}
$temp = self::subtractHelper($x_value, false, $y_value, false);
$temp[self::SIGN] = self::compareHelper($x_value, false, $y_value, false) > 0 ?
$x_negative : $y_negative;
$x_negative : $y_negative;
return $temp;
}
@ -255,22 +255,22 @@ abstract class PHP extends Engine
$value[count($value)] = 0; // just in case the carry adds an extra digit
$carry = 0;
for ($i = 0, $j = 1; $j < $size; $i += 2, $j += 2) {
for ($i = 0, $j = 1; $j < $size; $i+=2, $j+=2) {
//$sum = $x_value[$j] * static::BASE_FULL + $x_value[$i] + $y_value[$j] * static::BASE_FULL + $y_value[$i] + $carry;
$sum = ($x_value[$j] + $y_value[$j]) * static::BASE_FULL + $x_value[$i] + $y_value[$i] + $carry;
$carry = $sum >= static::MAX_DIGIT2; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1
$sum = $carry ? $sum-static::MAX_DIGIT2 : $sum;
$sum = $carry ? $sum - static::MAX_DIGIT2 : $sum;
$temp = static::BASE === 26 ? intval($sum / 0x4000000) : ($sum >> 31);
$value[$i] = (int) ($sum-static::BASE_FULL * $temp); // eg. a faster alternative to fmod($sum, 0x4000000)
$value[$i] = (int) ($sum - static::BASE_FULL * $temp); // eg. a faster alternative to fmod($sum, 0x4000000)
$value[$j] = $temp;
}
if ($j == $size) { // ie. if $y_size is odd
$sum = $x_value[$i] + $y_value[$i] + $carry;
$carry = $sum >= static::BASE_FULL;
$value[$i] = $carry ? $sum-static::BASE_FULL : $sum;
$value[$i] = $carry ? $sum - static::BASE_FULL : $sum;
++$i; // ie. let $i = $j since we've just done $value[$i]
}
@ -283,7 +283,7 @@ abstract class PHP extends Engine
return [
self::VALUE => self::trim($value),
self::SIGN => $x_negative,
self::SIGN => $x_negative
];
}
@ -296,7 +296,7 @@ abstract class PHP extends Engine
* @param bool $y_negative
* @return array
*/
public static function subtractHelper(array $x_value, $x_negative, array $y_value, $y_negative)
static function subtractHelper(array $x_value, $x_negative, array $y_value, $y_negative)
{
$x_size = count($x_value);
$y_size = count($y_value);
@ -304,12 +304,12 @@ abstract class PHP extends Engine
if ($x_size == 0) {
return [
self::VALUE => $y_value,
self::SIGN => !$y_negative,
self::SIGN => !$y_negative
];
} elseif ($y_size == 0) {
return [
self::VALUE => $x_value,
self::SIGN => $x_negative,
self::SIGN => $x_negative
];
}
@ -326,7 +326,7 @@ abstract class PHP extends Engine
if (!$diff) {
return [
self::VALUE => [],
self::SIGN => false,
self::SIGN => false
];
}
@ -345,27 +345,27 @@ abstract class PHP extends Engine
// at this point, $x_value should be at least as big as - if not bigger than - $y_value
$carry = 0;
for ($i = 0, $j = 1; $j < $y_size; $i += 2, $j += 2) {
for ($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2) {
$sum = ($x_value[$j] - $y_value[$j]) * static::BASE_FULL + $x_value[$i] - $y_value[$i] - $carry;
$carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1
$sum = $carry ? $sum+static::MAX_DIGIT2 : $sum;
$sum = $carry ? $sum + static::MAX_DIGIT2 : $sum;
$temp = static::BASE === 26 ? intval($sum / 0x4000000) : ($sum >> 31);
$x_value[$i] = (int) ($sum-static::BASE_FULL * $temp);
$x_value[$i] = (int) ($sum - static::BASE_FULL * $temp);
$x_value[$j] = $temp;
}
if ($j == $y_size) { // ie. if $y_size is odd
$sum = $x_value[$i] - $y_value[$i] - $carry;
$carry = $sum < 0;
$x_value[$i] = $carry ? $sum+static::BASE_FULL : $sum;
$x_value[$i] = $carry ? $sum + static::BASE_FULL : $sum;
++$i;
}
if ($carry) {
for (;!$x_value[$i]; ++$i) {
for (; !$x_value[$i]; ++$i) {
$x_value[$i] = static::MAX_DIGIT;
}
--$x_value[$i];
@ -373,7 +373,7 @@ abstract class PHP extends Engine
return [
self::VALUE => self::trim($x_value),
self::SIGN => $x_negative,
self::SIGN => $x_negative
];
}
@ -401,15 +401,15 @@ abstract class PHP extends Engine
if (!$x_length || !$y_length) { // a 0 is being multiplied
return [
self::VALUE => [],
self::SIGN => false,
self::SIGN => false
];
}
return [
self::VALUE => min($x_length, $y_length) < 2 * self::KARATSUBA_CUTOFF ?
self::trim(self::regularMultiply($x_value, $y_value)) :
self::trim(self::karatsuba($x_value, $y_value)),
self::SIGN => $x_negative != $y_negative,
self::trim(self::regularMultiply($x_value, $y_value)) :
self::trim(self::karatsuba($x_value, $y_value)),
self::SIGN => $x_negative != $y_negative
];
}
@ -484,7 +484,7 @@ abstract class PHP extends Engine
for ($j = 0; $j < $x_length; ++$j) { // ie. $i = 0
$temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
$carry = static::BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
$product_value[$j] = (int) ($temp-static::BASE_FULL * $carry);
$product_value[$j] = (int) ($temp - static::BASE_FULL * $carry);
}
$product_value[$j] = $carry;
@ -497,7 +497,7 @@ abstract class PHP extends Engine
for ($j = 0, $k = $i; $j < $x_length; ++$j, ++$k) {
$temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
$carry = static::BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
$product_value[$k] = (int) ($temp-static::BASE_FULL * $carry);
$product_value[$k] = (int) ($temp - static::BASE_FULL * $carry);
}
$product_value[$k] = $carry;
@ -557,7 +557,7 @@ abstract class PHP extends Engine
// normalize $x and $y as described in HAC 14.23 / 14.24
$msb = $y->value[count($y->value) - 1];
for ($shift = 0;!($msb & static::MSB); ++$shift) {
for ($shift = 0; !($msb & static::MSB); ++$shift) {
$msb <<= 1;
}
$x->lshift($shift);
@ -574,11 +574,11 @@ abstract class PHP extends Engine
static $temp, $lhs, $rhs;
if (!isset($temp)) {
$temp = new static();
$lhs = new static();
$rhs = new static();
$lhs = new static();
$rhs = new static();
}
$temp_value = &$temp->value;
$rhs_value = &$rhs->value;
$rhs_value = &$rhs->value;
// $temp = $y << ($x_max - $y_max-1) in base 2**26
$temp_value = array_merge(self::array_repeat(0, $x_max - $y_max), $y_value);
@ -595,11 +595,11 @@ abstract class PHP extends Engine
$x_window = [
isset($x_value[$i]) ? $x_value[$i] : 0,
isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0,
isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0,
isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0
];
$y_window = [
$y_value[$y_max],
($y_max > 0) ? $y_value[$y_max - 1] : 0,
($y_max > 0) ? $y_value[$y_max - 1] : 0
];
$q_index = $i - $y_max - 1;
@ -879,7 +879,7 @@ abstract class PHP extends Engine
* @return array
*/
protected static function array_repeat($input, $multiplier)
{
{
return $multiplier ? array_fill(0, $multiplier, $input) : [];
}
@ -976,8 +976,8 @@ abstract class PHP extends Engine
protected static function square(array $x)
{
return count($x) < 2 * self::KARATSUBA_CUTOFF ?
self::trim(self::baseSquare($x)) :
self::trim(self::karatsubaSquare($x));
self::trim(self::baseSquare($x)) :
self::trim(self::karatsubaSquare($x));
}
/**
@ -1002,13 +1002,13 @@ abstract class PHP extends Engine
$temp = $square_value[$i2] + $value[$i] * $value[$i];
$carry = static::BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
$square_value[$i2] = (int) ($temp-static::BASE_FULL * $carry);
$square_value[$i2] = (int) ($temp - static::BASE_FULL * $carry);
// note how we start from $i+1 instead of 0 as we do in multiplication.
for ($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k) {
$temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry;
$carry = static::BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
$square_value[$k] = (int) ($temp-static::BASE_FULL * $carry);
$square_value[$k] = (int) ($temp - static::BASE_FULL * $carry);
}
// the following line can yield values larger 2**15. at this point, PHP should switch
@ -1227,7 +1227,7 @@ abstract class PHP extends Engine
$digit = [];
if (!$overflow) {
$digit = array_slice($val, $i, $width);
$i += $width;
$i+= $width;
$overflow = $split % static::BASE;
if ($overflow) {
$mask = (1 << $overflow) - 1;
@ -1239,7 +1239,7 @@ abstract class PHP extends Engine
$tempsplit = $split - $remaining;
$tempwidth = (int) ($tempsplit / static::BASE + 1);
$digit = array_slice($val, $i, $tempwidth);
$i += $tempwidth;
$i+= $tempwidth;
$tempoverflow = $tempsplit % static::BASE;
if ($tempoverflow) {
$tempmask = (1 << $tempoverflow) - 1;
@ -1282,9 +1282,9 @@ abstract class PHP extends Engine
$remaining = static::BASE;
while ($i != $len) {
$digit = $val[$i] & $mask;
$val[$i] >>= $split;
$val[$i]>>= $split;
if (!$overflow) {
$remaining -= $split;
$remaining-= $split;
$overflow = $split <= $remaining ? 0 : $split - $remaining;
if (!$remaining) {
@ -1294,8 +1294,8 @@ abstract class PHP extends Engine
}
} else if (++$i != $len) {
$tempmask = (1 << $overflow) - 1;
$digit |= ($val[$i] & $tempmask) << $remaining;
$val[$i] >>= $overflow;
$digit|= ($val[$i] & $tempmask) << $remaining;
$val[$i]>>= $overflow;
$remaining = static::BASE - $overflow;
$overflow = $split <= $remaining ? 0 : $split - $remaining;
}
@ -1309,4 +1309,4 @@ abstract class PHP extends Engine
return array_reverse($vals);
}
}
}