Skip to content

Commit

Permalink
Align examples with template (risc0#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzerrell authored Mar 31, 2023
1 parent 3cfe642 commit d1e4916
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 22 deletions.
13 changes: 6 additions & 7 deletions examples/factors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ fn main() {

// Multiply them inside the ZKP
// First, we make the prover, loading the 'multiply' method
let mut prover = Prover::new(MULTIPLY_ELF).expect(
"Prover should be constructed from valid method source code and corresponding method ID",
);
let mut prover =
Prover::new(MULTIPLY_ELF).expect("Prover should be constructed from valid ELF binary");

// Next we send a & b to the guest
prover.add_input_u32_slice(&to_vec(&a).expect("should be serializable"));
prover.add_input_u32_slice(&to_vec(&b).expect("should be serializable"));
// Run prover & generate receipt
let receipt = prover
.run()
.expect("Should be able to prove valid code that fits in the cycle count.");
let receipt = prover.run().expect(
"Code should be provable unless it had an error or exceeded the maximum cycle limit",
);

// Extract journal of receipt (i.e. output c, where c = a * b)
let c: u64 = from_slice(&receipt.journal).expect(
Expand All @@ -49,7 +48,7 @@ fn main() {

// Verify receipt, panic if it's wrong
receipt.verify(&MULTIPLY_ID).expect(
"Code you have proven should successfully verify; did you specify the correct method ID?",
"Code you have proven should successfully verify; did you specify the correct image ID?",
);
}

Expand Down
8 changes: 5 additions & 3 deletions examples/json/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ fn main() {
let data = include_str!("../res/example.json");

// Make the prover.
let mut prover = Prover::new(SEARCH_JSON_ELF)
.expect("Prover should be constructed from matching method code & ID");
let mut prover =
Prover::new(SEARCH_JSON_ELF).expect("Prover should be constructed from valid ELF binary");

prover.add_input_u32_slice(&to_vec(&data).expect("should be serializable"));

// Run prover & generate receipt
let receipt = prover.run().expect("Code should be provable");
let receipt = prover.run().expect(
"Code should be provable unless it had an error or exceeded the maximum cycle limit",
);

receipt
.verify(&SEARCH_JSON_ID)
Expand Down
8 changes: 5 additions & 3 deletions examples/sha/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ use sha_methods::{HASH_ELF, HASH_ID};

fn provably_hash(input: &str) -> Receipt {
// Make the prover.
let mut prover = Prover::new(HASH_ELF)
.expect("Prover should be constructed from matching code and method ID");
let mut prover =
Prover::new(HASH_ELF).expect("Prover should be constructed from valid ELF binary");

prover.add_input_u32_slice(&to_vec(input).expect("input string should serialize"));

// Run prover & generate receipt
prover.run().expect("Code should be provable")
prover.run().expect(
"Code should be provable unless it had an error or exceeded the maximum cycle limit",
)
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions risc0/cargo-risczero/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {

assert!(find_in_file(
"#![no_std]",
&proj_path.join("methods/guest/src/bin/method_name.rs")
&proj_path.join("methods/guest/src/main.rs")
));
}

Expand Down Expand Up @@ -281,7 +281,7 @@ mod tests {

assert!(!find_in_file(
"#![no_std]",
&proj_path.join("methods/guest/src/bin/method_name.rs")
&proj_path.join("methods/guest/src/main.rs")
));
assert!(!find_in_file(
"feature = ['std']",
Expand Down
2 changes: 1 addition & 1 deletion templates/rust-starter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"methods",
"host",
"methods",
]

# Always optimize; building and running the guest takes much longer without optimization.
Expand Down
4 changes: 2 additions & 2 deletions templates/rust-starter/host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "starter"
name = "host"
version = "0.1.0"
edition = "2021"

[dependencies]
methods = { path = "../methods" }
risc0-zkvm = { {{ risc0_zkvm }} }
serde = "1.0"
serde = "1.0"
5 changes: 4 additions & 1 deletion templates/rust-starter/host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// `METHOD_NAME_ID` with `MULTIPLY_ID`
use methods::{METHOD_NAME_ELF, METHOD_NAME_ID};
use risc0_zkvm::Prover;
// TODO: Uncomment the `use` line below for serialization helper functions for
// communication with the guest
// use risc0_zkvm::serde::{from_slice, to_vec}

fn main() {
// Make the prover.
Expand All @@ -22,6 +25,6 @@ fn main() {
// Optional: Verify receipt to confirm that recipients will also be able to
// verify your receipt
receipt.verify(&METHOD_NAME_ID).expect(
"Code you have proven should successfully verify; did you specify the correct method ID?",
"Code you have proven should successfully verify; did you specify the correct image ID?",
);
}
3 changes: 2 additions & 1 deletion templates/rust-starter/methods/guest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "methods-guest"
# TODO: Rename this package to change the method name from METHOD_NAME
name = "method_name"
version = "0.1.0"
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// TODO: Rename this file to change the name of this method from METHOD_NAME

#![no_main]
{% unless risc0_std -%}
// If you want to try std support, also update the guest Cargo.toml file
#![no_std] // std support is experimental
{% endunless %}

// TODO: Uncomment the line below for tools for communicating with the host
// use risc0_zkvm::guest::env;

risc0_zkvm::guest::entry!(main);

pub fn main() {
Expand Down

0 comments on commit d1e4916

Please sign in to comment.