---
eip: 665
label: EIP-665
title: Add precompiled contract for Ed25519 signature verification
status: Stagnant
type: Standards Track
category: Core
created: 2018-03-25
official: https://eips.ethereum.org/EIPS/eip-665
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-665.md
canonical: https://eips.huatuo.xyz/zh/eip-665/
lang: zh
tldr: 提议增加一个廉价的预编译合约，用于在 EVM 中验证 Ed25519 签名，使合约能够检查来自 SSH、DNSSEC、TLS 以及基于 Ed25519 的区块链的签名。
tags: [precompile, cryptography, ed25519, signatures, core]
---

# EIP-665: Add precompiled contract for Ed25519 signature verification

> Support performant and cheap verification of Ed25519 cryptographic signatures in smart contracts in general by adding a precompiled contract for Ed25519 signature verification to the EVM.

**TL;DR:** 提议增加一个廉价的预编译合约，用于在 EVM 中验证 Ed25519 签名，使合约能够检查来自 SSH、DNSSEC、TLS 以及基于 Ed25519 的区块链的签名。

## 它解决什么问题？

以太坊原生只能验证 secp256k1 签名（通过 `ecrecover`）。而外部世界的很多系统使用 **Ed25519** 签名，包括 OpenSSH 密钥、DNSSEC、TLS 证书、OpenPGP、软件签名工具以及若干权益证明区块链。用普通 EVM 字节码也能验证 Ed25519，但它的宽字长运算与 EVM 不太匹配，因此 gas 成本非常高。

预编译可以让合约：

- 证明同一个人既控制某个以太坊账户，又控制某个链下的 Ed25519 密钥（服务器、域名、SSH 身份），
- 接受由任意发送者转发的、经 Ed25519 签名的数据，
- 验证来自基于 Ed25519 的链的区块头或消息。

## 它如何运作

该提案增加了一个预编译 `ED25519VFY`：

| 项目 | 值 |
| --- | --- |
| 地址 | `0x09` |
| 输入 | 128 字节：32 字节消息、32 字节公钥、64 字节签名 |
| 输出 | 4 字节：有效时为 `0x00000000`，无效时为任意非零值 |
| Gas | 2000（比 `ecrecover` 的 3000 便宜，因为 Ed25519 更快） |

与 `ecrecover` 不同，公钥必须作为输入传入，因为 Ed25519 无法从签名中恢复出公钥。返回零表示成功，因此不同的非零代码可以表示不同的失败原因。

为避免客户端之间出现共识错误，作者建议所有客户端使用 **libsodium**（或其绑定），并指出了 IETF 和 NaCl 的测试向量。Geth、Parity、pyethereum 和 cpp-ethereum 都已提交了实现的 PR。

## 示例代码

合约**本来会**如何调用它。这只是假设。在主网上，地址 `0x09` 是另一个预编译，因此这段代码无法工作。

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

library Ed25519Proposed {
    address constant ED25519VFY = address(0x09); // 仅为 EIP-665 的提案内容

    function verify(bytes32 message, bytes32 pubKey, bytes memory sig)
        internal view returns (bool)
    {
        require(sig.length == 64, "bad sig length");
        (bool ok, bytes memory out) =
            ED25519VFY.staticcall(abi.encodePacked(message, pubKey, sig));
        return ok && out.length == 4 && bytes4(out) == bytes4(0);
    }
}
```

## 需要注意

- 该提案处于 **Stagnant** 状态，从未被采纳。以太坊主网没有 Ed25519 预编译。
- 它所申请的地址 `0x09` 后来被分配给了 BLAKE2 压缩函数（[EIP-152](/eip-152/)）。
- 消息输入固定为 32 字节，因此调用方通常会对哈希签名，而不是对任意长度的数据签名。
- 其他曲线的预编译后来确实上线了：BLS12-381 的 [EIP-2537](/eip-2537/)，以及 secp256r1（P-256，passkey 使用）的 [EIP-7951](/eip-7951/)。没有预编译时，在 Solidity 中验证 Ed25519 是可行的，但成本很高。


---

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