lit_node_core/traits/encoding/
p256.rs

1use super::{BeBytes, CompressedBytes, LeBytes};
2use lit_rust_crypto::{
3    elliptic_curve::sec1::{EncodedPoint, FromEncodedPoint, ToEncodedPoint},
4    ff::PrimeField,
5    p256::{AffinePoint, FieldBytes, NistP256, NonZeroScalar, ProjectivePoint, Scalar, ecdsa},
6};
7
8impl CompressedBytes for ProjectivePoint {
9    fn to_compressed(&self) -> Vec<u8> {
10        self.to_encoded_point(true).to_bytes().to_vec()
11    }
12
13    fn from_compressed(bytes: &[u8]) -> Option<Self> {
14        let pt = EncodedPoint::<NistP256>::from_bytes(bytes).ok()?;
15        Option::from(Self::from_encoded_point(&pt))
16    }
17
18    fn to_uncompressed(&self) -> Vec<u8> {
19        self.to_encoded_point(false).to_bytes().to_vec()
20    }
21
22    fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
23        let pt = EncodedPoint::<NistP256>::from_bytes(bytes).ok()?;
24        Option::from(Self::from_encoded_point(&pt))
25    }
26}
27
28impl CompressedBytes for AffinePoint {
29    fn to_compressed(&self) -> Vec<u8> {
30        self.to_encoded_point(true).to_bytes().to_vec()
31    }
32
33    fn from_compressed(bytes: &[u8]) -> Option<Self> {
34        let pt = EncodedPoint::<NistP256>::from_bytes(bytes).ok()?;
35        Option::from(Self::from_encoded_point(&pt))
36    }
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::<NistP256>::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::<NistP256>::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::<NistP256>::from_bytes(bytes).ok()?;
63        Self::from_encoded_point(&pt).ok()
64    }
65}
66
67impl BeBytes for Scalar {
68    fn to_be_bytes(&self) -> Vec<u8> {
69        self.to_bytes().to_vec()
70    }
71
72    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
73        let mut repr = FieldBytes::default();
74        repr.copy_from_slice(bytes);
75        Option::from(Self::from_repr(repr))
76    }
77}
78
79impl LeBytes for Scalar {}
80
81impl CompressedBytes for Scalar {
82    fn to_compressed(&self) -> Vec<u8> {
83        self.to_bytes().to_vec()
84    }
85
86    fn from_compressed(bytes: &[u8]) -> Option<Self> {
87        let mut repr = FieldBytes::default();
88        repr.copy_from_slice(bytes);
89        Option::from(Self::from_repr(repr))
90    }
91}
92
93impl BeBytes for NonZeroScalar {
94    fn to_be_bytes(&self) -> Vec<u8> {
95        self.to_bytes().to_vec()
96    }
97
98    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
99        let mut repr = FieldBytes::default();
100        repr.copy_from_slice(bytes);
101        Option::from(Self::from_repr(repr))
102    }
103}
104
105impl LeBytes for NonZeroScalar {}
106
107impl BeBytes for ecdsa::SigningKey {
108    fn to_be_bytes(&self) -> Vec<u8> {
109        self.as_nonzero_scalar().to_be_bytes()
110    }
111
112    fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
113        let mut repr = FieldBytes::default();
114        repr.copy_from_slice(bytes);
115        Self::from_bytes(&repr).ok()
116    }
117}
118
119impl LeBytes for ecdsa::SigningKey {}