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 serialize_to_vec! convenience macro #693

Merged
merged 2 commits into from
Oct 27, 2023
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 @@ -3,6 +3,7 @@
## Pending

- [\#689](https://github.com/arkworks-rs/algebra/pull/689) (`ark-serialize`) Add `CanonicalSerialize` and `CanonicalDeserialize` impls for `VecDeque` and `LinkedList`.
- [\#693](https://github.com/arkworks-rs/algebra/pull/693) (`ark-serialize`) Add `serialize_to_vec!` convenience macro.

### Breaking changes

Expand Down
1 change: 1 addition & 0 deletions serialize/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ impl_tuple!(A:0,);
impl_tuple!(A:0, B:1,);
impl_tuple!(A:0, B:1, C:2,);
impl_tuple!(A:0, B:1, C:2, D:3,);
impl_tuple!(A:0, B:1, C:2, D:3, E:4,);

impl<K, V> CanonicalSerialize for BTreeMap<K, V>
where
Expand Down
20 changes: 20 additions & 0 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ pub use ark_serialize_derive::*;

use digest::{generic_array::GenericArray, Digest, OutputSizeUser};

/// Serializes the given `CanonicalSerialize` items in sequence. `serialize_to_vec![a, b, c, d, e]`
/// is identical to the value of `buf` after `(a, b, c, d, e).serialize_compressed(&mut buf)`.
#[macro_export]
macro_rules! serialize_to_vec {
($($x:expr),*) => ({
let mut buf = ::ark_std::vec![];
{$crate::serialize_to_vec!(@inner buf, $($x),*)}.map(|_| buf)
});

(@inner $buf:expr, $y:expr, $($x:expr),*) => ({
{
$crate::CanonicalSerialize::serialize_uncompressed(&$y, &mut $buf)
}.and({$crate::serialize_to_vec!(@inner $buf, $($x),*)})
});

(@inner $buf:expr, $x:expr) => ({
$crate::CanonicalSerialize::serialize_uncompressed(&$x, &mut $buf)
});
}

/// Whether to use a compressed version of the serialization algorithm. Specific behavior depends
/// on implementation. If no compressed version exists (e.g. on `Fp`), mode is ignored.
#[derive(Copy, Clone, PartialEq, Eq)]
Expand Down
27 changes: 27 additions & 0 deletions serialize/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,30 @@ fn test_biguint() {
.unwrap();
assert_eq!(bytes, expected);
}

#[test]
fn test_serialize_macro() {
// Make a bunch of distinctly typed values
let val1 = [vec![1u8, 2u8, 3u8], vec![4u8, 5u8, 6u8]]
.into_iter()
.collect::<BTreeSet<_>>();
let val2: core::marker::PhantomData<Dummy> = core::marker::PhantomData;
let val3 = Some(10u64);
let val4 = 192830918usize;
let val5 = [1u64, 2, 3, 4, 5].into_iter().collect::<LinkedList<_>>();

// Make sure the serialization macro matches just serializing them as a tuple
let mut tuple_bytes = Vec::new();
(
val1.clone(),
val2.clone(),
val3.clone(),
val4.clone(),
val5.clone(),
)
.serialize_uncompressed(&mut tuple_bytes)
.unwrap();
let macro_bytes = serialize_to_vec![val1, val2, val3, val4, val5].unwrap();

assert_eq!(tuple_bytes, macro_bytes);
}
Loading