fedimint_core/encoding/
bls12_381.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
57
58
use bls12_381::{G1Affine, G2Affine, Scalar};

use crate::encoding::{Decodable, DecodeError, Encodable};
use crate::module::registry::ModuleDecoderRegistry;

impl Encodable for Scalar {
    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
        self.to_bytes().consensus_encode(writer)
    }
}

impl Decodable for Scalar {
    fn consensus_decode<D: std::io::Read>(
        d: &mut D,
        modules: &ModuleDecoderRegistry,
    ) -> Result<Self, DecodeError> {
        let byte_array = <[u8; 32]>::consensus_decode(d, modules)?;

        Option::from(Self::from_bytes(&byte_array))
            .ok_or_else(|| DecodeError::from_str("Error decoding Scalar"))
    }
}

impl Encodable for G1Affine {
    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
        self.to_compressed().consensus_encode(writer)
    }
}

impl Decodable for G1Affine {
    fn consensus_decode<D: std::io::Read>(
        d: &mut D,
        modules: &ModuleDecoderRegistry,
    ) -> Result<Self, DecodeError> {
        let byte_array = <[u8; 48]>::consensus_decode(d, modules)?;

        Option::from(Self::from_compressed(&byte_array))
            .ok_or_else(|| DecodeError::from_str("Error decoding G1Affine"))
    }
}

impl Encodable for G2Affine {
    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
        self.to_compressed().consensus_encode(writer)
    }
}

impl Decodable for G2Affine {
    fn consensus_decode<D: std::io::Read>(
        d: &mut D,
        modules: &ModuleDecoderRegistry,
    ) -> Result<Self, DecodeError> {
        let byte_array = <[u8; 96]>::consensus_decode(d, modules)?;

        Option::from(Self::from_compressed(&byte_array))
            .ok_or_else(|| DecodeError::from_str("Error decoding G2Affine"))
    }
}