public function testCmp()
 {
     $this->assertEquals(gmp_strval(gmp_cmp($this->a, $this->a)), $this->math->cmp($this->a, $this->a));
     $this->assertEquals(gmp_strval(gmp_cmp($this->b, $this->b)), $this->math->cmp($this->b, $this->b));
     $this->assertEquals(gmp_strval(gmp_cmp($this->c, $this->c)), $this->math->cmp($this->c, $this->c));
     $this->assertEquals(0, $this->math->cmp(1, 1));
 }
 public function testGMP()
 {
     $math = \Mdanter\Ecc\EccFactory::getAdapter();
     $I_l = "e97a4d6be13f8f5804c0a76080428fc6d51260f74801678c4127045d2640af14";
     $private_key = "142018c66b43a95de58c1cf603446fc0da322bc15fb4df068b844b57c706dd05";
     $n = "115792089237316195423570985008687907852837564279074904382605163141518161494337";
     $gmp_I_l = gmp_init($I_l, 16);
     $gmp_private_key = gmp_init($private_key, 16);
     $gmp_add = gmp_add($gmp_I_l, $gmp_private_key);
     $gmp_add_res = gmp_strval($gmp_add, 10);
     $this->assertEquals("105604983404708440304568772161069255144976878830542744455590282065741265022740", gmp_strval($gmp_I_l));
     $this->assertEquals("9102967069016248707169900673545386030247334423973996501079368232055584775429", gmp_strval($gmp_private_key));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval($gmp_add));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval(gmp_div_r($gmp_add, gmp_init($n))));
     $this->assertEquals("-4", gmp_strval(gmp_cmp(0, gmp_div_r($gmp_add, $n))));
     $this->assertEquals("230500039711040884435309657843302549028061777533591645339274813439315011292506", gmp_strval(gmp_add(gmp_init($n), gmp_div_r($gmp_add, gmp_init($n)))));
     $gmp_mod2 = $math->mod($gmp_add_res, $n);
     $this->assertTrue(is_string($gmp_mod2));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", $gmp_mod2);
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", $gmp_mod2);
     // when no base is provided both a resource and string work
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval(gmp_init($gmp_mod2)));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval($gmp_mod2));
     // when base is provided it fails on HHVM when inputting a string
     $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", gmp_strval(gmp_init($gmp_mod2), 16));
     // $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", gmp_strval($gmp_mod2, 16));
     $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", str_pad(gmp_strval(gmp_init($gmp_mod2), 16), 64, '0', STR_PAD_LEFT));
 }
Beispiel #3
0
function show_mini_orderbook_table_row($id, $curr, $price, $have, $want, $this_fiat, $this_btc, $sum_fiat, $sum_btc, $mine)
{
    global $minimum_btc_amount, $minimum_fiat_amount;
    $this_btc_str = internal_to_numstr($this_btc, BTC_PRECISION);
    $this_fiat_str = internal_to_numstr($this_fiat, FIAT_PRECISION);
    if (string_is_zero($this_btc_str) || string_is_zero($this_fiat_str) || gmp_cmp($this_btc, $minimum_btc_amount) < 0 || gmp_cmp($this_fiat, $minimum_fiat_amount) < 0) {
        return;
    }
    if ($mine) {
        $sum_btc_str = internal_to_numstr($sum_btc, BTC_PRECISION);
        $sum_fiat_str = internal_to_numstr($sum_fiat, FIAT_PRECISION);
        active_table_row("me", "?page=view_order&orderid={$mine}");
        echo "<td class='right'>{$price}</td>\n";
        echo "<td class='right'>{$this_btc_str}</td>\n";
        echo "<td class='right'>{$this_fiat_str}</td>\n";
        echo "<td class='right'>{$sum_btc_str}</td>\n";
        echo "<td class='right'>{$sum_fiat_str}</td>\n";
        echo "</tr>\n";
    } else {
        echo "<tr>\n";
        echo "<td class='right'>{$price}</td>\n";
        show_mini_orderbook_table_cell($id . 't', $curr, $price, $have, $want, $this_fiat, $this_btc);
        show_mini_orderbook_table_cell($id . 'c', $curr, $price, $have, $want, $sum_fiat, $sum_btc);
    }
    echo "</tr>\n";
}
function GetAuthID($i64friendID)
{
    $tmpfriendID = $i64friendID;
    $iServer = "1";
    if (extension_loaded('bcmath') == 1) {
        //decode communityid with bcmath
        if (bcmod($i64friendID, "2") == "0") {
            $iServer = "0";
        }
        $tmpfriendID = bcsub($tmpfriendID, $iServer);
        if (bccomp("76561197960265728", $tmpfriendID) == -1) {
            $tmpfriendID = bcsub($tmpfriendID, "76561197960265728");
        }
        $tmpfriendID = bcdiv($tmpfriendID, "2");
        return "STEAM_0:" . $iServer . ":" . $tmpfriendID;
    } else {
        if (extension_loaded('gmp') == 1) {
            //decode communityid with gmp
            if (gmp_mod($i64friendID, "2") == "0") {
                $iServer = "0";
            }
            $tmpfriendID = gmp_sub($tmpfriendID, $iServer);
            if (gmp_cmp("76561197960265728", $tmpfriendID) == -1) {
                $tmpfriendID = gmp_sub($tmpfriendID, "76561197960265728");
            }
            $tmpfriendID = gmp_div($tmpfriendID, "2");
            return "STEAM_0:" . $iServer . ":" . gmp_strval($tmpfriendID);
        }
    }
    return false;
}
Beispiel #5
0
 private function computeBaseNLength($number, $targetBase)
 {
     $digits = 0;
     while (gmp_cmp($number, gmp_pow($targetBase, $digits)) != -1) {
         $digits++;
     }
     return $digits ?: 1;
 }
 /**
  * {@inheritDoc}
  * @see \Mdanter\Ecc\RandomNumberGeneratorInterface::generate()
  */
 public function generate($max)
 {
     $random = gmp_strval(gmp_random());
     $small_rand = rand();
     while (gmp_cmp($random, $max) > 0) {
         $random = gmp_div($random, $small_rand, GMP_ROUND_ZERO);
     }
     return gmp_strval($random);
 }
Beispiel #7
0
 /**
  * This will return either the integer value of the number or the string value
  * (if the integer value is too large). Use this when you prefer to receive
  * an integer, but you also don't want the number to be rounded down to the
  * maximum integer size if it is too large.
  *
  * @return string || int
  */
 public function GetValue()
 {
     $comparison = gmp_cmp($this->number, $this->int_max);
     if ($comparison > 0) {
         return gmp_strval($this->number);
     } else {
         return gmp_intval($this->number);
     }
 }
function digit($n1, $n2, $d)
{
    global $u, $v;
    $u = gmp_div_q($n1, $d);
    $v = gmp_div_q($n2, $d);
    if (gmp_cmp($u, $v) == 0) {
        return $u;
    }
    return false;
}
Beispiel #9
0
 /**
  * @param $value
  *
  * @return string
  */
 public static function getBits($value)
 {
     $bits = [];
     for ($i = gmp_init(1); gmp_cmp($value, $i) >= 0; $i = gmp_mul($i, 2)) {
         if (static::has($value, $i)) {
             $bits[] = gmp_strval($i);
         }
     }
     return $bits;
 }
 /**
  * {@inheritdoc}
  */
 public function encodeVarint($varint)
 {
     $bytes = [];
     $value = $this->is32Bit ? gmp_and($varint, '0x0ffffffffffffffff') : sprintf('%u', $varint);
     while (gmp_cmp($value, $this->gmp_x00) > 0) {
         $bytes[] = gmp_intval(gmp_and($value, $this->gmp_x7f)) | 0x80;
         $value = gmp_div_q($value, $this->gmp_x80);
     }
     return $bytes;
 }
Beispiel #11
0
 protected function calculateContentLength()
 {
     $nrOfOctets = 1;
     // we need at least one octet
     $tmpValue = gmp_abs(gmp_init($this->value, 10));
     while (gmp_cmp($tmpValue, 127) > 0) {
         $tmpValue = $this->rightShift($tmpValue, 8);
         $nrOfOctets++;
     }
     return $nrOfOctets;
 }
Beispiel #12
0
 public function testcmp()
 {
     $a = 1234;
     $b = '1234123412341234123412341234123412412341234213412421341342342';
     $c = '0x1234123412341234123412341234123412412341234213412421341342342';
     $math = new GmpEngine();
     $this->assertEquals(gmp_strval(gmp_cmp($a, $a)), $math->cmp($a, $a));
     $this->assertEquals(gmp_strval(gmp_cmp($b, $b)), $math->cmp($b, $b));
     $this->assertEquals(gmp_strval(gmp_cmp($c, $c)), $math->cmp($c, $c));
     $this->assertEquals(0, $math->cmp(1, 1));
 }
 public function verifies($hash, Signature $signature)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         $G = $this->generator;
         $n = $this->generator->getOrder();
         $point = $this->point;
         $r = $signature->getR();
         $s = $signature->getS();
         if (gmp_cmp($r, 1) < 0 || gmp_cmp($r, gmp_sub($n, 1)) > 0) {
             return false;
         }
         if (gmp_cmp($s, 1) < 0 || gmp_cmp($s, gmp_sub($n, 1)) > 0) {
             return false;
         }
         $c = NumberTheory::inverse_mod($s, $n);
         $u1 = gmp_Utils::gmp_mod2(gmp_mul($hash, $c), $n);
         $u2 = gmp_Utils::gmp_mod2(gmp_mul($r, $c), $n);
         $xy = Point::add(Point::mul($u1, $G), Point::mul($u2, $point));
         $v = gmp_Utils::gmp_mod2($xy->getX(), $n);
         if (gmp_cmp($v, $r) == 0) {
             return true;
         } else {
             return false;
         }
     } else {
         if (extension_loaded('bcmath') && USE_EXT == 'BCMATH') {
             $G = $this->generator;
             $n = $this->generator->getOrder();
             $point = $this->point;
             $r = $signature->getR();
             $s = $signature->getS();
             if (bccomp($r, 1) == -1 || bccomp($r, bcsub($n, 1)) == 1) {
                 return false;
             }
             if (bccomp($s, 1) == -1 || bccomp($s, bcsub($n, 1)) == 1) {
                 return false;
             }
             $c = NumberTheory::inverse_mod($s, $n);
             $u1 = bcmod(bcmul($hash, $c), $n);
             $u2 = bcmod(bcmul($r, $c), $n);
             $xy = Point::add(Point::mul($u1, $G), Point::mul($u2, $point));
             $v = bcmod($xy->getX(), $n);
             if (bccomp($v, $r) == 0) {
                 return true;
             } else {
                 return false;
             }
         } else {
             throw new ErrorException("Please install BCMATH or GMP");
         }
     }
 }
Beispiel #14
0
 public static function cmp(CurveFp $cp1, CurveFp $cp2)
 {
     $same = null;
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         if (gmp_cmp($cp1->a, $cp2->a) == 0 && gmp_cmp($cp1->b, $cp2->b) == 0 && gmp_cmp($cp1->prime, $cp2->prime) == 0) {
             return 0;
         } else {
             return 1;
         }
     } else {
         throw new ErrorException("Please install GMP");
     }
 }
 public static function gmp_random($n)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         $random = gmp_strval(gmp_random());
         $small_rand = rand();
         while (gmp_cmp($random, $n) > 0) {
             $random = gmp_div($random, $small_rand, GMP_ROUND_ZERO);
         }
         return gmp_strval($random);
     } else {
         throw new Exception("PLEASE INSTALL GMP");
     }
 }
Beispiel #16
0
 public function xrecover($y)
 {
     $xx = gmp_mul(gmp_sub(gmp_mul($y, $y), 1), $this->inv(gmp_add(gmp_mul(gmp_mul($this->params['d'], $y), $y), 1)));
     $x = gmp_powm($xx, gmp_div_q(gmp_add($this->params['q'], 3), 8), $this->params['q']);
     $t = gmp_mod(gmp_sub(gmp_mul($x, $x), $xx), $this->params['q']);
     if (gmp_cmp($t, 0)) {
         $x = gmp_mod(gmp_mul($x, $this->params['I']), $this->params['q']);
     }
     if (gmp_cmp(gmp_mod($x, 2), 0)) {
         $x = gmp_sub($this->params['q'], $x);
     }
     return $x;
 }
Beispiel #17
0
/**
 * Назначение: Основные пользовательские функции
 */
function number62_encode($number)
{
    if (preg_match('#^[0-9]+$#iu', $number) == 0) {
        return "";
    }
    $out = "";
    $string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    do {
        $key = gmp_mod($number, 62);
        $out = $string[$key] . $out;
        $number = gmp_strval(gmp_div_q($number, 62, GMP_ROUND_ZERO));
    } while (gmp_cmp($number, 0) > 0);
    return $out;
}
 public function GOST_verifies($hash, Signature $signature)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         $G = $this->generator;
         //P
         $n = $this->generator->getOrder();
         //q
         $point = $this->point;
         //Q
         $r = $signature->getR();
         $s = $signature->getS();
         if (gmp_cmp($r, 1) < 0 || gmp_cmp($r, gmp_sub($n, 1)) > 0) {
             return false;
         }
         if (gmp_cmp($s, 1) < 0 || gmp_cmp($s, gmp_sub($n, 1)) > 0) {
             return false;
         }
         //step 3 GOST
         $e = gmp_Utils::gmp_mod2($hash, $n);
         if (gmp_cmp($e, '0') === 0) {
             $e = gmp_init('1');
         }
         // step 4 GOST
         $v = gmp_strval(gmp_invert($e, $n));
         // step 5 GOST
         $z1 = gmp_Utils::gmp_mod2(gmp_mul($s, $v), $n);
         $z2 = gmp_Utils::gmp_mod2(gmp_mul(gmp_neg($r), $v), $n);
         // step 6 GOST
         $C = Point::add(Point::mul($z1, $G), Point::mul($z2, $point));
         $R = gmp_Utils::gmp_mod2($C->getX(), $n);
         if (0) {
             echo "n - " . $n . "\n";
             echo "h - " . $hash . "\n";
             echo "e - " . gmp_Utils::gmp_dechex($e) . "\n";
             echo "v - " . gmp_Utils::gmp_dechex($v) . "\n";
             echo "r - " . $r . "\n";
             echo "s - " . $s . "\n";
             echo "z1 - " . gmp_Utils::gmp_dechex($z1) . "\nz2 - " . gmp_Utils::gmp_dechex($z2) . "\n";
             echo "Q - " . $point . "\nG - " . $G . "\n";
             echo "C - " . $C . "\nR - " . $R . "\n";
         }
         if (gmp_cmp($R, $r) == 0) {
             return true;
         } else {
             return false;
         }
     } else {
         throw new ErrorException("Please install GMP");
     }
 }
Beispiel #19
0
function base58_encode($string)
{
    $table = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
    $long_value = gmp_init(bin2hex($string), 16);
    $result = '';
    while (gmp_cmp($long_value, 58) > 0) {
        list($long_value, $mod) = gmp_div_qr($long_value, 58);
        $result .= $table[gmp_intval($mod)];
    }
    $result .= $table[gmp_intval($long_value)];
    for ($nPad = 0; $string[$nPad] == ""; ++$nPad) {
    }
    return str_repeat($table[0], $nPad) . strrev($result);
}
Beispiel #20
0
function Pidigit_Next(&$pd, $times)
{
    $digits = '';
    $z = $pd[0];
    do {
        $y = Transformation_Extract($z, 3);
        do {
            $z = Transformation_Compose($z, Transformation_Next($pd[1]));
            $y = Transformation_Extract($z, 3);
        } while (0 != gmp_cmp(Transformation_Extract($z, 4), $y));
        $z = Transformation_Compose2($y, $z);
        $digits .= gmp_strval($y);
    } while (--$times);
    $pd[0] = $z;
    return $digits;
}
Beispiel #21
0
 /**
  * On each iteration of the while loop:
  *    - the left range of the first range is: 
  *          - assigned to $this->ip (in dotted format) 
  *          - incremented until it is equal to righ range and then
  *          the while loop breaks
  *    - $this->ip is used in formatRule method and saveToXml
  *    - a session is saved
  * After while breaks the first range is removed from $this->ranges 
  * and a session is saved again.    
  */
 public function iterateRanges()
 {
     while (true) {
         $this->ip = long2ip($this->ranges[0][0]);
         array_map(array($this, 'iterateRules'), $this->rules);
         array_map(array($this, 'iterateRulesListAlert'), $this->rulesListAlert);
         $this->saveToXml($this->ip, $this->listsCount);
         $this->resetToDefaults();
         if (0 === gmp_cmp($this->ranges[0][0], $this->ranges[0][1])) {
             break;
         }
         $this->ranges[0][0] = gmp_strval(gmp_add($this->ranges[0][0], "1"));
         $this->saveSession();
     }
     array_shift($this->ranges);
     $this->saveSession();
 }
Beispiel #22
0
 /**
  * Constructor tries to guess what is the $ip
  *
  * @param $ip mixed String, binary string, int or float
  */
 public function __construct($ip)
 {
     if (is_int($ip)) {
         // if an integer is provided, we have to be careful of the architecture
         // on 32 bits plateform, it's always a valid IP
         // on 64 bits plateform, we have to test the value
         $ip = gmp_init(sprintf('%u', $ip), 10);
         if (gmp_cmp($ip, self::MAX_INT) > 0) {
             throw new InvalidArgumentException(sprintf('The integer %s is not a valid IPv4 address', gmp_strval($ip)));
         }
         $this->ip = $ip;
     } elseif (is_float($ip) && floor($ip) == $ip) {
         // float (or double) with an integer value
         $ip = gmp_init(sprintf('%s', $ip), 10);
         if (gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0) {
             throw new InvalidArgumentException(sprintf('The double %s is not a valid IPv4 address', gmp_strval($ip)));
         }
         $this->ip = $ip;
     } elseif (is_string($ip)) {
         // binary string
         if (!ctype_print($ip)) {
             if (strlen($ip) != 4) {
                 throw new InvalidArgumentException("The binary string is not a valid IPv4 address");
             }
             $hex = unpack('H*', $ip);
             $this->ip = gmp_init($hex[1], 16);
         } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
             $this->ip = gmp_init(sprintf('%u', ip2long($ip)));
         } elseif (ctype_digit($ip)) {
             $ip = gmp_init($ip);
             if (gmp_cmp($ip, self::MAX_INT) > 0) {
                 throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv4 address", gmp_strval($ip)));
             }
             $this->ip = $ip;
         } else {
             throw new InvalidArgumentException("{$ip} is not a valid IPv4 address");
         }
     } elseif (is_resource($ip) && get_resource_type($ip) == 'GMP integer' || $ip instanceof GMP) {
         if (gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0) {
             throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv4 address", gmp_strval($ip)));
         }
         $this->ip = $ip;
     } else {
         throw new InvalidArgumentException("Unsupported argument type: " . gettype($ip));
     }
 }
Beispiel #23
0
function Zend_OpenId_bigNumToBin2($bn)
{
    if (extension_loaded('gmp')) {
        /*The GMP conversion code in this function was liberally copied 
        	 and adapted from  JanRain's Auth_OpenId_MathLibrary::longToBinary*/
        $cmp = gmp_cmp($bn, 0);
        if ($cmp < 0) {
            throw new Zend_OpenId_Exception('Big integer arithmetic error', Zend_OpenId_Exception::ERROR_LONG_MATH);
        }
        if ($cmp == 0) {
            return "";
        }
        $bytes = array();
        while (gmp_cmp($bn, 0) > 0) {
            array_unshift($bytes, gmp_mod($bn, 256));
            $bn = gmp_div($bn, pow(2, 8));
        }
        if ($bytes && $bytes[0] > 127) {
            array_unshift($bytes, 0);
        }
        $string = '';
        foreach ($bytes as $byte) {
            $string .= pack('C', $byte);
        }
        return $string;
    } else {
        if (extension_loaded('bcmath')) {
            $cmp = bccomp($bn, 0);
            if ($cmp == 0) {
                return chr(0);
            } else {
                if ($cmp < 0) {
                    throw new Zend_OpenId_Exception('Big integer arithmetic error', Zend_OpenId_Exception::ERROR_LONG_MATH);
                }
            }
            $bin = "";
            while (bccomp($bn, 0) > 0) {
                $bin = chr(bcmod($bn, 256)) . $bin;
                $bn = bcdiv($bn, 256);
            }
            return $bin;
        }
    }
    throw new Zend_OpenId_Exception('The system doesn\'t have proper big integer extension', Zend_OpenId_Exception::UNSUPPORTED_LONG_MATH);
}
Beispiel #24
0
 /**
  * Constuctor tries to guess what is $ip.
  *
  * @param mixed $ip
  */
 public function __construct($ip)
 {
     if (is_int($ip)) {
         // always a valid IP, since even in 64bits plateform, it's less than max value
         $this->ip = gmp_init(sprintf('%u', $ip), 10);
     } elseif (is_float($ip) && floor($ip) == $ip) {
         // float (or double) with an integer value
         $ip = gmp_init(sprintf('%s', $ip), 10);
         if (gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0) {
             throw new InvalidArgumentException(sprintf('The double %s is not a valid IPv6 address', gmp_strval($ip)));
         }
         $this->ip = $ip;
     } elseif (is_string($ip)) {
         // binary string
         if (!ctype_print($ip)) {
             // probably the result of inet_pton
             // must be 16 bytes exactly to be valid
             if (strlen($ip) != 16) {
                 throw new InvalidArgumentException("The binary string is not a valid IPv6 address");
             }
             $hex = unpack('H*', $ip);
             $this->ip = gmp_init($hex[1], 16);
         } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
             $ip = inet_pton($ip);
             $hex = unpack('H*', $ip);
             $this->ip = gmp_init($hex[1], 16);
         } elseif (ctype_digit($ip)) {
             $ip = gmp_init($ip, 10);
             if (gmp_cmp($ip, '340282366920938463463374607431768211455') > 0) {
                 throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv6 address", gmp_strval($ip)));
             }
             $this->ip = $ip;
         } else {
             throw new InvalidArgumentException("{$ip} is not a valid IPv6 address");
         }
     } elseif (is_resource($ip) && get_resource_type($ip) == 'GMP integer' || $ip instanceof GMP) {
         if (gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0) {
             throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv6 address", gmp_strval($ip)));
         }
         $this->ip = $ip;
     } else {
         throw new InvalidArgumentException("Unsupported argument type: " . gettype($ip));
     }
 }
Beispiel #25
0
function summa($type)
{
    $total_in = gmp_init('0');
    $query = "\n        SELECT SUM(amount) AS sum\n        FROM purses\n        WHERE type='{$type}'\n        ";
    $result = do_query($query);
    $row = get_row($result);
    $v = gmp_init($row['sum']);
    $total_in = gmp_add($total_in, $v);
    $query = "\n        SELECT SUM(amount) AS sum\n        FROM orderbook\n        WHERE type='{$type}' AND status='OPEN'\n        ";
    $result = do_query($query);
    $row = get_row($result);
    if (isset($row['sum'])) {
        $v = gmp_init($row['sum']);
        $total_in = gmp_add($total_in, $v);
    }
    $query = "\n        SELECT SUM(amount) AS sum\n        FROM requests\n        WHERE curr_type='{$type}' AND req_type='WITHDR' AND status='VERIFY'\n        ";
    $result = do_query($query);
    $row = get_row($result);
    if (isset($row['sum'])) {
        $v = gmp_init($row['sum']);
        $total_in = gmp_add($total_in, $v);
    }
    $total_in = gmp_strval($total_in);
    $total_out = gmp_init('0');
    $query = "\n        SELECT SUM(amount) AS sum\n        FROM requests\n        WHERE curr_type='{$type}' AND req_type='DEPOS' AND status='FINAL'\n        ";
    $result = do_query($query);
    $row = get_row($result);
    if (isset($row['sum'])) {
        $v = gmp_init($row['sum']);
        $total_out = gmp_add($total_out, $v);
    }
    $query = "\n        SELECT SUM(amount) AS sum\n        FROM requests\n        WHERE curr_type='{$type}' AND req_type='WITHDR' AND status='FINAL'\n        ";
    $result = do_query($query);
    $row = get_row($result);
    if (isset($row['sum'])) {
        $v = gmp_init($row['sum']);
        $total_out = gmp_sub($total_out, $v);
    }
    $total_out = gmp_strval($total_out);
    echo "{$type} = {$total_in}\t  {$total_out}\n";
    if (gmp_cmp($total_in, $total_out) != 0) {
        echo "*********** MISMATCH ****************\n";
    }
}
 public function __construct($value)
 {
     if (!is_string($value)) {
         throw InvalidArgumentException::invalidType('string', 'value', $value);
     }
     // Numeric IDs must be left-padded with zeroes until eight characters. Longer IDs may not be padded.
     if (!preg_match('~^\\d+$~', $value)) {
         throw new InvalidArgumentException('Given Yubikey public ID is not a string of digits');
     }
     if ($value !== sprintf('%08s', ltrim($value, '0'))) {
         throw new InvalidArgumentException('Given Yubikey public ID is longer than 8 digits, yet left-padded with zeroes');
     }
     // Yubikey public IDs, in their (mod)hex format, may be up to sixteen characters in length. Thus, this is their
     // maximum value.
     if (gmp_cmp(gmp_init($value, 10), gmp_init('ffffffffffffffff', 16)) > 0) {
         throw new InvalidArgumentException('Given Yubikey public ID is larger than 0xffffffffffffffff');
     }
     $this->value = $value;
 }
 /**
  * Encode a binary string to base58
  *
  * @param $binary
  * @return string
  * @throws \Exception
  */
 protected function encodeBase58($binary_bitcoin_address)
 {
     $size = strlen($binary_bitcoin_address);
     if ($size == 0) {
         return '';
     }
     $orig = $binary_bitcoin_address;
     $decimal = gmp_import($binary_bitcoin_address);
     $return = "";
     while (gmp_cmp($decimal, 0) > 0) {
         list($decimal, $rem) = gmp_div_qr($decimal, 58);
         $return = $return . self::$BASE_58_CHARS[gmp_intval($rem)];
     }
     $return = strrev($return);
     //leading zeros
     for ($i = 0; $i < $size && $orig[$i] == ""; $i++) {
         $return = "1" . $return;
     }
     return $return;
 }
Beispiel #28
0
 /**
  * verify using gmp extendsions
  */
 function _verifyByGmp($message, $sig, $sigKeys)
 {
     $p = $sigKeys['p'];
     $q = $sigKeys['q'];
     $g = $sigKeys['g'];
     $pubKey = $sigKeys['pub_key'];
     list($r_sig, $s_sig) = explode(":", $sig);
     $r_sig = base64_decode($r_sig);
     $s_sig = base64_decode($s_sig);
     $p = gmp_init($p);
     $q = gmp_init($q);
     $g = gmp_init($g);
     $pubKey = gmp_init($pubKey);
     $s1 = Security_DSA::_bindecGmp($r_sig);
     $s2 = Security_DSA::_bindecGmp($s_sig);
     $w = gmp_invert($s2, $q);
     $hash_m = gmp_init('0x' . sha1($message));
     $u1 = gmp_mod(gmp_mul($hash_m, $w), $q);
     $u2 = gmp_mod(gmp_mul($s1, $w), $q);
     $v = gmp_mod(gmp_mod(gmp_mul(gmp_powm($g, $u1, $p), gmp_powm($pubKey, $u2, $p)), $p), $q);
     return gmp_cmp($v, $s1) == 0;
 }
Beispiel #29
0
 /**
  * @param string $octets
  *
  * @throws InvalidArgumentException if the given octets represent a malformed base-128 value or the decoded value would exceed the the maximum integer length
  *
  * @return int
  */
 public static function decode($octets)
 {
     $bitsPerOctet = 7;
     $value = gmp_init(0, 10);
     $i = 0;
     $leftShift = function ($number, $positions) {
         return gmp_mul($number, gmp_pow(2, $positions));
     };
     while (true) {
         if (!isset($octets[$i])) {
             throw new InvalidArgumentException(sprintf('Malformed base-128 encoded value (0x%s).', strtoupper(bin2hex($octets)) ?: '0'));
         }
         $octet = gmp_init(ord($octets[$i++]), 10);
         $l1 = $leftShift($value, $bitsPerOctet);
         $r1 = gmp_and($octet, 0x7f);
         $value = gmp_add($l1, $r1);
         if (0 === gmp_cmp(gmp_and($octet, 0x80), 0)) {
             break;
         }
     }
     return gmp_strval($value);
 }
 public function format(DateTimePrintContext $context, &$buf)
 {
     $value = $context->getValueField($this->field);
     if ($value === null) {
         return false;
     }
     $decimalStyle = $context->getDecimalStyle();
     $fraction = $this->convertToFraction($value);
     if (gmp_cmp($fraction, 0) === 0) {
         // scale is zero if value is zero
         if ($this->minWidth > 0) {
             if ($this->decimalPoint) {
                 $buf .= $decimalStyle->getDecimalSeparator();
             }
             for ($i = 0; $i < $this->minWidth; $i++) {
                 $buf .= $decimalStyle->getZeroDigit();
             }
         }
     } else {
         $outputScale = Math::min(Math::max(9, $this->minWidth), $this->maxWidth);
         if ($outputScale !== 9) {
             $div = 1 . str_repeat('0', 9 - $outputScale);
             $fraction = gmp_div($fraction, $div);
         }
         $str = gmp_strval($fraction);
         $pad = $outputScale - strlen($str);
         $str = str_repeat('0', $pad) . $str;
         // trim trailing zeros
         while (strlen($str) > $this->minWidth && $str[strlen($str) - 1] === '0') {
             $str = substr($str, 0, strlen($str) - 1);
         }
         $str = $decimalStyle->convertNumberToI18N($str);
         if ($this->decimalPoint) {
             $buf .= $decimalStyle->getDecimalSeparator();
         }
         $buf .= $str;
     }
     return true;
 }