Skip to content

Commit

Permalink
initial removal of share partitioning
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 20, 2023
1 parent 81d4dd2 commit 9d38f62
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 183 deletions.
17 changes: 3 additions & 14 deletions ferveo-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,13 @@ impl<E: PairingEngine> ValidatorSet<E> {
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Validator<E: PairingEngine> {
pub validator: TendermintValidator<E>,
pub weight: u32,
pub share_start: usize,
pub share_end: usize,
pub share_index: usize,
}

impl<E: PairingEngine> PartialEq for Validator<E> {
fn eq(&self, other: &Self) -> bool {
(
&self.validator,
self.weight,
self.share_start,
self.share_end,
) == (
&other.validator,
other.weight,
other.share_start,
other.share_end,
)
(&self.validator, self.share_index)
== (&other.validator, other.share_index)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ferveo/benches/benchmarks/pvdkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn gen_validators(
ValidatorSet::new(
(0..keypairs.len())
.map(|i| TendermintValidator {
power: i as u64,
power: 1,// TODO: Remove it. //i as u64,
address: format!("validator_{}", i),
public_key: keypairs[i].public(),
})
Expand Down
2 changes: 1 addition & 1 deletion ferveo/examples/pvdkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn setup_dkg(
Params {
tau: 0,
security_threshold: shares / 3,
total_weight: shares,
shares_num: shares,
retry_after: 1,
},
me,
Expand Down
20 changes: 10 additions & 10 deletions ferveo/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub use pv::*;
#[derive(Copy, Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Params {
pub tau: u64,
pub security_threshold: u32, // threshold
pub total_weight: u32, // total weight
pub retry_after: u32,
pub security_threshold: u32,
pub shares_num: u32,
pub retry_after: u32, // TODO: Remove. Not relevant in our scheme.
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -36,7 +36,7 @@ pub enum PvssScheduler {

#[derive(Debug, Clone)]
pub enum DkgState<E: PairingEngine> {
Sharing { accumulated_weight: u32, block: u32 },
Sharing { accumulated_shares: u32, block: u32 },
Dealt,
Success { final_key: E::G1Affine },
Invalid,
Expand All @@ -50,12 +50,12 @@ impl<E: PairingEngine> CanonicalSerialize for DkgState<E> {
) -> Result<(), SerializationError> {
match self {
Self::Sharing {
accumulated_weight,
accumulated_shares,
block,
} => {
CanonicalSerialize::serialize(&0u8, &mut writer)?;
CanonicalSerialize::serialize(
&(*accumulated_weight, *block),
&(*accumulated_shares, *block),
&mut writer,
)
}
Expand All @@ -72,11 +72,11 @@ impl<E: PairingEngine> CanonicalSerialize for DkgState<E> {
fn serialized_size(&self) -> usize {
match self {
Self::Sharing {
accumulated_weight,
accumulated_shares,
block,
} => {
0u8.serialized_size()
+ (*accumulated_weight, *block).serialized_size()
+ (*accumulated_shares, *block).serialized_size()
}
Self::Dealt => 1u8.serialized_size(),
Self::Success { final_key } => {
Expand All @@ -93,12 +93,12 @@ impl<E: PairingEngine> CanonicalDeserialize for DkgState<E> {
let variant = <u8 as CanonicalDeserialize>::deserialize(&mut reader)?;
match variant {
0 => {
let (accumulated_weight, block) =
let (accumulated_shares, block) =
<(u32, u32) as CanonicalDeserialize>::deserialize(
&mut reader,
)?;
Ok(Self::Sharing {
accumulated_weight,
accumulated_shares,
block,
})
}
Expand Down
62 changes: 10 additions & 52 deletions ferveo/src/dkg/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,16 @@ use crate::*;
use ferveo_common::ValidatorSet;
use itertools::izip;

/// partition_domain takes as input a vector of validators from
/// participants in the DKG, containing their total stake amounts
/// and public address (as Bech32m string)
///
/// The validators are *assumed to be* stable-sorted by staking weight
/// (so highest weight participants come first), then by address
/// and the DKG share domain is partitioned into continuous segments roughly
/// the same relative size as the staked weight.
///
/// partition_domain returns a vector of DKG participants
pub fn partition_domain<E: PairingEngine>(
params: &Params,
mut validator_set: ValidatorSet<E>,
) -> Result<Vec<ferveo_common::Validator<E>>> {
// Sort participants from greatest to least stake

// Compute the total amount staked
let total_voting_power =
params.total_weight as f64 / validator_set.total_voting_power() as f64;

// Compute the weight of each participant rounded down
let mut weights = validator_set
pub fn make_validators<E: PairingEngine>(
validator_set: ValidatorSet<E>,
) -> Vec<ferveo_common::Validator<E>> {
validator_set
.validators
.iter()
.map(|p| (p.power as f64 * total_voting_power).floor() as u32)
.collect::<Vec<_>>();

// Add any excess weight to the largest weight participants
let adjust_weight = params
.total_weight
.checked_sub(weights.iter().sum())
.ok_or_else(|| anyhow!("adjusted weight negative"))?
as usize;
for i in &mut weights[0..adjust_weight] {
*i += 1;
}

let mut allocated_weight = 0usize;
let mut participants = vec![];
// note that the order of `participants` corresponds to the same
// order as `validator_set`
for (ix, validator) in validator_set.validators.drain(0..).enumerate() {
participants.push(ferveo_common::Validator::<E> {
validator,
weight: weights[ix],
share_start: allocated_weight,
share_end: allocated_weight + weights[ix] as usize,
});
allocated_weight =
allocated_weight
.checked_add(weights[ix] as usize)
.ok_or_else(|| anyhow!("allocated weight overflow"))?;
}
Ok(participants)
.enumerate()
.map(|(index, validator)| ferveo_common::Validator::<E> {
validator: validator.clone(),
share_index: index,
})
.collect()
}
Loading

0 comments on commit 9d38f62

Please sign in to comment.