Skip to content

Commit

Permalink
secret-sharing/src/churp: Separate handoff kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
peternose committed Sep 10, 2024
1 parent 9c911a5 commit 6cb0497
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 228 deletions.
Empty file added .changelog/5847.trivial.md
Empty file.
44 changes: 28 additions & 16 deletions keymanager/src/churp/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use oasis_core_runtime::{
};

use secret_sharing::{
churp::{encode_shareholder, Dealer, Handoff, HandoffKind, Shareholder, VerifiableSecretShare},
churp::{
encode_shareholder, CommitteeChanged, CommitteeUnchanged, Dealer, DealingPhase, Handoff,
HandoffKind, Shareholder, VerifiableSecretShare,
},
kdc::KeySharer,
poly::{scalar_from_bytes, scalar_to_bytes},
suites::{p384, Suite},
Expand Down Expand Up @@ -114,10 +117,10 @@ struct HandoffInfo<G: Group + GroupEncoding> {
/// The handoff epoch.
epoch: EpochTime,
/// The handoff associated with this information.
handoff: Arc<Handoff<G>>,
handoff: Arc<Box<dyn Handoff<G>>>,
}

pub(crate) trait Handler {
pub(crate) trait Handler: Send + Sync {
/// Returns the verification matrix of the shared secret bivariate
/// polynomial from the last successfully completed handoff.
///
Expand Down Expand Up @@ -340,7 +343,7 @@ pub struct Churp {
churp_state: ChurpState,

/// Cached instances.
instances: Mutex<HashMap<u8, Arc<dyn Handler + Send + Sync>>>,
instances: Mutex<HashMap<u8, Arc<dyn Handler>>>,
/// Cached verified policies.
policies: Arc<VerifiedPolicies>,
}
Expand Down Expand Up @@ -369,11 +372,7 @@ impl Churp {
}
}

fn get_instance(
&self,
churp_id: u8,
runtime_id: Namespace,
) -> Result<Arc<dyn Handler + Send + Sync>> {
fn get_instance(&self, churp_id: u8, runtime_id: Namespace) -> Result<Arc<dyn Handler>> {
// Ensure runtime_id matches.
if self.runtime_id != runtime_id {
return Err(Error::RuntimeMismatch.into());
Expand Down Expand Up @@ -597,7 +596,7 @@ impl<S: Suite> Instance<S> {
&self,
node_id: PublicKey,
status: &Status,
handoff: &Handoff<S::Group>,
handoff: &Arc<Box<dyn Handoff<S::Group>>>,
client: &RemoteClient,
) -> Result<bool> {
let x = encode_shareholder::<S>(&node_id.0, &self.shareholder_dst)?;
Expand Down Expand Up @@ -652,7 +651,7 @@ impl<S: Suite> Instance<S> {
&self,
node_id: PublicKey,
status: &Status,
handoff: &Handoff<S::Group>,
handoff: &Arc<Box<dyn Handoff<S::Group>>>,
client: &RemoteClient,
) -> Result<bool> {
let x = encode_shareholder::<S>(&node_id.0, &self.shareholder_dst)?;
Expand Down Expand Up @@ -686,7 +685,7 @@ impl<S: Suite> Instance<S> {
&self,
node_id: PublicKey,
status: &Status,
handoff: &Handoff<S::Group>,
handoff: &Arc<Box<dyn Handoff<S::Group>>>,
client: &RemoteClient,
) -> Result<bool> {
let x = encode_shareholder::<S>(&node_id.0, &self.shareholder_dst)?;
Expand Down Expand Up @@ -930,7 +929,7 @@ impl<S: Suite> Instance<S> {
}

/// Returns the handoff for the given epoch.
fn get_handoff(&self, epoch: EpochTime) -> Result<Arc<Handoff<S::Group>>> {
fn get_handoff(&self, epoch: EpochTime) -> Result<Arc<Box<dyn Handoff<S::Group>>>> {
let handoff_guard = self.handoff.lock().unwrap();

let handoff_info = handoff_guard
Expand All @@ -943,7 +942,7 @@ impl<S: Suite> Instance<S> {

/// Creates a handoff for the next handoff epoch. If a handoff already
/// exists, the existing one is returned.
fn get_or_create_handoff(&self, status: &Status) -> Result<Arc<Handoff<S::Group>>> {
fn get_or_create_handoff(&self, status: &Status) -> Result<Arc<Box<dyn Handoff<S::Group>>>> {
// Make sure to lock the handoff so that we don't create two handoffs
// for the same epoch.
let mut handoff_guard = self.handoff.lock().unwrap();
Expand All @@ -965,8 +964,21 @@ impl<S: Suite> Instance<S> {
shareholders.push(x);
}
let kind = Self::handoff_kind(status);
let handoff = Handoff::new(threshold, me, shareholders, kind)?;
let handoff = Arc::new(handoff);
let handoff: Arc<Box<dyn Handoff<S::Group>>> = match kind {
HandoffKind::DealingPhase => {
Arc::new(Box::new(DealingPhase::new(threshold, me, shareholders)?))
}
HandoffKind::CommitteeUnchanged => Arc::new(Box::new(CommitteeUnchanged::new(
threshold,
me,
shareholders,
)?)),
HandoffKind::CommitteeChanged => Arc::new(Box::new(CommitteeChanged::new(
threshold,
me,
shareholders,
)?)),
};

// If the committee hasn't changed, we need the latest shareholder
// to randomize its share.
Expand Down
Loading

0 comments on commit 6cb0497

Please sign in to comment.