Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Reduce number of reallocations (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare authored Oct 10, 2023
1 parent 7553d9a commit c8285c5
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/dkg/complaint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<C: CipherSuite> Complaint<C> {

/// Serialize this [`Complaint`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -146,7 +146,7 @@ pub struct ComplaintProof<C: CipherSuite> {
impl<C: CipherSuite> ComplaintProof<C> {
/// Serialize this [`ComplaintProof`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down
23 changes: 13 additions & 10 deletions src/dkg/key_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ pub struct DKGParticipantList<C: CipherSuite> {
impl<C: CipherSuite> DistributedKeyGeneration<RoundOne, C> {
/// Serialize this [`DistributedKeyGeneration<RoundOne, _>`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes =
Vec::with_capacity(self.state.compressed_size() + self.data.compressed_size());

self.state
.serialize_compressed(&mut bytes)
Expand Down Expand Up @@ -741,7 +742,7 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundOne, C> {
SecretShare::<C>::evaluate_polynomial(my_index, &p.index, my_coefficients.unwrap());

let dh_key = p.dh_public_key.key * dh_private_key.0;
let mut dh_key_bytes = Vec::new();
let mut dh_key_bytes = Vec::with_capacity(dh_key.compressed_size());
dh_key
.serialize_compressed(&mut dh_key_bytes)
.map_err(|_| Error::CompressionError)?;
Expand Down Expand Up @@ -820,7 +821,7 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundOne, C> {
for pk in self.state.their_dh_public_keys.iter() {
if pk.0 == encrypted_share.sender_index {
let dh_shared_key = *pk.1 * self.state.dh_private_key.0;
let mut dh_key_bytes = Vec::new();
let mut dh_key_bytes = Vec::with_capacity(dh_shared_key.compressed_size());
dh_shared_key
.serialize_compressed(&mut dh_key_bytes)
.map_err(|_| Error::CompressionError)?;
Expand Down Expand Up @@ -879,7 +880,8 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundOne, C> {
impl<C: CipherSuite> DistributedKeyGeneration<RoundTwo, C> {
/// Serialize this [`DistributedKeyGeneration<RoundTwo, _>`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes =
Vec::with_capacity(self.state.compressed_size() + self.data.compressed_size());

self.state
.serialize_compressed(&mut bytes)
Expand Down Expand Up @@ -929,7 +931,7 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundTwo, C> {
Error::Custom("Could not retrieve participant's secret shares".to_string())
})?;

let mut index_vector: Vec<u32> = Vec::new();
let mut index_vector = Vec::with_capacity(my_secret_shares.len());

for share in my_secret_shares.iter() {
index_vector.push(share.sender_index);
Expand Down Expand Up @@ -961,7 +963,8 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundTwo, C> {
/// my_commitment is needed for now, but won't be when the distinction
/// dealers/signers is implemented.
pub(crate) fn calculate_group_key(&self) -> FrostResult<C, GroupVerifyingKey<C>> {
let mut index_vector: Vec<u32> = Vec::new();
let mut index_vector =
Vec::with_capacity(self.state.their_commitments.as_ref().unwrap().len());

for commitment in self.state.their_commitments.as_ref().unwrap().iter() {
index_vector.push(commitment.index);
Expand Down Expand Up @@ -1025,7 +1028,7 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundTwo, C> {
return complaint.maker_index;
}

let mut dh_key_bytes = Vec::new();
let mut dh_key_bytes = Vec::with_capacity(complaint.dh_shared_key.compressed_size());
if complaint
.dh_shared_key
.serialize_compressed(&mut dh_key_bytes)
Expand Down Expand Up @@ -1078,7 +1081,7 @@ mod test {

#[test]
fn secret_share_from_one_coefficients() {
let mut coeffs: Vec<Fr> = Vec::new();
let mut coeffs: Vec<Fr> = Vec::with_capacity(5);

for _ in 0..5 {
coeffs.push(Fr::ONE);
Expand All @@ -1105,7 +1108,7 @@ mod test {

#[test]
fn secret_share_participant_index_zero() {
let mut coeffs: Vec<Fr> = Vec::new();
let mut coeffs: Vec<Fr> = Vec::with_capacity(5);

for _ in 0..5 {
coeffs.push(Fr::ONE);
Expand Down Expand Up @@ -2148,7 +2151,7 @@ mod test {
// Wrong encrypted share
{
let dh_key = p1.dh_public_key.key * dh_sk1.0;
let mut dh_key_bytes = Vec::new();
let mut dh_key_bytes = Vec::with_capacity(dh_key.compressed_size());
dh_key.serialize_compressed(&mut dh_key_bytes).unwrap();
let wrong_encrypted_secret_share = encrypt_share(
&SecretShare::<Secp256k1Sha256> {
Expand Down
4 changes: 2 additions & 2 deletions src/dkg/nizkpok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct NizkPokOfSecretKey<C: CipherSuite> {
impl<C: CipherSuite> NizkPokOfSecretKey<C> {
/// Serialize this [`NizkPokOfSecretKey`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -121,7 +121,7 @@ mod test {
s: Fr::rand(&mut rng),
r: Fr::rand(&mut rng),
};
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(nizk.compressed_size());
nizk.serialize_compressed(&mut bytes).unwrap();
assert_eq!(
nizk,
Expand Down
2 changes: 1 addition & 1 deletion src/dkg/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl<C: CipherSuite> Participant<C> {

/// Serialize this [`Participant`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down
14 changes: 7 additions & 7 deletions src/dkg/secret_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Coefficients<C: CipherSuite>(pub(crate) Vec<Scalar<C>>);
impl<C: CipherSuite> Coefficients<C> {
/// Serialize this `coefficients` to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<C: CipherSuite> Drop for SecretShare<C> {
impl<C: CipherSuite> SecretShare<C> {
/// Serialize this [`SecretShare`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<C: CipherSuite> EncryptedSecretShare<C> {

/// Serialize this [`EncryptedSecretShare`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand All @@ -205,7 +205,7 @@ pub struct VerifiableSecretSharingCommitment<C: CipherSuite> {
impl<C: CipherSuite> VerifiableSecretSharingCommitment<C> {
/// Serialize this [`VerifiableSecretSharingCommitment`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -263,7 +263,7 @@ pub(crate) fn encrypt_share<C: CipherSuite>(
let cipher = Aes128::new(final_aes_key);
let mut cipher = Aes128Ctr::from_block_cipher(cipher, nonce);

let mut share_bytes = Vec::new();
let mut share_bytes = Vec::with_capacity(share.polynomial_evaluation.compressed_size());
share
.polynomial_evaluation
.serialize_compressed(&mut share_bytes)
Expand Down Expand Up @@ -326,7 +326,7 @@ mod test {
receiver_index: rng.next_u32(),
polynomial_evaluation: Fr::rand(&mut rng),
};
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(secret_share.compressed_size());
secret_share.serialize_compressed(&mut bytes).unwrap();
assert_eq!(
secret_share,
Expand All @@ -345,7 +345,7 @@ mod test {
nonce,
encrypted_polynomial_evaluation,
);
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(encrypted_secret_share.compressed_size());
encrypted_secret_share
.serialize_compressed(&mut bytes)
.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct DiffieHellmanPrivateKey<C: CipherSuite>(pub(crate) <C::G as Group>::S
impl<C: CipherSuite> DiffieHellmanPrivateKey<C> {
/// Serialize this [`DiffieHellmanPrivateKey`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl<C: CipherSuite> DiffieHellmanPublicKey<C> {

/// Serialize this [`DiffieHellmanPublicKey`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -99,7 +99,7 @@ pub struct IndividualVerifyingKey<C: CipherSuite> {
impl<C: CipherSuite> IndividualVerifyingKey<C> {
/// Serialize this [`IndividualVerifyingKey`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<C: CipherSuite> IndividualVerifyingKey<C> {
let mut rhs: C::G = <C as CipherSuite>::G::zero();
let term: <C::G as Group>::ScalarField = self.index.into();

let mut index_vector: Vec<u32> = Vec::new();
let mut index_vector = Vec::with_capacity(commitments.len());
for commitment in commitments.iter() {
index_vector.push(commitment.index);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<C: CipherSuite> IndividualVerifyingKey<C> {
let mut share: C::G = <C as CipherSuite>::G::zero();
let term: <C::G as Group>::ScalarField = participant_index.into();

let mut index_vector: Vec<u32> = Vec::new();
let mut index_vector = Vec::with_capacity(commitments.len());
for commitment in commitments.iter() {
index_vector.push(commitment.index);
}
Expand Down Expand Up @@ -233,7 +233,7 @@ pub struct IndividualSigningKey<C: CipherSuite> {
impl<C: CipherSuite> IndividualSigningKey<C> {
/// Serialize this [`IndividualSigningKey`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<C: CipherSuite> GroupVerifyingKey<C> {

/// Serialize this [`GroupVerifyingKey`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down
2 changes: 1 addition & 1 deletion src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<C: CipherSuite> ThresholdParameters<C> {

/// Serialize this [`ThresholdParameters`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down
10 changes: 5 additions & 5 deletions src/sign/precomputation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub struct CommitmentShare<C: CipherSuite> {
impl<C: CipherSuite> CommitmentShare<C> {
/// Serialize this [`CommitmentShare`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -160,7 +160,7 @@ pub struct SecretCommitmentShareList<C: CipherSuite> {
impl<C: CipherSuite> SecretCommitmentShareList<C> {
/// Serialize this [`SecretCommitmentShareList`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -190,7 +190,7 @@ pub struct PublicCommitmentShareList<C: CipherSuite> {
impl<C: CipherSuite> PublicCommitmentShareList<C> {
/// Serialize this [`PublicCommitmentShareList`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -305,7 +305,7 @@ mod test {
let secret = Fr::rand(&mut rng);
let commit = Projective::generator().mul(secret);
let commitment = Commitment::<Secp256k1Sha256> { secret, commit };
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(commitment.compressed_size());

commitment.serialize_compressed(&mut bytes).unwrap();
assert_eq!(
Expand All @@ -320,7 +320,7 @@ mod test {
let binding = Commitment::<Secp256k1Sha256> { secret, commit };
let hiding = binding.clone();
let commitment_share = CommitmentShare { binding, hiding };
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(commitment_share.compressed_size());

commitment_share.serialize_compressed(&mut bytes).unwrap();
assert_eq!(
Expand Down
12 changes: 7 additions & 5 deletions src/sign/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct PartialThresholdSignature<C: CipherSuite> {
impl<C: CipherSuite> PartialThresholdSignature<C> {
/// Serialize this [`PartialThresholdSignature`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand All @@ -88,7 +88,7 @@ pub struct ThresholdSignature<C: CipherSuite> {
impl<C: CipherSuite> ThresholdSignature<C> {
/// Serialize this [`ThresholdSignature`] to a vector of bytes.
pub fn to_bytes(&self) -> FrostResult<C, Vec<u8>> {
let mut bytes = Vec::new();
let mut bytes = Vec::with_capacity(self.compressed_size());

self.serialize_compressed(&mut bytes)
.map_err(|_| Error::SerializationError)?;
Expand Down Expand Up @@ -293,7 +293,9 @@ pub(crate) fn compute_challenge<C: CipherSuite>(
group_key: &GroupVerifyingKey<C>,
message_hash: &[u8],
) -> FrostResult<C, Scalar<C>> {
let mut challenge_input = Vec::new();
let mut challenge_input = Vec::with_capacity(
group_commitment.compressed_size() + group_key.compressed_size() + message_hash.len(),
);
group_commitment
.serialize_compressed(&mut challenge_input)
.map_err(|_| Error::CompressionError)?;
Expand Down Expand Up @@ -531,7 +533,7 @@ impl<C: CipherSuite> SignatureAggregator<C, Initial<'_>> {
/// A sorted [`Vec`] of unique [`Signer`]s who have yet to contribute their
/// partial signatures.
pub fn get_remaining_signers(&self) -> Vec<Signer<C>> {
let mut remaining_signers: Vec<Signer<C>> = Vec::new();
let mut remaining_signers = Vec::with_capacity(self.state.signers.len());

for signer in self.state.signers.iter() {
if self
Expand Down Expand Up @@ -869,7 +871,7 @@ mod test {
let mut signers_states_1 = Vec::<Dkg<_>>::new();
let mut signers_states_2 = Vec::<Dkg<_>>::new();

let mut dealers = Vec::new();
let mut dealers = Vec::with_capacity(n1 as usize);
let mut dealers_encrypted_secret_shares_for_signers: Vec<
Vec<EncryptedSecretShare<Secp256k1Sha256>>,
> = (0..n1).map(|_| Vec::new()).collect();
Expand Down

0 comments on commit c8285c5

Please sign in to comment.