From 07c476c86a1bd640356ad67199e8e7c5bba2c97f Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Tue, 20 Feb 2024 11:57:06 +0100
Subject: [PATCH] refactor: update serde serialization
---
ferveo/src/api.rs | 71 ++++++++++++++++++++-----------------------
ferveo/src/refresh.rs | 28 ++++++++++++++---
2 files changed, 56 insertions(+), 43 deletions(-)
diff --git a/ferveo/src/api.rs b/ferveo/src/api.rs
index 4555b436..3e0f80ec 100644
--- a/ferveo/src/api.rs
+++ b/ferveo/src/api.rs
@@ -36,8 +36,8 @@ pub type Transcript = PubliclyVerifiableSS;
pub type ValidatorMessage = (Validator, Transcript);
// Normally, we would use a custom trait for this, but we can't because
-// the arkworks will not let us create a blanket implementation for G1Affine
-// and Fr types. So instead, we're using this shared utility function:
+// the `arkworks` will not let us create a blanket implementation for G1Affine
+// and `Fr` types. So instead, we're using this shared utility function:
pub fn to_bytes(item: &T) -> Result> {
let mut writer = Vec::new();
item.serialize_compressed(&mut writer)?;
@@ -356,9 +356,7 @@ impl AggregatedTranscript {
Ok(PrivateKeyShare(
self.0
.aggregate
- .decrypt_private_key_share(validator_keypair, share_index)?
- .0
- .clone(),
+ .decrypt_private_key_share(validator_keypair, share_index)?,
))
}
@@ -426,7 +424,7 @@ impl ShareRecoveryUpdate {
#[serde_as]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
-pub struct ShareRefreshUpdate(pub ferveo_tdec::PrivateKeyShare);
+pub struct ShareRefreshUpdate(pub crate::ShareRefreshUpdate);
impl ShareRefreshUpdate {
pub fn create_share_updates(dkg: &Dkg) -> Result> {
@@ -437,8 +435,8 @@ impl ShareRefreshUpdate {
dkg.0.dkg_params.security_threshold(),
rng,
)
- .iter()
- .map(|update| ShareRefreshUpdate(update.0.clone()))
+ .into_iter()
+ .map(ShareRefreshUpdate)
.collect();
Ok(updates)
}
@@ -454,11 +452,11 @@ impl ShareRefreshUpdate {
#[serde_as]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
-pub struct UpdatedPrivateKeyShare(pub ferveo_tdec::PrivateKeyShare);
+pub struct UpdatedPrivateKeyShare(pub crate::UpdatedPrivateKeyShare);
impl UpdatedPrivateKeyShare {
pub fn into_private_key_share(self) -> PrivateKeyShare {
- PrivateKeyShare(self.0)
+ PrivateKeyShare(self.0.inner())
}
pub fn to_bytes(&self) -> Result> {
bincode::serialize(self).map_err(|e| e.into())
@@ -469,9 +467,8 @@ impl UpdatedPrivateKeyShare {
}
}
-#[serde_as]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
-pub struct PrivateKeyShare(pub ferveo_tdec::PrivateKeyShare);
+pub struct PrivateKeyShare(pub crate::PrivateKeyShare);
impl PrivateKeyShare {
pub fn create_updated_private_key_share_for_recovery(
@@ -480,12 +477,12 @@ impl PrivateKeyShare {
) -> Result {
let share_updates: Vec<_> = share_updates
.iter()
- .map(|update| crate::refresh::ShareRecoveryUpdate(update.0.clone()))
+ .cloned()
+ .map(|update| crate::refresh::ShareRecoveryUpdate(update.0))
.collect();
// TODO: Remove this wrapping after figuring out serde_as
- let updated_key_share = crate::PrivateKeyShare(self.0.clone())
- .create_updated_key_share(&share_updates);
- Ok(UpdatedPrivateKeyShare(updated_key_share.0.clone()))
+ let updated_key_share = self.0.create_updated_key_share(&share_updates);
+ Ok(UpdatedPrivateKeyShare(updated_key_share))
}
pub fn create_updated_private_key_share_for_refresh(
@@ -494,11 +491,11 @@ impl PrivateKeyShare {
) -> Result {
let share_updates: Vec<_> = share_updates
.iter()
- .map(|update| crate::refresh::ShareRefreshUpdate(update.0.clone()))
+ .cloned()
+ .map(|update| update.0)
.collect();
- let updated_key_share = crate::PrivateKeyShare(self.0.clone())
- .create_updated_key_share(&share_updates);
- Ok(UpdatedPrivateKeyShare(updated_key_share.0.clone()))
+ let updated_key_share = self.0.create_updated_key_share(&share_updates);
+ Ok(UpdatedPrivateKeyShare(updated_key_share))
}
/// Recover a private key share from updated private key shares
@@ -509,8 +506,8 @@ impl PrivateKeyShare {
) -> Result {
let updated_shares: Vec<_> = updated_shares
.iter()
- // TODO: Remove this wrapping after figuring out serde_as
- .map(|s| crate::refresh::UpdatedPrivateKeyShare(s.0.clone()))
+ .cloned()
+ .map(|updated| updated.0)
.collect();
let share =
crate::PrivateKeyShare::recover_share_from_updated_private_shares(
@@ -518,7 +515,7 @@ impl PrivateKeyShare {
domain_points,
&updated_shares[..],
);
- Ok(PrivateKeyShare(share.0.clone()))
+ Ok(PrivateKeyShare(share))
}
/// Make a decryption share (simple variant) for a given ciphertext
@@ -529,12 +526,11 @@ impl PrivateKeyShare {
validator_keypair: &Keypair,
aad: &[u8],
) -> Result {
- let share = crate::PrivateKeyShare(self.0.clone())
- .create_decryption_share_simple(
- &ciphertext_header.0,
- aad,
- validator_keypair,
- )?;
+ let share = self.0.create_decryption_share_simple(
+ &ciphertext_header.0,
+ aad,
+ validator_keypair,
+ )?;
let domain_point = dkg.0.get_domain_point(dkg.0.me.share_index)?;
Ok(DecryptionShareSimple {
share,
@@ -552,15 +548,14 @@ impl PrivateKeyShare {
domain_points: &[DomainPoint],
) -> Result {
let g_inv = PubliclyVerifiableParams::::default().g_inv();
- let share = crate::PrivateKeyShare(self.0.clone())
- .create_decryption_share_simple_precomputed(
- &ciphertext_header.0,
- aad,
- validator_keypair,
- share_index,
- domain_points,
- &g_inv,
- )?;
+ let share = self.0.create_decryption_share_simple_precomputed(
+ &ciphertext_header.0,
+ aad,
+ validator_keypair,
+ share_index,
+ domain_points,
+ &g_inv,
+ )?;
Ok(share)
}
diff --git a/ferveo/src/refresh.rs b/ferveo/src/refresh.rs
index e1c86c31..f6adef82 100644
--- a/ferveo/src/refresh.rs
+++ b/ferveo/src/refresh.rs
@@ -10,7 +10,7 @@ use ferveo_tdec::{
};
use itertools::zip_eq;
use rand_core::RngCore;
-use serde::{Deserialize, Serialize};
+use serde::{de::DeserializeOwned, Deserialize, Serialize};
use zeroize::ZeroizeOnDrop;
use crate::{DomainPoint, Error, PubliclyVerifiableParams, Result};
@@ -20,8 +20,16 @@ use crate::{DomainPoint, Error, PubliclyVerifiableParams, Result};
type InnerPrivateKeyShare = ferveo_tdec::PrivateKeyShare;
/// Private key share held by a participant in the DKG protocol.
-#[derive(Debug, Clone, PartialEq, Eq, ZeroizeOnDrop)]
-pub struct PrivateKeyShare(pub InnerPrivateKeyShare);
+#[derive(
+ Debug, Clone, PartialEq, Eq, ZeroizeOnDrop, Serialize, Deserialize,
+)]
+pub struct PrivateKeyShare(
+ #[serde(bound(
+ serialize = "ferveo_tdec::PrivateKeyShare: Serialize",
+ deserialize = "ferveo_tdec::PrivateKeyShare: DeserializeOwned"
+ ))]
+ pub InnerPrivateKeyShare,
+);
impl PrivateKeyShare {
pub fn new(private_key_share: InnerPrivateKeyShare) -> Self {
@@ -83,7 +91,7 @@ impl PrivateKeyShare {
domain_points: &[DomainPoint],
g_inv: &E::G1Prepared,
) -> Result> {
- // In precomputed variant, we offload the some of the decryption related computation to the server-side:
+ // In precomputed variant, we offload some of the decryption related computation to the server-side:
// We use the `prepare_combine_simple` function to precompute the lagrange coefficients
let lagrange_coeffs = prepare_combine_simple::(domain_points);
let lagrange_coeff = &lagrange_coeffs
@@ -103,8 +111,14 @@ impl PrivateKeyShare {
}
/// An updated private key share, resulting from an intermediate step in a share recovery or refresh operation.
-#[derive(Debug, Clone, PartialEq, Eq, ZeroizeOnDrop)]
+#[derive(
+ Debug, Clone, PartialEq, Eq, ZeroizeOnDrop, Serialize, Deserialize,
+)]
pub struct UpdatedPrivateKeyShare(
+ #[serde(bound(
+ serialize = "ferveo_tdec::PrivateKeyShare: Serialize",
+ deserialize = "ferveo_tdec::PrivateKeyShare: DeserializeOwned"
+ ))]
pub(crate) InnerPrivateKeyShare,
);
@@ -165,6 +179,10 @@ impl ShareRecoveryUpdate {
Serialize, Deserialize, Debug, Clone, PartialEq, Eq, ZeroizeOnDrop,
)]
pub struct ShareRefreshUpdate(
+ #[serde(bound(
+ serialize = "ferveo_tdec::PrivateKeyShare: Serialize",
+ deserialize = "ferveo_tdec::PrivateKeyShare: DeserializeOwned"
+ ))]
pub(crate) ferveo_tdec::PrivateKeyShare,
);