bcpowmod


php128 apg

RAISE an arbitrary precision number to another, reduced by a specific modulus.





This function returns the result as STRING.

If numbers are not positive integers may give unexpected results.

If $modulus = 0 or $exponent < 0,
this function returns:
Warning in PHP 7.4.XX or
Fatal error in PHP 8.0.XX.

This function is used in encryption and decryption algoritims.

If $scale is omited, this function will default to the scale set globally with the bcscale function, or fallback to 0 if this has not been set.


Related links:

   The RSA Ontology 1.0  !

   RSA-SHA1 Signature Suite - Version 1.0  !

   Math Is Fun  !

   Surjective (onto) ... (one-to-one) ...   !




<?php

string bcpowmod 
string $num
                              
string $exponent
                              
string $modulus
                                 ?
int $scale null )


where,

$num The base as STRING

$exponent 
The exponent as STRING

$modulus 
The modulus, as an integral STRING

$scale 
The number of decimal places 
                                     considered by this operation

?>
 

$base


The base treated as STRING.



$exponent


The exponent treated as STRING.



$modulus


The modulus as an integral treated as STRING.



$scale


The number of decimal places to be considered.

This is nullable as of PHP 8.0.XX.



  1 EXERCISE   

<?php

$a 
'3';

$mod01 17;

for (
$b 0$b <=9$b++)
{
$xbcpwmd bcpowmod($a$b$mod01);
$xbcpw bcpow($a,$b);
$xbcmd bcmod($xbcpw$mod01);

echo 
'<br>bcpow(' $a ', ' $b ') = ' $xbcpw '&nbsp;=>&nbsp;';

echo 
'bcmod(' $xbcpw ', ' $mod01 ') = ' $xbcmd '<br>';

echo 
'bcpowmod(' $a ', ' $b ', ' $mod01 ') = ' 
                                                             
$xbcpwmd '<br>';
}

?>

 RESULT   

modulus = 17


A

bcpow(3, 0) = 1 => bcmod(1, 17) = 1
bcpowmod(3, 0, 17) = 1

where:

3 ** 0 = 1
and
3 * 0 ≡ 1


B

bcpow(3, 1) = 3 => bcmod(3, 17) = 3
bcpowmod(3, 1, 17) = 3

where:

3 ** 1 = 3
and
3 * 1 = 3


C

bcpow(3, 2) = 9 => bcmod(9, 17) = 9
bcpowmod(3, 2, 17) = 9

where:

3 ** 2 = 9
and
3 * 3 = 9


D

bcpow(3, 3) = 27 => bcmod(27, 17) = 10
bcpowmod(3, 3, 17) = 10

where:

( 3 ** 3 ) = 27
and
( 3 * 9 ) = 27 = ( 17 * 1 + 10 ) ⇒ 27 ≡ 10


E

bcpow(3, 4) = 81 => bcmod(81, 17) = 13
bcpowmod(3, 4, 17) = 13

where:

( 3 ** 4 ) = 81
and
( 3 * 27 ) = 81 = ( 17 * 4 + 13 ) ⇒ 81 ≡ 13
or
3 * 10 ) = 30 = ( 17 * 1 ) + 13 ⇒ 30 ≡ 13


F

bcpow(3, 5) = 243 => bcmod(243, 17) = 5
bcpowmod(3, 5, 17) = 5

where:

( 3 ** 13 ) = 39 = ( 17 * 2 ) + 5 ⇒ 39 ≡ 5



G

bcpow(3, 6) = 729 => bcmod(729, 17) = 15
bcpowmod(3, 6, 17) = 15


H

bcpow(3, 7) = 2187 => bcmod(2187, 17) = 11
bcpowmod(3, 7, 17) = 11


I

bcpow(3, 8) = 6561 => bcmod(6561, 17) = 16
bcpowmod(3, 8, 17) = 16


J

bcpow(3, 9) = 19683 => bcmod(19683, 17) = 14
bcpowmod(3, 9, 17) = 14


Try to solve as the proposed on A, B, C, D, E and F...
... the same conditions imposed on G, H, I and J.


  2 EXERCISE   

<?php

$base
3;

$exp = -3;

$mod 0;

$xbcpwmd bcpowmod($base$exp$mod);
$xbcpw bcpow($base,$exp);
$xbcmd bcmod($xbcpw$mod);

echo 
'<br>bcpow(' $base', ' $exp ') = ' $xbcpw 
                                                    
'&nbsp;=>&nbsp;';

echo 
'bcmod(' $xbcpw ', ' $mod ') = ' $xbcmd '<br>';

echo 
'bcpowmod(' $base', ' $exp ', ' $mod ') = ' 
                                                 
$xbcpwmd '<br>';

?>