当前位置: 首页>>代码示例>>PHP>>正文


PHP bcpowmod函数代码示例

本文整理汇总了PHP中bcpowmod函数的典型用法代码示例。如果您正苦于以下问题:PHP bcpowmod函数的具体用法?PHP bcpowmod怎么用?PHP bcpowmod使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了bcpowmod函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: is_prime

function is_prime($n, $k)
{
    if ($n == 2) {
        return true;
    }
    if ($n < 2 || $n % 2 == 0) {
        return false;
    }
    $d = $n - 1;
    $s = 0;
    while ($d % 2 == 0) {
        $d /= 2;
        $s++;
    }
    for ($i = 0; $i < $k; $i++) {
        $a = rand(2, $n - 1);
        $x = bcpowmod($a, $d, $n);
        if ($x == 1 || $x == $n - 1) {
            continue;
        }
        for ($j = 1; $j < $s; $j++) {
            $x = bcmod(bcmul($x, $x), $n);
            if ($x == 1) {
                return false;
            }
            if ($x == $n - 1) {
                continue 2;
            }
        }
        return false;
    }
    return true;
}
开发者ID:Gecko136,项目名称:RosettaCodeData,代码行数:33,代码来源:miller-rabin-primality-test.php

示例2: powmod

function powmod($base, $exponent, $modulus)
{
    if (function_exists('gmp_powm')) {
        // fast
        return gmp_strval(gmp_powm($base, $exponent, $modulus));
    }
    if (function_exists('bi_powmod')) {
        // not tested
        return bi_sto_str(bi_powmod($base, $exponent, $modulus));
    }
    if (function_exists('bcpowmod')) {
        // slow
        return bcpowmod($base, $exponent, $modulus);
    }
    // emulation, slow
    $square = bcmod($base, $modulus);
    $result = 1;
    while (bccomp($exponent, 0) > 0) {
        if (bcmod($exponent, 2)) {
            $result = bcmod(bcmul($result, $square), $modulus);
        }
        $square = bcmod(bcmul($square, $square), $modulus);
        $exponent = bcdiv($exponent, 2);
    }
    return $result;
}
开发者ID:yuang1516,项目名称:WebbrowserLock,代码行数:26,代码来源:keygen.php

示例3: encryptPortion

 public static function encryptPortion($portion, $n, $e)
 {
     $plain = '0';
     foreach (array_reverse($portion) as $k => $v) {
         $plain = bcadd($plain, bcmul($v, bcpowmod(256, $k, $n)));
     }
     $t = self::dec2hex(bcpowmod($plain, $e, $n));
     return $t;
 }
开发者ID:romka-chev,项目名称:php-yandex-fotki,代码行数:9,代码来源:Encrypt.php

示例4: expmod

 protected function expmod($b, $e, $m)
 {
     //if($e==0){return 1;}
     $t = bcpowmod($b, $e, $m);
     if ($t[0] === '-') {
         $t = bcadd($t, $m);
     }
     return $t;
 }
开发者ID:trianglman,项目名称:sqrl,代码行数:9,代码来源:Crypto.php

示例5: encrypt

 public static function encrypt($text)
 {
     $text = self::bchexdec(bin2hex($text));
     $n = bcpowmod($text, self::$rsa_exp, self::$rsa_mod);
     $ret = '';
     while ($n > 0) {
         $ret = chr(bcmod($n, 256)) . $ret;
         $n = bcdiv($n, 256, 0);
     }
     return $ret;
 }
开发者ID:wooberlong,项目名称:myauth.us,代码行数:11,代码来源:Authenticator.Crypto.php

示例6: powmod

/**
 * powmod
 * Raise a number to a power mod n
 * This could probably be made faster with some Montgomery trickery, but it's just fallback for now
 * @param string Decimal string to be raised
 * @param string Decimal string of the power to raise to
 * @param string Decimal string the modulus
 * @return string Decimal string
 */
function powmod($num, $pow, $mod)
{
    if (function_exists('bcpowmod')) {
        return bcpowmod($num, $pow, $mod);
    }
    // Emulate bcpowmod
    $result = '1';
    do {
        if (!bccomp(bcmod($pow, '2'), '1')) {
            $result = bcmod(bcmul($result, $num), $mod);
        }
        $num = bcmod(bcpow($num, '2'), $mod);
        $pow = bcdiv($pow, '2');
    } while (bccomp($pow, '0'));
    return $result;
}
开发者ID:jschilli,项目名称:Shine,代码行数:25,代码来源:ap.inc.php

示例7: biDecryptedString

 function biDecryptedString($s, $utf8_decoded = FALSE)
 {
     $blocks = split(",", $s);
     $result = "";
     for ($i = 0; $i < count($blocks); $i++) {
         $block = bcpowmod(self::biFromHex($blocks[$i]), $this->d, $this->m);
         for ($j = 0; $block !== "0"; $j++) {
             $curchar = bcmod($block, 256);
             $result .= chr($curchar);
             $block = bcdiv($block, 256, 0);
         }
     }
     $result = str_replace(chr(255), chr(0), $result);
     $result = substr($result, 0, strpos($result, chr(254)));
     return $utf8_decoded ? utf8_decode($result) : $result;
 }
开发者ID:chenwaichung,项目名称:bi2php,代码行数:16,代码来源:biRSA.php

示例8: _computeK

 private function _computeK()
 {
     $hash_input = str_pad($this->_Ahex, strlen($this->_srp->Nhex()), "0", STR_PAD_LEFT) . str_pad($this->_Bhex, strlen($this->_srp->Nhex()), "0", STR_PAD_LEFT);
     $hash_input = pack("H*", $hash_input);
     $this->_uhex = $this->_srp->hash($hash_input);
     $this->_udec = hex2dec($this->_uhex);
     $Stmp = bcpowmod($this->_vdec, $this->_udec, $this->_srp->Ndec());
     // v^u (mod N)
     $Stmp = bcmod(bcmul($Stmp, $this->_Adec), $this->_srp->Ndec());
     //v^u*A (mod N)
     $Stmp = bcpowmod($Stmp, $this->_bdec, $this->_srp->Ndec());
     // (v^u*A)^b (mod N)
     $this->_Sdec = $Stmp;
     $this->_Shex = dec2hex($this->_Sdec);
     $this->_Shex = str_pad($this->_Shex, strlen($this->_srp->Nhex()), "0", STR_PAD_LEFT);
     $this->_Khex = $this->_srp->keyHash(pack("H*", $this->_Shex));
 }
开发者ID:nduhamel,项目名称:pwdremind,代码行数:17,代码来源:srpsession.php

示例9: modular_exp

 public static function modular_exp($base, $exponent, $modulus)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         if ($exponent < 0) {
             return new ErrorException("Negative exponents (" . $exponent . ") not allowed");
         } else {
             $p = gmp_strval(gmp_powm($base, $exponent, $modulus));
             return $p;
         }
     } elseif (extension_loaded('bcmath') && USE_EXT == 'BCMATH') {
         if ($exponent < 0) {
             return new ErrorException("Negative exponents (" . $exponent . ") not allowed");
         } else {
             $p = bcpowmod($base, $exponent, $modulus);
             return $p;
         }
     } else {
         throw new ErrorException("Please install BCMATH or GMP");
     }
 }
开发者ID:veis,项目名称:CryptoCoin,代码行数:20,代码来源:NumberTheory.php

示例10: sign

 /**
  * Create signature for given data
  *
  * @param string $data
  *
  * @return string
  */
 public function sign($data)
 {
     // Make data hash (16 bytes)
     $base = hash('md4', $data, true);
     // Add 40 random bytes
     for ($i = 0; $i < 10; ++$i) {
         $base .= pack('V', mt_rand());
     }
     // Add length of the base as first 2 bytes
     $base = pack('v', strlen($base)) . $base;
     // Modular exponentiation
     $dec = bcpowmod($this->reverseToDecimal($base), $this->power, $this->modulus);
     // Convert result to hexadecimal
     $hex = gmp_strval($dec, 16);
     // Fill empty bytes with zeros
     $hex = str_repeat('0', 132 - strlen($hex)) . $hex;
     // Reverse byte order
     $hexReversed = '';
     for ($i = 0; $i < strlen($hex) / 4; ++$i) {
         $hexReversed = substr($hex, $i * 4, 4) . $hexReversed;
     }
     return strtolower($hexReversed);
 }
开发者ID:richweber,项目名称:yii2-wm-exchanger,代码行数:30,代码来源:Signer.php

示例11: sign

 /**
  * Create a signature for the given data
  *
  * @param string $data
  *
  * @return string
  */
 public function sign($data)
 {
     // Make data hash (16 bytes)
     $base = hash('md4', $data, true);
     // Add 40 random bytes
     for ($i = 0; $i < 10; ++$i) {
         $base .= pack('V', mt_rand());
     }
     // Add the length of the base (56 = 16 + 40) as the first 2 bytes
     $base = pack('v', mb_strlen($base, self::MB_ENCODING)) . $base;
     // Modular exponentiation
     $dec = bcpowmod(self::reverseToDecimal($base), $this->power, $this->modulus, 0);
     // Convert to hexadecimal
     $hex = self::dec2hex($dec);
     // Fill empty bytes with zeros
     $hex = str_repeat('0', 132 - mb_strlen($hex, self::MB_ENCODING)) . $hex;
     // Reverse byte order
     $hexReversed = '';
     for ($i = 0; $i < mb_strlen($hex, self::MB_ENCODING) / 4; ++$i) {
         $hexReversed = mb_substr($hex, $i * 4, 4, self::MB_ENCODING) . $hexReversed;
     }
     return mb_strtolower($hexReversed, self::MB_ENCODING);
 }
开发者ID:baibaratsky,项目名称:php-wmsigner,代码行数:30,代码来源:Signer.php

示例12: modPow

 /**
  * Performs modular exponentiation.
  *
  * Here's a quick 'n dirty example:
  * <code>
  * <?php
  *    include('Math/BigInteger.php');
  *
  *    $a = new Math_BigInteger('10');
  *    $b = new Math_BigInteger('20');
  *    $c = new Math_BigInteger('30');
  *
  *    $c = $a->modPow($b, $c);
  *
  *    echo $c->toString(); // outputs 10
  * ?>
  * </code>
  *
  * @param Math_BigInteger $e
  * @param Math_BigInteger $n
  * @return Math_BigInteger
  * @access public
  * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and
  *    and although the approach involving repeated squaring does vastly better, it, too, is impractical
  *    for our purposes.  The reason being that division - by far the most complicated and time-consuming
  *    of the basic operations (eg. +,-,*,/) - occurs multiple times within it.
  *
  *    Modular reductions resolve this issue.  Although an individual modular reduction takes more time
  *    then an individual division, when performed in succession (with the same modulo), they're a lot faster.
  *
  *    The two most commonly used modular reductions are Barrett and Montgomery reduction.  Montgomery reduction,
  *    although faster, only works when the gcd of the modulo and of the base being used is 1.  In RSA, when the
  *    base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because
  *    the product of two odd numbers is odd), but what about when RSA isn't used?
  *
  *    In contrast, Barrett reduction has no such constraint.  As such, some bigint implementations perform a
  *    Barrett reduction after every operation in the modpow function.  Others perform Barrett reductions when the
  *    modulo is even and Montgomery reductions when the modulo is odd.  BigInteger.java's modPow method, however,
  *    uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and
  *    the other, a power of two - and recombine them, later.  This is the method that this modPow function uses.
  *    {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates.
  */
 function modPow($e, $n)
 {
     $n = $n->abs();
     if ($e->compare(new Math_BigInteger()) < 0) {
         $e = $e->abs();
         $temp = $this->modInverse($n);
         if ($temp === false) {
             return false;
         }
         return $temp->modPow($e, $n);
     }
     switch (MATH_BIGINTEGER_MODE) {
         case MATH_BIGINTEGER_MODE_GMP:
             $temp = new Math_BigInteger();
             $temp->value = gmp_powm($this->value, $e->value, $n->value);
             return $temp;
         case MATH_BIGINTEGER_MODE_BCMATH:
             // even though the last parameter is optional, according to php.net, it's not optional in
             // PHP_Compat 1.5.0 when running PHP 4.
             $temp = new Math_BigInteger();
             $temp->value = bcpowmod($this->value, $e->value, $n->value, 0);
             return $temp;
     }
     if (empty($e->value)) {
         $temp = new Math_BigInteger();
         $temp->value = array(1);
         return $temp;
     }
     if ($e->value == array(1)) {
         list(, $temp) = $this->divide($n);
         return $temp;
     }
     if ($e->value == array(2)) {
         $temp = $this->_square();
         list(, $temp) = $temp->divide($n);
         return $temp;
     }
     // is the modulo odd?
     if ($n->value[0] & 1) {
         return $this->_slidingWindow($e, $n, MATH_BIGINTEGER_MONTGOMERY);
     }
     // if it's not, it's even
     // find the lowest set bit (eg. the max pow of 2 that divides $n)
     for ($i = 0; $i < count($n->value); $i++) {
         if ($n->value[$i]) {
             $temp = decbin($n->value[$i]);
             $j = strlen($temp) - strrpos($temp, '1') - 1;
             $j += 26 * $i;
             break;
         }
     }
     // at this point, 2^$j * $n/(2^$j) == $n
     $mod1 = $n->_copy();
     $mod1->_rshift($j);
     $mod2 = new Math_BigInteger();
     $mod2->value = array(1);
     $mod2->_lshift($j);
     $part1 = $mod1->value != array(1) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new Math_BigInteger();
//.........这里部分代码省略.........
开发者ID:thu0ng91,项目名称:jmc,代码行数:101,代码来源:biginteger.php

示例13: powmod

 function powmod($base, $exponent, $modulus)
 {
     if (function_exists('bcpowmod')) {
         return bcpowmod($base, $exponent, $modulus);
     } else {
         return $this->_powmod($base, $exponent, $modulus);
     }
 }
开发者ID:ookwudili,项目名称:chisimba,代码行数:8,代码来源:BigMath.php

示例14: powmod

 /**
  * Gets the remainder of this integer number raised to the integer `$exponent`, divided by the integer `$modulus`
  *
  * This method is faster than doing `$num->pow($exponent)->mod($modulus)`
  * and is primarily useful for cryptographic functionality.
  *
  * @throws fValidationException  When `$exponent` or `$modulus` is not a valid number
  *
  * @param  fNumber|string $exponent  The power to raise to - all non integer values will be truncated to integers
  * @param  fNumber|string $modulus   The value to divide by - all non integer values will be truncated to integers
  * @return fNumber  The remainder
  */
 public function powmod($exponent, $modulus)
 {
     $exp = self::parse($exponent, 'array');
     $mod = self::parse($modulus, 'array');
     if ($this->value[0] == '-') {
         throw new fProgrammerException('The method %s can only be called for positive number, however this number is negative', 'powmod()');
     }
     if ($exp['integer'][0] == '-') {
         throw new fProgrammerException('The exponent specified, %s, must be a positive integer, however it is negative', $exponent);
     }
     if ($mod['integer'][0] == '-') {
         throw new fProgrammerException('The modulus specified, %s, must be a positive integer, however it is negative', $modulus);
     }
     // All numbers involved in this need to be integers
     $exponent = $exp['integer'];
     $modulus = $mod['integer'];
     $len = strpos($this->value, '.') !== FALSE ? strpos($this->value, '.') : strlen($this->value);
     $value = substr($this->value, 0, $len);
     if (function_exists('bcpowmod')) {
         $result = bcpowmod($value, $exponent, $modulus, 0);
     } else {
         $exponent = self::baseConvert($exponent, 10, 2);
         $result = '+1';
         self::performDiv($value, $modulus, $first_modulus);
         for ($i = 0; $i < strlen($exponent); $i++) {
             self::performDiv(self::performMul($result, $result), $modulus, $result);
             if ($exponent[$i] == '1') {
                 self::performDiv(self::performMul($result, $first_modulus), $modulus, $result);
             }
         }
     }
     return new fNumber($result);
 }
开发者ID:gopalgrover23,项目名称:flourish-classes,代码行数:45,代码来源:fNumber.php

示例15: powmod

 /**
  * Raises an arbitrary precision number to another,
  * reduced by a specified modulus.
  *
  * @param  string  $a        The first number.
  * @param  string  $b        The exponent.
  * @param  string  $c        The modulus.
  * @return string            The result of the operation.
  */
 public function powmod($a, $b, $c)
 {
     return bcpowmod($this->bcNormalize($a), $this->bcNormalize($b), $this->bcNormalize($c));
 }
开发者ID:ionux,项目名称:phactor,代码行数:13,代码来源:BC.php


注:本文中的bcpowmod函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。