[GIT-PULLS] [php-src] PR #22852: ext/gmp: Add gmp_powm_sec()

[email protected] (LamentXU123) Tue, 21 Jul 2026 19:24:46 +0000
Newsgroups php.git-pulls
Message-ID <[email protected]>
Pull Request: https://github.com/php/php-src/pull/22852
Author: LamentXU123

I am currently migrating a cryptographic lib (RSA signature & decryption) from python to PHP, relying on ext-gmp. Now, I want to compute modular exponentiation with secret private exponents. For RSA decryption and signing I need to compute base^d mod N where d is a secret private exponent.

Ah, I need to consider security issue because clearly `gmp_powm` (implemented with `mpz_powm` in libgmp) leaks exponent bits via timing and cache side channels, which creates a security vulnerability.

Now, we've got [`mpz_powm_sec`](https://gmplib.org/manual/Integer-Exponentiation#index-mpz_005fpowm_005fsec) in gmp >= 5.0 which perfectly solve my issue. And, is used by gmpy2 (a very popular python lib for gmp, and is the lib my original python app relies on). In gmpy2, we have [`powmod_sec`](https://gmpy-skirpichev.readthedocs.io/en/latest/mpz.html#gmpy2.powmod_sec) and [`powmod`](https://gmpy-skirpichev.readthedocs.io/en/latest/mpz.html#gmpy2.powmod). It is also supported in [.Net apps](https://documentation.help/Math.Gmp.Native.NET/93210ab6-2523-3130-044a-80bcf43c181d.htm) (and more...). However in PHP we only have `gmp_powm`.

...And I need to implement the security version by myself!

So let's expose the gmp API `mpz_powm_sec` to userland. It is really useful.