---
eip: 5988
label: EIP-5988
title: Add Poseidon hash function precompile
status: Stagnant
type: Standards Track
category: Core
created: 2022-11-15
official: https://eips.ethereum.org/EIPS/eip-5988
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-5988.md
canonical: https://eips.huatuo.xyz/zh/eip-5988/
lang: zh
tldr: 提议为 Poseidon 哈希添加预编译合约。Poseidon 是许多 rollup 使用的对零知识友好的哈希，参数由调用者提供，因此不同项目的变体可以共用。
tags: [precompile, cryptography, zk, rollups, hash]
---

# EIP-5988: Add Poseidon hash function precompile

> Add a precompiled contract which implements the hash function used in the Poseidon cryptographic hashing algorithm

**TL;DR:** 提议为 Poseidon 哈希添加预编译合约。Poseidon 是许多 rollup 使用的对零知识友好的哈希，参数由调用者提供，因此不同项目的变体可以共用。

## 它解决什么问题？

零知识证明系统在需要证明 Keccak 或 SHA-256 时成本很高，因为这些哈希由位运算构成。**Poseidon** 是一种“算术”哈希，设计上原生运行于素数域之上，因此在 SNARK 和 STARK 电路中便宜得多。许多 ZK rollup 和 ZK 应用把它用于 Merkle 树和承诺。

EVM 没有原生的 Poseidon，所以需要重新计算 Poseidon 哈希的合约（例如验证链下构建的 Merkle 根）必须用 Solidity 实现，这会花费大量 gas。EIP-5988 提议用一个预编译合约让它变得便宜。

## 它如何运作

不同项目使用不同的 Poseidon 参数（域、轮数、状态宽度），因此预编译不固定某一种变体，而是把参数作为输入：

| 字段 | 字节数 | 含义 |
| --- | --- | --- |
| `p` | 32 | 素数域模数 |
| `security_level` | 2 | 以位为单位的安全级别（`M`） |
| `alpha` | 1 | S-box 的幂 |
| `input_rate` | 2 | 输入元素的数量 |
| `t` | 1 | 状态大小 |
| `full_round` | 1 | 完整轮数（`R_F`） |
| `partial_round` | 1 | 部分轮数（`R_P`） |
| `input` | `input_rate × 32` | 要哈希的域元素 |

这些字段按此顺序拼接后传给预编译，预编译按 Poseidon 论文中的定义计算哈希并返回输出。草案将地址列为 `0xA`，激活区块和 gas 成本留作 `TBD`。

安全部分强调参数的选择很重要，尤其是 MDS 混合矩阵，并指向已发表的 Poseidon 密码分析。

## 示例代码

仅作说明：*假如*预编译存在于草案地址，合约会如何构造输入并调用它。目前并没有这样的预编译上线。

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

library PoseidonDraft {
    address constant POSEIDON = address(0x0A); // draft value, not deployed

    function hash(
        uint256 p, uint16 securityLevel, uint8 alpha, uint8 t,
        uint8 fullRounds, uint8 partialRounds, uint256[] memory inputs
    ) internal view returns (bytes memory out) {
        bytes memory data = abi.encodePacked(
            p, securityLevel, alpha, uint16(inputs.length), t, fullRounds, partialRounds
        );
        for (uint256 i; i < inputs.length; ++i) data = abi.encodePacked(data, inputs[i]);
        (bool ok, bytes memory ret) = POSEIDON.staticcall(data);
        require(ok, "poseidon failed");
        return ret;
    }
}
```

## 需要注意

- **状态：Stagnant。** 该提案并不完整（gas 成本、分叉区块和设计理由均为 `TBD`），也没有被安排进任何升级。目前需要 Poseidon 的合约使用 Solidity 或 Yul 实现。
- 地址 `0xA` 后来被 [EIP-4844](/eip-4844/) 的 KZG 点评估预编译占用，所以这个草案中的地址无法按原样使用。
- Poseidon 比 Keccak 或 SHA-256 年轻得多；它的安全性建立在更新、经受实战检验更少的分析之上，规范本身也承认这一点。
- 可配置的预编译把选择安全参数的责任转移到了调用者身上。


---

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