Skip to content

Commit 170fa58

Browse files
authored
Merge pull request #106 from yingjingyang/main
add readme for task 22
2 parents 7bc88d8 + a90b6d9 commit 170fa58

File tree

5 files changed

+141
-14
lines changed

5 files changed

+141
-14
lines changed

basic/21-scaffold-lender/README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
- <https://github.com/austintgriffith/scaffold-eth/tree/defi-rtokens>
2-
- <https://github.com/austintgriffith/scaffold-eth/tree/unifactory>
3-
- <https://github.com/austintgriffith/scaffold-eth/tree/clr-dev>
4-
- <https://medium.com/dapphub/introducing-ds-math-an-innovative-safe-math-library-d58bc88313da>
1+
##
2+
3+
## 参考资料
4+
https://github.com/austintgriffith/scaffold-eth/tree/defi-rtokens
5+
https://github.com/austintgriffith/scaffold-eth/tree/unifactory
6+
https://github.com/austintgriffith/scaffold-eth/tree/clr-dev
7+
https://medium.com/dapphub/introducing-ds-math-an-innovative-safe-math-library-d58bc88313da

basic/22-scaffold-zk/readme.md

-10
This file was deleted.

basic/22-zk-snarkjs/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "22-scaffold-zk",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"circom": "^0.5.45",
14+
"snarkjs": "^0.4.6"
15+
}
16+
}
Binary file not shown.

basic/22-zk-snarkjs/readme.md

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)