Skip to content

Commit a293368

Browse files
authored
der: simplify der_cmp for Length (#1997)
The order of a DER encoded length is the same as the order of the length as an integer so avoid overhead of encoding and comparing the encoded bytes.
1 parent a53335b commit a293368

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

der/src/length.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ impl Length {
4545
#[cfg(feature = "ber")]
4646
pub(crate) const EOC_LEN: Self = Self::new(2);
4747

48-
/// Maximum number of octets in a DER encoding of a [`Length`] using the
49-
/// rules implemented by this crate.
50-
pub(crate) const MAX_SIZE: usize = 5;
51-
5248
/// Create a new [`Length`] for any value which fits inside of a [`u16`].
5349
///
5450
/// This function is const-safe and therefore useful for [`Length`] constants.
@@ -336,13 +332,8 @@ impl Encode for Length {
336332

337333
impl DerOrd for Length {
338334
fn der_cmp(&self, other: &Self) -> Result<Ordering> {
339-
let mut buf1 = [0u8; Self::MAX_SIZE];
340-
let mut buf2 = [0u8; Self::MAX_SIZE];
341-
342-
let buf1 = self.encode_to_slice(&mut buf1)?;
343-
let buf2 = other.encode_to_slice(&mut buf2)?;
344-
345-
Ok(buf1.cmp(buf2))
335+
// The DER encoding has the same ordering as the integer value
336+
Ok(self.inner.cmp(&other.inner))
346337
}
347338
}
348339

0 commit comments

Comments
 (0)