vitaminc_permutation/
lib.rs1#![doc = include_str!("../README.md")]
2mod bitwise;
3mod elementwise;
4mod key;
5
6pub use bitwise::BitwisePermute;
9pub use elementwise::{Depermute, Permute};
10pub use key::PermutationKey;
11
12mod private {
13 use vitaminc_protected::Zeroed;
14
15 pub trait IsPermutable: Zeroed {}
16 impl IsPermutable for [u8; 8] {}
17 impl IsPermutable for [u8; 16] {}
18 impl IsPermutable for [u8; 32] {}
19 impl IsPermutable for [u8; 64] {}
20 impl IsPermutable for [u8; 128] {}
21 impl IsPermutable for [u16; 8] {}
22 impl IsPermutable for [u16; 16] {}
23 impl IsPermutable for [u16; 32] {}
24 impl IsPermutable for [u16; 64] {}
25 impl IsPermutable for [u16; 128] {}
26 impl IsPermutable for [u32; 8] {}
27 impl IsPermutable for [u32; 16] {}
28 impl IsPermutable for [u32; 32] {}
29 impl IsPermutable for [u32; 64] {}
30 impl IsPermutable for [u32; 128] {}
31
32 pub(crate) const fn identity<const N: usize>() -> [u8; N]
33 where
34 [u8; N]: IsPermutable,
35 {
36 let mut out = [0; N];
37 let mut i = 0;
38 while i < N {
39 out[i] = i as u8;
40 i += 1;
41 }
42 out
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::private::IsPermutable;
49 use crate::PermutationKey;
50 use rand::SeedableRng;
51 use vitaminc_random::{Generatable, SafeRand};
52
53 pub fn gen_rand_key<const N: usize>() -> PermutationKey<N>
54 where
55 [u8; N]: IsPermutable,
56 {
57 let mut rng = SafeRand::from_entropy();
58 PermutationKey::random(&mut rng).expect("Failed to generate key")
59 }
60
61 pub fn gen_key<const N: usize>(seed: [u8; 32]) -> PermutationKey<N>
62 where
63 [u8; N]: IsPermutable,
64 {
65 let mut rng = SafeRand::from_seed(seed);
66 PermutationKey::random(&mut rng).expect("Failed to generate key")
67 }
68}