Skip to content

Commit

Permalink
Merge pull request #35 from entropyxyz/example-risc0-verify
Browse files Browse the repository at this point in the history
Example for RISC0 zkVM Verification
  • Loading branch information
jakehemmerle authored Nov 2, 2023
2 parents 596a546 + bec010d commit 3b852fe
Show file tree
Hide file tree
Showing 23 changed files with 1,115 additions and 14 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ ec-acl = { path = "acl", default-features = false }
ec-evm = { path = "evm", default-features = false }
ec-runtime = { path = "runtime", default-features = false }
wit-bindgen = { version = "0.7.0", default_features = false }
risc0-zkvm = { git = "https://github.com/risc0/risc0", tag = "v0.18.0", default-features = false }
risc0-build = { git = "https://github.com/risc0/risc0", tag = "v0.18.0" }

# strip debug info since that makes up a major part of Wasm blobs, see Wasm's `twiggy`
[profile.release.package.template-basic-transaction]
strip = "debuginfo"
[profile.release.package.template-barebones]
strip = "debuginfo"
[profile.release.package."*"]
[profile.release]
strip = "debuginfo"
22 changes: 22 additions & 0 deletions examples/barebones-with-extra/Cargo.toml
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]
77 changes: 77 additions & 0 deletions examples/barebones-with-extra/src/lib.rs
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());
}
}
4 changes: 2 additions & 2 deletions examples/barebones/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod tests {
fn test_should_sign() {
let signature_request = InitialState {
preimage: "some_data_longer_than_10_bytes".to_string().into_bytes(),
extra: None
extra: None,
};

assert!(BarebonesProgram::evaluate(signature_request).is_ok());
Expand All @@ -52,7 +52,7 @@ mod tests {
// data being checked is under 10 bytes in length
let signature_request = InitialState {
preimage: "under10".to_string().into_bytes(),
extra: None
extra: None,
};

assert!(BarebonesProgram::evaluate(signature_request).is_err());
Expand Down
34 changes: 34 additions & 0 deletions examples/risczero-zkvm-verification/Cargo.toml
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]
15 changes: 15 additions & 0 deletions examples/risczero-zkvm-verification/json/Cargo.toml
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"]
24 changes: 24 additions & 0 deletions examples/risczero-zkvm-verification/json/README.md
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).
8 changes: 8 additions & 0 deletions examples/risczero-zkvm-verification/json/core/Cargo.toml
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"
22 changes: 22 additions & 0 deletions examples/risczero-zkvm-verification/json/core/src/lib.rs
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 examples/risczero-zkvm-verification/json/methods/Cargo.toml
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"]
17 changes: 17 additions & 0 deletions examples/risczero-zkvm-verification/json/methods/build.rs
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();
}
Loading

0 comments on commit 3b852fe

Please sign in to comment.