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

[RFC] Is it possible to be both const generic and dynamic size #29

Closed
elichai opened this issue Oct 31, 2021 · 5 comments
Closed

[RFC] Is it possible to be both const generic and dynamic size #29

elichai opened this issue Oct 31, 2021 · 5 comments

Comments

@elichai
Copy link

elichai commented Oct 31, 2021

Const generics and no-std are great features, both for performance and for interoperability.
The down side is that some protocols require a dynamic moduli, and maybe even dynamic sizes, for example RSA sizes(2048/3072/4096/8192 etc), or DSA moduli sizes, and more.

This gets more obvious with something like #28, which makes the type generic over the field, then you have to support a dynamic field to be able to support RSA, dynamic DSA moduli, groups with unknown order etc.

My question is is it possible to win both worlds, and somehow both allow const generics for arrays, but also allow dynamic generation over vectors?

@tarcieri
Copy link
Member

It sounds like you want heap-backed, arbitrary precision integers in addition to the fixed-sized const generic ones?

If so, I'd say there aren't any plans to add that sort of thing any time soon. Maybe down the road, but it isn't on the radar right now. I'd also point out that if this is really what you want, there are many, many libraries in the Rust ecosystem to select from which provide heap-backed arbitrary precision big integers.

dynamic sizes, for example RSA sizes(2048/3072/4096/8192 etc), dynamic DSA moduli,

If you want to select from different sizes at runtime, use an enum with variants for your acceptable alternatives. This is a solution that works not just for different sizes of RSA, but for whatever ciphers happen to fulfill a particular role.

@elichai
Copy link
Author

elichai commented Oct 31, 2021

If so, I'd say there aren't any plans to add that sort of thing any time soon. Maybe down the road, but it isn't on the radar right now. I'd also point out that if this is really what you want, there are many, many libraries in the Rust ecosystem to select from which provide heap-backed arbitrary precision big integers.

Well, even when using a heap based arbitrary precision integer, I'd want it to have a specific size determined in runtime, and not allow it to alloc any more than that. it's possible now if you prealloc + only use modular operations, but I'd rather have in enforced in the type system.

@tarcieri
Copy link
Member

If that's all you want, it sounds like Box<UInt<N>> would suffice?

@elichai
Copy link
Author

elichai commented Oct 31, 2021

Not quite, it doesn't allow you to write the follwing:

fn get_integer(moduli: &str) -> Box<Uint>

(such that it will determine the size of the uint depending on the moduli)

It can probably also be done with a custom allocator that prohibits the reallocation.

@tarcieri
Copy link
Member

Then you really need something like an enum to provide the type-level glue between a const-generic N and runtime selection of its size.

const N: usize needs to be fixed at compile-time, so there's no way to "select" it at runtime other than picking a fixed set of N values (e.g. your 2048/3072/4096/8192 above), placing those in something like an enum, and then picking the appropriate one at runtime.

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

2 participants