1use crate::{Block, backends::soft::MinWord};
2use cipher::{
3 Array,
4 array::ArraySize,
5 consts::{U1, U2, U4},
6};
7use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Shl, Shr};
8
9pub(crate) trait Word:
11 Sized
12 + Copy
13 + Default
14 + 'static
15 + BitAnd<Output = Self>
16 + BitAndAssign
17 + BitOr<Output = Self>
18 + BitOrAssign
19 + BitXor<Output = Self>
20 + BitXorAssign
21 + Not<Output = Self>
22 + Shl<u32, Output = Self>
23 + Shr<u32, Output = Self>
24{
25 type Blocks: ArraySize;
27
28 const ROW_BITS: u32 = (size_of::<Self>() * 2) as u32;
31
32 const HALF_ROW: u32 = Self::ROW_BITS / 2;
34 const QUARTER_ROW: u32 = Self::ROW_BITS / 4;
36
37 #[inline(always)]
39 fn ror_distance(rows: u32, cols: u32) -> u32 {
40 rows * Self::ROW_BITS + cols * Self::QUARTER_ROW
41 }
42
43 fn ror(self, n: u32) -> Self;
45
46 fn uniform_row(b: u8) -> Self;
48
49 fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> Self;
51
52 fn byte_repeat(b: u8) -> Self;
54
55 fn bitslice(output: &mut [Self], input: &Array<Block, Self::Blocks>);
57
58 fn inv_bitslice(input: &[Self]) -> Array<Block, Self::Blocks>;
60
61 fn broadcast(rkey: MinWord) -> Self;
63}
64
65impl Word for u16 {
66 type Blocks = U1;
67
68 #[inline(always)]
69 fn ror(self, n: u32) -> u16 {
70 self.rotate_right(n)
71 }
72
73 #[inline(always)]
74 fn uniform_row(b: u8) -> u16 {
75 (b as u16) * 0x1111
76 }
77
78 #[inline(always)]
79 fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> u16 {
80 (r0 as u16) | ((r1 as u16) << 4) | ((r2 as u16) << 8) | ((r3 as u16) << 12)
81 }
82
83 #[inline(always)]
84 fn byte_repeat(b: u8) -> u16 {
85 (b as u16) * 0x0101
86 }
87
88 fn bitslice(output: &mut [u16], input: &Array<Block, U1>) {
90 debug_assert_eq!(output.len(), 8);
91 let b = input[0].as_slice();
92
93 fn read_reordered(input: &[u8]) -> u16 {
102 (u16::from(input[0x0])) | (u16::from(input[0x2]) << 8)
103 }
104
105 let mut t = [
110 read_reordered(&b[0x00..0x03]),
111 read_reordered(&b[0x04..0x07]),
112 read_reordered(&b[0x08..0x0b]),
113 read_reordered(&b[0x0c..0x0f]),
114 read_reordered(&b[0x01..0x04]),
115 read_reordered(&b[0x05..0x08]),
116 read_reordered(&b[0x09..0x0c]),
117 read_reordered(&b[0x0d..0x10]),
118 ];
119
120 bitslice_swaps(&mut t);
121
122 output[..8].copy_from_slice(&t);
125 }
126
127 fn inv_bitslice(input: &[u16]) -> Array<Block, U1> {
129 debug_assert_eq!(input.len(), 8);
130
131 let mut t = [
140 input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7],
141 ];
142
143 bitslice_swaps(&mut t);
144
145 fn write_reordered(rows: u16, output: &mut [u8]) {
146 output[0x0] = rows as u8;
147 output[0x2] = (rows >> 8) as u8;
148 }
149
150 let mut output = Array::<Block, U1>::default();
151 write_reordered(t[0], &mut output[0][0x00..0x03]);
156 write_reordered(t[1], &mut output[0][0x04..0x07]);
157 write_reordered(t[2], &mut output[0][0x08..0x0b]);
158 write_reordered(t[3], &mut output[0][0x0c..0x0f]);
159 write_reordered(t[4], &mut output[0][0x01..0x04]);
160 write_reordered(t[5], &mut output[0][0x05..0x08]);
161 write_reordered(t[6], &mut output[0][0x09..0x0c]);
162 write_reordered(t[7], &mut output[0][0x0d..0x10]);
163
164 output
167 }
168
169 fn broadcast(rkey: MinWord) -> u16 {
170 rkey
171 }
172}
173
174#[inline(always)]
178const fn double_bits_8_to_16(b: u8) -> u16 {
179 let x = b as u16;
180 let x = (x | (x << 4)) & 0x0f0f;
182 let x = (x | (x << 2)) & 0x3333;
183 let x = (x | (x << 1)) & 0x5555;
184 x | (x << 1)
186}
187
188#[inline(always)]
192const fn double_bits_16_to_32(b: u16) -> u32 {
193 let x = b as u32;
194 let x = (x | (x << 8)) & 0x00FF00FF;
196 let x = (x | (x << 4)) & 0x0F0F0F0F;
197 let x = (x | (x << 2)) & 0x33333333;
198 let x = (x | (x << 1)) & 0x55555555;
199 x | (x << 1)
201}
202
203#[inline(always)]
208const fn quad_bits_16_to_64(b: u16) -> u64 {
209 let x = b as u64;
210 let x = (x | (x << 24)) & 0x000000FF000000FF;
212 let x = (x | (x << 12)) & 0x000F000F000F000F;
213 let x = (x | (x << 6)) & 0x0303030303030303;
214 let x = (x | (x << 3)) & 0x1111111111111111;
215 x | (x << 1) | (x << 2) | (x << 3)
217}
218
219impl Word for u32 {
220 type Blocks = U2;
221
222 #[inline(always)]
223 fn ror(self, n: u32) -> u32 {
224 self.rotate_right(n)
225 }
226
227 #[inline(always)]
228 fn uniform_row(b: u8) -> u32 {
229 double_bits_8_to_16(b) as u32 * 0x01010101
230 }
231
232 #[inline(always)]
233 fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> u32 {
234 (double_bits_8_to_16(r0) as u32)
235 | ((double_bits_8_to_16(r1) as u32) << 8)
236 | ((double_bits_8_to_16(r2) as u32) << 16)
237 | ((double_bits_8_to_16(r3) as u32) << 24)
238 }
239
240 #[inline(always)]
241 fn byte_repeat(b: u8) -> u32 {
242 (b as u32) * 0x01010101
243 }
244
245 fn bitslice(output: &mut [u32], input: &Array<Block, U2>) {
247 debug_assert_eq!(output.len(), 8);
248 let input0 = input[0].as_slice();
249 let input1 = input[1].as_slice();
250
251 let mut t = [
262 u32::from_le_bytes(input0[0x00..0x04].try_into().unwrap()),
263 u32::from_le_bytes(input1[0x00..0x04].try_into().unwrap()),
264 u32::from_le_bytes(input0[0x04..0x08].try_into().unwrap()),
265 u32::from_le_bytes(input1[0x04..0x08].try_into().unwrap()),
266 u32::from_le_bytes(input0[0x08..0x0c].try_into().unwrap()),
267 u32::from_le_bytes(input1[0x08..0x0c].try_into().unwrap()),
268 u32::from_le_bytes(input0[0x0c..0x10].try_into().unwrap()),
269 u32::from_le_bytes(input1[0x0c..0x10].try_into().unwrap()),
270 ];
271
272 bitslice_swaps(&mut t);
273
274 output[..8].copy_from_slice(&t);
277 }
278
279 fn inv_bitslice(input: &[u32]) -> Array<Block, U2> {
281 debug_assert_eq!(input.len(), 8);
282
283 let mut t = [
292 input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7],
293 ];
294
295 bitslice_swaps(&mut t);
296
297 let mut output = Array::<Block, U2>::default();
298 output[0][0x00..0x04].copy_from_slice(&t[0].to_le_bytes());
301 output[0][0x04..0x08].copy_from_slice(&t[2].to_le_bytes());
302 output[0][0x08..0x0c].copy_from_slice(&t[4].to_le_bytes());
303 output[0][0x0c..0x10].copy_from_slice(&t[6].to_le_bytes());
304 output[1][0x00..0x04].copy_from_slice(&t[1].to_le_bytes());
305 output[1][0x04..0x08].copy_from_slice(&t[3].to_le_bytes());
306 output[1][0x08..0x0c].copy_from_slice(&t[5].to_le_bytes());
307 output[1][0x0c..0x10].copy_from_slice(&t[7].to_le_bytes());
308
309 output
312 }
313
314 fn broadcast(rkey: MinWord) -> Self {
315 double_bits_16_to_32(rkey)
316 }
317}
318
319impl Word for u64 {
320 type Blocks = U4;
321
322 #[inline(always)]
323 fn ror(self, n: u32) -> u64 {
324 self.rotate_right(n)
325 }
326
327 #[inline(always)]
328 fn uniform_row(b: u8) -> u64 {
329 quad_bits_16_to_64(b as u16) * 0x0001_0001_0001_0001
330 }
331
332 #[inline(always)]
333 fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> u64 {
334 quad_bits_16_to_64(r0 as u16)
335 | (quad_bits_16_to_64(r1 as u16) << 16)
336 | (quad_bits_16_to_64(r2 as u16) << 32)
337 | (quad_bits_16_to_64(r3 as u16) << 48)
338 }
339
340 #[inline(always)]
341 fn byte_repeat(b: u8) -> u64 {
342 (b as u64) * 0x0101010101010101
343 }
344
345 fn bitslice(output: &mut [u64], input: &Array<Block, U4>) {
347 debug_assert_eq!(output.len(), 8);
348
349 #[rustfmt::skip]
358 fn read_reordered(input: &[u8]) -> u64 {
359 (u64::from(input[0x0]) ) |
360 (u64::from(input[0x1]) << 0x10) |
361 (u64::from(input[0x2]) << 0x20) |
362 (u64::from(input[0x3]) << 0x30) |
363 (u64::from(input[0x8]) << 0x08) |
364 (u64::from(input[0x9]) << 0x18) |
365 (u64::from(input[0xa]) << 0x28) |
366 (u64::from(input[0xb]) << 0x38)
367 }
368
369 let mut t = [
374 read_reordered(&input[0][0x00..0x0c]),
375 read_reordered(&input[1][0x00..0x0c]),
376 read_reordered(&input[2][0x00..0x0c]),
377 read_reordered(&input[3][0x00..0x0c]),
378 read_reordered(&input[0][0x04..0x10]),
379 read_reordered(&input[1][0x04..0x10]),
380 read_reordered(&input[2][0x04..0x10]),
381 read_reordered(&input[3][0x04..0x10]),
382 ];
383
384 bitslice_swaps(&mut t);
385
386 output[..8].copy_from_slice(&t);
389 }
390
391 fn inv_bitslice(input: &[u64]) -> Array<Block, U4> {
393 debug_assert_eq!(input.len(), 8);
394
395 let mut t = [
404 input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7],
405 ];
406
407 bitslice_swaps(&mut t);
408
409 #[rustfmt::skip]
410 fn write_reordered(columns: u64, output: &mut [u8]) {
411 output[0x0] = (columns ) as u8;
412 output[0x1] = (columns >> 0x10) as u8;
413 output[0x2] = (columns >> 0x20) as u8;
414 output[0x3] = (columns >> 0x30) as u8;
415 output[0x8] = (columns >> 0x08) as u8;
416 output[0x9] = (columns >> 0x18) as u8;
417 output[0xa] = (columns >> 0x28) as u8;
418 output[0xb] = (columns >> 0x38) as u8;
419 }
420
421 let mut output = Array::<Block, U4>::default();
422 write_reordered(t[0], &mut output[0][0x00..0x0c]);
427 write_reordered(t[4], &mut output[0][0x04..0x10]);
428 write_reordered(t[1], &mut output[1][0x00..0x0c]);
429 write_reordered(t[5], &mut output[1][0x04..0x10]);
430 write_reordered(t[2], &mut output[2][0x00..0x0c]);
431 write_reordered(t[6], &mut output[2][0x04..0x10]);
432 write_reordered(t[3], &mut output[3][0x00..0x0c]);
433 write_reordered(t[7], &mut output[3][0x04..0x10]);
434
435 output
438 }
439
440 fn broadcast(rkey: MinWord) -> Self {
441 quad_bits_16_to_64(rkey)
442 }
443}
444
445#[inline(always)]
452fn bitslice_swaps<W: Word>(t: &mut [W; 8]) {
453 use super::utils::delta_swap_2;
454 let [t0, t1, t2, t3, t4, t5, t6, t7] = t;
455
456 let m0 = W::byte_repeat(0x55);
459 delta_swap_2(t1, t0, 1, m0);
460 delta_swap_2(t3, t2, 1, m0);
461 delta_swap_2(t5, t4, 1, m0);
462 delta_swap_2(t7, t6, 1, m0);
463
464 let m1 = W::byte_repeat(0x33);
467 delta_swap_2(t2, t0, 2, m1);
468 delta_swap_2(t3, t1, 2, m1);
469 delta_swap_2(t6, t4, 2, m1);
470 delta_swap_2(t7, t5, 2, m1);
471
472 let m2 = W::byte_repeat(0x0f);
475 delta_swap_2(t4, t0, 4, m2);
476 delta_swap_2(t5, t1, 4, m2);
477 delta_swap_2(t6, t2, 4, m2);
478 delta_swap_2(t7, t3, 4, m2);
479}