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

View file

@ -53,7 +53,7 @@ pub fn key_gen<R: RngCore + CryptoRng, C: Ciphersuite>(
.drain() .drain()
.map(|(i, core)| { .map(|(i, core)| {
assert_eq!( assert_eq!(
&ThresholdCore::<C>::deserialize::<&[u8]>(&mut core.serialize().as_ref()).unwrap(), &ThresholdCore::<C>::read::<&[u8]>(&mut core.serialize().as_ref()).unwrap(),
&core &core
); );
(i, ThresholdKeys::new(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()); 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!(these_keys.params().t(), vectors.threshold);
assert_eq!(usize::from(these_keys.params().n()), shares.len()); assert_eq!(usize::from(these_keys.params().n()), shares.len());
let participant = Participant::new(i).unwrap(); let participant = Participant::new(i).unwrap();