lit_node_core/traits/encoding/
p384.rs1use super::{BeBytes, CompressedBytes, LeBytes};
2use lit_rust_crypto::{
3 elliptic_curve::sec1::{EncodedPoint, FromEncodedPoint, ToEncodedPoint},
4 ff::PrimeField,
5 p384::{AffinePoint, FieldBytes, NistP384, 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::<NistP384>::from_bytes(bytes).ok()?;
15 Option::from(Self::from_encoded_point(&pt))
16 }
17 fn to_uncompressed(&self) -> Vec<u8> {
18 self.to_encoded_point(false).to_bytes().to_vec()
19 }
20
21 fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
22 let pt = EncodedPoint::<NistP384>::from_bytes(bytes).ok()?;
23 Option::from(Self::from_encoded_point(&pt))
24 }
25}
26
27impl CompressedBytes for AffinePoint {
28 fn to_compressed(&self) -> Vec<u8> {
29 self.to_encoded_point(true).to_bytes().to_vec()
30 }
31
32 fn from_compressed(bytes: &[u8]) -> Option<Self> {
33 let pt = EncodedPoint::<NistP384>::from_bytes(bytes).ok()?;
34 Option::from(Self::from_encoded_point(&pt))
35 }
36 fn to_uncompressed(&self) -> Vec<u8> {
37 self.to_encoded_point(false).to_bytes().to_vec()
38 }
39
40 fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
41 let pt = EncodedPoint::<NistP384>::from_bytes(bytes).ok()?;
42 Option::from(Self::from_encoded_point(&pt))
43 }
44}
45
46impl CompressedBytes for ecdsa::VerifyingKey {
47 fn to_compressed(&self) -> Vec<u8> {
48 self.to_encoded_point(true).to_bytes().to_vec()
49 }
50
51 fn from_compressed(bytes: &[u8]) -> Option<Self> {
52 let pt = EncodedPoint::<NistP384>::from_bytes(bytes).ok()?;
53 Self::from_encoded_point(&pt).ok()
54 }
55 fn to_uncompressed(&self) -> Vec<u8> {
56 self.to_encoded_point(false).to_bytes().to_vec()
57 }
58
59 fn from_uncompressed(bytes: &[u8]) -> Option<Self> {
60 let pt = EncodedPoint::<NistP384>::from_bytes(bytes).ok()?;
61 Self::from_encoded_point(&pt).ok()
62 }
63}
64
65impl BeBytes for Scalar {
66 fn to_be_bytes(&self) -> Vec<u8> {
67 self.to_bytes().to_vec()
68 }
69
70 fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
71 let mut repr = FieldBytes::default();
72 repr.copy_from_slice(bytes);
73 Option::from(Self::from_repr(repr))
74 }
75}
76
77impl LeBytes for Scalar {}
78
79impl CompressedBytes for Scalar {
80 fn to_compressed(&self) -> Vec<u8> {
81 self.to_bytes().to_vec()
82 }
83
84 fn from_compressed(bytes: &[u8]) -> Option<Self> {
85 let mut repr = FieldBytes::default();
86 repr.copy_from_slice(bytes);
87 Option::from(Self::from_repr(repr))
88 }
89}
90
91impl BeBytes for NonZeroScalar {
92 fn to_be_bytes(&self) -> Vec<u8> {
93 self.to_bytes().to_vec()
94 }
95
96 fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
97 let mut repr = FieldBytes::default();
98 repr.copy_from_slice(bytes);
99 Option::from(Self::from_repr(repr))
100 }
101}
102
103impl LeBytes for NonZeroScalar {}
104
105impl BeBytes for ecdsa::SigningKey {
106 fn to_be_bytes(&self) -> Vec<u8> {
107 self.as_nonzero_scalar().to_be_bytes()
108 }
109
110 fn from_be_bytes(bytes: &[u8]) -> Option<Self> {
111 let mut repr = FieldBytes::default();
112 repr.copy_from_slice(bytes);
113 Self::from_bytes(&repr).ok()
114 }
115}
116
117impl LeBytes for ecdsa::SigningKey {}