Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize exp for even bases #188

Open
Valeh2012 opened this issue Oct 30, 2024 · 0 comments · May be fixed by #189
Open

optimize exp for even bases #188

Valeh2012 opened this issue Oct 30, 2024 · 0 comments · May be fixed by #189

Comments

@Valeh2012
Copy link

The math/big package includes several optimizations for modular exponentiation. It employs three different algorithms depending on whether the modulus is odd, a power of two, or a power of two times an odd number. Specifically, for the second case, it checks the parity of the base. When the base is even and the exponent is greater than or equal to log2(modulus), the result is zero. The uint256 data type falls into this category since the modulus is 2^{256}. Thus, we can optimize the exponentiation operation for even bases.

For example, any even integer raised to the 256th power modulo 2^{256}is zero (and so are any higher powers). This means that for very large exponents, the current implementation mostly performs trivial multiplications like 0 * 0 = 0 or 0 * base = 0 after the 9th bit of the exponent.

While potential solution might slightly increase the performance of exponentiation with odd bases due to branching, it will be exceptionally fast for even bases!

@Valeh2012 Valeh2012 changed the title optimize exp for even base optimize exp for even bases Oct 30, 2024
@Valeh2012 Valeh2012 linked a pull request Oct 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant