以太坊隱私技術深度實作教程:從密碼學基礎到智慧合約部署完整指南
本文提供以太坊隱私技術的深度實作教程,涵蓋密碼學基礎、零知識證明電路設計、circom和Noir框架開發、隱私智慧合約開發、以及實際部署的完整流程。通過大量的程式碼範例和詳細的技術解說,讀者將能夠從零開始構建自己的隱私應用程式。涵蓋有限域運算、Merkle樹驗證電路、隱私代幣、隱私借貸、Aztec Connect整合等完整實作。
以太坊隱私技術深度實作教程:從密碼學基礎到智慧合約部署完整指南
概述
本文提供以太坊隱私技術的深度實作教程,涵蓋密碼學基礎、零知識證明電路設計、隱私智慧合約開發、以及實際部署的完整流程。通過大量的程式碼範例和詳細的技術解說,讀者將能夠從零開始構建自己的隱私應用程式。根據以太坊隱私技術的最新發展,本教程重點介紹circom、Noir等主流zk-SNARK開發框架,以及如何在以太坊上部署真正可用的隱私智慧合約。
隱私技術是以太坊生態系統中最具挑戰性但也最有價值的領域之一。通過零知識證明,用戶可以在不透露具體信息的情況下證明某些陳述的正確性。這種能力為區塊鏈應用開闢了全新的可能性,從隱私交易到身份驗證,從信用評估到投票系統。本教程將帶領讀者深入理解這些技術的底層原理,並通過實際動手項目掌握開發技能。
第一章:密碼學基礎與數學原理
1.1 有限域與橢圓曲線密碼學
理解零知識證明的數學基礎是開發隱私應用的前提。以太坊所使用的密碼學主要基於有限域運算和橢圓曲線密碼學兩個支柱。
有限域基礎:
有限域(Finite Field)是密碼學的核心數學結構。在有限域中,所有運算結果都落在確定的範圍內。對於以太坊所使用的 BN128 曲線,有限域的素數定義為:
p = 21888242871839275222246405745257275088548364400416034343698204186575808495617
這個素數的選擇經過精心設計,確保了域運算的高效性和安全性。
// 使用 JavaScript 進行有限域運算示例
class FiniteField {
constructor(p) {
this.p = BigInt(p);
}
// 模加法
add(a, b) {
return ((a + b) % this.p + this.p) % this.p;
}
// 模減法
sub(a, b) {
return ((a - b) % this.p + this.p) % this.p;
}
// 模乘法
mul(a, b) {
return (a * b) % this.p;
}
// 模指數運算
pow(base, exp) {
let result = 1n;
let b = base % this.p;
let e = exp;
while (e > 0n) {
if (e & 1n) {
result = (result * b) % this.p;
}
e >>= 1n;
b = (b * b) % this.p;
}
return result;
}
// 模逆元(使用擴展歐幾里得算法)
inv(a) {
// Fermat's little theorem: a^(-1) = a^(p-2) mod p
return this.pow(a, this.p - 2n);
}
// 模除法
div(a, b) {
return this.mul(a, this.inv(b));
}
}
// 以太坊 BN128 曲線的素數域
const BN128_FIELD = new FiniteField(
21888242871839275222246405745257275088548364400416034343698204186575808495617n
);
1.2 橢圓曲線運算
以太坊使用的 BN128 曲線是橢圓曲線密碼學的一種實現。曲線方程為:
y² = x³ + 3 (在Fp域上)
// 橢圓曲線點運算
class BN128Curve {
constructor() {
// 曲線參數
this.p = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
this.a = 0n;
this.b = 3n;
// 基點座標
this.Gx = 1n;
this.Gy = 2n;
this.field = new FiniteField(this.p);
}
// 判斷是否為零點
isInfinity(point) {
return point.x === 0n && point.y === 0n;
}
// 曲線上的點加法
add(p1, p2) {
if (this.isInfinity(p1)) return p2;
if (this.isInfinity(p2)) return p1;
const { field } = this;
// 相同點:倍點
if (p1.x === p2.x) {
if (p1.y === p2.y) {
return this.double(p1);
}
// 互為相反數
return { x: 0n, y: 0n };
}
// 不同點:加法
const lambda = field.div(
field.sub(p2.y, p1.y),
field.sub(p2.x, p1.x)
);
const x3 = field.sub(
field.mul(lambda, lambda),
field.add(p1.x, p2.x)
);
const y3 = field.sub(
field.mul(lambda, field.sub(p1.x, x3)),
p1.y
);
return { x: x3, y: y3 };
}
// 點倍增
double(p) {
if (this.isInfinity(p)) return p;
const { field } = this;
// lambda = (3 * x² + a) / (2 * y)
const x2 = field.mul(p.x, p.x);
const numerator = field.add(field.mul(3n, x2), this.a);
const denominator = field.mul(2n, p.y);
const lambda = field.div(numerator, denominator);
const x3 = field.sub(
field.mul(lambda, lambda),
field.mul(2n, p.x)
);
const y3 = field.sub(
field.mul(lambda, field.sub(p.x, x3)),
p.y
);
return { x: x3, y: y3 };
}
// 標量乘法(使用二元展開優化)
scalarMultiply(k, point) {
let result = { x: 0n, y: 0n };
let addend = point;
while (k > 0n) {
if (k & 1n) {
result = this.add(result, addend);
}
addend = this.double(addend);
k >>= 1n;
}
return result;
}
}
1.3 配對與多線性映射
配對(Pairing)是構建高級密碼系統的關鍵技術。在零知識證明中,配對用於驗證多項式承諾和構建可驗證計算。
// 橢圓曲線配對基礎(概念)
class Pairing {
constructor(curve) {
this.curve = curve;
// G1 和 G2 的嵌入度為 12
}
// Tate 配對
tatePairing(p, q) {
// 複雜的數學運算
// 實際實現需要使用 Miller 算法
}
// optimal Ate 配對(更高效)
optimalAtePairing(p, q) {
// 優化版本的配對計算
}
// 配對的雙線性性質
// e(aG, bH) = e(G, H)^(ab)
verifyPairing(g1, h1, g2, h2) {
const left = this.optimalAtePairing(g1, h1);
const right = this.optimalAtePairing(g2, h2);
return left === right;
}
}
第二章:零知識證明系統
2.1 zk-SNARKs 原理
零知識簡潔非交互式論證(Zero-Knowledge Succinct Non-Interactive Arguments of Knowledge,zk-SNARKs)是一種允許一方(證明者)向另一方(驗證者)證明某個陳述為真,而不透露任何額外信息的密碼學協議。
核心組件:
// Solidity 中的配對檢查(使用 BN128)
// 驗證 e(G1, G2) = e(P, Q) 的配對方程
contract PairingChecker {
struct G1Point {
uint256 x;
uint256 y;
}
struct G2Point {
uint256[2] x;
uint256[2] y;
}
// 使用預編譯合約進行配對驗證
// 地址 0x08
function pairingCheck(
G1Point[] memory g1Points,
G2Point[] memory g2Points
) public view returns (bool) {
// 配對檢查實現
// 預編譯合約需要偶數個點對
require(g1Points.length == g2Points.length, "Length mismatch");
require(g1Points.length >= 1, "Need at least one pair");
uint256[24] memory input;
for (uint i = 0; i < g1Points.length; i++) {
uint256 idx = i * 6;
input[idx] = g1Points[i].x;
input[idx + 1] = g1Points[i].y;
input[idx + 2] = g2Points[i].x[0];
input[idx + 3] = g2Points[i].x[1];
input[idx + 4] = g2Points[i].y[0];
input[idx + 5] = g2Points[i].y[1];
}
// 調用預編譯合約
uint256[1] memory result;
assembly {
if iszero(staticcall(sub(gas(), 2000), 8, input, mul(24, 0x20), result, 0x20)) {
revert(0, 0)
}
}
return result[0] == 1;
}
}
2.2 circom 電路開發
circom 是一種用於構建零知識證明電路的領域特定語言(DSL)。它允許開發者以高度抽象的方式描述電路邏輯,然後編譯為可供證明系統使用的格式。
circom 電路基礎語法:
// circom 語法示例:簡單的範圍證明
/*
* 電路:證明輸入 x 在 [0, 2^n) 範圍內
* 這是最基本的範圍證明電路
*/
template RangeProof(n) {
// 宣言輸入信號
signal private input in;
signal output out[n];
// 將輸入轉換為二進制
// 通過約束確保信號正確
var bits[n];
var temp = in;
for (var i = 0; i < n; i++) {
bits[i] <-- temp % 2;
temp \= 2;
// 約束:每個位元只能是 0 或 1
bits[i] * (1 - bits[i]) === 0;
// 約束:正確的重構
out[i] <-- bits[i];
}
// 約束:輸入等於二進制的值
var sum = 0;
for (var i = 0; i < n; i++) {
sum += bits[i] * (1 << i);
}
// 最終約束
in === sum;
}
component main { public [in] } = RangeProof(32);
更複雜的電路:Merkle 樹驗證:
// circom Merkle 樹驗證電路
include "../node_modules/circomlib/poseidon.circom";
template MerkleTreeChecker(levels) {
// 公開輸入:Merkle 根
signal input root;
// 私有輸入:葉子節點
signal input leaf;
// 私有輸入:Merkle 證明路徑
signal input siblings[levels];
signal input pathIndices[levels];
// 中間計算
signal hash[levels + 1][2];
// 初始化:第一層使用葉子
hash[0][0] <== leaf;
// 計算每層的哈希
for (var i = 0; i < levels; i++) {
// 根據路徑索引確定左右順序
signal left;
signal right;
// 如果 pathIndices[i] == 0,則左邊是當前哈希,右邊是兄弟節點
// 否則相反
left <== (1 - pathIndices[i]) * hash[i][0] + pathIndices[i] * siblings[i];
right <== pathIndices[i] * hash[i][0] + (1 - pathIndices[i]) * siblings[i];
// 使用 Poseidon 哈希
component hasher = Poseidon(2);
hasher.inputs[0] <== left;
hasher.inputs[1] <== right;
hash[i + 1][0] <== hasher.out;
}
// 約束:最終哈希等於聲稱的根
root === hash[levels][0];
}
// 實例化:32層 Merkle 樹
component main { public [root] } = MerkleTreeChecker(32);
2.3 Noir 框架開發
Noir 是由 Aztec Labs 開發的零知識證明語言,目標是提供更友好的開發體驗。Noir 的語法類似 Rust,編譯目標包括多個後端證明系統。
Noir 語法示例:
// Noir 語法:簡單的範圍證明
fn main(
x: Field,
y: pub Field
) {
// 斷言:x + y = 10
assert(x + y == 10);
// 斷言:x 在範圍內
assert(x as u8 < 100);
}
// 編譯命令
// nargo compile
// 生成證明
// nargo prove
// 驗證證明
// nargo verify
更完整的 Noir 示例:存款證明:
// Noir 隱私存款證明
use dep::std;
// 承諾結構
struct Commitment {
secret: Field,
nullifier: Field,
}
// Merkle 樹驗證
fn verify_merkle_proof(
leaf: Field,
root: Field,
path_indices: [u8; 32],
siblings: [Field; 32]
) -> bool {
let mut current = leaf;
for i in 0..32 {
let (left, right) = if path_indices[i] == 0 {
(current, siblings[i])
} else {
(siblings[i], current)
};
// 使用 Poseidon 哈希
current = std::hash::poseidon::Hash::hash([left, right]);
}
current == root
}
// 主函數
fn main(
// 私有輸入
secret: Field,
nullifier: Field,
// Merkle 證明
path_indices: [u8; 32],
siblings: [Field; 32],
// 公開輸入
root: Field,
recipient: Field
) {
// 計算存款承諾
let commitment = secret + nullifier * 31337;
let commitment_hash = std::hash::poseidon::Hash::hash([commitment]);
// 驗證 Merkle 證明
assert(verify_merkle_proof(commitment_hash, root, path_indices, siblings));
// 計算提款 nullifier(防止雙花)
let nullifier_hash = std::hash::poseidon::Hash::hash([nullifier]);
// 公開輸出 recipient
std::println(recipient);
}
// 驗證電路
#[test]
fn test_commitment_proof() {
// 測試案例
}
2.4 電路編譯與證明生成
完整的 circom 編譯流程:
#!/bin/bash
# 電路編譯腳本
# 1. 安裝 circom
npm install -g circom
# 2. 編譯電路
circom my_circuit.circom --r1cs --wasm --sym --c
# 3. 生成 trusted setup(僅首次需要)
# 使用 snarkjs 進行 powers of tau
snarkjs powersoftau new bn128 16 pot16_0000.ptau -v
snirkjs powersoftau contribute pot16_0000.ptau pot16_0001.ptau --name="First contribution" -v -e="random entropy"
snarkjs powersoftau prepare phase2 pot16_0001.ptau pot16_final.ptau -v
# 4. 生成 zkey(電路特定)
snarkjs groth16 setup my_circuit.r1cs pot16_final.ptau my_circuit_0000.zkey
snarkjs zkey contribute my_circuit_0000.zkey my_circuit_0001.zkey --name="Contributor 1" -v -e="more entropy"
snarkjs zkey export verificationkey my_circuit_0001.zkey verification_key.json
# 5. 計算 witness
node my_circuit_js/generate_witness.js my_circuit_js/my_circuit.wasm input.json witness.wtns
# 6. 生成證明
snarkjs groth16 prove my_circuit_0001.zkey witness.wtns proof.json public.json
# 7. 驗證證明
snarkjs groth16 verify verification_key.json public.json proof.json
JavaScript 證明生成示例:
// 使用 snarkjs 生成證明
const snarkjs = require("snarkjs");
const fs = require("fs");
async function generateProof(input) {
// 加載電路
const { wasm } = await import("./my_circuit_js/my_circuit.wasm");
const { groth16 } = snarkjs;
// 計算 witness
const witness = await wasm.calculateWTNSBin(input, 0);
// 生成證明
const [proof, publicSignals] = await groth16.fullProve(
witness,
"./my_circuit_0001.zkey",
"./my_circuit_js/my_circuit.wasm"
);
return { proof, publicSignals };
}
// 驗證證明
async function verifyProof(proof, publicSignals) {
const vKey = JSON.parse(fs.readFileSync("verification_key.json"));
return await snarkjs.groth16.verify(
vKey,
publicSignals,
proof
);
}
// 示例使用
async function main() {
const input = {
// 私有輸入
secret: "123456789",
// 公開輸入
root: "987654321"
};
const { proof, publicSignals } = await generateProof(input);
const isValid = await verifyProof(proof, publicSignals);
console.log("Proof valid:", isValid);
}
main();
第三章:隱私智慧合約開發
3.1 隱私代幣標準
ERC-20 代幣的隱私版本需要實現存款和提款的隱私保護機制。
// 隱私代幣合約框架
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
/**
* @title PrivacyToken
* @dev 實現隱私保護的代幣合約
*
* 設計特點:
* 1. 存款時生成加密承諾
* 2. 提款時使用零知識證明
* 3. 支援多個匿名集
* 4. 內建合規檢查接口
*/
contract PrivacyToken is ERC20, ReentrancyGuard {
// Merkle 樹根(用於驗證存款)
mapping(bytes32 => bool) public roots;
bytes32 public currentRoot;
// 承諾映射(防止雙重存款)
mapping(bytes32 => bool) public commitments;
// Nullifier 映射(防止雙重提款)
mapping(bytes32 => bool) public nullifierHashes;
// 儲備證明
uint256 public denominator = 10 ** 18;
// 事件
event Deposit(
bytes32 indexed commitment,
bytes32 indexed root,
uint256 leafIndex,
address depositor
);
event Withdrawal(
address indexed recipient,
bytes32 indexed nullifierHash,
address indexed relayer,
uint256 fee
);
constructor(
string memory name,
string memory symbol
) ERC20(name, symbol) {
// 初始化 Merkle 樹
}
/**
* @dev 存款函數
* @param commitment 存款承諾(keccak256(secret, nonce))
*/
function deposit(bytes32 commitment)
external
payable
nonReentrant
{
require(msg.value >= 0.1 ether, "Minimum deposit is 0.1 ETH");
require(!commitments[commitment], "Commitment already exists");
// 記錄承諾
commitments[commitment] = true;
// 插入 Merkle 樹
uint256 leafIndex = insert(uint256(commitment));
// 更新當前根
currentRoot = getLastRoot();
emit Deposit(commitment, currentRoot, leafIndex, msg.sender);
}
/**
* @dev 提款函數
* @param proof 零知識證明
* @param root Merkle 樹根
* @param nullifierHash Nullifier 哈希
* @param recipient 接收者地址
* @param relayer 中繼器地址(可選)
* @param fee 中繼費用
*/
function withdraw(
bytes calldata proof,
bytes32 root,
bytes32 nullifierHash,
address payable recipient,
address payable relayer,
uint256 fee
) external nonReentrant {
// 1. 驗證根是否有效
require(roots[root], "Invalid Merkle root");
// 2. 驗證 nullifier 未被使用
require(!nullifierHashes[nullifierHash], "Already withdrawn");
// 3. 驗證零知識證明
require(
verifyProof(proof, root, nullifierHash, recipient),
"Invalid proof"
);
// 4. 標記 nullifier 為已使用
nullifierHashes[nullifierHash] = true;
// 5. 轉移資金
require(fee < msg.value, "Fee exceeds withdrawal amount");
if (fee > 0) {
relayer.transfer(fee);
}
recipient.transfer(msg.value - fee);
emit Withdrawal(recipient, nullifierHash, relayer, fee);
}
/**
* @dev 驗證零知識證明
* @dev 實際部署需要調用 ZK 驗證合約
*/
function verifyProof(
bytes calldata proof,
bytes32 root,
bytes32 nullifierHash,
address recipient
) internal pure returns (bool) {
// 簡化版本:實際需要驗證 groth16 或 plonk 證明
// 這裡需要調用專門的驗證合約
return proof.length > 0;
}
/**
* @dev Merkle 樹操作(簡化版本)
*/
function insert(uint256 leaf) internal returns (uint256) {
// 實際實現需要完整的 Merkle 樹合約
return 0;
}
function getLastRoot() internal view returns (bytes32) {
return currentRoot;
}
}
3.2 隱私借貸協議
隱私借貸協議允許用戶在不暴露具體餘額的情況下進行借貸操作。
// 隱私借貸合約框架
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/**
* @title PrivacyLending
* @dev 隱私借貸協議
*
* 用戶可以:
* 1. 存入資產並獲得隱藏的存款餘額
* 2. 借款時證明有足夠抵押而不暴露具體金額
* 3. 還款後保持借款歷史的隱私
*/
contract PrivacyLending is ERC20 {
// 存款承諾
struct DepositCommitment {
bytes32 commitment;
uint256 amount;
bool withdrawn;
}
mapping(bytes32 => DepositCommitment) public deposits;
// 借款記錄
struct Loan {
bytes32 collateralCommitment;
uint256 borrowedAmount;
uint256 interestRate;
uint256 startTime;
bool repaid;
}
mapping(address => Loan[]) public loans;
// 借款因子
uint256 public constant COLLATERAL_FACTOR = 150; // 150%
// 事件
event DepositMade(
bytes32 indexed commitment,
uint256 amount,
address depositor
);
event Borrowed(
address indexed borrower,
uint256 amount,
bytes32 proof
);
event Repaid(
address indexed borrower,
uint256 amount,
uint256 interest
);
/**
* @dev 隱私存款
* @param commitment 存款承諾 keccak256(secret, nonce)
*/
function deposit(bytes32 commitment) external payable {
require(msg.value > 0, "Cannot deposit 0");
deposits[commitment] = DepositCommitment({
commitment: commitment,
amount: msg.value,
withdrawn: false
});
emit DepositMade(commitment, msg.value, msg.sender);
}
/**
* @dev 隱私借款
* @param proof 零知識證明(證明抵押足夠)
* @param collateralCommitment 抵押承諾
* @param amount 借款金額
*/
function borrow(
bytes calldata proof,
bytes32 collateralCommitment,
uint256 amount
) external {
// 1. 驗證抵押承諾存在
require(
deposits[collateralCommitment].amount > 0,
"Invalid collateral"
);
// 2. 驗證零知識證明
// 證明:存款金額 * COLLATERAL_FACTOR >= 借款金額
require(
verifyBorrowProof(
proof,
collateralCommitment,
amount
),
"Invalid proof"
);
// 3. 計算利息
uint256 interest = calculateInterest(amount);
// 4. 記錄借款
loans[msg.sender].push(Loan({
collateralCommitment: collateralCommitment,
borrowedAmount: amount,
interestRate: 500, // 5% 年化
startTime: block.timestamp,
repaid: false
}));
// 5. 轉移借款資金
payable(msg.sender).transfer(amount);
emit Borrowed(msg.sender, amount, keccak256(proof));
}
/**
* @dev 還款
*/
function repay(uint256 loanIndex) external payable {
require(loans[msg.sender].length > loanIndex, "Invalid loan");
Loan storage loan = loans[msg.sender][loanIndex];
require(!loan.repaid, "Already repaid");
uint256 totalDue = loan.borrowedAmount + calculateInterest(
loan.borrowedAmount,
loan.startTime
);
require(msg.value >= totalDue, "Insufficient repayment");
loan.repaid = true;
// 處理多餘的資金
if (msg.value > totalDue) {
payable(msg.sender).transfer(msg.value - totalDue);
}
emit Repaid(msg.sender, loan.borrowedAmount, totalDue - loan.borrowedAmount);
}
/**
* @dev 計算利息
*/
function calculateInterest(uint256 amount) internal view returns (uint256) {
// 5% 年化利率
return (amount * 500 * (block.timestamp - block.timestamp)) / (10000 * 365 days);
}
function calculateInterest(uint256 amount, uint256 startTime)
internal
view
returns (uint256)
{
uint256 duration = block.timestamp - startTime;
return (amount * 500 * duration) / (10000 * 365 days);
}
/**
* @dev 驗證借款證明
*/
function verifyBorrowProof(
bytes calldata proof,
bytes32 collateralCommitment,
uint256 amount
) internal pure returns (bool) {
// 實際實現需要 ZK 驗證
// 這裡是簡化版本
return proof.length > 0;
}
}
3.3 與 Aztec Connect 整合
Aztec Network 是以太坊上主要的隱私 Rollup 解決方案,提供原生的隱私交易支持。
// Aztec Connect 整合示例
// SPDX-License-Identifier: GPL-2.0
pragma solidity ^0.8.19;
interface IAztecBridge {
function claim(
address asset,
uint256 amount,
bytes calldata proof
) external;
}
/**
* @title AztecPrivacyIntegration
* @dev 展示如何與 Aztec Connect 整合實現隱私交易
*/
contract AztecPrivacyIntegration {
IAztecBridge public bridge;
address public rollupProcessor;
// 映射 ERC20 代幣到 Aztec 資產 ID
mapping(address => uint256) public assetIds;
constructor(address _bridge, address _rollupProcessor) {
bridge = IAztecBridge(_bridge);
rollupProcessor = _rollupProcessor;
// 初始化常見代幣的 asset ID
assetIds[address(0)] = 0; // ETH
}
/**
* @dev 存款到 Aztec 隱私rollup
* @param asset 代幣地址(address(0) 為 ETH)
* @param amount 數量
* @param proof 存款證明
*/
function depositToAztec(
address asset,
uint256 amount,
bytes calldata proof
) external payable {
uint256 assetId = assetIds[asset];
// 處理 ETH 存款
if (asset == address(0)) {
require(msg.value == amount, "Invalid ETH amount");
} else {
// 處理 ERC20 存款
require(msg.value == 0, "Unexpected ETH");
// 轉移代幣到合約
}
// 調用 Aztec Bridge 進行存款
bridge.claim(asset, amount, proof);
}
/**
* @dev 從 Aztec 提款
* @param asset 代幣地址
* @param amount 數量
* @param proof 提款證明
*/
function withdrawFromAztec(
address asset,
uint256 amount,
bytes calldata proof
) external {
// 驗證提款證明
// 實際實現需要完整的驗證邏輯
// 轉移資金給用戶
if (asset == address(0)) {
payable(msg.sender).transfer(amount);
} else {
// 轉移 ERC20 代幣
}
}
}
第四章:實際部署指南
4.1 本地開發環境設置
#!/bin/bash
# 隱私應用開發環境設置腳本
# 1. 安裝 Node.js 和 npm
# https://nodejs.org/
# 2. 安裝 Hardhat
npm init -y
npm install --save-dev hardhat
# 3. 安裝依賴
npm install @openzeppelin/contracts
npm install ethers@5.7.0
npm install circomlib@2.0.5
npm install snarkjs
# 4. 安裝 circom
npm install -g circom
# 5. 初始化 Hardhat 項目
npx hardhat init
# 6. 配置 hardhat.config.js
cat > hardhat.config.js << 'EOF'
require("@nomicfoundation/hardhat-toolbox");
require("circom");
module.exports = {
solidity: "0.8.19",
networks: {
localhost: {
url: "http://127.0.0.1:8545",
},
sepolia: {
url: process.env.SEPOLIA_URL,
accounts: [process.env.PRIVATE_KEY],
},
},
};
EOF
# 7. 編譯電路
# 在 circuits 目錄下放置 .circom 文件
# 執行編譯
circom circuits/my_circuit.circom --r1cs --wasm --sym
# 8. 運行本地網絡
npx hardhat node
4.2 測試網部署示例
// hardhat 測試腳本
const { ethers } = require("hardhat");
async function main() {
// 部署者
const [deployer, user1, user2] = await ethers.getSigners();
console.log("Deploying contracts with account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());
// 部署隱私代幣
const PrivacyToken = await ethers.getContractFactory("PrivacyToken");
const privacyToken = await PrivacyToken.deploy(
"Privacy ETH",
"PETH"
);
await privacyToken.deployed();
console.log("PrivacyToken deployed to:", privacyToken.address);
// 部署 Merkle 樹合約
const MerkleTree = await ethers.getContractFactory("MerkleTree");
const merkleTree = await MerkleTree.deploy(32); // 32 層
await merkleTree.deployed();
console.log("MerkleTree deployed to:", merkleTree.address);
// 測試存款
console.log("\nTesting deposit...");
// 生成承諾
const secret = ethers.utils.randomBytes(32);
const nonce = ethers.utils.randomBytes(32);
const commitment = ethers.utils.keccak256(
ethers.utils.concat([secret, nonce])
);
// 存款
const depositTx = await privacyToken.deposit(commitment, {
value: ethers.utils.parseEther("1.0")
});
await depositTx.wait();
console.log("Deposit successful!");
// 獲取 Merkle 根
const root = await merkleTree.getLastRoot();
console.log("Merkle Root:", root);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
4.3 主網部署檢查清單
部署前檢查清單:
□ 安全審計
□ 智慧合約代碼審計
□ ZK 電路審計
□ 前端代碼審計
□ 第三方依賴審計
□ 測試覆蓋
□ 單元測試 > 90%
□ 集成測試
□ 模糊測試
□ 形式化驗證(如適用)
□ 經濟模型審查
□ 代幣經濟學設計
□ 費率設定
□ 激勵機制
□ 合規檢查
□ KYC/AML 政策
□ 旅行規則合規
□ 數據隱私合規
□ 運營準備
□ 監控系統部署
□ 備份策略
□ 緊急響應計劃
□ 社區準備
□ 文檔齊全
□ Bug bounty 計劃
□ 社區支持渠道
第五章:進階應用場景
5.1 去中心化身份與隱私
// 隱私身份驗證合約
pragma solidity ^0.8.19;
/**
* @title PrivacyIdentity
* @dev 基於零知識證明的隱私身份系統
*
* 用戶可以證明:
* - 年齡 > 18 歲(不透露具體年齡)
* - 來自特定國家(不透露具體位置)
* - 持有特定 credentials(不透露內容)
*/
contract PrivacyIdentity {
// 身份承諾
struct IdentityCommitment {
bytes32 commitment;
uint256 timestamp;
bool revoked;
}
mapping(bytes32 => IdentityCommitment) public commitments;
// 信任Issuer清單
mapping(address => bool) public trustedIssuers;
// 事件
event IdentityRegistered(
bytes32 indexed commitment,
address indexed issuer,
uint256 timestamp
);
event IdentityVerified(
address indexed user,
bytes32 indexed commitment,
bytes32 indexed schema
);
/**
* @dev 註冊身份承諾
* @param commitment 身份承諾
* @param issuer 信任的 issuer
*/
function registerIdentity(
bytes32 commitment,
address issuer
) external {
require(trustedIssuers[issuer], "Untrusted issuer");
commitments[commitment] = IdentityCommitment({
commitment: commitment,
timestamp: block.timestamp,
revoked: false
});
emit IdentityRegistered(commitment, issuer, block.timestamp);
}
/**
* @dev 驗證年齡(零知識)
* @param proof 年齡證明
* @param commitment 身份承諾
*/
function verifyAge(
bytes calldata proof,
bytes32 commitment,
uint256 minAge
) external returns (bool) {
require(!commitments[commitment].revoked, "Identity revoked");
// 驗證 ZK 證明
// 證明:用戶年齡 >= minAge
bool valid = verifyZKProof(proof, commitment, minAge);
if (valid) {
emit IdentityVerified(
msg.sender,
commitment,
keccak256("age")
);
}
return valid;
}
function verifyZKProof(
bytes calldata proof,
bytes32 commitment,
uint256 minAge
) internal pure returns (bool) {
// 實際實現
return proof.length > 0;
}
}
5.2 隱私投票系統
// 隱私投票合約
pragma solidity ^0.8.19;
/**
* @title PrivacyVoting
* @dev 實現隱私投票功能
*
* 特點:
* 1. 投票選項隱藏
* 2. 投票權重隱藏
* 3. 防止雙重投票
* 4. 結果可驗證
*/
contract PrivacyVoting {
// 投票議案
struct Proposal {
string description;
bytes32 merkleRoot; // 候選人 Merkle 樹根
uint256 startTime;
uint256 endTime;
uint256 voteCount;
bool executed;
}
Proposal[] public proposals;
// 投票記錄(防止雙重投票)
mapping(bytes32 => bool) public hasVoted;
// 結果承諾(投票結束後揭露)
mapping(uint256 => bytes32) public resultCommitments;
// 事件
event ProposalCreated(
uint256 indexed proposalId,
string description,
uint256 startTime,
uint256 endTime
);
event VoteCommitted(
uint256 indexed proposalId,
bytes32 voteCommitment
);
event VoteRevealed(
uint256 indexed proposalId,
bytes32 vote,
uint256 weight
);
/**
* @dev 創建投票議案
*/
function createProposal(
string memory description,
bytes32 merkleRoot,
uint256 votingPeriod
) external returns (uint256) {
uint256 proposalId = proposals.length;
proposals.push(Proposal({
description: description,
merkleRoot: merkleRoot,
startTime: block.timestamp,
endTime: block.timestamp + votingPeriod,
voteCount: 0,
executed: false
}));
emit ProposalCreated(
proposalId,
description,
block.timestamp,
block.timestamp + votingPeriod
);
return proposalId;
}
/**
* @dev 提交投票(隱藏階段)
* @param proposalId 議案ID
* @param voteCommitment 投票承諾 keccak256(vote, secret)
*/
function commitVote(
uint256 proposalId,
bytes32 voteCommitment
) external {
require(proposalId < proposals.length, "Invalid proposal");
Proposal storage proposal = proposals[proposalId];
require(
block.timestamp >= proposal.startTime &&
block.timestamp <= proposal.endTime,
"Voting not active"
);
// 防止雙重投票
bytes32 voteKey = keccak256(
abi.encode(proposalId, msg.sender)
);
require(!hasVoted[voteKey], "Already voted");
hasVoted[voteKey] = true;
emit VoteCommitted(proposalId, voteCommitment);
}
/**
* @dev 揭露投票(驗證階段)
*/
function revealVote(
uint256 proposalId,
bytes32 vote,
bytes32 secret,
uint256 weight
) external {
bytes32 voteKey = keccak256(
abi.encode(proposalId, msg.sender)
);
require(hasVoted[voteKey], "Has not voted");
// 驗證承諾
bytes32 commitment = keccak256(abi.encode(vote, secret));
// 這裡需要驗證 vote 是有效的候選人
// 並驗證 weight
Proposal storage proposal = proposals[proposalId];
proposal.voteCount += weight;
emit VoteRevealed(proposalId, vote, weight);
}
}
結論
本文提供了以太坊隱私技術的完整實作教程,從密碼學基礎到實際部署,涵蓋了開發隱私應用所需的核心知識。通過大量的程式碼範例,讀者可以快速掌握circom、Noir等電路開發工具的使用,並能夠構建自己的隱私智慧合約。
隱私技術仍在快速發展中,新的證明系統和優化方案持續湧現。建議開發者持續關注以太坊隱私生態的最新進展,並積極參與社區討論。同時,在開發隱私應用時,務必重視安全審計和合規要求,確保應用的長期可靠性和合規性。
參考資源
- circom 文檔:https://docs.circom.io/
- Noir 文檔:https://noir-lang.org/
- snarkjs GitHub:https://github.com/iden3/snarkjs
- Aztec Network 文檔:https://aztec.network/
- OpenZeppelin 合約:https://openzeppelin.com/contracts/
相關文章
- Aztec Network 完整開發指南:從隱私交易原理到實際應用部署 — Aztec Network是以太坊生態系統中最重要的隱私保護解決方案之一,通過結合零知識證明(zkSNARKs)和匯總技術(zkRollup),為以太坊提供了可擴展的隱私交易能力。本文深入分析Aztec的技術架構、隱私機制原理、隱私代幣標準、集成開發指南、以及安全最佳實踐。詳細介紹Pedersen Commitments、zkSNARKs證明電路、Mixer協議等核心技術,提供完整的隱私ERC-20合約代碼、隱私NFT標準、以及與DeFi協議集成的實作範例。同時探討隱私與合規的平衡策略,幫助開發者構建隱私保護的DeFi應用和企業級解決方案。
- 以太坊隱私池與 DeFi 整合最新發展完整指南:2025-2026 技術實踐與監管趨勢 — 本文深入分析以太坊隱私池技術的最新發展,涵蓋技術架構演進、合規框架成熟、主要協議比較以及隱私池在 DeFi 借貸、交易、質押等場景的實際應用。我們特別關注 Aztec Network、Tornado Cash、Railgun 等主流隱私協議的 2025-2026 年技術升級與市場表現,同時分析亞洲市場的監管動態。
- ZK-SNARK 數學原理與以太坊實作完整指南:從理論到部署的工程實踐 — 本文深入探討 ZK-SNARK(零知識簡潔非交互式論證)的數學原理,提供完整的 Circom 代碼範例,並詳細講解如何在以太坊上部署零知識證明電路。內容涵蓋有限域運算、橢圓曲線密碼學、配對基礎、多項式承諾等密碼學基礎知識,以及完整的電路開發流程和智慧合約部署實踐。
- 隱私合約開發實務:從密碼學原理到 Noir 程式設計完整指南 — 隱私是以太坊生態系統中最具挑戰性也最被低估的技術領域之一。本指南從密碼學原理出發,深入探討如何在以太坊上構建真正保護用戶隱私的智慧合約。我們將詳細分析各種隱私技術的優缺點,並提供基於 Noir 語言的完整實作範例,幫助開發者從理論到實踐全面掌握隱私合約開發技術。
- 以太坊隱私技術實際應用案例與合規框架深度分析:2026 年產業現況、技術實現與監管趨勢 — 本文深入探討以太坊隱私技術的實際應用案例與合規框架,涵蓋 Tornado Cash、Aztec Network、Railgun、隱私池等主流協議的技術實現。分析零知識證明在隱私保護中的應用,提供合規友好的隱私設計模式,並探討隱私借貸、私有 DEX、隱私穩定幣等新興應用場景。包含 2026 年產業數據與監管趨勢分析。
延伸閱讀與來源
- Ethereum.org 以太坊官方入口
- EthHub 以太坊知識庫
這篇文章對您有幫助嗎?
請告訴我們如何改進:
評論
發表評論
注意:由於這是靜態網站,您的評論將儲存在本地瀏覽器中,不會公開顯示。
目前尚無評論,成為第一個發表評論的人吧!