Skip to content

Core\BigInt::raisedToModulo

$bigInt->raisedToModulo(
    Core\BigInt $exponent,
    Core\BigInt $modulus,
): Core\BigInt

Raises a Core\BigInt to a power and gives the remainder, in one step.

raisedToModulo gives the same answer as raising the number to the power and then dividing by the modulus, but the whole power is never built. The memory it uses stays about the size of the modulus, however large the power is.

7 to the power 1000000 is a number of 845099 digits. Core\BigInt::raisedTo throws an error for a number that wide. raisedToModulo gives its remainder at once.

The power may not be negative, and the modulus may not be 0. Both of those throw an ArithmeticError.

This is the method used in a Diffie-Hellman key exchange and an RSA signature, where the numbers are hundreds of digits long and every step is taken modulo a prime.

Parameters

  • $exponentCore\BigIntThe power to raise this value to. It must not be negative.
  • $modulusCore\BigIntThe modulus the result is reduced by.

Return value

Returns Core\BigInt. A BigInt in [0, |$modulus|). This is also true when this value is negative. For example, -3 to the power 3, modulo 7, is 1.

Errors

  • ArithmeticError$modulus is zero, or $exponent is negative.

Changelog

VersionChange
0.0.6Each Core\BigInt method that computes a new number is named for its result: plus, minus, multipliedBy, dividedBy, remainder, raisedTo, raisedToModulo, negated, shiftedLeft and shiftedRight. The old names were add, sub, mul, div, mod, pow, powMod, neg, shl and shr.

Examples

A power and a remainder in one step

<?nvs
// Shows how to raise a number to a power and take the remainder in one step.
// `raisedToModulo` raises the number to the power, divides the result by the modulus
// and keeps the remainder. This prints 445.
echo Core\BigInt::of(4)->raisedToModulo(Core\BigInt::of(13), Core\BigInt::of(497)), "\n";
// The power 0 gives 1, as it does everywhere else. This prints 1.
echo Core\BigInt::of(12345)->raisedToModulo(Core\BigInt::of(0), Core\BigInt::of(497)), "\n";
// The power may not be negative. This throws an `ArithmeticError` and prints
// the message.
try {
Core\BigInt::of(4)->raisedToModulo(Core\BigInt::of(-1), Core\BigInt::of(497));
} catch (ArithmeticError $negative) {
echo $negative->message, "\n";
}
// The modulus may not be 0, because nothing can be divided by 0. This throws
// an `ArithmeticError` too.
try {
Core\BigInt::of(4)->raisedToModulo(Core\BigInt::of(13), Core\BigInt::of(0));
} catch (ArithmeticError $zero) {
echo $zero->message, "\n";
}
Output
445
1
Core\BigInt::raisedToModulo(): the exponent is negative
Core\BigInt::raisedToModulo(): the modulus is zero

A power too large to build

<?nvs
// Shows how `raisedToModulo` gives the remainder of a power that is far too large to build.
// For a small power, `raisedToModulo` gives the same result as `raisedTo` followed by
// `remainder`. This prints 445 twice.
echo Core\BigInt::of(4)->raisedToModulo(Core\BigInt::of(13), Core\BigInt::of(497)), "\n";
echo Core\BigInt::of(4)->raisedTo(13)->remainder(Core\BigInt::of(497)), "\n";
// 7 to the power 1000000 is a number of 845099 digits. `raisedTo` throws an
// error for a number that wide, and this prints the message.
try {
Core\BigInt::of(7)->raisedTo(1000000);
} catch (ArithmeticError $tooWide) {
echo $tooWide->message, "\n";
}
// `raisedToModulo` gives the remainder of that same power at once, and never builds the
// whole number. This prints 880007888.
echo Core\BigInt::of(7)->raisedToModulo(
Core\BigInt::of(1000000),
Core\BigInt::of(1000000007),
), "\n";
Output
445
445
Core\BigInt::raisedTo(): the answer would be 3000000 bits wide, past the 1048576 one call may produce
880007888

Two programs agree on a shared secret

<?nvs
// Shows two programs agreeing on a secret without ever sending it, which is
// what a Diffie-Hellman key exchange does. Each side raises a shared number to
// its own private number, modulo a shared prime, and sends only the result.
// The prime and the base are public. Both sides know them before they start.
// A real exchange uses a prime of about 600 digits.
var $prime = Core\BigInt::parse('170141183460469231731687303715884105727');
var $base = Core\BigInt::of(5);
// Each side keeps its own number secret.
var $mySecret = Core\BigInt::parse('94823749283749283749283749283749');
var $theirSecret = Core\BigInt::parse('38472938472938472938472938472938');
// Each side sends this, and it says nothing about the secret behind it.
var $mine = $base->raisedToModulo($mySecret, $prime);
var $theirs = $base->raisedToModulo($theirSecret, $prime);
// Each side now raises what it received to its own secret. The two results are
// the same number, and it was never sent. This prints the shared secret twice.
echo $theirs->raisedToModulo($mySecret, $prime), "\n";
echo $mine->raisedToModulo($theirSecret, $prime), "\n";
Output
82856664452204462996770021318850689253
82856664452204462996770021318850689253