spectral_vm 0.1.6

HYPERION: Production-ready zero-knowledge virtual machine with spectral analysis
Documentation
/*
 * ═══════════════════════════════════════════════════════════════════════════
 * ERROR TYPES: Comprehensive Error Handling for HYPERION
 * ═══════════════════════════════════════════════════════════════════════════
 */

use thiserror::Error;

/// Main error type for HYPERION Spectral ZK-VM operations
#[derive(Debug, Error)]
pub enum HyperionError {
    /// Field arithmetic errors
    #[error("Field arithmetic error: {0}")]
    Field(#[from] FieldError),

    /// Virtual machine execution errors
    #[error("VM execution error: {0}")]
    VM(#[from] VMError),

    /// FRI protocol errors
    #[error("FRI protocol error: {0}")]
    FRI(#[from] FRIError),

    /// Circuit compilation errors
    #[error("Circuit compilation error: {0}")]
    Circuit(#[from] CircuitError),

    /// Proof verification errors
    #[error("Proof verification error: {0}")]
    Proof(#[from] ProofError),

    /// Configuration errors
    #[error("Configuration error: {0}")]
    Config(#[from] ConfigError),

    /// I/O errors
    #[error("I/O error: {0}")]
    IO(#[from] std::io::Error),

    /// Serialization errors
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Invalid input data
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Internal consistency errors
    #[error("Internal error: {0}")]
    Internal(String),
}

/// Field arithmetic specific errors
#[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 },
}

/// Virtual machine specific errors
#[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 },
}

/// FRI protocol specific errors
#[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,
}

/// Circuit compilation specific errors
#[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 },
}

/// Proof verification specific errors
#[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,
}

/// Configuration specific errors
#[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 },
}

/// Result type alias for Hyperion operations
pub type Result<T> = std::result::Result<T, HyperionError>;

/// Convenience macro for internal errors
#[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)*)))
    };
}

/// Convenience macro for invalid input errors
#[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)*)))
    };
}