arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/poly-iop/src/utils.rs
poly-iop/src/utils.rsRaw
 1//! useful macros.
 2
 3/// Takes as input a struct, and converts them to a series of bytes. All traits
 4/// that implement `CanonicalSerialize` can be automatically converted to bytes
 5/// in this manner.
 6#[macro_export]
 7macro_rules! to_bytes {
 8    ($x:expr) => {{
 9        let mut buf = ark_std::vec![];
10        ark_serialize::CanonicalSerialize::serialize($x, &mut buf).map(|_| buf)
11    }};
12}
13
14#[cfg(test)]
15pub(crate) fn bit_decompose(input: u64, num_var: usize) -> Vec<bool> {
16    let mut res = Vec::with_capacity(num_var);
17    let mut i = input;
18    for _ in 0..num_var {
19        res.push(i & 1 == 1);
20        i >>= 1;
21    }
22    res
23}
24
25#[test]
26fn test_to_bytes() {
27    use ark_bls12_381::Fr;
28    use ark_serialize::CanonicalSerialize;
29    use ark_std::One;
30    let f1 = Fr::one();
31
32    let mut bytes = ark_std::vec![];
33    f1.serialize(&mut bytes).unwrap();
34    assert_eq!(bytes, to_bytes!(&f1).unwrap());
35}