Rename dkg serialize/deserialize to write/read

This commit is contained in:
Luke Parker 2023-03-07 03:37:25 -05:00
parent 5b26115f81
commit 5037962d3c
No known key found for this signature in database
3 changed files with 23 additions and 15 deletions

View file

@ -10,7 +10,7 @@ use core::{
fmt::{self, Debug},
ops::Deref,
};
use std::{io::Read, sync::Arc, collections::HashMap};
use std::{io, sync::Arc, collections::HashMap};
use thiserror::Error;
@ -251,21 +251,29 @@ impl<C: Ciphersuite> ThresholdCore<C> {
self.verification_shares.clone()
}
pub fn serialize(&self) -> Vec<u8> {
let mut serialized = vec![];
serialized.extend(u32::try_from(C::ID.len()).unwrap().to_le_bytes());
serialized.extend(C::ID);
serialized.extend(self.params.t.to_le_bytes());
serialized.extend(self.params.n.to_le_bytes());
serialized.extend(self.params.i.to_bytes());
serialized.extend(self.secret_share.to_repr().as_ref());
for l in (1 ..= self.params.n).map(Participant) {
serialized.extend(self.verification_shares[&l].to_bytes().as_ref());
pub fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
writer.write_all(&u32::try_from(C::ID.len()).unwrap().to_le_bytes())?;
writer.write_all(C::ID)?;
writer.write_all(&self.params.t.to_le_bytes())?;
writer.write_all(&self.params.n.to_le_bytes())?;
writer.write_all(&self.params.i.to_bytes())?;
let mut share_bytes = self.secret_share.to_repr();
writer.write_all(share_bytes.as_ref())?;
share_bytes.as_mut().zeroize();
for l in 1 ..= self.params.n {
writer
.write_all(self.verification_shares[&Participant::new(l).unwrap()].to_bytes().as_ref())?;
}
Ok(())
}
pub fn serialize(&self) -> Zeroizing<Vec<u8>> {
let mut serialized = Zeroizing::new(vec![]);
self.write::<Vec<u8>>(serialized.as_mut()).unwrap();
serialized
}
pub fn deserialize<R: Read>(reader: &mut R) -> Result<ThresholdCore<C>, DkgError<()>> {
pub fn read<R: io::Read>(reader: &mut R) -> Result<ThresholdCore<C>, DkgError<()>> {
{
let missing = DkgError::InternalError("ThresholdCore serialization is missing its curve");
let different = DkgError::InternalError("deserializing ThresholdCore for another curve");
@ -413,7 +421,7 @@ impl<C: Ciphersuite> ThresholdKeys<C> {
self.core.verification_shares()
}
pub fn serialize(&self) -> Vec<u8> {
pub fn serialize(&self) -> Zeroizing<Vec<u8>> {
self.core.serialize()
}

View file

@ -53,7 +53,7 @@ pub fn key_gen<R: RngCore + CryptoRng, C: Ciphersuite>(
.drain()
.map(|(i, core)| {
assert_eq!(
&ThresholdCore::<C>::deserialize::<&[u8]>(&mut core.serialize().as_ref()).unwrap(),
&ThresholdCore::<C>::read::<&[u8]>(&mut core.serialize().as_ref()).unwrap(),
&core
);
(i, ThresholdKeys::new(core))

View file

@ -128,7 +128,7 @@ fn vectors_to_multisig_keys<C: Curve>(vectors: &Vectors) -> HashMap<Participant,
serialized.extend(share.to_bytes().as_ref());
}
let these_keys = ThresholdCore::<C>::deserialize::<&[u8]>(&mut serialized.as_ref()).unwrap();
let these_keys = ThresholdCore::<C>::read::<&[u8]>(&mut serialized.as_ref()).unwrap();
assert_eq!(these_keys.params().t(), vectors.threshold);
assert_eq!(usize::from(these_keys.params().n()), shares.len());
let participant = Participant::new(i).unwrap();