Skip to content
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

Multi version and no-std #4

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .cargo/audit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[advisories]
ignore = [
"RUSTSEC-2024-0388", # Just linked in test from risc0-zkvm for groth16
"RUSTSEC-2024-0370", # Just linked in test from risc0-zkvm for bonsai-sdk
]
informational_warnings = ["unmaintained", "unsound"]

[output]
deny = ["warnings", "unsound", "yanked"]
quiet = false
12 changes: 3 additions & 9 deletions .github/workflows/CI-ci-remote.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,13 @@ jobs:
with:
command: install
args: --debug cargo-make
- name: Run CI full (stable)
uses: actions-rs/cargo@v1
with:
toolchain: stable
command: make
args: ci-remote
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
- name: Run CI check-deps only (nightly)
- name: Run CI full
uses: actions-rs/cargo@v1
with:
toolchain: nightly
toolchain: stable
command: make
args: udeps
args: ci-remote
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
/Cargo.lock
lcov.info
lcov.info
resources/cases/temp
flamegraph*
34 changes: 28 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "risc0-verifier"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/HorizenLabs/risc0-verifier"
Expand All @@ -9,13 +9,35 @@ description = "A Rust library to verify risc0 STARK proofs"
keywords = ["crypto", "no-std", "blockchain", "cryptography", "risc0"]

[dependencies]
risc0-zkvm = { version = "=1.0.1", default-features = false }
risc0-zkp = "=1.0.1"
bincode = "1.3"
snafu = { version = "0.8.5", default-features = false }
serde = { version = "1.0.215", default-features = false, features = ["derive"] }
ciborium = { version = "0.2.2", default-features = false }
risc0-core = { version = "=1.2.0", default-features = false }
risc0-zkp = { version = "=1.2.0", default-features = false }
risc0-binfmt = { version = "=1.2.0", default-features = false }
risc0-circuit-rv32im = { version = "=1.2.0", default-features = false }
risc0-circuit-recursion = { version = "=1.2.0", default-features = false }
anyhow = { version = "1.0.93", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
bytemuck = { version = "1.20.0", default-features = false }
log = { version = "0.4.22", default-features = false }
digest = { version = "0.10", features = ["oid"] }

[dev-dependencies]
snafu = { version = "0.8.5", default-features = false }
serde_json = "1.0.114"
serde = "1.0.197"
hex = "0.4.3"
rstest = "0.23.0"
anyhow = "1.0.93"
ciborium = "0.2.2"
bincode = "1.3"
divan = "0.1.17"
rstest_reuse = "0.7.0"
hex-literal = "0.4.1"
sha2 = "0.10.8"

[[bench]]
name = "base"
harness = false

[profile.bench]
debug = true
1 change: 1 addition & 0 deletions HEADER-APACHE2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2024, Horizen Labs, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
38 changes: 25 additions & 13 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ workspace = false
command = "cargo"
args = ["clean"]

[tasks.install-bare-metal]
command = "rustup"
args = ["target", "add", "thumbv7em-none-eabi"]

[tasks.build-bare-metal]
dependencies = ["install-bare-metal"]
command = "cargo"
args = ["build", "--no-default-features", "--target", "thumbv7em-none-eabi"]

[tasks.build]
command = "cargo"
args = ["build"]
Expand Down Expand Up @@ -38,13 +47,19 @@ dependencies = ["clippy-inst"]
command = "cargo"
args = ["clippy", "--", "--deny", "warnings"]

[tasks.compile-header-fix]
command = "cargo"
args = ["build", "--manifest-path", "header-fix/Cargo.toml", "--release"]

[tasks.header-add]
script = { file = "./scripts/add_header_if_missing.sh" }
args = ["HEADER-APACHE2", "./!(target)/**/*.rs"]
dependencies = ["compile-header-fix"]
command = "./header-fix/target/release/header-fix"
args = ["--glob", "**/*.rs", "HEADER-APACHE2"]

[tasks.header-check]
env = { CHECK_DIRTY = "true", DRY_RUN = "true" }
run_task = "header-add"
dependencies = ["compile-header-fix"]
command = "./header-fix/target/release/header-fix"
args = ["--check", "--glob", "**/*.rs", "HEADER-APACHE2"]

[tasks.audit-inst]
command = "cargo"
Expand All @@ -60,24 +75,21 @@ command = "cargo"
args = ["llvm-cov", "--workspace", "--lcov", "--output-path", "lcov.info"]

[tasks.udeps-inst]
toolchain = "nightly"
command = "cargo"
args = ["install", "cargo-udeps", "--locked"]

[tasks.udeps]
dependencies = ["udeps-inst"]
toolchain = "nightly"
command = "cargo"
args = ["udeps", "--all-targets"]

[tasks.ci-common]
dependencies = ["build", "build-bare-metal", "test", "clippy", "audit", "udeps"]

[tasks.ci]
dependencies = ["build", "test", "format", "header-add", "clippy", "audit"]
dependencies = ["format", "header-add", "ci-common"]

[tasks.ci-remote]
dependencies = [
"build",
"test",
"format-check",
"header-check",
"clippy",
"audit",
]
dependencies = ["format-check", "header-check", "ci-common"]
92 changes: 80 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,89 @@
# risc0-verifier

A verifier for [RISC-Zero](https://github.com/risc0/risc0) STARK proofs.
A `no-std` verifier for [RISC-Zero](https://github.com/risc0/risc0) STARK proofs.

This crate provides a way for deserializing the proof and the verification key (aka image id) and a function to check if the proof is correct:
This crate provides a way for deserializing

- `Proof` (aka risc0 receipt)
- the verification key `Vk` (aka risc0 image id)
- public inputs `Journal` (aka risc0 journal):

```rust
use risc0_verifier::{verify};
use risc0_verifier::*;
use std::path::PathBuf;
use serde::Deserialize;
use std::fs::File;

#[derive(Deserialize)]
struct Data {
vk: [u32; 8],
proof: String,
pubs: String,
pub struct Case {
pub receipt_path: PathBuf,
pub journal: Journal,
pub vk: Vk,
}

let Data { vk, proof, pubs } =
serde_json::from_reader(std::fs::File::open("./resources/valid_proof_1.json").unwrap()).unwrap();
let Case { receipt_path, journal, vk } =
serde_json::from_reader(
std::fs::File::open("./resources/cases/prover_1.2.0/vm_1.2.0/poseidon2_22.json").unwrap()
).unwrap();

let proof = <Vec<u8>>::try_from(hex::decode(proof).unwrap()).unwrap();
let pubs = <Vec<u8>>::try_from(hex::decode(pubs).unwrap()).unwrap();
let proof = ciborium::from_reader(File::open(receipt_path).unwrap()).unwrap();

assert!(verify(vk.into(), &proof, &pubs).is_ok());
assert!(verify(vk, proof, journal).is_ok());
```

## Save a risc0 receipt

`risc0-verifier` accepts _any_ **serde** serialized risc0 `Receipt` that doesn't
contain groth16 proof. So, is you have a risc0's `Receipt` you can just serialize it
with `serde` in your preferred format (i.e. `ciborium` or `json`) and the deserialize
it to use with `risc0-verifier`. You can do the same thing with the `Journal` because the
serialized risc0's `Journal` can be deserialized for `risc0-verifier` as well. For the
`Vk` the risc0 image key bytes can be used directly to build it:

```rust
use risc0_verifier::Vk;
let vk : Vk = hex_literal::hex!("9db9988d9fbcacadf2bd29fc7c60b98bc4234342fe536eb983169eb6cc248009").into();
let r0 : risc0_zkp::core::digest::Digest = [
2375596445,
2913778847,
4230594034,
2344181884,
1111696324,
3111015422,
3063813763,
159392972
].into();

assert_eq!(vk.as_words(), r0.as_words());
assert_eq!(vk.as_bytes(), r0.as_bytes());
```

## Verify a proof generate with an old risc0 version

If you need to verify a proof generated with an old risc0 prover version, for instance the `1.1.3`,
you can use [`verify_with_context`] instead:

```rust
use risc0_verifier::*;
use std::path::PathBuf;
use serde::Deserialize;
use std::fs::File;

#[derive(Deserialize)]
pub struct Case {
pub receipt_path: PathBuf,
pub journal: Journal,
pub vk: Vk,
}

let Case { receipt_path, journal, vk } =
serde_json::from_reader(
std::fs::File::open("./resources/cases/prover_1.1.3/vm_1.1.3/poseidon2_22.json").unwrap()
).unwrap();

let proof = ciborium::from_reader(File::open(receipt_path).unwrap()).unwrap();

assert!(verify_with_context(&VerifierContext::v1_1(), vk, proof, journal).is_ok());
```

## Develop
Expand All @@ -38,6 +100,12 @@ to run all CI's steps. You can also use `makers ci` and bypass `cargo` wrapper.
Another useful defined task is `coverage` that executes tests and compute code
coverage file `lcov.info`.

## Generate the proofs

In `generate_proofs` you can find a both a simple risc0 method and a program that
generate several proofs (different configurations) for a given compiled methods. You
can find some notes about how to generate sample proofs in the file `generate_proofs/notes.md`

## License

These crates are released under the [APACHE 2.0 license](LICENSE-APACHE2)
Loading
Loading