From 3e94bb560350da9507b905e539cf36b3d48dcf2f Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Mon, 28 Aug 2023 15:08:36 -0700 Subject: [PATCH] add _vartime suffix; add test of to_decimal_string Signed-off-by: Andrew Whitehead --- src/decimal.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/decimal.rs b/src/decimal.rs index 33f343c1..a329099c 100644 --- a/src/decimal.rs +++ b/src/decimal.rs @@ -279,7 +279,7 @@ fn _encode_decimal_limbs(uint: &mut [Limb], buf: &mut [u8]) { /// Obtain the decimal representation of an integer as a vector /// of ASCII bytes. #[cfg(feature = "alloc")] -pub fn to_decimal_vec(uint: &T) -> Vec +pub fn to_decimal_vec_vartime(uint: &T) -> Vec where T: AsRef<[Limb]> + AsMut<[Limb]> + Clone, { @@ -296,17 +296,17 @@ where /// Obtain the decimal representation of an integer as a String. #[cfg(feature = "alloc")] -pub fn to_decimal_string(uint: &T) -> String +pub fn to_decimal_string_vartime(uint: &T) -> String where T: AsRef<[Limb]> + AsMut<[Limb]> + Clone, { - String::from_utf8(to_decimal_vec(uint)).expect("Error converting to utf-8") + String::from_utf8(to_decimal_vec_vartime(uint)).expect("Error converting to utf-8") } /// Write the decimal representation of `uint` to a provided buffer, /// returning the length of the output. If the output is too large for the /// buffer, then zero is returned. -pub fn write_decimal_bytes(uint: &T, buf: &mut [u8]) -> usize +pub fn write_decimal_bytes_vartime(uint: &T, buf: &mut [u8]) -> usize where T: AsRef<[Limb]> + AsMut<[Limb]> + Clone, { @@ -431,7 +431,7 @@ mod tests { T: AsRef<[Limb]> + AsMut<[Limb]> + Clone + Debug + FromDecimal + PartialEq, { let mut buf = [0u8; 2560]; - let len = write_decimal_bytes(uint, &mut buf); + let len = write_decimal_bytes_vartime(uint, &mut buf); let des = T::from_decimal_bytes_vartime(&buf[..len]).expect("Error deserializing"); assert_eq!(&des, uint); } @@ -483,4 +483,15 @@ mod tests { let des: AsDecimal = bincode::deserialize(&enc).unwrap(); assert_eq!(des.0, uint); } + + #[cfg(feature = "alloc")] + #[test] + fn to_decimal() { + let input = "123456789012345678901234567890"; + let uint = crate::U128::from_be_hex("000000018ee90ff6c373e0ee4e3f0ad2"); + let num_string = to_decimal_string_vartime(&uint); + assert_eq!(num_string.as_str(), input); + let num_vec = to_decimal_vec_vartime(&uint); + assert_eq!(num_vec.as_slice(), input.as_bytes()); + } }