-
Notifications
You must be signed in to change notification settings - Fork 39
Ceno book. #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yczhangsjtu
wants to merge
25
commits into
master
Choose a base branch
from
feat/ceno-book
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Ceno book. #1054
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6fe7283
Add doc
yczhangsjtu 0fba355
Update doc to use cargo ceno
yczhangsjtu 7f3b924
Update doc to add additional contents
yczhangsjtu 302d09d
Update generating and verifying proof
yczhangsjtu ac328af
Remove gitignore in docs
yczhangsjtu 76f59f4
Remove fibonacci example
yczhangsjtu f742752
Update
yczhangsjtu d942232
Update
yczhangsjtu 2980a1d
Update
yczhangsjtu 822207d
Add more to the outline
yczhangsjtu c0fded9
Merge remote-tracking branch 'origin/master' into feat/ceno-book
yczhangsjtu 459af41
Fix
yczhangsjtu 470733f
Use binary file for hints
yczhangsjtu df0cc1b
Use binary file for hints in example
yczhangsjtu cac2e7f
Add some todos
yczhangsjtu 0f3c013
Merge remote-tracking branch 'origin/master' into feat/ceno-book
yczhangsjtu 38ffdb2
Merge branch 'master' into feat/ceno-book
yczhangsjtu ed6d254
Update
yczhangsjtu aa6d1a6
Merge remote-tracking branch 'origin/master' into feat/ceno-book
yczhangsjtu bc69b51
Update
yczhangsjtu e44face
Update
yczhangsjtu dac8cdb
Update
yczhangsjtu c3e2d17
Update
yczhangsjtu 9b3157d
Update
yczhangsjtu 90376ce
Merge remote-tracking branch 'origin/master' into feat/ceno-book
yczhangsjtu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ table_cache_dev_* | |
.DS_Store | ||
.env | ||
proptest-regressions/ | ||
docs/book | ||
|
||
# ceno serialized files | ||
*.bin |
This file contains hidden or 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
This file contains hidden or 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,5 @@ | ||
[book] | ||
authors = ["Yuncong Zhang"] | ||
language = "en" | ||
src = "src" | ||
title = "Ceno Docs" |
This file contains hidden or 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,17 @@ | ||
# Summary | ||
|
||
- [Overview](./overview.md) | ||
- [Getting Started](./getting-started.md) | ||
- [Your First ZK Program](./your-first-zk-program.md) | ||
- [Host-Guest Interaction](./host-guest-interaction.md) | ||
- [Generating and Verifying Proofs](./generating-and-verifying-proofs.md) | ||
- [The `ceno` CLI](./ceno-cli.md) | ||
- [Walkthroughs of Examples](./examples-walkthrough.md) | ||
- [Advanced Topics](./advanced-topics.md) | ||
- [Guest Programming (`ceno_rt`)](./guest-programming.md) | ||
- [Accelerated Operations with Precompiles (Syscalls)](./precompiles.md) | ||
- [Profiling & Performance](./profiling.md) | ||
- [Differences from RISC-V](./differences-from-risc-v.md) | ||
- [Integration with Ethereum](./integration-with-ethereum.md) | ||
- [Prover Network](./prover-network.md) | ||
- [On-chain verification](./on-chain-verification.md) |
This file contains hidden or 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 @@ | ||
# Advanced Topics | ||
|
||
This chapter is for developers who want to dive deeper into the internals of Ceno. |
This file contains hidden or 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,17 @@ | ||
# The `ceno` CLI | ||
|
||
The `ceno` command-line interface is the primary way to interact with the Ceno ZKVM. It allows you to build, run, and verify your ZK programs. | ||
|
||
The available commands are: | ||
|
||
- `cargo ceno build`: Compiles a guest program written in Rust into a RISC-V ELF file. | ||
- `cargo ceno info`: Provides information about a compiled ELF file, such as its size and segments. | ||
- `cargo ceno keygen`: Generates a proving key and a verification key for a guest program. | ||
- `cargo ceno prove`: Compiles, runs, and proves a Ceno guest program in one go. | ||
- `cargo ceno run`: Executes a guest program and generates a witness of its execution. | ||
- `cargo ceno verify`: Verifies a proof generated by `cargo ceno prove`. | ||
- `cargo ceno raw-keygen`: A lower-level command to generate keys from a compiled ELF file. | ||
- `cargo ceno raw-prove`: A lower-level command to prove a program from a compiled ELF file and a witness. | ||
- `cargo ceno raw-run`: A lower-level command to run a program without the full proof generation, useful for debugging. | ||
|
||
For detailed usage of each command, you can use the `--help` flag, for example: `cargo ceno run --help`. |
This file contains hidden or 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 @@ | ||
# Differences from RISC-V |
This file contains hidden or 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,5 @@ | ||
# Walkthroughs of Examples | ||
|
||
This chapter will contain detailed walkthroughs of selected examples from the `examples/` directory. | ||
|
||
*(Content to be added)* |
This file contains hidden or 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,123 @@ | ||
# Generating and Verifying Proofs | ||
|
||
The `cargo ceno` command provides a streamlined workflow for generating and verifying proofs of your ZK programs. This is handled primarily by the `keygen`, `prove` and `verify` subcommands. | ||
|
||
Here's a more detailed look at the steps involved in generating a proof: | ||
|
||
1. **Key Generation (`cargo ceno keygen`):** Before proving, you need a proving key and a verification key. The `keygen` command generates these keys for a given guest program. | ||
|
||
```bash | ||
cargo ceno keygen --example <GUEST_EXAMPLE_NAME> --out-vk <PATH_TO_VERIFICATION_KEY_FILE> | ||
``` | ||
|
||
2. **Proof Generation (using the witness):** The final step is to use the proving key and the witness to generate the proof. The `prove` command automates this, but you can also perform this step manually using the lower-level `raw-prove` command if you have the ELF file, proving key, and witness. | ||
|
||
By using `cargo ceno prove`, you get a simplified experience that handles these steps for you. For most use cases, `cargo ceno prove` and `cargo ceno verify` are the primary commands you will use. | ||
|
||
```bash | ||
cargo ceno prove --example <GUEST_EXAMPLE_NAME> --hints=<HINTS_SEPARATED_BY_COMMA> --public-io=<PUBLIC_IO> --out-vk <PATH_TO_VERIFICATION_KEY_FILE> --out-proof target/fibonacci.proof | ||
``` | ||
|
||
## Concrete Example | ||
|
||
You can use `ceno` to generate proofs for your own custom Rust programs. Let's walk through how to set up a new project and use `ceno` with it. | ||
|
||
### 1. Project Setup | ||
|
||
First, create a new binary crate with `cargo`: | ||
|
||
```bash | ||
cargo new my-ceno-program | ||
cd my-ceno-program | ||
``` | ||
|
||
Your project will have the following structure: | ||
|
||
``` | ||
my-ceno-program/ | ||
├── Cargo.toml | ||
└── src/ | ||
└── main.rs | ||
``` | ||
|
||
### 2. Cargo.toml | ||
|
||
Next, you need to add `ceno_rt` as a dependency in your `Cargo.toml`. `ceno_rt` provides the runtime environment and syscalls for guest programs. | ||
|
||
```toml | ||
[package] | ||
name = "my-ceno-program" | ||
version = "0.1.0" | ||
edition = "2024" | ||
|
||
[dependencies] | ||
ceno_rt = { git = "https://github.com/scroll-tech/ceno.git" } | ||
rkyv = { version = "0.8", default-features = false, features = [ | ||
"alloc", | ||
"bytecheck", | ||
] } | ||
``` | ||
|
||
_Note: For local development, you can use a path dependency: `ceno_rt = { path = "../ceno/ceno_rt" }`_ | ||
|
||
### 3. Writing the Guest Program | ||
|
||
Now, let's write a simple guest program in `src/main.rs`. This program will read one `u32` values from the input, add a constant to it, and write the result to the output. | ||
|
||
```rust | ||
extern crate ceno_rt; | ||
use rkyv::Archived; | ||
|
||
fn main() { | ||
let a: &Archived<u32> = ceno_rt::read(); | ||
let a: u32 = a.into(); | ||
let b: u32 = 3; | ||
let c = a.wrapping_add(b); | ||
|
||
ceno_rt::commit::<Archived<u32>, _>(&c); | ||
} | ||
``` | ||
|
||
### 4. Building, Proving, and Verifying | ||
|
||
With your custom program ready, you can use `ceno` to manage the workflow. These commands are typically run from the root of your project (`my-ceno-program`). | ||
|
||
1. **Build the program:** | ||
|
||
The `build` command compiles your guest program into a RISC-V ELF file. | ||
|
||
```bash | ||
cargo ceno build | ||
``` | ||
|
||
This will create an ELF file at `target/riscv32im-ceno-zkvm-elf/debug/my-ceno-program`. | ||
|
||
2. **Generate Keys:** | ||
|
||
Next, generate the proving and verification keys. | ||
|
||
```bash | ||
cargo ceno keygen --out-vk vk.bin | ||
``` | ||
|
||
This will save the keys in a `keys` directory. | ||
|
||
3. **Generate a Proof:** | ||
|
||
Now, run the program and generate a proof. You can provide input via the `--stdin` flag. | ||
|
||
```bash | ||
cargo ceno prove --hints=5 --public-io=8 --out-proof proof.bin | ||
``` | ||
|
||
This command executes the ELF, generates a proof, and saves it as `proof.bin`. The output of the program will be printed to your console. | ||
|
||
4. **Verify the Proof:** | ||
|
||
Finally, verify the generated proof. | ||
|
||
```bash | ||
cargo ceno verify --vk vk.bin --proof proof.bin | ||
``` | ||
|
||
If the proof is valid, you'll see a success message. This workflow allows you to integrate `ceno`'s proving capabilities into your own Rust projects. |
This file contains hidden or 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,60 @@ | ||
# Getting Started | ||
|
||
This chapter will guide you through setting up your local development environment for Ceno and running your first zero-knowledge program. | ||
|
||
## Local Build Requirements | ||
|
||
Ceno is built in Rust, so you must [install the Rust toolchain](https://www.rust-lang.org/tools/install) first. | ||
|
||
We also use `cargo-make` to orchestrate the build process. You can install it with the following command: | ||
|
||
```sh | ||
cargo install cargo-make | ||
``` | ||
|
||
Ceno executes RISC-V instructions, so you will also need to install the Risc-V target for Rust. You can do this with the following command: | ||
|
||
```sh | ||
rustup target add riscv32im-ceno-zkvm-elf | ||
``` | ||
|
||
## Installing `cargo ceno` | ||
|
||
The `cargo ceno` command is the primary tool for interacting with the Ceno zkVM. You can install it by running the following command from the root of the repository: | ||
|
||
```sh | ||
cargo install --path ceno_cli | ||
``` | ||
|
||
## Building the Examples | ||
|
||
The Ceno project includes a variety of [examples](https://github.com/scroll-tech/ceno/tree/master/examples/examples) to help you get started. | ||
|
||
You can build all the examples using the `cargo ceno` command-line tool. Execute the following command in the Ceno repository root directory: | ||
|
||
```sh | ||
cargo ceno build --example fibonacci | ||
``` | ||
|
||
This command will compile the example `fibonacci` located in the `examples/examples` directory and place the resulting ELF files in the `examples/target/riscv32im-ceno-zkvm-elf/release` directory. | ||
|
||
## Running an Example | ||
|
||
Once the examples are built, you can run any of them using the `cargo ceno run` command. We will run the Fibonacci example. | ||
|
||
This example calculates the `n`-th Fibonacci number, where `n` is determined by a hint value provided at runtime. For this guide, we will calculate the 1024-th number (corresponding to hint value `10` as `2^10=1024`) in the sequence. | ||
|
||
Execute the following command in the Ceno repository root directory to run the Fibonacci example: | ||
|
||
```sh | ||
cargo ceno run --example fibonacci --hints=10 --public-io=4191 | ||
``` | ||
|
||
Let's break down the command: | ||
|
||
- `cargo ceno run`: This is the command to run a Ceno program. | ||
- `--example fibonacci`: This specifies that we want to run the `fibonacci` example. | ||
- `--hints=10`: This is a private input to our program. In this case, it tells the program to run 2^10 (1024) Fibonacci steps. | ||
- `--public-io=4191`: This is the expected public output. The program will verify that the result of the computation matches this value. | ||
|
||
If the command runs successfully, you have just run your first ZK program with Ceno! The next chapter will dive into the code for this example. |
This file contains hidden or 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,12 @@ | ||
# Guest Programming (`ceno_rt`) | ||
|
||
The `ceno_rt` crate provides the runtime environment for guest programs running inside the Ceno ZKVM. It offers essential functionalities for interacting with the host and the ZKVM environment. | ||
|
||
Key features of `ceno_rt` include: | ||
|
||
- **I/O:** Functions for reading input from the host and writing output. See the `ceno_rt::io` module. | ||
- **Memory Management:** A simple allocator for dynamic memory allocation within the guest. See the `ceno_rt::allocator` module. | ||
- **Syscalls:** Low-level functions to access precompiled operations for performance-critical tasks. See the `ceno_rt::syscalls` module and the "Accelerated Operations with Precompiles" chapter. | ||
- **Panicking:** Macros and functions for handling unrecoverable errors in the guest. | ||
|
||
When writing a guest program, you will typically include `ceno_rt` as a dependency in your `Cargo.toml`. |
This file contains hidden or 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,57 @@ | ||
# Host-Guest Interaction | ||
|
||
A critical aspect of developing ZK applications is understanding how the "host" (the machine running the prover) and the "guest" (the ZK program running inside the vm) communicate with each other. | ||
|
||
In Ceno, this communication happens in two main ways: | ||
1. **Private Inputs (Hints)**: The host can pass private data to the guest. | ||
2. **Public Inputs/Outputs (I/O)**: The guest can receive public data and commit to public outputs that the host can verify. | ||
|
||
We saw both of these in the command used to run the Fibonacci example: | ||
|
||
```sh | ||
cargo ceno run --example fibonacci --hints=10 --public-io=4191 | ||
``` | ||
|
||
## Private Inputs (Hints) | ||
|
||
Private inputs, which Ceno refers to as "hints," are data known only to the host and the guest. They are not revealed publicly and do not become part of the final proof. This is the primary way to provide secret inputs to your ZK program. | ||
|
||
In the guest code, you use the `ceno_rt::read()` function to access this data. | ||
|
||
**Guest Code:** | ||
```rust | ||
// Reads the private hint value provided by the host. | ||
let log_n: &Archived<u32> = ceno_rt::read(); | ||
let log_n: u32 = log_n.into(); | ||
``` | ||
|
||
**Host Command:** | ||
```sh | ||
... --hints=10 ... | ||
``` | ||
|
||
In this interaction, the value `10` is passed from the host to the guest. The guest program reads this value and uses it to determine how many Fibonacci iterations to perform. This input remains private. | ||
|
||
## Public Inputs and Outputs | ||
|
||
Public I/O is data that is known to both the host and the verifier. It is part of the public record and is used to ensure the ZK program is performing the correct computation on the correct public data. | ||
|
||
In Ceno, the guest program can commit data to the public record using the `ceno_rt::commit()` function. | ||
|
||
**Guest Code:** | ||
```rust | ||
// Commits the final result `b` to the public output. | ||
ceno_rt::commit::<Archived<u32>, _>(&b); | ||
``` | ||
|
||
**Host Command:** | ||
```sh | ||
... --public-io=4191 ... | ||
``` | ||
|
||
Here, the guest calculates the final Fibonacci number and commits the result `b`. The Ceno host environment then checks that this committed value is equal to the value provided in the `--public-io` argument (`4191`). If they do not match, the proof will fail, indicating an incorrect computation or a different result than expected. | ||
|
||
This mechanism is crucial for creating verifiable computations. You can use public I/O to: | ||
|
||
- Provide public inputs that the program must use. | ||
- Assert that the program produces a specific, known public output. |
This file contains hidden or 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 @@ | ||
# Integration with Ethereum |
This file contains hidden or 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 @@ | ||
# On-chain verification |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to add
rust-toolchain.toml
specify as nightly build as currently we rely on some features not in stable channel. Otherwise it will suffer build failed