Skip to content

Commit

Permalink
Use PublicKeys instead of internal G2 type when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
cygnusv committed Sep 23, 2024
1 parent 966e265 commit 7c32b2d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion ferveo-tdec/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct PublicDecryptionContextSimple<E: Pairing> {
pub share_commitment: ShareCommitment<E>,
pub blinded_key_share: BlindedKeyShare<E>,
pub h: E::G2Affine,
pub validator_public_key: E::G2,
pub validator_public_key: ferveo_common::PublicKey<E>,
}

// TODO: Mark for removal
Expand Down
2 changes: 1 addition & 1 deletion ferveo-tdec/src/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub fn verify_decryption_shares_simple<E: Pairing>(
{
let is_valid = decryption_share.verify(
y_i,
&pub_context.validator_public_key.into_affine(),
&pub_context.validator_public_key.encryption_key,
&pub_context.h.into(),
ciphertext,
);
Expand Down
10 changes: 5 additions & 5 deletions ferveo-tdec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ pub mod test_common {
share_commitment: ShareCommitment::<E>(*share_commit), // FIXME
blinded_key_share,
h,
validator_public_key: blinded_key_share
.validator_public_key
.into_group(),
validator_public_key: ferveo_common::PublicKey {
encryption_key: blinded_key_share.validator_public_key,
},
});
}
for private_ctxt in private_contexts.iter_mut() {
Expand Down Expand Up @@ -458,7 +458,7 @@ mod tests {

assert!(!has_bad_checksum.verify(
&pub_contexts[0].blinded_key_share.blinded_key_share,
&pub_contexts[0].validator_public_key.into_affine(),
&pub_contexts[0].validator_public_key.encryption_key,
&pub_contexts[0].h.into_group(),
&ciphertext,
));
Expand All @@ -469,7 +469,7 @@ mod tests {

assert!(!has_bad_share.verify(
&pub_contexts[0].blinded_key_share.blinded_key_share,
&pub_contexts[0].validator_public_key.into_affine(),
&pub_contexts[0].validator_public_key.encryption_key,
&pub_contexts[0].h.into_group(),
&ciphertext,
));
Expand Down
10 changes: 4 additions & 6 deletions ferveo/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,15 @@ impl<E: Pairing> PubliclyVerifiableDkg<E> {

// TODO: Revisit naming later
/// Return a map of domain points for the DKG
pub fn domain_and_key_map(&self) -> HashMap<u32, (DomainPoint<E>, E::G2)> {
pub fn domain_and_key_map(
&self,
) -> HashMap<u32, (DomainPoint<E>, PublicKey<E>)> {
let map = self.domain_point_map();
self.validators
.values()
.map(|v| {
let domain_point = map.get(&v.share_index).unwrap();
// TODO: Use PublicKey directly. See same problem in lib.rs::test_dkg_simple_tdec_share_refreshing
(
v.share_index,
(*domain_point, E::G2::from(v.public_key.encryption_key)),
)
(v.share_index, (*domain_point, v.public_key))
})
.collect::<_>()
}
Expand Down
14 changes: 4 additions & 10 deletions ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ mod test_dkg_full {
use super::*;
use crate::test_common::*;

type G2 = <ark_bls12_381::Bls12_381 as ark_ec::pairing::Pairing>::G2;

pub fn create_shared_secret_simple_tdec(
dkg: &PubliclyVerifiableDkg<E>,
aad: &[u8],
Expand Down Expand Up @@ -669,14 +667,10 @@ mod test_dkg_full {
);
validator_map.insert(
validator.share_index,
// TODO: Probably should consume public keys. See domain_and_key_map() in dkg.rs
G2::from(
validator_keypairs
.get(validator.share_index as usize)
.unwrap()
.public_key()
.encryption_key,
),
validator_keypairs
.get(validator.share_index as usize)
.unwrap()
.public_key(),
);
}

Expand Down
6 changes: 3 additions & 3 deletions ferveo/src/pvss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ark_poly::{
polynomial::univariate::DensePolynomial, DenseUVPolynomial,
EvaluationDomain, Polynomial,
};
use ferveo_common::{serialization, Keypair};
use ferveo_common::{serialization, Keypair, PublicKey};
use ferveo_tdec::{
BlindedKeyShare, CiphertextHeader, DecryptionSharePrecomputed,
DecryptionShareSimple,
Expand Down Expand Up @@ -379,7 +379,7 @@ impl<E: Pairing, T: Aggregate> PubliclyVerifiableSS<E, T> {
pub fn refresh(
&self,
update_transcripts: &HashMap<u32, UpdateTranscript<E>>,
validator_keys_map: &HashMap<u32, E::G2>,
validator_keys_map: &HashMap<u32, PublicKey<E>>,
) -> Result<Self> {
let num_shares = self.shares.len();
let fft_domain =
Expand Down Expand Up @@ -411,7 +411,7 @@ impl<E: Pairing, T: Aggregate> PubliclyVerifiableSS<E, T> {
validator_public_key: validator_keys_map
.get(&(index as u32))
.unwrap()
.into_affine(),
.encryption_key,
};
let updated_share = UpdatableBlindedKeyShare(blinded_key_share)
.apply_share_updates(update_transcripts, index as u32);
Expand Down
29 changes: 17 additions & 12 deletions ferveo/src/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ark_poly::{
Polynomial,
};
use ark_std::UniformRand;
use ferveo_common::{serialization, Keypair};
use ferveo_common::{serialization, Keypair, PublicKey};
use ferveo_tdec::{
prepare_combine_simple, BlindedKeyShare, CiphertextHeader,
DecryptionSharePrecomputed, DecryptionShareSimple,
Expand Down Expand Up @@ -180,9 +180,14 @@ pub struct ShareUpdate<E: Pairing> {

impl<E: Pairing> ShareUpdate<E> {
// TODO: Unit tests
pub fn verify(&self, target_validator_public_key: E::G2) -> Result<bool> {
pub fn verify(
&self,
target_validator_public_key: &PublicKey<E>,
) -> Result<bool> {
let public_key_point: E::G2Affine =
target_validator_public_key.encryption_key;
let is_valid = E::pairing(E::G1::generator(), self.update)
== E::pairing(self.commitment, target_validator_public_key);
== E::pairing(self.commitment, public_key_point);
if is_valid {
Ok(true)
} else {
Expand All @@ -204,7 +209,7 @@ pub struct UpdateTranscript<E: Pairing> {
impl<E: Pairing> UpdateTranscript<E> {
/// From PSS paper, section 4.2.1, (https://link.springer.com/content/pdf/10.1007/3-540-44750-4_27.pdf)
pub fn create_refresh_updates(
domain_points_and_keys: &HashMap<u32, (DomainPoint<E>, E::G2)>, // FIXME: eeewww
domain_points_and_keys: &HashMap<u32, (DomainPoint<E>, PublicKey<E>)>,
threshold: u32,
rng: &mut impl RngCore,
) -> UpdateTranscript<E> {
Expand All @@ -219,7 +224,7 @@ impl<E: Pairing> UpdateTranscript<E> {
}

pub fn create_recovery_updates(
domain_points_and_keys: &HashMap<u32, (DomainPoint<E>, E::G2)>, // FIXME: eeewww
domain_points_and_keys: &HashMap<u32, (DomainPoint<E>, PublicKey<E>)>,
x_r: &DomainPoint<E>,
threshold: u32,
rng: &mut impl RngCore,
Expand All @@ -237,7 +242,7 @@ impl<E: Pairing> UpdateTranscript<E> {
// TODO: Unit tests
pub fn verify_recovery(
&self,
validator_public_keys: &HashMap<u32, E::G2>,
validator_public_keys: &HashMap<u32, PublicKey<E>>,
domain: &ark_poly::GeneralEvaluationDomain<E::ScalarField>,
root: E::ScalarField,
) -> Result<bool> {
Expand All @@ -254,7 +259,7 @@ impl<E: Pairing> UpdateTranscript<E> {
for (index, update) in self.updates.iter() {
// Next, validate share updates against their corresponding target validators
update
.verify(*validator_public_keys.get(index).unwrap())
.verify(validator_public_keys.get(index).unwrap())
.unwrap();

// Finally, validate update commitments against update polynomial commitments
Expand Down Expand Up @@ -291,7 +296,7 @@ impl<E: Pairing> UpdateTranscript<E> {

pub fn verify_refresh(
&self,
validator_public_keys: &HashMap<u32, E::G2>,
validator_public_keys: &HashMap<u32, PublicKey<E>>,
domain: &ark_poly::GeneralEvaluationDomain<E::ScalarField>,
) -> Result<bool> {
self.verify_recovery(
Expand Down Expand Up @@ -378,10 +383,9 @@ impl<E: Pairing> HandoverTranscript<E> {
/// This is a helper function for `ShareUpdate::create_share_updates_for_recovery` and `ShareUpdate::create_share_updates_for_refresh`
/// It generates a new random polynomial with a defined root and evaluates it at each of the participants' indices.
/// The result is a map of share updates.
// TODO: Use newtype type ??? = (DomainPoint<E>, E::G2)
// TODO: Replace E::G2 with ferveo_common::PublicKey
// TODO: Use newtype type for (DomainPoint<E>, PublicKey<E>)
fn prepare_share_updates_with_root<E: Pairing>(
domain_points_and_keys: &HashMap<u32, (DomainPoint<E>, E::G2)>, // FIXME: eeewww
domain_points_and_keys: &HashMap<u32, (DomainPoint<E>, PublicKey<E>)>,
root: &DomainPoint<E>,
threshold: u32,
rng: &mut impl RngCore,
Expand All @@ -400,7 +404,8 @@ fn prepare_share_updates_with_root<E: Pairing>(
.map(|(share_index, tuple)| {
let (x_i, pubkey_i) = tuple;
let eval = update_poly.evaluate(x_i);
let update = pubkey_i.mul(eval).into_affine();
let update =
E::G2::from(pubkey_i.encryption_key).mul(eval).into_affine();
let commitment = g.mul(eval).into_affine();
let share_update = ShareUpdate { update, commitment };
(*share_index, share_update)
Expand Down

0 comments on commit 7c32b2d

Please sign in to comment.