lit_node_core/traits/encoding/
redjubjub.rs

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