jf_utils/
macros.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the Jellyfish library.
3
4// You should have received a copy of the MIT License
5// along with the Jellyfish library. If not, see <https://mit-license.org/>.
6
7//! useful macros.
8
9/// Takes as input a struct, and converts them to a series of bytes. All traits
10/// that implement `CanonicalSerialize` can be automatically converted to bytes
11/// in this manner.
12#[macro_export]
13macro_rules! to_bytes {
14    ($x:expr) => {{
15        let mut buf = ark_std::vec![];
16        ark_serialize::CanonicalSerialize::serialize_compressed($x, &mut buf).map(|_| buf)
17    }};
18}
19
20#[test]
21fn test_to_bytes() {
22    use ark_bls12_381::Fr;
23    use ark_serialize::CanonicalSerialize;
24    use ark_std::One;
25    let f1 = Fr::one();
26
27    let mut bytes = ark_std::vec![];
28    f1.serialize_compressed(&mut bytes).unwrap();
29    assert_eq!(bytes, to_bytes!(&f1).unwrap());
30}