8000 Fix `Uint256::increment` panics by TheBlueMatt · Pull Request #612 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Uint256::increment panics #612

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

Merged
merged 4 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion fuzz/fuzz_targets/uint128_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn do_test(data: &[u8]) {
} }
}

if data.len() != 16*2 + 1 { return; }
if data.len() != 16*2 { return; }
let (a_native, a) = read_ints!(0);

// Checks using only a:
Expand All @@ -40,6 +40,10 @@ fn do_test(data: &[u8]) {
assert_eq!(128 - a_native.leading_zeros() as usize, a.bits());
assert_eq!(a_native as u64, bitcoin::util::uint::Uint128::from_u64(a_native as u64).unwrap().low_u64());

let mut a_inc = a.clone();
a_inc.increment();
check_eq!(a_native.wrapping_add(1), a_inc);

// Checks with two numbers:
let (b_native, b) = read_ints!(16);

Expand Down
73 changes: 57 additions & 16 deletions src/util/uint.rs
8000 8A1C
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ macro_rules! construct_uint {

($name(ret), sub_copy)
}

/// Increment by 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to explain in the doc that the function does wrapping increment

#[inline]
pub fn increment(&mut self) {
let &mut $name(ref mut arr) = self;
for i in 0..$n_words {
arr[i] = arr[i].wrapping_add(1);
if arr[i] != 0 { break; }
}
}
}

impl PartialOrd for $name {
Expand Down Expand Up @@ -512,22 +522,6 @@ impl ::std::fmt::Display for ParseLengthError {
impl ::std::error::Error for ParseLengthError {}

impl Uint256 {
/// Increment by 1
#[inline]
pub fn increment(&mut self) {
let &mut Uint256(ref mut arr) = self;
arr[0] += 1;
if arr[0] == 0 {
arr[1] += 1;
if arr[1] == 0 {
arr[2] += 1;
if arr[2] == 0 {
arr[3] += 1;
}
}
}
}

/// Decay to a uint128
#[inline]
pub fn low_128(&self) -> Uint128 {
Expand Down Expand Up @@ -689,6 +683,53 @@ mod tests {
0x4AFCFF6F0375C608u64, 0x928D92B4D7F5DF33u64]));
}

#[test]
pub fn increment_test() {
let mut val = Uint256([
0xFFFFFFFFFFFFFFFEu64,
0xFFFFFFFFFFFFFFFFu64,
0xFFFFFFFFFFFFFFFFu64,
0xEFFFFFFFFFFFFFFFu64,
]);
val.increment();
assert_eq!(
val,
Uint256([
0xFFFFFFFFFFFFFFFFu64,
0xFFFFFFFFFFFFFFFFu64,
0xFFFFFFFFFFFFFFFFu64,
0xEFFFFFFFFFFFFFFFu64,
])
);
val.increment();
assert_eq!(
val,
Uint256([
0x0000000000000000u64,
0x0000000000000000u64,
0x0000000000000000u64,
0xF000000000000000u64,
])
);

let mut val = Uint256([
0xFFFFFFFFFFFFFFFFu64,
0xFFFFFFFFFFFFFFFFu64,
0xFFFFFFFFFFFFFFFFu64,
0xFFFFFFFFFFFFFFFFu64,
]);
val.increment();
assert_eq!(
val,
Uint256([
0x0000000000000000u64,
0x0000000000000000u64,
0x0000000000000000u64,
0x0000000000000000u64,
])
);
}

#[test]
pub fn uint256_bitslice_test() {
let init = Uint256::from_u64(0xDEADBEEFDEADBEEF).unwrap();
Expand Down
0