Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overflowing_shr result is incorrect #337

Closed
wooong210 opened this issue Oct 26, 2023 · 0 comments · Fixed by #347
Closed

overflowing_shr result is incorrect #337

wooong210 opened this issue Oct 26, 2023 · 0 comments · Fixed by #347

Comments

@wooong210
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant