Skip to content

Commit 5d51b9d

Browse files
committed
Added MAX_LEN constant to serialized_signature
This also asserts that libsecp256k1 set the correct length to help the compiler elide bound checks.
1 parent e642a52 commit 5d51b9d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/ecdsa/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl Signature {
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);
170171
ret.set_len(len);
171172
}
172173
ret

src/ecdsa/serialized_signature.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ use core::{fmt, ops};
1111
use crate::Error;
1212
use super::Signature;
1313

14+
pub(crate) const MAX_LEN: usize = 72;
15+
1416
/// A DER serialized Signature
1517
#[derive(Copy, Clone)]
1618
pub struct SerializedSignature {
17-
data: [u8; 72],
19+
data: [u8; MAX_LEN],
1820
len: usize,
1921
}
2022

@@ -37,7 +39,7 @@ impl Default for SerializedSignature {
3739
#[inline]
3840
fn default() -> SerializedSignature {
3941
SerializedSignature {
40-
data: [0u8; 72],
42+
data: [0u8; MAX_LEN],
4143
len: 0,
4244
}
4345
}
@@ -224,18 +226,18 @@ mod into_iter {
224226

225227
#[cfg(test)]
226228
mod tests {
227-
use super::SerializedSignature;
229+
use super::{SerializedSignature, MAX_LEN};
228230

229231
#[test]
230232
fn iterator_ops_are_homomorphic() {
231-
let mut fake_signature_data = [0; 72];
233+
let mut fake_signature_data = [0; MAX_LEN];
232234
// fill it with numbers 0 - 71
233235
for (i, byte) in fake_signature_data.iter_mut().enumerate() {
234-
// up to 72
236+
// up to MAX_LEN
235237
*byte = i as u8;
236238
}
237239

238-
let fake_signature = SerializedSignature { data: fake_signature_data, len: 72 };
240+
let fake_signature = SerializedSignature { data: fake_signature_data, len: MAX_LEN };
239241

240242
let mut iter1 = fake_signature.into_iter();
241243
let mut iter2 = fake_signature.iter();

0 commit comments

Comments
 (0)