Skip to content

Commit

Permalink
refactor: refactor aggregate method params
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 30, 2024
1 parent e6a7f6e commit 4bb4158
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
15 changes: 12 additions & 3 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ impl Dkg {
for (validator, transcript) in messages {
self.0.deal(validator, transcript)?;
}
Ok(AggregatedTranscript(crate::pvss::aggregate(&self.0.vss)?))
let pvss = messages
.iter()
.map(|(_, t)| t)
.cloned()
.collect::<Vec<PubliclyVerifiableSS<E>>>();
Ok(AggregatedTranscript(crate::pvss::aggregate(&pvss)?))
}

pub fn public_params(&self) -> DkgPublicParameters {
Expand All @@ -269,8 +274,12 @@ pub struct AggregatedTranscript(PubliclyVerifiableSS<E, crate::Aggregated>);

impl AggregatedTranscript {
pub fn new(messages: &[ValidatorMessage]) -> Result<Self> {
let pvss_map = make_pvss_map(messages);
Ok(AggregatedTranscript(crate::pvss::aggregate(&pvss_map)?))
let pvss_list = messages
.iter()
.map(|(_, t)| t)
.cloned()
.collect::<Vec<PubliclyVerifiableSS<E>>>();
Ok(AggregatedTranscript(crate::pvss::aggregate(&pvss_list)?))
}

pub fn verify(
Expand Down
3 changes: 2 additions & 1 deletion ferveo/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ impl<E: Pairing> PubliclyVerifiableDkg<E> {
match self.state {
DkgState::Dealt => {
let public_key = self.public_key();
let pvss_list = self.vss.values().cloned().collect::<Vec<_>>();
Ok(Message::Aggregate(Aggregation {
vss: aggregate(&self.vss)?,
vss: aggregate(&pvss_list)?,
public_key,
}))
}
Expand Down
16 changes: 11 additions & 5 deletions ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ mod test_dkg_full {
Vec<DecryptionShareSimple<E>>,
SharedSecret<E>,
) {
let pvss_aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
let pvss_aggregated = aggregate(&pvss_list).unwrap();
assert!(pvss_aggregated.verify_aggregation(dkg).is_ok());

let decryption_shares: Vec<DecryptionShareSimple<E>> =
Expand Down Expand Up @@ -247,7 +248,8 @@ mod test_dkg_full {
)
.unwrap();

let pvss_aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
let pvss_aggregated = aggregate(&pvss_list).unwrap();
pvss_aggregated.verify_aggregation(&dkg).unwrap();
let domain_points = dkg
.domain
Expand Down Expand Up @@ -434,7 +436,8 @@ mod test_dkg_full {

// Creates updated private key shares
// TODO: Why not using dkg.aggregate()?
let pvss_aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
let pvss_aggregated = aggregate(&pvss_list).unwrap();
pvss_aggregated
.update_private_key_share_for_recovery(
&decryption_key,
Expand Down Expand Up @@ -465,7 +468,9 @@ mod test_dkg_full {
.enumerate()
.map(|(share_index, validator_keypair)| {
// TODO: Why not using dkg.aggregate()?
let pvss_aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list =
dkg.vss.values().cloned().collect::<Vec<_>>();
let pvss_aggregated = aggregate(&pvss_list).unwrap();
pvss_aggregated
.make_decryption_share_simple(
&ciphertext.header().unwrap(),
Expand Down Expand Up @@ -577,7 +582,8 @@ mod test_dkg_full {

// Creates updated private key shares
// TODO: Why not using dkg.aggregate()?
let pvss_aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
let pvss_aggregated = aggregate(&pvss_list).unwrap();
pvss_aggregated
.update_private_key_share_for_recovery(
&decryption_key,
Expand Down
14 changes: 9 additions & 5 deletions ferveo/src/pvss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ impl<E: Pairing, T: Aggregate> PubliclyVerifiableSS<E, T> {
/// into a new PVSS instance
/// See: https://nikkolasg.github.io/ferveo/pvss.html?highlight=aggregate#aggregation
pub(crate) fn aggregate<E: Pairing>(
pvss_map: &PVSSMap<E>,
pvss_list: &[PubliclyVerifiableSS<E>],
) -> Result<PubliclyVerifiableSS<E, Aggregated>> {
let mut pvss_iter = pvss_map.values();
let mut pvss_iter = pvss_list.iter();
let first_pvss = pvss_iter
.next()
.ok_or_else(|| Error::NoTranscriptsToAggregate)?;
Expand Down Expand Up @@ -494,6 +494,7 @@ mod test_pvss {
assert!(!bad_pvss.verify_full(&dkg));
}

// TODO: Move this code to dkg.rs
/// Check that the canonical share indices of validators are expected and enforced
/// by the DKG methods.
#[test]
Expand Down Expand Up @@ -526,7 +527,8 @@ mod test_pvss {
#[test]
fn test_aggregate_pvss() {
let (dkg, _) = setup_dealt_dkg();
let aggregate = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
let aggregate = aggregate(&pvss_list).unwrap();
// Check that a polynomial of the correct degree was created
assert_eq!(
aggregate.coeffs.len(),
Expand All @@ -547,10 +549,12 @@ mod test_pvss {
#[test]
fn test_verify_aggregation_fails_if_constant_term_wrong() {
let (dkg, _) = setup_dealt_dkg();
let mut aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
let mut aggregated = aggregate(&pvss_list).unwrap();
while aggregated.coeffs[0] == G1::zero() {
let (dkg, _) = setup_dkg(0);
aggregated = aggregate(&dkg.vss).unwrap();
let pvss_list = dkg.vss.values().cloned().collect::<Vec<_>>();
aggregated = aggregate(&pvss_list).unwrap();
}
aggregated.coeffs[0] = G1::zero();
assert_eq!(
Expand Down

0 comments on commit 4bb4158

Please sign in to comment.