generic_ec/
encoded.rs

1use core::{fmt, ops};
2
3use crate::{as_raw::AsRaw, core::ByteArray, Curve};
4
5/// Bytes representation of an elliptic point
6pub struct EncodedPoint<E: Curve>(EncodedPointInner<E>);
7
8impl<E: Curve> EncodedPoint<E> {
9    pub(crate) fn new_compressed(bytes: E::CompressedPointArray) -> Self {
10        Self(EncodedPointInner::Compressed(bytes))
11    }
12
13    pub(crate) fn new_uncompressed(bytes: E::UncompressedPointArray) -> Self {
14        Self(EncodedPointInner::Uncompressed(bytes))
15    }
16
17    /// Returns bytes representation of the point
18    pub fn as_bytes(&self) -> &[u8] {
19        match &self.0 {
20            EncodedPointInner::Compressed(bytes) => bytes.as_ref(),
21            EncodedPointInner::Uncompressed(bytes) => bytes.as_ref(),
22        }
23    }
24}
25
26impl<E: Curve> Clone for EncodedPoint<E> {
27    fn clone(&self) -> Self {
28        Self(self.0.clone())
29    }
30}
31
32impl<E: Curve> PartialEq for EncodedPoint<E> {
33    fn eq(&self, other: &Self) -> bool {
34        self.as_bytes() == other.as_bytes()
35    }
36}
37
38impl<E: Curve> Eq for EncodedPoint<E> {}
39
40impl<E: Curve> fmt::Debug for EncodedPoint<E> {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        let mut tuple = f.debug_tuple("EncodedPoint");
43        #[cfg(feature = "alloc")]
44        {
45            tuple.field(&hex::encode(self.as_bytes()));
46        }
47        tuple.finish()
48    }
49}
50
51impl<E: Curve> ops::Deref for EncodedPoint<E> {
52    type Target = [u8];
53    fn deref(&self) -> &[u8] {
54        self.as_bytes()
55    }
56}
57
58#[derive(Clone)]
59enum EncodedPointInner<E: Curve> {
60    Compressed(E::CompressedPointArray),
61    Uncompressed(E::UncompressedPointArray),
62}
63
64impl<E: Curve> AsRef<[u8]> for EncodedPoint<E> {
65    fn as_ref(&self) -> &[u8] {
66        self.as_bytes()
67    }
68}
69
70/// Bytes representation of a scalar (either in big-endian or in little-endian)
71#[derive(Clone)]
72pub struct EncodedScalar<E: Curve>(E::ScalarArray);
73
74impl<E: Curve> EncodedScalar<E> {
75    pub(crate) fn new(bytes: E::ScalarArray) -> Self {
76        Self(bytes)
77    }
78
79    /// Returns bytes representation of a scalar
80    pub fn as_bytes(&self) -> &[u8] {
81        self.0.as_ref()
82    }
83}
84
85impl<E: Curve> AsRef<[u8]> for EncodedScalar<E> {
86    fn as_ref(&self) -> &[u8] {
87        self.as_bytes()
88    }
89}
90
91impl<E: Curve> AsMut<[u8]> for EncodedScalar<E> {
92    fn as_mut(&mut self) -> &mut [u8] {
93        self.0.as_mut()
94    }
95}
96
97impl<E: Curve> ops::Deref for EncodedScalar<E> {
98    type Target = [u8];
99    fn deref(&self) -> &[u8] {
100        self.as_bytes()
101    }
102}
103
104impl<E: Curve> fmt::Debug for EncodedScalar<E> {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        let mut s = f.debug_tuple("EncodedScalar");
107        #[cfg(feature = "std")]
108        {
109            s.field(&hex::encode(self.as_bytes()));
110        }
111        s.finish()
112    }
113}
114
115impl<E: Curve> PartialEq for EncodedScalar<E> {
116    fn eq(&self, other: &Self) -> bool {
117        self.as_bytes() == other.as_bytes()
118    }
119}
120
121impl<E: Curve> Eq for EncodedScalar<E> {}
122
123impl<E: Curve> Default for EncodedScalar<E> {
124    fn default() -> Self {
125        let bytes = E::ScalarArray::zeroes();
126        Self(bytes)
127    }
128}
129
130impl<E: Curve> AsRaw for EncodedScalar<E> {
131    type Raw = E::ScalarArray;
132    fn as_raw(&self) -> &Self::Raw {
133        &self.0
134    }
135}