Skip to content

Commit 7975be5

Browse files
committed
Merge #456: SerializedSignature improvements
0e0fa06 Simplify `Display` impl of `SerializedSignature` (Martin Habovstiak) 5d51b9d Added `MAX_LEN` constant to `serialized_signature` (Martin Habovstiak) e642a52 Add `#[inline]` to methods of `SerializedSignatre` (Martin Habovstiak) e92540b `impl IntoIterator for SerializedSignature` (Martin Habovstiak) 7f2d3d2 Move `SerializedSignature` into its own module (Martin Habovstiak) 901d5ff `impl<'a> IntoIterator for &'a SerializedSignature` (Martin Habovstiak) 1d2a1c3 Deduplicate `self.data[..self.len]` expressions (Martin Habovstiak) Pull request description: This * Deduplicates slicing operations * Implements `IntoIterator` (owned and borrowed) * Reorganizes the code for better clarity * Adds `#[inline]`s * Checks length set by libsep256k1 Closes #367 Closes #368 Individual commits are hopefully easier to review. ACKs for top commit: apoelstra: ACK 0e0fa06 Tree-SHA512: bbc759af767c8b84bfd6720456efc1e86da501aa193641dae3c99847a3c882f7d4aa7e5cbec074fdd9c2595f1f65e5fbb4c80620539a6357927149e5c2fbc734
2 parents 8ecb75c + 0e0fa06 commit 7975be5

File tree

2 files changed

+269
-91
lines changed

2 files changed

+269
-91
lines changed

src/ecdsa/mod.rs

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
//! Structs and functionality related to the ECDSA signature algorithm.
22
3-
use core::{fmt, str, ops, ptr};
3+
use core::{fmt, str, ptr};
44

55
use crate::{Signing, Verification, Message, PublicKey, Secp256k1, SecretKey, from_hex, Error, ffi};
66
use crate::ffi::CPtr;
77

8+
pub mod serialized_signature;
9+
810
#[cfg(feature = "recovery")]
911
mod recovery;
1012

1113
#[cfg(feature = "recovery")]
1214
#[cfg_attr(docsrs, doc(cfg(feature = "recovery")))]
1315
pub use self::recovery::{RecoveryId, RecoverableSignature};
1416

17+
pub use serialized_signature::SerializedSignature;
18+
1519
#[cfg(feature = "global-context")]
1620
use crate::SECP256K1;
1721

1822
/// An ECDSA signature
1923
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
2024
pub struct Signature(pub(crate) ffi::Signature);
2125

22-
/// A DER serialized Signature
23-
#[derive(Copy, Clone)]
24-
pub struct SerializedSignature {
25-
data: [u8; 72],
26-
len: usize,
27-
}
28-
2926
impl fmt::Debug for Signature {
3027
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3128
fmt::Display::fmt(self, f)
@@ -39,21 +36,6 @@ impl fmt::Display for Signature {
3936
}
4037
}
4138

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-
5739
impl str::FromStr for Signature {
5840
type Err = Error;
5941
fn from_str(s: &str) -> Result<Signature, Error> {
@@ -65,74 +47,6 @@ impl str::FromStr for Signature {
6547
}
6648
}
6749

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-
13650
impl Signature {
13751
#[inline]
13852
/// Converts a DER-encoded byte slice to a signature
@@ -253,6 +167,7 @@ impl Signature {
253167
self.as_c_ptr(),
254168
);
255169
debug_assert!(err == 1);
170+
assert!(len <= serialized_signature::MAX_LEN, "libsecp256k1 set length to {} but the maximum is {}", len, serialized_signature::MAX_LEN);
256171
ret.set_len(len);
257172
}
258173
ret

0 commit comments

Comments
 (0)