---
eip: 7883
label: EIP-7883
title: ModExp Gas Cost Increase
status: Final
type: Standards Track
category: Core
created: 2025-02-11
requires: 2565
official: https://eips.ethereum.org/EIPS/eip-7883
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7883.md
canonical: https://eips.huatuo.xyz/zh/eip-7883/
lang: zh
tldr: 提高 ModExp 预编译的 gas 价格（最低值从 200 升至 500，一般成本约为原来的三倍，大输入的定价更陡），使其最坏情况不再阻碍提高区块 gas 上限。
tags: [gas, precompile, modexp, repricing, core]
---

# EIP-7883: ModExp Gas Cost Increase

> Increases cost of ModExp precompile

**TL;DR:** 提高 ModExp 预编译的 gas 价格（最低值从 200 升至 500，一般成本约为原来的三倍，大输入的定价更陡），使其最坏情况不再阻碍提高区块 gas 上限。

## 它解决什么问题？

位于地址 `0x05` 的 `ModExp` 预编译对任意大整数计算 `base^exp mod modulus`（[EIP-198](/eip-198/)）。它的 gas 公式最后一次调整是在 [EIP-2565](/eip-2565/)。后来，跨执行客户端的基准测试显示，一些输入被明显低估了：塞满此类调用的区块，执行时间可能远远超出其 gas 所暗示的水平。

这一点很重要，因为只有当*每个*操作的最坏情况都能落在时间预算之内时，区块 gas 上限才能被安全地提高。EIP-7883 重新定价 `ModExp`，使它不再是扩展 gas 上限的瓶颈之一。

## 它如何运作

接口和算术保持不变，变化的只有 gas 公式。对 EIP-2565 公式做了三处调整：

1. **更高的下限，且不再除以 3。** `max(200, complexity * iterations / 3)` 变为 `max(500, complexity * iterations)`。最低值从 200 gas 升至 500 gas，一般成本变为三倍。
2. **大指数成本更高。** 对于长度超过 32 字节的指数，每多一个字节增加 16 次迭代，而不是 8 次。
3. **最小操作数大小与更陡的增长。** 乘法复杂度至少为 16（相当于操作数为 32 字节），超过 32 字节后为 `2 * words²`，而不是 `words²`（其中 `words = ceil(max(base_len, mod_len) / 8)`）。

```python
def multiplication_complexity(base_len, mod_len):
    max_len = max(base_len, mod_len)
    words = math.ceil(max_len / 8)
    return 16 if max_len <= 32 else 2 * words**2

def iteration_count(exp_len, exp):
    if exp_len <= 32 and exp == 0: n = 0
    elif exp_len <= 32:            n = exp.bit_length() - 1
    else: n = 16 * (exp_len - 32) + ((exp & (2**256 - 1)).bit_length() - 1)
    return max(n, 1)

def gas(base_len, mod_len, exp_len, exp):
    return max(500, multiplication_complexity(base_len, mod_len) * iteration_count(exp_len, exp))
```

根据规范的分析，大约 99.7 % 的历史调用要么遇到从 200 提到 500 的下限提升，要么遇到三倍增长。一些边缘情况的测试向量会上升 20 倍或更多。

## 示例代码

从 Solidity 调用该预编译；请预留充足的 gas，因为针对旧定价写死的 gas 额度现在可能太低：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

library ModExp {
    /// Returns base^exp % mod for 32-byte operands.
    function modexp(uint256 base, uint256 exp, uint256 mod) internal view returns (uint256 r) {
        bytes memory input = abi.encodePacked(
            uint256(32), uint256(32), uint256(32), // lengths of base, exp, mod
            base, exp, mod
        );
        (bool ok, bytes memory out) = address(0x05).staticcall(input);
        require(ok, "modexp failed");
        r = abi.decode(out, (uint256));
    }
}
```

## 需要注意

- 状态：**Final**。这是一次向后不兼容的 gas 变更：以固定 gas 数量调用 `0x05`（该数量是针对 EIP-2565 价格调好的）的合约，现在可能会耗尽 gas。
- 典型的 RSA 验证和大数密码学合约是受影响的主要使用者；请针对你的输入重新测量 gas，而不要沿用旧数字。
- 它常与限制 `ModExp` 输入大小的 [EIP-7823](/eip-7823/) 以及限制单笔交易 gas 的 [EIP-7825](/eip-7825/) 一起讨论；三者合起来可约束最坏情况的工作量。
- 规范指出，剩下的风险是对某些情况定价过高，它认为这比定价过低的危害更小。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7883
