gm_rs/sm2/
error.rs

1use std::fmt::Display;
2use std::fmt::Formatter;
3
4pub type Sm2Result<T> = Result<T, Sm2Error>;
5
6#[derive(PartialEq)]
7pub enum Sm2Error {
8    NotOnCurve,
9    FieldSqrtError,
10    InvalidDer,
11    InvalidPublic,
12    InvalidPrivate,
13    ZeroDivisor,
14    ZeroPoint,
15    InvalidPoint,
16    CheckPointErr,
17    ZeroData,
18    HashNotEqual,
19    IdTooLong,
20    ZeroFiled,
21    InvalidFieldLen,
22    ZeroSig,
23    InvalidDigestLen,
24    InvalidDigest,
25    InvalidSecretKey,
26    KdfHashError,
27}
28
29impl ::std::fmt::Debug for Sm2Error {
30    fn fmt(&self, f: &mut Formatter<'_>) -> ::std::fmt::Result {
31        write!(f, "{}", self)
32    }
33}
34
35impl From<Sm2Error> for &str {
36    fn from(e: Sm2Error) -> Self {
37        match e {
38            Sm2Error::NotOnCurve => "the point not on curve",
39            Sm2Error::FieldSqrtError => "field elem sqrt error",
40            Sm2Error::InvalidDer => "invalid der",
41            Sm2Error::InvalidPublic => "invalid public key",
42            Sm2Error::InvalidPrivate => "invalid private key",
43            Sm2Error::ZeroDivisor => "zero has no inversion",
44            Sm2Error::ZeroPoint => "cannot convert the infinite point to affine",
45            Sm2Error::InvalidPoint => "invalid jacobian point",
46            Sm2Error::CheckPointErr => "check point error",
47            Sm2Error::ZeroData => "the vector is zero",
48            Sm2Error::HashNotEqual => "hash not equal",
49            Sm2Error::IdTooLong => "ID is too long",
50            Sm2Error::ZeroFiled => "zero has no inversion in filed",
51            Sm2Error::InvalidFieldLen => "a SCA-256 field element must be 32-byte long",
52            Sm2Error::ZeroSig => "the signature is zero, cannot sign",
53            Sm2Error::InvalidDigestLen => "the length of digest must be 32-bytes",
54            Sm2Error::InvalidSecretKey => "invalid secret key",
55            Sm2Error::KdfHashError => "KDF hash error",
56            Sm2Error::InvalidDigest => "invalid signature digest",
57        }
58    }
59}
60
61impl Display for Sm2Error {
62    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63        let err_msg = match self {
64            Sm2Error::NotOnCurve => "the point not on curve",
65            Sm2Error::FieldSqrtError => "field elem sqrt error",
66            Sm2Error::InvalidDer => "invalid der",
67            Sm2Error::InvalidPublic => "invalid public key",
68            Sm2Error::InvalidPrivate => "invalid private key",
69            Sm2Error::ZeroDivisor => "zero has no inversion",
70            Sm2Error::ZeroPoint => "cannot convert the infinite point to affine",
71            Sm2Error::InvalidPoint => "invalid jacobian point",
72            Sm2Error::CheckPointErr => "check point error",
73            Sm2Error::ZeroData => "the vector is zero",
74            Sm2Error::HashNotEqual => "hash and cipher not equal",
75            Sm2Error::IdTooLong => "ID is too long",
76            Sm2Error::ZeroFiled => "zero has no inversion in filed",
77            Sm2Error::InvalidFieldLen => "a SCA-256 field element must be 32-byte long",
78            Sm2Error::ZeroSig => "the signature is zero, cannot sign",
79            Sm2Error::InvalidDigestLen => "the length of digest must be 32-bytes",
80            Sm2Error::InvalidSecretKey => "invalid secret key",
81            Sm2Error::KdfHashError => "KDF hash error",
82            Sm2Error::InvalidDigest => "invalid signature digest",
83        };
84        write!(f, "{}", err_msg)
85    }
86}