You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
Valeh2012
changed the title
optimize exp for even base
optimize exp for even bases
Oct 30, 2024
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 is2^{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 like0 * 0 = 0
or0 * 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!
The text was updated successfully, but these errors were encountered: