diff --git a/CHANGELOG.md b/CHANGELOG.md index f4dde225d..eb8f9284e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ The main features of this release are: - #141 (ark-ff) Add `Fp64` - #144 (ark-poly) Add serialization for polynomials and evaluations - #149 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `String`. +- #153 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `Rc`. ### Bug fixes - #36 (ark-ec) In Short-Weierstrass curves, include an infinity bit in `ToConstraintField`. diff --git a/serialize/src/lib.rs b/serialize/src/lib.rs index 6a815ee2f..65c992229 100644 --- a/serialize/src/lib.rs +++ b/serialize/src/lib.rs @@ -9,6 +9,7 @@ use ark_std::{ borrow::{Cow, ToOwned}, collections::{BTreeMap, BTreeSet}, convert::TryFrom, + rc::Rc, string::String, vec::Vec, }; @@ -580,6 +581,51 @@ impl CanonicalDeserialize for Option { } } +// Implement Serialization for `Rc` +impl CanonicalSerialize for Rc { + #[inline] + fn serialize(&self, mut writer: W) -> Result<(), SerializationError> { + self.as_ref().serialize(&mut writer) + } + + #[inline] + fn serialized_size(&self) -> usize { + self.as_ref().serialized_size() + } + + #[inline] + fn serialize_uncompressed(&self, mut writer: W) -> Result<(), SerializationError> { + self.as_ref().serialize_uncompressed(&mut writer) + } + + #[inline] + fn uncompressed_size(&self) -> usize { + self.as_ref().uncompressed_size() + } + + #[inline] + fn serialize_unchecked(&self, mut writer: W) -> Result<(), SerializationError> { + self.as_ref().serialize_unchecked(&mut writer) + } +} + +impl CanonicalDeserialize for Rc { + #[inline] + fn deserialize(mut reader: R) -> Result { + Ok(Rc::new(T::deserialize(&mut reader)?)) + } + + #[inline] + fn deserialize_uncompressed(mut reader: R) -> Result { + Ok(Rc::new(T::deserialize_uncompressed(&mut reader)?)) + } + + #[inline] + fn deserialize_unchecked(mut reader: R) -> Result { + Ok(Rc::new(T::deserialize_unchecked(&mut reader)?)) + } +} + // Serialize boolean with a full byte impl CanonicalSerialize for bool { #[inline] @@ -800,7 +846,7 @@ mod test { &self, mut writer: W, ) -> Result<(), SerializationError> { - (&[100u8, 200u8]).serialize(&mut writer) + (&[100u8, 200u8]).serialize_uncompressed(&mut writer) } #[inline] @@ -810,7 +856,7 @@ mod test { #[inline] fn serialize_unchecked(&self, mut writer: W) -> Result<(), SerializationError> { - (&[100u8, 200u8]).serialize(&mut writer) + (&[100u8, 200u8]).serialize_unchecked(&mut writer) } } @@ -944,6 +990,11 @@ mod test { test_serialize(None::); } + #[test] + fn test_rc() { + test_serialize(Rc::new(Dummy)); + } + #[test] fn test_bool() { test_serialize(true);