@@ -19,7 +19,7 @@ use core::str::FromStr;
19
19
use {
20
20
crate :: {
21
21
point:: PointCompression ,
22
- sec1:: { EncodedPoint , FromEncodedPoint , ModulusSize , ToEncodedPoint } ,
22
+ sec1:: { CompressedPoint , EncodedPoint , FromEncodedPoint , ModulusSize , ToEncodedPoint } ,
23
23
Curve , FieldBytesSize ,
24
24
} ,
25
25
core:: cmp:: Ordering ,
@@ -143,8 +143,7 @@ where
143
143
AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
144
144
FieldBytesSize < C > : ModulusSize ,
145
145
{
146
- let point = EncodedPoint :: < C > :: from ( self ) ;
147
- point. to_bytes ( )
146
+ EncodedPoint :: < C > :: from ( self ) . to_bytes ( )
148
147
}
149
148
150
149
/// Borrow the inner [`AffinePoint`] from this [`PublicKey`].
@@ -250,6 +249,30 @@ where
250
249
}
251
250
}
252
251
252
+ #[ cfg( feature = "sec1" ) ]
253
+ impl < C > From < PublicKey < C > > for CompressedPoint < C >
254
+ where
255
+ C : CurveArithmetic + PointCompression ,
256
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
257
+ FieldBytesSize < C > : ModulusSize ,
258
+ {
259
+ fn from ( public_key : PublicKey < C > ) -> CompressedPoint < C > {
260
+ CompressedPoint :: < C > :: from ( & public_key)
261
+ }
262
+ }
263
+
264
+ #[ cfg( feature = "sec1" ) ]
265
+ impl < C > From < & PublicKey < C > > for CompressedPoint < C >
266
+ where
267
+ C : CurveArithmetic + PointCompression ,
268
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
269
+ FieldBytesSize < C > : ModulusSize ,
270
+ {
271
+ fn from ( public_key : & PublicKey < C > ) -> CompressedPoint < C > {
272
+ CompressedPoint :: < C > :: clone_from_slice ( public_key. to_encoded_point ( true ) . as_bytes ( ) )
273
+ }
274
+ }
275
+
253
276
#[ cfg( feature = "sec1" ) ]
254
277
impl < C > From < PublicKey < C > > for EncodedPoint < C >
255
278
where
@@ -341,6 +364,62 @@ where
341
364
}
342
365
}
343
366
367
+ #[ cfg( feature = "sec1" ) ]
368
+ impl < C > TryFrom < CompressedPoint < C > > for PublicKey < C >
369
+ where
370
+ C : CurveArithmetic ,
371
+ FieldBytesSize < C > : ModulusSize ,
372
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
373
+ {
374
+ type Error = Error ;
375
+
376
+ fn try_from ( point : CompressedPoint < C > ) -> Result < Self > {
377
+ Self :: from_sec1_bytes ( & point)
378
+ }
379
+ }
380
+
381
+ #[ cfg( feature = "sec1" ) ]
382
+ impl < C > TryFrom < & CompressedPoint < C > > for PublicKey < C >
383
+ where
384
+ C : CurveArithmetic ,
385
+ FieldBytesSize < C > : ModulusSize ,
386
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
387
+ {
388
+ type Error = Error ;
389
+
390
+ fn try_from ( point : & CompressedPoint < C > ) -> Result < Self > {
391
+ Self :: from_sec1_bytes ( point)
392
+ }
393
+ }
394
+
395
+ #[ cfg( feature = "sec1" ) ]
396
+ impl < C > TryFrom < EncodedPoint < C > > for PublicKey < C >
397
+ where
398
+ C : CurveArithmetic ,
399
+ FieldBytesSize < C > : ModulusSize ,
400
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
401
+ {
402
+ type Error = Error ;
403
+
404
+ fn try_from ( point : EncodedPoint < C > ) -> Result < Self > {
405
+ Self :: from_sec1_bytes ( point. as_bytes ( ) )
406
+ }
407
+ }
408
+
409
+ #[ cfg( feature = "sec1" ) ]
410
+ impl < C > TryFrom < & EncodedPoint < C > > for PublicKey < C >
411
+ where
412
+ C : CurveArithmetic ,
413
+ FieldBytesSize < C > : ModulusSize ,
414
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
415
+ {
416
+ type Error = Error ;
417
+
418
+ fn try_from ( point : & EncodedPoint < C > ) -> Result < Self > {
419
+ Self :: from_sec1_bytes ( point. as_bytes ( ) )
420
+ }
421
+ }
422
+
344
423
#[ cfg( all( feature = "pkcs8" , feature = "sec1" ) ) ]
345
424
impl < C > TryFrom < pkcs8:: SubjectPublicKeyInfoRef < ' _ > > for PublicKey < C >
346
425
where
@@ -351,6 +430,20 @@ where
351
430
type Error = pkcs8:: spki:: Error ;
352
431
353
432
fn try_from ( spki : pkcs8:: SubjectPublicKeyInfoRef < ' _ > ) -> pkcs8:: spki:: Result < Self > {
433
+ Self :: try_from ( & spki)
434
+ }
435
+ }
436
+
437
+ #[ cfg( all( feature = "pkcs8" , feature = "sec1" ) ) ]
438
+ impl < C > TryFrom < & pkcs8:: SubjectPublicKeyInfoRef < ' _ > > for PublicKey < C >
439
+ where
440
+ C : AssociatedOid + CurveArithmetic ,
441
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
442
+ FieldBytesSize < C > : ModulusSize ,
443
+ {
444
+ type Error = pkcs8:: spki:: Error ;
445
+
446
+ fn try_from ( spki : & pkcs8:: SubjectPublicKeyInfoRef < ' _ > ) -> pkcs8:: spki:: Result < Self > {
354
447
spki. algorithm . assert_oids ( ALGORITHM_OID , C :: OID ) ?;
355
448
356
449
let public_key_bytes = spki
0 commit comments