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

How could I get/set n-th bit of BigInt? #172

Closed
kazunetakahashi opened this issue Oct 27, 2020 · 8 comments · Fixed by #183
Closed

How could I get/set n-th bit of BigInt? #172

kazunetakahashi opened this issue Oct 27, 2020 · 8 comments · Fixed by #183

Comments

@kazunetakahashi
Copy link

Would you do me a favor? I need some help.

I'd like to get and set $n$-th bit of BigInt. How could I do it in a fast way?

For example, ramp has such methods; bit and set_bit.

I know that I can do it by bitwise operation such as (x >> bit) & BigInt::one() == BigInt::one() or so on. But I need faster way.

I would be grateful if you could give me advice.

@cuviper
Copy link
Member

cuviper commented Oct 28, 2020

I can't think of a fast way to do it with the public API right now, but we could add methods like ramp's.

Can you offer the some example motivation of what you would use this for?

@kazunetakahashi
Copy link
Author

Thank you for your comment.

Can you offer the some example motivation of what you would use this for?

I am trying making big prime numbers by a randomized algorithm now. This is needed for a cryptographic algorithm that I am researching on. In this method, one will try making a random integer and check if it is prime or not many times. To reduce useless attempts, set_bit is useful. For example, they set its 0-th bit as 1 to gain an odd number and 1023-th bit as 1 to gain such a big number for strangeness of cryptography. Similarly, get_bit is also good for checking prime numbers.

Other good big-integer libraries depends on C language, but yours is made of pure Rust. This is also a good point for my purpose.

@cuviper
Copy link
Member

cuviper commented Oct 30, 2020

For BigUint, both getting and setting a bit should be straightforward to implement.

For BigInt, there's a design question of how to handle negative numbers. If we just use bits of the unsigned magnitude, it can simply forward to the BigUint methods. But if we want to expose bits as if it were a two's complement representation, as ramp does, then it will require some translation. We do implement the bitwise-ops (BitAnd etc.) as two's complement, so that's probably the right choice here as well.

(Do not copy ramp's implementation, because the licenses are different.)

@janmarthedal
Copy link
Contributor

I am currently working on this, so a PR should be coming soon!

@mkaczanowski
Copy link

@janmarthedal how's the PR going? I also need this feature (the only reason why I use rug now is that function is missing)

@janmarthedal
Copy link
Contributor

@mkaczanowski I have submitted a PR so I guess it is up to the project maintainers now.

bors bot added a commit that referenced this issue Feb 24, 2021
183: Get/set n-th bit of `BigUint` and `BigInt` r=cuviper a=janmarthedal

This PR implements `bit` and `set_bit` for `BigUint` and `BigInt`.

The method names have been chosen to match those of Ramp (https://docs.rs/ramp/0.5.9/ramp/int/struct.Int.html#method.bit).

For `BigInt` the implementation uses the number's two's complement representation when the number is negative. This matches what libraries like Ramp or languages like Python do.

Resolves #172 

Co-authored-by: Jan Marthedal Rasmussen <jan@janmr.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors bors bot closed this as completed in 7492eec Feb 24, 2021
@mkaczanowski
Copy link

Nice work! Thanks, @cuviper @janmarthedal

@kazunetakahashi
Copy link
Author

@cuviper @janmarthedal Thank you so much. Today, I confirmed that my program got faster using your bit and set_bit. It helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants