---
eip: 7812
label: ERC-7812
title: ZK Identity Registry
status: Review
type: Standards Track
category: ERC
created: 2024-11-08
official: https://eips.ethereum.org/EIPS/eip-7812
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7812.md
canonical: https://eips.huatuo.xyz/zh/eip-7812/
lang: zh
tldr: 一个单一的共享链上注册表，应用在其中以稀疏默克尔树存储对私有数据的承诺，使用户之后可以在不泄露数据的前提下，用零知识证明关于该数据的事实。
tags: [zk, identity, privacy, registry, merkle]
---

# ERC-7812: ZK Identity Registry

> Singleton registry system for storing abstract private provable statements.

**TL;DR:** 一个单一的共享链上注册表，应用在其中以稀疏默克尔树存储对私有数据的承诺，使用户之后可以在不泄露数据的前提下，用零知识证明关于该数据的事实。

## 它解决什么问题？

零知识身份和声誉系统（护照证明、出席证明、凭证）往往各自部署合约来存放承诺。这会割裂信任，使证明难以在应用和链之间复用，也迫使每个集成方学习新的格式。

ERC-7812 提出一个无需许可、不可变的 **Evidence Registry**，任何此类系统都可以向其中写入。所有内容都存放在同一个可证明的数据库中，因此一个 32 字节的根就能概括整个注册表，并且可以跨链传递。

## 它如何运作

三种角色：

- **EvidenceDB**：可证明的键值存储（参考实现使用高度为 80、采用 Poseidon 哈希的稀疏默克尔树）。它必须支持添加、更新和删除，并同时提供包含证明和不包含证明。只有注册表可以写入它。
- **EvidenceRegistry**：单例入口。它的 `addStatement`、`updateStatement` 和 `removeStatement` 接收 32 字节的键和值。
- **Registrars**：特定于应用的合约（此处不做标准化），决定声明的含义以及谁可以写入。

要点：

- **命名空间。** 注册表从不直接使用你的键。它以 `isolatedKey = hash(msg.sender, key)` 存储，因此每个 registrar 都有自己的命名空间，其他人无法覆盖它的单元。
- **承诺。** 声明通常用一个私有的“承诺密钥”（盐）进行盲化，因此链上的值不会泄露任何信息。
- **根的历史。** 每次写入都会触发 `RootUpdated(prev, curr)`。`getRootTimestamp(root)` 返回该根被替换的时间；对当前的根返回 `block.timestamp`，对未知的根返回 `0`。
- **字段限制。** 使用 ZK 友好的哈希时，键和值必须小于 BN128 标量域的大小。

ZK 电路（参考实现中是 Circom）证明 `hash(registrar, key) -> value` 在给定公开根下的树中存在或不存在。

## 示例代码

registrar 按规范建议，针对较新的根验证证明：

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

interface IEvidenceRegistry {
    event RootUpdated(bytes32 indexed prev, bytes32 indexed curr);
    function addStatement(bytes32 key, bytes32 value) external;
    function removeStatement(bytes32 key) external;
    function updateStatement(bytes32 key, bytes32 newValue) external;
    function getRootTimestamp(bytes32 root) external view returns (uint256);
    function getIsolatedKey(address source, bytes32 key) external view returns (bytes32);
}

interface IVerifier { function verify(bytes calldata proof, bytes32 root) external view returns (bool); }

contract AgeGate {
    IEvidenceRegistry public immutable registry;
    IVerifier public immutable verifier;
    uint256 constant MAX_ROOT_AGE = 1 hours;

    constructor(IEvidenceRegistry r, IVerifier v) { registry = r; verifier = v; }

    function enter(bytes32 root, bytes calldata proof) external view returns (bool) {
        // Pass the root in; don't read EvidenceDB.getRoot() onchain (front-running).
        uint256 ts = registry.getRootTimestamp(root);
        require(ts != 0 && block.timestamp - ts <= MAX_ROOT_AGE, "stale root");
        return verifier.verify(proof, root);
    }
}
```

## 需要注意

- 状态：**Review**。规范列出了一个通过确定性部署代理部署的规范单例；集成前请在规范中确认地址。
- 该 ERC 有意保持抽象。具体的 registrar（护照、POAP、[ERC-4337](/eip-4337/) 账户身份）预计会以单独的提案出现。
- 访问控制完全是 registrar 的职责；注册表只保证命名空间隔离。
- ZK verifier 需要正确的可信设置；有问题的 verifier 可能接受伪造的证明。
- 请使用调用方传入的根来调用 `getRootTimestamp`，而不是使用实时的根，因为从生成证明到被打包上链之间根可能发生变化。


---

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