-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(task): new task 81-zkVM (#1169)
* add task 81-zkVM * remove zkVM example projects * task 81-zkVM add default project file
- Loading branch information
Showing
40 changed files
with
1,087 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Cargo.lock | ||
.github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## 什么是 zkVM ? | ||
|
||
虚拟机(VM)是运行程序的程序。zkVM是一种虚拟机技术,它专门设计用来创建零知识证明。这种技术能够为任何程序或计算过程提供一个通用的框架,从而生成证明,证明这些程序或计算是按照预定的方式正确执行的,而无需透露任何关于执行过程的具体信息。 | ||
|
||
zkVM不要求学习复杂的数学和密码学来设计和编码ZK,让任何开发人员都可以执行用他们喜欢的语言编写的程序并生成ZKP(零知识证明),从而更容易与零知识证明集成和交互。从广义上讲,大多数zkVM都意味着包括附加到执行程序的虚拟机的编译器工具链和证明系统,而不仅仅是虚拟机本身。 | ||
|
||
## zkVM 运行流程 | ||
|
||
### 1. 编译阶段(Compiler Stage): | ||
|
||
程序使用传统编程语言(如C、C++、Rust、Solidity)编写,然后被编译器编译成特定指令集架构(ISA)的机器码。 | ||
|
||
RISC0/SP1 使用 RUST 来编写,编译器将RUST 程序编译成RISC-V指令集, 生成 RISC-V ELF 文件。 | ||
|
||
### 2. 虚拟机阶段(VM Stage): | ||
|
||
虚拟机(VM)执行机器码,并生成执行跟踪,这是程序执行的一系列步骤。 | ||
执行跟踪的格式由选择的算术化方案和多项式约束集预定。 | ||
|
||
### 3. 证明者阶段(Prover Stage): | ||
|
||
Prover 使用某种ZKP证明系统(例如 zkSTARK)生成执行证明,来证明任意 RISC-V 程序执行的正确执行。 | ||
|
||
### 4. 验证者阶段(Verifier Stage): | ||
|
||
Verifier 通过遵循证明系统的验证协议来检查证明, 根据证明的有效性接受或拒绝结果。 | ||
|
||
|
||
## zkVM 与区块链集成 | ||
|
||
1. 使用 zkVM 链下生成 SNARK proof。 | ||
2. 编写智能合约方法验证 proof, 如果 proof 验证成功,继续执行合约逻辑。 | ||
3. 发起交易,调用智能合约方法验证 proof 。 | ||
|
||
|
||
## zkVM 项目 | ||
本 task 将介绍两个 zkVM 项目 | ||
* [RISC Zero](risczero) | ||
* [SP1](sp1) | ||
|
||
## zkVM 使用场景 | ||
一些使用场景举例 | ||
1. ZK 协处理器: 使区块链应用程序能够通过将应用程序逻辑的昂贵部分移至链外来降低 Gas 成本 | ||
|
||
2. Rollup: 使用 ZKP 有效性证明或 ZK 欺诈证明构建rollups。 | ||
3. 可验证的 ETH 区块: 例如基于 RISC0 的 [Zeth](https://github.com/risc0/zeth) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
## RISC Zero | ||
|
||
RISC Zero 为全球开发人员创建利用 ZK 技术的软件所需的基础设施和工具。 | ||
|
||
## 安装 | ||
|
||
### 准备 | ||
RISC Zero zkVM 需要先安装 rust, 请参考[RUST安装教程](https://www.rust-lang.org/tools/install)自行安装。 | ||
|
||
### 安装 rzup | ||
rzup 管理RISC Zero的安装 | ||
|
||
1. 使用下面命令安装 rzup | ||
|
||
``` | ||
curl -L https://risczero.com/install | bash | ||
``` | ||
2. 运行 rzup 安装 RISC Zero | ||
|
||
``` | ||
rzup | ||
``` | ||
使用 --version 参数 安装指定版本的 RISC Zero | ||
|
||
``` | ||
rzup --version 1.1.0-alpha.1 | ||
``` | ||
|
||
|
||
## 创建zkVM Application | ||
|
||
--guest-name guest是程序名字,zkVM 运行 guest程序,生成执行证明。 | ||
|
||
``` | ||
cargo risczero new my_project --guest-name guest_code_for_zk_proof | ||
``` | ||
|
||
### 目录结构 | ||
|
||
``` | ||
├── host | ||
│ ├── Cargo.toml | ||
│ └── src | ||
│ └── main.rs | ||
├── methods | ||
│ ├── guest | ||
│ │ └── src | ||
│ │ └── main.rs | ||
│ └── src | ||
│ └── lib.rs | ||
``` | ||
**host** 就是主机, 他给 guest 程序传入数据,调用执行 guest 并获得执行结果和生成执行证明,代码在 **host/src/main.rs** 文件中 | ||
|
||
**guest** 是需要 zkVM 来执行的程序,接收 host 传入的数据,然后执行计算并返回执行结果, 代码在 **methods/guest/src/main.rs**文件中 | ||
|
||
|
||
### 生成证明 | ||
|
||
执行 host 代码,运行 host/src/main.rs 文件 | ||
|
||
``` | ||
cargo run --release | ||
``` | ||
|
||
## 使用Bonsai 生成 proofs | ||
Bonsai 是一个zk coprocessor,是RISC Zero 官方的 一个远程的 zk 证明计算服务。它能帮助 zk Application 低成本快速的生成证明。 | ||
|
||
|
||
|
||
## 与区块链集成 | ||
链下生成证明,链上验证证明 | ||
|
||
![alt text](images/image.png) | ||
|
||
详情参见官方脚手架 [risc0-foundry-template](https://github.com/risc0/risc0-foundry-template) | ||
|
||
|
||
|
||
## 相关链接 | ||
|
||
1. 官方文档。https://dev.risczero.com/ | ||
2. Bonsai。 https://www.risczero.com/blog/a-guide-to-zk-coprocessors-for-scalability |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
Cargo.lock | ||
methods/guest/Cargo.lock | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[workspace] | ||
resolver = "2" | ||
members = ["host", "methods"] | ||
|
||
# Always optimize; building and running the guest takes much longer without optimization. | ||
[profile.dev] | ||
opt-level = 3 | ||
|
||
[profile.release] | ||
debug = 1 | ||
lto = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# RISC Zero Rust Starter Template | ||
|
||
Welcome to the RISC Zero Rust Starter Template! This template is intended to | ||
give you a starting point for building a project using the RISC Zero zkVM. | ||
Throughout the template (including in this README), you'll find comments | ||
labelled `TODO` in places where you'll need to make changes. To better | ||
understand the concepts behind this template, check out the [zkVM | ||
Overview][zkvm-overview]. | ||
|
||
## Quick Start | ||
|
||
First, make sure [rustup] is installed. The | ||
[`rust-toolchain.toml`][rust-toolchain] file will be used by `cargo` to | ||
automatically install the correct version. | ||
|
||
To build all methods and execute the method within the zkVM, run the following | ||
command: | ||
|
||
```bash | ||
cargo run | ||
``` | ||
|
||
This is an empty template, and so there is no expected output (until you modify | ||
the code). | ||
|
||
### Executing the project locally in development mode | ||
|
||
During development, faster iteration upon code changes can be achieved by leveraging [dev-mode], we strongly suggest activating it during your early development phase. Furthermore, you might want to get insights into the execution statistics of your project, and this can be achieved by specifying the environment variable `RUST_LOG="[executor]=info"` before running your project. | ||
|
||
Put together, the command to run your project in development mode while getting execution statistics is: | ||
|
||
```bash | ||
RUST_LOG="[executor]=info" RISC0_DEV_MODE=1 cargo run | ||
``` | ||
|
||
### Running proofs remotely on Bonsai | ||
|
||
_Note: The Bonsai proving service is still in early Alpha; an API key is | ||
required for access. [Click here to request access][bonsai access]._ | ||
|
||
If you have access to the URL and API key to Bonsai you can run your proofs | ||
remotely. To prove in Bonsai mode, invoke `cargo run` with two additional | ||
environment variables: | ||
|
||
```bash | ||
BONSAI_API_KEY="YOUR_API_KEY" BONSAI_API_URL="BONSAI_URL" cargo run | ||
``` | ||
|
||
## How to create a project based on this template | ||
|
||
Search this template for the string `TODO`, and make the necessary changes to | ||
implement the required feature described by the `TODO` comment. Some of these | ||
changes will be complex, and so we have a number of instructional resources to | ||
assist you in learning how to write your own code for the RISC Zero zkVM: | ||
|
||
- The [RISC Zero Developer Docs][dev-docs] is a great place to get started. | ||
- Example projects are available in the [examples folder][examples] of | ||
[`risc0`][risc0-repo] repository. | ||
- Reference documentation is available at [https://docs.rs][docs.rs], including | ||
[`risc0-zkvm`][risc0-zkvm], [`cargo-risczero`][cargo-risczero], | ||
[`risc0-build`][risc0-build], and [others][crates]. | ||
|
||
## Directory Structure | ||
|
||
It is possible to organize the files for these components in various ways. | ||
However, in this starter template we use a standard directory structure for zkVM | ||
applications, which we think is a good starting point for your applications. | ||
|
||
```text | ||
project_name | ||
├── Cargo.toml | ||
├── host | ||
│ ├── Cargo.toml | ||
│ └── src | ||
│ └── main.rs <-- [Host code goes here] | ||
└── methods | ||
├── Cargo.toml | ||
├── build.rs | ||
├── guest | ||
│ ├── Cargo.toml | ||
│ └── src | ||
│ └── method_name.rs <-- [Guest code goes here] | ||
└── src | ||
└── lib.rs | ||
``` | ||
|
||
## Video Tutorial | ||
|
||
For a walk-through of how to build with this template, check out this [excerpt | ||
from our workshop at ZK HACK III][zkhack-iii]. | ||
|
||
## Questions, Feedback, and Collaborations | ||
|
||
We'd love to hear from you on [Discord][discord] or [Twitter][twitter]. | ||
|
||
[bonsai access]: https://bonsai.xyz/apply | ||
[cargo-risczero]: https://docs.rs/cargo-risczero | ||
[crates]: https://github.com/risc0/risc0/blob/main/README.md#rust-binaries | ||
[dev-docs]: https://dev.risczero.com | ||
[dev-mode]: https://dev.risczero.com/api/generating-proofs/dev-mode | ||
[discord]: https://discord.gg/risczero | ||
[docs.rs]: https://docs.rs/releases/search?query=risc0 | ||
[examples]: https://github.com/risc0/risc0/tree/main/examples | ||
[risc0-build]: https://docs.rs/risc0-build | ||
[risc0-repo]: https://www.github.com/risc0/risc0 | ||
[risc0-zkvm]: https://docs.rs/risc0-zkvm | ||
[rustup]: https://rustup.rs | ||
[rust-toolchain]: rust-toolchain.toml | ||
[twitter]: https://twitter.com/risczero | ||
[zkvm-overview]: https://dev.risczero.com/zkvm | ||
[zkhack-iii]: https://www.youtube.com/watch?v=Yg_BGqj_6lg&list=PLcPzhUaCxlCgig7ofeARMPwQ8vbuD6hC5&index=5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "host" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
methods = { path = "../methods" } | ||
risc0-zkvm = { version = "1.0.5" } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
serde = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// These constants represent the RISC-V ELF and the image ID generated by risc0-build. | ||
// The ELF is used for proving and the ID is used for verification. | ||
use methods::{ | ||
GUEST_CODE_FOR_ZK_PROOF_ELF, GUEST_CODE_FOR_ZK_PROOF_ID | ||
}; | ||
use risc0_zkvm::{default_prover, ExecutorEnv}; | ||
|
||
fn main() { | ||
// Initialize tracing. In order to view logs, run `RUST_LOG=info cargo run` | ||
tracing_subscriber::fmt() | ||
.with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env()) | ||
.init(); | ||
|
||
// An executor environment describes the configurations for the zkVM | ||
// including program inputs. | ||
// An default ExecutorEnv can be created like so: | ||
// `let env = ExecutorEnv::builder().build().unwrap();` | ||
// However, this `env` does not have any inputs. | ||
// | ||
// To add guest input to the executor environment, use | ||
// ExecutorEnvBuilder::write(). | ||
// To access this method, you'll need to use ExecutorEnv::builder(), which | ||
// creates an ExecutorEnvBuilder. When you're done adding input, call | ||
// ExecutorEnvBuilder::build(). | ||
|
||
// For example: | ||
let input: u32 = 15 * u32::pow(2, 27) + 1; | ||
let env = ExecutorEnv::builder() | ||
.write(&input) | ||
.unwrap() | ||
.build() | ||
.unwrap(); | ||
|
||
// Obtain the default prover. | ||
let prover = default_prover(); | ||
|
||
// Proof information by proving the specified ELF binary. | ||
// This struct contains the receipt along with statistics about execution of the guest | ||
let prove_info = prover | ||
.prove(env, GUEST_CODE_FOR_ZK_PROOF_ELF) | ||
.unwrap(); | ||
|
||
// extract the receipt. | ||
let receipt = prove_info.receipt; | ||
|
||
// TODO: Implement code for retrieving receipt journal here. | ||
|
||
// For example: | ||
let _output: u32 = receipt.journal.decode().unwrap(); | ||
|
||
// The receipt was verified at the end of proving, but the below code is an | ||
// example of how someone else could verify this receipt. | ||
receipt | ||
.verify(GUEST_CODE_FOR_ZK_PROOF_ID) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "methods" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[build-dependencies] | ||
risc0-build = { version = "1.0.5" } | ||
|
||
[package.metadata.risc0] | ||
methods = ["guest"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
risc0_build::embed_methods(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "guest_code_for_zk_proof" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[workspace] | ||
|
||
[dependencies] | ||
risc0-zkvm = { version = "1.0.5", default-features = false, features = ['std'] } |
13 changes: 13 additions & 0 deletions
13
basic/81-zkVM/riskzero/my_project/methods/guest/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use risc0_zkvm::guest::env; | ||
|
||
fn main() { | ||
// TODO: Implement your guest code here | ||
|
||
// read the input | ||
let input: u32 = env::read(); | ||
|
||
// TODO: do something with the input | ||
|
||
// write public output to the journal | ||
env::commit(&input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include!(concat!(env!("OUT_DIR"), "/methods.rs")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[toolchain] | ||
channel = "stable" | ||
components = ["rustfmt", "rust-src"] | ||
profile = "minimal" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fibonacci/contracts/lib |
Oops, something went wrong.