use thiserror::Error;
#[derive(Debug, Error)]
pub enum HyperionError {
#[error("Field arithmetic error: {0}")]
Field(#[from] FieldError),
#[error("VM execution error: {0}")]
VM(#[from] VMError),
#[error("FRI protocol error: {0}")]
FRI(#[from] FRIError),
#[error("Circuit compilation error: {0}")]
Circuit(#[from] CircuitError),
#[error("Proof verification error: {0}")]
Proof(#[from] ProofError),
#[error("Configuration error: {0}")]
Config(#[from] ConfigError),
#[error("I/O error: {0}")]
IO(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Internal error: {0}")]
Internal(String),
}
#[derive(Debug, Error)]
pub enum FieldError {
#[error("Division by zero")]
DivisionByZero,
#[error("Invalid field element: {value}")]
InvalidElement { value: u64 },
#[error("SIMD operation failed: {reason}")]
SimdError { reason: String },
}
#[derive(Debug, Error)]
pub enum VMError {
#[error("Invalid register index: {index}")]
InvalidRegister { index: usize },
#[error("Memory access out of bounds: address {address}, size {size}")]
MemoryOutOfBounds { address: usize, size: usize },
#[error("Invalid instruction: {instruction}")]
InvalidInstruction { instruction: u64 },
#[error("Execution timeout after {cycles} cycles")]
ExecutionTimeout { cycles: u64 },
#[error("Stack overflow")]
StackOverflow,
#[error("Invalid opcode: {opcode}")]
InvalidOpcode { opcode: u32 },
}
#[derive(Debug, Error)]
pub enum FRIError {
#[error("Invalid FRI parameters: {reason}")]
InvalidParameters { reason: String },
#[error("Codeword size mismatch: expected {expected}, got {actual}")]
CodewordSizeMismatch { expected: usize, actual: usize },
#[error("Folding consistency check failed")]
FoldingConsistencyFailed,
#[error("Query verification failed for index {index}")]
QueryVerificationFailed { index: usize },
#[error("Merkle proof verification failed")]
MerkleProofFailed,
}
#[derive(Debug, Error)]
pub enum CircuitError {
#[error("Undefined variable: {name}")]
UndefinedVariable { name: String },
#[error("Type mismatch: expected {expected}, got {actual}")]
TypeMismatch { expected: String, actual: String },
#[error("Invalid expression: {reason}")]
InvalidExpression { reason: String },
#[error("Optimization failed: {reason}")]
OptimizationFailed { reason: String },
#[error("Control flow error: {reason}")]
ControlFlowError { reason: String },
}
#[derive(Debug, Error)]
pub enum ProofError {
#[error("Proof deserialization failed")]
DeserializationFailed,
#[error("Proof size too large: {size} bytes")]
ProofTooLarge { size: usize },
#[error("Invalid proof structure: {reason}")]
InvalidStructure { reason: String },
#[error("Verification failed: {reason}")]
VerificationFailed { reason: String },
#[error("Transcript inconsistency")]
TranscriptInconsistency,
}
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("Configuration file not found: {path}")]
FileNotFound { path: String },
#[error("Invalid configuration: {reason}")]
InvalidConfig { reason: String },
#[error("Environment variable error: {var}")]
EnvVarError { var: String },
}
pub type Result<T> = std::result::Result<T, HyperionError>;
#[macro_export]
macro_rules! internal_error {
($msg:expr) => {
Err($crate::error::HyperionError::Internal($msg.to_string()))
};
($fmt:expr, $($arg:tt)*) => {
Err($crate::error::HyperionError::Internal(format!($fmt, $($arg)*)))
};
}
#[macro_export]
macro_rules! invalid_input {
($msg:expr) => {
Err($crate::error::HyperionError::InvalidInput($msg.to_string()))
};
($fmt:expr, $($arg:tt)*) => {
Err($crate::error::HyperionError::InvalidInput(format!($fmt, $($arg)*)))
};
}