1
1
//! Structs and functionality related to the ECDSA signature algorithm.
2
2
3
- use core:: { fmt, str, ops , ptr} ;
3
+ use core:: { fmt, str, ptr} ;
4
4
5
5
use crate :: { Signing , Verification , Message , PublicKey , Secp256k1 , SecretKey , from_hex, Error , ffi} ;
6
6
use crate :: ffi:: CPtr ;
7
7
8
+ pub mod serialized_signature;
9
+
8
10
#[ cfg( feature = "recovery" ) ]
9
11
mod recovery;
10
12
11
13
#[ cfg( feature = "recovery" ) ]
12
14
#[ cfg_attr( docsrs, doc( cfg( feature = "recovery" ) ) ) ]
13
15
pub use self :: recovery:: { RecoveryId , RecoverableSignature } ;
14
16
17
+ pub use serialized_signature:: SerializedSignature ;
18
+
15
19
#[ cfg( feature = "global-context" ) ]
16
20
use crate :: SECP256K1 ;
17
21
18
22
/// An ECDSA signature
19
23
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
20
24
pub struct Signature ( pub ( crate ) ffi:: Signature ) ;
21
25
22
- /// A DER serialized Signature
23
- #[ derive( Copy , Clone ) ]
24
- pub struct SerializedSignature {
25
- data : [ u8 ; 72 ] ,
26
- len : usize ,
27
- }
28
-
29
26
impl fmt:: Debug for Signature {
30
27
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
31
28
fmt:: Display :: fmt ( self , f)
@@ -39,21 +36,6 @@ impl fmt::Display for Signature {
39
36
}
40
37
}
41
38
42
- impl fmt:: Debug for SerializedSignature {
43
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
44
- fmt:: Display :: fmt ( self , f)
45
- }
46
- }
47
-
48
- impl fmt:: Display for SerializedSignature {
49
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
50
- for v in self . data . iter ( ) . take ( self . len ) {
51
- write ! ( f, "{:02x}" , v) ?;
52
- }
53
- Ok ( ( ) )
54
- }
55
- }
56
-
57
39
impl str:: FromStr for Signature {
58
40
type Err = Error ;
59
41
fn from_str ( s : & str ) -> Result < Signature , Error > {
@@ -65,74 +47,6 @@ impl str::FromStr for Signature {
65
47
}
66
48
}
67
49
68
- impl Default for SerializedSignature {
69
- fn default ( ) -> SerializedSignature {
70
- SerializedSignature {
71
- data : [ 0u8 ; 72 ] ,
72
- len : 0 ,
73
- }
74
- }
75
- }
76
-
77
- impl PartialEq for SerializedSignature {
78
- fn eq ( & self , other : & SerializedSignature ) -> bool {
79
- self . data [ ..self . len ] == other. data [ ..other. len ]
80
- }
81
- }
82
-
83
- impl AsRef < [ u8 ] > for SerializedSignature {
84
- fn as_ref ( & self ) -> & [ u8 ] {
85
- & self . data [ ..self . len ]
86
- }
87
- }
88
-
89
- impl ops:: Deref for SerializedSignature {
90
- type Target = [ u8 ] ;
91
-
92
- fn deref ( & self ) -> & [ u8 ] {
93
- & self . data [ ..self . len ]
94
- }
95
- }
96
-
97
- impl Eq for SerializedSignature { }
98
-
99
- impl SerializedSignature {
100
- /// Get a pointer to the underlying data with the specified capacity.
101
- pub ( crate ) fn get_data_mut_ptr ( & mut self ) -> * mut u8 {
102
- self . data . as_mut_ptr ( )
103
- }
104
-
105
- /// Get the capacity of the underlying data buffer.
106
- pub fn capacity ( & self ) -> usize {
107
- self . data . len ( )
108
- }
109
-
110
- /// Get the len of the used data.
111
- pub fn len ( & self ) -> usize {
112
- self . len
113
- }
114
-
115
- /// Set the length of the object.
116
- pub ( crate ) fn set_len ( & mut self , len : usize ) {
117
- self . len = len;
118
- }
119
-
120
- /// Convert the serialized signature into the Signature struct.
121
- /// (This DER deserializes it)
122
- pub fn to_signature ( & self ) -> Result < Signature , Error > {
123
- Signature :: from_der ( self )
124
- }
125
-
126
- /// Create a SerializedSignature from a Signature.
127
- /// (this DER serializes it)
128
- pub fn from_signature ( sig : & Signature ) -> SerializedSignature {
129
- sig. serialize_der ( )
130
- }
131
-
132
- /// Check if the space is zero.
133
- pub fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
134
- }
135
-
136
50
impl Signature {
137
51
#[ inline]
138
52
/// Converts a DER-encoded byte slice to a signature
@@ -253,6 +167,7 @@ impl Signature {
253
167
self . as_c_ptr ( ) ,
254
168
) ;
255
169
debug_assert ! ( err == 1 ) ;
170
+ assert ! ( len <= serialized_signature:: MAX_LEN , "libsecp256k1 set length to {} but the maximum is {}" , len, serialized_signature:: MAX_LEN ) ;
256
171
ret. set_len ( len) ;
257
172
}
258
173
ret
0 commit comments