Skip to content

Commit

Permalink
Do not reprocess old round ids (#9)
Browse files Browse the repository at this point in the history
* Do not reprocess old round ids

Signed-off-by: Jacinta Ferrant <[email protected]>

* Add tests to ensure old round ids are ignored

Signed-off-by: Jacinta Ferrant <[email protected]>

---------

Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant authored and xoloki committed Apr 11, 2024
1 parent 61745f5 commit 943f865
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 2 deletions.
91 changes: 90 additions & 1 deletion src/state_machine/coordinator/fire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,21 @@ impl<Aggregator: AggregatorTrait> Coordinator<Aggregator> {
State::Idle => {
// Did we receive a coordinator message?
if let Message::DkgBegin(dkg_begin) = &packet.msg {
if self.current_dkg_id >= dkg_begin.dkg_id {
// We have already processed this DKG round
return Ok((None, None));
}
// Set the current sign id to one before the current message to ensure
// that we start the next round at the correct id. (Do this rather
// than overwriting afterwards to ensure logging is accurate)
self.current_dkg_id = dkg_begin.dkg_id.wrapping_sub(1);
let packet = self.start_dkg_round()?;
return Ok((Some(packet), None));
} else if let Message::NonceRequest(nonce_request) = &packet.msg {
if self.current_sign_id >= nonce_request.sign_id {
// We have already processed this sign round
return Ok((None, None));
}
// Set the current sign id to one before the current message to ensure
// that we start the next round at the correct id. (Do this rather
// than overwriting afterwards to ensure logging is accurate)
Expand Down Expand Up @@ -1232,7 +1240,7 @@ impl<Aggregator: AggregatorTrait> CoordinatorTrait for Coordinator<Aggregator> {
pub mod test {
use crate::{
curve::{point::Point, scalar::Scalar},
net::{DkgFailure, DkgPrivateShares, Message, Packet},
net::{DkgBegin, DkgFailure, DkgPrivateShares, Message, NonceRequest, Packet},
state_machine::{
coordinator::{
fire::Coordinator as FireCoordinator,
Expand Down Expand Up @@ -2375,4 +2383,85 @@ pub mod test {
assert_eq!(coordinator.state, State::Idle);
}
}

#[test]
fn old_round_ids_are_ignored_v1() {
old_round_ids_are_ignored::<v1::Aggregator, v1::Signer>();
}

#[test]
fn old_round_ids_are_ignored_v2() {
old_round_ids_are_ignored::<v2::Aggregator, v2::Signer>();
}

fn old_round_ids_are_ignored<Aggregator: AggregatorTrait, Signer: SignerTrait>() {
let (mut coordinators, _) = setup::<FireCoordinator<Aggregator>, Signer>(3, 10);
for coordinator in &mut coordinators {
let id: u64 = 10;
let old_id = id.saturating_sub(1);
coordinator.current_dkg_id = id;
coordinator.current_sign_id = id;
// Attempt to start an old DKG round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: old_id }),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_dkg_id, id);

// Attempt to start the same DKG round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: id }),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_dkg_id, id);

// Attempt to start an old Sign round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::NonceRequest(NonceRequest {
dkg_id: id,
sign_id: old_id,
message: vec![],
sign_iter_id: id,
is_taproot: false,
merkle_root: None,
}),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_sign_id, id);

// Attempt to start the same Sign round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::NonceRequest(NonceRequest {
dkg_id: id,
sign_id: id,
message: vec![],
sign_iter_id: id,
is_taproot: false,
merkle_root: None,
}),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_sign_id, id);
}
}
}
91 changes: 90 additions & 1 deletion src/state_machine/coordinator/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ impl<Aggregator: AggregatorTrait> Coordinator<Aggregator> {
State::Idle => {
// Did we receive a coordinator message?
if let Message::DkgBegin(dkg_begin) = &packet.msg {
if self.current_dkg_id >= dkg_begin.dkg_id {
// We have already processed this DKG round
return Ok((None, None));
}
// Set the current sign id to one before the current message to ensure
// that we start the next round at the correct id. (Do this rather
// then overwriting afterwards to ensure logging is accurate)
self.current_dkg_id = dkg_begin.dkg_id.wrapping_sub(1);
let packet = self.start_dkg_round()?;
return Ok((Some(packet), None));
} else if let Message::NonceRequest(nonce_request) = &packet.msg {
if self.current_sign_id >= nonce_request.sign_id {
// We have already processed this sign round
return Ok((None, None));
}
// Set the current sign id to one before the current message to ensure
// that we start the next round at the correct id. (Do this rather
// then overwriting afterwards to ensure logging is accurate)
Expand Down Expand Up @@ -747,7 +755,7 @@ impl<Aggregator: AggregatorTrait> CoordinatorTrait for Coordinator<Aggregator> {
pub mod test {
use crate::{
curve::scalar::Scalar,
net::Message,
net::{DkgBegin, Message, NonceRequest, Packet},
state_machine::coordinator::{
frost::Coordinator as FrostCoordinator,
test::{
Expand Down Expand Up @@ -857,4 +865,85 @@ pub mod test {
fn process_inbound_messages_v2() {
run_dkg_sign::<FrostCoordinator<v2::Aggregator>, v2::Signer>(5, 2);
}

#[test]
fn old_round_ids_are_ignored_v1() {
old_round_ids_are_ignored::<v1::Aggregator>();
}

#[test]
fn old_round_ids_are_ignored_v2() {
old_round_ids_are_ignored::<v2::Aggregator>();
}

fn old_round_ids_are_ignored<Aggregator: AggregatorTrait>() {
let mut rng = OsRng;
let config = Config::new(10, 40, 28, Scalar::random(&mut rng));
let mut coordinator = FrostCoordinator::<Aggregator>::new(config);
let id: u64 = 10;
let old_id = id.saturating_sub(1);
coordinator.current_dkg_id = id;
coordinator.current_sign_id = id;
// Attempt to start an old DKG round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: old_id }),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_dkg_id, id);

// Attempt to start the same DKG round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: id }),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_dkg_id, id);

// Attempt to start an old Sign round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::NonceRequest(NonceRequest {
dkg_id: id,
sign_id: old_id,
message: vec![],
sign_iter_id: id,
is_taproot: false,
merkle_root: None,
}),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_sign_id, id);

// Attempt to start the same Sign round
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::NonceRequest(NonceRequest {
dkg_id: id,
sign_id: id,
message: vec![],
sign_iter_id: id,
is_taproot: false,
merkle_root: None,
}),
}])
.unwrap();
assert!(packets.is_empty());
assert!(results.is_empty());
assert_eq!(coordinator.state, State::Idle);
assert_eq!(coordinator.current_sign_id, id);
}
}

0 comments on commit 943f865

Please sign in to comment.