-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathsign.rs
881 lines (801 loc) · 29.4 KB
/
sign.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
//! # High-level threshold signing protocol implementation
//!
//! Key feature of GG20 protocol is one-round online signing, meaning that every party needs to
//! broadcast just a single message to sign a data. However, it still requires completing an offline
//! computation for fixed set of parties
//!
//! ## How to get things work
//!
//! First of all, parties need to carry out distributed key generation protocol (see [keygen module]).
//! After DKG is successfully completed, it outputs [LocalKey] — a party local secret share.
//! Then you fix a set of parties who will participate in threshold signing, and they run
//! [OfflineStage] protocol. `OfflineStage` implements [StateMachine] and can be executed in the same
//! way as [Keygen]. `OfflineStage` outputs a [CompletedOfflineStage]. [SignManual] takes a
//! `CompletedOfflineStage` and allows you to perform one-round signing. It doesn't implement
//! `StateMachine`, but rather provides methods to construct messages and final signature manually
//! (refer to [SignManual] documentation to see how to use it).
//!
//! [keygen module]: super::keygen
//! [Keygen]: super::keygen::Keygen
//! [LocalKey]: super::keygen::LocalKey
//! [StateMachine]: round_based::StateMachine
use std::convert::TryFrom;
use std::mem::replace;
use std::time::Duration;
use round_based::containers::{push::Push, BroadcastMsgs, MessageStore, P2PMsgs, Store, StoreErr};
use round_based::{IsCritical, Msg, StateMachine};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::utilities::mta::MessageA;
use crate::protocols::multi_party_ecdsa::gg_2020 as gg20;
use crate::utilities::zk_pdl_with_slack::PDLwSlackProof;
use gg20::party_i::{LocalSignature, SignBroadcastPhase1, SignDecommitPhase1, SignatureRecid};
use gg20::state_machine::keygen::LocalKey;
mod fmt;
mod rounds;
use curv::BigInt;
use rounds::*;
pub use rounds::{CompletedOfflineStage, Error as ProceedError};
/// Offline Stage of GG20 signing
///
/// Successfully carried out Offline Stage will produce [CompletedOfflineStage] that can
/// be used for one-round signing multiple times.
pub struct OfflineStage {
round: OfflineR,
decommit_round: DecommitR,
msgs1: Option<Store<BroadcastMsgs<MessageA>>>,
msgs2: Option<(Store<BroadcastMsgs<GWI>>, Store<P2PMsgs<(GammaI, WI)>>)>,
msgs3: Option<(
Store<BroadcastMsgs<SignDecommitPhase1>>,
Store<BroadcastMsgs<DeltaI>>,
)>,
msgs4: Option<(
Store<BroadcastMsgs<RDash>>,
Store<BroadcastMsgs<PDLwSlackProof>>,
Store<BroadcastMsgs<RVal>>,
)>,
msgs_com: Option<Store<BroadcastMsgs<SignBroadcastPhase1>>>,
msgs_queue: MsgQueue,
party_i: u16,
party_n: u16,
}
impl OfflineStage {
/// Construct a party of offline stage of threshold signing protocol
///
/// Once offline stage is finished, parties can do one-round threshold signing (i.e. they only
/// need to exchange a single set of messages).
///
/// Takes party index `i` (in range `[1; n]`), list `s_l` of parties' indexes from keygen protocol
/// (`s_l[i]` must be an index of party `i` that was used by this party in keygen protocol), and
/// party local secret share `local_key`.
///
/// Returns error if given arguments are contradicting.
pub fn new(i: u16, s_l: Vec<u16>, local_key: LocalKey) -> Result<Self> {
if s_l.len() < 2 {
return Err(Error::TooFewParties);
}
if i == 0 || usize::from(i) > s_l.len() {
return Err(Error::InvalidPartyIndex);
}
let keygen_n = local_key.n;
if s_l.iter().any(|&i| i == 0 || i > keygen_n) {
return Err(Error::InvalidSl);
}
{
// Check if s_l has duplicates
let mut s_l_sorted = s_l.clone();
s_l_sorted.sort();
let mut s_l_sorted_deduped = s_l_sorted.clone();
s_l_sorted_deduped.dedup();
if s_l_sorted != s_l_sorted_deduped {
return Err(Error::InvalidSl);
}
}
let n = u16::try_from(s_l.len()).map_err(|_| Error::TooManyParties { n: s_l.len() })?;
Ok(Self {
round: OfflineR::R0(Round0 { i, s_l, local_key }),
decommit_round: DecommitR::NotStarted,
msgs1: Some(Round1::expects_messages(i, n)),
msgs2: Some(Round2::expects_messages(i, n)),
msgs3: Some(Round3::expects_messages(i, n)),
msgs4: Some(Round4::expects_messages(i, n)),
msgs_com: Some(DecommitRound::expects_messages(i, n)),
msgs_queue: MsgQueue(vec![]),
party_i: i,
party_n: n,
})
}
fn proceed_state(&mut self, may_block: bool) -> Result<()> {
self.proceed_round(may_block)?;
self.proceed_decommit_round(may_block)
}
fn proceed_round(&mut self, may_block: bool) -> Result<()> {
let store1_wants_more = self.msgs1.as_ref().map(|s| s.wants_more()).unwrap_or(false);
let store2_wants_more = self
.msgs2
.as_ref()
.map(|(s1, s2)| s1.wants_more() || s2.wants_more())
.unwrap_or(false);
let store3_wants_more = self
.msgs3
.as_ref()
.map(|(s1, s2)| s1.wants_more() || s2.wants_more())
.unwrap_or(false);
let store4_wants_more = self
.msgs4
.as_ref()
.map(|(s1, s2, s3)| {
s1.wants_more() || s2.wants_more() || s3.wants_more()
})
.unwrap_or(false);
let next_state: OfflineR;
let try_again: bool = match replace(&mut self.round, OfflineR::Gone) {
OfflineR::R0(round) if !round.is_expensive() || may_block => {
if !matches!(self.decommit_round, DecommitR::NotStarted) {
return Err(InternalError::DecommitRoundWasntInInitialState.into());
}
let (next_s, next_d) = round
.proceed(&mut self.msgs_queue)
.map_err(Error::ProceedRound)?;
next_state = OfflineR::R1(next_s);
self.decommit_round = DecommitR::R0(next_d);
true
}
s @ OfflineR::R0(_) => {
next_state = s;
false
}
OfflineR::R1(round) if !store1_wants_more && (!round.is_expensive() || may_block) => {
let store = self.msgs1.take().ok_or(InternalError::StoreGone)?;
let msgs = store
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
next_state = round
.proceed(msgs, &mut self.msgs_queue)
.map(OfflineR::R2)
.map_err(Error::ProceedRound)?;
true
}
s @ OfflineR::R1(_) => {
next_state = s;
false
}
OfflineR::R2(round) if !store2_wants_more && (!round.is_expensive() || may_block) => {
let (store1, store2) = self.msgs2.take().ok_or(InternalError::StoreGone)?;
let msgs1 = store1
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
let msgs2 = store2
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
next_state = round
.proceed(msgs1, msgs2, &mut self.msgs_queue)
.map(OfflineR::CR2)
.map_err(Error::ProceedRound)?;
true
}
s @ OfflineR::R2(_) => {
next_state = s;
false
}
OfflineR::CR2(completed_round) => match self.decommit_round.steal_results() {
Some(result) => {
next_state = OfflineR::R3(Round3::new(completed_round, result));
true
}
None => {
next_state = OfflineR::CR2(completed_round);
false
}
},
OfflineR::R3(round) if !store3_wants_more && (!round.is_expensive() || may_block) => {
let (store1, store2) = self.msgs3.take().ok_or(InternalError::StoreGone)?;
let msgs1 = store1
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
let msgs2 = store2
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
next_state = round
.proceed(msgs1, msgs2, &mut self.msgs_queue)
.map(OfflineR::R4)
.map_err(Error::ProceedRound)?;
true
}
s @ OfflineR::R3(_) => {
next_state = s;
false
}
OfflineR::R4(round) if !store4_wants_more && (!round.is_expensive() || may_block) => {
let (store1, store2, store3) =
self.msgs4.take().ok_or(InternalError::StoreGone)?;
let msgs1 = store1
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
let msgs2 = store2
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
let msgs3 = store3
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
next_state = round
.proceed(msgs1, msgs2, msgs3)
.map(OfflineR::Finished)
.map_err(Error::ProceedRound)?;
false
}
s @ OfflineR::R4(_) => {
next_state = s;
false
}
s @ OfflineR::Finished(_) | s @ OfflineR::Gone => {
next_state = s;
false
}
};
self.round = next_state;
if try_again {
self.proceed_round(may_block)
} else {
Ok(())
}
}
fn proceed_decommit_round(&mut self, may_block: bool) -> Result<()> {
let store_com_wants_more = self
.msgs_com
.as_ref()
.map(|s| s.wants_more())
.unwrap_or(false);
let next_state: DecommitR;
let finished: bool = match replace(&mut self.decommit_round, DecommitR::Gone) {
s @ DecommitR::NotStarted => {
next_state = s;
false
}
DecommitR::R0(round)
if !store_com_wants_more && (!round.is_expensive() || may_block) =>
{
let store = self.msgs_com.take().ok_or(InternalError::StoreGone)?;
let msgs = store
.finish()
.map_err(InternalError::RetrieveMessagesFromStore)?;
next_state = round
.proceed(msgs, &mut self.msgs_queue)
.map(DecommitR::Finished)
.map_err(Error::ProceedRound)?;
true
}
s @ DecommitR::R0(_) => {
next_state = s;
false
}
s @ DecommitR::Finished(_) => {
next_state = s;
true
}
s @ DecommitR::Gone => {
next_state = s;
false
}
};
self.decommit_round = next_state;
if finished {
self.proceed_round(may_block)
} else {
Ok(())
}
}
}
impl StateMachine for OfflineStage {
type MessageBody = OfflineProtocolMessage;
type Err = Error;
type Output = CompletedOfflineStage;
fn handle_incoming(&mut self, msg: Msg<Self::MessageBody>) -> Result<(), Self::Err> {
let current_round = self.current_round();
match msg.body {
OfflineProtocolMessage(OfflineM::M1(m)) => {
let store = self
.msgs1
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 1,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M2A(m)) => {
let (store, _) = self
.msgs2
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 2,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M2B(m)) => {
let (_, store) = self
.msgs2
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 2,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M3A(m)) => {
let (store, _) = self
.msgs3
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 3,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M3B(m)) => {
let (_, store) = self
.msgs3
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 3,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M4A(m)) => {
let (store, _, _) =
self.msgs4
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 4,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M4B(m)) => {
let (_, store, _) =
self.msgs4
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 4,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::M4C(m)) => {
let (_, _, store) =
self.msgs4
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 4,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
OfflineProtocolMessage(OfflineM::MD(m)) => {
let store = self
.msgs_com
.as_mut()
.ok_or(Error::ReceivedOutOfOrderMessage {
current_round,
msg_round: 10,
})?;
store
.push_msg(Msg {
sender: msg.sender,
receiver: msg.receiver,
body: m,
})
.map_err(Error::HandleMessage)?;
}
}
self.proceed_state(false)
}
fn message_queue(&mut self) -> &mut Vec<Msg<Self::MessageBody>> {
&mut self.msgs_queue.0
}
fn wants_to_proceed(&self) -> bool {
let store1_wants_more = self.msgs1.as_ref().map(|s| s.wants_more()).unwrap_or(false);
let store2_wants_more = self
.msgs2
.as_ref()
.map(|(s1, s2)| s1.wants_more() || s2.wants_more())
.unwrap_or(false);
let store3_wants_more = self
.msgs3
.as_ref()
.map(|(s1, s2)| s1.wants_more() || s2.wants_more())
.unwrap_or(false);
let store4_wants_more = self
.msgs4
.as_ref()
.map(|(s1, s2, s3)| {
s1.wants_more() || s2.wants_more() || s3.wants_more()
})
.unwrap_or(false);
let store_com_wants_more = self
.msgs_com
.as_ref()
.map(|s| s.wants_more())
.unwrap_or(false);
let proceed_round = match &self.round {
OfflineR::R0(_) => true,
OfflineR::R1(_) => !store1_wants_more,
OfflineR::R2(_) => !store2_wants_more,
OfflineR::CR2(_) => matches!(&self.decommit_round, DecommitR::Finished(_)),
OfflineR::R3(_) => !store3_wants_more,
OfflineR::R4(_) => !store4_wants_more,
OfflineR::Finished(_) | OfflineR::Gone => false,
};
let proceed_decommit_round = match &self.decommit_round {
DecommitR::NotStarted => false,
DecommitR::R0(_) => !store_com_wants_more,
DecommitR::Finished(_) | DecommitR::Gone => false,
};
proceed_round || proceed_decommit_round
}
fn proceed(&mut self) -> Result<(), Self::Err> {
self.proceed_state(true)
}
fn round_timeout(&self) -> Option<Duration> {
None
}
fn round_timeout_reached(&mut self) -> Self::Err {
panic!("no timeout was set")
}
fn is_finished(&self) -> bool {
matches!(&self.round, OfflineR::Finished(_))
}
fn pick_output(&mut self) -> Option<Result<Self::Output, Self::Err>> {
match self.round {
OfflineR::Finished(_) => (),
OfflineR::Gone => return Some(Err(Error::DoublePickOutput)),
_ => return None,
}
match replace(&mut self.round, OfflineR::Gone) {
OfflineR::Finished(result) => Some(Ok(result)),
_ => unreachable!("guaranteed by match expression above"),
}
}
fn current_round(&self) -> u16 {
match &self.round {
OfflineR::R0(_) => 0,
OfflineR::R1(_) => 1,
OfflineR::R2(_) => 2,
OfflineR::CR2(_) => 2,
OfflineR::R3(_) => 3,
OfflineR::R4(_) => 4,
OfflineR::Finished(_) | OfflineR::Gone => 5,
}
}
fn total_rounds(&self) -> Option<u16> {
Some(4)
}
fn party_ind(&self) -> u16 {
self.party_i
}
fn parties(&self) -> u16 {
self.party_n
}
}
enum OfflineR {
R0(Round0),
R1(Round1),
R2(Round2),
CR2(CompletedRound2),
R3(Round3),
R4(Round4),
Finished(CompletedOfflineStage),
Gone,
}
enum DecommitR {
NotStarted,
R0(DecommitRound),
Finished(Commitments),
Gone,
}
impl DecommitR {
pub fn steal_results(&mut self) -> Option<Commitments> {
match replace(self, DecommitR::Gone) {
DecommitR::Finished(r) => Some(r),
s => {
*self = s;
None
}
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OfflineProtocolMessage(OfflineM);
#[derive(Serialize, Deserialize, Debug, Clone)]
enum OfflineM {
M1(MessageA),
M2A(GWI),
M2B((GammaI, WI)),
M3A(SignDecommitPhase1),
M3B(DeltaI),
M4A(RDash),
M4B(PDLwSlackProof),
M4C(RVal),
MD(SignBroadcastPhase1),
}
struct MsgQueue(Vec<Msg<OfflineProtocolMessage>>);
macro_rules! make_pushable {
($($constructor:ident $t:ty),*$(,)?) => {
$(
impl Push<Msg<$t>> for MsgQueue {
fn push(&mut self, m: Msg<$t>) {
Vec::push(&mut self.0, Msg{
sender: m.sender,
receiver: m.receiver,
body: OfflineProtocolMessage(OfflineM::$constructor(m.body))
})
}
}
)*
};
}
make_pushable! {
M1 MessageA,
M2A GWI,
M2B (GammaI, WI),
M3A SignDecommitPhase1,
M3B DeltaI,
M4A RDash,
M4B PDLwSlackProof,
M4C RVal,
MD SignBroadcastPhase1,
}
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)]
pub enum Error {
/// Too few parties (`n < 2`)
#[error("at least 2 parties are required for signing")]
TooFewParties,
/// Too many parties. `n` must fit into `u16`, so only `n < u16::MAX` values are supported.
#[error("too many parties: n={n}, n must be less than 2^16")]
TooManyParties { n: usize },
/// Party index `i` is not in range `[1; n]`
#[error("party index is not in range [1; n]")]
InvalidPartyIndex,
/// List `s_l` is invalid. Either it contains duplicates (`exist i j. i != j && s_l[i] = s_l[j]`),
/// or contains index that is not in the range `[1; keygen_n]`, `keygen_n` — number of parties
/// participated in DKG (`exist i. s_l[i] = 0 || s_l[i] > keygen_n`).
#[error("invalid s_l")]
InvalidSl,
/// Round proceeding resulted in protocol error
#[error("proceeding round: {0}")]
ProceedRound(rounds::Error),
/// Received message which we didn't expect to receive now (e.g. message from previous round)
#[error(
"didn't expect to receive message from round {msg_round} (being at round {current_round})"
)]
ReceivedOutOfOrderMessage { current_round: u16, msg_round: u16 },
/// Received message didn't pass pre-validation
#[error("received message didn't pass pre-validation: {0}")]
HandleMessage(#[source] StoreErr),
/// [OfflineStage::pick_output] called twice
#[error("pick_output called twice")]
DoublePickOutput,
/// A bug in protocol implementation
#[error("offline stage protocol bug: {0}")]
Bug(InternalError),
}
#[derive(Debug, Error)]
pub enum InternalError {
#[error("store gone")]
StoreGone,
#[error("store reported that it's collected all the messages it needed, but refused to give received messages")]
RetrieveMessagesFromStore(StoreErr),
#[error("decommit round expected to be in NotStarted state")]
DecommitRoundWasntInInitialState,
}
impl From<InternalError> for Error {
fn from(err: InternalError) -> Self {
Error::Bug(err)
}
}
impl IsCritical for Error {
fn is_critical(&self) -> bool {
match self {
Error::TooFewParties => true,
Error::TooManyParties { .. } => true,
Error::InvalidPartyIndex => true,
Error::InvalidSl => true,
Error::ProceedRound(_) => true,
Error::ReceivedOutOfOrderMessage { .. } => false,
Error::HandleMessage(_) => false,
Error::DoublePickOutput => true,
Error::Bug(_) => true,
}
}
}
/// Manual GG20 signing
///
/// After you completed [OfflineStage] and got [CompletedOfflineStage], parties can perform signing
/// simply by broadcasting a single message.
///
/// ## Example
/// ```no_run
/// # use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::{
/// # state_machine::sign::{CompletedOfflineStage, SignManual},
/// # party_i::{LocalSignature, verify},
/// # };
/// # use curv::arithmetic::{BigInt, Converter};
/// # type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
/// # fn broadcast(msg: LocalSignature) -> Result<()> { panic!() }
/// # fn wait_messages() -> Result<Vec<LocalSignature>> { panic!() }
/// # fn main() -> Result<()> {
/// # let completed_offline_stage: CompletedOfflineStage = panic!();
/// let data = BigInt::from_bytes(b"a message");
///
/// // Sign a message locally
/// let (sign, msg) = SignManual::new(data.clone(), completed_offline_stage)?;
/// // Broadcast local signature
/// broadcast(msg)?;
/// // Collect local signatures from all the parties
/// let sigs: Vec<LocalSignature> = wait_messages()?;
/// // Complete signing
/// let signature = sign.complete(sigs)?;
/// // Verify that signature matches joint public key
/// assert!(verify(&signature, completed_offline_stage.public_key(), &data).is_ok());
/// # Ok(())
/// # }
/// ```
pub struct SignManual {
state: Round6,
}
impl SignManual {
pub fn new(
message_bn: BigInt,
completed_offline_stage: CompletedOfflineStage,
) -> Result<(Self, LocalSignature), SignError> {
Round5::new(message_bn, completed_offline_stage)
.proceed_manual()
.map(|(state, m)| (Self { state }, m))
.map_err(SignError::LocalSigning)
}
pub fn complete(self, sigs: Vec<LocalSignature>) -> Result<SignatureRecid, SignError> {
self.state
.proceed_manual(sigs)
.map_err(SignError::CompleteSigning)
}
}
#[derive(Debug, Error)]
pub enum SignError {
#[error("signing message locally: {0}")]
LocalSigning(rounds::Error),
#[error("couldn't complete signing: {0}")]
CompleteSigning(rounds::Error),
}
#[cfg(test)]
mod test {
use curv::arithmetic::Converter;
use curv::cryptographic_primitives::hashing::hash_sha256::HSha256;
use curv::cryptographic_primitives::hashing::traits::Hash;
use round_based::dev::Simulation;
use super::*;
use gg20::party_i::verify;
use gg20::state_machine::keygen::test::simulate_keygen;
fn simulate_offline_stage(
local_keys: Vec<LocalKey>,
s_l: &[u16],
) -> Vec<CompletedOfflineStage> {
let mut simulation = Simulation::new();
simulation.enable_benchmarks(true);
for (i, &keygen_i) in (1..).zip(s_l) {
simulation.add_party(
OfflineStage::new(
i,
s_l.to_vec(),
local_keys[usize::from(keygen_i - 1)].clone(),
)
.unwrap(),
);
}
let stages = simulation.run().unwrap();
println!("Benchmark results:");
println!("{:#?}", simulation.benchmark_results().unwrap());
stages
}
fn simulate_signing(offline: Vec<CompletedOfflineStage>, message: &[u8]) {
let message = HSha256::create_hash(&[&BigInt::from_bytes(message)]);
let pk = offline[0].public_key().clone();
let parties = offline
.iter()
.map(|o| SignManual::new(message.clone(), o.clone()))
.collect::<Result<Vec<_>, _>>()
.unwrap();
let (parties, local_sigs): (Vec<_>, Vec<_>) = parties.into_iter().unzip();
assert!(parties
.into_iter()
.map(|p| p.complete(local_sigs.clone()).unwrap())
.all(|signature| verify(&signature, &pk, &message).is_ok()));
}
#[test]
fn simulate_offline_stage_t1_n2_s2() {
let local_keys = simulate_keygen(1, 2);
simulate_offline_stage(local_keys, &[1, 2]);
}
#[test]
fn simulate_offline_stage_t1_n3_s2() {
let local_keys = simulate_keygen(1, 3);
simulate_offline_stage(local_keys, &[1, 3]);
}
#[test]
fn simulate_offline_stage_t2_n3_s3() {
let local_keys = simulate_keygen(2, 3);
simulate_offline_stage(local_keys, &[1, 2, 3]);
}
#[test]
fn simulate_signing_t1_n2_s2() {
let local_keys = simulate_keygen(1, 2);
let offline_stage = simulate_offline_stage(local_keys, &[1, 2]);
simulate_signing(offline_stage, b"ZenGo")
}
#[test]
fn simulate_signing_t1_n3_s2() {
let local_keys = simulate_keygen(1, 3);
let offline_stage = simulate_offline_stage(local_keys.clone(), &[1, 2]);
simulate_signing(offline_stage, b"ZenGo");
let offline_stage = simulate_offline_stage(local_keys.clone(), &[1, 3]);
simulate_signing(offline_stage, b"ZenGo");
let offline_stage = simulate_offline_stage(local_keys.clone(), &[2, 3]);
simulate_signing(offline_stage, b"ZenGo");
}
#[test]
fn simulate_signing_t2_n3_s3() {
let local_keys = simulate_keygen(2, 3);
let offline_stage = simulate_offline_stage(local_keys, &[1, 2, 3]);
simulate_signing(offline_stage, b"ZenGo")
}
}