Skip to content

Commit

Permalink
embed public and private shares into DkgPrivateBegin and DkgEndBegin …
Browse files Browse the repository at this point in the history
…messages
  • Loading branch information
xoloki committed Dec 2, 2024
1 parent abaeca9 commit 38cf8ee
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
9 changes: 9 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ pub struct DkgPrivateBegin {
pub signer_ids: Vec<u32>,
/// Key IDs who responded in time for this DKG round
pub key_ids: Vec<u32>,
/// Include DkgPublicShares to avoid p2p related message delivery
/// order issues when signers communicate directly with each other
pub dkg_public_shares: HashMap<u32, DkgPublicShares>,
}

impl Signable for DkgPrivateBegin {
Expand All @@ -179,6 +182,7 @@ impl Signable for DkgPrivateBegin {
}
for signer_id in &self.signer_ids {
hasher.update(signer_id.to_be_bytes());
self.dkg_public_shares[signer_id].hash(hasher);
}
}
}
Expand Down Expand Up @@ -228,6 +232,9 @@ pub struct DkgEndBegin {
pub signer_ids: Vec<u32>,
/// Key IDs who responded in time for this DKG round
pub key_ids: Vec<u32>,
/// Include DkgPrivateShares to avoid p2p related message delivery
/// order issues when signers communicate directly with each other
pub dkg_private_shares: HashMap<u32, DkgPrivateShares>,
}

impl Signable for DkgEndBegin {
Expand All @@ -239,6 +246,7 @@ impl Signable for DkgEndBegin {
}
for signer_id in &self.signer_ids {
hasher.update(signer_id.to_be_bytes());
self.dkg_private_shares[signer_id].hash(hasher);
}
}
}
Expand Down Expand Up @@ -650,6 +658,7 @@ mod test {
dkg_id: 0,
key_ids: Default::default(),
signer_ids: Default::default(),
dkg_public_shares: Default::default(),
};
let msg = Message::DkgBegin(dkg_begin.clone());
let coordinator_packet_dkg_begin = Packet {
Expand Down
10 changes: 10 additions & 0 deletions src/state_machine/coordinator/fire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ impl<Aggregator: AggregatorTrait> Coordinator<Aggregator> {
dkg_id: self.current_dkg_id,
key_ids: active_key_ids,
signer_ids: self.dkg_public_shares.keys().cloned().collect(),
dkg_public_shares: self
.dkg_public_shares
.iter()
.map(|(id, share)| (*id, share.clone()))
.collect(),
};
let dkg_private_begin_msg = Packet {
sig: dkg_begin
Expand Down Expand Up @@ -463,6 +468,11 @@ impl<Aggregator: AggregatorTrait> Coordinator<Aggregator> {
dkg_id: self.current_dkg_id,
key_ids: active_key_ids,
signer_ids: self.dkg_private_shares.keys().cloned().collect(),
dkg_private_shares: self
.dkg_private_shares
.iter()
.map(|(id, share)| (*id, share.clone()))
.collect(),
};
let dkg_end_begin_msg = Packet {
sig: dkg_end_begin
Expand Down
46 changes: 26 additions & 20 deletions src/state_machine/coordinator/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ impl<Aggregator: AggregatorTrait> Coordinator<Aggregator> {
dkg_id: self.current_dkg_id,
key_ids: (1..self.config.num_keys + 1).collect(),
signer_ids: (0..self.config.num_signers).collect(),
dkg_public_shares: (0..self.config.num_signers)
.map(|id| (id, self.dkg_public_shares[&id].clone()))
.collect(),
};
let dkg_private_begin_msg = Packet {
sig: dkg_begin
Expand All @@ -255,6 +258,9 @@ impl<Aggregator: AggregatorTrait> Coordinator<Aggregator> {
dkg_id: self.current_dkg_id,
key_ids: (0..self.config.num_keys).collect(),
signer_ids: (0..self.config.num_signers).collect(),
dkg_private_shares: (0..self.config.num_signers)
.map(|id| (id, self.dkg_private_shares[&id].clone()))
.collect(),
};
let dkg_end_begin_msg = Packet {
sig: dkg_begin.sign(&self.config.message_private_key).expect(""),
Expand Down Expand Up @@ -841,30 +847,30 @@ pub mod test {
assert_eq!(coordinator.get_state(), State::DkgPublicGather);
assert_eq!(coordinator.current_dkg_id, 0);
}
/*
#[test]
fn start_private_shares_v1() {
start_private_shares::<v1::Aggregator>();
}
#[test]
fn start_private_shares_v1() {
start_private_shares::<v1::Aggregator>();
}

#[test]
fn start_private_shares_v2() {
start_private_shares::<v2::Aggregator>();
}

fn start_private_shares<Aggregator: AggregatorTrait>() {
let mut rng = OsRng;
let config = Config::new(10, 40, 28, Scalar::random(&mut rng));
let mut coordinator = FrostCoordinator::<Aggregator>::new(config);
#[test]
fn start_private_shares_v2() {
start_private_shares::<v2::Aggregator>();
}
coordinator.state = State::DkgPrivateDistribute; // Must be in this state before calling start private shares
fn start_private_shares<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 message = coordinator.start_private_shares().unwrap();
assert!(matches!(message.msg, Message::DkgPrivateBegin(_)));
assert_eq!(coordinator.get_state(), State::DkgPrivateGather);
assert_eq!(coordinator.current_dkg_id, 0);
}
coordinator.state = State::DkgPrivateDistribute; // Must be in this state before calling start private shares
let message = coordinator.start_private_shares().unwrap();
assert!(matches!(message.msg, Message::DkgPrivateBegin(_)));
assert_eq!(coordinator.get_state(), State::DkgPrivateGather);
assert_eq!(coordinator.current_dkg_id, 0);
}
*/
#[test]
fn run_dkg_sign_v1() {
run_dkg_sign::<FrostCoordinator<v1::Aggregator>, v1::Signer>(5, 2);
Expand Down
31 changes: 27 additions & 4 deletions src/state_machine/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ impl<SignerType: SignerTrait> Signer<SignerType> {
self.dkg_private_begin(dkg_private_begin)
}
Message::DkgEndBegin(dkg_end_begin) => self.dkg_end_begin(dkg_end_begin),
Message::DkgPublicShares(dkg_public_shares) => self.dkg_public_share(dkg_public_shares),
Message::DkgPrivateShares(dkg_private_shares) => {
self.dkg_private_shares(dkg_private_shares)
}
//Message::DkgPublicShares(dkg_public_shares) => self.dkg_public_share(dkg_public_shares),
//Message::DkgPrivateShares(dkg_private_shares) => {
// self.dkg_private_shares(dkg_private_shares)
//}
Message::SignatureShareRequest(sign_share_request) => {
self.sign_share_request(sign_share_request)
}
Expand Down Expand Up @@ -674,6 +674,10 @@ impl<SignerType: SignerTrait> Signer<SignerType> {
.cloned()
.collect::<HashSet<u32>>();

for (_, shares) in &dkg_private_begin.dkg_public_shares {
let _ = self.dkg_public_share(shares)?;
}

self.dkg_private_begin_msg = Some(dkg_private_begin.clone());
self.move_to(State::DkgPrivateDistribute)?;

Expand Down Expand Up @@ -728,6 +732,10 @@ impl<SignerType: SignerTrait> Signer<SignerType> {

self.dkg_end_begin_msg = Some(dkg_end_begin.clone());

for (_, shares) in &dkg_end_begin.dkg_private_shares {
let _ = self.dkg_private_shares(shares)?;
}

info!(
"Signer {} received DkgEndBegin for round {}",
self.signer.get_id(),
Expand Down Expand Up @@ -863,6 +871,7 @@ impl<SignerType: SignerTrait> StateMachine<State, Error> for Signer<SignerType>

#[cfg(test)]
pub mod test {

Check failure on line 873 in src/state_machine/signer/mod.rs

View workflow job for this annotation

GitHub Actions / test-all

missing documentation for a module
use hashbrown::HashMap;
use rand_core::OsRng;

use crate::{
Expand Down Expand Up @@ -969,21 +978,35 @@ pub mod test {
let _ = signer
.process(&dkg_public_shares[0])
.expect("failed to process DkgPublicShares");
let mut public_shares = HashMap::new();
if let Message::DkgPublicShares(shares) = &dkg_public_shares[0] {
public_shares.insert(0, shares.clone());
} else {
panic!("");
}
let dkg_private_begin = Message::DkgPrivateBegin(DkgPrivateBegin {
dkg_id: 1,
signer_ids: vec![0],
key_ids: vec![1],
dkg_public_shares: public_shares,
});
let dkg_private_shares = signer
.process(&dkg_private_begin)
.expect("failed to process DkgBegin");
let _ = signer
.process(&dkg_private_shares[0])
.expect("failed to process DkgPrivateShares");
let mut private_shares = HashMap::new();
if let Message::DkgPrivateShares(shares) = &dkg_private_shares[0] {
private_shares.insert(0u32, shares.clone());
} else {
panic!("");
}
let dkg_end_begin = DkgEndBegin {
dkg_id: 1,
signer_ids: vec![0],
key_ids: vec![1],
dkg_private_shares: private_shares,
};
let _ = signer
.dkg_end_begin(&dkg_end_begin)
Expand Down

0 comments on commit 38cf8ee

Please sign in to comment.