44
55pub use sec1:: point:: { Coordinates , ModulusSize , Tag } ;
66
7- use crate :: { Curve , FieldBytesSize , Result , SecretKey , ctutils:: CtOption } ;
8- use array:: Array ;
7+ use crate :: { Curve , Error , FieldBytesSize , Result , SecretKey , array:: Array , ctutils:: CtOption } ;
98
109#[ cfg( feature = "arithmetic" ) ]
11- use crate :: { AffinePoint , CurveArithmetic , Error } ;
10+ use crate :: { AffinePoint , CurveArithmetic } ;
11+ #[ cfg( feature = "alloc" ) ]
12+ use { crate :: point:: PointCompression , alloc:: boxed:: Box } ;
1213
1314/// Encoded elliptic curve point with point compression.
1415pub type CompressedPoint < C > = Array < u8 , CompressedPointSize < C > > ;
@@ -17,38 +18,69 @@ pub type CompressedPoint<C> = Array<u8, CompressedPointSize<C>>;
1718pub type CompressedPointSize < C > = <FieldBytesSize < C > as ModulusSize >:: CompressedPointSize ;
1819
1920/// Encoded elliptic curve point sized appropriately for a given curve.
20- pub type EncodedPoint < C > = sec1:: point:: EncodedPoint < FieldBytesSize < C > > ;
21+ pub type EncodedPoint < C > = :: sec1:: point:: EncodedPoint < FieldBytesSize < C > > ;
2122
2223/// Encoded elliptic curve point *without* point compression.
2324pub type UncompressedPoint < C > = Array < u8 , UncompressedPointSize < C > > ;
2425
2526/// Size of an uncompressed elliptic curve point.
2627pub type UncompressedPointSize < C > = <FieldBytesSize < C > as ModulusSize >:: UncompressedPointSize ;
2728
28- /// Trait for deserializing a value from a SEC1 encoded curve point.
29- ///
30- /// This is intended for use with the `AffinePoint` type for a given elliptic curve .
29+ /// Decode curve point using the `Octet-String-to-Elliptic-Curve-Point` conversion described in
30+ /// [SEC 1: Elliptic Curve Cryptography (Version 2.0)](https://www.secg.org/sec1-v2.pdf)
31+ /// §2.3.4 (page 11) .
3132pub trait FromEncodedPoint < C >
3233where
3334 Self : Sized ,
3435 C : Curve ,
3536 FieldBytesSize < C > : ModulusSize ,
3637{
37- /// Deserialize the type this trait is impl'd on from an [`EncodedPoint`].
38+ /// Decode curve point from a SEC1 [`EncodedPoint`].
3839 fn from_encoded_point ( point : & EncodedPoint < C > ) -> CtOption < Self > ;
40+
41+ /// Decode curve point from the provided SEC1 encoding (compressed, uncompressed, or
42+ /// identity) using the `Octet-String-to-Elliptic-Curve-Point` conversion.
43+ fn from_sec1_bytes ( bytes : & [ u8 ] ) -> Result < Self > {
44+ let point = EncodedPoint :: < C > :: from_bytes ( bytes) ?;
45+ Self :: from_encoded_point ( & point) . into_option ( ) . ok_or ( Error )
46+ }
3947}
4048
41- /// Trait for serializing a value to a SEC1 encoded curve point.
42- ///
43- /// This is intended for use with the `AffinePoint` type for a given elliptic curve .
49+ /// Encode curve point using the `Elliptic-Curve-Point-to-Octet-String` conversion described in
50+ /// [SEC 1: Elliptic Curve Cryptography (Version 2.0)](https://www.secg.org/sec1-v2.pdf)
51+ /// §2.3.3 (page 10) .
4452pub trait ToEncodedPoint < C >
4553where
4654 C : Curve ,
4755 FieldBytesSize < C > : ModulusSize ,
4856{
49- /// Serialize this value as a SEC1 [`EncodedPoint`], optionally applying
50- /// point compression .
57+ /// Serialize curve point as a SEC1 [`EncodedPoint`], optionally applying point compression
58+ /// according to the `compress` flag .
5159 fn to_encoded_point ( & self , compress : bool ) -> EncodedPoint < C > ;
60+
61+ /// Serialize curve point as a [`CompressedPoint`].
62+ fn to_compressed_point ( & self ) -> CompressedPoint < C > {
63+ let mut ret = CompressedPoint :: < C > :: default ( ) ;
64+ ret. copy_from_slice ( self . to_encoded_point ( true ) . as_bytes ( ) ) ;
65+ ret
66+ }
67+
68+ /// Serialize curve point as a [`CompressedPoint`].
69+ fn to_uncompressed_point ( & self ) -> UncompressedPoint < C > {
70+ let mut ret = UncompressedPoint :: < C > :: default ( ) ;
71+ ret. copy_from_slice ( self . to_encoded_point ( false ) . as_bytes ( ) ) ;
72+ ret
73+ }
74+
75+ /// Encode curve point using the `Elliptic-Curve-Point-to-Octet-String` conversion and the
76+ /// point compression default for this curve as specified by the [`PointCompression`] trait.
77+ #[ cfg( feature = "alloc" ) ]
78+ fn to_sec1_bytes ( & self ) -> Box < [ u8 ] >
79+ where
80+ C : PointCompression ,
81+ {
82+ self . to_encoded_point ( C :: COMPRESS_POINTS ) . to_bytes ( )
83+ }
5284}
5385
5486/// Trait for serializing a value to a SEC1 encoded curve point with compaction.
0 commit comments