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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::fmt;
use failure::{Backtrace, Context, Fail};
pub type SchnorrResult<T> = ::std::result::Result<T, SchnorrError>;
#[derive(Eq, PartialEq, Debug, Fail, Clone)]
pub enum MuSigError {
#[fail(display = "The number of public nonces must match the number of public keys in the joint key")]
MismatchedNonces,
#[fail(display = "The number of partial signatures must match the number of public keys in the joint key")]
MismatchedSignatures,
#[fail(display = "The aggregate signature did not verify")]
InvalidAggregateSignature,
#[fail(display = "A partial signature did not validate at index: {}", _0)]
InvalidPartialSignature(usize),
#[fail(display = "The participant list must be sorted before making this call")]
NotSorted,
#[fail(display = "The participant key is not in the list")]
ParticipantNotFound,
#[fail(display = "An attempt was made to perform an invalid MuSig state transition")]
InvalidStateTransition,
#[fail(display = "An attempt was made to add a duplicate public key to a MuSig signature")]
DuplicatePubKey,
#[fail(display = "There are too many parties in the MuSig signature")]
TooManyParticipants,
#[fail(display = "There are too few parties in the MuSig signature")]
NotEnoughParticipants,
#[fail(display = "A nonce hash is missing")]
MissingNonce,
#[fail(display = "The message to be signed can only be set once")]
MessageAlreadySet,
#[fail(display = "The message to be signed MUST be set before the final nonce is added to the MuSig ceremony")]
MissingMessage,
#[fail(display = "The message to sign is invalid. have you hashed it?")]
InvalidMessage,
#[fail(display = "MuSig requires a hash function with a 32 byte digest")]
IncompatibleHashFunction,
}
#[derive(Eq, PartialEq, Debug, Fail, Clone)]
pub enum InternalError {
#[fail(display = "Cannot decompress Edwards point")]
PointDecompressionError,
#[fail(display = "Cannot use scalar with high-bit set")]
ScalarFormatError,
#[fail(
display = "{} must be {} bytes in length",
name, length
)]
BytesLengthError {
name: &'static str,
description: &'static str,
length: usize
},
#[fail(display = "Verification equation was not satisfied")]
VerifyError,
#[fail(display = "Function is called with bad arguments")]
BadArguments,
#[fail(
display = "Absent {} violated multi-signature protocol",
_0
)]
MuSig {
kind: MuSigError
}
}
#[derive(Debug)]
pub struct SchnorrError {
inner: Context<InternalError>,
}
impl SchnorrError {
pub fn kind(&self) -> &InternalError {
self.inner.get_context()
}
}
impl Clone for SchnorrError {
fn clone(&self) -> Self {
SchnorrError {
inner: Context::new(self.inner.get_context().clone()),
}
}
}
impl fmt::Display for SchnorrError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
impl Fail for SchnorrError {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}
impl From<InternalError> for SchnorrError {
fn from(kind: InternalError) -> SchnorrError {
SchnorrError {
inner: Context::new(kind),
}
}
}
impl From<Context<InternalError>> for SchnorrError {
fn from(inner: Context<InternalError>) -> SchnorrError {
SchnorrError { inner }
}
}
pub(crate) fn serde_error_from_signature_error<E>(err: SchnorrError) -> E
where E: ::serde::de::Error
{
match *err.kind() {
InternalError::PointDecompressionError
=> E::custom("Ristretto point decompression failed"),
InternalError::ScalarFormatError
=> E::custom("improper scalar has high-bit set"),
InternalError::BytesLengthError{ description, length, .. }
=> E::invalid_length(length, &description),
_ => panic!("Non-serialisation error encountered by serde!"),
}
}