PHP Conference Nagoya 2025

bcpowmod

(PHP 5, PHP 7, PHP 8)

bcpowmodRaise an arbitrary precision number to another, reduced by a specified modulus

说明

bcpowmod(
    string $num,
    string $exponent,
    string $modulus,
    ?int $scale = null
): string

Use the fast-exponentiation method to raise num to the power exponent with respect to the modulus modulus.

参数

num

The base, as an integral string (i.e. the scale has to be zero).

exponent

The exponent, as an non-negative, integral string (i.e. the scale has to be zero).

modulus

The modulus, as an integral string (i.e. the scale has to be zero).

scale
此参数用于设置结果中的小数位数。如果为 null,则使用 bcscale() 设置的默认小数位数,或者回退到 bcmath.scale INI 指令的值。

返回值

Returns the result as a string.

错误/异常

This function throws a ValueError in the following cases:

  • num, exponent or modulus is not a well-formed BCMath numeric string
  • num, exponent or modulus has a fractional part
  • exponent is a negative value
  • scale is outside the valid range

This function throws a DivisionByZeroError exception if modulus is 0.

更新日志

版本 说明
8.0.0 scale is now nullable.
8.0.0 Now throws a ValueError instead of returning false if exponent is a negative value.
8.0.0 Dividing by 0 now throws a DivisionByZeroError exception instead of returning false.

示例

The following two statements are functionally identical. The bcpowmod() version however, executes in less time and can accept larger parameters.

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

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

// $a and $b are equal to each other.

?>

注释

注意:

Because this method uses the modulus operation, numbers which are not positive integers may give unexpected results.

参见

  • bcpow() - 任意精度数字的乘方
  • bcmod() - 任意精度数字取模

添加备注

用户贡献的备注 3 notes

up
2
ewilde aht bsmdevelopment dawt com
19 years ago
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);
}
up
-3
rrasss at gmail dot com
18 years ago
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."
up
-5
laysoft at gmail dot com
17 years ago
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);
}
}
To Top