零知识证明引擎
版本: v4.2.0 | 状态: ✅ 生产就绪 | 6 IPC Handlers | 2 数据库表 | Phase 88
ChainlessChain 零知识证明引擎(ZKP Engine)提供 zk-SNARK/zk-STARK 本地证明生成能力,基于 Groth16 证明系统和 Circom 电路编译器,支持隐私交易和选择性披露的身份证明,在不泄露原始数据的前提下验证声明的真实性。
核心特性
- 🔐 zk-SNARK/zk-STARK: 本地生成零知识证明,无需可信第三方
- ⚡ Groth16 证明系统: 高效的配对友好曲线证明,验证速度极快
- 🔧 Circom 电路编译: 支持 Circom DSL 编写和编译自定义算术电路
- 💸 隐私交易: 隐藏交易金额和参与方的零知识交易证明
- 🪪 身份证明: 选择性披露,证明身份属性而不泄露完整信息
系统架构
┌──────────────────────────────────────────────┐
│ 零知识证明引擎 (ZKP Engine) │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Circom │ │ 身份证明 │ │ 选择性披露 │ │
│ │ 电路编译 │ │ DID+ZKP │ │ Verifier │ │
│ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────┐ │
│ │ Groth16 / PLONK / STARK │ │
│ │ 证明系统核心引擎 │ │
│ └──────────────────┬───────────────────┘ │
│ │ │
│ ┌─────────────┼─────────────┐ │
│ ▼ ▼ ▼ │
│ ┌─────────┐ ┌──────────┐ ┌─────────┐ │
│ │证明生成 │ │证明验证 │ │统计查询 │ │
│ └─────────┘ └──────────┘ └─────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────┐ │
│ │ zkp_circuits / zkp_proofs (SQLite) │ │
│ └──────────────────────────────────────┘ │
└──────────────────────────────────────────────┘关键文件
| 文件 | 职责 |
|---|---|
desktop-app-vue/src/main/crypto/zkp-engine.js | ZKP 证明生成与验证核心 |
desktop-app-vue/src/main/crypto/circom-compiler.js | Circom 电路编译器 |
desktop-app-vue/src/main/crypto/identity-proof.js | DID 身份零知识证明 |
desktop-app-vue/src/main/crypto/zkp-ipc.js | ZKP IPC 处理器 (6 个) |
desktop-app-vue/src/renderer/stores/zkpEngine.ts | Pinia 状态管理 |
IPC 接口
ZKP 操作(6 个)
| 通道 | 功能 | 说明 |
|---|---|---|
zkp:generate-proof | 生成证明 | 根据电路和输入生成零知识证明 |
zkp:verify-proof | 验证证明 | 验证零知识证明的有效性 |
zkp:compile-circuit | 编译电路 | 编译 Circom 电路为 R1CS 约束系统 |
zkp:create-identity-proof | 身份证明 | 创建身份属性的零知识证明 |
zkp:selective-disclose | 选择性披露 | 披露部分身份属性,隐藏其余 |
zkp:get-stats | 获取统计 | 查询证明生成/验证的统计信息 |
使用示例
编译 Circom 电路
javascript
const circuit = await window.electron.ipcRenderer.invoke(
"zkp:compile-circuit",
{
source: `
pragma circom 2.0.0;
template AgeCheck() {
signal input age;
signal input threshold;
signal output valid;
valid <-- (age >= threshold) ? 1 : 0;
valid * (valid - 1) === 0;
}
component main = AgeCheck();
`,
name: "age-check",
optimization: 2,
},
);
// circuit = { success: true, circuitId: "cir_age_check", constraints: 3, compiled: true, wasmPath: "...", zkeyPath: "..." }生成零知识证明
javascript
const proof = await window.electron.ipcRenderer.invoke("zkp:generate-proof", {
circuitId: "cir_age_check",
inputs: {
age: 25,
threshold: 18,
},
proofSystem: "groth16", // groth16 | plonk | stark
});
// proof = { success: true, proofId: "prf_abc123", proof: { pi_a: [...], pi_b: [...], pi_c: [...] }, publicSignals: ["1"], generationTime: 1200 }验证证明
javascript
const result = await window.electron.ipcRenderer.invoke("zkp:verify-proof", {
proofId: "prf_abc123",
circuitId: "cir_age_check",
publicSignals: ["1"],
});
// result = { success: true, valid: true, verificationTime: 15 }创建身份证明
javascript
const idProof = await window.electron.ipcRenderer.invoke(
"zkp:create-identity-proof",
{
did: "did:agent:my-agent",
claims: {
ageOver18: true,
country: "CN",
role: "developer",
},
disclose: ["country"], // 仅披露国家,隐藏年龄和角色
},
);
// idProof = { success: true, proofId: "prf_id_001", disclosed: { country: "CN" }, hiddenClaims: ["ageOver18", "role"], proof: { ... } }选择性披露
javascript
const disclosure = await window.electron.ipcRenderer.invoke(
"zkp:selective-disclose",
{
proofId: "prf_id_001",
requestedClaims: ["ageOver18"],
verifierDid: "did:agent:verifier-001",
},
);
// disclosure = { success: true, disclosed: { ageOver18: true }, proof: { ... }, verifiableUntil: 1709209856789 }数据库 Schema
2 张核心表:
| 表名 | 用途 | 关键字段 |
|---|---|---|
zkp_circuits | 电路存储 | id, name, source_hash, wasm_path, zkey_path, constraints |
zkp_proofs | 证明存储 | id, circuit_id, proof_data, public_signals, status |
zkp_circuits 表
sql
CREATE TABLE IF NOT EXISTS zkp_circuits (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
source_hash TEXT NOT NULL,
wasm_path TEXT,
zkey_path TEXT,
vkey_data TEXT, -- 验证密钥 JSON
constraints INTEGER DEFAULT 0,
optimization_level INTEGER DEFAULT 1,
status TEXT DEFAULT 'compiled', -- compiled | error | deprecated
created_at INTEGER DEFAULT (strftime('%s','now') * 1000),
updated_at INTEGER DEFAULT (strftime('%s','now') * 1000)
);
CREATE INDEX IF NOT EXISTS idx_zkp_circuits_name ON zkp_circuits(name);
CREATE INDEX IF NOT EXISTS idx_zkp_circuits_status ON zkp_circuits(status);zkp_proofs 表
sql
CREATE TABLE IF NOT EXISTS zkp_proofs (
id TEXT PRIMARY KEY,
circuit_id TEXT NOT NULL,
proof_data TEXT NOT NULL, -- 证明数据 JSON
public_signals TEXT, -- 公共信号 JSON 数组
proof_system TEXT DEFAULT 'groth16', -- groth16 | plonk | stark
status TEXT DEFAULT 'valid', -- valid | expired | revoked
generation_time INTEGER, -- 生成耗时(ms)
verification_count INTEGER DEFAULT 0,
created_at INTEGER DEFAULT (strftime('%s','now') * 1000),
expires_at INTEGER,
FOREIGN KEY (circuit_id) REFERENCES zkp_circuits(id)
);
CREATE INDEX IF NOT EXISTS idx_zkp_proofs_circuit ON zkp_proofs(circuit_id);
CREATE INDEX IF NOT EXISTS idx_zkp_proofs_status ON zkp_proofs(status);配置
在 .chainlesschain/config.json 中配置:
json
{
"zkpEngine": {
"enabled": true,
"defaultProofSystem": "groth16",
"circom": {
"version": "2.0.0",
"optimizationLevel": 2,
"maxConstraints": 1000000
},
"proof": {
"defaultExpiry": 86400000,
"cacheEnabled": true,
"maxConcurrentGeneration": 2
},
"identity": {
"defaultDisclosure": [],
"autoRenew": true,
"renewBeforeExpiry": 3600000
}
}
}故障排除
| 问题 | 解决方案 |
|---|---|
| 电路编译失败 | 检查 Circom 语法,确认约束数未超限 |
| 证明生成慢 | 减少电路约束数,限制并发数,确认 CPU 可用 |
| 验证失败 | 确认 publicSignals 与生成时一致 |
| 身份证明过期 | 启用 autoRenew 或手动重新生成 |
| 内存不足 | 减小电路规模,增加系统内存 |
