HomeEIPsNewsletter
EIPsEIP-7883
EIP-7883

ModExp Gas Cost Increase

Increases cost of ModExp precompile
DraftStandards Track: Core
Created: 2025-02-11
Requires: EIP-2565
Marcin Sobczak (@marcindsobczak), Marek Moraczyński (@MarekM25), Marcos Maceo (@stdevMac)
Discussions ForumOriginal Proposal LinkEdit
1 min read
Anyone may contribute to propose contents.
Go propose
Video
Anyone may contribute to propose contents.
Go propose
Original

Abstract

This EIP is modifying the ModExp precompile pricing algorithm introduced in EIP-2565.

Motivation

Currently the ModExp precompile is underpriced in certain scenarios relative to its resource consumption. By adjusting the pricing formula, this EIP aims to address these discrepancies, making ModExp sufficiently efficient to enable potential increases in the block gas limit.

Specification

Upon activation of this EIP, the gas cost of calling the precompile at address 0x0000000000000000000000000000000000000005 will be calculated as follows:

def calculate_multiplication_complexity(base_length, modulus_length):
    max_length = max(base_length, modulus_length)
    words = math.ceil(max_length / 8)
    multiplication_complexity = 16
    if max_length > 32: multiplication_complexity = 2 * words**2
    return multiplication_complexity

def calculate_iteration_count(exponent_length, exponent):
    iteration_count = 0
    if exponent_length <= 32 and exponent == 0: iteration_count = 0
    elif exponent_length <= 32: iteration_count = exponent.bit_length() - 1
    elif exponent_length > 32: iteration_count = (16 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)
    return max(iteration_count, 1)

def calculate_gas_cost(base_length, modulus_length, exponent_length, exponent):
    multiplication_complexity = calculate_multiplication_complexity(base_length, modulus_length)
    iteration_count = calculate_iteration_count(exponent_length, exponent)
    return max(500, math.floor(multiplication_complexity * iteration_count))

The specific changes from the algorithm defined in EIP-2565:

1. Increased minimal and general price

The gas cost calculation is modified from:

    return max(200, math.floor(multiplication_complexity * iteration_count / 3))

to:

    return max(500, math.floor(multiplication_complexity * iteration_count))

This change increases the minimum gas cost from 200 to 500 and triples the general cost by removing the division by 3.

2. Increase cost for exponents larger than 32 bytes

The gas cost calculation is modified from:

    elif exponent_length > 32: iteration_count = (8 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)

to:

    elif exponent_length > 32: iteration_count = (16 * (exponent_length - 32)) + ((exponent & (2**256 - 1)).bit_length() - 1)

The multiplier for exponents larger than 32 bytes is increased from 8 to 16, doubling its impact.

3. Assume the minimal base / modulus length to be 32 and increase the cost when it is larger than 32 bytes

The gas cost calculation is modified from:

def calculate_multiplication_complexity(base_length, modulus_length):
    max_length = max(base_length, modulus_length)
    words = math.ceil(max_length / 8)
    return words**2

to:

def calculate_multiplication_complexity(base_length, modulus_length):
    max_length = max(base_length, modulus_length)
    words = math.ceil(max_length / 8)
    multiplication_complexity = 16
    if max_length > 32: multiplication_complexity = 2 * words**2
    return multiplication_complexity

This change introduces a minimal multiplication complexity of 16 and doubles the complexity if the base or modulus length exceeds 32 bytes.

Rationale

Benchmarking the ModExp precompile revealed several scenarios where its gas cost was significantly underestimated. Pricing adjustments are designed to rectify underpriced edge cases by modifying the existing ModExp pricing formula parameters. Specifically, the minimum cost for ModExp will rise from 200 to 500 (a 150% increase), the general cost will triple (a 200% increase), a minimum base/modulus length of 32 bytes will be assumed and the cost will scale more aggressively when the base, modulus, or exponent exceed 32 bytes. These modifications aim to ensure that the ModExp precompile's performance, even in its most resource-intensive edge cases across all execution layer clients, no longer impedes potential increases to the block gas limit.

Backwards Compatibility

This EIP introduces a backwards-incompatible change. However, similar gas repricings have occurred multiple times in the Ethereum ecosystem, and their effects are well understood.

Test Cases

The most common usages (approximately 99.69% of historical Modexp calls as of January 4th, 2025) will experience either a 150% increase (from 200 to 500 gas) or a 200% increase (tripling from approximately 1360 gas). No changes are made to the underlying interface or arithmetic algorithms, allowing existing test vectors to be reused. The table below presents the updated gas costs for these test vectors:

Test CaseEIP-2565 PricingEIP-7883 PricingIncrease
modexp_nagydani_1_square200500150%
modexp_nagydani_1_qube200500150%
modexp_nagydani_1_pow0x100013412048501%
modexp_nagydani_2_square200512156%
modexp_nagydani_2_qube200512156%
modexp_nagydani_2_pow0x1000113658192501%
modexp_nagydani_3_square3412048501%
modexp_nagydani_3_qube3412048501%
modexp_nagydani_3_pow0x10001546132768500%
modexp_nagydani_4_square13658192501%
modexp_nagydani_4_qube13658192501%
modexp_nagydani_4_pow0x1000121845131072500%
modexp_nagydani_5_square546132768500%
modexp_nagydani_5_qube546132768500%
modexp_nagydani_5_pow0x1000187381525288500%
modexp_marius_1_even2057452962102%
modexp_guido_1_even2298511362125%
modexp_guido_2_even2300511522124%
modexp_guido_3_even540032400500%
modexp_guido_4_even1026944489105%
modexp_marcin_1_base_heavy2001152476%
modexp_marcin_1_exp_heavy215166247632%
modexp_marcin_1_balanced2001200500%
modexp_marcin_2_base_heavy8675202500%
modexp_marcin_2_exp_heavy852163681821%
modexp_marcin_2_balanced9965978500%
modexp_marcin_3_base_heavy6772032200%
modexp_marcin_3_exp_heavy7654080433%
modexp_marcin_3_balanced13604080200%

Security Considerations

This EIP does not introduce any new functionality or make existing operations cheaper, therefore there are no direct security concerns related to new attack vectors or reduced costs. The primary security consideration for this EIP is the potential for ModExp scenarios to be overpriced, though this is deemed a lesser risk compared to the current underpricing issues.

Copyright and related rights waived via CC0.

Further reading
Anyone may contribute to propose contents.
Go propose

Not miss a beat of EIPs' update?

Subscribe EIPs Fun to receive the latest updates of EIPs Good for Buidlers to follow up.

View all
Serve Ethereum Builders, Scale the Community.
Resources
GitHub