Skip to content
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 serialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rust-version = "1.56"
ark-serialize-derive = { version = "^0.3.0", path = "../serialize-derive", optional = true }
ark-std = { version = "^0.3.0", default-features = false }
digest = { version = "0.10", default-features = false }
num-bigint = { version = "0.4", default-features = false }

[dev-dependencies]
sha2 = { version = "0.10", default-features = false}
Expand Down
45 changes: 45 additions & 0 deletions serialize/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ark_std::{
string::String,
vec::Vec,
};
use num_bigint::BigUint;

use crate::*;

Expand Down Expand Up @@ -147,6 +148,50 @@ impl CanonicalDeserialize for usize {
}
}

impl CanonicalSerialize for BigUint {
#[inline]
fn serialize_with_mode<W: Write>(
&self,
writer: W,
compress: Compress,
) -> Result<(), SerializationError> {
self.to_bytes_le().serialize_with_mode(writer, compress)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check that this serializes the length (maybe putting a regression test?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me add one. Ideally this translates to [T].

}

#[inline]
fn serialized_size(&self, compress: Compress) -> usize {
self.to_bytes_le().serialized_size(compress)
}
}

impl CanonicalDeserialize for BigUint {
#[inline]
fn deserialize_with_mode<R: Read>(
reader: R,
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
Ok(BigUint::from_bytes_le(&Vec::<u8>::deserialize_with_mode(
reader, compress, validate,
)?))
}
}

impl Valid for BigUint {
#[inline]
fn check(&self) -> Result<(), SerializationError> {
Ok(())
}

#[inline]
fn batch_check<'a>(_batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
Ok(())
}
}

impl<T: CanonicalSerialize> CanonicalSerialize for Option<T> {
#[inline]
fn serialize_with_mode<W: Write>(
Expand Down
22 changes: 22 additions & 0 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ mod test {
vec,
vec::Vec,
};
use num_bigint::BigUint;

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
struct Dummy;
Expand Down Expand Up @@ -461,4 +462,25 @@ mod test {
test_hash::<_, sha3::Sha3_256>(Dummy);
test_hash::<_, sha3::Sha3_512>(Dummy);
}

#[test]
fn test_biguint() {
let biguint = BigUint::from(123456u64);
test_serialize(biguint.clone());

let mut expected = (biguint.to_bytes_le().len() as u64).to_le_bytes().to_vec();
expected.extend_from_slice(&biguint.to_bytes_le());

let mut bytes = Vec::new();
biguint
.serialize_with_mode(&mut bytes, Compress::Yes)
.unwrap();
assert_eq!(bytes, expected);

let mut bytes = Vec::new();
biguint
.serialize_with_mode(&mut bytes, Compress::No)
.unwrap();
assert_eq!(bytes, expected);
}
}