diff --git a/crates/ere-sp1/src/compile.rs b/crates/ere-sp1/src/compile.rs index dba2a72..9563019 100644 --- a/crates/ere-sp1/src/compile.rs +++ b/crates/ere-sp1/src/compile.rs @@ -1,53 +1,10 @@ -use std::{ - fs, - path::{Path, PathBuf}, - process::{Command, ExitStatus}, -}; +use std::{fs, path::Path, process::Command}; use tempfile::TempDir; -use thiserror::Error; use toml::Value as TomlValue; use tracing::info; -/// Errors that can be encountered while compiling a SP1 program -#[derive(Debug, Error)] -pub enum CompileError { - #[error("Program path does not exist or is not a directory: {0}")] - InvalidProgramPath(PathBuf), - #[error( - "Cargo.toml not found in program directory: {program_dir}. Expected at: {manifest_path}" - )] - CargoTomlMissing { - program_dir: PathBuf, - manifest_path: PathBuf, - }, - #[error("Could not find `[package].name` in guest Cargo.toml at {path}")] - MissingPackageName { path: PathBuf }, - #[error("Compiled ELF not found at expected path: {0}")] - ElfNotFound(PathBuf), - #[error("`cargo prove build` failed with status: {status} for program at {path}")] - CargoBuildFailed { status: ExitStatus, path: PathBuf }, - #[error("Failed to read file at {path}: {source}")] - ReadFile { - path: PathBuf, - #[source] - source: std::io::Error, - }, - #[error("Failed to parse guest Cargo.toml at {path}: {source}")] - ParseCargoToml { - path: PathBuf, - #[source] - source: toml::de::Error, - }, - #[error("Failed to execute `cargo prove build` in {cwd}: {source}")] - CargoProveBuild { - cwd: PathBuf, - #[source] - source: std::io::Error, - }, - #[error("Failed to create temporary output directory: {0}")] - TempDir(#[from] std::io::Error), -} +use crate::error::CompileError; /// Compile the guest crate and return raw ELF bytes. pub fn compile_sp1_program(program_crate_path: &Path) -> Result, CompileError> { diff --git a/crates/ere-sp1/src/error.rs b/crates/ere-sp1/src/error.rs new file mode 100644 index 0000000..9310e91 --- /dev/null +++ b/crates/ere-sp1/src/error.rs @@ -0,0 +1,82 @@ +use std::{path::PathBuf, process::ExitStatus}; + +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum SP1Error { + #[error(transparent)] + CompileError(#[from] CompileError), + + #[error(transparent)] + Execute(#[from] ExecuteError), + + #[error(transparent)] + Prove(#[from] ProveError), + + #[error(transparent)] + Verify(#[from] VerifyError), +} + +/// Errors that can be encountered while compiling a SP1 program +#[derive(Debug, Error)] +pub enum CompileError { + #[error("Program path does not exist or is not a directory: {0}")] + InvalidProgramPath(PathBuf), + #[error( + "Cargo.toml not found in program directory: {program_dir}. Expected at: {manifest_path}" + )] + CargoTomlMissing { + program_dir: PathBuf, + manifest_path: PathBuf, + }, + #[error("Could not find `[package].name` in guest Cargo.toml at {path}")] + MissingPackageName { path: PathBuf }, + #[error("Compiled ELF not found at expected path: {0}")] + ElfNotFound(PathBuf), + #[error("`cargo prove build` failed with status: {status} for program at {path}")] + CargoBuildFailed { status: ExitStatus, path: PathBuf }, + #[error("Failed to read file at {path}: {source}")] + ReadFile { + path: PathBuf, + #[source] + source: std::io::Error, + }, + #[error("Failed to parse guest Cargo.toml at {path}: {source}")] + ParseCargoToml { + path: PathBuf, + #[source] + source: toml::de::Error, + }, + #[error("Failed to execute `cargo prove build` in {cwd}: {source}")] + CargoProveBuild { + cwd: PathBuf, + #[source] + source: std::io::Error, + }, + #[error("Failed to create temporary output directory: {0}")] + TempDir(#[from] std::io::Error), +} + +#[derive(Debug, Error)] +pub enum ExecuteError { + #[error("SP1 execution failed: {0}")] + Client(#[source] Box), +} + +#[derive(Debug, Error)] +pub enum ProveError { + #[error("SP1 SDK proving failed: {0}")] + Client(#[source] Box), + + #[error("Serialising proof with `bincode` failed: {0}")] + Bincode(#[from] bincode::Error), +} + +#[derive(Debug, Error)] +pub enum VerifyError { + #[error("Deserialising proof failed: {0}")] + Bincode(#[from] bincode::Error), + + #[error("SP1 SDK verification failed: {0}")] + Client(#[source] Box), +} diff --git a/crates/ere-sp1/src/lib.rs b/crates/ere-sp1/src/lib.rs index e5af2c8..411b2e0 100644 --- a/crates/ere-sp1/src/lib.rs +++ b/crates/ere-sp1/src/lib.rs @@ -2,56 +2,19 @@ use compile::compile_sp1_program; use sp1_sdk::{Prover, ProverClient, SP1ProofWithPublicValues, SP1Stdin}; -use thiserror::Error; use tracing::info; use zkvm_interface::{Compiler, ProgramExecutionReport, ProgramProvingReport, zkVM}; mod compile; +mod error; +use error::{ExecuteError, ProveError, SP1Error, VerifyError}; + #[allow(non_camel_case_types)] pub struct RV32_IM_SUCCINCT_ZKVM_ELF; pub struct EreSP1; -#[derive(Debug, thiserror::Error)] -pub enum SP1Error { - #[error(transparent)] - CompileError(#[from] compile::CompileError), - - #[error(transparent)] - Execute(#[from] ExecuteError), - - #[error(transparent)] - Prove(#[from] ProveError), - - #[error(transparent)] - Verify(#[from] VerifyError), -} - -#[derive(Debug, Error)] -pub enum ExecuteError { - #[error("SP1 execution failed: {0}")] - Client(#[source] Box), -} - -#[derive(Debug, Error)] -pub enum ProveError { - #[error("SP1 SDK proving failed: {0}")] - Client(#[source] Box), - - #[error("Serialising proof with `bincode` failed: {0}")] - Bincode(#[from] bincode::Error), -} - -#[derive(Debug, Error)] -pub enum VerifyError { - #[error("Deserialising proof failed: {0}")] - Bincode(#[from] bincode::Error), - - #[error("SP1 SDK verification failed: {0}")] - Client(#[source] Box), -} - impl Compiler for RV32_IM_SUCCINCT_ZKVM_ELF { type Error = SP1Error; @@ -112,7 +75,7 @@ impl zkVM for EreSP1 { let proving_time = start.elapsed(); let bytes = bincode::serialize(&proof_with_inputs) - .map_err(|err| SP1Error::Verify(VerifyError::Bincode(err)))?; + .map_err(|err| SP1Error::Prove(ProveError::Bincode(err)))?; Ok((bytes, ProgramProvingReport::new(proving_time))) }