Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0deabc3

Browse files
committedFeb 16, 2024·
Further expand array size support
This adds support for sizes identified as needed for post-quantum KEM/DSA use cases, namely the ones from this comment: RustCrypto/KEMs#2 (comment) These should ideally get expanded into some consistent multiples above 1024, e.g. multiples of 32, and generated in a purely automated manner (e.g. by a script that can break down the bit representation and build the generic syntax), but this should at least be sufficient to unblock these use cases. Note that `UInt<UInt<..<UTerm, B#>, B#...` aliases expressing the explicit bits for a given number are used instead of e.g. `<U1024 as Add<U32>>::Output` because when the latter is used it causes similar errors for conflicting trait impls as we saw with `typenum::U<N>` for whatever reason: error[E0119]: conflicting implementations of trait `traits::ArraySize` for type `UTerm` --> src/sizes.rs:82:13 | 82 | unsafe impl ArraySize for $ty { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | first implementation here | conflicting implementation for `UTerm`
1 parent 70189c0 commit 0deabc3

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed
 

‎src/sizes.rs

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
//! Macros for defining various array sizes, and their associated invocations.
22
33
use super::{ArraySize, AssocArraySize};
4+
use typenum::consts::*;
5+
6+
/// Additional typenum size aliases beyond what are normally provided.
7+
///
8+
/// These are defined using their component bits rather than `Add` to avoid conflicting impls.
9+
#[rustfmt::skip]
10+
pub mod extra_sizes {
11+
use typenum::{UInt, UTerm, B0, B1};
12+
13+
pub type U1088 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
14+
pub type U1152 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
15+
pub type U1184 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
16+
pub type U1472 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
17+
pub type U1536 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
18+
pub type U1568 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
19+
pub type U1600 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
20+
pub type U1632 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>;
21+
pub type U2336 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
22+
pub type U2368 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
23+
pub type U2400 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>;
24+
pub type U3072 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
25+
pub type U3104 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
26+
pub type U3136 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
27+
pub type U3168 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>;
28+
}
29+
30+
pub use extra_sizes::*;
431

532
macro_rules! impl_array_size {
633
($($len:expr => $ty:ident),+) => {
734
$(
8-
unsafe impl ArraySize for typenum::consts::$ty {
35+
unsafe impl ArraySize for $ty {
936
type ArrayType<T> = [T; $len];
1037
}
1138

1239
impl<T> AssocArraySize for [T; $len] {
13-
type Size = typenum::consts::$ty;
40+
type Size = $ty;
1441
}
1542
)+
1643
};
@@ -321,5 +348,22 @@ impl_array_size! {
321348
976 => U976,
322349
992 => U992,
323350
1008 => U1008,
324-
1024 => U1024
351+
1024 => U1024,
352+
1088 => U1088,
353+
1152 => U1152,
354+
1184 => U1184,
355+
1472 => U1472,
356+
1536 => U1536,
357+
1568 => U1568,
358+
1600 => U1600,
359+
1632 => U1632,
360+
2048 => U2048,
361+
2336 => U2336,
362+
2368 => U2368,
363+
2400 => U2400,
364+
3072 => U3072,
365+
3104 => U3104,
366+
3136 => U3136,
367+
3168 => U3168,
368+
4096 => U4096
325369
}

0 commit comments

Comments
 (0)
Please sign in to comment.