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