lit_node_core/traits/encoding/
curve25519.rs1use super::{BeBytes, CompressedBytes, LeBytes};
2use lit_rust_crypto::{
3 group::GroupEncoding,
4 vsss_rs::{curve25519, curve25519_dalek},
5};
6
7impl CompressedBytes for curve25519_dalek::RistrettoPoint {
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 pt = curve25519_dalek::ristretto::CompressedRistretto::from_slice(bytes).ok()?;
17 pt.decompress()
18 }
19}
20
21impl CompressedBytes for curve25519_dalek::EdwardsPoint {
22 fn to_compressed(&self) -> Vec<u8> {
23 self.to_bytes().to_vec()
24 }
25
26 fn from_compressed(bytes: &[u8]) -> Option<Self> {
27 let pt = curve25519_dalek::edwards::CompressedEdwardsY::from_slice(bytes).ok()?;
28 pt.decompress()
29 }
30}
31
32impl CompressedBytes for ed25519_dalek::VerifyingKey {
33 fn to_compressed(&self) -> Vec<u8> {
34 self.to_bytes().to_vec()
35 }
36
37 fn from_compressed(bytes: &[u8]) -> Option<Self> {
38 let arr = <[u8; 32]>::try_from(bytes).ok()?;
39 Self::from_bytes(&arr).ok()
40 }
41}
42
43impl BeBytes for curve25519_dalek::Scalar {
44 fn to_be_bytes(&self) -> Vec<u8> {
45 let mut out = self.to_bytes().to_vec();
46 out.reverse();
47 out
48 }
49
50 fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
51 let mut arr = [0u8; 32];
52 for (i, byte) in bytes.iter().enumerate() {
53 arr[31 - i] = *byte;
54 }
55 Option::from(Self::from_canonical_bytes(arr))
56 }
57}
58
59impl LeBytes for curve25519_dalek::Scalar {
60 fn to_le_bytes(&self) -> Vec<u8> {
61 self.to_bytes().to_vec()
62 }
63
64 fn from_le_bytes(bytes: &[u8]) -> Option<Self> {
65 let arr = <[u8; 32]>::try_from(bytes).ok()?;
66 Option::from(Self::from_canonical_bytes(arr))
67 }
68}
69
70impl BeBytes for ed25519_dalek::SigningKey {
71 fn to_be_bytes(&self) -> Vec<u8> {
72 let mut out = self.to_bytes().to_vec();
73 out.reverse();
74 out
75 }
76
77 fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
78 if bytes.len() != 32 {
79 return None;
80 }
81 let mut sk = ed25519_dalek::SecretKey::default();
82 for (i, byte) in bytes.iter().enumerate() {
83 sk[31 - i] = *byte;
84 }
85 Option::from(Self::from_bytes(&sk))
86 }
87}
88
89impl LeBytes for ed25519_dalek::SigningKey {
90 fn to_le_bytes(&self) -> Vec<u8> {
91 self.to_bytes().to_vec()
92 }
93
94 fn from_le_bytes(bytes: &[u8]) -> Option<Self> {
95 let arr = ed25519_dalek::SecretKey::try_from(bytes).ok()?;
96 Option::from(Self::from_bytes(&arr))
97 }
98}
99
100impl CompressedBytes for curve25519::WrappedRistretto {
101 fn to_compressed(&self) -> Vec<u8> {
102 self.0.compress().to_bytes().to_vec()
103 }
104
105 fn from_compressed(bytes: &[u8]) -> Option<Self> {
106 let arr = <[u8; 32]>::try_from(bytes).ok()?;
107 Option::from(Self::from_bytes(&arr))
108 }
109}
110
111impl CompressedBytes for curve25519::WrappedEdwards {
112 fn to_compressed(&self) -> Vec<u8> {
113 self.0.compress().to_bytes().to_vec()
114 }
115
116 fn from_compressed(bytes: &[u8]) -> Option<Self> {
117 let arr = <[u8; 32]>::try_from(bytes).ok()?;
118 Option::from(Self::from_bytes(&arr))
119 }
120}
121
122impl BeBytes for curve25519::WrappedScalar {
123 fn to_be_bytes(&self) -> Vec<u8> {
124 let mut out = self.0.to_bytes().to_vec();
125 out.reverse();
126 out
127 }
128
129 fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
130 let mut arr = [0u8; 32];
131 for (i, byte) in bytes.iter().enumerate() {
132 arr[31 - i] = *byte;
133 }
134 let s = curve25519_dalek::Scalar::from_canonical_bytes(arr);
135 Option::from(s.map(Self))
136 }
137}
138
139impl LeBytes for curve25519::WrappedScalar {
140 fn to_le_bytes(&self) -> Vec<u8> {
141 self.0.to_bytes().to_vec()
142 }
143
144 fn from_le_bytes(bytes: &[u8]) -> Option<Self> {
145 let arr = <[u8; 32]>::try_from(bytes).ok()?;
146 let s = curve25519_dalek::Scalar::from_canonical_bytes(arr);
147 Option::from(s.map(Self))
148 }
149}
150
151impl CompressedBytes for curve25519::WrappedScalar {
152 fn to_compressed(&self) -> Vec<u8> {
153 self.0.to_bytes().to_vec()
154 }
155
156 fn from_compressed(bytes: &[u8]) -> Option<Self> {
157 let arr = <[u8; 32]>::try_from(bytes).ok()?;
158 let s = curve25519_dalek::Scalar::from_canonical_bytes(arr);
159 Option::from(s.map(Self))
160 }
161}