From ebf0545d6168b9e538a13e359e09ec3478821795 Mon Sep 17 00:00:00 2001 From: Nabih Date: Wed, 22 Oct 2025 13:56:27 +0200 Subject: [PATCH] Remove an unwrap from prover setup function --- crates/build/src/lib.rs | 23 +++++++++++++++++------ crates/prover/src/build.rs | 7 ++++++- crates/prover/src/lib.rs | 18 ++++++++++++++---- crates/sdk/src/cpu/mod.rs | 13 +++++++++---- crates/sdk/src/cuda/mod.rs | 13 +++++++++---- crates/sdk/src/env/mod.rs | 16 +++++++++++----- crates/sdk/src/network/prover.rs | 17 ++++++++++------- crates/sdk/src/prover.rs | 12 ++++++------ 8 files changed, 82 insertions(+), 37 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index ec2851707f..c354f01fda 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -3,6 +3,7 @@ mod command; mod utils; use std::{collections::HashMap, fs::File, io::Read}; +use anyhow::Error; use build::build_program_internal; pub use build::{execute_build_program, generate_elf_paths}; pub use command::TOOLCHAIN_NAME; @@ -156,7 +157,7 @@ pub fn build_program_with_args(path: &str, args: BuildArgs) { /// /// Note: If used in a script `build.rs`, this function should be called *after* [`build_program`] /// to returns the vkey corresponding to the latest program version which has just been compiled. -pub fn vkey(path: &str, target_name: &str) -> String { +pub fn vkey(path: &str, target_name: &str) -> Result { let program_dir = std::path::Path::new(path); let metadata_file = program_dir.join("Cargo.toml"); let mut metadata_cmd = cargo_metadata::MetadataCommand::new(); @@ -170,8 +171,13 @@ pub fn vkey(path: &str, target_name: &str) -> String { let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); - let (_, _, _, vk) = prover.setup(&elf); - vk.bytes32() + let (_, _, _, vk) = match prover.setup(&elf) { + Ok(res) => res, + Err(e) => { + return Err(Error::msg(format!("Failed to setup prover: {}", e))); + } + }; + Ok(vk.bytes32()) } /// Returns the verification keys for the provided programs in a [`HashMap`] with the target names @@ -184,7 +190,7 @@ pub fn vkey(path: &str, target_name: &str) -> String { /// /// Note: If used in a script `build.rs`, this function should be called *after* [`build_program`] /// to returns the vkey corresponding to the latest program version which has just been compiled. -pub fn vkeys(path: &str, args: BuildArgs) -> HashMap { +pub fn vkeys(path: &str, args: BuildArgs) -> Result, Error> { let program_dir = std::path::Path::new(path); let metadata_file = program_dir.join("Cargo.toml"); let mut metadata_cmd = cargo_metadata::MetadataCommand::new(); @@ -200,10 +206,15 @@ pub fn vkeys(path: &str, args: BuildArgs) -> HashMap { let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); - let (_, _, _, vk) = prover.setup(&elf); + let (_, _, _, vk) = match prover.setup(&elf) { + Ok(res) => res, + Err(e) => { + return Err(Error::msg(format!("Failed to setup prover: {}", e))); + } + }; let vk = vk.bytes32(); - (target_name, vk) + Ok((target_name, vk)) }) .collect() } diff --git a/crates/prover/src/build.rs b/crates/prover/src/build.rs index b6a25c6077..eaed0b0658 100644 --- a/crates/prover/src/build.rs +++ b/crates/prover/src/build.rs @@ -157,7 +157,12 @@ pub fn dummy_proof() -> (StarkVerifyingKey, ShardProof) { let context = SP1Context::default(); tracing::info!("setup elf"); - let (_, pk_d, program, vk) = prover.setup(elf); + let (_, pk_d, program, vk) = match prover.setup(elf) { + Ok(res) => res, + Err(e) => { + panic!("Failed to setup prover: {}", e); + } + }; tracing::info!("prove core"); let mut stdin = SP1Stdin::new(); diff --git a/crates/prover/src/lib.rs b/crates/prover/src/lib.rs index ece896a71f..c7f919a187 100644 --- a/crates/prover/src/lib.rs +++ b/crates/prover/src/lib.rs @@ -268,8 +268,13 @@ impl SP1Prover { pub fn setup( &self, elf: &[u8], - ) -> (SP1ProvingKey, DeviceProvingKey, Program, SP1VerifyingKey) { - let program = self.get_program(elf).unwrap(); + ) -> eyre::Result<(SP1ProvingKey, DeviceProvingKey, Program, SP1VerifyingKey)> { + let program = match self.get_program(elf) { + Ok(prog) => prog, + Err(e) => { + return Err(eyre::eyre!("Failed to parse ELF into program: {}", e)); + } + }; let (pk, vk) = self.core_prover.setup(&program); let vk = SP1VerifyingKey { vk }; let pk = SP1ProvingKey { @@ -278,12 +283,17 @@ impl SP1Prover { vk: vk.clone(), }; let pk_d = self.core_prover.pk_to_device(&pk.pk); - (pk, pk_d, program, vk) + Ok((pk, pk_d, program, vk)) } /// Get a program with an allowed preprocessed shape. pub fn get_program(&self, elf: &[u8]) -> eyre::Result { - let mut program = Program::from(elf)?; + let mut program = match Program::from(elf) { + Ok(prog) => prog, + Err(e) => { + return Err(eyre::eyre!("Failed to parse ELF into program: {}", e)); + } + }; if let Some(core_shape_config) = &self.core_shape_config { core_shape_config.fix_preprocessed_shape(&mut program)?; } diff --git a/crates/sdk/src/cpu/mod.rs b/crates/sdk/src/cpu/mod.rs index 9de4479030..fda01de8fb 100644 --- a/crates/sdk/src/cpu/mod.rs +++ b/crates/sdk/src/cpu/mod.rs @@ -6,7 +6,7 @@ pub mod builder; pub mod execute; pub mod prove; -use anyhow::Result; +use anyhow::{Error, Result}; use execute::CpuExecuteBuilder; use prove::CpuProveBuilder; use sp1_core_executor::{SP1Context, SP1ContextBuilder}; @@ -210,9 +210,14 @@ impl CpuProver { } impl Prover for CpuProver { - fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) { - let (pk, _, _, vk) = self.prover.setup(elf); - (pk, vk) + fn setup(&self, elf: &[u8]) -> Result<(SP1ProvingKey, SP1VerifyingKey), Error> { + let (pk, _, _, vk) = match self.prover.setup(elf) { + Ok(res) => res, + Err(e) => { + return Err(Error::msg(format!("Failed to setup prover: {}", e))); + } + }; + Ok((pk, vk)) } fn inner(&self) -> &SP1Prover { diff --git a/crates/sdk/src/cuda/mod.rs b/crates/sdk/src/cuda/mod.rs index 4e6e399ab0..8f94b2dd73 100644 --- a/crates/sdk/src/cuda/mod.rs +++ b/crates/sdk/src/cuda/mod.rs @@ -5,7 +5,7 @@ pub mod builder; pub mod prove; -use anyhow::Result; +use anyhow::{Error, Result}; use prove::CudaProveBuilder; use sp1_core_executor::SP1ContextBuilder; use sp1_core_machine::io::SP1Stdin; @@ -160,9 +160,14 @@ impl CudaProver { } impl Prover for CudaProver { - fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) { - let (pk, vk) = self.cuda_prover.setup(elf).unwrap(); - (pk, vk) + fn setup(&self, elf: &[u8]) -> Result<(SP1ProvingKey, SP1VerifyingKey), Error> { + let (pk, vk) = match self.cuda_prover.setup(elf) { + Ok(res) => res, + Err(e) => { + return Err(Error::msg(format!("Failed to setup prover: {}", e))); + } + }; + Ok((pk, vk)) } fn inner(&self) -> &SP1Prover { diff --git a/crates/sdk/src/env/mod.rs b/crates/sdk/src/env/mod.rs index 104a60a45b..7b958e94d9 100644 --- a/crates/sdk/src/env/mod.rs +++ b/crates/sdk/src/env/mod.rs @@ -7,7 +7,7 @@ pub mod prove; use std::env; -use anyhow::Result; +use anyhow::{Error, Result}; use prove::EnvProveBuilder; use sp1_core_executor::SP1ContextBuilder; use sp1_core_machine::io::SP1Stdin; @@ -160,8 +160,11 @@ impl EnvProver { /// Setup a program to be proven and verified by the SP1 RISC-V zkVM by computing the proving /// and verifying keys. #[must_use] - pub fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) { - self.prover.setup(elf) + pub fn setup(&self, elf: &[u8]) -> Result<(SP1ProvingKey, SP1VerifyingKey), Error> { + match self.prover.setup(elf) { + Ok(res) => Ok(res), + Err(e) => Err(Error::msg(format!("Failed to setup prover: {}", e))), + } } } @@ -176,8 +179,11 @@ impl Prover for EnvProver { self.prover.inner() } - fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) { - self.prover.setup(elf) + fn setup(&self, elf: &[u8]) -> Result<(SP1ProvingKey, SP1VerifyingKey), Error> { + match self.prover.setup(elf) { + Ok(res) => Ok(res), + Err(e) => Err(Error::msg(format!("Failed to setup prover: {}", e))), + } } fn prove( diff --git a/crates/sdk/src/network/prover.rs b/crates/sdk/src/network/prover.rs index 98dfe1c40f..cf0b1a788d 100644 --- a/crates/sdk/src/network/prover.rs +++ b/crates/sdk/src/network/prover.rs @@ -690,11 +690,11 @@ impl NetworkProver { if let Some(network_error) = e.downcast_ref::() { if matches!( network_error, - Error::RequestUnfulfillable { .. } | - Error::RequestTimedOut { .. } | - Error::RequestAuctionTimedOut { .. } - ) && strategy == FulfillmentStrategy::Auction && - whitelist.is_none() + Error::RequestUnfulfillable { .. } + | Error::RequestTimedOut { .. } + | Error::RequestAuctionTimedOut { .. } + ) && strategy == FulfillmentStrategy::Auction + && whitelist.is_none() { tracing::warn!( "Retrying auction request with fallback whitelist..." @@ -888,8 +888,11 @@ impl NetworkProver { } impl Prover for NetworkProver { - fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey) { - self.prover.setup(elf) + fn setup(&self, elf: &[u8]) -> Result<(SP1ProvingKey, SP1VerifyingKey), anyhow::Error> { + match self.prover.setup(elf) { + Ok((pk, vk)) => Ok((pk, vk)), + Err(e) => Err(anyhow::anyhow!(e)), + } } fn inner(&self) -> &SP1Prover { diff --git a/crates/sdk/src/prover.rs b/crates/sdk/src/prover.rs index da5e79ee39..312f51adf8 100644 --- a/crates/sdk/src/prover.rs +++ b/crates/sdk/src/prover.rs @@ -4,7 +4,7 @@ use std::borrow::Borrow; -use anyhow::Result; +use anyhow::{Error, Result}; use itertools::Itertools; use p3_field::PrimeField32; use sp1_core_executor::{ExecutionReport, SP1Context}; @@ -32,7 +32,7 @@ pub trait Prover: Send + Sync { } /// Generate the proving and verifying keys for the given program. - fn setup(&self, elf: &[u8]) -> (SP1ProvingKey, SP1VerifyingKey); + fn setup(&self, elf: &[u8]) -> Result<(SP1ProvingKey, SP1VerifyingKey), Error>; /// Executes the program on the given input. fn execute(&self, elf: &[u8], stdin: &SP1Stdin) -> Result<(SP1PublicValues, ExecutionReport)> { @@ -121,8 +121,8 @@ pub(crate) fn verify_proof( // Make sure the committed value digest matches the public values hash. // It is computationally infeasible to find two distinct inputs, one processed with // SHA256 and the other with Blake3, that yield the same hash value. - if committed_value_digest_bytes != bundle.public_values.hash() && - committed_value_digest_bytes != bundle.public_values.blake3_hash() + if committed_value_digest_bytes != bundle.public_values.hash() + && committed_value_digest_bytes != bundle.public_values.blake3_hash() { return Err(SP1VerificationError::InvalidPublicValues); } @@ -146,8 +146,8 @@ pub(crate) fn verify_proof( // Make sure the committed value digest matches the public values hash. // It is computationally infeasible to find two distinct inputs, one processed with // SHA256 and the other with Blake3, that yield the same hash value. - if committed_value_digest_bytes != bundle.public_values.hash() && - committed_value_digest_bytes != bundle.public_values.blake3_hash() + if committed_value_digest_bytes != bundle.public_values.hash() + && committed_value_digest_bytes != bundle.public_values.blake3_hash() { return Err(SP1VerificationError::InvalidPublicValues); }