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

Count big bits in u64 instead of usize #143

Merged
merged 2 commits into from Apr 15, 2020
Merged

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Apr 4, 2020

A 32-bit target could create a giant BigUint over 2²⁹ bytes = 512MB,
not that this would be computationally useful, but that value would have
more than usize::MAX bits. To avoid that possibility of overflow, we
can better represent bit counts in u64.

For 64-bit targets, a BigUint over 2⁶¹ bytes is not realistic.

This changes the public API in the return values of bits() and
trailing_zeros(), as well as the bit_size parameter of RandBigInt
and RandomBits. Trying to randomize an absurd size will naturally
cause a capacity overflow, just as if you'd made a huge Vec.

A 32-bit target _could_ create a giant `BigUint` over 2²⁹ bytes = 512MB,
not that this would be computationally useful, but that value would have
more than `usize::MAX` bits. To avoid that possibility of overflow, we
can better represent bit counts in `u64`.

For 64-bit targets, a `BigUint` over 2⁶¹ bytes is not realistic.

This changes the public API in the return values of `bits()` and
`trailing_zeros()`, as well as the `bit_size` parameter of `RandBigInt`
and `RandomBits`. Trying to randomize an absurd size will naturally
cause a capacity overflow, just as if you'd made a huge `Vec`.
@cuviper
Copy link
Member Author

cuviper commented Apr 4, 2020

I'm not certain this is a good idea -- I'll think on it for a bit before merging. Feedback welcome!

@cuviper
Copy link
Member Author

cuviper commented Apr 14, 2020

Ah, let's do it. If this makes it to 0.3 and folks complain, there's always 0.4 and beyond...

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 15, 2020

Build succeeded:

@bors bors bot merged commit ee2b80b into rust-num:master Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant