lit_node_core/traits/encoding/
ed448.rs

1use super::{BeBytes, CompressedBytes, LeBytes};
2use lit_rust_crypto::{
3    ed448_goldilocks::{EdwardsPoint, Scalar},
4    ff::PrimeField,
5    group::GroupEncoding,
6};
7// NOTE: There is no difference between compressed and uncompressed points for
8// this curve
9
10impl CompressedBytes for EdwardsPoint {
11    fn to_compressed(&self) -> Vec<u8> {
12        self.compress().to_bytes().to_vec()
13    }
14
15    fn from_compressed(bytes: &[u8]) -> Option<Self> {
16        let mut repr = <EdwardsPoint as GroupEncoding>::Repr::default();
17        if bytes.len() != repr.len() {
18            return None;
19        }
20        repr.copy_from_slice(bytes);
21        Option::from(Self::from_bytes(&repr))
22    }
23}
24
25impl LeBytes for Scalar {
26    fn to_le_bytes(&self) -> Vec<u8> {
27        self.to_bytes_rfc_8032().to_vec()
28    }
29
30    fn from_le_bytes(bytes: &[u8]) -> Option<Self> {
31        let mut repr = <Scalar as PrimeField>::Repr::default();
32        repr.copy_from_slice(bytes);
33        Option::from(Self::from_repr(repr))
34    }
35}
36
37impl BeBytes for Scalar {
38    fn to_be_bytes(&self) -> Vec<u8> {
39        let mut r = self.to_bytes_rfc_8032().to_vec();
40        r.reverse();
41        r
42    }
43
44    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
45        let mut repr = <Scalar as PrimeField>::Repr::default();
46        let mut r = bytes.to_vec();
47        r.reverse();
48        repr.copy_from_slice(&r);
49        Option::from(Self::from_repr(repr))
50    }
51}
52
53impl CompressedBytes for Scalar {
54    fn to_compressed(&self) -> Vec<u8> {
55        self.to_bytes_rfc_8032().to_vec()
56    }
57
58    fn from_compressed(bytes: &[u8]) -> Option<Self> {
59        let mut repr = <Scalar as PrimeField>::Repr::default();
60        repr.copy_from_slice(bytes);
61        Option::from(Self::from_repr(repr))
62    }
63}