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

Create IBig from arrays/mod_pow? #6

Closed
username223 opened this issue Jul 4, 2021 · 5 comments
Closed

Create IBig from arrays/mod_pow? #6

username223 opened this issue Jul 4, 2021 · 5 comments

Comments

@username223
Copy link

Hello,

Thanks for the excellent library and bigint comparison. I'm unsure if this functionality is present since the docs do not mention it, but is it possible to create an IBig from an array/slice? It only seems to be possible to create them from strings, and since I already have my values as arrays on the stack it doesn't make much sense to allocate and convert to a string only to parse it and convert back.

I'm writing a small library with very well defined integer limits of at most 32 bytes, so if possible I would like to avoid allocations at all and just do operations on stack arrays.

I also can't find a mod_pow equivalent, is that included?

Thanks :)

@vks
Copy link

vks commented Jul 4, 2021

@username223 I have a generic implementation of mod_pow in this open PR. It should work with ibig if you implement the num-integer traits. Alternatively, see my code here, which already works with ibig.

According to #1, modular exponentiation is already implemented.

@vks
Copy link

vks commented Jul 4, 2021

I found the modular exponentiation here. It does however not support negative exponents. See the code above if you need that.

@vks
Copy link

vks commented Jul 4, 2021

@username223 This let's you create a UBig from a &[u8].

@tczajka
Copy link
Owner

tczajka commented Jul 4, 2021

To summarize:

Conversion from bytes already exists as UBig::from_le_bytes and UBig::from_be_bytes. Is this sufficient?

Modular exponentiation already exists as ibig::modular::Modulo::pow.

Integers bigger than 8 bytes (on 64-bit systems) currently allocate on the heap. This may change in the future, with something like 24 or 32 bytes not requiring an allocation (since this can be done without increasing mem::size_of<UBig>()), but let's keep that out of the scope of this ticket.

@username223
Copy link
Author

@vks @tczajka
Ah yes, that seems to do. I'm not sure how I missed that. Thanks for the help. :)

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

No branches or pull requests

3 participants