Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Run rustfmt
  • Loading branch information
leoyvens committed Dec 14, 2018
commit 443cc8ea1f986a1503adfe75e55527b41d9adc4f
32 changes: 19 additions & 13 deletions src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,10 @@ impl ShlAssign<usize> for BigInt {
// Negative values need a rounding adjustment if there are any ones in the
// bits that are getting shifted out.
fn shr_round_down(i: &BigInt, rhs: usize) -> bool {
i.is_negative() && biguint::trailing_zeros(&i.data)
.map(|n| n < rhs)
.unwrap_or(false)
i.is_negative()
&& biguint::trailing_zeros(&i.data)
.map(|n| n < rhs)
.unwrap_or(false)
}

impl Shr<usize> for BigInt {
Expand Down Expand Up @@ -1126,15 +1127,13 @@ macro_rules! bigint_sub {
(_, NoSign) => $a_owned,
(NoSign, _) => -$b_owned,
// opposite signs => keep the sign of the left with the sum of magnitudes
(Plus, Minus) | (Minus, Plus) =>
BigInt::from_biguint($a.sign, $a_data + $b_data),
(Plus, Minus) | (Minus, Plus) => BigInt::from_biguint($a.sign, $a_data + $b_data),
// same sign => keep or toggle the sign of the left with the difference of magnitudes
(Plus, Plus) | (Minus, Minus) =>
match $a.data.cmp(&$b.data) {
Less => BigInt::from_biguint(-$a.sign, $b_data - $a_data),
Greater => BigInt::from_biguint($a.sign, $a_data - $b_data),
Equal => Zero::zero(),
},
(Plus, Plus) | (Minus, Minus) => match $a.data.cmp(&$b.data) {
Less => BigInt::from_biguint(-$a.sign, $b_data - $a_data),
Greater => BigInt::from_biguint($a.sign, $a_data - $b_data),
Equal => Zero::zero(),
},
}
};
}
Expand Down Expand Up @@ -2759,7 +2758,11 @@ impl BigInt {
pub fn to_signed_bytes_be(&self) -> Vec<u8> {
let mut bytes = self.data.to_bytes_be();
let first_byte = bytes.first().map(|v| *v).unwrap_or(0);
if first_byte > 0x7f && !(first_byte == 0x80 && bytes.iter().skip(1).all(Zero::is_zero) && self.sign == Sign::Minus) {
if first_byte > 0x7f
&& !(first_byte == 0x80
&& bytes.iter().skip(1).all(Zero::is_zero)
&& self.sign == Sign::Minus)
{
// msb used by magnitude, extend by 1 byte
bytes.insert(0, 0);
}
Expand All @@ -2783,7 +2786,10 @@ impl BigInt {
pub fn to_signed_bytes_le(&self) -> Vec<u8> {
let mut bytes = self.data.to_bytes_le();
let last_byte = bytes.last().map(|v| *v).unwrap_or(0);
if last_byte > 0x7f && !(last_byte == 0x80 && bytes.iter().rev().skip(1).all(Zero::is_zero) && self.sign == Sign::Minus)
if last_byte > 0x7f
&& !(last_byte == 0x80
&& bytes.iter().rev().skip(1).all(Zero::is_zero)
&& self.sign == Sign::Minus)
{
// msb used by magnitude, extend by 1 byte
bytes.push(0);
Expand Down
3 changes: 2 additions & 1 deletion src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ fn from_bitwise_digits_le(v: &[u8], bits: usize) -> BigUint {
.iter()
.rev()
.fold(0, |acc, &c| (acc << bits) | c as BigDigit)
}).collect();
})
.collect();

BigUint::new(data)
}
Expand Down