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
//! Helper functions for the CGGMP protocol drivers.
use rand::rngs::OsRng;
use std::num::NonZeroU16;
use super::Result;
use synedrion::{
ecdsa::{Signature, SigningKey, VerifyingKey},
sessions::{PreprocessedMessage, RoundAccumulator, Session},
ProtocolResult,
};
use crate::{RoundInfo, RoundMessage};
use super::MessageOut;
pub fn round_info<Res>(
session: &Session<Res, Signature, SigningKey, VerifyingKey>,
accum: &RoundAccumulator<Signature, VerifyingKey>,
) -> Result<RoundInfo>
where
Res: ProtocolResult + Send + 'static,
{
let (round_number, is_echo) = session.current_round();
let can_finalize = session.can_finalize(accum)?;
Ok(RoundInfo {
round_number,
is_echo,
can_finalize,
})
}
pub fn proceed<Res>(
session: &mut Session<Res, Signature, SigningKey, VerifyingKey>,
accum: &mut RoundAccumulator<Signature, VerifyingKey>,
verifiers: &[VerifyingKey],
cached_messages: &mut Vec<
PreprocessedMessage<Signature, VerifyingKey>,
>,
key: &VerifyingKey,
) -> Result<Vec<RoundMessage<MessageOut, VerifyingKey>>>
where
Res: ProtocolResult + Send + 'static,
{
let mut outgoing = Vec::new();
let destinations = session.message_destinations();
/*
let key_str = key_to_str(&session.verifier());
println!(
"{key_str}: *** starting round {:?} ***",
session.current_round()
);
*/
for destination in destinations.iter() {
// In production usage, this will happen in a spawned task
// (since it can take some time to create a message),
// and the artifact will be sent back to the host task
// to be added to the accumulator.
let (message, artifact) =
session.make_message(&mut OsRng, destination)?;
/*
println!(
"{key_str}: sending a message to {} (round = {})",
key_to_str(destination),
session.current_round().0,
);
*/
// This will happen in a host task
accum.add_artifact(artifact)?;
let receiver =
verifiers.iter().position(|i| i == destination).unwrap();
let receiver: NonZeroU16 =
((receiver + 1) as u16).try_into()?;
let round: NonZeroU16 =
(session.current_round().0 as u16).try_into()?;
outgoing.push(RoundMessage {
body: message,
sender: key.clone(),
receiver,
round,
});
}
for preprocessed in cached_messages.drain(..) {
// In production usage, this will happen in a spawned task.
// println!("{key_str}: applying a cached message");
let mut rng = OsRng;
let result =
session.process_message(&mut rng, preprocessed).unwrap();
// This will happen in a host task.
accum.add_processed_message(result)??;
}
Ok(outgoing)
}
pub fn handle_incoming<Res>(
session: &mut Session<Res, Signature, SigningKey, VerifyingKey>,
accum: &mut RoundAccumulator<Signature, VerifyingKey>,
message: RoundMessage<MessageOut, VerifyingKey>,
) -> Result<()>
where
Res: ProtocolResult + Send + 'static,
{
if !session.can_finalize(accum)? {
/*
let key_str = key_to_str(&session.verifier());
tracing::info!(
key = %key_str,
current_round = session.current_round().0,
message_round = message.round_number(),
"handle_incoming",
);
*/
// This can be checked if a timeout expired, to see
// which nodes have not responded yet.
let unresponsive_parties = session.missing_messages(accum)?;
assert!(!unresponsive_parties.is_empty());
// let message_round_number = message.round_number();
let (body, from) = message.into_body();
// Perform quick checks before proceeding with the verification.
let preprocessed =
session.preprocess_message(accum, &from, body).unwrap();
if let Some(preprocessed) = preprocessed {
/*
println!(
"{key_str}: applying a message from {} (round {})",
key_to_str(&from),
message_round_number,
);
*/
let mut rng = OsRng;
let result = session
.process_message(&mut rng, preprocessed)
.unwrap();
// This will happen in a host task.
accum.add_processed_message(result)??;
}
}
Ok(())
}