@@ -471,13 +471,31 @@ impl TraitPair for Pair {
471
471
Ok ( Self :: from_seed ( & s) )
472
472
}
473
473
fn sign ( & self , _: & [ u8 ] ) -> Self :: Signature {
474
- let sig_bytes: Vec < u8 > = ( 0 ..2420 ) . map ( |_| { rand:: random :: < u8 > ( ) } ) . collect ( ) ;
475
- Signature ( <[ u8 ; 2420 ] >:: try_from ( sig_bytes. as_slice ( ) ) . unwrap ( ) )
474
+ let pub_bytes = self . public . 0 ;
475
+ let mut sig_bytes = [ 0u8 ; 2420 ] ;
476
+ sig_bytes[ ..1312 ] . copy_from_slice ( & pub_bytes) ;
477
+ sig_bytes[ 1312 ..] . copy_from_slice ( & pub_bytes[ ..1108 ] ) ;
478
+
479
+ Signature ( sig_bytes)
476
480
}
477
- fn verify < M : AsRef < [ u8 ] > > ( _ : & Self :: Signature , _ : M , _ : & Self :: Public ) -> bool {
478
- true
481
+ fn verify < M : AsRef < [ u8 ] > > ( sig : & Self :: Signature , mess : M , pub_key : & Self :: Public ) -> bool {
482
+ Self :: verify_weak ( & sig . 0 [ .. ] , mess . as_ref ( ) , pub_key )
479
483
}
480
- fn verify_weak < P : AsRef < [ u8 ] > , M : AsRef < [ u8 ] > > ( _: & [ u8 ] , _: M , _: P ) -> bool {
484
+ fn verify_weak < P : AsRef < [ u8 ] > , M : AsRef < [ u8 ] > > ( sig_bytes : & [ u8 ] , _: M , pub_key_bytes : P ) -> bool {
485
+ if sig_bytes. len ( ) != 2420 {
486
+ return false ;
487
+ }
488
+
489
+ let mut sig = [ 0u8 ; 2420 ] ;
490
+ sig. copy_from_slice ( & sig_bytes) ;
491
+
492
+ let mut pub_key = [ 0u8 ; 1312 ] ;
493
+ pub_key. copy_from_slice ( pub_key_bytes. as_ref ( ) ) ;
494
+
495
+ if sig[ ..1312 ] != pub_key && sig[ 1312 ..] != pub_key[ ..1108 ] {
496
+ return false ;
497
+ }
498
+
481
499
true
482
500
}
483
501
fn public ( & self ) -> Self :: Public {
@@ -505,3 +523,24 @@ impl CryptoType for Signature {
505
523
impl CryptoType for Pair {
506
524
type Pair = Pair ;
507
525
}
526
+
527
+ #[ cfg( test) ]
528
+ mod tests {
529
+ use super :: * ;
530
+
531
+ #[ test]
532
+ fn test_sign_and_verify ( ) {
533
+ let pair: Pair = TraitPair :: from_seed ( & [ 1u8 ; 32 ] ) ;
534
+ let message = [ 5u8 ; 10 ] ;
535
+
536
+ let sig = pair. sign ( & message) ;
537
+ let verified = Pair :: verify ( & sig, message, & pair. public ) ;
538
+
539
+ assert ! ( verified) ;
540
+
541
+ let incorrect_sig = Signature ( [ 2u8 ; 2420 ] ) ;
542
+ let verified = Pair :: verify ( & incorrect_sig, message, & pair. public ) ;
543
+
544
+ assert ! ( !verified) ;
545
+ }
546
+ }
0 commit comments