arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/subroutines/src/poly_iop/utils.rs
subroutines/src/poly_iop/utils.rsRaw
 1// Copyright (c) 2023 Espresso Systems (espressosys.com)
 2// This file is part of the HyperPlonk library.
 3
 4// You should have received a copy of the MIT License
 5// along with the HyperPlonk 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($x, &mut buf).map(|_| buf)
17    }};
18}
19
20#[cfg(test)]
21mod test {
22    use ark_bls12_381::Fr;
23    use ark_serialize::CanonicalSerialize;
24    use ark_std::One;
25
26    #[test]
27    fn test_to_bytes() {
28        let f1 = Fr::one();
29
30        let mut bytes = ark_std::vec![];
31        f1.serialize(&mut bytes).unwrap();
32        assert_eq!(bytes, to_bytes!(&f1).unwrap());
33    }
34}