fedimint_core/
bls12_381_serde.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
pub mod scalar {
    use bls12_381::Scalar;
    use serde::de::Error;
    use serde::{Deserializer, Serializer};

    pub fn serialize<S: Serializer>(scalar: &Scalar, s: S) -> Result<S::Ok, S::Error> {
        serdect::array::serialize_hex_lower_or_bin(&scalar.to_bytes(), s)
    }

    pub fn deserialize<'d, D: Deserializer<'d>>(d: D) -> Result<Scalar, D::Error> {
        let mut byte_array = [0; 32];

        serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?;

        Option::from(Scalar::from_bytes(&byte_array))
            .ok_or_else(|| Error::custom("Could not decode scalar"))
    }
}

pub mod g1 {
    use bls12_381::G1Affine;
    use serde::de::Error;
    use serde::{Deserializer, Serializer};

    pub fn serialize<S: Serializer>(point: &G1Affine, s: S) -> Result<S::Ok, S::Error> {
        serdect::array::serialize_hex_lower_or_bin(&point.to_compressed(), s)
    }

    pub fn deserialize<'d, D: Deserializer<'d>>(d: D) -> Result<G1Affine, D::Error> {
        let mut byte_array = [0; 48];

        serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?;

        Option::from(G1Affine::from_compressed(&byte_array))
            .ok_or_else(|| Error::custom("Could not decode compressed group element"))
    }
}

pub mod g2 {
    use bls12_381::G2Affine;
    use serde::de::Error;
    use serde::{Deserializer, Serializer};

    pub fn serialize<S: Serializer>(point: &G2Affine, s: S) -> Result<S::Ok, S::Error> {
        serdect::array::serialize_hex_lower_or_bin(&point.to_compressed(), s)
    }

    pub fn deserialize<'d, D: Deserializer<'d>>(d: D) -> Result<G2Affine, D::Error> {
        let mut byte_array = [0; 96];

        serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?;

        Option::from(G2Affine::from_compressed(&byte_array))
            .ok_or_else(|| Error::custom("Could not decode compressed group element"))
    }
}