Skip to content

Fix increment of Uint256 with carry#578

Closed
carolcapps wants to merge 1 commit intorust-bitcoin:masterfrom
carolcapps:feature/fix-uint256-increment
Closed

Fix increment of Uint256 with carry#578
carolcapps wants to merge 1 commit intorust-bitcoin:masterfrom
carolcapps:feature/fix-uint256-increment

Conversation

@carolcapps
Copy link
Copy Markdown
Contributor

The issue is that panic occurs when increment Uint256 values with carry on Rust debug mode.
Changed to use wrapping_add as same as add() function and add tests.

arr[2] = arr[2].wrapping_add(1);
if arr[2] == 0 {
arr[3] += 1;
arr[3] = arr[3].wrapping_add(1);
Copy link
Copy Markdown
Member

@TheBlueMatt TheBlueMatt Mar 13, 2021

Choose a reason for hiding this comment

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

Shouldn't we let the last one panic (or rename the function to wrapping_)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In Uint256, add and multiply operation also don't panic when overflow. I think increment should follow their behavior, so if we prefer panic, we should change them too.

@@ -516,13 +516,13 @@ impl Uint256 {
#[inline]
pub fn increment(&mut self) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be nice if we could move this into the macro generation, that way we can add a fuzz case for it for u128 and get fuzz coverage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Err, can you go ahead and move it there in this PR (can be a separate commit). That would require swapping the if branches for a loop, but that should be easy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do.

Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

Shouldn't we follow Rust API and have different addition etc operations for UInt256 type which will at least match add (panicking) , saturating_add, wrapping_add and checked_add functions? Otherwise it's really strange that u32::add panics and UInt256 does wrapping

@sgeisler
Copy link
Copy Markdown
Contributor

Is this PR still active?

@TheBlueMatt
Copy link
Copy Markdown
Member

Superseded by #612.

@TheBlueMatt TheBlueMatt closed this Jun 8, 2021
apoelstra added a commit that referenced this pull request Sep 27, 2021
5d71a9d Correct input length check for uin128 fuzzer (Matt Corallo)
9c256cc Add a fuzz check for `Uint128::increment` (Matt Corallo)
a15f263 Move the `increment` fn into the uint macro to add it to Uint128 (Matt Corallo)
d52b88b Fix increment of Uint256 with carry (carolcapps)

Pull request description:

  This is #578 with review feedback addressed.

ACKs for top commit:
  apoelstra:
    ACK 5d71a9d
  sanket1729:
    ACK 5d71a9d

Tree-SHA512: 32e5ea6387943ecad8f190a0de336a545fda72b6ff7388d3479037a5f880434276a7d0607f5cf61710d45e984c01954f4e3199a60c542be48b397717afb3d406
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 this pull request may close these issues.

4 participants