Skip to content

Commit 093c629

Browse files
authored
Merge pull request #795 from liberhe/dev
optimise the task 22
2 parents f7e8840 + b925bb6 commit 093c629

File tree

15 files changed

+155
-130
lines changed

15 files changed

+155
-130
lines changed

basic/22-zk-snarkjs/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ circuit_js/
55
circuit.circom
66
circuit.r1cs
77
circuit.sym
8+
circuit.json
89
powersOfTau28_hez_final_10.ptau
910
circuit_0000.zkey
1011
verification_key.json
1112
input.json
1213
witness.wtns
1314
witness.json
1415
proof.json
16+
proving_key.json
1517
public.json
1618
verifier.sol
1719
circuit_final.zkey

basic/22-zk-snarkjs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "22-scaffold-zk",
2+
"name": "22-zk-snarkjs",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
Binary file not shown.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"34"
3+
]

basic/22-zk-snarkjs/readme.md

+38-51
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
## circom 与 snarkjs
22

3-
本章节, 我们将讲解如何使用 circom 和 snarkjs 创建一个零知识 zkSnark 电路, 并展示如何创建证明并在以太坊上进行链外和链上验证
3+
本章节, 我们将讲解如何使用 circom 和 snarkjs 创建一个零知识 zkSnark 电路, 并展示如何创建证明并在以太坊上进行链外和链上验证
44

5-
## 电路使用步骤
6-
7-
libsnark 使用步骤:
8-
9-
1. 将待证明的命题表达为 R1CS
10-
2. 使用生成算法 G 为该命题生成公共参数
11-
3. 使用证明生成算法生成 R1CS 可满足的证明
12-
4. 使用验证算法来验证证明
135

146
## 测试步骤
157

@@ -32,8 +24,8 @@ pragma circom 2.0.0;
3224
template Multiplier2 () {
3325
3426
// Declaration of signals.
35-
signal input a;
36-
signal input b;
27+
signal private input a;
28+
signal private input b;
3729
signal output c;
3830
3931
// Statements.
@@ -52,97 +44,92 @@ circom circuit.circom --r1cs --wasm --sym
5244
- 显示电路的信息
5345

5446
```sh
55-
npx snarkjs r1cs info circuit.r1cs
47+
snarkjs info -r circuit.r1cs
5648
```
5749

5850
PS: 查看 snarkjs 的具体命令参数可使用 npx snarkjs --help
5951

6052
- 打印电路的约束
6153

6254
```sh
63-
npx snarkjs r1cs print circuit.r1cs circuit.sym
64-
```
65-
66-
- 下载 tau ceremony 文件
67-
68-
```sh
69-
wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_10.ptau
70-
```
71-
72-
- 生成 zkey 文件
73-
74-
```sh
75-
npx snarkjs zkey new circuit.r1cs powersOfTau28_hez_final_10.ptau circuit_0000.zkey
76-
```
77-
78-
- 增加 out contribution,随机输入一段文本,比如'123'
79-
80-
```sh
81-
npx snarkjs zkey contribute circuit_0000.zkey circuit_final.zkey
55+
snarkjs printconstraints -r circuit.r1cs -s circuit.sym
8256
```
8357

84-
- 导出证明 key
58+
- 可信设置,生成proving key & verification key。 执行后可以看到有两个新文件proving_key.json 和 verification_key.json.
8559

8660
```sh
87-
npx snarkjs zkey export verificationkey circuit_final.zkey verification_key.json
61+
snarkjs setup -r circuit.r1cs
8862
```
8963

90-
- 创建 input.json 文件, 文件内容如下
91-
64+
- 计算witness,需要创建input.json
9265
```json
9366
{"a": 3, "b": 11}
9467
```
95-
96-
- 计算见证
68+
指定a,b的值。此步可以得出电路所有中间变量, 可以看到witness.json.
9769

9870
```sh
99-
node ./circuit_js/generate_witness.js ./circuit_js/circuit.wasm input.json witness.wtns
71+
snarkjs calculatewitness --wasm circuit.wasm --input input.json
10072
```
10173

102-
- 导出 witness.wtns 见证文件为 json 格式
74+
75+
- 生成证明,根据witness.json和 procing_key.json生成证明。
10376

10477
```sh
105-
npx snarkjs wtns export json witness.wtns witness.json
78+
snarkjs proof --witness witness.json --provingkey proving_key.json
10679
```
80+
执行后会生成proof.json 和 public.json。public.json包含公开输入和输出
10781

108-
- 创建证明
10982

110-
```sh
111-
npx snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json
112-
```
11383

11484
- 验证证明
11585

11686
```sh
117-
npx snarkjs groth16 verify verification_key.json public.json proof.json
87+
snarkjs verify --verificationkey verification_key.json --proof proof.json --public public.json
88+
```
89+
可以看到可以OK。
90+
也可以新建一个public-invalid.json。
91+
```
92+
snarkjs verify --verificationkey verification_key.json --proof proof.json --public public-invalid.json
11893
```
94+
可以看到invalid.
11995

120-
### 链上证明
96+
### 链上证明(Proving on-chain)
12197

122-
- 生成 Solidity 的证明
98+
- 生成 Solidity 的证明合约
12399

124100
```sh
125-
npx snarkjs zkey export solidityverifier circuit_final.zkey verifier.sol
101+
snarkjs generateverifier --verificationkey verification_key.json --verifier verifier.sol
126102
```
103+
会有Pairings and Verifier 两个合约,关注Verifier即可。
127104

128-
- 发布证明
105+
- 发布证明
129106
可以复制 verifier.sol 代码到 remix 进行部署
130107

131108
- 生成调用的参数
132109

133110
```sh
134-
npx snarkjs zkey export soliditycalldata public.json proof.json
111+
snarkjs generatecall --proof proof.json --public public.json
135112
```
136113

137-
- 进行调用
114+
- 进行合约调用
138115
将命令的输出复制到 Remix 中的 verifyProof 方法的 parameters 字段中,点击 call 调用 verifyProof
139116
如果一切正常,方法应该返回 true
140117
如果仅更改参数中的任何位,则可以检查结果返回 false
141118

119+
120+
121+
122+
## circom语法
123+
1. <-- assigns a value to a signal without adding a constraint.
124+
2. Whereas === adds a constraint without assigning a value.
125+
3. <== both assigns a value to a signal and adds a contraint。Which means it’s just the combination of === and <--.
142126
## 参考资料
143127

144128
- 创建第一个零知识 snark 电路: https://learnblockchain.cn/article/1078
129+
- 参考文档:https://blog.iden3.io/first-zk-proof.html
145130
- circom2 doc: https://docs.circom.io/circom-language/basic-operators/
146131
- snarkjs: https://github.com/iden3/snarkjs
147132
- 深入浅出零知识证明之zk-SNARKs: https://www.yuque.com/u428635/scg32w/edmn74
148133
- ZK Jargon Decoder: https://nmohnblatt.github.io/zk-jargon-decoder/foreword.html
134+
- CTN rollup分享:https://www.bilibili.com/video/BV1oL4y1h7iE?p=1&share_medium=android&share_plat=android&share_session_id=9d2f7c31-a4dc-46a5-a2d9-4d6d0ebc3997&share_source=WEIXIN&share_tag=s_i&timestamp=1653798331&unique_k=921Lj1L&vd_source=3c62940e414c68a7f639c5737b9fd3d1
135+
- zkRollup tutorial: https://keen-noyce-c29dfa.netlify.app/#16

basic/31-starkNet-layer2/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,8 @@ starknet call \
185185
- develop docs <https://starknet.io/docs/index.html>
186186
- blockscan <https://voyager.online/>
187187
- StarkNet AMM demo <https://amm-demo.starknet.starkware.co/swap>
188+
- 官网链接:https://starkware.co/starknet/
189+
- 网络简介:https://twitter.com/SimaoCCruz/status/1494733568962146308
190+
- 研究报告:https://mp.weixin.qq.com/s/-VEJEzuPQ9R3g2Fytckndw
191+
- 项目生态:https://starkware.notion.site/Projects-Building-on-StarkNet-a33dee55778a4515a9be9bdae02ee682
192+

basic/36-nft-ipfs/README.md

-67
This file was deleted.

basic/36-zk-rollup/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Zk-rollup
2+
3+
4+
## 执行步骤
5+
1. compile
6+
```
7+
circom sample_circuit.circom -o circuit.json
8+
```
9+
10+
2. Generate your input for the circuit
11+
```
12+
node generate_circuit_input.js
13+
```
14+
15+
3. Calculate the witness for the circuit
16+
```
17+
snarkjs calculatewitness -c circuit.json -i input.json
18+
19+
```
20+
21+
4. Perform trusted setup
22+
```
23+
snarkjs setup -c circuit.json --protocol groth
24+
```
25+
26+
5. Generate the proof
27+
```
28+
snarkjs proof -w witness.json --pk proving_key.json
29+
```
30+
31+
6. Verify the proof
32+
33+
```
34+
snarkjs verify
35+
```
36+
## 参考文档
37+
- ZkRollup Tutorial: https://keen-noyce-c29dfa.netlify.app/#3
38+
39+

basic/36-zk-rollup/RollupNC_tutorial

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 7c67b63fc046d93400cf80f104c9cc8396cf3ff8
File renamed without changes.
File renamed without changes.
File renamed without changes.

basic/65-ZK-NFT/readme.md

+66-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1-
#
1+
# 简介
22

3+
数据存储在 ipfs 上后, 在 Dapp 前端就可以通过 api 进行访问, 达到去中心化的目的.
4+
我们可以使用两种方式进行
5+
//todo
6+
FileCoin
37

4-
## 参考链接
5-
- zknft: https://medium.com/centrifuge/bringing-privacy-to-non-fungible-tokens-a-recap-from-the-zokrates-workshop-at-zcon1-6d9ea8a74b7f
8+
## 测试 nft.storage 流程
9+
10+
- 账户注册
11+
注册https://nft.storage/, 获得 nft.storage API Key, 写入.env
12+
- 安装 nft.storage 以及其他依赖
13+
14+
```shell
15+
npm install
16+
```
17+
18+
- 运行 js 文件
19+
20+
```shell
21+
node scripts/nftStorage-uploadfile
22+
```
23+
24+
## 测试 ipfs.infura 流程
25+
26+
- 在 infura 上创建 ipfs 工程
27+
登陆 infura, 然后进入 Dashboard, 之后在 "IPFS" 标签页中创建 IPFS 工程
28+
<center><img src="https://github.com/Dapp-Learning-DAO/Dapp-Learning-Arsenal/blob/main/images/basic/36-nft-ipfs/ipfs-infura.png?raw=true" /></center>
29+
30+
- 获取 "PROJECT ID" 和 "PROJECT SECRET"
31+
点击刚才创建的 IPFS 工程, 然后获取其中的 "PROJECT ID" 和 "PROJECT SECRET"
32+
33+
- 上传文件
34+
替换如下命令中的 PROJECT_ID, PROJECT_SECRET.
35+
36+
```shell
37+
cd data;
38+
curl -X POST -F [email protected] \
39+
-u "<PROJECT_ID>:<PROJECT_SECRET>" \
40+
"https://ipfs.infura.io:5001/api/v0/add"
41+
```
42+
43+
之后可以看到如下输出:
44+
45+
```shell
46+
{
47+
"Name":"ipfs_file_docs_getting_started_demo.txt",
48+
"Hash":"QmeGAVddnBSnKc1DLE7DLV9uuTqo5F7QbaveTjr45JUdQn",
49+
"Size":"44"
50+
}
51+
```
52+
53+
- 获取文件 && 验证
54+
替换如下命令中的 Hash 参数, 然后执行如下命令, 之后检查 matic.jpeg 文件.
55+
56+
```shell
57+
rm matic.jpeg
58+
curl -X POST -u "PROJECT_ID:PROJECT_SECRET" \
59+
"https://ipfs.infura.io:5001/api/v0/pin/add?arg=<Hash>" -o matic.jpeg
60+
```
61+
62+
## 参考文档
63+
64+
- <https://www.bilibili.com/video/BV1j5411w7MH>
65+
- <https://pinata.cloud/pinmanager>
66+
- <https://nft.storage>
67+
- https://infura.io/docs/ipfs#section/Authentication/Using-Javascript
68+
- zknft: https://medium.com/centrifuge/bringing-privacy-to-non-fungible-tokens-a-recap-from-the-zokrates-workshop-at-zcon1-6d9ea8a74b7f

crypto/Starkware/readme.md

-8
This file was deleted.

0 commit comments

Comments
 (0)