Skip to content

Update EIP-7883: Triple price #9969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 11, 2025
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
94 changes: 48 additions & 46 deletions EIPS/eip-7883.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This EIP is modifying the `ModExp` precompile pricing algorithm introduced in [E

## Motivation

There are cases where the `ModExp` precompile is underpriced for it's resource consumption. By modifying the `ModExp` pricing formula these scenarios would be covered with minimal impact on real world applications. The target is to make `ModExp` at least as fast as EcRecover precompile in all cases.
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

Expand All @@ -41,44 +41,46 @@ def calculate_iteration_count(exponent_length, exponent):
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 / 3))
return max(500, math.floor(multiplication_complexity * iteration_count))
```

Changes (with algorithm from [EIP-2565](./eip-2565.md)):
The specific changes from the algorithm defined in [EIP-2565](./eip-2565.md):

### 1. Increase minimal price from 200 to 500
### 1. Increased minimal and general price

This part of the equation:
The gas cost calculation is modified from:

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

Is replaced by this:
to:

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

### 2. Increase cost when exponent is larger than 32 bytes
This change increases the minimum gas cost from 200 to 500 and triples the general cost by removing the division by 3.

This part of the equation:
### 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)
```

Is replaced by this:
to:

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

Multiplier 8 is replaced by 16.
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

This part of the equation:
The gas cost calculation is modified from:

```
def calculate_multiplication_complexity(base_length, modulus_length):
Expand All @@ -87,7 +89,7 @@ def calculate_multiplication_complexity(base_length, modulus_length):
return words**2
```

Is replaced by this:
to:

```
def calculate_multiplication_complexity(base_length, modulus_length):
Expand All @@ -98,56 +100,56 @@ def calculate_multiplication_complexity(base_length, modulus_length):
return multiplication_complexity
```

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

## Rationale

After benchmarking the `ModExp` precompile, we identified certain scenarios that are underpriced and require repricing to ensure appropriate costs. Further research revealed that all underpriced edge cases can be addressed by adjusting the parameters in the current `ModExp` pricing formula. With these changes, the minimum cost for using the `ModExp` precompile will increase from 200 to 500 (a 150% increase), and the cost will scale higher when the `base`, `modulus`, or `exponent` exceed 32 bytes. These adjustments will ensure that the worst-performing edge cases of the `ModExp` precompile perform no worse than the `EcRecover` precompile.
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 change is backwards incompatible. However, similar gas repricings have occurred multiple times in the Ethereum ecosystem, and their effects are well understood.
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

There are no changes to the underlying interface or arithmetic algorithms, so the existing test vectors can be reused. Below is a table with the updated test vectors:
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 Case | [EIP-2565](./eip-2565.md) Pricing | EIP-7883 Pricing | Increase |
|------------------------------|-----|-----|----|
| modexp_nagydani_1_square | 200 | 500 | 150% |
| modexp_nagydani_1_qube | 200 | 500 | 150% |
| modexp_nagydani_1_pow0x10001 | 341 | 682 | 100% |
| modexp_nagydani_2_square | 200 | 500 | 150% |
| modexp_nagydani_2_qube | 200 | 500 | 150% |
| modexp_nagydani_2_pow0x10001 | 1365 | 2730 | 100% |
| modexp_nagydani_3_square | 341 | 682 | 100% |
| modexp_nagydani_3_qube | 341 | 682 | 100% |
| modexp_nagydani_3_pow0x10001 | 5461 | 10922 | 100% |
| modexp_nagydani_4_square | 1365 | 2730 | 100% |
| modexp_nagydani_4_qube | 1365 | 2730 | 100% |
| modexp_nagydani_4_pow0x10001 | 21845 | 43690 | 100% |
| modexp_nagydani_5_square | 5461 | 10922 | 100% |
| modexp_nagydani_5_qube | 5461 | 10922 | 100% |
| modexp_nagydani_5_pow0x10001 | 87381 | 174762 | 100% |
| modexp_marius_1_even | 2057 | 3774 | 83% |
| modexp_guido_1_even | 2298 | 4261 | 85% |
| modexp_guido_2_even | 2300 | 4262 | 85% |
| modexp_guido_3_even | 5400 | 10800 | 100% |
| modexp_guido_4_even | 1026 | 1967 | 92% |
| modexp_marcin_1_base_heavy | 200 | 500 | 150% |
| modexp_marcin_1_exp_heavy | 215 | 500 | 133% |
| modexp_marcin_1_balanced | 200 | 500 | 150% |
| modexp_marcin_2_base_heavy | 867 | 1734 | 100% |
| modexp_marcin_2_exp_heavy | 852 | 1364 | 60% |
| modexp_marcin_2_balanced | 996 | 1992 | 100% |
| modexp_marcin_3_base_heavy | 677 | 677 | 0% |
| modexp_marcin_3_exp_heavy | 765 | 765 | 0% |
| modexp_marcin_3_balanced | 1360 | 1360 | 0% |

| modexp_nagydani_1_pow0x10001 | 341 | 2048 | 501% |
| modexp_nagydani_2_square | 200 | 512 | 156% |
| modexp_nagydani_2_qube | 200 | 512 | 156% |
| modexp_nagydani_2_pow0x10001 | 1365 | 8192 | 501% |
| modexp_nagydani_3_square | 341 | 2048 | 501% |
| modexp_nagydani_3_qube | 341 | 2048 | 501% |
| modexp_nagydani_3_pow0x10001 | 5461 | 32768 | 500% |
| modexp_nagydani_4_square | 1365 | 8192 | 501% |
| modexp_nagydani_4_qube | 1365 | 8192 | 501% |
| modexp_nagydani_4_pow0x10001 | 21845 | 131072 | 500% |
| modexp_nagydani_5_square | 5461 | 32768 | 500% |
| modexp_nagydani_5_qube | 5461 | 32768 | 500% |
| modexp_nagydani_5_pow0x10001 | 87381 | 525288 | 500% |
Copy link
Contributor

Choose a reason for hiding this comment

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

We got 524288 for modexp_nagydani_5_pow0x10001. It's the only discrepancy we have. Should this be the case? I am double checking now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have 524288 too, so it was a typo. I will fix it in a moment

| modexp_marius_1_even | 2057 | 45296 | 2102% |
| modexp_guido_1_even | 2298 | 51136 | 2125% |
| modexp_guido_2_even | 2300 | 51152 | 2124% |
| modexp_guido_3_even | 5400 | 32400 | 500% |
| modexp_guido_4_even | 1026 | 94448 | 9105% |
| modexp_marcin_1_base_heavy | 200 | 1152 | 476% |
| modexp_marcin_1_exp_heavy | 215 | 16624 | 7632% |
| modexp_marcin_1_balanced | 200 | 1200 | 500% |
| modexp_marcin_2_base_heavy | 867 | 5202 | 500% |
| modexp_marcin_2_exp_heavy | 852 | 16368 | 1821% |
| modexp_marcin_2_balanced | 996 | 5978 | 500% |
| modexp_marcin_3_base_heavy | 677 | 2032 | 200% |
| modexp_marcin_3_exp_heavy | 765 | 4080 | 433% |
| modexp_marcin_3_balanced | 1360 | 4080 | 200% |

## Security Considerations

There are no security concerns since no new functionality is introduced or made cheaper. The primary consideration for this EIP is the risk of potentially overpriced `ModExp` scenarios.
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

Expand Down