lit_node_core/traits/encoding/
k256.rs

1use super::{BeBytes, CompressedBytes, LeBytes};
2use lit_rust_crypto::{
3    elliptic_curve::sec1::{EncodedPoint, FromEncodedPoint, ToEncodedPoint},
4    ff::PrimeField,
5    k256::{
6        AffinePoint, FieldBytes, NonZeroScalar, ProjectivePoint, Scalar, Secp256k1, ecdsa, schnorr,
7    },
8};
9
10impl CompressedBytes for ProjectivePoint {
11    fn to_compressed(&self) -> Vec<u8> {
12        self.to_encoded_point(true).to_bytes().to_vec()
13    }
14
15    fn from_compressed(bytes: &[u8]) -> Option<Self> {
16        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
17        Option::from(Self::from_encoded_point(&pt))
18    }
19    fn to_uncompressed(&self) -> Vec<u8> {
20        self.to_encoded_point(false).to_bytes().to_vec()
21    }
22
23    fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
24        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
25        Option::from(Self::from_encoded_point(&pt))
26    }
27}
28
29impl CompressedBytes for AffinePoint {
30    fn to_compressed(&self) -> Vec<u8> {
31        self.to_encoded_point(true).to_bytes().to_vec()
32    }
33
34    fn from_compressed(bytes: &[u8]) -> Option<Self> {
35        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
36        Option::from(Self::from_encoded_point(&pt))
37    }
38    fn to_uncompressed(&self) -> Vec<u8> {
39        self.to_encoded_point(false).to_bytes().to_vec()
40    }
41
42    fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
43        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
44        Option::from(Self::from_encoded_point(&pt))
45    }
46}
47
48impl CompressedBytes for ecdsa::VerifyingKey {
49    fn to_compressed(&self) -> Vec<u8> {
50        self.to_encoded_point(true).to_bytes().to_vec()
51    }
52
53    fn from_compressed(bytes: &[u8]) -> Option<Self> {
54        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
55        Self::from_encoded_point(&pt).ok()
56    }
57    fn to_uncompressed(&self) -> Vec<u8> {
58        self.to_encoded_point(false).to_bytes().to_vec()
59    }
60
61    fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
62        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
63        Self::from_encoded_point(&pt).ok()
64    }
65}
66
67impl CompressedBytes for schnorr::VerifyingKey {
68    fn to_compressed(&self) -> Vec<u8> {
69        self.as_affine().to_encoded_point(true).to_bytes().to_vec()
70    }
71
72    fn from_compressed(bytes: &[u8]) -> Option<Self> {
73        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
74        Self::from_bytes(pt.compress().as_bytes()).ok()
75    }
76    fn to_uncompressed(&self) -> Vec<u8> {
77        self.as_affine().to_encoded_point(false).to_bytes().to_vec()
78    }
79
80    fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
81        let pt = EncodedPoint::<Secp256k1>::from_bytes(bytes).ok()?;
82        Self::from_bytes(pt.compress().as_bytes()).ok()
83    }
84}
85
86impl BeBytes for Scalar {
87    fn to_be_bytes(&self) -> Vec<u8> {
88        self.to_bytes().to_vec()
89    }
90
91    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
92        let mut repr = FieldBytes::default();
93        repr.copy_from_slice(bytes);
94        Option::from(Self::from_repr(repr))
95    }
96}
97
98impl LeBytes for Scalar {}
99
100impl CompressedBytes for Scalar {
101    fn to_compressed(&self) -> Vec<u8> {
102        self.to_bytes().to_vec()
103    }
104
105    fn from_compressed(bytes: &[u8]) -> Option<Self> {
106        let mut repr = FieldBytes::default();
107        repr.copy_from_slice(bytes);
108        Option::from(Self::from_repr(repr))
109    }
110}
111
112impl BeBytes for NonZeroScalar {
113    fn to_be_bytes(&self) -> Vec<u8> {
114        self.to_bytes().to_vec()
115    }
116
117    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
118        let mut repr = FieldBytes::default();
119        repr.copy_from_slice(bytes);
120        Option::from(Self::from_repr(repr))
121    }
122}
123
124impl LeBytes for NonZeroScalar {}
125
126impl BeBytes for ecdsa::SigningKey {
127    fn to_be_bytes(&self) -> Vec<u8> {
128        self.as_nonzero_scalar().to_be_bytes()
129    }
130
131    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
132        let mut repr = FieldBytes::default();
133        repr.copy_from_slice(bytes);
134        Self::from_bytes(&repr).ok()
135    }
136}
137
138impl LeBytes for ecdsa::SigningKey {}
139
140impl BeBytes for schnorr::SigningKey {
141    fn to_be_bytes(&self) -> Vec<u8> {
142        self.as_nonzero_scalar().to_be_bytes()
143    }
144
145    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
146        Self::from_bytes(bytes).ok()
147    }
148}
149
150impl LeBytes for schnorr::SigningKey {}