pub use sec1::point::{Coordinates, ModulusSize, Tag};
use crate::{Curve, Error, FieldBytesSize, Result, SecretKey, array::Array, ctutils::CtOption};
#[cfg(feature = "arithmetic")]
use crate::{AffinePoint, CurveArithmetic};
#[cfg(feature = "alloc")]
use {crate::point::PointCompression, alloc::boxed::Box};
pub type CompressedPoint<C> = Array<u8, CompressedPointSize<C>>;
pub type CompressedPointSize<C> = <FieldBytesSize<C> as ModulusSize>::CompressedPointSize;
pub type UncompressedPoint<C> = Array<u8, UncompressedPointSize<C>>;
pub type UncompressedPointSize<C> = <FieldBytesSize<C> as ModulusSize>::UncompressedPointSize;
pub type Sec1Point<C> = ::sec1::point::EncodedPoint<FieldBytesSize<C>>;
#[deprecated(since = "0.14.0", note = "use `Sec1Point` instead")]
pub type EncodedPoint<C> = Sec1Point<C>;
pub trait FromSec1Point<C>
where
Self: Sized,
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
fn from_sec1_point(point: &Sec1Point<C>) -> CtOption<Self>;
fn from_sec1_bytes(bytes: &[u8]) -> Result<Self> {
let point = Sec1Point::<C>::from_bytes(bytes)?;
Self::from_sec1_point(&point).into_option().ok_or(Error)
}
#[deprecated(
since = "0.14.0",
note = "use `FromSec1Point::from_sec1_point` instead"
)]
fn from_encoded_point(point: &Sec1Point<C>) -> CtOption<Self> {
Self::from_sec1_point(point)
}
}
pub trait ToSec1Point<C>
where
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
fn to_sec1_point(&self, compress: bool) -> Sec1Point<C>;
#[cfg(feature = "alloc")]
fn to_sec1_bytes(&self) -> Box<[u8]>
where
C: PointCompression,
{
self.to_sec1_point(C::COMPRESS_POINTS).to_bytes()
}
fn to_compressed_point(&self) -> CompressedPoint<C> {
let mut ret = CompressedPoint::<C>::default();
ret.copy_from_slice(self.to_sec1_point(true).as_bytes());
ret
}
fn to_uncompressed_point(&self) -> UncompressedPoint<C> {
let mut ret = UncompressedPoint::<C>::default();
ret.copy_from_slice(self.to_sec1_point(false).as_bytes());
ret
}
#[deprecated(since = "0.14.0", note = "use `ToSec1Point::to_sec1_point` instead")]
fn to_encoded_point(&self, compress: bool) -> Sec1Point<C> {
self.to_sec1_point(compress)
}
}
#[deprecated(since = "0.14.0", note = "use `FromSec1Point` instead")]
pub trait FromEncodedPoint<C>: FromSec1Point<C>
where
Self: Sized,
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
}
#[allow(deprecated)]
impl<P, C> FromEncodedPoint<C> for P
where
Self: FromSec1Point<C> + Sized,
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
}
#[deprecated(since = "0.14.0", note = "use `ToSec1Point` instead")]
pub trait ToEncodedPoint<C>: ToSec1Point<C>
where
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
}
#[allow(deprecated)]
impl<T, C> ToEncodedPoint<C> for T
where
Self: ToSec1Point<C>,
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
}
pub trait ToCompactSec1Point<C>
where
C: Curve,
FieldBytesSize<C>: ModulusSize,
{
fn to_compact_encoded_point(&self) -> CtOption<Sec1Point<C>>;
}
pub trait ValidatePublicKey
where
Self: Curve,
FieldBytesSize<Self>: ModulusSize,
{
#[allow(unused_variables)]
fn validate_public_key(
secret_key: &SecretKey<Self>,
public_key: &Sec1Point<Self>,
) -> Result<()> {
Ok(())
}
}
#[cfg(feature = "arithmetic")]
impl<C> ValidatePublicKey for C
where
C: CurveArithmetic,
AffinePoint<C>: FromSec1Point<C> + ToSec1Point<C>,
FieldBytesSize<C>: ModulusSize,
{
fn validate_public_key(secret_key: &SecretKey<C>, public_key: &Sec1Point<C>) -> Result<()> {
let pk = secret_key
.public_key()
.to_sec1_point(public_key.is_compressed());
if public_key == &pk {
Ok(())
} else {
Err(Error)
}
}
}