Skip to content

Commit 249e90d

Browse files
committed
Encrypt dkg and reshare bundles in spawn_blocking.
1 parent 4dc2b7b commit 249e90d

File tree

2 files changed

+86
-40
lines changed

2 files changed

+86
-40
lines changed

timeboost-sequencer/src/decrypt.rs

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use timeboost_types::{
2626
};
2727
use tokio::spawn;
2828
use tokio::sync::mpsc::{Receiver, Sender, channel};
29-
use tokio::task::{JoinError, JoinHandle};
29+
use tokio::task::{JoinError, JoinHandle, spawn_blocking};
3030
use tracing::{debug, error, info, trace, warn};
3131

3232
use crate::config::DecrypterConfig;
@@ -242,23 +242,49 @@ impl Decrypter {
242242
/// # Returns
243243
/// - `Some(DkgBundle)` if a new dealing was successfully created for the current committee.
244244
/// - `None` if already submitted or if encryption key is missing.
245-
pub fn gen_dkg_bundle(&mut self) -> Option<DkgBundle> {
246-
let guard = self.key_stores.read();
247-
let Some(store) = guard.get(self.current) else {
245+
pub async fn gen_dkg_bundle(&mut self) -> Option<DkgBundle> {
246+
let Some(current_store) = self.key_stores.read().get(self.current).cloned() else {
248247
warn!(node = %self.label, committee = %self.current, "missing current key store");
249248
return None;
250249
};
251-
let Some(node_idx) = store.committee().get_index(&self.label) else {
250+
let Some(node_idx) = current_store.committee().get_index(&self.label) else {
252251
warn!(node = %self.label, committee = %self.current, "local key not in store");
253252
return None;
254253
};
255254

256-
let vess = Vess::new_fast();
257-
let mut rng = thread_rng();
258-
let secret = <Vss as VerifiableSecretSharing>::Secret::rand(&mut rng);
259-
let (ct, cm) = vess
260-
.encrypt_shares(store.committee(), store.sorted_keys(), secret, DKG_AAD)
261-
.ok()?;
255+
let (ct, cm) = match spawn_blocking(move || {
256+
let vess = Vess::new_fast();
257+
let mut rng = thread_rng();
258+
let secret = <Vss as VerifiableSecretSharing>::Secret::rand(&mut rng);
259+
vess.encrypt_shares(
260+
current_store.committee(),
261+
current_store.sorted_keys(),
262+
secret,
263+
DKG_AAD,
264+
)
265+
})
266+
.await
267+
{
268+
Ok(Ok(result)) => result,
269+
Ok(Err(e)) => {
270+
warn!(
271+
node = %self.label,
272+
committee = %self.current,
273+
error = ?e,
274+
"failed to produce dkg bundle"
275+
);
276+
return None;
277+
}
278+
Err(e) => {
279+
warn!(
280+
node = %self.label,
281+
committee = %self.current,
282+
error = ?e,
283+
"task failed producing dkg bundle"
284+
);
285+
return None;
286+
}
287+
};
262288
debug!(node = %self.label, curr = %self.current, "produced dkg bundle");
263289
Some(DkgBundle::new((node_idx, self.label), self.current, ct, cm))
264290
}
@@ -268,10 +294,9 @@ impl Decrypter {
268294
/// # Returns
269295
/// - `Some(DkgBundle)` if a Resharing dealing was successfully created.
270296
/// - `None` if already submitted or if encryption key is missing.
271-
pub fn gen_resharing_bundle(&mut self, next_store: KeyStore) -> Option<DkgBundle> {
297+
pub async fn gen_resharing_bundle(&mut self, next_store: KeyStore) -> Option<DkgBundle> {
272298
let committee_id = next_store.committee().id();
273-
let guard = self.key_stores.read();
274-
let Some(current_store) = guard.get(self.current) else {
299+
let Some(current_store) = self.key_stores.read().get(self.current).cloned() else {
275300
warn!(node = %self.label, committee = %self.current, "missing current key store");
276301
return None;
277302
};
@@ -285,15 +310,38 @@ impl Decrypter {
285310
warn!(node = %self.label, committee = %committee_id, "no existing key to reshare");
286311
return None;
287312
};
288-
let vess = Vess::new_fast();
289-
let (ct, cm) = vess
290-
.encrypt_reshares(
313+
314+
let (ct, cm) = match spawn_blocking(move || {
315+
let vess = Vess::new_fast();
316+
vess.encrypt_reshares(
291317
next_store.committee(),
292318
next_store.sorted_keys(),
293319
*dec_key.privkey().share(),
294320
DKG_AAD,
295321
)
296-
.ok()?;
322+
})
323+
.await
324+
{
325+
Ok(Ok(result)) => result,
326+
Ok(Err(e)) => {
327+
warn!(
328+
node = %self.label,
329+
committee = %committee_id,
330+
error = ?e,
331+
"failed to produce reshare bundle"
332+
);
333+
return None;
334+
}
335+
Err(e) => {
336+
warn!(
337+
node = %self.label,
338+
committee = %committee_id,
339+
error = ?e,
340+
"task failed producing resharing bundle"
341+
);
342+
return None;
343+
}
344+
};
297345
debug!(node = %self.label, curr = %self.current, next = %committee_id, "produced resharing bundle");
298346
Some(DkgBundle::new((node_idx, self.label), committee_id, ct, cm))
299347
}
@@ -979,8 +1027,8 @@ impl Worker {
9791027

9801028
/// Get the key store for the current committee.
9811029
fn current_store(&self) -> Result<KeyStore> {
982-
let guard = self.key_stores.read();
983-
guard
1030+
self.key_stores
1031+
.read()
9841032
.get(self.current)
9851033
.cloned()
9861034
.ok_or_else(|| DecrypterError::NoCommittee(self.current))
@@ -1068,7 +1116,7 @@ impl Worker {
10681116
}
10691117

10701118
// process each ciphertext in parallel using spawn_blocking
1071-
let combine_results = tokio::task::spawn_blocking({
1119+
let combine_results = spawn_blocking({
10721120
let key_store = key_store.clone();
10731121
let dec_key = dec_key.clone();
10741122
let ciphertexts: Vec<_> = ciphertexts.collect();
@@ -2162,26 +2210,24 @@ mod tests {
21622210

21632211
/// Generate all DKG bundle (one per decrypter) then enqueue all bundles at all decrypters
21642212
async fn enqueue_all_dkg_bundles(decrypters: &mut [Decrypter], key_store: Option<KeyStore>) {
2165-
let bundles = if let Some(key_store) = key_store {
2166-
decrypters
2167-
.iter_mut()
2168-
.map(|decrypter| {
2169-
decrypter
2170-
.gen_resharing_bundle(key_store.clone())
2171-
.expect("DKG bundle should be generated")
2172-
})
2173-
.collect::<VecDeque<_>>()
2213+
let bundles: VecDeque<_> = if let Some(key_store) = key_store {
2214+
futures::future::join_all(
2215+
decrypters
2216+
.iter_mut()
2217+
.map(|d| d.gen_resharing_bundle(key_store.clone())),
2218+
)
2219+
.await
2220+
.into_iter()
2221+
.collect()
21742222
} else {
2175-
decrypters
2176-
.iter_mut()
2177-
.map(|decrypter| {
2178-
decrypter
2179-
.gen_dkg_bundle()
2180-
.expect("DKG bundle should be generated")
2181-
})
2182-
.collect::<VecDeque<_>>()
2223+
futures::future::join_all(decrypters.iter_mut().map(Decrypter::gen_dkg_bundle))
2224+
.await
2225+
.into_iter()
2226+
.collect()
21832227
};
21842228

2229+
let bundles: VecDeque<_> = bundles.into_iter().flatten().collect();
2230+
21852231
// enqueuing them all to decrypters
21862232
for decrypter in decrypters.iter_mut() {
21872233
for dkg in bundles.clone() {

timeboost-sequencer/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl Task {
274274

275275
// DKG bundle generation
276276
if !self.sailfish.awaits_handover() {
277-
if let Some(bundle) = self.decrypter.gen_dkg_bundle() {
277+
if let Some(bundle) = self.decrypter.gen_dkg_bundle().await {
278278
self.bundles.add_bundles(once(BundleVariant::Dkg(bundle)));
279279
}
280280
}
@@ -350,7 +350,7 @@ impl Task {
350350
error!(node = %self.label, %err, "decrypt next committee error");
351351
}
352352
// Resharing bundle generation
353-
if let Some(bundle) = self.decrypter.gen_resharing_bundle(k) {
353+
if let Some(bundle) = self.decrypter.gen_resharing_bundle(k).await {
354354
self.bundles.add_bundles(once(BundleVariant::Dkg(bundle)));
355355
}
356356
}

0 commit comments

Comments
 (0)