Skip to content

Commit

Permalink
Fix for risc0#681 (risc0#684)
Browse files Browse the repository at this point in the history
Use thiserror for public Error types.
  • Loading branch information
flaub authored Jul 12, 2023
1 parent a8d8cc2 commit e96ab3e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 43 deletions.
2 changes: 1 addition & 1 deletion risc0/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"

[dependencies]
risc0-zkp = { path = "../zkp", default-features = false }
risc0-zkvm = { path = "../zkvm", default-features = false }
risc0-zkvm = { path = "../zkvm", default-features = false, features = ["std"] }
1 change: 1 addition & 0 deletions risc0/zkp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ndarray = { version = "0.15", features = ["rayon"], optional = true }
rand = { version = "0.8", optional = true }
rayon = { version = "1.5", optional = true }
sha2 = { version = "0.10", default-features = false, features = ["compress"] }
thiserror = "1.0"

[dev-dependencies]
criterion = "0.5"
Expand Down
3 changes: 3 additions & 0 deletions risc0/zkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ pub mod core;
#[cfg(feature = "prove")]
pub mod hal;
pub mod layout;
#[cfg(not(target_os = "zkvm"))]
mod merkle;
#[cfg(feature = "prove")]
pub mod prove;
pub mod taps;
#[cfg(not(target_os = "zkvm"))]
pub mod verify;

#[cfg(not(feature = "prove"))]
Expand Down Expand Up @@ -61,4 +63,5 @@ const FRI_FOLD_PO2: usize = 4;
pub const FRI_FOLD: usize = 1 << FRI_FOLD_PO2;

/// FRI continues until the degree of the FRI polynomial reaches FRI_MIN_DEGREE
#[cfg(not(target_os = "zkvm"))]
const FRI_MIN_DEGREE: usize = 256;
40 changes: 11 additions & 29 deletions risc0/zkp/src/verify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ mod merkle;
mod read_iop;

use alloc::{vec, vec::Vec};
use core::{
cell::RefCell,
fmt::{self},
iter::zip,
};
use core::{cell::RefCell, iter::zip};

pub(crate) use merkle::MerkleTreeVerifier;
pub use read_iop::ReadIOP;
use risc0_core::field::{Elem, ExtElem, Field, RootsOfUnity};
use thiserror::Error;

use crate::{
adapter::{CircuitCoreDef, REGISTER_GROUP_ACCUM, REGISTER_GROUP_CODE, REGISTER_GROUP_DATA},
Expand All @@ -36,41 +33,26 @@ use crate::{
INV_RATE, MAX_CYCLES_PO2, QUERIES,
};

#[derive(Debug, PartialEq)]
#[derive(Debug, Error, PartialEq)]
pub enum VerificationError {
#[error("invalid receipt format")]
ReceiptFormatError,
#[error("control_id mismatch")]
ControlVerificationError,
#[error("image_id mismatch")]
ImageVerificationError,
#[error("Requested Merkle validation on row {idx}, but only {rows} rows exist")]
MerkleQueryOutOfRange { idx: usize, rows: usize },
#[error("Verification indicates proof is invalid")]
InvalidProof,
#[error("Journal digest mismatch detected")]
JournalDigestMismatch,
#[error("Unexpected exit_code")]
UnexpectedExitCode,
#[error("Invalid hash suite")]
InvalidHashSuite,
}

impl fmt::Display for VerificationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VerificationError::ReceiptFormatError => write!(f, "invalid receipt format"),
VerificationError::ControlVerificationError => write!(f, "control_id mismatch"),
VerificationError::ImageVerificationError => write!(f, "image_id mismatch"),
VerificationError::MerkleQueryOutOfRange { idx, rows } => write!(
f,
"Requested Merkle validation on row {idx}, but only {rows} rows exist",
),
VerificationError::InvalidProof => write!(f, "Verification indicates proof is invalid"),
VerificationError::JournalDigestMismatch => {
write!(f, "Journal digest mismatch detected")
}
VerificationError::UnexpectedExitCode => write!(f, "Unexpected exit_code"),
VerificationError::InvalidHashSuite => write!(f, "Invalid hash suite"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for VerificationError {}

trait VerifyParams<F: Field> {
const CHECK_SIZE: usize = INV_RATE * F::ExtElem::EXT_SIZE;
}
Expand Down
1 change: 1 addition & 0 deletions risc0/zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ rand = { version = "0.8", optional = true }
rayon = { version = "1.5", optional = true }
rrs-lib = { version = "0.1", optional = true }
sha2 = { version = "0.10", optional = true }
thiserror = "1.0"
typetag = "0.2"

[dev-dependencies]
Expand Down
18 changes: 5 additions & 13 deletions risc0/zkvm/src/exec/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use risc0_zkvm_platform::{
SyscallName,
},
};
use thiserror::Error;

use super::{
io::{slice_io_from_fn, syscalls, PosixIo, SliceIo, Syscall, SyscallTable},
Expand Down Expand Up @@ -113,20 +114,11 @@ impl<'a> Default for ExecutorEnvBuilder<'a> {
}

/// [ExecutorEnvBuilder] errors.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ExecutorEnvBuilderErr {
/// Segment limit PO2 falls outside supported range.
SegmentLimitPo2OutOfBounds { given: usize },
}

impl core::fmt::Display for ExecutorEnvBuilderErr {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
ExecutorEnvBuilderErr::SegmentLimitPo2OutOfBounds { given } => {
write!(f, "Invalid segment_limit_po2: {given}",)
}
}
}
#[error("Invalid segment_limit_po2: {po2}")]
SegmentLimitPo2OutOfBounds { po2: usize },
}

impl<'a> ExecutorEnvBuilder<'a> {
Expand All @@ -145,7 +137,7 @@ impl<'a> ExecutorEnvBuilder<'a> {
|| self.inner.segment_limit_po2 > risc0_zkp::MAX_CYCLES_PO2
{
return Err(ExecutorEnvBuilderErr::SegmentLimitPo2OutOfBounds {
given: self.inner.segment_limit_po2,
po2: self.inner.segment_limit_po2,
});
}

Expand Down

0 comments on commit e96ab3e

Please sign in to comment.