Skip to content

Commit

Permalink
add _vartime suffix; add test of to_decimal_string
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <[email protected]>
  • Loading branch information
andrewwhitehead committed Aug 28, 2023
1 parent 081ccf3 commit 3e94bb5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(uint: &T) -> Vec<u8>
pub fn to_decimal_vec_vartime<T>(uint: &T) -> Vec<u8>
where
T: AsRef<[Limb]> + AsMut<[Limb]> + Clone,
{
Expand All @@ -296,17 +296,17 @@ where

/// Obtain the decimal representation of an integer as a String.
#[cfg(feature = "alloc")]
pub fn to_decimal_string<T>(uint: &T) -> String
pub fn to_decimal_string_vartime<T>(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<T>(uint: &T, buf: &mut [u8]) -> usize
pub fn write_decimal_bytes_vartime<T>(uint: &T, buf: &mut [u8]) -> usize
where
T: AsRef<[Limb]> + AsMut<[Limb]> + Clone,
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -483,4 +483,15 @@ mod tests {
let des: AsDecimal<BoxedUint> = 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());
}
}

0 comments on commit 3e94bb5

Please sign in to comment.