ext/gmp: Add gmp_powm_sec()#22852
Open
LamentXU123 wants to merge 1 commit into
Open
Conversation
iliaal
approved these changes
Jul 21, 2026
iliaal
left a comment
Contributor
There was a problem hiding this comment.
The three guards match mpz_powm_sec()'s preconditions exactly (exp > 0, odd non-zero modulus), so nothing reaches GMP's DIVIDE_BY_ZERO. Clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withmpz_powmin libgmp) leaks exponent bits via timing and cache side channels, which creates a security vulnerability.Now, we've got
mpz_powm_secin 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 havepowmod_secandpowmod. It is also supported in .Net apps (and more...). However in PHP we only havegmp_powm....And I need to implement the securer version by myself!
So let's expose the gmp API
mpz_powm_secto userland. It is really useful. Any pow calculation which requires cryptographic security should use this instead ofmpz_powmAnd as a direct exposure to the GMP internal API, this doesn't requires a RFC.