Skip to content

Commit 38df762

Browse files
authored
impl to_be_bytes_trimmed and to_le_bytes_trimmed for BoxedUint (#824)
This helpers strips the leading zeroes from the serialization. See: - RustCrypto/RSA#519 - RustCrypto/RSA#518
1 parent 7b65b74 commit 38df762

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/uint/boxed/encoding.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ impl BoxedUint {
9090
out.into()
9191
}
9292

93+
/// Serialize this [`BoxedUint`] as big-endian without leading zeroes.
94+
#[inline]
95+
pub fn to_be_bytes_trimmed_vartime(&self) -> Box<[u8]> {
96+
let zeroes = self.leading_zeros() as usize / 8;
97+
98+
(&self.to_be_bytes()[zeroes..]).into()
99+
}
100+
93101
/// Serialize this [`BoxedUint`] as little-endian.
94102
#[inline]
95103
pub fn to_le_bytes(&self) -> Box<[u8]> {
@@ -107,6 +115,16 @@ impl BoxedUint {
107115
out.into()
108116
}
109117

118+
/// Serialize this [`BoxedUint`] as little-endian without trailing zeroes.
119+
#[inline]
120+
pub fn to_le_bytes_trimmed_vartime(&self) -> Box<[u8]> {
121+
let zeroes = self.leading_zeros() as usize / 8;
122+
123+
let bytes = self.to_le_bytes();
124+
125+
(&bytes[..bytes.len() - zeroes]).into()
126+
}
127+
110128
/// Create a new [`BoxedUint`] from the provided big endian hex string.
111129
pub fn from_be_hex(hex: &str, bits_precision: u32) -> CtOption<Self> {
112130
let nlimbs = (bits_precision / Limb::BITS) as usize;
@@ -425,13 +443,64 @@ mod tests {
425443
assert_eq!(bytes.as_slice(), &*n.to_be_bytes());
426444
}
427445

446+
#[test]
447+
fn to_be_bytes_trimmed_vartime() {
448+
let bytes = hex!("ff112233445566778899aabbccddeeff");
449+
let n = BoxedUint::from_be_slice(&bytes, 128).unwrap();
450+
assert_eq!(&bytes, &*n.to_be_bytes_trimmed_vartime());
451+
452+
let bytes = hex!("00112233445566778899aabbccddeeff");
453+
let n = BoxedUint::from_be_slice(&bytes, 128).unwrap();
454+
assert_eq!(&bytes.as_slice()[1..], &*n.to_be_bytes_trimmed_vartime());
455+
456+
let bytes: &[u8] = b"";
457+
let n = BoxedUint::from_be_slice(bytes, 128).unwrap();
458+
assert_eq!(
459+
hex!("00000000000000000000000000000000"),
460+
n.to_be_bytes().as_ref()
461+
);
462+
assert_eq!(bytes, n.to_be_bytes_trimmed_vartime().as_ref());
463+
464+
let bytes = hex!("00012233445566778899aabbccddeeff");
465+
let n = BoxedUint::from_be_slice(&bytes, 128).unwrap();
466+
assert_eq!(&bytes.as_slice()[1..], &*n.to_be_bytes_trimmed_vartime());
467+
468+
let bytes = hex!("00000000000000000000000000000001");
469+
let n = BoxedUint::from_be_slice(&bytes, 128).unwrap();
470+
assert_eq!(bytes, n.to_be_bytes().as_ref());
471+
assert_eq!(&bytes.as_slice()[15..], &*n.to_be_bytes_trimmed_vartime());
472+
}
473+
428474
#[test]
429475
fn to_le_bytes() {
430476
let bytes = hex!("ffeeddccbbaa99887766554433221100");
431477
let n = BoxedUint::from_le_slice(&bytes, 128).unwrap();
432478
assert_eq!(bytes.as_slice(), &*n.to_le_bytes());
433479
}
434480

481+
#[test]
482+
fn to_le_bytes_trimmed_vartime() {
483+
let bytes = hex!("ffeeddccbbaa998877665544332211ff");
484+
let n = BoxedUint::from_le_slice(&bytes, 128).unwrap();
485+
assert_eq!(bytes.as_slice(), &*n.to_le_bytes_trimmed_vartime());
486+
487+
let bytes = hex!("ffeeddccbbaa99887766554433221100");
488+
let n = BoxedUint::from_le_slice(&bytes, 128).unwrap();
489+
assert_eq!(&bytes.as_slice()[..15], &*n.to_le_bytes_trimmed_vartime());
490+
491+
let bytes = hex!("ff000000000000000000000000000000");
492+
let n = BoxedUint::from_le_slice(&bytes, 128).unwrap();
493+
assert_eq!(&bytes.as_slice()[..1], &*n.to_le_bytes_trimmed_vartime());
494+
495+
let bytes = hex!("01000000000000000000000000000000");
496+
let n = BoxedUint::from_le_slice(&bytes, 128).unwrap();
497+
assert_eq!(&bytes.as_slice()[..1], &*n.to_le_bytes_trimmed_vartime());
498+
499+
let bytes = hex!("00000000000000000000000000000000");
500+
let n = BoxedUint::from_le_slice(&bytes, 128).unwrap();
501+
assert_eq!(b"", &*n.to_le_bytes_trimmed_vartime());
502+
}
503+
435504
#[test]
436505
fn from_str_radix_invalid() {
437506
assert_eq!(

0 commit comments

Comments
 (0)