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
bcpowmod
(PHP 5)
bcpowmod — Calcule le reste modulo d'un nombre élevé à une puissance
Description
Utilise la méthode d'exponentiation rapide pour élever le nombre left_operand à la puissance right_operand , et en calculant le reste modulo modulus .
Liste de paramètres
- left_operand
-
L'opérande de gauche, sous la forme d'une chaîne de caractères.
- right_operand
-
L'opérande de droite, sous la forme d'une chaîne de caractères.
- modulus
-
Le modulo, sous la forme d'une chaîne de caractères.
- scale
-
Ce paramètre optionnel est utilisé pour définir le nombre de digits après la décimale à placer dans le résultat. Vous pouvez également définir la précision globale par défaut pour toutes les fonctions en utilisant la fonction bcscale().
Valeurs de retour
Retourne le résultat, sous la forme d'une chaîne de caractères, ou NULL si modulus vaut 0.
Notes
Note: Comme cette méthode utilise les opérations de modulo, les nombres non naturels risquent de donner des résultats inattendus. Un nombre naturel est un entier positif non-nul.
Exemples
Les deux lignes suivantes produisent le même résultat. La version qui utilise bcpowmod() est bien plus rapide, et accepte des paramètres plus grands.
<?php
$a = bcpowmod($x, $y, $mod);
$b = bcmod(bcpow($x, $y), $mod);
// $a et $b sont égaux.
?>
bcpowmod
21-Feb-2007 11:04
30-Jan-2007 02: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);
}
}
15-May-2006 11: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."
28-Sep-2005 06: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);
}
