function Calculator::modInverse
Returns the modular multiplicative inverse of $x modulo $m.
If $x has no multiplicative inverse mod m, this method must return null.
This method can be overridden by the concrete implementation if the underlying library has built-in support.
Parameters
string $m The modulus; must not be negative or zero.:
1 method overrides Calculator::modInverse()
- GmpCalculator::modInverse in vendor/
brick/ math/ src/ Internal/ Calculator/ GmpCalculator.php - Returns the modular multiplicative inverse of $x modulo $m.
File
-
vendor/
brick/ math/ src/ Internal/ Calculator.php, line 233
Class
- Calculator
- Performs basic operations on arbitrary size integers.
Namespace
Brick\Math\InternalCode
public function modInverse(string $x, string $m) : ?string {
if ($m === '1') {
return '0';
}
$modVal = $x;
if ($x[0] === '-' || $this->cmp($this->abs($x), $m) >= 0) {
$modVal = $this->mod($x, $m);
}
[
$g,
$x,
] = $this->gcdExtended($modVal, $m);
if ($g !== '1') {
return null;
}
return $this->mod($this->add($this->mod($x, $m), $m), $m);
}