Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 20, 2023
1 parent 57255f5 commit 7cad9ae
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 48 deletions.
1 change: 1 addition & 0 deletions ferveo/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ark_poly::{
EvaluationDomain, Polynomial,
};
use ark_serialize::*;
use bincode::Options;
use ed25519_dalek as ed25519;

pub mod common;
Expand Down
85 changes: 52 additions & 33 deletions ferveo/src/dkg/pv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<E: PairingEngine> PubliclyVerifiableDkg<E> {
let domain = ark_poly::Radix2EvaluationDomain::<E::Fr>::new(
params.shares_num as usize,
)
.ok_or_else(|| anyhow!("unable to construct domain"))?;
.ok_or_else(|| anyhow!("unable to construct domain"))?;

// keep track of the owner of this instance in the validator set
let me = validator_set
Expand Down Expand Up @@ -78,22 +78,22 @@ impl<E: PairingEngine> PubliclyVerifiableDkg<E> {
pub fn increase_block(&mut self) -> PvssScheduler {
match self.state {
DkgState::Sharing { ref mut block, .. }
if !self.vss.contains_key(&(self.me as u32)) =>
{
*block += 1;
// if our scheduled window begins, issue PVSS
if self.window.0 + 1 == *block {
PvssScheduler::Issue
} else if &self.window.1 < block {
// reset the window during which we try to get our
// PVSS on chain
*block = self.window.0 + 1;
// reissue PVSS
PvssScheduler::Issue
} else {
PvssScheduler::Wait
}
if !self.vss.contains_key(&(self.me as u32)) =>
{
*block += 1;
// if our scheduled window begins, issue PVSS
if self.window.0 + 1 == *block {
PvssScheduler::Issue
} else if &self.window.1 < block {
// reset the window during which we try to get our
// PVSS on chain
*block = self.window.0 + 1;
// reissue PVSS
PvssScheduler::Issue
} else {
PvssScheduler::Wait
}
}
_ => PvssScheduler::Wait,
}
}
Expand Down Expand Up @@ -222,12 +222,12 @@ impl<E: PairingEngine> PubliclyVerifiableDkg<E> {
}

#[derive(
Serialize,
Deserialize,
Clone,
Debug,
CanonicalSerialize,
CanonicalDeserialize,
Serialize,
Deserialize,
Clone,
Debug,
CanonicalSerialize,
CanonicalDeserialize,
)]
#[serde(bound = "")]
pub struct Aggregation<E: PairingEngine> {
Expand Down Expand Up @@ -255,14 +255,15 @@ pub(crate) mod test_common {

pub type G1 = <EllipticCurve as PairingEngine>::G1Affine;

pub fn gen_n_keypairs(n: u32) -> Vec<ferveo_common::Keypair<EllipticCurve>> {
pub fn gen_n_keypairs(
n: u32,
) -> Vec<ferveo_common::Keypair<EllipticCurve>> {
let rng = &mut ark_std::test_rng();
(0..n)
.map(|_| ferveo_common::Keypair::<EllipticCurve>::new(rng))
.collect()
}


/// Generate a set of keypairs for each validator
pub fn gen_keypairs() -> Vec<ferveo_common::Keypair<EllipticCurve>> {
gen_n_keypairs(4)
Expand Down Expand Up @@ -290,8 +291,13 @@ pub(crate) mod test_common {
gen_n_validators(keypairs, 4)
}

pub fn setup_dkg_for_n_validators(n_validators: u32, security_threshold: u32, shares_num: u32, my_index: usize) -> PubliclyVerifiableDkg<EllipticCurve> {
let keypairs = gen_n_keypairs(n_validators );
pub fn setup_dkg_for_n_validators(
n_validators: u32,
security_threshold: u32,
shares_num: u32,
my_index: usize,
) -> PubliclyVerifiableDkg<EllipticCurve> {
let keypairs = gen_n_keypairs(n_validators);
let validators = gen_n_validators(&keypairs, n_validators);
let me = validators.validators[my_index].clone();
PubliclyVerifiableDkg::new(
Expand All @@ -305,7 +311,7 @@ pub(crate) mod test_common {
me,
keypairs[my_index],
)
.expect("Setup failed")
.expect("Setup failed")
}

/// Create a test dkg
Expand All @@ -315,27 +321,40 @@ pub(crate) mod test_common {
setup_dkg_for_n_validators(4, 2, 6, validator)
}


/// Set up a dkg with enough pvss transcripts to meet the threshold
///
/// The correctness of this function is tested in the module [`test_dealing`]
pub fn setup_dealt_dkg() -> PubliclyVerifiableDkg<EllipticCurve> {
setup_dealt_dkg_with_n_validators(4, 2, 6)
}

pub fn setup_dealt_dkg_with_n_validators(n_validators: u32, security_threshold: u32, shares_num: u32) -> PubliclyVerifiableDkg<EllipticCurve> {
pub fn setup_dealt_dkg_with_n_validators(
n_validators: u32,
security_threshold: u32,
shares_num: u32,
) -> PubliclyVerifiableDkg<EllipticCurve> {
let rng = &mut ark_std::test_rng();

// Gather everyone's transcripts
let transcripts = (0..n_validators)
.map(|i| {
let mut dkg = setup_dkg_for_n_validators(n_validators, security_threshold, shares_num, i as usize);
let mut dkg = setup_dkg_for_n_validators(
n_validators,
security_threshold,
shares_num,
i as usize,
);
dkg.share(rng).expect("Test failed")
})
.collect::<Vec<_>>();

// Our test dkg
let mut dkg = setup_dkg_for_n_validators(n_validators, security_threshold, shares_num, 0);
let mut dkg = setup_dkg_for_n_validators(
n_validators,
security_threshold,
shares_num,
0,
);
transcripts
.into_iter()
.enumerate()
Expand All @@ -344,7 +363,7 @@ pub(crate) mod test_common {
dkg.validators[sender].validator.clone(),
pvss,
)
.expect("Setup failed");
.expect("Setup failed");
});
dkg
}
Expand Down Expand Up @@ -377,7 +396,7 @@ mod test_dkg_init {
},
keypair,
)
.expect_err("Test failed");
.expect_err("Test failed");
assert_eq!(
err.to_string(),
"could not find this validator in the provided validator set"
Expand Down
38 changes: 26 additions & 12 deletions ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ mod test_dkg_full {
let ciphertext = tpke::encrypt::<_, E>(msg, aad, &public_key, rng);

let validator_keypair = gen_n_keypairs(1)[0];
let encrypted_shares = batch_to_projective(&dkg.vss.get(&0).unwrap().shares);
let encrypted_shares =
batch_to_projective(&dkg.vss.get(&0).unwrap().shares);

let decryption_shares =
encrypted_shares.iter().map(|encrypted_share| {
let decryption_shares = encrypted_shares
.iter()
.map(|encrypted_share| {
// Decrypt private key shares https://nikkolasg.github.io/ferveo/pvss.html#validator-decryption-of-private-key-shares
let z_i = encrypted_share.mul(validator_keypair.decryption_key.inverse().unwrap().into_repr());
let z_i = encrypted_share.mul(
validator_keypair
.decryption_key
.inverse()
.unwrap()
.into_repr(),
);
let u = ciphertext.commitment;
let c_i = E::pairing(u, z_i);
c_i

E::pairing(u, z_i)
})
.collect::<Vec<_>>();

Expand All @@ -77,9 +85,12 @@ mod test_dkg_full {
.elements()
.take(decryption_shares.len())
.collect::<Vec<_>>();
let lagrange_coeffs = tpke::prepare_combine_simple::<E>(&shares_x);
let lagrange_coeffs = tpke::prepare_combine_simple::<E>(shares_x);

let s = tpke::share_combine_simple::<E>(&decryption_shares, &lagrange_coeffs);
let s = tpke::share_combine_simple::<E>(
&decryption_shares,
&lagrange_coeffs,
);

let plaintext =
tpke::checked_decrypt_with_shared_secret(&ciphertext, aad, &s);
Expand Down Expand Up @@ -131,8 +142,8 @@ mod test_dkg_full {
.map(|(keypair, encrypted_shares)| {
let z_i = encrypted_shares.mul(keypair.decryption_key);
let u = ciphertext.commitment;
let c_i = E::pairing(u, z_i);
c_i

E::pairing(u, z_i)
})
.collect::<Vec<_>>();

Expand All @@ -141,9 +152,12 @@ mod test_dkg_full {
.elements()
.take(decryption_shares.len())
.collect::<Vec<_>>();
let lagrange_coeffs = tpke::prepare_combine_simple::<E>(&shares_x);
let lagrange_coeffs = tpke::prepare_combine_simple::<E>(shares_x);

let s = tpke::share_combine_simple::<E>(&decryption_shares, &lagrange_coeffs);
let s = tpke::share_combine_simple::<E>(
&decryption_shares,
&lagrange_coeffs,
);

let plaintext =
tpke::checked_decrypt_with_shared_secret(&ciphertext, aad, &s);
Expand Down
4 changes: 2 additions & 2 deletions subproductdomain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl<F: FftField> SubproductTree<F> {
pub fn derivative<F: FftField>(f: &Poly<F>) -> Poly<F> {
let mut coeffs = Vec::with_capacity(f.coeffs().len() - 1);
for (i, c) in f.coeffs.iter().enumerate().skip(1) {
coeffs.push(F::from(i as u64) * c);
coeffs.push(F::from(i as u128) * c);
}
Poly::<F> { coeffs }
}
Expand Down Expand Up @@ -374,7 +374,7 @@ pub fn toeplitz_mul<E: PairingEngine, const NORMALIZE: bool>(

Ok((
tmp[..toeplitz_size].to_vec(),
E::Fr::from(domain.size() as u64).inverse().unwrap(),
E::Fr::from(domain.size() as u128).inverse().unwrap(),
))
}

Expand Down
Empty file added tpke/benches/benchmarks.rs
Empty file.
2 changes: 1 addition & 1 deletion tpke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ mod tests {
&contexts[0].public_decryption_contexts,
);

let shared_secret =
let shared_secret =
share_combine_simple::<E>(&decryption_shares, &lagrange);

test_ciphertext_validation_fails(msg, aad, &ciphertext, &shared_secret);
Expand Down

0 comments on commit 7cad9ae

Please sign in to comment.