1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::utils::string::*;
use core::fmt;

// INPUT ERROR
// ================================================================================================

#[derive(Clone, Debug)]
pub enum InputError {
    NotFieldElement(u64, String),
    DuplicateAdviceRoot([u8; 32]),
}

impl fmt::Display for InputError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use InputError::*;
        match self {
            NotFieldElement(num, description) => {
                write!(f, "{num} is not a valid field element: {description}")
            }
            DuplicateAdviceRoot(key) => {
                write!(f, "{key:02x?} is a duplicate of the current merkle set")
            }
        }
    }
}

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

// OUTPUT ERROR
// ================================================================================================

#[derive(Clone, Debug)]
pub enum OutputError {
    InvalidOverflowAddress(u64),
    InvalidOverflowAddressLength(usize, usize),
    InvalidStackElement(u64),
    OutputSizeTooBig(usize),
}

impl fmt::Display for OutputError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use OutputError::*;
        match self {
            InvalidOverflowAddress(address) => {
                write!(f, "overflow addresses contains {address} that is not a valid field element")
            }
            InvalidOverflowAddressLength(actual, expected) => {
                write!(f, "overflow addresses length is {actual}, but expected {expected}")
            }
            InvalidStackElement(element) => {
                write!(f, "stack contains {element} that is not a valid field element")
            }
            OutputSizeTooBig(size) => {
                write!(f, "too many elements for output stack, {size} elements")
            }
        }
    }
}

// KERNEL ERROR
// ================================================================================================

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

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum KernelError {
    DuplicatedProcedures,
    TooManyProcedures(usize, usize),
}

impl fmt::Display for KernelError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            KernelError::DuplicatedProcedures => {
                write!(f, "Kernel can not have duplicated procedures",)
            }
            KernelError::TooManyProcedures(max, count) => {
                write!(f, "Kernel can have at most {} procedures, received {}", max, count)
            }
        }
    }
}

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