Skip to content

Commit

Permalink
Initial changes matching desired Ferveo API
Browse files Browse the repository at this point in the history
  • Loading branch information
Acentelles committed Aug 18, 2021
1 parent 5cc06d9 commit d38bfea
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 54 deletions.
25 changes: 18 additions & 7 deletions src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,24 @@ pub struct Params {
}

#[derive(Debug, Clone)]
pub enum DKGState<E: ark_ec::PairingEngine> {
Init {
announce_messages: Vec<PubliclyVerifiableAnnouncement<E>>,
},
Sharing {
finalized_weight: u32,
pub enum DKGState {
Init,
Dealt,
Shared,
Aggregated {
finalized_weight: u32
},
Success,
Failure,
Invalid
}
// pub enum DKGState<E: ark_ec::PairingEngine> {
// Init {
// announce_messages: Vec<PubliclyVerifiableAnnouncement<E>>,
// },
// Sharing {
// finalized_weight: u32,
// },
// Success,
// Failure,
// }

93 changes: 46 additions & 47 deletions src/dkg/pv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ where
pub participants: Vec<PubliclyVerifiableParticipant<E>>,
pub vss: BTreeMap<u32, PubliclyVerifiableSS<E>>,
pub domain: ark_poly::Radix2EvaluationDomain<E::Fr>,
pub state: DKGState<E>,
pub state: DKGState,
pub me: usize,
pub local_shares: Vec<E::G2Affine>,
pub announce_messages: Vec<PubliclyVerifiableAnnouncement<E>>,
}

impl<E> PubliclyVerifiableDKG<E>
Expand Down Expand Up @@ -50,17 +51,17 @@ where
participants: vec![],
vss: BTreeMap::new(),
domain,
state: DKGState::<E>::Init {
announce_messages: vec![],
},
state: DKGState::Init,
me: 0, // TODO: invalid value
//final_state: None,
local_shares: vec![],
// TODO: Read from storage
announce_messages: vec![],
})
}
/// Create a new PVSS instance within this DKG session, contributing to the final key
/// `rng` is a cryptographic random number generator
/// Returns a PVSS sharing message to post on-chain
/// Returns a PVSS dealing message to post on-chain
pub fn share<R: Rng>(
&mut self,
rng: &mut R,
Expand All @@ -74,7 +75,7 @@ where
let sharing = vss.clone();
self.vss.insert(self.me as u32, vss);

Ok(PubliclyVerifiableMessage::Sharing(sharing))
Ok(PubliclyVerifiableMessage::Deal(sharing))
}
/// Aggregate all received PVSS messages into a single message, prepared to post on-chain
pub fn aggregate(&mut self) -> PubliclyVerifiableMessage<E> {
Expand All @@ -91,17 +92,13 @@ where
/// Call `finish_announce` once the Announcement phase is complete
/// Partitions the share domain among the announced participants
/// and begins the sharing phase of the DKG
/// TODO: Since announce is no longer a phase, do we still need this?
pub fn finish_announce(&mut self) -> Result<()> {
if let DKGState::Init { announce_messages } = &mut self.state {
self.participants =
partition_domain(&self.params, announce_messages)?;
self.me = self
.find_by_key(&self.ed_key.public)
.ok_or_else(|| anyhow!("self not found"))?;
self.state = DKGState::Sharing {
finalized_weight: 0u32,
};
}
self.participants =
partition_domain(&self.params, &mut self.announce_messages)?;
self.me = self
.find_by_key(&self.ed_key.public)
.ok_or_else(|| anyhow!("self not found"))?;
Ok(())
}
/// Returns the public key generated by the DKG
Expand All @@ -116,16 +113,16 @@ where
/// Create an `Announce` message
/// `stake`: the amount staked by this participant in the DKG
/// Returns an Announcement nessage to post on chain
pub fn announce(&mut self, stake: u64) -> SignedMessage {
SignedMessage::sign(
self.params.tau,
&PubliclyVerifiableMessage::Announce {
stake,
session_key: self.session_keypair.public(),
},
&self.ed_key,
)
}
// pub fn announce(&mut self, stake: u64) -> SignedMessage {
// SignedMessage::sign(
// self.params.tau,
// &PubliclyVerifiableMessage::Announce {
// stake,
// session_key: self.session_keypair.public(),
// },
// &self.ed_key,
// )
// }

/// Handle a DKG related message posted on chain
/// `signer` is the ed25519 public key of the sender of the message
Expand All @@ -136,20 +133,22 @@ where
payload: PubliclyVerifiableMessage<E>,
) -> Result<Option<SignedMessage>> {
match payload {
PubliclyVerifiableMessage::Announce { stake, session_key } => {
if let DKGState::Init { announce_messages } = &mut self.state {
announce_messages.push(
PubliclyVerifiableAnnouncement::<E> {
stake,
session_key,
signer: *signer,
},
);
}
Ok(None)
}
PubliclyVerifiableMessage::Sharing(sharing) => {
if let DKGState::Sharing { finalized_weight } = self.state {
// TODO: Validators don't announce themselves through DKG
// TODO: Instead, we read stakes from storage
// PubliclyVerifiableMessage::Announce { stake, session_key } => {
// if let DKGState::Init { announce_messages } = &mut self.state {
// announce_messages.push(
// PubliclyVerifiableAnnouncement::<E> {
// stake,
// session_key,
// signer: *signer,
// },
// );
// }
// Ok(None)
// }
PubliclyVerifiableMessage::Deal(sharing) => {
if let DKGState::Init = self.state {
let dealer = self.find_by_key(signer).ok_or_else(|| {
anyhow!("received dealing from unknown dealer")
})? as u32;
Expand All @@ -163,7 +162,7 @@ where
Ok(None)
}
PubliclyVerifiableMessage::Aggregate(vss) => {
if let DKGState::Sharing { finalized_weight } = self.state {
if let DKGState::Shared = self.state {
let minimum_weight = self.params.total_weight
//- self.params.failure_threshold
- self.params.security_threshold;
Expand All @@ -173,7 +172,7 @@ where
self.local_shares = local_shares;
self.state = DKGState::Success;
} else {
self.state = DKGState::Sharing {
self.state = DKGState::Aggregated {
finalized_weight: verified_weight,
};
}
Expand All @@ -188,12 +187,12 @@ where
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(bound = "")]
pub enum PubliclyVerifiableMessage<E: PairingEngine> {
Announce {
stake: u64,
session_key: PubliclyVerifiablePublicKey<E>,
},
// Announce {
// stake: u64,
// session_key: PubliclyVerifiablePublicKey<E>,
// },
#[serde(with = "ark_serde")]
Sharing(PubliclyVerifiableSS<E>),
Deal(PubliclyVerifiableSS<E>),
#[serde(with = "ark_serde")]
Aggregate(PubliclyVerifiableSS<E>),
}
Expand Down

0 comments on commit d38bfea

Please sign in to comment.