Skip to content

Commit

Permalink
fix rest of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Dec 20, 2024
1 parent a02ff56 commit a1943e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 123 deletions.
97 changes: 0 additions & 97 deletions crates/sdk/src/network/sign_message.rs

This file was deleted.

4 changes: 4 additions & 0 deletions examples/fibonacci/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ path = "bin/compressed.rs"
name = "execute"
path = "bin/execute.rs"

[[bin]]
name = "network"
path = "bin/network.rs"

[[bin]]
name = "fibonacci-script"
path = "src/main.rs"
Expand Down
81 changes: 81 additions & 0 deletions examples/fibonacci/script/bin/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use sp1_sdk::network::Error;
use sp1_sdk::{include_elf, utils, ProverClient, SP1ProofWithPublicValues, SP1Stdin};

/// The ELF we want to execute inside the zkVM.
const ELF: &[u8] = include_elf!("fibonacci-program");

fn main() {
// Setup logging.
utils::setup_logger();

// Create an input stream and write '500' to it.
let n = 1000u32;

// The input stream that the program will read from using `sp1_zkvm::io::read`. Note that the
// types of the elements in the input stream must match the types being read in the program.
let mut stdin = SP1Stdin::new();
stdin.write(&n);

// Create a `ProverClient` method.
let client = ProverClient::from_env();

// Generate the proof for the given program and input.
let (pk, vk) = client.setup(ELF);
let proof_result = client.prove(&pk, &stdin).compressed().run();

// Handle possible prover network errors.
let mut proof = match proof_result {
Ok(proof) => proof,
Err(e) => {
if let Some(network_error) = e.downcast_ref::<Error>() {
match network_error {
Error::SimulationFailed {
eprintln!("Program cannot be simulated: {}", e);
std::process::exit(1);
}
Error::RequestUnexecutable { request_id: _ } => {
eprintln!("Program is unexecutable: {}", e);
std::process::exit(1);
}
Error::RequestUnfulfillable { request_id: _ } => {
eprintln!("Proof request cannot be fulfilled: {}", e);
std::process::exit(1);
}
_ => {
eprintln!("Unexpected error: {}", e);
std::process::exit(1);
}
}
} else {
eprintln!("Unexpected error: {}", e);
std::process::exit(1);
}
}
};

println!("generated proof");

// Read and verify the output.
//
// Note that this output is read from values committed to in the program using
// `sp1_zkvm::io::commit`.
let _ = proof.public_values.read::<u32>();
let a = proof.public_values.read::<u32>();
let b = proof.public_values.read::<u32>();

println!("a: {}", a);
println!("b: {}", b);

// Verify proof and public values
client.verify(&proof, &vk).expect("verification failed");

// Test a round trip of proof serialization and deserialization.
proof.save("proof-with-pis.bin").expect("saving proof failed");
let deserialized_proof =
SP1ProofWithPublicValues::load("proof-with-pis.bin").expect("loading proof failed");

// Verify the deserialized proof.
client.verify(&deserialized_proof, &vk).expect("verification failed");

println!("successfully generated and verified proof for the program!")
}
27 changes: 1 addition & 26 deletions examples/fibonacci/script/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use sp1_sdk::network::Error;
use sp1_sdk::{include_elf, utils, ProverClient, SP1ProofWithPublicValues, SP1Stdin};

/// The ELF we want to execute inside the zkVM.
Expand All @@ -25,31 +24,7 @@ fn main() {

// Generate the proof for the given program and input.
let (pk, vk) = client.setup(ELF);
let proof_result = client.prove(&pk, &stdin).run();
let mut proof = match proof_result {
Ok(proof) => proof,
Err(e) => {
if let Some(network_error) = e.downcast_ref::<Error>() {
match network_error {
Error::RequestUnexecutable { request_id: _ } => {
eprintln!("Program is unexecutable: {}", e);
std::process::exit(1);
}
Error::RequestUnfulfillable { request_id: _ } => {
eprintln!("Proof request cannot be fulfilled: {}", e);
std::process::exit(1);
}
_ => {
eprintln!("Unexpected error: {}", e);
std::process::exit(1);
}
}
} else {
eprintln!("Unexpected error: {}", e);
std::process::exit(1);
}
}
};
let proof = client.prove(&pk, &stdin).run().unwrap();

println!("generated proof");

Expand Down

0 comments on commit a1943e3

Please sign in to comment.