@@ -429,3 +429,58 @@ pub(crate) fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
429429 }
430430 len <= max_len
431431}
432+
433+ #[ cfg( test) ]
434+ mod tests {
435+ use crate :: ecdsa:: Signature ;
436+ use crate :: Scalar ;
437+
438+ #[ test]
439+ fn test_from_compact_min_r_and_min_s ( ) {
440+ // From libsecp256k1: "The signature must consist of a 32-byte big endian R value, followed
441+ // by a 32-byte big endian S value. If R or S fall outside of [0..order-1], the encoding is
442+ // invalid. R and S with value 0 are allowed in the encoding."
443+ let r = Scalar :: ZERO ;
444+ let s = Scalar :: ZERO ;
445+ let mut bytes: [ u8 ; 64 ] = [ 0 ; 64 ] ;
446+ bytes[ ..32 ] . copy_from_slice ( & r. to_be_bytes ( ) ) ;
447+ bytes[ 32 ..] . copy_from_slice ( & s. to_be_bytes ( ) ) ;
448+
449+ assert ! ( Signature :: from_compact( & bytes) . is_ok( ) )
450+ }
451+
452+ #[ test]
453+ fn test_from_compact_max_r_and_max_s ( ) {
454+ let r = Scalar :: MAX ;
455+ let s = Scalar :: MAX ;
456+ let mut bytes: [ u8 ; 64 ] = [ 0 ; 64 ] ;
457+ bytes[ ..32 ] . copy_from_slice ( & r. to_be_bytes ( ) ) ;
458+ bytes[ 32 ..] . copy_from_slice ( & s. to_be_bytes ( ) ) ;
459+
460+ assert ! ( Signature :: from_compact( & bytes) . is_ok( ) )
461+ }
462+
463+ #[ test]
464+ fn test_from_compact_invalid_r ( ) {
465+ let r = Scalar :: MAX ;
466+ let s = Scalar :: MAX ;
467+ let mut bytes: [ u8 ; 64 ] = [ 0 ; 64 ] ;
468+ bytes[ ..32 ] . copy_from_slice ( & r. to_be_bytes ( ) ) ;
469+ bytes[ 32 ..] . copy_from_slice ( & s. to_be_bytes ( ) ) ;
470+ bytes[ 31 ] += 1 ;
471+
472+ assert ! ( Signature :: from_compact( & bytes) . is_err( ) )
473+ }
474+
475+ #[ test]
476+ fn test_from_compact_invalid_s ( ) {
477+ let r = Scalar :: MAX ;
478+ let s = Scalar :: MAX ;
479+ let mut bytes: [ u8 ; 64 ] = [ 0 ; 64 ] ;
480+ bytes[ ..32 ] . copy_from_slice ( & r. to_be_bytes ( ) ) ;
481+ bytes[ 32 ..] . copy_from_slice ( & s. to_be_bytes ( ) ) ;
482+ bytes[ 63 ] += 1 ;
483+
484+ assert ! ( Signature :: from_compact( & bytes) . is_err( ) )
485+ }
486+ }
0 commit comments