---
eip: 3068
label: EIP-3068
title: Precompile for BN256 HashToCurve Algorithms
status: Stagnant
type: Standards Track
category: Core
created: 2020-10-23
requires: 198, 1108
official: https://eips.ethereum.org/EIPS/eip-3068
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3068.md
canonical: https://eips.huatuo.xyz/zh/eip-3068/
lang: zh
tldr: 一项已停滞的提案：新增把任意消息哈希到 BN256（alt_bn128）曲线 G1 和 G2 群上的预编译，使链上 BLS 签名验证变得便宜。
tags: [core, precompile, cryptography, bls, bn254, stagnant]
---

# EIP-3068: Precompile for BN256 HashToCurve Algorithms

> This EIP defines a hash-to-curve precompile for use in BN256 and would allow for cheaper BLS signature verification.

**TL;DR:** 一项已停滞的提案：新增把任意消息哈希到 BN256（alt_bn128）曲线 G1 和 G2 群上的预编译，使链上 BLS 签名验证变得便宜。

## 它解决什么问题？

以太坊已经有针对 BN256 曲线（也叫 alt_bn128 或 BN254）的预编译：点加法、标量乘法（[EIP-196](/eip-196/)）和配对检查（[EIP-197](/eip-197/)），并由 [EIP-1108](/eip-1108/) 降低了成本。这些足以验证 SNARK。

然而，要验证**针对任意消息的 BLS 签名**，首先需要把消息映射到曲线上的一个点（hash-to-curve）。没有预编译的话，这只能用 Solidity 完成，而据规范所述，它消耗的 gas 与配对检查本身大致相当，尽管计算量要少一个数量级。这阻碍了例如验证分布式密钥生成所产生的群签名（规范以 ETHDKG 为例）等用途。

## 它如何运作

该提案规定了两个确定性函数：

- **`HashToG1(msg)`**
  1. `HashToBase` 使用两个带有域分隔前缀的 Keccak-256 哈希把消息转换为一个域元素，并对域的素数取模后合并。用不同的前缀这样做两次。
  2. `BaseToG1` 使用 Fouque–Tibouchi 方法（含 Wahby–Boneh 的改进）把每个域元素映射到曲线上。它以常数时间的方式从三个候选的 `x` 值中选出一个，并计算 `y`。
  3. 把两个点相加；其和即为结果。
- **`HashToG2(msg)`** 在二次扩域上做同样的事情，落在扭曲曲线上。它使用四个域元素，把两个扭曲点相加，再乘以余因子到达 G2。

根据与 6000 gas 的 ECMUL 价格对比的基准测试推算出的 gas 估算：

| 运算 | 提议的 gas |
| --- | --- |
| HashToG1 | `8500 + len(msg)` |
| HashToG2 | `80000 + 3 * len(msg)` |

## 示例代码

规范中 hash-to-base 步骤的伪代码：

```python
def hash_to_base(msg: bytes, dsp1: int, dsp2: int, p: int) -> int:
    h0 = int.from_bytes(keccak256(bytes([dsp1]) + msg), "big")
    h1 = int.from_bytes(keccak256(bytes([dsp2]) + msg), "big")
    c = pow(2, 256, p)                 # 2^256 mod p
    return (h0 * c + h1) % p           # ~512-bit hash reduced mod p

def hash_to_g1(msg: bytes):
    t0 = hash_to_base(msg, 0x00, 0x01, P)
    t1 = hash_to_base(msg, 0x02, 0x03, P)
    return ec_add(base_to_g1(t0), base_to_g1(t1))
```

随后 BLS 验证如何使用现有的配对预编译（`0x08`）：检查 `e(sig, g2) == e(HashToG1(msg), pubkey)`，它可以作为 `e(-sig, g2) * e(H(msg), pubkey) == 1` 传给配对预编译。

## 需要注意

- 状态：**Stagnant**。没有分配地址，Test Cases 和 Implementation 部分均为"TBD"。
- 规范警告说，BN256 的安全性因较新的数域筛攻击而降低。估算各不相同，较保守的估计约为 100 位，而不是原先的 128 位。这影响该曲线所有基于配对的用途，而不仅是这个 EIP。
- 作者指出，把 `sign0` 替换为 `is_square` 将与原论文完全一致；`sign0` 是否保持与随机预言机的不可区分性尚未得到证明。
- 对于需要在以太坊上使用 BLS 的新设计，[EIP-2537](/eip-2537/) 新增了 BLS12-381 预编译，其中包括 map-to-curve 运算。
- 作者披露了其与 MadHive 的关联，这正是该提案的动机所在。


---

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