chik_bls/
error.rs

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
use blst::BLST_ERROR;
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum Error {
    #[error("SecretKey byte data must be less than the group order")]
    SecretKeyGroupOrder,
    #[error("Given G1 infinity element must be canonical")]
    G1NotCanonical,
    #[error("Given G1 non-infinity element must start with 0b10")]
    G1InfinityInvalidBits,
    #[error("G1 non-infinity element can't have only zeros")]
    G1InfinityNotZero,
    #[error("PublicKey is invalid (BLST ERROR: {0:?})")]
    InvalidPublicKey(BLST_ERROR),
    #[error("Signature is invalid (BLST ERROR: {0:?})")]
    InvalidSignature(BLST_ERROR),
}

pub type Result<T> = std::result::Result<T, Error>;

impl From<Error> for chik_traits::Error {
    fn from(err: Error) -> chik_traits::Error {
        chik_traits::Error::Custom(format!("{err}"))
    }
}

#[cfg(feature = "py-bindings")]
mod pybindings {
    use super::*;

    use pyo3::{exceptions::PyValueError, PyErr};

    impl From<Error> for PyErr {
        fn from(err: Error) -> PyErr {
            PyValueError::new_err(format!("BLS Error {err:?}"))
        }
    }
}