Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization for Rc #153

Merged
merged 2 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`.

### Bug fixes
- #36 (ark-ec) In Short-Weierstrass curves, include an infinity bit in `ToConstraintField`.
Expand Down
55 changes: 53 additions & 2 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ark_std::{
borrow::{Cow, ToOwned},
collections::{BTreeMap, BTreeSet},
convert::TryFrom,
rc::Rc,
string::String,
vec::Vec,
};
Expand Down Expand Up @@ -580,6 +581,51 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for Option<T> {
}
}

// Implement Serialization for `Rc<T>`
impl<T: CanonicalSerialize> CanonicalSerialize for Rc<T> {
#[inline]
fn serialize<W: Write>(&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<W: Write>(&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<W: Write>(&self, mut writer: W) -> Result<(), SerializationError> {
self.as_ref().serialize_unchecked(&mut writer)
}
}

impl<T: CanonicalDeserialize> CanonicalDeserialize for Rc<T> {
#[inline]
fn deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Rc::new(T::deserialize(&mut reader)?))
}

#[inline]
fn deserialize_uncompressed<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Rc::new(T::deserialize_uncompressed(&mut reader)?))
}

#[inline]
fn deserialize_unchecked<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Rc::new(T::deserialize_unchecked(&mut reader)?))
}
}

// Serialize boolean with a full byte
impl CanonicalSerialize for bool {
#[inline]
Expand Down Expand Up @@ -800,7 +846,7 @@ mod test {
&self,
mut writer: W,
) -> Result<(), SerializationError> {
(&[100u8, 200u8]).serialize(&mut writer)
(&[100u8, 200u8]).serialize_uncompressed(&mut writer)
}

#[inline]
Expand All @@ -810,7 +856,7 @@ mod test {

#[inline]
fn serialize_unchecked<W: Write>(&self, mut writer: W) -> Result<(), SerializationError> {
(&[100u8, 200u8]).serialize(&mut writer)
(&[100u8, 200u8]).serialize_unchecked(&mut writer)
}
}

Expand Down Expand Up @@ -944,6 +990,11 @@ mod test {
test_serialize(None::<u64>);
}

#[test]
fn test_rc() {
test_serialize(Rc::new(Dummy));
}

#[test]
fn test_bool() {
test_serialize(true);
Expand Down