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

core-only "heapless" support #51

Open
roblabla opened this issue Jun 11, 2020 · 8 comments
Open

core-only "heapless" support #51

roblabla opened this issue Jun 11, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@roblabla
Copy link
Contributor

Right now, there is a PR open (#22) that will allow the RSA crate to be used in core+alloc environments (e.g. environments without libstd, but where a dynamic allocator is available).

The next step is for the RSA crate to support core-only environments. This would require multiple changes: num-bigint-dig would need to be represented either as a stack-allocated array (GenericArray? ArrayVec?) or a slice reference (wooo lifetimes) instead of a heap-allocated vector. Furthermore, we'll need to make extra sure that all the operations are bounded within a certain numeric range.

@dignifiedquire
Copy link
Member

it will be hard, but very valuable to be able to make the bigint lib usable with genericarray,

@tarcieri
Copy link
Member

tarcieri commented Jun 11, 2020

You could potentially use heapless::Vec to simplify usage of GenericArray with something closer to the ergonomics of alloc::vec::Vec.

Unfortunately they haven't updated to generic-array v0.14 yet, which is annoying:

rust-embedded/heapless#166

@dignifiedquire
Copy link
Member

Yes heapless::Vec has some nice utilities that we would need. I would love to keep the ability for returning larger numbers, eg multiply that returnst the full result and not just the truncated, might make sense to implement the std operations like std lib, with overflow panic, and having wrapping, checked and a new variant sth like full which would return the fully expanded version

@newpavlov
Copy link
Member

I would love to keep the ability for returning larger numbers, eg multiply that returnst the full result and not just the truncated

IIUC it should be possible to do completely on stack, with something like:

fn mul<const N: usize>(a: [Word; N], b: [Word; N]) -> [Word; 2*N] { .. }

@tarcieri
Copy link
Member

tarcieri commented Jun 11, 2020

For what it's worth, I started working on a crate with traits to abstract over the details between containers like Vec and heapless::Vec (or arrayvec if so desired), with all operations fallible for use with fixed-sized containers:

Not using it for anything yet and it could probably use some work, but I think it has a similar inspiration to this sort of use case.

@dignifiedquire
Copy link
Member

@newpavlov it is possible, but as we don't have const generics yet this gets quite ugly and complex quickly, especially if your interfaces have to rely on the certain size, as they would for RSA given a fixed bitwitdth

@tarcieri
Copy link
Member

@dignifiedquire if you'd like any help doing typenum arithmetic for computing GenericArray sizes, I've done a lot of it in the other crates. Here's an example:

https://docs.rs/ecdsa/0.6.0/ecdsa/asn1_signature/struct.Asn1Signature.html

@tarcieri
Copy link
Member

crypto-bigint has a stack-allocated Uint type based on const generics.

It might be interesting to switch to that if it also provided a heap-allocated type like UintVec which could replace the existing usages of num_bigint_dig::BigUint, along with traits to abstract across both options. Unfortunately it doesn't have support for that yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants