-
Notifications
You must be signed in to change notification settings - Fork 253
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
Use const generics instead of generic-array #303
Comments
We definitely have const generics on our radar. See the That said, Notably pub trait Digest {
const OUTPUT_SIZE: usize;
}
pub struct Output<D: Digest> {
bytes: [u8; D::OUTPUT_SIZE]
} This has the following compile error today:
In order to express traits like |
FYI relevant rust issues are rust-lang/rust#60551 and rust-lang/rust#76560. |
I had a similar use case recently, and I found a decent workaround on stable Rust for this kind of thing: pub trait Array<T>: Copy + Clone + AsRef<[T]> + AsMut<[T]> /* + (other slice traits) + private::Sealed */ {}
impl<T: Copy, const N: usize> Array<T> for [T; N] {}
pub trait Digest {
type Output: Array<u8>;
const OutputSize: usize = std::mem::size_of::<Self::Output>(); // (Optional, for backwards-compatibility)
}
pub struct Output<D: Digest> {
bytes: D::Output
}
// ...
impl Digest for Sha256 {
type Output = [u8; 32];
} The difference between this approach and the current The disadvantage is that anything that consumes a |
@not-an-aardvark there are other traits where something like that might suffice, but that doesn't cover our existing use cases for We use sizes as bounds and also to compute other properties/sizes using the type system. Consider something like the It has this bound: D: FixedOutput<OutputSize = FieldSize<C>> In that case, we need to know that the output size of a digest is the same size as the base field of an elliptic curve, as verified by the type system. That one may seem simple enough, but there's also the use case of a wide reduction to a scalar: RustCrypto/elliptic-curves#432 ...where the input is twice the width of the elliptic curve's modulus which can be used for a hash-to-scalar use case. So we also need to compute properties using the type system (in this example, multiply-by-2), then use those in trait bounds which say that the output of a particular digest is equal to twice the width of a given elliptic curve's scalar field. |
is there an easy way to convert this into a |
@randomairborne you can use |
There's an overall tracking issue for a const generics migration here: RustCrypto/traits#970 Please follow up there if you have any questions/concerns |
A minimal version of const generics have been stabilized in Rust 1.51, back in March. Thanks to this, we can now get rid of the dependency on generic-array and move to using a simpler
[u8; N]
type with const generics. This should give us several benefits, such as unblocking #87.The downside is that it requires bumping the MSRV (currently at 1.41, I believe) up to 1.51. It may be a bit too early for this bump to take place.
Is there any plans for an eventual migrations to const generics?
The text was updated successfully, but these errors were encountered: