vsss_rs/
error.rs

1/*
2    Copyright Michael Lodder. All Rights Reserved.
3    SPDX-License-Identifier: Apache-2.0
4*/
5
6use core::fmt::{self, Display, Formatter};
7
8/// Errors during secret sharing
9#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
10pub enum Error {
11    /// Error when threshold is less than 2
12    SharingMinThreshold,
13    /// Error when limit is less than threshold
14    SharingLimitLessThanThreshold,
15    /// When dealing with fixed size arrays, the caller requested more shares than there is space
16    /// or more shares the field supports.
17    InvalidSizeRequest,
18    /// Invalid share identifier
19    SharingInvalidIdentifier,
20    /// Duplicate identifier when combining
21    SharingDuplicateIdentifier,
22    /// The maximum number of shares to be made when splitting
23    SharingMaxRequest,
24    /// An invalid share was supplied for verification or combine
25    InvalidShare,
26    /// An invalid generator was supplied for share generation
27    InvalidGenerator(&'static str),
28    /// An invalid secret was supplied for split
29    InvalidSecret,
30    /// A share cannot be converted to a group or field element
31    InvalidShareConversion,
32    /// A specific function is not implemented
33    NotImplemented,
34    /// Invalid share element
35    InvalidShareElement,
36    /// Not enough share identifiers available when creating shares
37    NotEnoughShareIdentifiers,
38}
39
40impl Display for Error {
41    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42        match self {
43            Error::SharingMinThreshold => write!(f, "Threshold cannot be less than 2"),
44            Error::SharingLimitLessThanThreshold => write!(f, "Limit is less than threshold"),
45            Error::InvalidSizeRequest => write!(f, "Requested more shares than space was provided"),
46            Error::SharingInvalidIdentifier => write!(f, "An invalid share detected"),
47            Error::SharingDuplicateIdentifier => write!(f, "Duplicate share detected"),
48            Error::SharingMaxRequest => write!(
49                f,
50                "The maximum number of shares to be made when splitting was reached"
51            ),
52            Error::InvalidShare => write!(
53                f,
54                "An invalid share was supplied for verification or combine"
55            ),
56            Error::InvalidGenerator(s) => write!(
57                f,
58                "An invalid generator was supplied for share generation: {}",
59                s
60            ),
61            Error::InvalidSecret => write!(f, "An invalid secret was supplied for split"),
62            Error::InvalidShareConversion => {
63                write!(f, "A share cannot be converted to a group or field element")
64            }
65            Error::NotImplemented => write!(f, "Not implemented"),
66            Error::InvalidShareElement => write!(f, "Invalid share element"),
67            Error::NotEnoughShareIdentifiers => write!(f, "Not enough share identifiers available"),
68        }
69    }
70}
71
72/// Results returned by this crate
73pub type VsssResult<T> = Result<T, Error>;