Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, 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();
Expand All @@ -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
Expand All @@ -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<String, String> {
pub fn vkeys(path: &str, args: BuildArgs) -> Result<HashMap<String, String>, 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();
Expand All @@ -200,10 +206,15 @@ pub fn vkeys(path: &str, args: BuildArgs) -> HashMap<String, String> {
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()
}
Expand Down
7 changes: 6 additions & 1 deletion crates/prover/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ pub fn dummy_proof() -> (StarkVerifyingKey<OuterSC>, ShardProof<OuterSC>) {
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();
Expand Down
18 changes: 14 additions & 4 deletions crates/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,13 @@ impl<C: SP1ProverComponents> SP1Prover<C> {
pub fn setup(
&self,
elf: &[u8],
) -> (SP1ProvingKey, DeviceProvingKey<C>, Program, SP1VerifyingKey) {
let program = self.get_program(elf).unwrap();
) -> eyre::Result<(SP1ProvingKey, DeviceProvingKey<C>, 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 {
Expand All @@ -278,12 +283,17 @@ impl<C: SP1ProverComponents> SP1Prover<C> {
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<Program> {
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)?;
}
Expand Down
13 changes: 9 additions & 4 deletions crates/sdk/src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -210,9 +210,14 @@ impl CpuProver {
}

impl Prover<CpuProverComponents> 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<CpuProverComponents> {
Expand Down
13 changes: 9 additions & 4 deletions crates/sdk/src/cuda/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -160,9 +160,14 @@ impl CudaProver {
}

impl Prover<CpuProverComponents> 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<CpuProverComponents> {
Expand Down
16 changes: 11 additions & 5 deletions crates/sdk/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))),
}
}
}

Expand All @@ -176,8 +179,11 @@ impl Prover<CpuProverComponents> 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(
Expand Down
17 changes: 10 additions & 7 deletions crates/sdk/src/network/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,11 @@ impl NetworkProver {
if let Some(network_error) = e.downcast_ref::<Error>() {
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..."
Expand Down Expand Up @@ -888,8 +888,11 @@ impl NetworkProver {
}

impl Prover<CpuProverComponents> 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 {
Expand Down
12 changes: 6 additions & 6 deletions crates/sdk/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -32,7 +32,7 @@ pub trait Prover<C: SP1ProverComponents>: 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)> {
Expand Down Expand Up @@ -121,8 +121,8 @@ pub(crate) fn verify_proof<C: SP1ProverComponents>(
// 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);
}
Expand All @@ -146,8 +146,8 @@ pub(crate) fn verify_proof<C: SP1ProverComponents>(
// 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);
}
Expand Down
Loading