---
eip: 2666
label: EIP-2666
title: Repricing of precompiles and Keccak256 function
status: Stagnant
type: Standards Track
category: Core
created: 2020-05-22
requires: 1352, 2046, 2565
official: https://eips.ethereum.org/EIPS/eip-2666
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2666.md
canonical: https://eips.huatuo.xyz/zh/eip-2666/
lang: zh
tldr: 为 SHA256 和 RIPEMD-160 预编译以及 KECCAK256 操作码提出了新的、基于基准测试的 gas 公式，按内部哈希块而非 32 字节字收费。
tags: [gas, precompile, hashing, core, repricing]
---

# EIP-2666: Repricing of precompiles and Keccak256 function

> This EIP tries to set prices of certain precompiles and built-in EVM function to be in line with their performance, consumed resources and newer changes in EVM itself.

**TL;DR:** 为 SHA256 和 RIPEMD-160 预编译以及 KECCAK256 操作码提出了新的、基于基准测试的 gas 公式，按内部哈希块而非 32 字节字收费。

## 它解决什么问题？

以太坊内置哈希函数的 gas 成本定得很早，按输入的每个 32 字节字收费。但哈希函数并不以 32 字节为单位工作。SHA-256 和 RIPEMD-160 处理 64 字节的块，Keccak-256 每轮吸收 136 字节。因此旧价格与真实的 CPU 工作量并不匹配，尤其是 RIPEMD 非常昂贵。

ERC-2666 请客户端团队对这些函数做基准测试，并提出了遵循内部块结构的公式。它还预期了 [EIP-2046](/eip-2046/)：该提案会让对预编译的 `STATICCALL` 变得便宜，从而去掉某些预编译价格可能一直在弥补的一大笔固定开销。

## 它如何运作

分叉区块之后，成本将变为（全程使用整数除法）：

| 函数 | 当前 | 提议 |
| --- | --- | --- |
| SHA256 预编译 `0x02` | 60 + 每字 12 | `10 + ((len + 8) / 64 + 1) * 9` |
| RIPEMD-160 预编译 `0x03` | 600 + 每字 120 | `6 + ((len + 8) / 64 + 1) * 12` |
| `KECCAK256` 操作码 `0x20` | 30 + 每字 6 | `13 + (len / 136 + 1) * 15` |

`+ 1` 项有意单独保留：即使对空输入做哈希，也要完整运行一轮。

这些系数是根据 Geth、OpenEthereum、Besu 和 Nethermind 提供的基准测试，并结合各哈希函数的块大小选定的。

## 示例代码

使用规范自带的测试用例，对比 Keccak-256 的新旧成本：

```ts
const oldKeccak = (len: number) => 30 + 6 * Math.ceil(len / 32);
const newKeccak = (len: number) => 13 + 15 * (Math.floor(len / 136) + 1);

for (const len of [0, 64, 160]) {
  console.log(len, oldKeccak(len), newKeccak(len));
}
// 0   30 28
// 64  42 28
// 160 60 43
```

SHA256 的思路相同：

```ts
const newSha256 = (len: number) => 10 + 9 * (Math.floor((len + 8) / 64) + 1);
console.log(newSha256(32)); // 19 (old price: 72)
```

## 需要注意

- 状态为 **Stagnant**。这些公式都没有被采纳；主网上的哈希成本仍然是按字计费。
- 它依赖 [EIP-2046](/eip-2046/)（更便宜的预编译调用），最好还依赖 [EIP-2565](/eip-2565/) 中的 ModExp 重新定价，后者已在 Berlin 中上线。Berlin 还引入了 [EIP-2929](/eip-2929/)，改变了对预编译调用的收费方式。
- 降低成本可能会破坏那些依赖固定 gas 量来保证安全的合约，例如基于 2300 gas stipend 的重入保护。规范认为这类设计本来就不可靠。


---

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