零知識證明系統與以太坊整合完整技術指南:從理論到實際部署
本文從密碼學理論出發,深入分析各類零知識證明系統(ZK-SNARK、ZK-STARK、Bulletproofs)的數學原理,並提供在以太坊上實際部署的完整技術指南。涵蓋 Groth16、PLONK、FRI 協議、Circom/Noir 電路開發、以及 zkSync/Starknet 應用實例。
零知識證明系統與以太坊整合完整技術指南:從理論到實際部署
概述
零知識證明(Zero-Knowledge Proof, ZKP)是現代密碼學中最具革命性的技術之一,其核心理念允許「證明者」在不透露任何敏感資訊的情況下,向「驗證者」證明某個陳述的正確性。以太坊生態系統將零知識證明視為實現隱私保護和可擴展性的核心技術手段。本文從密碼學理論出發,深入分析各類零知識證明系統(ZK-SNARK、ZK-STARK、Bulletproofs)的數學原理,並提供在以太坊上實際部署的完整技術指南。
截至 2026 年第一季度,零知識證明技術已在以太坊生態中處理超過 15 億筆 Layer 2 交易,ZK-Rollup 的總鎖定價值(TVL)超過 200 億美元,成為區塊鏈產業最重要的技術創新之一。
第一章:零知識證明密碼學基礎
1.1 形式化定義與核心特性
零知識證明系統由三個概率多項式時間(PPT)算法構成:
定義 1.1.1(零知識證明系統)
三元組 (Setup, Prove, Verify) 構成零知識證明系統:
1. Setup(1^λ, C) → (pp, vp)
- 輸入:安全參數 λ,電路 C(描述要證明的計算)
- 輸出:公共參數 pp,驗證金鑰 vp
2. Prove(pp, x, w) → π
- 輸入:公共參數 pp,公共輸入 x,私有見證 w
- 輸出:證明 π
3. Verify(vp, x, π) → {accept, reject}
- 輸入:驗證金鑰 vp,公共輸入 x,證明 π
- 輸出:接受(accept)或拒絕(reject)
定義 1.1.2(零知識性的三個核心特性)
1. 完整性(Completeness):
若 (x, w) ∈ R(正確的輸入和見證),則
Pr[Verify(vp, x, Prove(pp, x, w)) = accept] = 1
2. 可靠性(Soundness):
若 x ∉ L(錯誤的陳述),則對於任何 PPT 證明者 P*:
Pr[Verify(vp, x, P*(vp, x)) = accept] ≤ ε(λ)
3. 零知識性(Zero-Knowledge):
存在模擬器 Sim,使得:
{Sim(vp, x)} ≈ {Prove(pp, x, w)}
即驗證者無法從證明中獲取任何關於 w 的資訊
1.2 交互式與非交互式證明
交互式零知識證明(iZKP)
交互式協議透過多輪挑戰-回應機制實現零知識性:
┌──────────────┐ ┌──────────────┐
│ 證明者 │ │ 驗證者 │
│ (P) │ │ (V) │
└──────┬───────┘ └──────┬───────┘
│ │
│──── Commitment ──────────▶│ 承諾階段
│ │ 證明者發送初始承諾
│ │
│◀─── Challenge c ─────────│ 挑戰階段
│ │ 驗證者發送隨機挑戰
│ │
│──── Response r ──────────▶│ 回應階段
│ │ 證明者計算回應
│ │
│◀─── Verify ──────────────│ 驗證階段
│ │ 驗證者決定接受/拒絕
▼ ▼
Fiat-Shamir 啟發式轉換
將交互式證明轉換為非交互式(NIP)版本:
def fiat_shamir_transform(prover_func, hash_func):
"""
Fiat-Shamir 啟發式:交互式 → 非交互式
原理:使用密碼學哈希函數模擬驗證者的隨機種子
"""
def non_interactive_prove(public_input, witness):
# 步驟 1:計算初始承諾
commitment = compute_commitment(public_input, witness)
# 步驟 2:使用哈希模擬挑戰
challenge = hash_func(
public_input || commitment
)
# 步驟 3:計算回應
response = compute_response(
public_input, witness, challenge
)
# 步驟 4:輸出非交互式證明
return {
'commitment': commitment,
'challenge': challenge,
'response': response
}
return non_interactive_prove
def hash_func(data):
"""Keccak-256 哈希函數"""
import hashlib
return int.from_bytes(
hashlib.new('keccak256', data).digest()[:32],
'big'
)
1.3 知識誤差與安全性分析
定義 1.3.1(知識可靠性)
知識可靠性確保:如果證明者能夠說服驗證者,
則必然「知道」某個符合條件的見證。
形式化定義:
存在提取器 Ext,使得:
Ext^P*(vp, x) ≈ {w : C(x, w) = 1}
其中 Ext 可以與 P* 進行多項式次數的交互。
知識誤差(Knowledge Error)
def calculate_repetition_for_security(
security_parameter, # λ(如 128 位安全)
knowledge_error, # ε(單次證明的知識誤差)
):
"""
計算需要重複多少次證明才能達到目標安全等級
公式:ε^k < 2^(-λ)
其中 k 是重複次數
解:k > λ / (-log₂ ε)
"""
import math
k = math.ceil(
security_parameter / (-math.log2(knowledge_error))
)
return k
# 示例:SNARKs 單次知識誤差約 ε = 1/2
# 需達到 128 位安全
k = calculate_repetition_for_security(128, 0.5)
print(f"需要重複次數: {k}")
# 輸出: 1(因為 log₂(1/2) = -1)
# 示例:STARKs 單次知識誤差 ε ≈ 1/128
k = calculate_repetition_for_security(128, 1/128)
print(f"需要重複次數: {k}")
# 輸出: 128(因為 log₂(128) = 7,所以 128/7 ≈ 18...)
# 實際 STARKs 使用哈希函數碰撞,效率更高
第二章:ZK-SNARKs 深度技術分析
2.1 算術電路與約束系統
ZK-SNARKs 的第一步是將待證明的計算轉換為算術電路。
定義 2.1.1(算術電路)
算術電路 C(x, w) = 0 由以下元素構成:
- 輸入線:對應公共輸入 x 和私有見證 w
- 加法門:c = a + b
- 乘法門:c = a × b
- 常數門:c = k
電路滿足條件:C(x, w) = 0 ⟺ 陳述成立
R1CS(Rank-1 Constraint System)約束
R1CS 是最廣泛使用的約束系統形式:
class R1CSConstraint:
def __init__(self, a, b, c):
"""
R1CS 約束:<a, variables> × <b, variables> = <c, variables>
其中 variables 是所有變數的向量
"""
self.a = a # 左向量
self.b = b # 右向量
self.c = c # 輸出向量
def check(self, assignment):
"""
驗證約束是否滿足
"""
a_val = sum(a * v for a, v in zip(self.a, assignment))
b_val = sum(b * v for b, v in zip(self.b, assignment))
c_val = sum(c * v for c, v in zip(self.c, assignment))
return a_val * b_val == c_val
# 示例:電路 x × y = z 的 R1CS 表示
# 假設變數順序:[one, x, y, z, ...]
constraint = R1CSConstraint(
a = [0, 1, 0, 0, ...], # x
b = [0, 0, 1, 0, ...], # y
c = [0, 0, 0, 1, ...], # z
)
# 驗證:當 x=3, y=4, z=12 時約束成立
assignment = [1, 3, 4, 12]
print(constraint.check(assignment)) # True
2.2 多項式承諾
多項式承諾是構建高效 SNARKs 的核心密碼學原語。
定義 2.2.1(多項式承諾)
多項式承諾允許:
1. 證明者對多項式 f(x) 生成承諾 com(f)
2. 證明者可以打開承諾,證明 f(z) = y
3. 驗證者可以驗證證明
承諾具有隱藏性:無法從 com(f) 推斷 f
承諾具有綁定性:無法將 com(f) 偽造成 com(g),g ≠ f
KZG 承諾(Kate 承諾)
KZG 承諾是最常用的多項式承諾方案:
import numpy as np
class KZGCommitment:
def __init__(self, G1, G2, pairing):
"""
KZG 承諾系統
參數:
- G1, G2: 橢圓曲線群
- pairing: 雙線性配對
"""
self.G1 = G1
self.G2 = G2
self.pairing = pairing
def trusted_setup(self, degree, secret):
"""
可信設定:生成結構化參考字串 (SRS)
SRS = [g^τ, g^τ², ..., g^τ^n, g^τ]
其中 τ 是秘密隨機值(設定後需銷毀)
"""
tau = secret # 秘密值
powers_of_tau = []
for i in range(degree + 1):
powers_of_tau.append(self.G1 * (tau ** i))
# 第二個群元素用於驗證
g_tau = self.G2 * tau
return {
'powers': powers_of_tau,
'g_tau': g_tau
}
def commit(self, polynomial, srs):
"""
對多項式生成承諾
Commitment = g^{f(τ)} = ∏(g^{τ^i})^{coeff_i}
"""
commitment = self.G1 * 0 # 單位元
for i, coeff in enumerate(polynomial):
commitment = commitment + srs['powers'][i] * coeff
return commitment
def open(self, polynomial, point, srs):
"""
在指定點打開多項式,生成證明
計算:π = g^{q(τ)}
其中 q(x) = f(x) - f(z) / (x - z)
"""
f_z = self.evaluate(polynomial, point)
# 構造商多項式 f(x) - f(z)
diff = [coeff for coeff in polynomial]
diff[0] -= f_z
# 構造除數 (x - z)
divisor = [-point, 1] # x - z
# 多項式除法
quotient = self.polynomial_divide(diff, divisor)
# 承諾商多項式
proof = self.commit(quotient, srs)
return {
'value': f_z,
'proof': proof
}
def verify(self, commitment, opening, point, srs):
"""
驗證開口的正確性
驗證方程:
e(π, g^τ - z·g) = e(C - f(z)·g, g)
"""
g = self.G1.generator()
g_tau = srs['g_tau']
# 計算左邊
left = self.pairing(
opening['proof'],
g_tau - g * point
)
# 計算右邊
C_minus_fz = commitment - self.G1 * opening['value']
right = self.pairing(C_minus_fz, g)
return left == right
@staticmethod
def evaluate(polynomial, point):
""" Horner 法多項式求值 """
result = 0
for coeff in reversed(polynomial):
result = result * point + coeff
return result
@staticmethod
def polynomial_divide(numerator, denominator):
"""多項式長除法"""
# 簡化實現
if len(denominator) == 0:
raise ValueError("除數不能為零")
result = [0] * (len(numerator) - len(denominator) + 1)
remainder = numerator.copy()
for i in range(len(result) - 1, -1, -1):
if len(remainder) >= len(denominator):
coeff = remainder[-1] / denominator[-1]
result[i] = coeff
for j in range(len(denominator)):
idx = i + j
if idx < len(remainder):
remainder[idx] -= coeff * denominator[j]
remainder.pop()
return result
2.3 Groth16 協議
Groth16 是目前最廣泛使用的 SNARK 協議。
協議流程
class Groth16:
@staticmethod
def keygen(r1cs, lambda_secret):
"""
金鑰生成
1. 將 R1CS 轉換為 QAP(Quadratic Arithmetic Program)
2. 選擇秘密 τ
3. 計算 [α], [β], [γ], [δ], [τ^i]
"""
# 計算 R1CS → QAP 轉換(省略詳細實現)
A = compute_A_polynomials(r1cs)
B = compute_B_polynomials(r1cs)
C = compute_C_polynomials(r1cs)
# 可信設定
tau = lambda_secret
alpha = random_scalar()
beta = random_scalar()
gamma = random_scalar()
delta = random_scalar()
# 計算 proving key 和 verification key
proving_key = {
'A': [G1 * A_i(tau) for A_i in A],
'B': [G2 * B_i(tau) for B_i in B],
'C': [G1 * C_i(tau) for C_i in C],
'H': [G1 * tau**i for i in range(len(A))],
'L': [G1 * beta * A_i(tau) + alpha * B_i(tau) + C_i(tau)]
}
verification_key = {
'alpha': G1 * alpha,
'beta': G2 * beta,
'gamma': G2 * gamma,
'delta': G2 * delta,
'IC': [G1 * (beta * A_i(tau) + alpha * B_i(tau)) / gamma
for A_i in A]
}
return proving_key, verification_key
@staticmethod
def prove(proving_key, public_input, private_witness):
"""
證明生成
A = Σ a_i(τ) × variables_i
B = Σ b_i(τ) × variables_i
C = Σ c_i(τ) × variables_i
π = [A·B - C] / δ + H(τ)·δ
"""
variables = [1] + public_input + private_witness
# 計算 A, B, C
A = sum(
proving_key['A'][i] * variables[i]
for i in range(len(variables))
)
B = sum(
proving_key['B'][i] * variables[i]
for i in range(len(variables))
)
# 計算線性組合 L
L = sum(
proving_key['L'][i] * variables[len(public_input)+1+i]
for i in range(len(private_witness))
)
# 計算 H
ABC = A * B - L
H = ABC / proving_key['delta']
return {
'A': A,
'B': B,
'C': H
}
@staticmethod
def verify(verification_key, public_input, proof):
"""
驗證證明
e(A, B) = α·β + Σ IC_i × public_input_i / γ + C/δ
"""
left = pairing(proof['A'], proof['B'])
IC_sum = sum(
verification_key['IC'][i] * public_input[i]
for i in range(len(public_input))
)
right = (
pairing(verification_key['alpha'], verification_key['beta']) +
pairing(IC_sum, verification_key['gamma']) +
pairing(proof['C'], verification_key['delta'])
)
return left == right
Groth16 效率分析
證明大小:183 bytes(2 G1 + 1 G2 元素)
驗證時間:3 個配對運算
證明時間:O(n),n 為電路大小
| 電路大小 | 證明時間 | 驗證時間 | 證明大小 |
|---------|---------|---------|---------|
| 1,000 gates | ~100ms | ~5ms | 183 bytes |
| 100,000 gates | ~10s | ~5ms | 183 bytes |
| 1,000,000 gates | ~100s | ~5ms | 183 bytes |
2.4 PLONK 協議
PLONK(Permutations over Lagrange-bases for Oecumenical Noninteractive arguments of Knowledge)使用通用可信設定。
PLONK 約束系統
class PLONKConstraint:
def __init__(self, num_wires, num_constraints):
self.num_wires = num_wires
self.num_constraints = num_constraints
def generate_selector_polynomials(self, constraints):
"""
為加法和乘法門生成選擇器多項式
q_L × a + q_R × b + q_M × a×b + q_O × c + q_C = 0
其中:
- q_L, q_R, q_M, q_O, q_C 是選擇器
- a, b, c 是線的複製
"""
q_L = [0] * self.num_constraints
q_R = [0] * self.num_constraints
q_M = [0] * self.num_constraints
q_O = [0] * self.num_constraints
q_C = [0] * self.num_constraints
for i, constraint in enumerate(constraints):
if constraint['type'] == 'add':
q_L[i] = 1
q_R[i] = 1
q_O[i] = -1
elif constraint['type'] == 'mul':
q_M[i] = 1
q_O[i] = -1
return {
'q_L': self.interpolate(q_L),
'q_R': self.interpolate(q_R),
'q_M': self.interpolate(q_M),
'q_O': self.interpolate(q_O),
'q_C': self.interpolate(q_C),
}
def copy_constraints(self, copies):
"""
複製約束:強制某些線相等
使用置換論證實現
"""
permutation = self.build_permutation(copies)
return permutation
@staticmethod
def interpolate(values):
"""拉格朗日插值"""
# 簡化實現
return values # 實際應使用 FFT
PLONK 與 Groth16 比較
| 特性 | Groth16 | PLONK |
|---------------|----------------|----------------|
| 可信設定 | 電路特定 | 通用 |
| 證明大小 | 183 bytes | ~400 bytes |
| 驗證時間 | 3 pairings | 4 pairings |
| 透明度 | 需要 CRS | 部分透明 |
| 更新性 | 不可更新 | 可更新 |
| 用途 | 固定電路 | 多電路 |
第三章:ZK-STARKs 深度技術分析
3.1 STARKs 的核心特性
ZK-STARKs(Zero-Knowledge Scalable Transparent Arguments of Knowledge)是 STARKs 的零知識版本。
STARKs 的核心優勢:
1. 透明性(Transparency):
- 不需要可信設定
- 使用公開隨機性(Public Randomness)
- 任何人都可驗證
2. 後量子安全(Post-Quantum Security):
- 僅依賴哈希函數
- 不依賴橢圓曲線或配對
- 可抵禦量子計算機攻擊
3. 擴展性(Scalability):
- 驗證時間 O(log n)
- 證明時間 O(n log n)
3.2 FRI 協議(Fast Reed-Solomon IOP)
FRI 是 STARKs 中用於多項式承諾的核心協議。
class FRIProtocol:
def __init__(self, field, expansion_factor=4):
self.field = field
self.expansion_factor = expansion_factor
def commit(self, polynomial, domain_size):
"""
對多項式生成承諾
將多項式在擴展域上取值
"""
extended_domain = self.field.get_extended_domain(domain_size)
values = [polynomial(x) for x in extended_domain]
return {
'domain': extended_domain,
'values': values,
'merkle_root': self.merkle_commit(values)
}
def prove_low_degree(self, polynomial, max_degree):
"""
證明多項式的度低於 max_degree
使用遞歸壓縮:
f₀(x) → (f₀(ωx), f₀(-ωx)) → f₁(x) → ...
"""
current_poly = polynomial
proofs = []
current_domain = self.field.base_domain
while len(current_poly) > max_degree + 1:
# 將當前多項式一分為二
f_even, f_odd = self.split_even_odd(current_poly)
# 隨機 challenges
challenge = self.field.random()
# 構造下一層多項式
# f'(x) = f_even(x²) + x × f_odd(x²)
# 或使用不同的組合方式
next_poly = self.combine(f_even, f_odd, challenge)
# 證明者發送 commitment
commitment = self.commit(next_poly, len(next_poly))
proofs.append({
'f_even_commit': self.commit(f_even, len(f_even)),
'f_odd_commit': self.commit(f_odd, len(f_odd)),
'challenge': challenge,
'next_commit': commitment
})
current_poly = next_poly
return proofs
def verify_low_degree(self, merkle_proofs, challenges, max_degree):
"""
驗證低度數證明
檢查最終多項式的度是否足夠低
"""
final_poly_degree = len(merkle_proofs[-1]['next_commit']) - 1
# 最終多項式度需 ≤ max_degree / (expansion_factor^depth)
expected_degree = max_degree // (self.expansion_factor ** len(merkle_proofs))
return final_poly_degree <= expected_degree
@staticmethod
def split_even_odd(polynomial):
"""將多項式分為偶數項和奇數項"""
f_even = [0] * ((len(polynomial) + 1) // 2)
f_odd = [0] * (len(polynomial) // 2)
for i, coeff in enumerate(polynomial):
if i % 2 == 0:
f_even[i // 2] = coeff
else:
f_odd[i // 2] = coeff
return f_even, f_odd
@staticmethod
def combine(f_even, f_odd, alpha):
"""組合偶數項和奇數項"""
result = [0] * (len(f_even) + len(f_odd) - 1)
for i, coeff in enumerate(f_even):
result[i] += coeff
for i, coeff in enumerate(f_odd):
result[i] += coeff * alpha
return result
3.3 STARKs 與 SNARKs 比較
| 特性 | Groth16 | PLONK | STARKs |
|------------------|----------------|----------------|----------------|
| 證明大小 | 183 bytes | ~400 bytes | ~100-500 KB |
| 驗證時間 | ~5ms | ~5ms | ~50ms |
| 證明時間 | O(n) | O(n log n) | O(n log n) |
| 可信設定 | 需要(電路特定)| 需要(通用) | 不需要 |
| 後量子安全 | 否 | 否 | 是 |
| 透明性 | 否 | 部分 | 完全 |
| 適用場景 | 固定電路 | 多電路 | 大型計算 |
第四章:以太坊上的零知識證明應用
4.1 ZK-Rollup 架構
ZK-Rollup 將大量交易批次打包,在 Layer 2 執行計算,生成簡潔的零知識證明,提交到以太坊主網。
class ZKRollup:
def __init__(self, state_tree, zk_prover):
self.state_tree = state_tree
self.prover = zk_prover
def create_batch(self, transactions):
"""
建立交易批次
批次包含:
1. 壓縮後的交易列表
2. 新狀態的 Merkle 根
3. 零知識證明
"""
# 執行交易並計算新狀態
old_state = self.state_tree.get_root()
new_state, process_logs = self.execute_transactions(transactions)
# 構造電路輸入
circuit_input = {
'old_state_root': old_state,
'new_state_root': new_state,
'transactions': self.compress_transactions(transactions),
'public_data': self.extract_public_data(process_logs)
}
# 生成零知識證明
proof = self.prover.prove(circuit_input)
return {
'state_root': new_state,
'proof': proof,
'public_data_hash': self.hash_public_data(process_logs),
'batch_number': self.get_next_batch_number()
}
def verify_on_ethereum(self, batch, verification_key):
"""
在以太坊上驗證 ZK 證明
調用 Verifier 合約
"""
verifier = self.get_verifier_contract()
# 調用合約驗證
is_valid = verifier.verify(
verification_key,
batch['proof'],
[batch['state_root'], batch['public_data_hash']]
)
return is_valid
@staticmethod
def execute_transactions(transactions):
"""在 Layer 2 執行交易"""
# 實現交易執行邏輯
new_state = {}
logs = []
for tx in transactions:
result = execute_tx(tx)
new_state.update(result.state_changes)
logs.extend(result.logs)
return new_state, logs
@staticmethod
def compress_transactions(transactions):
"""
壓縮交易以降低 Layer 1 成本
使用增量編碼和範圍壓縮
"""
compressed = []
for tx in transactions:
compressed_tx = {
'from': compress_pubkey(tx.from),
'to': compress_pubkey(tx.to),
'nonce': delta_encode(tx.nonce),
'value': rlp_encode(tx.value),
'data': rlp_encode(tx.data),
'gas': compress_gas(tx.gas)
}
compressed.append(compressed_tx)
return compressed
4.2 zkSync Era 技術分析
zkSync Era 是以太坊上最流行的 ZK-Rollup 實現之一。
// zkSync Era 簡化版Verifier合約
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract ZKSyncVerifier {
// 主驗證函數
function verifyProof(
uint256[] memory proof,
uint256[] memory pubSignals
) public view returns (bool) {
// 驗證公共輸入
require(pubSignals.length >= 2, "Invalid signals");
// 獲取狀態根
uint256 stateRoot = pubSignals[0];
uint256 publicDataHash = pubSignals[1];
// 調用 VK 驗證
return _verifyGroth16(proof, pubSignals);
}
// Groth16 驗證(簡化)
function _verifyGroth16(
uint256[] memory proof,
uint256[] memory pubSignals
) internal view returns (bool) {
// 配對檢查
// e(A, B) = e(α, β) × ∏e(IC[i], γ) × e(C, δ)
(uint256 a_x, uint256 a_y) = (proof[0], proof[1]);
(uint256 b_x0, uint256 b_x1, uint256 b_y0, uint256 b_y1) =
(proof[2], proof[3], proof[4], proof[5]);
(uint256 c_x, uint256 c_y) = (proof[6], proof[7]);
// 執行配對運算
return _pairingCheck(
a_x, a_y, b_x0, b_x1, b_y0, b_y1,
c_x, c_y, pubSignals
);
}
}
4.3 Starknet 技術分析
Starknet 使用 STARKs 而非 SNARKs,實現了完全透明性和後量子安全。
# Starknet Cairo 程式範例
# 計算累加和(帶 ZK 證明)
# Cairo 程式碼
cairo_code = """
%builtins output pedersen range_check
from starkware.cairo./common.cairo import (
alloc_locals, verify_zero, local felt: felt, local x: felt
)
func main{output_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
alloc_locals;
local a: felt;
local b: felt;
local c: felt;
// 讀取三個輸入
[output_ptr] = a;
output_ptr = output_ptr + 1;
[output_ptr] = b;
output_ptr = output_ptr + 1;
[output_ptr] = c;
output_ptr = output_ptr + 1;
// 約束:c = a + b
[output_ptr] = c - a - b;
output_ptr = output_ptr + 1;
// 返回
return ();
}
func main_wrapper{output_ptr, pedersen_ptr, range_check_ptr}() {
// 呼叫 main
%{
ids.a = 3
ids.b = 5
ids.c = 8
%}
call main;
return ();
}
"""
# 編譯並生成證明
def compile_cairo_program(code):
"""編譯 Cairo 程式"""
compiled = cairo_compile(code)
return compiled
def generate_proof(compiled_program, inputs):
"""生成 STARK 證明"""
# Cairo 執行跟蹤
trace = cairo_execute(compiled_program, inputs)
# 生成約束系統
constraints = generate_constraints(trace)
# 使用 FRI 證明低度數
fri_proof = fri_prove(constraints)
# 組合最終證明
return {
'trace': trace,
'fri_proof': fri_proof,
'public_inputs': inputs,
'output': execute_and_get_output(trace)
}
4.4 隱私交易的零知識證明
// 簡化版隱私轉帳合約(使用 zkSNARK)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract PrivacyToken {
// Merkle 樹根
bytes32 public merkleRoot;
// 已使用的 nullifier(防止雙花)
mapping(bytes32 => bool) public usedNullifiers;
// 存款事件
event Deposit(
bytes32 indexed commitment,
bytes32 indexed leafIndex,
uint32 timestamp
);
// 提款事件
event Withdrawal(
address indexed recipient,
bytes32 nullifierHash,
address indexed relayer,
uint256 fee
);
// 存款
function deposit(bytes32 _commitment) external payable {
// 計算 leaf 在 Merkle 樹中的位置
uint32 leafIndex = _insert(uint256(_commitment));
emit Deposit(_commitment, leafIndex, block.timestamp);
}
// 提款(需要零知識證明)
function withdraw(
bytes32 _root,
bytes32 _nullifierHash,
address payable _recipient,
address payable _relayer,
uint256 _fee,
uint256 _refund,
// ZK 證明
uint256[2] memory a,
uint256[2] memory a_p,
uint256[2][2] memory b,
uint256[2] memory b_p,
uint256[2] memory c,
uint256[2] memory h,
uint256[4] memory inputs // [root, nullifierHash, recipient, relayer]
) external {
// 驗證零知識證明
require(
verifier.verifyProof(
a, a_p, b, b_p, c, h, inputs
),
"Invalid ZK proof"
);
// 驗證 Merkle 根
require(merkleRoot == bytes32(inputs[0]), "Invalid root");
// 驗證 nullifier 未使用
require(!usedNullifiers[bytes32(inputs[1])], "Already spent");
// 標記 nullifier 為已使用
usedNullifiers[bytes32(inputs[1])] = true;
// 轉帳
(bool success, ) = _recipient.call{value: msg.value - _fee - _refund}("");
require(success, "Transfer failed");
// 支付 relayer 費用
if (_fee > 0) {
(success, ) = _relayer.call{value: _fee}("");
require(success, "Fee transfer failed");
}
emit Withdrawal(
_recipient,
bytes32(inputs[1]),
_relayer,
_fee
);
}
}
第五章:開發實務指南
5.1 Circom 電路開發
Circom 是最流行的 ZK-SNARKs 電路描述語言:
// circom 電路示例:範圍證明
pragma circom 2.0.0;
include "../node_modules/circomlib/circuits/comparators.circom";
template RangeProof(n) {
// n 位範圍證明:證明 a ∈ [min, max]
signal input a;
signal input min;
signal input max;
signal output out;
component lowerBound = LessEqThan(n);
component upperBound = LessEqThan(n);
// 檢查 a >= min
// 轉換:a >= min ⟺ min <= a
lowerBound.in[0] <== min;
lowerBound.in[1] <== a;
// 檢查 a <= max
// 轉換:a <= max ⟺ a <= max
upperBound.in[0] <== a;
upperBound.in[1] <== max;
// 輸出:同時滿足兩個條件
out <== lowerBound.out * upperBound.out;
}
template IsZero(n) {
signal input in;
signal output out;
component isZero = IsZero();
isZero.in <== in;
out <== isZero.out;
}
template AssertNotEqual() {
signal input a;
signal input b;
// 強制 a ≠ b
a - b === 1;
}
template PrivateTransfer(n) {
// 私密轉帳電路
signal input amount;
signal input balance;
signal input nullifier;
signal input salt;
// 公開輸入
signal input root;
signal input newCommitment;
// 輸出
signal output nullifierHash;
signal output commitment;
// 1. 驗證餘額 >= 金額
component sufficientBalance = GreaterThanOrEqual(n);
sufficientBalance.in[0] <== balance;
sufficientBalance.in[1] <== amount;
sufficientBalance.out === 1;
// 2. 計算 nullifier hash
// nullifierHash = Hash(nullifier, salt)
nullifierHash <== Poseidon(2)([nullifier, salt]);
// 3. 計算新 commitment
// commitment = Hash(amount, nullifier)
commitment <== Poseidon(2)([amount, nullifier]);
// 4. 驗證 commitment 匹配新 commitment
commitment === newCommitment;
}
component main {public [root, newCommitment]} = PrivateTransfer(64);
5.2 Noir 程式設計
Noir 是 Aztec 開發的 ZK 語言,具有 Rust 語法:
// Noir 程式示例:簡單餘額檢查
use dep::std;
fn main(
amount: Field,
balance: Field,
secret: Field,
nullifier: Field,
// 公開輸入
root: Field,
new_commitment: Field
) -> pub [Field; 4] {
// 驗證 balance >= amount
assert(balance >= amount);
// 計算 nullifier hash
let nullifier_hash = std::hash::pedersen_hash([nullifier, secret]);
// 計算新 commitment
let new_commit = std::hash::pedersen_hash([amount, nullifier_hash]);
// 驗證 commitment
assert(new_commit == new_commitment);
// 返回公共信號
[root, new_commit, nullifier_hash, amount]
}
// 使用枚舉進行條件邏輯
fn conditional_transfer<
T,
N,
>(
condition: bool,
then_branch: Fn[() -> T],
else_branch: Fn[() -> T]
) -> T {
if condition {
then_branch()
} else {
else_branch()
}
}
// 使用結構體組織複雜資料
struct PrivateAccount {
balance: Field,
secret: Field,
nullifier: Field,
}
impl PrivateAccount {
fn new(balance: Field, secret: Field, nullifier: Field) -> Self {
Self { balance, secret, nullifier }
}
fn verify_transfer(self, amount: Field) {
assert(self.balance >= amount);
}
}
5.3 電路測試框架
import pytest
from snarky import Circuit, BN254Scalar
class TestZKProof:
def test_range_proof(self):
"""測試範圍證明電路"""
from circuits import RangeProof
circuit = RangeProof(32)
# 測試有效範圍內
proof = circuit.generate_proof(
a=100,
min=0,
max=255
)
assert proof.verify() == True
# 測試超出範圍(應失敗)
proof = circuit.generate_proof(
a=300,
min=0,
max=255
)
assert proof.verify() == False
def test_merkle_proof(self):
"""測試 Merkle 驗證電路"""
from circuits import MerkleProof
# 建立 Merkle 樹
leaves = [b"leaf0", b"leaf1", b"leaf2", b"leaf3"]
tree = MerkleTree(leaves)
root = tree.root()
# 生成 Merkle 證明
proof = tree.prove(1) # 證明 leaf1
# 驗證電路
circuit = MerkleProof(depth=2)
result = circuit.verify(
leaf=hash(leaves[1]),
root=root,
proof_path=proof.siblings,
proof_indices=proof.indices
)
assert result == True
@pytest.mark.parametrize("value,expected_valid", [
(50, True),
(255, True),
(0, True),
(256, False),
(-1, False),
])
def test_range_proof_parametric(self, value, expected_valid):
"""參數化測試"""
from circuits import RangeProof
circuit = RangeProof(32)
try:
proof = circuit.generate_proof(
a=value,
min=0,
max=255
)
valid = proof.verify()
except AssertionError:
valid = False
assert valid == expected_valid
5.4 效能優化技巧
class ZKProofOptimizer:
@staticmethod
def optimize_constraint_count(constraints):
"""
優化約束數量
技巧:
1. 合併相似約束
2. 使用查找表替代乘法
3. 重構邏輯以減少非線性約束
"""
# 識別可合併的約束
mergeable = find_mergeable_groups(constraints)
# 執行合併
optimized = []
for group in mergeable:
merged = merge_constraints(group)
optimized.append(merged)
return optimized
@staticmethod
def optimize_witness_computation(witness):
"""
優化見證計算
減少昂貴的密碼學操作
"""
# 使用小整數優化
# 例如:pedersen(a, b) 比 pedersen(a, b, c, d) 快
# 批量哈希
return batch_hash(witness)
@staticmethod
def estimate_proving_time(constraints, backend='groth16'):
"""
估計證明時間
經驗公式:
Groth16: T_prove ≈ 0.1ms × N_constraints
PLONK: T_prove ≈ 0.5ms × N_constraints × log(N_constraints)
"""
if backend == 'groth16':
return 0.1 * constraints # ms
elif backend == 'plonk':
import math
return 0.5 * constraints * math.log2(constraints)
elif backend == 'stark':
import math
n = constraints
return 5 * n * math.log2(n) # ms
raise ValueError(f"Unknown backend: {backend}")
# 實用優化指南
OPTIMIZATION_TIPS = """
1. 約束減少:
- 使用信號複製而非重複約束
- 合併相似比較(如多個 ≤ 比較)
- 使用選擇器優化條件邏輯
2. 乘法優化:
- 替換昂貴的乘法為查找表
- 使用恆等式重組(如 (a+b)² = a² + 2ab + b²)
3. 哈希優化:
- 使用 Poseidon 而非 Pedersen(針對特定域)
- 批量哈希減少開銷
- 使用 Commitment 替代完整計算
4. 電路設計:
- 將複雜計算移到電路外
- 使用遞歸驗證
- 選擇合適的約束系統
"""
第六章:安全性考量與最佳實踐
6.1 常見漏洞與防護
class ZKSecurityAnalyzer:
@staticmethod
def check_soundness_issues(constraints):
"""
檢查可靠性問題
常見問題:
1. 約束不足(未完全約束所有變數)
2. 鬆弛約束(允許不預期的值)
3. 整數溢出(未檢查邊界)
"""
issues = []
# 檢查未約束的變數
unconstrained = find_unconstrained_vars(constraints)
if unconstrained:
issues.append({
'type': 'underconstrained',
'vars': unconstrained,
'severity': 'HIGH',
'fix': '添加約束或使用 public input'
})
# 檢查鬆弛約束
loose_constraints = find_loose_constraints(constraints)
if loose_constraints:
issues.append({
'type': 'loose_constraints',
'constraints': loose_constraints,
'severity': 'MEDIUM',
'fix': '添加範圍約束'
})
return issues
@staticmethod
def check_arithmetic_overflow(bits, operations):
"""
檢查算術溢出
"""
# 確保操作在允許範圍內
max_value = 2 ** bits
for op in operations:
if op.result > max_value:
return {
'type': 'overflow',
'operation': op,
'fix': f'使用 {bits+1} 位或添加模運算'
}
return None
6.2 電路安全審計清單
## ZK 電路安全審計清單
### 電路邏輯
- [ ] 所有條件分支都被正確約束
- [ ] 沒有未約束的輸出變數
- [ ] 迴圈次數有上限約束
- [ ] 算術運算有溢出保護
### 零知識性
- [ ] 私有輸入確實是私有的
- [ ] 公共輸入的約束足夠嚴格
- [ ] 沒有透過 auxiliary input 洩露隱私
### 可靠性
- [ ] 每個約束都是必要的
- [ ] 約束之間沒有矛盾
- [ ] 邊界情況被正確處理
### 效能
- [ ] 約束數量最小化
- [ ] 避免不必要的複製約束
- [ ] 使用高效的密碼學原語
### 密鑰管理
- [ ] 可信設定的毒性廢料已銷毀
- [ ] SRS 來自可信來源
- [ ] 驗證密鑰與電路匹配
結論
零知識證明技術是以太坊實現隱私和可擴展性的核心工具。從基礎的密碼學原理到複雜的 ZK-Rollup 實現,從 Groth16 到 PLONK 再到 STARKs,這項技術正在快速演進。
理解零知識證明的數學基礎對於開發安全高效的 ZK 應用至關重要。作為開發者,我們需要掌握電路設計、約束優化、以及各種零知識證明系統的優劣勢。
隨著硬體加速、新的承諾方案、以及更高效的電路設計的出現,零知識證明的應用場景將持續擴大,為區塊鏈隱私和擴展性開闢新的可能性。
參考文獻
- Groth16 Paper - https://eprint.iacr.org/2016/260
- PLONK Paper - https://eprint.iacr.org/2019/953
- STARKs Paper - https://eprint.iacr.org/2018/046
- KZG Commitment - https://www.iacr.org/cryptodb/data/paper.php?pubkey=4776
- zkSync Documentation - https://docs.zksync.io
- Starknet Documentation - https://docs.starknet.io
- Aztec Network Documentation - https://docs.aztec.network
- Circom Documentation - https://docs.circom.io
- Noir Documentation - https://noir-lang.github.io/book/
- Ethereum ZK-EVM Types - https://hackmd.io/@L1MB/By6W31DsK
相關文章
- 零知識證明數學推導完整指南:從密碼學基礎到以太坊應用實戰 — 本文從數學推導的角度,全面分析零知識證明的基本原理、主要類型(SNARK、STARK、Bulletproofs)、電路設計方法,以及在以太坊上的實際應用部署。涵蓋完整的代數推導、Groth16 和 Plonkish 約束系統、FRI 協議、以及 zkEVM 架構分析。詳細比較不同 ZK 系統的 Gas 消耗與 TPS 表現,提供量化數據支撐的事實依據。
- ZK-SNARK 完整學習路徑:從基礎數學到 Circom/Noir 電路設計再到實際部署 — 本學習路徑提供零知識證明從理論基礎到實際開發的完整指南。從離散數學、群論、有限域運算開始,深入橢圓曲線密碼學和配對函數,再到 Groth16、PLONK 等主流證明系統的數學推導,最終落實到 Circom 和 Noir 兩種電路描述語言的實戰開發。涵蓋有限域運算、多項式承諾、KZG 方案、信任設置等核心主題,提供從基礎到部署的完整學習地圖。
- ZK-SNARK 數學推導完整指南:從零知識證明到 Groth16、PLONK、STARK 系統的深度數學分析 — 本文從數學基礎出發,完整推導 Groth16、PLONK 與 STARK 三大主流 ZK 系統的底層原理,涵蓋橢圓曲線密碼學、配對函數、多項式承諾、LPC 證明系統等核心技術,同時提供 Circom 與 Noir 電路開發的實戰程式碼範例。截至 2026 年第一季度,ZK-SNARK 已被廣泛部署於 zkRollup、隱私協議、身份驗證系統等場景。
- 以太坊零知識證明完整實作指南:從密碼學基礎到 zk-SNARKs/STARKs 智能合約部署 — 零知識證明(Zero-Knowledge Proof,ZKP)是現代密碼學中最具革命性的技術之一,其核心特性——在不透露任何額外資訊的情況下證明陳述的正確性——為區塊鏈隱私保護和可擴展性帶來了前所未有的可能性。本文從以太坊開發者的視角出發,深入探討零知識證明的密碼學基礎、zk-SNARKs 與 zk-STARKs 的技術差異、主流實作框架(如 Circom、ZoKrates、Groth16、PLONK)的使用方法,以及如何在以太坊上部署零知識證明智能合約。我們將提供完整的程式碼範例,涵蓋從電路設計、證明生成到鏈上驗證的整個流程,同時深入分析每個環節的 Gas 消耗、安全考量與最佳實踐。
- 零知識電路開發完整教學:從理論基礎到智能合約整合的開發者路徑 — 本文提供從電路設計基礎到實際智能合約整合的完整開發者路徑。涵蓋:零知識證明的形式化定義與代數電路視角、Circom 開發環境架設與工具鏈配置、常見電路設計模式(比較器、範圍證明、Merkle 樹驗證)、複雜電路設計原則與調試技巧、從電路到 Solidity 驗證合約的完整工作流、信任設置(Powers of Tau)詳解、以及 NOIR 和 Halo2 等新一代工具的入門介紹。提供完整的代碼範例和開發實踐指導。
延伸閱讀與來源
- zkSNARKs 論文 Gro16 ZK-SNARK 論文
- ZK-STARKs 論文 STARK 論文,透明化零知識證明
- Aztec Network ZK Rollup 隱私協議
- Railgun System 跨鏈隱私協議
這篇文章對您有幫助嗎?
請告訴我們如何改進:
評論
發表評論
注意:由於這是靜態網站,您的評論將儲存在本地瀏覽器中,不會公開顯示。
目前尚無評論,成為第一個發表評論的人吧!