diff --git a/crates/sdk/src/client.rs b/crates/sdk/src/client.rs index 996d96c21..86d664cb4 100644 --- a/crates/sdk/src/client.rs +++ b/crates/sdk/src/client.rs @@ -1,8 +1,14 @@ use crate::{ local::{LocalProver, LocalProverBuilder}, network::{NetworkProver, NetworkProverBuilder}, + opts::ProofOpts, + proof::SP1ProofWithPublicValues, prover::Prover, }; +use anyhow::Result; +use sp1_core_executor::ExecutionReport; +use sp1_core_machine::io::SP1Stdin; +use sp1_prover::{SP1ProvingKey, SP1VerifyingKey}; use std::env; pub struct None; @@ -24,6 +30,40 @@ impl ProverClient { pub fn new() -> Self { Self::builder().from_env() } + + pub async fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) { + self.inner.setup(elf).await + } + + pub async fn execute(&self, elf: &[u8], stdin: SP1Stdin) -> Result { + self.inner.execute(elf, stdin).await + } + + pub async fn prove( + &self, + pk: &SP1ProvingKey, + stdin: &SP1Stdin, + ) -> Result { + let opts = ProofOpts::default(); + self.inner.prove_with_options(pk, stdin, &opts).await + } + + pub async fn prove_with_options( + &self, + pk: &SP1ProvingKey, + stdin: &SP1Stdin, + opts: &ProofOpts, + ) -> Result { + self.inner.prove_with_options(pk, stdin, opts).await + } + + pub async fn verify( + &self, + proof: &SP1ProofWithPublicValues, + vk: &SP1VerifyingKey, + ) -> Result<(), crate::provers::SP1VerificationError> { + self.inner.verify(proof, vk).await + } } impl ProverClientBuilder { diff --git a/crates/sdk/src/opts.rs b/crates/sdk/src/opts.rs index d36629876..f9a32ff8b 100644 --- a/crates/sdk/src/opts.rs +++ b/crates/sdk/src/opts.rs @@ -5,3 +5,10 @@ pub struct ProofOpts { pub timeout: u64, pub cycle_limit: u64, } + +impl Default for ProofOpts { + fn default() -> Self { + // TODO better defaults + Self { mode: Mode::default(), timeout: 10000, cycle_limit: 100000000 } + } +} diff --git a/examples/Cargo.lock b/examples/Cargo.lock index d429dd84b..0812a6305 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -1828,6 +1828,12 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "downcast-rs" version = "1.2.1" @@ -2121,6 +2127,7 @@ dependencies = [ name = "fibonacci-script" version = "1.1.0" dependencies = [ + "dotenv", "futures", "hex", "sp1-build", diff --git a/examples/fibonacci/script/Cargo.toml b/examples/fibonacci/script/Cargo.toml index 57ceb5d50..e9d28ec06 100644 --- a/examples/fibonacci/script/Cargo.toml +++ b/examples/fibonacci/script/Cargo.toml @@ -11,6 +11,7 @@ sp1-sdk = { workspace = true, features = ["network-v2"] } hex = "0.4.3" tokio = { version = "1.0", features = ["full", "macros"] } futures = "0.3" +dotenv = "0.15" [build-dependencies] sp1-build = { workspace = true } diff --git a/examples/fibonacci/script/bin/network.rs b/examples/fibonacci/script/bin/network.rs index a6af15d4d..7be786aba 100644 --- a/examples/fibonacci/script/bin/network.rs +++ b/examples/fibonacci/script/bin/network.rs @@ -1,6 +1,7 @@ use sp1_sdk::network_v2::{Error, FulfillmentStrategy}; use sp1_sdk::{include_elf, utils, client::ProverClient, proof::SP1ProofWithPublicValues, SP1Stdin}; use std::time::Duration; +use dotenv::dotenv; /// The ELF we want to execute inside the zkVM. const ELF: &[u8] = include_elf!("fibonacci-program"); @@ -18,23 +19,28 @@ async fn main() { let mut stdin = SP1Stdin::new(); stdin.write(&n); + dotenv().ok(); + + let rpc_url = std::env::var("PROVER_NETWORK_RPC").unwrap(); + let private_key = std::env::var("SP1_PRIVATE_KEY").unwrap(); + // Generate the proof, using the specified network configuration. let client = ProverClient::builder() .network() - .with_rpc_url("https://...".to_string()) - .with_private_key("0x...".to_string()) + .with_rpc_url(rpc_url) + .with_private_key(private_key) .build(); // Generate the proving key and verifying key for the given program. - let (pk, vk) = client.setup(ELF).await.unwrap(); + let (pk, vk) = client.setup(ELF).await; // Generate the proof. let proof_result = client - .prove(&pk, stdin) - .timeout(300) - .cycle_limit(1_000_000) - .skip_simulation(true) - .strategy(FulfillmentStrategy::Hosted) + .prove(&pk, &stdin) + // .timeout(300) + // .cycle_limit(1_000_000) + // .skip_simulation(true) + // .strategy(FulfillmentStrategy::Hosted) .await; // Example of handling potential errors. @@ -81,7 +87,7 @@ async fn main() { println!("b: {}", b); // Verify proof and public values - client.verify(&proof, &vk).expect("verification failed"); + // 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"); @@ -89,7 +95,7 @@ async fn main() { SP1ProofWithPublicValues::load("proof-with-pis.bin").expect("loading proof failed"); // Verify the deserialized proof. - client.verify(&deserialized_proof, &vk).expect("verification failed"); + // client.verify(&deserialized_proof, &vk).expect("verification failed"); println!("successfully generated and verified proof for the program!") }