diff --git a/include/evmmax/evmmax.hpp b/include/evmmax/evmmax.hpp index 57c974c38f..0f07d11137 100644 --- a/include/evmmax/evmmax.hpp +++ b/include/evmmax/evmmax.hpp @@ -30,6 +30,22 @@ constexpr uint64_t inv_mod(uint64_t a) noexcept return inv; } +/// Compute the modulus inverse for Montgomery multiplication, i.e., N': mod⋅N' = 2⁶⁴-1. +template +constexpr uint64_t compute_mont_mod_inv(const UintT& mod) noexcept +{ + // Compute the inversion mod[0]⁻¹ mod 2⁶⁴, then the final result is N' = -mod[0]⁻¹ + // because this gives mod⋅N' = -1 mod 2⁶⁴ = 2⁶⁴-1. + return -inv_mod(mod[0]); +} + +constexpr std::pair addmul( + uint64_t t, uint64_t a, uint64_t b, uint64_t c) noexcept +{ + const auto p = intx::umul(a, b) + t + c; + return {p[1], p[0]}; +} + /// The modular arithmetic operations for EVMMAX (EVM Modular Arithmetic Extensions). template class ModArith @@ -50,21 +66,6 @@ class ModArith return intx::udivrem(RR, mod).rem; } - /// Compute the modulus inverse for Montgomery multiplication, i.e., N': mod⋅N' = 2⁶⁴-1. - static constexpr uint64_t compute_mont_mod_inv(const UintT& mod) noexcept - { - // Compute the inversion mod[0]⁻¹ mod 2⁶⁴, then the final result is N' = -mod[0]⁻¹ - // because this gives mod⋅N' = -1 mod 2⁶⁴ = 2⁶⁴-1. - return -inv_mod(mod[0]); - } - - static constexpr std::pair addmul( - uint64_t t, uint64_t a, uint64_t b, uint64_t c) noexcept - { - const auto p = intx::umul(a, b) + t + c; - return {p[1], p[0]}; - } - public: constexpr explicit ModArith(const UintT& mod) noexcept : mod_{mod}, r_squared_{compute_r_squared(mod)}, mod_inv_{compute_mont_mod_inv(mod)}