Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions include/evmmax/evmmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename UintT>
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<uint64_t, uint64_t> 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]};
}
Comment on lines +33 to +47
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compute_mont_mod_inv() and addmul() were previously private ModArith helpers but are now free functions in the public evmmax namespace, which expands the public API surface and increases the chance of name collisions (e.g., with other headers that use using namespace evmmax). If these are intended to remain internal implementation details, consider moving them into an internal namespace (e.g. evmmax::detail) or making them static/internal-linkage helpers so they don’t become part of the public API.

Copilot uses AI. Check for mistakes.

/// The modular arithmetic operations for EVMMAX (EVM Modular Arithmetic Extensions).
template <typename UintT>
class ModArith
Expand All @@ -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<uint64_t, uint64_t> 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)}
Expand Down