1pub use sec1::point::{Coordinates, ModulusSize, Tag};
6
7use crate::{Curve, Error, FieldBytesSize, Result, SecretKey, array::Array, ctutils::CtOption};
8
9#[cfg(feature = "arithmetic")]
10use crate::{AffinePoint, CurveArithmetic};
11#[cfg(feature = "alloc")]
12use {crate::point::PointCompression, alloc::boxed::Box};
13
14pub type CompressedPoint<C> = Array<u8, CompressedPointSize<C>>;
16
17pub type CompressedPointSize<C> = <FieldBytesSize<C> as ModulusSize>::CompressedPointSize;
19
20pub type UncompressedPoint<C> = Array<u8, UncompressedPointSize<C>>;
22
23pub type UncompressedPointSize<C> = <FieldBytesSize<C> as ModulusSize>::UncompressedPointSize;
25
26pub type Sec1Point<C> = ::sec1::point::EncodedPoint<FieldBytesSize<C>>;
28
29#[deprecated(since = "0.14.0", note = "use `Sec1Point` instead")]
31pub type EncodedPoint<C> = Sec1Point<C>;
32
33pub trait FromSec1Point<C>
37where
38 Self: Sized,
39 C: Curve,
40 FieldBytesSize<C>: ModulusSize,
41{
42 fn from_sec1_point(point: &Sec1Point<C>) -> CtOption<Self>;
44
45 fn from_sec1_bytes(bytes: &[u8]) -> Result<Self> {
53 let point = Sec1Point::<C>::from_bytes(bytes)?;
54 Self::from_sec1_point(&point).into_option().ok_or(Error)
55 }
56
57 #[deprecated(
59 since = "0.14.0",
60 note = "use `FromSec1Point::from_sec1_point` instead"
61 )]
62 fn from_encoded_point(point: &Sec1Point<C>) -> CtOption<Self> {
63 Self::from_sec1_point(point)
64 }
65}
66
67pub trait ToSec1Point<C>
71where
72 C: Curve,
73 FieldBytesSize<C>: ModulusSize,
74{
75 fn to_sec1_point(&self, compress: bool) -> Sec1Point<C>;
78
79 #[cfg(feature = "alloc")]
82 fn to_sec1_bytes(&self) -> Box<[u8]>
83 where
84 C: PointCompression,
85 {
86 self.to_sec1_point(C::COMPRESS_POINTS).to_bytes()
87 }
88
89 fn to_compressed_point(&self) -> CompressedPoint<C> {
91 let mut ret = CompressedPoint::<C>::default();
92 ret.copy_from_slice(self.to_sec1_point(true).as_bytes());
93 ret
94 }
95
96 fn to_uncompressed_point(&self) -> UncompressedPoint<C> {
98 let mut ret = UncompressedPoint::<C>::default();
99 ret.copy_from_slice(self.to_sec1_point(false).as_bytes());
100 ret
101 }
102
103 #[deprecated(since = "0.14.0", note = "use `ToSec1Point::to_sec1_point` instead")]
105 fn to_encoded_point(&self, compress: bool) -> Sec1Point<C> {
106 self.to_sec1_point(compress)
107 }
108}
109
110#[deprecated(since = "0.14.0", note = "use `FromSec1Point` instead")]
112pub trait FromEncodedPoint<C>: FromSec1Point<C>
113where
114 Self: Sized,
115 C: Curve,
116 FieldBytesSize<C>: ModulusSize,
117{
118}
119
120#[allow(deprecated)]
121impl<P, C> FromEncodedPoint<C> for P
122where
123 Self: FromSec1Point<C> + Sized,
124 C: Curve,
125 FieldBytesSize<C>: ModulusSize,
126{
127}
128
129#[deprecated(since = "0.14.0", note = "use `ToSec1Point` instead")]
131pub trait ToEncodedPoint<C>: ToSec1Point<C>
132where
133 C: Curve,
134 FieldBytesSize<C>: ModulusSize,
135{
136}
137
138#[allow(deprecated)]
139impl<T, C> ToEncodedPoint<C> for T
140where
141 Self: ToSec1Point<C>,
142 C: Curve,
143 FieldBytesSize<C>: ModulusSize,
144{
145}
146
147pub trait ToCompactSec1Point<C>
151where
152 C: Curve,
153 FieldBytesSize<C>: ModulusSize,
154{
155 fn to_compact_encoded_point(&self) -> CtOption<Sec1Point<C>>;
158}
159
160pub trait ValidatePublicKey
166where
167 Self: Curve,
168 FieldBytesSize<Self>: ModulusSize,
169{
170 #[allow(unused_variables)]
175 fn validate_public_key(
176 secret_key: &SecretKey<Self>,
177 public_key: &Sec1Point<Self>,
178 ) -> Result<()> {
179 Ok(())
187 }
188}
189
190#[cfg(feature = "arithmetic")]
191impl<C> ValidatePublicKey for C
192where
193 C: CurveArithmetic,
194 AffinePoint<C>: FromSec1Point<C> + ToSec1Point<C>,
195 FieldBytesSize<C>: ModulusSize,
196{
197 fn validate_public_key(secret_key: &SecretKey<C>, public_key: &Sec1Point<C>) -> Result<()> {
198 let pk = secret_key
199 .public_key()
200 .to_sec1_point(public_key.is_compressed());
201
202 if public_key == &pk {
203 Ok(())
204 } else {
205 Err(Error)
206 }
207 }
208}