Skip to content

Commit

Permalink
feat(raiko): load program from elf for risc zero (#194)
Browse files Browse the repository at this point in the history
* include_bytes!

* sgx default not enable

* no dummy-elf

* relative path

* test,bench

* fmt

* test,bench

* RUSTFLAGS='-C target-cpu=native'

---------

Co-authored-by: d1onys1us <[email protected]>
Co-authored-by: Brecht Devos <[email protected]>
  • Loading branch information
3 people authored May 14, 2024
1 parent 09e0005 commit dc0a427
Show file tree
Hide file tree
Showing 22 changed files with 98 additions and 185,786 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ rand_core = "0.6.4"
base64-serde = "0.7.0"
base64 = "0.21.7"
dirs = "5.0.1"
pathdiff = "0.2.1"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Look into `prove-block.sh` for the available options or run the script without i
```

## Provers
For all host program, you can enable CPU optimization through exporting `CPU_OPT=1`.
### Risc zero
To install, build, and run in one step:
```console
Expand Down Expand Up @@ -84,6 +85,7 @@ To build and run test on Sp1 Zkvm:
```console
$ TARGET=sp1 make test
```
Some optimized configuration tailored to the host can be found [here](docs/README_Sp1.md)

### SGX:
To install, build, and run in one step:
Expand All @@ -101,6 +103,10 @@ If your CPU doesn't support SGX, you can still run the SGX code through gramine
$ MOCK=1 TARGET=sgx make run
```

## Misc
[Docker & Remote Attestation Support](docs/README_Docker_and_RA.md)
[Metrics](docs/README_Metrics.md)

### Execution Trace

You can generate an execution trace for the block that is being proven by enabling the `tracer` feature:
Expand Down
32 changes: 32 additions & 0 deletions docs/README_Sp1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
reference: https://succinctlabs.github.io/sp1/generating-proofs/advanced.html

## CPU Acceleration

To enable CPU acceleration, you can use the `RUSTFLAGS` environment variable to enable the `target-cpu=native` flag when running your script. This will enable the compiler to generate code that is optimized for your CPU. We encapsulate this with `CPU_OPT=1`

```bash
RUSTFLAGS='-C target-cpu=native' cargo run --release
```

Currently there is support for AVX512 and NEON SIMD instructions. For NEON, you must also enable the `sp1-driver` feature `neon`.

```bash
cargo run --features sp1,neon --release
```

## Performance

For maximal performance, you should run proof generation with the following command and vary your `shard_size` depending on your program's number of cycles.

```rust,noplayground
SHARD_SIZE=4194304 RUST_LOG=info RUSTFLAGS='-C target-cpu=native' cargo run --release
```

## Memory Usage

To reduce memory usage, set the `SHARD_BATCH_SIZE` enviroment variable depending on how much RAM
your machine has. A higher number will use more memory, but will be faster.

```rust,noplayground
SHARD_BATCH_SIZE=1 SHARD_SIZE=2097152 RUST_LOG=info RUSTFLAGS='-C target-cpu=native' cargo run --release
```
3 changes: 2 additions & 1 deletion pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ once_cell = { workspace = true }
anyhow = { workspace = true }
dirs = { workspace = true }
risc0-binfmt = { workspace = true, optional = true }
pathdiff = { workspace = true, optional = true }

[features]
risc0 = ["dep:risc0-binfmt"]
risc0 = ["dep:risc0-binfmt", "dep:pathdiff"]
sp1 = []
18 changes: 11 additions & 7 deletions pipeline/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,24 @@ impl Executor {
pub fn risc0_placement(&self, dest: &str) -> Result<()> {
use crate::risc0_util::GuestListEntry;
let root = ROOT_DIR.get().unwrap();
let dest = PathBuf::from(dest);
if !dest.exists() {
fs::create_dir_all(&dest).unwrap();
let dest_dir = PathBuf::from(dest);
if !dest_dir.exists() {
fs::create_dir_all(&dest_dir).unwrap();
}
for src in &self.artifacts {
let mut name = file_name(src);
if self.test {
name = format!("test-{}", name.split('-').collect::<Vec<_>>()[0]).to_string();
}
let mut dest =
File::create(dest.join(&format!("{}.rs", name.replace('-', "_")))).unwrap();
let mut dest_file =
File::create(&dest_dir.join(&format!("{}.rs", name.replace('-', "_")))).unwrap();
let guest = GuestListEntry::build(&name, root.join(src).to_str().unwrap()).unwrap();
dest.write_all(guest.codegen_consts().as_bytes())?;
println!("Write from\n {:?}\nto\n {:?}", src, dest);
dest_file.write_all(
guest
.codegen_consts(&fs::canonicalize(&dest_dir).unwrap())
.as_bytes(),
)?;
println!("Write from\n {:?}\nto\n {:?}", src, dest_file);
}
Ok(())
}
Expand Down
20 changes: 13 additions & 7 deletions pipeline/src/risc0_util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use anyhow::Result;
use std::{borrow::Cow, path::PathBuf};
use std::{
borrow::Cow,
fs,
path::{Path, PathBuf},
};

pub const DIGEST_WORDS: usize = 8;

Expand Down Expand Up @@ -45,21 +49,23 @@ impl GuestListEntry {
})
}

pub fn codegen_consts(&self) -> String {
// Quick check for '#' to avoid injection of arbitrary Rust code into the the
// method.rs file. This would not be a serious issue since it would only
// affect the user that set the path, but it's good to add a check.
pub fn codegen_consts(&self, dest: &PathBuf) -> String {
if self.path.contains('#') {
panic!("method path cannot include #: {}", self.path);
}
let relative_path = pathdiff::diff_paths(
fs::canonicalize(Path::new(&self.path.as_ref())).unwrap(),
dest,
)
.map(|p| String::from(p.to_str().unwrap()))
.unwrap();

let upper = self.name.to_uppercase().replace('-', "_");
let image_id: [u32; DIGEST_WORDS] = self.image_id;
let elf_path: &str = &self.path;
let elf_contents: &[u8] = &self.elf;
format!(
r##"
pub const {upper}_ELF: &[u8] = &{elf_contents:?};
pub const {upper}_ELF: &[u8] = include_bytes!("{relative_path}");
pub const {upper}_ID: [u32; 8] = {image_id:?};
pub const {upper}_PATH: &str = r#"{elf_path}"#;
"##
Expand Down
1 change: 0 additions & 1 deletion provers/risc0/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ serde_json = { workspace = true, optional = true }
hex = { workspace = true, optional = true }

[features]
dummy-elf = [] # use dummy elf bytes to accelerate cargo check
enable = [
"raiko-lib",
"raiko-primitives",
Expand Down
Loading

0 comments on commit dc0a427

Please sign in to comment.