Skip to content

Commit 568f16a

Browse files
committed
Merge #458: Removed Default from SerializedSignature
b18f5d0 Removed `Default` from `SerializedSignature` (Martin Habovstiak) Pull request description: `Default` was pointless, so it was replaced with internal `from_raw_parts` method which also checks the length. This commit also documents changes to `SerializedSignature`. Closes #454 ACKs for top commit: tcharding: utACK b18f5d0 apoelstra: ACK b18f5d0 Tree-SHA512: 5ee32160721d4d22cfe7c5dcc433bf013fc78a350e86b3d8d42c207fec7f2bf11c47fce77269ae816567be77602fdc86231d86e2c62aa2d327540056ab445842
2 parents 7975be5 + b18f5d0 commit 568f16a

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The major change in this version is the increase of the Minimum Supported Rust V
99
* Key tweaking methods renamed and refactored to use a more [functional-style](https://github.com/rust-bitcoin/rust-secp256k1/pull/406), they now accept a [new Scalar](https://github.com/rust-bitcoin/rust-secp256k1/pull/445) type instead of raw slices
1010
* Update [`rand` dependency to 0.8](https://github.com/rust-bitcoin/rust-secp256k1/pull/331)
1111
* `KeyPair::from_secret_key` [borrows SecretKey](https://github.com/rust-bitcoin/rust-secp256k1/pull/430) instead of taking ownership
12+
* `SerializedSignature` no longer implements `Default`
1213

1314
## New features/APIs
1415

@@ -18,6 +19,7 @@ The major change in this version is the increase of the Minimum Supported Rust V
1819
* [Implemented `TryFrom` for `Parity`](https://github.com/rust-bitcoin/rust-secp256k1/pull/409)
1920
* The [alloc feature](https://github.com/rust-bitcoin/rust-secp256k1/pull/331) can be used on targets with allocators without a standard library
2021
* `SharedSecret` can be created from a slice, parsed from a hex string, or [(de)serialized using serde](https://github.com/rust-bitcoin/rust-secp256k1/pull/418)
22+
* `SerializedSignature` implements `IntoIterator` (both owned and shared reference)
2123
* We now [derive `std::hash::Hash` for `Signature`](https://github.com/rust-bitcoin/rust-secp256k1/pull/441)
2224

2325
## Other improvements

src/ecdsa/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,18 @@ impl Signature {
157157
#[inline]
158158
/// Serializes the signature in DER format
159159
pub fn serialize_der(&self) -> SerializedSignature {
160-
let mut ret = SerializedSignature::default();
161-
let mut len: usize = ret.capacity();
160+
let mut data = [0u8; serialized_signature::MAX_LEN];
161+
let mut len: usize = serialized_signature::MAX_LEN;
162162
unsafe {
163163
let err = ffi::secp256k1_ecdsa_signature_serialize_der(
164164
ffi::secp256k1_context_no_precomp,
165-
ret.get_data_mut_ptr(),
165+
data.as_mut_ptr(),
166166
&mut len,
167167
self.as_c_ptr(),
168168
);
169169
debug_assert!(err == 1);
170-
assert!(len <= serialized_signature::MAX_LEN, "libsecp256k1 set length to {} but the maximum is {}", len, serialized_signature::MAX_LEN);
171-
ret.set_len(len);
170+
SerializedSignature::from_raw_parts(data, len)
172171
}
173-
ret
174172
}
175173

176174
#[inline]

src/ecdsa/serialized_signature.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ impl fmt::Display for SerializedSignature {
3535
}
3636
}
3737

38-
impl Default for SerializedSignature {
39-
#[inline]
40-
fn default() -> SerializedSignature {
41-
SerializedSignature {
42-
data: [0u8; MAX_LEN],
43-
len: 0,
44-
}
45-
}
46-
}
47-
4838
impl PartialEq for SerializedSignature {
4939
#[inline]
5040
fn eq(&self, other: &SerializedSignature) -> bool {
@@ -91,10 +81,18 @@ impl<'a> IntoIterator for &'a SerializedSignature {
9181
}
9282

9383
impl SerializedSignature {
94-
/// Get a pointer to the underlying data with the specified capacity.
84+
/// Creates `SerializedSignature` from data and length.
85+
///
86+
/// ## Panics
87+
///
88+
/// If `len` > `MAX_LEN`
9589
#[inline]
96-
pub(crate) fn get_data_mut_ptr(&mut self) -> *mut u8 {
97-
self.data.as_mut_ptr()
90+
pub(crate) fn from_raw_parts(data: [u8; MAX_LEN], len: usize) -> Self {
91+
assert!(len <= MAX_LEN, "attempt to set length to {} but the maximum is {}", len, MAX_LEN);
92+
SerializedSignature {
93+
data,
94+
len,
95+
}
9896
}
9997

10098
/// Get the capacity of the underlying data buffer.
@@ -111,7 +109,7 @@ impl SerializedSignature {
111109

112110
/// Set the length of the object.
113111
#[inline]
114-
pub(crate) fn set_len(&mut self, len: usize) {
112+
pub(crate) fn set_len_unchecked(&mut self, len: usize) {
115113
self.len = len;
116114
}
117115

@@ -218,7 +216,7 @@ mod into_iter {
218216
// reach this
219217
let new_len = self.signature.len() - 1;
220218
let byte = self.signature[new_len];
221-
self.signature.set_len(new_len);
219+
self.signature.set_len_unchecked(new_len);
222220
Some(byte)
223221
}
224222
}

0 commit comments

Comments
 (0)