recmo/uint

`overflowing_shr` result is incorrect

Closed this issue · 0 comments

Version
1.10.1

Description

uint/src/bits.rs

Lines 376 to 381 in 666cd31

// Check for overflow
let mut overflow = false;
for i in 0..limbs {
overflow |= self.limbs[i] != 0;
}
overflow |= self.limbs[limbs] >> bits != 0;

The overflow result of overflowing_shr is incorrect.

The above can be modified as follows:

// Check for overflow
let mut overflow = false;
for i in 0..limbs {
overflow |= self.limbs[i] != 0;
}
overflow |= self.limbs[limbs] << (64 - bits) != 0;

I tried this code:

let mut test_slice = [40u64];
let a = Uint::<64, 1>::from_limbs(test_slice);
println!("{:?}", a.overflowing_shr(1));

test_slice[0] = 41u64;
let b = Uint::<64, 1>::from_limbs(test_slice);
println!("{:?}", b.overflowing_shr(1));

I expected to see this happen:

(0x0000000000000014_U64, false)
(0x0000000000000014_U64, true)

Instead, this happened:

(0x0000000000000014_U64, true)
(0x0000000000000014_U64, true)