|
| 1 | +## circom与snarkjs |
| 2 | +本章节, 我们将讲解如何使用 circom 和 snarkjs 创建一个零知识 zkSnark电路, 并展示如何创建证明并在以太坊上进行链外和链上验证 |
| 3 | + |
| 4 | +## 电路使用步骤 |
| 5 | +libsnark使用步骤: |
| 6 | +1. 将待证明的命题表达为R1CS |
| 7 | +2. 使用生成算法G为该命题生成公共参数 |
| 8 | +3. 使用证明生成算法生成R1CS可满足的证明 |
| 9 | +4. 使用验证算法来验证证明 |
| 10 | + |
| 11 | +## 测试步骤 |
| 12 | +### 链外证明 |
| 13 | +- 安装依赖 |
| 14 | +``` |
| 15 | +yarn |
| 16 | +``` |
| 17 | + |
| 18 | +- 创建 circuit.circom 文件 |
| 19 | +文件内容如下 |
| 20 | +``` |
| 21 | +template Multiplier() { |
| 22 | + signal private input a; |
| 23 | + signal private input b; |
| 24 | + signal output c; |
| 25 | +
|
| 26 | + c <== a*b; |
| 27 | +} |
| 28 | +
|
| 29 | +component main = Multiplier(); |
| 30 | +``` |
| 31 | + |
| 32 | +- 编译电路 |
| 33 | +``` |
| 34 | +circom circuit.circom --r1cs --wasm --sym |
| 35 | +``` |
| 36 | + |
| 37 | +- 显示电路的信息 |
| 38 | +``` |
| 39 | +npx snarkjs r1cs info circuit.r1cs |
| 40 | +``` |
| 41 | + |
| 42 | +PS: 查看 snarkjs 的具体命令参数可使用 npx snarkjs --help |
| 43 | + |
| 44 | +- 打印电路的约束 |
| 45 | +``` |
| 46 | +npx snarkjs r1cs print circuit.r1cs circuit.sym |
| 47 | +``` |
| 48 | + |
| 49 | +- 下载 tau ceremony 文件 |
| 50 | +``` |
| 51 | +wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_10.ptau |
| 52 | +``` |
| 53 | + |
| 54 | +- 生成 zkey 文件 |
| 55 | +``` |
| 56 | +npx snarkjs zkey new circuit.r1cs powersOfTau28_hez_final_10.ptau circuit_0000.zkey |
| 57 | +``` |
| 58 | + |
| 59 | +- 增加 out contribution |
| 60 | +``` |
| 61 | +npx snarkjs zkey contribute circuit_0000.zkey circuit_final.zkey |
| 62 | +``` |
| 63 | + |
| 64 | +- 导出证明 key |
| 65 | +``` |
| 66 | +npx snarkjs zkey export verificationkey circuit_final.zkey verification_key.json |
| 67 | +``` |
| 68 | + |
| 69 | +- 创建 input.json |
| 70 | +创建 input.json 文件, 文件内容如下 |
| 71 | +``` |
| 72 | +{"a": 3, "b": 11} |
| 73 | +``` |
| 74 | + |
| 75 | +- 计算见证 |
| 76 | +``` |
| 77 | +npx snarkjs wtns calculate circuit.wasm input.json witness.wtns |
| 78 | +``` |
| 79 | + |
| 80 | +- 导出 witness.wtns 见证文件为 json 格式 |
| 81 | +``` |
| 82 | +npx snarkjs wtns export json witness.wtns witness.json |
| 83 | +``` |
| 84 | + |
| 85 | +- 创建证明 |
| 86 | +``` |
| 87 | +npx snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json |
| 88 | +``` |
| 89 | + |
| 90 | +- 验证证明 |
| 91 | +``` |
| 92 | +npx snarkjs groth16 verify verification_key.json public.json proof.json |
| 93 | +``` |
| 94 | + |
| 95 | +### 链上证明 |
| 96 | +- 生成 Solidity 的证明 |
| 97 | +``` |
| 98 | +npx snarkjs zkey export solidityverifier circuit_final.zkey verifier.sol |
| 99 | +``` |
| 100 | + |
| 101 | +- 发布证明 |
| 102 | +可以复制verifier.sol代码到 remix 进行部署 |
| 103 | + |
| 104 | +- 生成调用的参数 |
| 105 | +``` |
| 106 | +npx snarkjs zkey export soliditycalldata public.json proof.json |
| 107 | +``` |
| 108 | + |
| 109 | +- 进行调用 |
| 110 | +将命令的输出复制到 Remix 中的 verifyProof 方法的 parameters 字段中,点击 call 调用 verifyProof |
| 111 | +如果一切正常,方法应该返回 true |
| 112 | +如果仅更改参数中的任何位,则可以检查结果返回 false |
| 113 | + |
| 114 | + |
| 115 | +## 参考资料 |
| 116 | +https://learnblockchain.cn/article/1078 |
| 117 | +https://iden3.io/blog/circom-and-snarkjs-tutorial2.html |
| 118 | +https://github.com/iden3/circom/blob/master/TUTORIAL.md |
0 commit comments