lit_node_core/traits/
encoding.rs1mod bls;
2mod curve25519;
3mod decaf377;
4mod ed448;
5mod k256;
6mod p256;
7mod p384;
8mod redjubjub;
9mod redpallas;
10
11pub trait CompressedBytes: Sized {
13 fn to_compressed(&self) -> Vec<u8>;
15
16 fn from_compressed(bytes: &[u8]) -> Option<Self>;
18 fn to_uncompressed(&self) -> Vec<u8> {
20 self.to_compressed()
21 }
22
23 fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
25 Self::from_compressed(bytes)
26 }
27}
28
29pub trait CompressedHex: Sized {
30 fn to_compressed_hex(&self) -> String;
32
33 fn from_compressed_hex(hex: &str) -> Option<Self>;
35
36 fn to_uncompressed_hex(&self) -> String;
38
39 fn from_uncompressed_hex(hex: &str) -> Option<Self>;
41}
42
43pub trait BeBytes: Sized {
44 fn to_be_bytes(&self) -> Vec<u8>;
45
46 fn from_be_bytes(bytes: &[u8]) -> Option<Self>;
47}
48
49pub trait LeBytes: BeBytes {
50 fn to_le_bytes(&self) -> Vec<u8> {
51 let mut out = self.to_be_bytes();
52 out.reverse();
53 out
54 }
55
56 fn from_le_bytes(bytes: &[u8]) -> Option<Self> {
57 let mut bytes = bytes.to_vec();
58 bytes.reverse();
59 Self::from_be_bytes(&bytes)
60 }
61}
62
63#[allow(dead_code)]
64pub trait BeHex: Sized {
65 fn to_be_hex(&self) -> String;
66
67 fn from_be_hex(hex: &str) -> Option<Self>;
68}
69
70#[allow(dead_code)]
71pub trait LeHex: Sized {
72 fn to_le_hex(&self) -> String;
73
74 fn from_le_hex(hex: &str) -> Option<Self>;
75}
76
77impl<B: BeBytes> BeHex for B {
78 fn to_be_hex(&self) -> String {
79 hex::encode(self.to_be_bytes())
80 }
81
82 fn from_be_hex(hex: &str) -> Option<Self> {
83 let bytes = hex::decode(hex).ok()?;
84 Self::from_be_bytes(&bytes)
85 }
86}
87
88impl<B: LeBytes> LeHex for B {
89 fn to_le_hex(&self) -> String {
90 hex::encode(self.to_le_bytes())
91 }
92
93 fn from_le_hex(hex: &str) -> Option<Self> {
94 let bytes = hex::decode(hex).ok()?;
95 Self::from_le_bytes(&bytes)
96 }
97}
98
99impl<P: CompressedBytes> CompressedHex for P {
100 fn to_compressed_hex(&self) -> String {
101 hex::encode(self.to_compressed())
102 }
103
104 fn from_compressed_hex(hex: &str) -> Option<Self> {
105 let bytes = hex::decode(hex).ok()?;
106 Self::from_compressed(&bytes)
107 }
108 fn to_uncompressed_hex(&self) -> String {
109 hex::encode(self.to_uncompressed())
110 }
111
112 fn from_uncompressed_hex(hex: &str) -> Option<Self> {
113 let bytes = hex::decode(hex).ok()?;
114 Self::from_uncompressed(&bytes)
115 }
116}