Skip to content

Commit 7f2d3d2

Browse files
committed
Move SerializedSignature into its own module
This de-clutters the code and prepares for the next step of adding `IntoIterator`. The type is still re-exported so the change is neither breaking nor inconvenient. This also adds more datialed explanation of `SerializedSignature` and why it's needed.
1 parent 901d5ff commit 7f2d3d2

File tree

2 files changed

+114
-100
lines changed

2 files changed

+114
-100
lines changed

src/ecdsa/mod.rs

Lines changed: 5 additions & 100 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,83 +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 == **other
80-
}
81-
}
82-
83-
impl AsRef<[u8]> for SerializedSignature {
84-
fn as_ref(&self) -> &[u8] {
85-
&*self
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<'a> IntoIterator for &'a SerializedSignature {
100-
type IntoIter = core::slice::Iter<'a, u8>;
101-
type Item = &'a u8;
102-
103-
fn into_iter(self) -> Self::IntoIter {
104-
self.iter()
105-
}
106-
}
107-
108-
impl SerializedSignature {
109-
/// Get a pointer to the underlying data with the specified capacity.
110-
pub(crate) fn get_data_mut_ptr(&mut self) -> *mut u8 {
111-
self.data.as_mut_ptr()
112-
}
113-
114-
/// Get the capacity of the underlying data buffer.
115-
pub fn capacity(&self) -> usize {
116-
self.data.len()
117-
}
118-
119-
/// Get the len of the used data.
120-
pub fn len(&self) -> usize {
121-
self.len
122-
}
123-
124-
/// Set the length of the object.
125-
pub(crate) fn set_len(&mut self, len: usize) {
126-
self.len = len;
127-
}
128-
129-
/// Convert the serialized signature into the Signature struct.
130-
/// (This DER deserializes it)
131-
pub fn to_signature(&self) -> Result<Signature, Error> {
132-
Signature::from_der(self)
133-
}
134-
135-
/// Create a SerializedSignature from a Signature.
136-
/// (this DER serializes it)
137-
pub fn from_signature(sig: &Signature) -> SerializedSignature {
138-
sig.serialize_der()
139-
}
140-
141-
/// Check if the space is zero.
142-
pub fn is_empty(&self) -> bool { self.len() == 0 }
143-
}
144-
14550
impl Signature {
14651
#[inline]
14752
/// Converts a DER-encoded byte slice to a signature

src/ecdsa/serialized_signature.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//! Implements [`SerializedSignature`] and related types.
2+
//!
3+
//! DER-serialized signatures have the issue that they can have different lengths.
4+
//! We want to avoid using `Vec` since that would require allocations making the code slower and
5+
//! unable to run on platforms without allocator. We implement a special type to encapsulate
6+
//! serialized signatures and since it's a bit more complicated it has its own module.
7+
8+
use core::{fmt, ops};
9+
use crate::Error;
10+
use super::Signature;
11+
12+
/// A DER serialized Signature
13+
#[derive(Copy, Clone)]
14+
pub struct SerializedSignature {
15+
data: [u8; 72],
16+
len: usize,
17+
}
18+
19+
impl fmt::Debug for SerializedSignature {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21+
fmt::Display::fmt(self, f)
22+
}
23+
}
24+
25+
impl fmt::Display for SerializedSignature {
26+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27+
for v in self.data.iter().take(self.len) {
28+
write!(f, "{:02x}", v)?;
29+
}
30+
Ok(())
31+
}
32+
}
33+
34+
impl Default for SerializedSignature {
35+
fn default() -> SerializedSignature {
36+
SerializedSignature {
37+
data: [0u8; 72],
38+
len: 0,
39+
}
40+
}
41+
}
42+
43+
impl PartialEq for SerializedSignature {
44+
fn eq(&self, other: &SerializedSignature) -> bool {
45+
**self == **other
46+
}
47+
}
48+
49+
impl AsRef<[u8]> for SerializedSignature {
50+
fn as_ref(&self) -> &[u8] {
51+
&*self
52+
}
53+
}
54+
55+
impl ops::Deref for SerializedSignature {
56+
type Target = [u8];
57+
58+
fn deref(&self) -> &[u8] {
59+
&self.data[..self.len]
60+
}
61+
}
62+
63+
impl Eq for SerializedSignature {}
64+
65+
impl<'a> IntoIterator for &'a SerializedSignature {
66+
type IntoIter = core::slice::Iter<'a, u8>;
67+
type Item = &'a u8;
68+
69+
fn into_iter(self) -> Self::IntoIter {
70+
self.iter()
71+
}
72+
}
73+
74+
impl SerializedSignature {
75+
/// Get a pointer to the underlying data with the specified capacity.
76+
pub(crate) fn get_data_mut_ptr(&mut self) -> *mut u8 {
77+
self.data.as_mut_ptr()
78+
}
79+
80+
/// Get the capacity of the underlying data buffer.
81+
pub fn capacity(&self) -> usize {
82+
self.data.len()
83+
}
84+
85+
/// Get the len of the used data.
86+
pub fn len(&self) -> usize {
87+
self.len
88+
}
89+
90+
/// Set the length of the object.
91+
pub(crate) fn set_len(&mut self, len: usize) {
92+
self.len = len;
93+
}
94+
95+
/// Convert the serialized signature into the Signature struct.
96+
/// (This DER deserializes it)
97+
pub fn to_signature(&self) -> Result<Signature, Error> {
98+
Signature::from_der(self)
99+
}
100+
101+
/// Create a SerializedSignature from a Signature.
102+
/// (this DER serializes it)
103+
pub fn from_signature(sig: &Signature) -> SerializedSignature {
104+
sig.serialize_der()
105+
}
106+
107+
/// Check if the space is zero.
108+
pub fn is_empty(&self) -> bool { self.len() == 0 }
109+
}

0 commit comments

Comments
 (0)