Esempio n. 1
0
/**
 * Calculate Wang hash for 64bit unsigned integer using GMP library
 * PHP only supports signed integers even with 64bit version
 *
 * See <code/nel/include/nel/misc/wang_hash.h> on https://bitbucket.org/ryzom/ryzomcore
 *
 * @param string $key
 *
 * @return string hash
 */
function wang_hash64($key)
{
    // force $key to be base 10
    $key = gmp_init($key, 10);
    //$key = (~$key) + ($key << 21);
    $key = gmp_add(gmp_com($key), gmp_mul($key, 1 << 21));
    //$key = $key ^ ($key >> 24);
    $key = gmp_xor($key, gmp_div($key, 1 << 24));
    //$key = $key * 265;
    $key = gmp_mul($key, 265);
    //$key = $key ^ ($key >> 14);
    $key = gmp_xor($key, gmp_div($key, 1 << 14));
    //$key = $key * 21;
    $key = gmp_mul($key, 21);
    //$key = $key ^ ($key >> 28);
    $key = gmp_xor($key, gmp_div($key, 1 << 28));
    //$key = $key + ($key << 31);
    $key = gmp_add($key, gmp_mul($key, gmp_pow(2, 31)));
    // limit to 64bit
    $key = gmp_and($key, "0xFFFFFFFFFFFFFFFF");
    return gmp_strval($key, 10);
}
Esempio n. 2
0
 /**
  * Logical Exclusive-Or
  *
  * @param Math_BigInteger $x
  * @access public
  * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  * @return Math_BigInteger
  */
 function bitwise_xor($x)
 {
     switch (MATH_BIGINTEGER_MODE) {
         case MATH_BIGINTEGER_MODE_GMP:
             $temp = new Math_BigInteger();
             $temp->value = gmp_xor($this->value, $x->value);
             return $this->_normalize($temp);
         case MATH_BIGINTEGER_MODE_BCMATH:
             $left = $this->toBytes();
             $right = $x->toBytes();
             $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 Math_BigInteger($left ^ $right, 256));
     }
     $length = max(count($this->value), count($x->value));
     $result = $this->copy();
     $result->value = array_pad($result->value, $length, 0);
     $x->value = array_pad($x->value, $length, 0);
     for ($i = 0; $i < $length; ++$i) {
         $result->value[$i] ^= $x->value[$i];
     }
     return $this->_normalize($result);
 }
Esempio n. 3
0
 /**
  * Logical Exclusive-Or
  *
  * @param Math_BigInteger $x
  * @access public
  * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  * @return Math_BigInteger
  */
 function bitwise_xor($x)
 {
     switch (MATH_BIGINTEGER_MODE) {
         case MATH_BIGINTEGER_MODE_GMP:
             $temp = new Math_BigInteger();
             $temp->value = gmp_xor($this->value, $x->value);
             return $temp;
         case MATH_BIGINTEGER_MODE_BCMATH:
             return new Math_BigInteger($this->toBytes() ^ $x->toBytes(), 256);
     }
     $result = new Math_BigInteger();
     $x_length = count($x->value);
     for ($i = 0; $i < $x_length; $i++) {
         $result->value[] = $this->value[$i] ^ $x->value[$i];
     }
     return $result->_normalize();
 }
 function tea_decrypt(&$v0, &$v1, $key)
 {
     $sum = "0xC6EF3720";
     $delta = "0x9e3779b9";
     for ($i = 0; $i < 32; $i++) {
         $v1 = hexdec(gmp_strval(gmp_and(gmp_sub("{$v1}", gmp_xor(gmp_add(gmp_shiftl("{$v0}", 4), $key[2]), gmp_xor(gmp_add("{$v0}", "{$sum}"), gmp_add(gmp_shiftr('0x' . dechex($v0), 5), $key[3])))), '0xffffffff'), 16));
         $v0 = hexdec(gmp_strval(gmp_and(gmp_sub("{$v0}", gmp_xor(gmp_add(gmp_shiftl("{$v1}", 4), $key[0]), gmp_xor(gmp_add("{$v1}", "{$sum}"), gmp_add(gmp_shiftr('0x' . dechex($v1), 5), $key[1])))), '0xffffffff'), 16));
         $sum = hexdec(gmp_strval(gmp_and(gmp_sub("{$sum}", "{$delta}"), '0xffffffff'), 16));
     }
 }
Esempio n. 5
0
 /**
  * @param $value
  * @param $mask
  *
  * @return string
  */
 public static function toggle($value, $mask)
 {
     return gmp_strval(gmp_xor($value, $mask));
 }
Esempio n. 6
0
list($sqrt2, $sqrt2rem) = gmp_sqrtrem("7");
list($sqrt3, $sqrt3rem) = gmp_sqrtrem("1048576");
echo gmp_strval($sqrt1) . ", " . gmp_strval($sqrt1rem) . "\n";
echo gmp_strval($sqrt2) . ", " . gmp_strval($sqrt2rem) . "\n";
echo gmp_strval($sqrt3) . ", " . gmp_strval($sqrt3rem) . "\n";
// gmp_strval
$a = gmp_init("0x41682179fbf5");
printf("Decimal: %s, 36-based: %s" . PHP_EOL, gmp_strval($a), gmp_strval($a, 36));
// gmp_sub
$sub = gmp_sub("281474976710656", "4294967296");
// 2^48 - 2^32
echo gmp_strval($sub) . "\n";
// gmp_testbit
$n = gmp_init("1000000");
var_dump(gmp_testbit($n, 1));
gmp_setbit($n, 1);
var_dump(gmp_testbit($n, 1));
// gmp_xor
$xor1 = gmp_init("1101101110011101", 2);
$xor2 = gmp_init("0110011001011001", 2);
$xor3 = gmp_xor($xor1, $xor2);
echo gmp_strval($xor3, 2) . "\n";
// misc
$someBigNumber = '521384146951941511609433057270365759591953092186117381932611' . '7931051185480744623799627495673518857527248912279381830119491298336733624' . '4065664308602139494639522473719070217986094370277293176752384674818467669' . '4051320005681271452635608277857713427577896091736371787214684409012654958' . '5371050792279689258923542019956112129021960864034418159813629774771309960' . '5187072113497804995105973173281609631859502445945534690830264252230825334' . '4685035261931188171010003137835332083814206171776691473035982534904287554' . '687311595628638823537875937519577818577805321712';
$a = gmp_init($someBigNumber, 10);
var_dump(gmp_strval($a) == $someBigNumber);
var_dump(gmp_strval($a, 32));
$b = gmp_init(0xffffff, 16);
var_dump(gmp_strval($b));
$c = gmp_init($b);
var_dump(gmp_strval($c, 16));
Esempio n. 7
0
 /**
  * Binary xor helper.
  *
  * @param string $left
  * @param string $round
  *
  * @return string Binary string.
  */
 private function _binaryXor($left, $round)
 {
     $xOr = gmp_xor(gmp_init($left, 2), gmp_init($round, 2));
     $bin = gmp_strval($xOr, 2);
     return str_pad($bin, $this->sideSize, '0', STR_PAD_LEFT);
 }
Esempio n. 8
0
 /**
  * Bitwise "xor" (^)
  *
  * @param mixed $number
  * @access public
  * @return self
  */
 public function bitXor($number)
 {
     $result = gmp_xor($this->getRawValue(), static::upgradeParam($number)->getRawValue());
     return static::factory($result);
 }
Esempio n. 9
0
<?php

var_dump(gmp_strval(gmp_xor("111111", "2222222")));
var_dump(gmp_strval(gmp_xor(123123, 435234)));
var_dump(gmp_strval(gmp_xor(555, "2342341123")));
var_dump(gmp_strval(gmp_xor(-1, 3333)));
var_dump(gmp_strval(gmp_xor(4545, -20)));
var_dump(gmp_strval(gmp_xor("test", "no test")));
$n = gmp_init("987657876543456");
var_dump(gmp_strval(gmp_xor($n, "34332")));
$n1 = gmp_init("987657878765436543456");
var_dump(gmp_strval(gmp_xor($n, $n1)));
var_dump(gmp_xor($n, $n1, 1));
var_dump(gmp_xor(1));
var_dump(gmp_xor(array(), 1));
var_dump(gmp_xor(1, array()));
var_dump(gmp_xor(array(), array()));
echo "Done\n";
Esempio n. 10
0
 /**
  * Logical Exclusive-Or
  * Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  *
  * @param BigInteger $x
  * @access public
  * @return BigInteger
  */
 function bitwiseXOR($x)
 {
     switch (BigInteger_MODE) {
         case BigInteger_MODE_GMP:
             $temp = new BigInteger();
             $temp->value = gmp_xor($this->value, $x->value);
             return $this->_normalize($temp);
         case BigInteger_MODE_BCMATH:
             $left = $this->toBytes();
             $right = $x->toBytes();
             $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 BigInteger($left ^ $right, 256));
     }
     $length = max(count($this->value), count($x->value));
     $result = $this->copy();
     if (!is_array($result->value)) {
         $result->value = [];
     }
     $result->value = array_pad($result->value, 0, $length);
     if (!is_array($x->value)) {
         $x->value = [];
     }
     $x->value = array_pad($x->value, 0, $length);
     for ($i = 0; $i < $length; ++$i) {
         $result->value[$i] = $this->value[$i] ^ $x->value[$i];
     }
     return $this->_normalize($result);
 }
Esempio n. 11
0
 /**
  * @param int[] $bytes array of ascii codes of bytes to decode
  * @return string represenation of decoded long.
  */
 static function decode_long_from_array($bytes)
 {
     $b = array_shift($bytes);
     $g = gmp_init($b & 0x7f);
     $shift = 7;
     while (0 != ($b & 0x80)) {
         $b = array_shift($bytes);
         $g = gmp_or($g, self::shift_left($b & 0x7f, $shift));
         $shift += 7;
     }
     $val = gmp_xor(self::shift_right($g, 1), gmp_neg(gmp_and($g, 1)));
     return gmp_strval($val);
 }
Esempio n. 12
0
 /**
  * {@inheritDoc}
  * @see \Mdanter\Ecc\MathAdapter::bitwiseXor()
  */
 public function bitwiseXor($first, $other)
 {
     return gmp_strval(gmp_xor(gmp_init($first, 10), gmp_init($other, 10)));
 }
Esempio n. 13
0
 protected function gctr($ICB, $X)
 {
     if ($X === '') {
         return '';
     }
     $Xn = str_split($X, 16);
     $n = count($Xn);
     $CB = array(1 => $ICB);
     $Yn = array();
     for ($i = 1; $i < $n; $i++) {
         $CB[$i + 1] = static::inc($CB[$i], 32);
         $t = mcrypt_generic($this->cipher, pack('H*', str_pad(gmp_strval($CB[$i], 16), 32, '0', STR_PAD_LEFT)));
         $t = gmp_xor(gmp_init(bin2hex($Xn[$i - 1]), 16), gmp_init(bin2hex($t), 16));
         $Yn[$i] = pack('H*', str_pad(gmp_strval($t, 16), 32, '0', STR_PAD_LEFT));
     }
     // Cipher
     $t = mcrypt_generic($this->cipher, pack('H*', str_pad(gmp_strval($CB[$n], 16), 32, '0', STR_PAD_LEFT)));
     // MSB Xn*
     $t = str_pad(gmp_strval(gmp_init(bin2hex($t), 16), 16), 32, '0', STR_PAD_LEFT);
     $nn = strlen($Xn[$n - 1]) << 1;
     $t = substr($t, 0, $nn);
     // Yn*
     $t = gmp_xor(gmp_init(bin2hex($Xn[$n - 1]), 16), gmp_init($t, 16));
     $Yn[$n] = pack('H*', str_pad(gmp_strval($t, 16), $nn, '0', STR_PAD_LEFT));
     return implode('', $Yn);
 }