Skip to content

Commit

Permalink
Guessing implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoferre committed Oct 10, 2018
1 parent 8d1361e commit cce8a35
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/dynamic_honey_badger/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::{self, Rand, Rng};
use serde::{Deserialize, Serialize};

use super::{ChangeState, DynamicHoneyBadger, JoinPlan, Result, Step, VoteCounter};
use honey_badger::{HoneyBadger, SubsetHandlingStrategy};
use honey_badger::{EncryptionSchedule, HoneyBadger, SubsetHandlingStrategy};
use util::SubRng;
use {Contribution, NetworkInfo, NodeIdT};

Expand All @@ -22,6 +22,8 @@ pub struct DynamicHoneyBadgerBuilder<C, N> {
rng: Box<dyn rand::Rng>,
/// Strategy used to handle the output of the `Subset` algorithm.
subset_handling_strategy: SubsetHandlingStrategy,
/// Threshold encryption schedule
encryption_schedule: EncryptionSchedule,
_phantom: PhantomData<(C, N)>,
}

Expand All @@ -32,6 +34,7 @@ impl<C, N> Default for DynamicHoneyBadgerBuilder<C, N> {
max_future_epochs: 3,
rng: Box::new(rand::thread_rng()),
subset_handling_strategy: SubsetHandlingStrategy::Incremental,
encryption_schedule: EncryptionSchedule::Always,
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -69,12 +72,19 @@ where
self
}

/// Sets the strategy to use when handling `Subset` output.
pub fn encryption_schedule(&mut self, encryption_schedule: EncryptionSchedule) -> &mut Self {
self.encryption_schedule = encryption_schedule;
self
}

/// Creates a new Dynamic Honey Badger instance with an empty buffer.
pub fn build(&mut self, netinfo: NetworkInfo<N>) -> DynamicHoneyBadger<C, N> {
let DynamicHoneyBadgerBuilder {
max_future_epochs,
rng,
subset_handling_strategy,
encryption_schedule,
_phantom,
} = self;
let max_future_epochs = *max_future_epochs;
Expand All @@ -83,6 +93,7 @@ where
.max_future_epochs(max_future_epochs)
.rng(rng.sub_rng())
.subset_handling_strategy(subset_handling_strategy.clone())
.encryption_schedule(encryption_schedule.clone())
.build();
DynamicHoneyBadger {
netinfo,
Expand Down
11 changes: 11 additions & 0 deletions src/honey_badger/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rand::{self, Rand, Rng};
use serde::{Deserialize, Serialize};

use super::HoneyBadger;
use honey_badger::EncryptionSchedule;
use honey_badger::SubsetHandlingStrategy;
use util::SubRng;
use {Contribution, NetworkInfo, NodeIdT};
Expand All @@ -20,6 +21,8 @@ pub struct HoneyBadgerBuilder<C, N> {
rng: Box<dyn Rng>,
/// Strategy used to handle the output of the `Subset` algorithm.
subset_handling_strategy: SubsetHandlingStrategy,
/// Threshold encryption schedule
encryption_schedule: EncryptionSchedule,
_phantom: PhantomData<C>,
}

Expand All @@ -36,6 +39,7 @@ where
max_future_epochs: 3,
rng: Box::new(rand::thread_rng()),
subset_handling_strategy: SubsetHandlingStrategy::Incremental,
encryption_schedule: EncryptionSchedule::Always,
_phantom: PhantomData,
}
}
Expand All @@ -61,6 +65,12 @@ where
self
}

/// Sets the strategy to use when handling `Subset` output.
pub fn encryption_schedule(&mut self, encryption_schedule: EncryptionSchedule) -> &mut Self {
self.encryption_schedule = encryption_schedule;
self
}

/// Creates a new Honey Badger instance.
pub fn build(&mut self) -> HoneyBadger<C, N> {
HoneyBadger {
Expand All @@ -72,6 +82,7 @@ where
incoming_queue: BTreeMap::new(),
rng: Box::new(self.rng.sub_rng()),
subset_handling_strategy: self.subset_handling_strategy.clone(),
encryption_schedule: self.encryption_schedule.clone(),
}
}
}
25 changes: 24 additions & 1 deletion src/honey_badger/epoch_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ impl<N> From<SubsetHandlingStrategy> for SubsetHandler<N> {
}
}

/// A parameter to enable or disable encryption or "every n-th" round.
#[derive(Debug, Clone)]
pub enum EncryptionSchedule {
/// Allways encrypt.
Always,
/// Never encrypt.
Never,
/// "every n-th" round.
EveryNthEpoch(u64),
}

/// The sub-algorithms and their intermediate results for a single epoch.
#[derive(Debug)]
pub struct EpochState<C, N: Rand> {
Expand All @@ -191,6 +202,8 @@ pub struct EpochState<C, N: Rand> {
accepted_proposers: BTreeSet<N>,
/// Determines the behavior upon receiving proposals from `subset`.
subset_handler: SubsetHandler<N>,
/// EncryptionSchedule
encryption_schedule: EncryptionSchedule,
_phantom: PhantomData<C>,
}

Expand All @@ -204,6 +217,7 @@ where
netinfo: Arc<NetworkInfo<N>>,
epoch: u64,
subset_handling_strategy: SubsetHandlingStrategy,
encryption_schedule: EncryptionSchedule,
) -> Result<Self> {
let cs = Subset::new(netinfo.clone(), epoch).map_err(ErrorKind::CreateSubset)?;
Ok(EpochState {
Expand All @@ -213,6 +227,7 @@ where
decryption: BTreeMap::default(),
accepted_proposers: Default::default(),
subset_handler: subset_handling_strategy.into(),
encryption_schedule,
_phantom: PhantomData,
})
}
Expand Down Expand Up @@ -314,8 +329,16 @@ where
is_done,
} = self.subset_handler.handle(cs_output);

let send_encryption = match self.encryption_schedule {
EncryptionSchedule::Always => true,
EncryptionSchedule::Never => false,
EncryptionSchedule::EveryNthEpoch(nth) => self.epoch % nth == 0,
};

for (k, v) in contributions {
step.extend(self.send_decryption_share(k.clone(), &v)?);
if send_encryption {
step.extend(self.send_decryption_share(k.clone(), &v)?);
}

This comment has been minimized.

Copy link
@afck

afck Oct 11, 2018

At this point, if the contributions we received are encrypted, we have to send decryption shares.
The encryption schedule needs to be checked earlier, before sending our contribution, to decide whether we should encrypt it or send as plaintext.

self.accepted_proposers.insert(k);
}

Expand Down
4 changes: 4 additions & 0 deletions src/honey_badger/honey_badger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::epoch_state::EpochState;
use super::{Batch, Error, ErrorKind, HoneyBadgerBuilder, Message, MessageContent, Result};
use {Contribution, DistAlgorithm, NetworkInfo, NodeIdT};

pub use super::epoch_state::EncryptionSchedule;
pub use super::epoch_state::SubsetHandlingStrategy;

/// An instance of the Honey Badger Byzantine fault tolerant consensus algorithm.
Expand All @@ -32,6 +33,8 @@ pub struct HoneyBadger<C, N: Rand> {
pub(super) rng: Box<dyn Rng + Send + Sync>,
/// Represents the optimization strategy to use for output of the `Subset` algorithm.
pub(super) subset_handling_strategy: SubsetHandlingStrategy,
/// Represents the threshold encryption schedule
pub(super) encryption_schedule: EncryptionSchedule,
}

impl<C, N> fmt::Debug for HoneyBadger<C, N>
Expand Down Expand Up @@ -189,6 +192,7 @@ where
self.netinfo.clone(),
epoch,
self.subset_handling_strategy.clone(),
self.encryption_schedule.clone(),
)?),
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/honey_badger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ mod message;
pub use self::batch::Batch;
pub use self::builder::HoneyBadgerBuilder;
pub use self::error::{Error, ErrorKind, Result};
pub use self::honey_badger::{HoneyBadger, Step, SubsetHandlingStrategy};
pub use self::honey_badger::{EncryptionSchedule, HoneyBadger, Step, SubsetHandlingStrategy};
pub use self::message::{Message, MessageContent};

0 comments on commit cce8a35

Please sign in to comment.