zk-merkle-tree 中文文档教程
zk-merkle-tree
使用零知识证明在以太坊区块链上进行匿名投票的 JavaScript 库。
该库基于Tornado Cash的源代码。 TC 最基本的组成部分是 Merkle 树,用户可以通过随机承诺
存入以太币,并可以通过nullifier
撤回。无效者被分配给承诺,但没有人知道哪个承诺被分配给哪个无效者,因为它们之间的联系是零知识。此方法也可用于匿名投票,即投票者在注册阶段发送承诺,并在投票时发送无效者。这种方法确保一个选民只能投票一次。
欲了解更多信息,请阅读我在 Medium 上关于该库的文章。
用法
创建您自己的投票合约,该合约继承自 zk-merkle-tree/contracts/ZKTree.sol
。
实现 _commit
和 _nullify
方法。
ZKTree 合约的简单实现如下所示(来自 zktree-vote 项目):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "zk-merkle-tree/contracts/ZKTree.sol";
contract ZKTreeVote is ZKTree {
address public owner;
mapping(address => bool) public validators;
mapping(uint256 => bool) uniqueHashes;
uint numOptions;
mapping(uint => uint) optionCounter;
constructor(
uint32 _levels,
IHasher _hasher,
IVerifier _verifier,
uint _numOptions
) ZKTree(_levels, _hasher, _verifier) {
owner = msg.sender;
numOptions = _numOptions;
for (uint i = 0; i <= numOptions; i++) optionCounter[i] = 0;
}
function registerValidator(address _validator) external {
require(msg.sender == owner, "Only owner can add validator!");
validators[_validator] = true;
}
function registerCommitment(
uint256 _uniqueHash,
uint256 _commitment
) external {
require(validators[msg.sender], "Only validator can commit!");
require(
!uniqueHashes[_uniqueHash],
"This unique hash is already used!"
);
_commit(bytes32(_commitment));
uniqueHashes[_uniqueHash] = true;
}
function vote(
uint _option,
uint256 _nullifier,
uint256 _root,
uint[2] memory _proof_a,
uint[2][2] memory _proof_b,
uint[2] memory _proof_c
) external {
require(_option <= numOptions, "Invalid option!");
_nullify(
bytes32(_nullifier),
bytes32(_root),
_proof_a,
_proof_b,
_proof_c
);
optionCounter[_option] = optionCounter[_option] + 1;
}
function getOptionCounter(uint _option) external view returns (uint) {
return optionCounter[_option];
}
}
构造函数有 4 个参数:
- < code>_levels 是 Merkle 树的级别。 如果您使用默认的验证器,则必须为 20。如果您想使用不同数量的级别,则必须实现自己的验证器电路。
_hasher
是MiMC海绵智能合约的地址。 (检查test
文件夹中的MiMC海绵生成器代码。)_verifier
是Verifier合约的地址。它是由准备脚本 (scripts/preapre.sh
) 从验证器 (Circuits/Verifier.circom
) 电路生成的。_numOptions
是选项的数量。
registerCommitment
方法实现 _commit
方法。它有 2 个参数:
_uniqueHash
是选民的唯一哈希值(例如:身份证的哈希值)。它确保一名选民仅登记一次。_commitment
是用户的承诺。
vote
方法实现了 _nullify
方法。它有 6 个参数:
_option
是投票者选择的选项。_nullifier
是承诺的无效符。_root
是证明的 Merkle 根。_proof_a
、_proof_b
和_proof_c
是零知识证明。
承诺和无效符是通过generateCommitment
方法在客户端生成的。 (请检查 zktree-vote 中的 VoterRegistration 组件
项目。)
要生成零知识证明,请使用 calculateMerkleRootAndZKProof
。 (请检查zktree-vote中的投票组件
项目。)
有关更多信息,请检查存储库中的 test
文件夹和 zktree-投票项目。
警告:该库未经审核,因此使用它的风险由您自行承担。
zk-merkle-tree
A JavaScript library for anonymous voting on Ethereum blockchain using zero-knowledge proof.
The library is based on the source code of Tornado Cash. The most essential component of TC is a Merkle tree where users can deposit ethers with a random commitment
, that can be withdrawn with a nullifier
. The nullifier is assigned to the commitment, but nobody knows which commitment is assigned to which nullifier, because the link between them is the zero-knowledge. This method can be also used for anonymous voting, where the voter sends a commitment in the registration phase, and a nullifier when she votes. This method ensures that one voter can vote only once.
For more info, please read my article on Medium about the library.
Usage
Create your own voting contract that is inherited from zk-merkle-tree/contracts/ZKTree.sol
.
Implement the _commit
and _nullify
methods.
A simple implementation of the ZKTree contract looks like this (from the zktree-vote project):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "zk-merkle-tree/contracts/ZKTree.sol";
contract ZKTreeVote is ZKTree {
address public owner;
mapping(address => bool) public validators;
mapping(uint256 => bool) uniqueHashes;
uint numOptions;
mapping(uint => uint) optionCounter;
constructor(
uint32 _levels,
IHasher _hasher,
IVerifier _verifier,
uint _numOptions
) ZKTree(_levels, _hasher, _verifier) {
owner = msg.sender;
numOptions = _numOptions;
for (uint i = 0; i <= numOptions; i++) optionCounter[i] = 0;
}
function registerValidator(address _validator) external {
require(msg.sender == owner, "Only owner can add validator!");
validators[_validator] = true;
}
function registerCommitment(
uint256 _uniqueHash,
uint256 _commitment
) external {
require(validators[msg.sender], "Only validator can commit!");
require(
!uniqueHashes[_uniqueHash],
"This unique hash is already used!"
);
_commit(bytes32(_commitment));
uniqueHashes[_uniqueHash] = true;
}
function vote(
uint _option,
uint256 _nullifier,
uint256 _root,
uint[2] memory _proof_a,
uint[2][2] memory _proof_b,
uint[2] memory _proof_c
) external {
require(_option <= numOptions, "Invalid option!");
_nullify(
bytes32(_nullifier),
bytes32(_root),
_proof_a,
_proof_b,
_proof_c
);
optionCounter[_option] = optionCounter[_option] + 1;
}
function getOptionCounter(uint _option) external view returns (uint) {
return optionCounter[_option];
}
}
The constructor has 4 parameters:
_levels
is the levels of the Merkle tree. It has to be 20 if you use the default Verifier. If you want to use different number of levels, you have to implement your own Verifier circuit._hasher
is the address of the MiMC sponge smart contract. (Check thetest
folder for the MiMC sponge generator code.)_verifier
is the address of the Verifier contract. It is generated from the Verifier (circuits/Verifier.circom
) circuit by the prepare script (scripts/preapre.sh
)._numOptions
is the number of options.
The registerCommitment
method implements the _commit
method. It has 2 parameters:
_uniqueHash
is a unique hash of the voter (ex.: the hash of the ID card). It ensures that one voter is registered only once._commitment
is the commitment of the user.
The vote
method implements the _nullify
method. It has 6 parameters:
_option
is the option what the voter chooses._nullifier
is the nullifier for the commitment._root
is the Merkle root for the proof._proof_a
,_proof_b
and_proof_c
are the zero-knowledge proof.
The commitment and the nullifier is generated on the client side by the generateCommitment
method. (Please check the VoterRegistration component in the zktree-vote
project.)
To generate the zero-knowledge proof, use calculateMerkleRootAndZKProof
. (Please check the Vote component in the zktree-vote
project.)
For more info, please check the test
folder in the repository and the zktree-vote project.
WARNING: This library is not audited, so use it at your own risk.