ERC-6327 · Draft(草案) · ERC · 2023-01-13
ERC-6327: Elastic Signature
一个基于密码的"弹性签名"草案接口:用户用零知识证明来证明自己知道一个可更改的秘密,由合约验证,作为私钥之上的额外签名因素。
它解决什么问题?
私钥对非技术用户很难:它不能更改,记不住,丢了就等于失去一切。托管式密钥服务让入门更容易,但要依赖供应商。
ERC-6327 提议一种弹性签名(ES):用一个对人友好的秘密(比如密码)来授权某项操作,用户可以随时更改这个秘密。密码从不被存储或泄露。用户生成的是一份证明自己知道它的零知识证明,由验证者合约在链上检查该证明。
规范把 ES 定位为额外的因素,而不是对密钥的替代:例如,即使钱包密钥被攻破,DeFi 应用也要求输入密码才能提款,或是用作 ERC-4337 智能账户的密码插件。
它如何运作
三个角色和几个哈希:
pwdhash= 密码的哈希(与用户地址绑定);由验证者存储在链上。datahash= 被授权操作的哈希,由请求者合约生成。fullhash=datahash加上若干已知值的哈希,其中应包含每个用户的nonce和chainid(参考实现还混入了expiration)。allhash=pwdhash和fullhash的哈希。
流程:
- 证明者(用户的客户端)根据密码、地址和
fullhash计算allhash和 zk-SNARK 的proof。 - 请求者合约在验证者上调用
verify(user, proof, datahash, expiration, allhash)。 - 验证者重新计算
fullhash,查出pwdhash,针对公开值[pwdhash, fullhash, allhash]检查证明,然后递增用户的 nonce 并触发Verified。
resetPassword 通过要求同时提供旧秘密和新秘密的证明来更改密码。参考电路使用 Poseidon 哈希(circom/snarkjs)。
示例代码
一个以弹性签名控制提款的请求者合约:
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
interface IElasticSignature {
event SetPassword(address indexed user, uint indexed pwdhash);
event Verified(address indexed user, uint indexed nonce);
function pwdhashOf(address user) external view returns (uint);
function verify(address user, uint[8] memory proof, uint datahash,
uint expiration, uint allhash) external;
}
contract PasswordVault {
IElasticSignature public immutable es;
mapping(address => uint256) public balance;
constructor(IElasticSignature _es) { es = _es; }
function withdraw(uint256 amount, uint[8] calldata proof,
uint expiration, uint allhash) external {
// datahash binds the proof to this exact action
uint datahash = uint(keccak256(abi.encode(address(this), msg.sender, amount)));
es.verify(msg.sender, proof, datahash, expiration, allhash); // reverts if invalid
balance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
}
}
需要注意
- 状态为 Draft;接口和哈希构造仍可能变化。
pwdhash是公开的,所以密码可以被离线暴力破解。规范自己的估算显示,短密码在 GPU 上很快就会被破解;只有非常长、高熵的秘密才接近私钥的强度。- nonce 防止证明被重放;
fullhash中的链 ID 防止跨链重用。证明者和验证者必须就fullhash和datahash的构造方式完全一致。 - 证明生成在客户端进行,需要电路产物;链上验证者由该电路生成,所以两者必须匹配。