From 7c32b2dda9e377d26f14428538af5f4999621b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=BA=C3=B1ez?= Date: Mon, 23 Sep 2024 21:57:04 +0200 Subject: [PATCH] Use PublicKeys instead of internal G2 type when possible --- ferveo-tdec/src/context.rs | 2 +- ferveo-tdec/src/decryption.rs | 2 +- ferveo-tdec/src/lib.rs | 10 +++++----- ferveo/src/dkg.rs | 10 ++++------ ferveo/src/lib.rs | 14 ++++---------- ferveo/src/pvss.rs | 6 +++--- ferveo/src/refresh.rs | 29 +++++++++++++++++------------ 7 files changed, 35 insertions(+), 38 deletions(-) diff --git a/ferveo-tdec/src/context.rs b/ferveo-tdec/src/context.rs index cd553c7f..741d9825 100644 --- a/ferveo-tdec/src/context.rs +++ b/ferveo-tdec/src/context.rs @@ -22,7 +22,7 @@ pub struct PublicDecryptionContextSimple { pub share_commitment: ShareCommitment, pub blinded_key_share: BlindedKeyShare, pub h: E::G2Affine, - pub validator_public_key: E::G2, + pub validator_public_key: ferveo_common::PublicKey, } // TODO: Mark for removal diff --git a/ferveo-tdec/src/decryption.rs b/ferveo-tdec/src/decryption.rs index 7c199fde..0a93cb24 100644 --- a/ferveo-tdec/src/decryption.rs +++ b/ferveo-tdec/src/decryption.rs @@ -234,7 +234,7 @@ pub fn verify_decryption_shares_simple( { 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, ); diff --git a/ferveo-tdec/src/lib.rs b/ferveo-tdec/src/lib.rs index 5fffe02b..19028793 100644 --- a/ferveo-tdec/src/lib.rs +++ b/ferveo-tdec/src/lib.rs @@ -168,9 +168,9 @@ pub mod test_common { share_commitment: ShareCommitment::(*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() { @@ -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, )); @@ -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, )); diff --git a/ferveo/src/dkg.rs b/ferveo/src/dkg.rs index 9388e3c5..29786b5f 100644 --- a/ferveo/src/dkg.rs +++ b/ferveo/src/dkg.rs @@ -179,17 +179,15 @@ impl PubliclyVerifiableDkg { // TODO: Revisit naming later /// Return a map of domain points for the DKG - pub fn domain_and_key_map(&self) -> HashMap, E::G2)> { + pub fn domain_and_key_map( + &self, + ) -> HashMap, PublicKey)> { 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::<_>() } diff --git a/ferveo/src/lib.rs b/ferveo/src/lib.rs index 7aa4a7c9..7d0a0101 100644 --- a/ferveo/src/lib.rs +++ b/ferveo/src/lib.rs @@ -131,8 +131,6 @@ mod test_dkg_full { use super::*; use crate::test_common::*; - type G2 = ::G2; - pub fn create_shared_secret_simple_tdec( dkg: &PubliclyVerifiableDkg, aad: &[u8], @@ -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(), ); } diff --git a/ferveo/src/pvss.rs b/ferveo/src/pvss.rs index f340d000..d8ffb2b9 100644 --- a/ferveo/src/pvss.rs +++ b/ferveo/src/pvss.rs @@ -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, @@ -379,7 +379,7 @@ impl PubliclyVerifiableSS { pub fn refresh( &self, update_transcripts: &HashMap>, - validator_keys_map: &HashMap, + validator_keys_map: &HashMap>, ) -> Result { let num_shares = self.shares.len(); let fft_domain = @@ -411,7 +411,7 @@ impl PubliclyVerifiableSS { 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); diff --git a/ferveo/src/refresh.rs b/ferveo/src/refresh.rs index adb5996b..cfdba44f 100644 --- a/ferveo/src/refresh.rs +++ b/ferveo/src/refresh.rs @@ -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, @@ -180,9 +180,14 @@ pub struct ShareUpdate { impl ShareUpdate { // TODO: Unit tests - pub fn verify(&self, target_validator_public_key: E::G2) -> Result { + pub fn verify( + &self, + target_validator_public_key: &PublicKey, + ) -> Result { + 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 { @@ -204,7 +209,7 @@ pub struct UpdateTranscript { impl UpdateTranscript { /// 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, E::G2)>, // FIXME: eeewww + domain_points_and_keys: &HashMap, PublicKey)>, threshold: u32, rng: &mut impl RngCore, ) -> UpdateTranscript { @@ -219,7 +224,7 @@ impl UpdateTranscript { } pub fn create_recovery_updates( - domain_points_and_keys: &HashMap, E::G2)>, // FIXME: eeewww + domain_points_and_keys: &HashMap, PublicKey)>, x_r: &DomainPoint, threshold: u32, rng: &mut impl RngCore, @@ -237,7 +242,7 @@ impl UpdateTranscript { // TODO: Unit tests pub fn verify_recovery( &self, - validator_public_keys: &HashMap, + validator_public_keys: &HashMap>, domain: &ark_poly::GeneralEvaluationDomain, root: E::ScalarField, ) -> Result { @@ -254,7 +259,7 @@ impl UpdateTranscript { 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 @@ -291,7 +296,7 @@ impl UpdateTranscript { pub fn verify_refresh( &self, - validator_public_keys: &HashMap, + validator_public_keys: &HashMap>, domain: &ark_poly::GeneralEvaluationDomain, ) -> Result { self.verify_recovery( @@ -378,10 +383,9 @@ impl HandoverTranscript { /// 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::G2) -// TODO: Replace E::G2 with ferveo_common::PublicKey +// TODO: Use newtype type for (DomainPoint, PublicKey) fn prepare_share_updates_with_root( - domain_points_and_keys: &HashMap, E::G2)>, // FIXME: eeewww + domain_points_and_keys: &HashMap, PublicKey)>, root: &DomainPoint, threshold: u32, rng: &mut impl RngCore, @@ -400,7 +404,8 @@ fn prepare_share_updates_with_root( .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)