Skip to content

optimize exp for even bases #188

@Valeh2012

Description

@Valeh2012

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions