Skip to content

Commit

Permalink
refactor(validator): replace dkg validator with validator
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 25, 2024
1 parent 6670da7 commit 935be2d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 87 deletions.
5 changes: 2 additions & 3 deletions ferveo/examples/bench_primitives_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ fn setup(
for i in 0..shares_num {
let mut dkg = setup_dkg(i as usize, shares_num, security_threshold);
let message = dkg.share(rng).expect("Test failed");
let sender = dkg.get_validator(&dkg.me.validator.public_key).unwrap();
let sender = dkg.get_validator(&dkg.me.public_key).unwrap();
transcripts.push((sender.clone(), message.clone()));
}

let mut dkg = setup_dkg(0, shares_num, security_threshold);
for (sender, pvss) in transcripts.into_iter() {
dkg.apply_message(&sender.validator, &pvss)
.expect("Setup failed");
dkg.apply_message(&sender, &pvss).expect("Setup failed");
}
dkg
}
Expand Down
7 changes: 4 additions & 3 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl AggregatedTranscript {
&ciphertext_header.0,
aad,
&validator_keypair.decryption_key,
dkg.0.me.share_index,
dkg.0.me.share_index as usize,
&domain_points,
&dkg.0.pvss_params.g_inv(),
)
Expand All @@ -344,12 +344,13 @@ impl AggregatedTranscript {
&ciphertext_header.0,
aad,
&validator_keypair.decryption_key,
dkg.0.me.share_index,
dkg.0.me.share_index as usize,
&dkg.0.pvss_params.g_inv(),
)?;
let domain_point = dkg.0.domain.element(dkg.0.me.share_index as usize);
Ok(DecryptionShareSimple {
share,
domain_point: dkg.0.domain.element(dkg.0.me.share_index),
domain_point,
})
}
}
Expand Down
87 changes: 31 additions & 56 deletions ferveo/src/dkg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cmp::Ordering, collections::BTreeMap};
use std::collections::BTreeMap;

use ark_ec::{pairing::Pairing, AffineRepr, CurveGroup, Group};
use ark_ec::{pairing::Pairing, AffineRepr, CurveGroup};
use ark_poly::EvaluationDomain;
use ferveo_common::PublicKey;
use measure_time::print_time;
Expand Down Expand Up @@ -62,35 +62,27 @@ impl DkgParams {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct DkgValidator<E: Pairing> {
pub validator: Validator<E>,
pub share_index: usize,
}

impl<E: Pairing> PartialOrd for DkgValidator<E> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl<E: Pairing> Ord for DkgValidator<E> {
fn cmp(&self, other: &Self) -> Ordering {
self.share_index.cmp(&other.share_index)
}
}

pub type ValidatorsMap<E> = BTreeMap<EthereumAddress, DkgValidator<E>>;
pub type ValidatorsMap<E> = BTreeMap<EthereumAddress, Validator<E>>;
pub type PVSSMap<E> = BTreeMap<EthereumAddress, PubliclyVerifiableSS<E>>;

#[derive(Debug, Clone)]
pub enum DkgState<E: Pairing> {
// TODO: Do we need to keep track of the block number?
Sharing { accumulated_shares: u32, block: u32 },
Dealt,
Success { public_key: E::G1Affine },
Invalid,
}

impl<E: Pairing> DkgState<E> {
fn new() -> Self {
DkgState::Sharing {
accumulated_shares: 0,
block: 0,
}
}
}

/// The DKG context that holds all of the local state for participating in the DKG
// TODO: Consider removing Clone to avoid accidentally NOT-mutating state.
// Currently, we're assuming that the DKG is only mutated by the owner of the instance.
Expand All @@ -102,7 +94,7 @@ pub struct PubliclyVerifiableDkg<E: Pairing> {
pub validators: ValidatorsMap<E>,
pub vss: PVSSMap<E>,
pub domain: ark_poly::GeneralEvaluationDomain<E::ScalarField>,
pub me: DkgValidator<E>,
pub me: Validator<E>,
pub state: DkgState<E>,
}

Expand All @@ -128,20 +120,14 @@ impl<E: Pairing> PubliclyVerifiableDkg<E> {
let validators: ValidatorsMap<E> = validators
.iter()
.enumerate()
.map(|(validator_index, validator)| {
(
validator.address.clone(),
DkgValidator {
validator: validator.clone(),
share_index: validator_index,
},
)
.map(|(_validator_index, validator)| {
(validator.address.clone(), validator.clone())
})
.collect();

// Make sure that `me` is a known validator
if let Some(my_validator) = validators.get(&me.address) {
if my_validator.validator.public_key != me.public_key {
if my_validator.public_key != me.public_key {
return Err(Error::ValidatorPublicKeyMismatch);
}
} else {
Expand All @@ -150,32 +136,22 @@ impl<E: Pairing> PubliclyVerifiableDkg<E> {

Ok(Self {
dkg_params: *dkg_params,
pvss_params: PubliclyVerifiableParams::<E> {
g: E::G1::generator(),
h: E::G2::generator(),
},
vss: BTreeMap::new(),
pvss_params: PubliclyVerifiableParams::<E>::default(),
vss: PVSSMap::<E>::new(),
domain,
me: DkgValidator {
validator: me.clone(),
share_index: validators[&me.address].share_index,
},
me: me.clone(),
validators,
state: DkgState::Sharing {
accumulated_shares: 0,
// TODO: Do we need to keep track of the block number?
block: 0,
},
state: DkgState::new(),
})
}

pub fn get_validator(
&self,
public_key: &PublicKey<E>,
) -> Option<&DkgValidator<E>> {
) -> Option<&Validator<E>> {
self.validators
.values()
.find(|validator| &validator.validator.public_key == public_key)
.find(|validator| &validator.public_key == public_key)
}

/// Create a new PVSS instance within this DKG session, contributing to the final key
Expand Down Expand Up @@ -414,7 +390,7 @@ mod test_dealing {
for i in 0..dkg.dkg_params.shares_num() {
let (mut dkg, _) = setup_dkg(i as usize);
let message = dkg.share(rng).unwrap();
let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();
messages.push((sender, message));
}

Expand Down Expand Up @@ -497,7 +473,7 @@ mod test_dealing {
let pvss = dkg.share(rng).unwrap();

// This validator has already sent a PVSS
let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();

// First PVSS is accepted
assert!(dkg.verify_message(&sender, &pvss).is_ok());
Expand Down Expand Up @@ -540,7 +516,7 @@ mod test_dealing {
}
));

let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();

// Sender verifies it's own PVSS transcript
assert!(dkg.verify_message(&sender, &pvss).is_ok());
Expand Down Expand Up @@ -594,8 +570,7 @@ mod test_dealing {
}
));

let sender = dkg.me.validator.clone();

let sender = dkg.me.clone();
dkg.state = DkgState::Success {
public_key: G1::zero(),
};
Expand Down Expand Up @@ -623,7 +598,7 @@ mod test_aggregation {
fn test_aggregate() {
let (mut dkg, _) = setup_dealt_dkg();
let aggregate = dkg.aggregate().unwrap();
let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();
assert!(dkg.verify_message(&sender, &aggregate).is_ok());
assert!(dkg.apply_message(&sender, &aggregate).is_ok());
assert!(matches!(dkg.state, DkgState::Success { .. }));
Expand Down Expand Up @@ -652,7 +627,7 @@ mod test_aggregation {
fn test_aggregate_message_state_guards() {
let (mut dkg, _) = setup_dealt_dkg();
let aggregate = dkg.aggregate().unwrap();
let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();

dkg.state = DkgState::Sharing {
accumulated_shares: 0,
Expand All @@ -675,7 +650,7 @@ mod test_aggregation {
let (mut dkg, _) = setup_dealt_dkg();
dkg.dkg_params.shares_num = 10;
let aggregate = dkg.aggregate().unwrap();
let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();
assert!(dkg.verify_message(&sender, &aggregate).is_err());
}

Expand All @@ -693,7 +668,7 @@ mod test_aggregation {
{
*public_key = G1::zero();
}
let sender = dkg.me.validator.clone();
let sender = dkg.me.clone();
assert!(dkg.verify_message(&sender, &aggregate).is_err());
}
}
Expand Down
20 changes: 12 additions & 8 deletions ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod test_dkg_full {
ciphertext_header,
aad,
&validator_keypair.decryption_key,
validator.share_index,
validator.share_index as usize,
&dkg.pvss_params.g_inv(),
)
.unwrap()
Expand Down Expand Up @@ -277,7 +277,7 @@ mod test_dkg_full {
&ciphertext.header().unwrap(),
AAD,
&validator_keypair.decryption_key,
validator.share_index,
validator.share_index as usize,
&domain_points,
&dkg.pvss_params.g_inv(),
)
Expand Down Expand Up @@ -431,12 +431,14 @@ mod test_dkg_full {
// Current participant receives updates from other participants
let updates_for_participant: Vec<_> = share_updates
.values()
.map(|updates| *updates.get(validator.share_index).unwrap())
.map(|updates| {
*updates.get(validator.share_index as usize).unwrap()
})
.collect();

// Each validator uses their decryption key to update their share
let decryption_key = validator_keypairs
.get(validator.share_index)
.get(validator.share_index as usize)
.unwrap()
.decryption_key;

Expand All @@ -446,7 +448,7 @@ mod test_dkg_full {
pvss_aggregated
.update_private_key_share_for_recovery(
&decryption_key,
validator.share_index,
validator.share_index as usize,
updates_for_participant.as_slice(),
)
.unwrap()
Expand Down Expand Up @@ -572,12 +574,14 @@ mod test_dkg_full {
// Current participant receives updates from other participants
let updates_for_participant: Vec<_> = share_updates
.values()
.map(|updates| *updates.get(validator.share_index).unwrap())
.map(|updates| {
*updates.get(validator.share_index as usize).unwrap()
})
.collect();

// Each validator uses their decryption key to update their share
let decryption_key = validator_keypairs
.get(validator.share_index)
.get(validator.share_index as usize)
.unwrap()
.decryption_key;

Expand All @@ -587,7 +591,7 @@ mod test_dkg_full {
pvss_aggregated
.update_private_key_share_for_recovery(
&decryption_key,
validator.share_index,
validator.share_index as usize,
updates_for_participant.as_slice(),
)
.unwrap()
Expand Down
22 changes: 6 additions & 16 deletions ferveo/src/pvss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl<E: Pairing, T> PubliclyVerifiableSS<E, T> {
// ek_{i}^{eval_i}, i = validator index
fast_multiexp(
// &evals.evals[i..i] = &evals.evals[i]
&[evals.evals[validator.share_index]], // one share per validator
validator.validator.public_key.encryption_key.into_group(),
&[evals.evals[validator.share_index as usize]], // one share per validator
validator.public_key.encryption_key.into_group(),
)[0]
})
.collect::<Vec<ShareEncryptions<E>>>();
Expand Down Expand Up @@ -198,17 +198,12 @@ impl<E: Pairing, T> PubliclyVerifiableSS<E, T> {
/// transcript was at fault so that the can issue a new one. This
/// function may also be used for that purpose.
pub fn verify_full(&self, dkg: &PubliclyVerifiableDkg<E>) -> bool {
let validators = dkg
.validators
.values()
.map(|v| v.validator.clone())
.collect::<Vec<_>>();
let validators = validators.as_slice();
let validators = dkg.validators.values().cloned().collect::<Vec<_>>();
do_verify_full(
&self.coeffs,
&self.shares,
&dkg.pvss_params,
validators,
&validators,
&dkg.domain,
)
}
Expand Down Expand Up @@ -287,17 +282,12 @@ impl<E: Pairing, T: Aggregate> PubliclyVerifiableSS<E, T> {
&self,
dkg: &PubliclyVerifiableDkg<E>,
) -> Result<bool> {
let validators = dkg
.validators
.values()
.map(|v| v.validator.clone())
.collect::<Vec<_>>();
let validators = validators.as_slice();
let validators = dkg.validators.values().cloned().collect::<Vec<_>>();
do_verify_aggregation(
&self.coeffs,
&self.shares,
&dkg.pvss_params,
validators,
&validators,
&dkg.domain,
&dkg.vss,
)
Expand Down
2 changes: 1 addition & 1 deletion ferveo/src/test_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn setup_dealt_dkg_with(
shares_num,
my_index as usize,
);
let me = dkg.me.validator.clone();
let me = dkg.me.clone();
let message = dkg.share(rng).unwrap();
(me, message)
})
Expand Down

0 comments on commit 935be2d

Please sign in to comment.