15
15
16
16
//! # Public and secret keys
17
17
18
- #[ cfg( any( test, feature = "serde" ) ) ] use std:: marker;
19
18
#[ cfg( any( test, feature = "rand" ) ) ] use rand:: Rng ;
20
- #[ cfg( any( test, feature = "rustc-serialize" ) ) ] use serialize:: { Decoder , Decodable , Encoder , Encodable } ;
21
- #[ cfg( any( test, feature = "serde" ) ) ] use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
22
19
23
20
use std:: mem;
24
21
@@ -271,36 +268,6 @@ impl PublicKey {
271
268
}
272
269
}
273
270
274
- #[ cfg( any( test, feature = "rustc-serialize" ) ) ]
275
- impl Decodable for PublicKey {
276
- fn decode < D : Decoder > ( d : & mut D ) -> Result < PublicKey , D :: Error > {
277
- d. read_seq ( |d, len| {
278
- let s = Secp256k1 :: with_caps ( :: ContextFlag :: None ) ;
279
- if len == constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE {
280
- unsafe {
281
- use std:: mem;
282
- let mut ret: [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] = mem:: uninitialized ( ) ;
283
- for i in 0 ..len {
284
- ret[ i] = try!( d. read_seq_elt ( i, |d| Decodable :: decode ( d) ) ) ;
285
- }
286
- PublicKey :: from_slice ( & s, & ret) . map_err ( |_| d. error ( "invalid public key" ) )
287
- }
288
- } else if len == constants:: PUBLIC_KEY_SIZE {
289
- unsafe {
290
- use std:: mem;
291
- let mut ret: [ u8 ; constants:: PUBLIC_KEY_SIZE ] = mem:: uninitialized ( ) ;
292
- for i in 0 ..len {
293
- ret[ i] = try!( d. read_seq_elt ( i, |d| Decodable :: decode ( d) ) ) ;
294
- }
295
- PublicKey :: from_slice ( & s, & ret) . map_err ( |_| d. error ( "invalid public key" ) )
296
- }
297
- } else {
298
- Err ( d. error ( "Invalid length" ) )
299
- }
300
- } )
301
- }
302
- }
303
-
304
271
/// Creates a new public key from a FFI public key
305
272
impl From < ffi:: PublicKey > for PublicKey {
306
273
#[ inline]
@@ -309,95 +276,37 @@ impl From<ffi::PublicKey> for PublicKey {
309
276
}
310
277
}
311
278
312
-
313
- #[ cfg( any( test, feature = "rustc-serialize" ) ) ]
314
- impl Encodable for PublicKey {
315
- fn encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
316
- self . serialize ( ) . encode ( s)
317
- }
318
- }
319
-
320
- #[ cfg( any( test, feature = "serde" ) ) ]
321
- impl < ' de > Deserialize < ' de > for PublicKey {
322
- fn deserialize < D > ( d : D ) -> Result < PublicKey , D :: Error >
323
- where D : Deserializer < ' de >
324
- {
325
- use serde:: de;
326
- struct Visitor {
327
- marker : marker:: PhantomData < PublicKey > ,
328
- }
329
- impl < ' de > de:: Visitor < ' de > for Visitor {
330
- type Value = PublicKey ;
331
-
332
- #[ inline]
333
- fn visit_seq < A > ( self , mut a : A ) -> Result < PublicKey , A :: Error >
334
- where A : de:: SeqAccess < ' de >
335
- {
336
- debug_assert ! ( constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE >= constants:: PUBLIC_KEY_SIZE ) ;
337
-
338
- let s = Secp256k1 :: with_caps ( :: ContextFlag :: None ) ;
339
- unsafe {
340
- use std:: mem;
341
- let mut ret: [ u8 ; constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE ] = mem:: uninitialized ( ) ;
342
-
343
- let mut read_len = 0 ;
344
- while read_len < constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE {
345
- let read_ch = match try!( a. next_element ( ) ) {
346
- Some ( c) => c,
347
- None => break
348
- } ;
349
- ret[ read_len] = read_ch;
350
- read_len += 1 ;
351
- }
352
- let one_after_last : Option < u8 > = try!( a. next_element ( ) ) ;
353
- if one_after_last. is_some ( ) {
354
- return Err ( de:: Error :: invalid_length ( read_len + 1 , & self ) ) ;
355
- }
356
-
357
- match read_len {
358
- constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE | constants:: PUBLIC_KEY_SIZE
359
- => PublicKey :: from_slice ( & s, & ret[ ..read_len] ) . map_err (
360
- |e| match e {
361
- InvalidPublicKey => de:: Error :: invalid_value ( de:: Unexpected :: Seq , & self ) ,
362
- _ => de:: Error :: custom ( & e. to_string ( ) ) ,
363
- }
364
- ) ,
365
- _ => Err ( de:: Error :: invalid_length ( read_len, & self ) ) ,
366
- }
367
- }
368
- }
369
-
370
- fn expecting ( & self , f : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
371
- write ! ( f, "a sequence of {} or {} bytes representing a valid compressed or uncompressed public key" ,
372
- constants:: PUBLIC_KEY_SIZE , constants:: UNCOMPRESSED_PUBLIC_KEY_SIZE )
373
- }
374
- }
375
-
376
- // Begin actual function
377
- d. deserialize_seq ( Visitor { marker : :: std:: marker:: PhantomData } )
378
- }
379
- }
380
-
381
- #[ cfg( any( test, feature = "serde" ) ) ]
382
- impl Serialize for PublicKey {
383
- fn serialize < S > ( & self , s : S ) -> Result < S :: Ok , S :: Error >
384
- where S : Serializer
385
- {
386
- self . serialize ( ) . serialize ( s)
387
- }
388
- }
389
-
390
279
#[ cfg( test) ]
391
280
mod test {
392
281
use super :: super :: { Secp256k1 , ContextFlag } ;
393
282
use super :: super :: Error :: { InvalidPublicKey , InvalidSecretKey , IncapableContext } ;
394
283
use super :: { PublicKey , SecretKey } ;
395
284
use super :: super :: constants;
396
285
397
- use serialize:: hex:: FromHex ;
398
286
use rand:: { Rng , thread_rng} ;
399
287
400
- macro_rules! hex ( ( $hex: expr) => ( FromHex :: from_hex( $hex) . unwrap( ) ) ) ;
288
+ macro_rules! hex {
289
+ ( $hex: expr) => {
290
+ {
291
+ let mut vec = Vec :: new( ) ;
292
+ let mut b = 0 ;
293
+ for ( idx, c) in $hex. as_bytes( ) . iter( ) . enumerate( ) {
294
+ b <<= 4 ;
295
+ match * c {
296
+ b'A' ...b'F' => b |= c - b'A' + 10 ,
297
+ b'a' ...b'f' => b |= c - b'a' + 10 ,
298
+ b'0' ...b'9' => b |= c - b'0' ,
299
+ _ => panic!( "Bad hex" ) ,
300
+ }
301
+ if ( idx & 1 ) == 1 {
302
+ vec. push( b) ;
303
+ b = 0 ;
304
+ }
305
+ }
306
+ vec
307
+ }
308
+ }
309
+ }
401
310
402
311
#[ test]
403
312
fn skey_from_slice ( ) {
@@ -486,134 +395,6 @@ mod test {
486
395
assert_eq ! ( pk. add_exp_assign( & s, & sk) , Err ( IncapableContext ) ) ;
487
396
}
488
397
489
- #[ test]
490
- fn test_bad_deserialize ( ) {
491
- use std:: io:: Cursor ;
492
- use serialize:: { json, Decodable } ;
493
-
494
- let zero31 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" . as_bytes ( ) ;
495
- let json31 = json:: Json :: from_reader ( & mut Cursor :: new ( zero31) ) . unwrap ( ) ;
496
- let zero32 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" . as_bytes ( ) ;
497
- let json32 = json:: Json :: from_reader ( & mut Cursor :: new ( zero32) ) . unwrap ( ) ;
498
- let zero65 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" . as_bytes ( ) ;
499
- let json65 = json:: Json :: from_reader ( & mut Cursor :: new ( zero65) ) . unwrap ( ) ;
500
- let string = "\" my key\" " . as_bytes ( ) ;
501
- let json = json:: Json :: from_reader ( & mut Cursor :: new ( string) ) . unwrap ( ) ;
502
-
503
- // Invalid length
504
- let mut decoder = json:: Decoder :: new ( json31. clone ( ) ) ;
505
- assert ! ( <PublicKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
506
- let mut decoder = json:: Decoder :: new ( json31. clone ( ) ) ;
507
- assert ! ( <SecretKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
508
- let mut decoder = json:: Decoder :: new ( json32. clone ( ) ) ;
509
- assert ! ( <PublicKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
510
- let mut decoder = json:: Decoder :: new ( json32. clone ( ) ) ;
511
- assert ! ( <SecretKey as Decodable >:: decode( & mut decoder) . is_ok( ) ) ;
512
- let mut decoder = json:: Decoder :: new ( json65. clone ( ) ) ;
513
- assert ! ( <PublicKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
514
- let mut decoder = json:: Decoder :: new ( json65. clone ( ) ) ;
515
- assert ! ( <SecretKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
516
-
517
- // Syntax error
518
- let mut decoder = json:: Decoder :: new ( json. clone ( ) ) ;
519
- assert ! ( <PublicKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
520
- let mut decoder = json:: Decoder :: new ( json. clone ( ) ) ;
521
- assert ! ( <SecretKey as Decodable >:: decode( & mut decoder) . is_err( ) ) ;
522
- }
523
-
524
- #[ test]
525
- fn test_serialize ( ) {
526
- use std:: io:: Cursor ;
527
- use serialize:: { json, Decodable , Encodable } ;
528
-
529
- macro_rules! round_trip (
530
- ( $var: ident) => ( {
531
- let start = $var;
532
- let mut encoded = String :: new( ) ;
533
- {
534
- let mut encoder = json:: Encoder :: new( & mut encoded) ;
535
- start. encode( & mut encoder) . unwrap( ) ;
536
- }
537
- let json = json:: Json :: from_reader( & mut Cursor :: new( encoded. as_bytes( ) ) ) . unwrap( ) ;
538
- let mut decoder = json:: Decoder :: new( json) ;
539
- let decoded = Decodable :: decode( & mut decoder) ;
540
- assert_eq!( Ok ( Some ( start) ) , decoded) ;
541
- } )
542
- ) ;
543
-
544
- let s = Secp256k1 :: new ( ) ;
545
- for _ in 0 ..500 {
546
- let ( sk, pk) = s. generate_keypair ( & mut thread_rng ( ) ) . unwrap ( ) ;
547
- round_trip ! ( sk) ;
548
- round_trip ! ( pk) ;
549
- }
550
- }
551
-
552
- #[ test]
553
- fn test_bad_serde_deserialize ( ) {
554
- use serde:: Deserialize ;
555
- use json;
556
-
557
- // Invalid length
558
- let zero31 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" ;
559
- let mut json = json:: de:: Deserializer :: from_str ( zero31) ;
560
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
561
- let mut json = json:: de:: Deserializer :: from_str ( zero31) ;
562
- assert ! ( <SecretKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
563
-
564
- let zero32 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" ;
565
- let mut json = json:: de:: Deserializer :: from_str ( zero32) ;
566
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
567
- let mut json = json:: de:: Deserializer :: from_str ( zero32) ;
568
- assert ! ( <SecretKey as Deserialize >:: deserialize( & mut json) . is_ok( ) ) ;
569
-
570
- let zero33 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" ;
571
- let mut json = json:: de:: Deserializer :: from_str ( zero33) ;
572
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
573
- let mut json = json:: de:: Deserializer :: from_str ( zero33) ;
574
- assert ! ( <SecretKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
575
-
576
- let trailing66 = "[4,149,16,196,140,38,92,239,179,65,59,224,230,183,91,238,240,46,186,252,
577
- 175,102,52,249,98,178,123,72,50,171,196,254,236,1,189,143,242,227,16,87,
578
- 247,183,162,68,237,140,92,205,151,129,166,58,111,96,123,64,180,147,51,12,
579
- 209,89,236,213,206,17]" ;
580
- let mut json = json:: de:: Deserializer :: from_str ( trailing66) ;
581
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
582
-
583
- // The first 65 bytes of trailing66 are valid
584
- let valid65 = "[4,149,16,196,140,38,92,239,179,65,59,224,230,183,91,238,240,46,186,252,
585
- 175,102,52,249,98,178,123,72,50,171,196,254,236,1,189,143,242,227,16,87,
586
- 247,183,162,68,237,140,92,205,151,129,166,58,111,96,123,64,180,147,51,12,
587
- 209,89,236,213,206]" ;
588
- let mut json = json:: de:: Deserializer :: from_str ( valid65) ;
589
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_ok( ) ) ;
590
-
591
- // All zeroes pk is invalid
592
- let zero65 = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]" ;
593
- let mut json = json:: de:: Deserializer :: from_str ( zero65) ;
594
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
595
- let mut json = json:: de:: Deserializer :: from_str ( zero65) ;
596
- assert ! ( <SecretKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
597
-
598
- // Syntax error
599
- let string = "\" my key\" " ;
600
- let mut json = json:: de:: Deserializer :: from_str ( string) ;
601
- assert ! ( <PublicKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
602
- let mut json = json:: de:: Deserializer :: from_str ( string) ;
603
- assert ! ( <SecretKey as Deserialize >:: deserialize( & mut json) . is_err( ) ) ;
604
- }
605
-
606
-
607
- #[ test]
608
- fn test_serialize_serde ( ) {
609
- let s = Secp256k1 :: new ( ) ;
610
- for _ in 0 ..500 {
611
- let ( sk, pk) = s. generate_keypair ( & mut thread_rng ( ) ) . unwrap ( ) ;
612
- round_trip_serde ! ( sk) ;
613
- round_trip_serde ! ( pk) ;
614
- }
615
- }
616
-
617
398
#[ test]
618
399
fn test_out_of_range ( ) {
619
400
0 commit comments