-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from entropyxyz/example-risc0-verify
Example for RISC0 zkVM Verification
- Loading branch information
Showing
23 changed files
with
1,115 additions
and
14 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
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,22 @@ | ||
[package] | ||
name = "example-barebones-with-extra" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
# This is required to compile constraints to a wasm module | ||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
ec-core ={ workspace = true } | ||
|
||
# These are used by `cargo component` | ||
[package.metadata.component] | ||
package = "entropy:example-barebones-with-extra" | ||
|
||
[package.metadata.component.target] | ||
path = "../../wit" | ||
|
||
[package.metadata.component.dependencies] |
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,77 @@ | ||
//! This example demonstrates a contrieved program that can include extra data. Note, only the data in `preimage` will be signed by Entropy; `extra` is used to provide additional data (eg an additional signature or a zkp related to the preimage) that the user requires during program evaluation. | ||
|
||
#![no_std] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::string::ToString; | ||
|
||
use ec_core::{bindgen::Error, bindgen::*, export_program, prelude::*}; | ||
|
||
// TODO confirm this isn't an issue for audit | ||
register_custom_getrandom!(always_fail); | ||
|
||
pub struct BarebonesWithExtra; | ||
|
||
impl Program for BarebonesWithExtra { | ||
/// This is the only function required by the program runtime. `signature_request` is the preimage of the curve element to be | ||
/// signed, eg. RLP-serialized Ethereum transaction request, raw x86_64 executable, etc. | ||
fn evaluate(signature_request: InitialState) -> Result<(), Error> { | ||
let InitialState { preimage, extra } = signature_request; | ||
|
||
// our constraint just checks that the length of the signature request is greater than 10 | ||
if preimage.len() < 10 { | ||
return Err(Error::Evaluation( | ||
"Length of data is too short.".to_string(), | ||
)); | ||
} | ||
|
||
// Just check and make sure the extra field is not empty. | ||
extra.ok_or(Error::Evaluation( | ||
"This program requires that `extra` be `Some`.".to_string(), | ||
))?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
export_program!(BarebonesWithExtra); | ||
|
||
// write a test that calls evaluate and passes it the proper parameters | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use alloc::vec; | ||
|
||
#[test] | ||
fn test_preimage_length_is_valid() { | ||
let signature_request = InitialState { | ||
preimage: "some_data_longer_than_10_bytes".to_string().into_bytes(), | ||
extra: Some(vec![0x00]), | ||
}; | ||
|
||
assert!(BarebonesWithExtra::evaluate(signature_request).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_preimage_length_is_invalid() { | ||
let signature_request = InitialState { | ||
// should error since preimage is less than 10 bytes | ||
preimage: "under10".to_string().into_bytes(), | ||
extra: Some(vec![0x00]), | ||
}; | ||
|
||
assert!(BarebonesWithExtra::evaluate(signature_request).is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_error_when_extra_field_is_none() { | ||
let signature_request = InitialState { | ||
preimage: "some_data_longer_than_10_bytes".to_string().into_bytes(), | ||
// should error since extra field is None | ||
extra: None, | ||
}; | ||
|
||
assert!(BarebonesWithExtra::evaluate(signature_request).is_err()); | ||
} | ||
} |
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
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,34 @@ | ||
[package] | ||
name = "example-risc0" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
# This is required to compile constraints to a wasm module | ||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
ec-core ={ workspace = true } | ||
serde = { version = "1.0", default-features = false, features = ["derive"] } | ||
bincode = "1.3.3" | ||
# json-example = { path = "json" } | ||
risc0-zkvm = { workspace = true } | ||
|
||
[dev-dependencies] | ||
json-core = { path = "json/core" } | ||
json-methods = { path = "json/methods" } | ||
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag = "v0.18.0", default-features = true } | ||
|
||
[features] | ||
std = ["risc0-zkvm/std"] | ||
|
||
# These are used by `cargo component` | ||
[package.metadata.component] | ||
package = "entropy:example-risc0" | ||
|
||
[package.metadata.component.target] | ||
path = "../../wit" | ||
|
||
[package.metadata.component.dependencies] |
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,15 @@ | ||
[package] | ||
name = "json-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
json-core = { path = "core" } | ||
json-methods = { path = "methods" } | ||
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag = "v0.18.0", default-features = false } | ||
serde = "1.0" | ||
|
||
[features] | ||
cuda = ["risc0-zkvm/cuda"] | ||
default = [] | ||
metal = ["risc0-zkvm/metal"] |
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,24 @@ | ||
# JSON Example | ||
|
||
This code demonstrates how to prove that a JSON file contains a specific field and value using the RISC Zero zkVM. The JSON file is identified by SHA-256 hash, allowing users to commit to a specific JSON file and then prove some of its contents without revealing the full file. | ||
|
||
## Quick Start | ||
|
||
First, [install Rust] if you don't already have it. | ||
|
||
Next, install the `cargo-risczero` tool and install the toolchain with: | ||
```bash | ||
cargo install cargo-risczero | ||
cargo risczero install | ||
``` | ||
|
||
Then, run the example with: | ||
```bash | ||
cargo run --release | ||
``` | ||
|
||
[install Rust]: https://doc.rust-lang.org/cargo/getting-started/installation.html | ||
|
||
## Video Tutorial | ||
|
||
For a walk-through of this example, check out this [excerpt from our workshop at ZK HACK III](https://www.youtube.com/watch?v=6vIgBHx61vc&list=PLcPzhUaCxlCgig7ofeARMPwQ8vbuD6hC5&index=7). |
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,8 @@ | ||
[package] | ||
name = "json-core" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag = "v0.18.0", default-features = false } | ||
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,22 @@ | ||
// Copyright 2023 RISC Zero, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use risc0_zkvm::sha::Digest; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] | ||
pub struct Outputs { | ||
pub data: u32, | ||
pub hash: Digest, | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/risczero-zkvm-verification/json/methods/Cargo.toml
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 = "json-methods" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[build-dependencies] | ||
risc0-build = { workspace = true } | ||
|
||
[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,17 @@ | ||
// Copyright 2023 RISC Zero, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
fn main() { | ||
risc0_build::embed_methods(); | ||
} |
Oops, something went wrong.