dkls23/sign/
types.rs

1// Copyright (c) Silence Laboratories Pte. Ltd. All Rights Reserved.
2// This software is licensed under the Silence Laboratories License Agreement.
3
4//! Types and error definitions for the Distributed Signature Generation (DSG) Protocol
5//!
6//! This module defines the error types and other fundamental types used throughout
7//! the DSG protocol implementation. These types help ensure proper error handling
8//! and type safety across the protocol implementation.
9
10use crate::proto::tags::Error;
11use sl_mpc_mate::coord::MessageSendError;
12
13/// Error types that can occur during the Distributed Signature Generation protocol
14///
15/// This enum represents all possible error conditions that can arise during
16/// the execution of the DSG protocol. Each variant includes a descriptive
17/// error message and implements the `std::error::Error` trait.
18#[derive(thiserror::Error, Debug)]
19#[allow(missing_docs)]
20pub enum SignError {
21    /// Indicates that a cryptographic commitment is invalid
22    #[error("Invalid commitment")]
23    InvalidCommitment,
24
25    /// Indicates that a message digest is invalid
26    #[error("Invalid digest")]
27    InvalidDigest,
28
29    /// Indicates that the final session ID is invalid
30    #[error("Invalid final_session_id")]
31    InvalidFinalSessionID,
32
33    /// Indicates that a protocol check has failed
34    #[error("Failed check: {0}")]
35    FailedCheck(&'static str),
36
37    /// Indicates an error from the k256 elliptic curve library
38    #[error("k256 error")]
39    K256Error,
40
41    /// Indicates that a pre-signature is invalid
42    #[error("invalid pre signature")]
43    InvalidPreSign,
44
45    /// Indicates that a message has an invalid format
46    #[error("invalid message format")]
47    InvalidMessage,
48
49    /// Indicates that a required message is missing
50    #[error("Missing message")]
51    MissingMessage,
52
53    /// Indicates that a message could not be sent
54    #[error("Send message")]
55    SendMessage,
56
57    /// Indicates that a party has decided to abort the protocol
58    #[error("Abort protocol by party {0}")]
59    AbortProtocol(usize),
60
61    /// Indicates that a party should be banned and the protocol aborted
62    #[error("Abort the protocol and ban the party {0}")]
63    AbortProtocolAndBanParty(u8),
64}
65
66/// Conversion from `MessageSendError` to `SignError`
67///
68/// This implementation allows `MessageSendError` to be automatically converted
69/// to `SignError::SendMessage` when using the `?` operator.
70impl From<MessageSendError> for SignError {
71    fn from(_err: MessageSendError) -> Self {
72        SignError::SendMessage
73    }
74}
75
76/// Conversion from `k256::ecdsa::Error` to `SignError`
77///
78/// This implementation allows k256 elliptic curve errors to be automatically
79/// converted to `SignError::K256Error` when using the `?` operator.
80impl From<k256::ecdsa::Error> for SignError {
81    fn from(_err: k256::ecdsa::Error) -> Self {
82        Self::K256Error
83    }
84}
85
86/// Conversion from `Error` to `SignError`
87///
88/// This implementation allows protocol tag errors to be automatically converted
89/// to appropriate `SignError` variants when using the `?` operator.
90impl From<Error> for SignError {
91    fn from(err: Error) -> Self {
92        match err {
93            Error::Abort(p) => SignError::AbortProtocol(p as _),
94            Error::Recv => SignError::MissingMessage,
95            Error::Send => SignError::SendMessage,
96            Error::InvalidMessage => SignError::InvalidMessage,
97        }
98    }
99}