1#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
4compile_error!("x86-avx2 backend can be used only on x86 and x86_64 target arches");
5
6#[cfg(target_arch = "x86")]
7use core::arch::x86::*;
8#[cfg(target_arch = "x86_64")]
9use core::arch::x86_64::*;
10
11#[target_feature(enable = "avx2")]
12pub(super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
13 let mut ms: MsgSchedule = [_mm_setzero_si128(); 8];
14 let mut t2: RoundStates = [_mm_setzero_si128(); 40];
15 let mut x = [_mm256_setzero_si256(); 8];
16
17 let (head, block_chunks) = blocks.as_rchunks::<2>();
18
19 if let [block] = head {
20 sha512_compress_x86_64_avx(state, block);
21 }
22
23 for block_chunk in block_chunks {
24 load_data_avx2(&mut x, &mut ms, &mut t2, block_chunk);
25
26 let mut current_state = *state;
28 rounds_0_63_avx2(&mut current_state, &mut x, &mut ms, &mut t2);
29 rounds_64_79(&mut current_state, &ms);
30 accumulate_state(state, ¤t_state);
31
32 current_state = *state;
34 process_second_block(&mut current_state, &t2);
35 accumulate_state(state, ¤t_state);
36 }
37}
38
39#[inline]
40#[target_feature(enable = "avx")]
41fn sha512_compress_x86_64_avx(state: &mut [u64; 8], block: &[u8; 128]) {
42 let mut ms = [_mm_setzero_si128(); 8];
43 let mut x = [_mm_setzero_si128(); 8];
44
45 let mut current_state = *state;
47 load_data_avx(&mut x, &mut ms, block.as_ptr().cast());
48 rounds_0_63_avx(&mut current_state, &mut x, &mut ms);
49 rounds_64_79(&mut current_state, &ms);
50 accumulate_state(state, ¤t_state);
51}
52
53#[inline]
54#[target_feature(enable = "avx")]
55fn load_data_avx(x: &mut [__m128i; 8], ms: &mut MsgSchedule, data: *const __m128i) {
56 #[allow(non_snake_case)]
57 let MASK = _mm_setr_epi32(0x04050607, 0x00010203, 0x0c0d0e0f, 0x08090a0b);
58
59 macro_rules! unrolled_iterations {
60 ($($i:literal),*) => {$(
61 x[$i] = unsafe { _mm_loadu_si128(data.add($i).cast()) };
62 x[$i] = _mm_shuffle_epi8(x[$i], MASK);
63
64 let y = _mm_add_epi64(x[$i], rk(2 * $i));
65
66 ms[$i] = y;
67 )*};
68 }
69
70 unrolled_iterations!(0, 1, 2, 3, 4, 5, 6, 7);
71}
72
73#[inline]
74#[target_feature(enable = "avx2")]
75fn load_data_avx2(
76 x: &mut [__m256i; 8],
77 ms: &mut MsgSchedule,
78 t2: &mut RoundStates,
79 block: &[[u8; 128]; 2],
80) {
81 #[allow(non_snake_case)]
82 let MASK = _mm256_set_epi64x(
83 0x0809_0A0B_0C0D_0E0F_i64,
84 0x0001_0203_0405_0607_i64,
85 0x0809_0A0B_0C0D_0E0F_i64,
86 0x0001_0203_0405_0607_i64,
87 );
88
89 let block_ptr: *const __m128i = block.as_ptr().cast();
90
91 macro_rules! unrolled_iterations {
92 ($($i:literal),*) => {$(
93 let d0 = unsafe { _mm_loadu_si128(block_ptr.add($i)) };
94 let d1 = unsafe { _mm_loadu_si128(block_ptr.add(8 + $i)) };
95 x[$i] = _mm256_insertf128_si256(x[$i], d1, 1);
96 x[$i] = _mm256_insertf128_si256(x[$i], d0, 0);
97
98 x[$i] = _mm256_shuffle_epi8(x[$i], MASK);
99
100 let t = rk(2 * $i);
101 let y = _mm256_add_epi64(x[$i], _mm256_set_m128i(t, t));
102
103 ms[$i] = _mm256_extracti128_si256(y, 0);
104 t2[$i] = _mm256_extracti128_si256(y, 1);
105 )*};
106 }
107
108 unrolled_iterations!(0, 1, 2, 3, 4, 5, 6, 7);
109}
110
111#[inline]
112#[target_feature(enable = "avx")]
113fn rounds_0_63_avx(current_state: &mut State, x: &mut [__m128i; 8], ms: &mut MsgSchedule) {
114 let mut k64_idx: usize = SHA512_BLOCK_WORDS_NUM;
115
116 for _ in 0..4 {
117 for j in 0..8 {
118 let y = sha512_update_x_avx(x, rk(k64_idx));
119
120 {
121 let ms = cast_ms(ms);
122 sha_round(current_state, ms[2 * j]);
123 sha_round(current_state, ms[2 * j + 1]);
124 }
125
126 ms[j] = y;
127 k64_idx += 2;
128 }
129 }
130}
131
132#[inline]
133#[target_feature(enable = "avx2")]
134fn rounds_0_63_avx2(
135 current_state: &mut State,
136 x: &mut [__m256i; 8],
137 ms: &mut MsgSchedule,
138 t2: &mut RoundStates,
139) {
140 let mut k64x4_idx: usize = SHA512_BLOCK_WORDS_NUM;
141
142 for i in 1..5 {
143 for j in 0..8 {
144 let t = rk(k64x4_idx);
145 let y = sha512_update_x_avx2(x, _mm256_set_m128i(t, t));
146
147 {
148 let ms = cast_ms(ms);
149 sha_round(current_state, ms[2 * j]);
150 sha_round(current_state, ms[2 * j + 1]);
151 }
152
153 ms[j] = _mm256_extracti128_si256(y, 0);
154 t2[8 * i + j] = _mm256_extracti128_si256(y, 1);
155
156 k64x4_idx += 2;
157 }
158 }
159}
160
161#[inline(always)]
162fn rounds_64_79(current_state: &mut State, ms: &MsgSchedule) {
163 let ms = cast_ms(ms);
164 for i in 64..80 {
165 sha_round(current_state, ms[i & 0xf]);
166 }
167}
168
169#[inline(always)]
170fn process_second_block(current_state: &mut State, t2: &RoundStates) {
171 for t2 in cast_rs(t2).iter() {
172 sha_round(current_state, *t2);
173 }
174}
175
176#[inline(always)]
177fn sha_round(s: &mut State, x: u64) {
178 macro_rules! big_sigma0 {
179 ($a:expr) => {
180 $a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39)
181 };
182 }
183 macro_rules! big_sigma1 {
184 ($a:expr) => {
185 $a.rotate_right(14) ^ $a.rotate_right(18) ^ $a.rotate_right(41)
186 };
187 }
188 macro_rules! bool3ary_202 {
189 ($a:expr, $b:expr, $c:expr) => {
190 $c ^ ($a & ($b ^ $c))
191 };
192 } macro_rules! bool3ary_232 {
194 ($a:expr, $b:expr, $c:expr) => {
195 ($a & $b) ^ ($a & $c) ^ ($b & $c)
196 };
197 } macro_rules! rotate_state {
200 ($s:ident) => {{
201 let tmp = $s[7];
202 $s[7] = $s[6];
203 $s[6] = $s[5];
204 $s[5] = $s[4];
205 $s[4] = $s[3];
206 $s[3] = $s[2];
207 $s[2] = $s[1];
208 $s[1] = $s[0];
209 $s[0] = tmp;
210 }};
211 }
212
213 let t = x
214 .wrapping_add(s[7])
215 .wrapping_add(big_sigma1!(s[4]))
216 .wrapping_add(bool3ary_202!(s[4], s[5], s[6]));
217
218 s[7] = t
219 .wrapping_add(big_sigma0!(s[0]))
220 .wrapping_add(bool3ary_232!(s[0], s[1], s[2]));
221 s[3] = s[3].wrapping_add(t);
222
223 rotate_state!(s);
224}
225
226#[inline(always)]
227fn accumulate_state(dst: &mut State, src: &State) {
228 for i in 0..SHA512_HASH_WORDS_NUM {
229 dst[i] = dst[i].wrapping_add(src[i]);
230 }
231}
232
233macro_rules! fn_sha512_update_x {
234 ($name:ident, $tf:literal, $ty:ident, {
235 ADD64 = $ADD64:ident,
236 ALIGNR8 = $ALIGNR8:ident,
237 SRL64 = $SRL64:ident,
238 SLL64 = $SLL64:ident,
239 XOR = $XOR:ident,
240 }) => {
241 #[inline]
242 #[target_feature(enable = $tf)]
243 fn $name(x: &mut [$ty; 8], k64: $ty) -> $ty {
244 let mut t0 = $ALIGNR8(x[1], x[0], 8);
246 let mut t3 = $ALIGNR8(x[5], x[4], 8);
248 let mut t2 = $SRL64(t0, 1);
250 x[0] = $ADD64(x[0], t3);
252 t3 = $SRL64(t0, 7);
254 let mut t1 = $SLL64(t0, 64 - 8);
256 t0 = $XOR(t3, t2);
259 t2 = $SRL64(t2, 8 - 1);
261 t0 = $XOR(t0, t1);
265 t1 = $SLL64(t1, 8 - 1);
267 t0 = $XOR(t0, t2);
269 t0 = $XOR(t0, t1);
270 t3 = $SRL64(x[7], 6);
272 t2 = $SLL64(x[7], 64 - 61);
274 x[0] = $ADD64(x[0], t0);
276 t1 = $SRL64(x[7], 19);
278 t3 = $XOR(t3, t2);
281 t2 = $SLL64(t2, 61 - 19);
283 t3 = $XOR(t3, t1);
287 t1 = $SRL64(t1, 61 - 19);
289 t3 = $XOR(t3, t2);
291 t3 = $XOR(t3, t1);
292
293 x[0] = $ADD64(x[0], t3);
295
296 let temp = x[0];
298 x[0] = x[1];
299 x[1] = x[2];
300 x[2] = x[3];
301 x[3] = x[4];
302 x[4] = x[5];
303 x[5] = x[6];
304 x[6] = x[7];
305 x[7] = temp;
306
307 $ADD64(x[7], k64)
308 }
309 };
310}
311
312fn_sha512_update_x!(sha512_update_x_avx, "avx", __m128i, {
313 ADD64 = _mm_add_epi64,
314 ALIGNR8 = _mm_alignr_epi8,
315 SRL64 = _mm_srli_epi64,
316 SLL64 = _mm_slli_epi64,
317 XOR = _mm_xor_si128,
318});
319
320fn_sha512_update_x!(sha512_update_x_avx2, "avx2", __m256i, {
321 ADD64 = _mm256_add_epi64,
322 ALIGNR8 = _mm256_alignr_epi8,
323 SRL64 = _mm256_srli_epi64,
324 SLL64 = _mm256_slli_epi64,
325 XOR = _mm256_xor_si256,
326});
327
328#[inline(always)]
329fn cast_ms(ms: &MsgSchedule) -> &[u64; SHA512_BLOCK_WORDS_NUM] {
330 unsafe { &*(ms.as_ptr().cast()) }
331}
332
333#[inline(always)]
334fn cast_rs(rs: &RoundStates) -> &[u64; SHA512_ROUNDS_NUM] {
335 unsafe { &*(rs.as_ptr().cast()) }
336}
337
338#[inline(always)]
339fn rk(i: usize) -> __m128i {
340 let chunk = &crate::consts::K64[i..][..2];
341 unsafe { _mm_loadu_si128(chunk.as_ptr().cast()) }
342}
343
344type State = [u64; SHA512_HASH_WORDS_NUM];
345type MsgSchedule = [__m128i; SHA512_BLOCK_WORDS_NUM / 2];
346type RoundStates = [__m128i; SHA512_ROUNDS_NUM / 2];
347
348const SHA512_BLOCK_BYTE_LEN: usize = 128;
349const SHA512_ROUNDS_NUM: usize = 80;
350const SHA512_HASH_BYTE_LEN: usize = 64;
351const SHA512_HASH_WORDS_NUM: usize = SHA512_HASH_BYTE_LEN / size_of::<u64>();
352const SHA512_BLOCK_WORDS_NUM: usize = SHA512_BLOCK_BYTE_LEN / size_of::<u64>();