Cover Image for PHP gmp_invert() Function
161 views

PHP gmp_invert() Function

As of my last update in September 2021, the gmp_invert() function is available in PHP’s GMP (GNU Multiple Precision) extension. The gmp_invert() function calculates the modular multiplicative inverse of a GMP number with respect to a given modulo.

Here’s the syntax of the gmp_invert() function:

GMP gmp_invert ( GMP $num , GMP $mod )

Parameters:

  • $num: The GMP number for which you want to calculate the modular multiplicative inverse.
  • $mod: The GMP number that serves as the modulo for the inversion.

Return value:

  • The gmp_invert() function returns the modular multiplicative inverse of $num modulo $mod, or false if the modular inverse does not exist.

Example:

$number = gmp_init('7');
$modulus = gmp_init('11');

$inverse = gmp_invert($number, $modulus);

if ($inverse !== false) {
    echo "The modular multiplicative inverse of 7 mod 11 is: " . gmp_strval($inverse); // Output: 8
} else {
    echo "The modular multiplicative inverse does not exist.";
}

In this example, the gmp_invert() function is used to calculate the modular multiplicative inverse of the GMP number 7 modulo 11. The result is 8, as 8 is the modular multiplicative inverse of 7 with respect to the modulo 11.

The modular multiplicative inverse is the integer “x” such that (num * x) % mod = 1. It is often used in various cryptographic and mathematical algorithms, such as RSA encryption and hashing.

Keep in mind that the GMP extension needs to be enabled in your PHP configuration to use this function. Additionally, the availability of functions may change in newer PHP versions, so always refer to the PHP manual for the specific version you are using.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS