Fix From<BigInt> for Integer
Sign was not properly preserved when the number of bytes in the BigInt
was less than the number bytes in isize.
@@ -72,10 +72,13 @@ impl From<BigInt> for Integer {
let small_max_big_int: BigInt = small::MAX.into();
if (small_min_big_int <= big_int) & (big_int <= small_max_big_int) {
- let small_isize = big_int
- .to_signed_bytes_be()
- .iter()
- .fold(0_isize, |acc, byte| (acc << 8) | (*byte as isize));
+ let (sign, bytes) = big_int.to_bytes_be();
+ let small_usize = bytes.iter().fold(0_usize, |acc, byte| (acc << 8) | (*byte as usize));
+
+ let small_isize = match sign {
+ Sign::Minus => -1 * (small_usize as isize),
+ _ => small_usize as isize
+ };
Integer::Small(small::Integer(small_isize))
} else {
