downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

bcscale> <bcpow
Last updated: Fri, 13 Nov 2009

view this page in

bcpowmod

(PHP 5)

bcpowmod任意精度数値のべき乗の、指定した数値による剰余

説明

string bcpowmod ( string $left_operand , string $right_operand , string $modulus [, int $scale ] )

modulus で割った余りを求めることを考慮して、 left_operandright_operand 乗を高速に計算します。

パラメータ

left_operand

左オペランドを表す文字列。

right_operand

右オペランドを表す文字列。

modulus

法を表す文字列。

scale

このオプションパラメータを使用して、 結果の小数点以下の桁数を指定します。すべての関数で使用するデフォルトのスケールを定義するには bcscale() を使用します。

返り値

結果を文字列で返します。modulus が 0 の場合は NULL を返します。

注意

注意: このメソッドでは剰余計算を行っているので、自然数ではない数を 指定すると予期せぬ結果となります。自然数とは 0 以外の正の整数です。

以下の 2 つの文は機能的に同じです。しかし bcpowmod() バージョンのほうが実行時間が早いうえ、 より大きな値の計算が可能です。

<?php
$a 
bcpowmod($x$y$mod);

$b bcmod(bcpow($x$y), $mod);

// $a と $b は同じ値になります

?>

参考

  • bcpow() - 任意精度数値をべき乗する
  • bcmod() - 2 つの任意精度数値の剰余を取得する



bcscale> <bcpow
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
bcpowmod
william at example dot com
22-Feb-2007 07:04
The author's statement:

"A natural number is any positive non-zero integer."

should be, imo, something like:

"In this context only positive non-zero integers are considered to be natural numbers."

http://mathworld.wolfram.com/NaturalNumber.html
http://planetmath.org/encyclopedia/NaturalNumber.html
http://en.wikipedia.org/wiki/Natural_number
laysoft at gmail dot com
30-Jan-2007 10:34
I found a better way to emulate bcpowmod on PHP 4, which works with very big numbers too:

function powmod($m,$e,$n) {
    if (intval(PHP_VERSION)>4) {
        return(bcpowmod($m,$e,$n));
    } else {
        $r="";
        while ($e!="0") {
            $t=bcmod($e,"4096");
            $r=substr("000000000000".decbin(intval($t)),-12).$r;
            $e=bcdiv($e,"4096");
        }
        $r=preg_replace("!^0+!","",$r);
        if ($r=="") $r="0";
        $m=bcmod($m,$n);
        $erb=strrev($r);
        $q="1";
        $a[0]=$m;
        for ($i=1;$i<strlen($erb);$i++) {
            $a[$i]=bcmod(bcmul($a[$i-1],$a[$i-1]),$n);
        }
        for ($i=0;$i<strlen($erb);$i++) {
            if ($erb[$i]=="1") {
                $q=bcmod(bcmul($q,$a[$i]),$n);
            }
        }
        return($q);
    }
}
rrasss at gmail dot com
16-May-2006 06:46
However, if you read his full note, you see this paragraph:
"The function bcpowmod(v, e, m) is supposedly equivalent to bcmod(bcpow(v, e), m).  However, for the large numbers used as keys in the RSA algorithm, the bcpow function generates a number so big as to overflow it.  For any exponent greater than a few tens of thousands, bcpow overflows and returns 1."

So you still can, and should (over bcmod(bcpow(v, e), m) ), use his function if you are using larger exponents, "any exponent greater than a few tens of thousand."
ewilde aht bsmdevelopment dawt com
28-Sep-2005 01:46
Versions of PHP prior to 5 do not have bcpowmod in their repertoire.  This routine simulates this function using bcdiv, bcmod and bcmul.  It is useful to have bcpowmod available because it is commonly used to implement the RSA algorithm.
 
The function bcpowmod(v, e, m) is supposedly equivalent to bcmod(bcpow(v, e), m).  However, for the large numbers used as keys in the RSA algorithm, the bcpow function generates a number so big as to overflow it.  For any exponent greater than a few tens of thousands, bcpow overflows and returns 1.

This routine will iterate through a loop squaring the result, modulo the modulus, for every one-bit in the exponent.  The exponent is shifted right by one bit for each iteration.  When it has been reduced to zero, the calculation ends.

This method may be slower than bcpowmod but at least it works.

function PowModSim($Value, $Exponent, $Modulus)
  {
  // Check if simulation is even necessary.
  if (function_exists("bcpowmod"))
    return (bcpowmod($Value, $Exponent, $Modulus));

  // Loop until the exponent is reduced to zero.
  $Result = "1";

  while (TRUE)
    {
    if (bcmod($Exponent, 2) == "1")
      $Result = bcmod(bcmul($Result, $Value), $Modulus);

    if (($Exponent = bcdiv($Exponent, 2)) == "0") break;

    $Value = bcmod(bcmul($Value, $Value), $Modulus);
    }

  return ($Result);
  }

bcscale> <bcpow
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites