Skip to main content

bicycl_rs/
error.rs

1use thiserror::Error;
2
3/// All errors that can be returned by this crate.
4///
5/// The variants fall into two categories:
6/// - **Rust-side conversion errors** (`NulByte`, `Utf8`): problems converting
7///   data before or after FFI calls.
8/// - **C library status codes** (all other variants): direct mappings of the
9///   `bicycl_status_t` enum returned by the C API.
10/// - **`Unknown`**: a status code not recognised by this version of the crate.
11#[non_exhaustive]
12#[derive(Debug, Error)]
13pub enum Error {
14    #[error("NUL byte in input string")]
15    NulByte(#[from] std::ffi::NulError),
16
17    #[error("UTF-8 error in FFI response")]
18    Utf8(#[from] std::str::Utf8Error),
19
20    /// The C library returned `BICYCL_ERR_NULL_PTR`, meaning the **caller**
21    /// passed a null pointer into the C API.  This should not occur under
22    /// normal use of the safe Rust wrappers.
23    #[error("BICYCL null pointer")]
24    NullPtr,
25
26    #[error("BICYCL invalid argument")]
27    InvalidArgument,
28
29    #[error("BICYCL allocation failed")]
30    AllocationFailed,
31
32    #[error("BICYCL internal error")]
33    Internal,
34
35    #[error("BICYCL output buffer too small")]
36    BufferTooSmall,
37
38    #[error("BICYCL parse error")]
39    Parse,
40
41    #[error("BICYCL invalid protocol state")]
42    InvalidState,
43
44    #[error("BICYCL verification failed")]
45    VerifyFailed,
46
47    #[error("BICYCL protocol aborted")]
48    ProtocolAbort,
49
50    #[error("BICYCL core math/runtime module error")]
51    Core,
52
53    #[error("BICYCL Paillier module error")]
54    Paillier,
55
56    #[error("BICYCL Joye-Libert module error")]
57    JoyeLibert,
58
59    #[error("BICYCL CL_HSMqk module error")]
60    ClHsmqk,
61
62    #[error("BICYCL CL_HSM2k module error")]
63    ClHsm2k,
64
65    #[error("BICYCL ECDSA module error")]
66    Ecdsa,
67
68    #[error("BICYCL TwoPartyECDSA module error")]
69    TwoPartyEcdsa,
70
71    #[error("BICYCL CL threshold module error")]
72    ClThreshold,
73
74    #[error("BICYCL CL DLog proof module error")]
75    ClDlog,
76
77    #[error("BICYCL threshold ECDSA module error")]
78    ThresholdEcdsa,
79
80    #[error("unknown BICYCL error code: {0}")]
81    Unknown(i32),
82}
83
84pub type Result<T> = core::result::Result<T, Error>;
85
86impl Error {
87    pub(crate) fn from_status(status: bicycl_rs_sys::bicycl_status_t) -> Self {
88        match status {
89            bicycl_rs_sys::bicycl_status_t::BICYCL_OK => Self::Unknown(0),
90            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_NULL_PTR => Self::NullPtr,
91            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_INVALID_ARGUMENT => Self::InvalidArgument,
92            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_ALLOCATION_FAILED => Self::AllocationFailed,
93            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_INTERNAL => Self::Internal,
94            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_BUFFER_TOO_SMALL => Self::BufferTooSmall,
95            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_PARSE => Self::Parse,
96            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_INVALID_STATE => Self::InvalidState,
97            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_VERIFY_FAILED => Self::VerifyFailed,
98            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_PROTOCOL_ABORT => Self::ProtocolAbort,
99            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_CORE => Self::Core,
100            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_PAILLIER => Self::Paillier,
101            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_JOYE_LIBERT => Self::JoyeLibert,
102            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_CL_HSMQK => Self::ClHsmqk,
103            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_CL_HSM2K => Self::ClHsm2k,
104            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_ECDSA => Self::Ecdsa,
105            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_TWO_PARTY_ECDSA => Self::TwoPartyEcdsa,
106            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_CL_THRESHOLD => Self::ClThreshold,
107            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_CL_DLOG => Self::ClDlog,
108            bicycl_rs_sys::bicycl_status_t::BICYCL_ERR_THRESHOLD_ECDSA => Self::ThresholdEcdsa,
109            _ => Self::Unknown(status as i32),
110        }
111    }
112}