entropy_protocol/
errors.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (C) 2023 Entropy Cryptography Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use synedrion::{
    sessions, AuxGenResult, InteractiveSigningResult, KeyInitResult, KeyResharingResult,
    ProtocolResult,
};
use thiserror::Error;

use crate::{protocol_message::ProtocolMessage, KeyParams, PartyId};

#[derive(Debug, Error)]
pub enum GenericProtocolError<Res: ProtocolResult> {
    #[error("Synedrion session error {0}")]
    Joined(Box<sessions::Error<Res, PartyId>>),
    #[error("Incoming message stream error: {0}")]
    IncomingStream(String),
    #[error("Broadcast error: {0}")]
    Broadcast(#[from] Box<tokio::sync::broadcast::error::SendError<ProtocolMessage>>),
    #[error("Mpsc send error: {0}")]
    Mpsc(#[from] tokio::sync::mpsc::error::SendError<ProtocolMessage>),
}

impl<Res: ProtocolResult> From<sessions::LocalError> for GenericProtocolError<Res> {
    fn from(err: sessions::LocalError) -> Self {
        Self::Joined(Box::new(sessions::Error::Local(err)))
    }
}

impl<Res: ProtocolResult> From<sessions::RemoteError<PartyId>> for GenericProtocolError<Res> {
    fn from(err: sessions::RemoteError<PartyId>) -> Self {
        Self::Joined(Box::new(sessions::Error::Remote(err)))
    }
}

impl<Res: ProtocolResult> From<sessions::Error<Res, PartyId>> for GenericProtocolError<Res> {
    fn from(err: sessions::Error<Res, PartyId>) -> Self {
        Self::Joined(Box::new(err))
    }
}

impl From<GenericProtocolError<InteractiveSigningResult<KeyParams, PartyId>>>
    for ProtocolExecutionErr
{
    fn from(err: GenericProtocolError<InteractiveSigningResult<KeyParams, PartyId>>) -> Self {
        tracing::error!("{:?}", err);
        match err {
            GenericProtocolError::Joined(err) => ProtocolExecutionErr::SigningProtocolError(err),
            GenericProtocolError::IncomingStream(err) => ProtocolExecutionErr::IncomingStream(err),
            GenericProtocolError::Broadcast(err) => ProtocolExecutionErr::Broadcast(err),
            GenericProtocolError::Mpsc(err) => ProtocolExecutionErr::Mpsc(err),
        }
    }
}

impl From<GenericProtocolError<KeyInitResult<KeyParams, PartyId>>> for ProtocolExecutionErr {
    fn from(err: GenericProtocolError<KeyInitResult<KeyParams, PartyId>>) -> Self {
        tracing::error!("{:?}", err);
        match err {
            GenericProtocolError::Joined(err) => ProtocolExecutionErr::KeyInitProtocolError(err),
            GenericProtocolError::IncomingStream(err) => ProtocolExecutionErr::IncomingStream(err),
            GenericProtocolError::Broadcast(err) => ProtocolExecutionErr::Broadcast(err),
            GenericProtocolError::Mpsc(err) => ProtocolExecutionErr::Mpsc(err),
        }
    }
}

impl From<GenericProtocolError<KeyResharingResult<KeyParams, PartyId>>> for ProtocolExecutionErr {
    fn from(err: GenericProtocolError<KeyResharingResult<KeyParams, PartyId>>) -> Self {
        tracing::error!("{:?}", err);
        match err {
            GenericProtocolError::Joined(err) => ProtocolExecutionErr::KeyReshareProtocolError(err),
            GenericProtocolError::IncomingStream(err) => ProtocolExecutionErr::IncomingStream(err),
            GenericProtocolError::Broadcast(err) => ProtocolExecutionErr::Broadcast(err),
            GenericProtocolError::Mpsc(err) => ProtocolExecutionErr::Mpsc(err),
        }
    }
}

impl From<GenericProtocolError<AuxGenResult<KeyParams, PartyId>>> for ProtocolExecutionErr {
    fn from(err: GenericProtocolError<AuxGenResult<KeyParams, PartyId>>) -> Self {
        tracing::error!("{:?}", err);
        match err {
            GenericProtocolError::Joined(err) => ProtocolExecutionErr::AuxGenProtocolError(err),
            GenericProtocolError::IncomingStream(err) => ProtocolExecutionErr::IncomingStream(err),
            GenericProtocolError::Broadcast(err) => ProtocolExecutionErr::Broadcast(err),
            GenericProtocolError::Mpsc(err) => ProtocolExecutionErr::Mpsc(err),
        }
    }
}

/// An error during or while setting up a protocol session
#[derive(Debug, Error)]
pub enum ProtocolExecutionErr {
    #[error("Incoming message stream error: {0}")]
    IncomingStream(String),
    #[error("Synedrion session creation error: {0}")]
    SessionCreation(sessions::LocalError),
    #[error("Synedrion signing session error")]
    SigningProtocolError(
        Box<sessions::Error<InteractiveSigningResult<KeyParams, PartyId>, PartyId>>,
    ),
    #[error("Synedrion key init session error")]
    KeyInitProtocolError(Box<sessions::Error<KeyInitResult<KeyParams, PartyId>, PartyId>>),
    #[error("Synedrion key reshare session error")]
    KeyReshareProtocolError(Box<sessions::Error<KeyResharingResult<KeyParams, PartyId>, PartyId>>),
    #[error("Synedrion aux generation session error")]
    AuxGenProtocolError(Box<sessions::Error<AuxGenResult<KeyParams, PartyId>, PartyId>>),
    #[error("Broadcast error: {0}")]
    Broadcast(#[from] Box<tokio::sync::broadcast::error::SendError<ProtocolMessage>>),
    #[error("Mpsc send error: {0}")]
    Mpsc(#[from] tokio::sync::mpsc::error::SendError<ProtocolMessage>),
    #[error("Bad keyshare error {0}")]
    BadKeyShare(String),
    #[error("Cannot serialize session ID {0}")]
    Bincode(#[from] bincode::Error),
    #[error("No output from reshare protocol")]
    NoOutputFromReshareProtocol,
    #[error("BigInt conversion: {0}")]
    BigIntConversion(#[from] num::bigint::TryFromBigIntError<num::bigint::BigUint>),
    #[error("Index out of bounds when selecting DKG committee")]
    IndexOutOfBounds,
    #[error("Received bad validating key {0}")]
    BadVerifyingKey(String),
    #[error("Expected verifying key but got a protocol message")]
    UnexpectedMessage,
}

#[derive(Debug, Error)]
pub enum ListenerErr {
    #[error("invalid party ID: {0}")]
    InvalidPartyId(String),
}

/// An error when handling a verifying key
#[derive(Debug, Error)]
pub enum VerifyingKeyError {
    #[error("Could not decode to encoded point")]
    DecodeEncodedPoint,
    #[error("Could not convert encoded point to verifying key")]
    EncodedPointToVerifyingKey,
}