Debian Codegraph @

debian/rust-vendor/sha2/src/sha512/wasm32_simd128.rs

raw
1 #![allow(clippy::many_single_char_names, unsafe_op_in_unsafe_fn)]
2
3 #[cfg(not(target_arch = "wasm32"))]
4 compile_error!("wasm32-simd128 backend can be used only on wasm32 target arches");
5 #[cfg(not(target_feature = "simd128"))]
6 compile_error!("wasm32-simd128 backend requires simd128 target feature");
7
8 use core::arch::wasm32::*;
9 use core::mem::size_of;
10
11 use crate::consts::K64;
12
13 pub(super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
14 let mut ms = [u64x2(0, 0); 8];
15 let mut x = [u64x2(0, 0); 8];
16
17 for block in blocks {
18 unsafe {
19 let mut current_state = *state;
20 load_data(&mut x, &mut ms, block.as_ptr().cast());
21 rounds_0_63(&mut current_state, &mut x, &mut ms);
22 rounds_64_79(&mut current_state, &ms);
23 accumulate_state(state, &current_state);
24 }
25 }
26 }
27
28 #[inline(always)]
29 unsafe fn load_data(x: &mut [v128; 8], ms: &mut MsgSchedule, data: *const v128) {
30 macro_rules! unrolled_iterations {
31 ($($i:literal),*) => {$(
32 x[$i] = v128_load(data.add($i).cast());
33 x[$i] = i8x16_shuffle::<7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8>(x[$i], x[$i]);
34
35 let y = i64x2_add(
36 x[$i],
37 v128_load(K64.as_ptr().add(2 * $i).cast()),
38 );
39
40 ms[$i] = y;
41 )*};
42 }
43
44 unrolled_iterations!(0, 1, 2, 3, 4, 5, 6, 7);
45 }
46
47 #[inline(always)]
48 unsafe fn rounds_0_63(current_state: &mut State, x: &mut [v128; 8], ms: &mut MsgSchedule) {
49 let mut k64_idx: usize = SHA512_BLOCK_WORDS_NUM;
50
51 for _ in 0..4 {
52 for j in 0..8 {
53 let k64 = v128_load(K64.as_ptr().add(k64_idx).cast());
54 let y = sha512_update_x(x, k64);
55
56 {
57 let ms = ms[j];
58 sha_round(current_state, u64x2_extract_lane::<0>(ms));
59 sha_round(current_state, u64x2_extract_lane::<1>(ms));
60 }
61
62 ms[j] = y;
63 k64_idx += 2;
64 }
65 }
66 }
67
68 #[inline(always)]
69 fn rounds_64_79(current_state: &mut State, ms: &MsgSchedule) {
70 for j in 0..8 {
71 let ms = ms[j];
72 sha_round(current_state, u64x2_extract_lane::<0>(ms));
73 sha_round(current_state, u64x2_extract_lane::<1>(ms));
74 }
75 }
76
77 #[inline(always)]
78 fn sha_round(s: &mut State, x: u64) {
79 macro_rules! big_sigma0 {
80 ($a:expr) => {
81 $a.rotate_right(28) ^ $a.rotate_right(34) ^ $a.rotate_right(39)
82 };
83 }
84 macro_rules! big_sigma1 {
85 ($a:expr) => {
86 $a.rotate_right(14) ^ $a.rotate_right(18) ^ $a.rotate_right(41)
87 };
88 }
89 macro_rules! bool3ary_202 {
90 ($a:expr, $b:expr, $c:expr) => {
91 $c ^ ($a & ($b ^ $c))
92 };
93 } // Choose, MD5F, SHA1C
94 macro_rules! bool3ary_232 {
95 ($a:expr, $b:expr, $c:expr) => {
96 ($a & $b) ^ ($a & $c) ^ ($b & $c)
97 };
98 } // Majority, SHA1M
99
100 macro_rules! rotate_state {
101 ($s:ident) => {{
102 let tmp = $s[7];
103 $s[7] = $s[6];
104 $s[6] = $s[5];
105 $s[5] = $s[4];
106 $s[4] = $s[3];
107 $s[3] = $s[2];
108 $s[2] = $s[1];
109 $s[1] = $s[0];
110 $s[0] = tmp;
111 }};
112 }
113
114 let t = x
115 .wrapping_add(s[7])
116 .wrapping_add(big_sigma1!(s[4]))
117 .wrapping_add(bool3ary_202!(s[4], s[5], s[6]));
118
119 s[7] = t
120 .wrapping_add(big_sigma0!(s[0]))
121 .wrapping_add(bool3ary_232!(s[0], s[1], s[2]));
122 s[3] = s[3].wrapping_add(t);
123
124 rotate_state!(s);
125 }
126
127 #[inline(always)]
128 fn accumulate_state(dst: &mut State, src: &State) {
129 for i in 0..SHA512_HASH_WORDS_NUM {
130 dst[i] = dst[i].wrapping_add(src[i]);
131 }
132 }
133
134 #[inline(always)]
135 unsafe fn sha512_update_x(x: &mut [v128; 8], k64: v128) -> v128 {
136 let mut t0 = u64x2_shuffle::<1, 2>(x[0], x[1]);
137 let mut t3 = u64x2_shuffle::<1, 2>(x[4], x[5]);
138 let mut t2 = u64x2_shr(t0, 1);
139 x[0] = i64x2_add(x[0], t3);
140 t3 = u64x2_shr(t0, 7);
141 let mut t1 = u64x2_shl(t0, 64 - 8);
142 t0 = v128_xor(t3, t2);
143 t2 = u64x2_shr(t2, 8 - 1);
144 t0 = v128_xor(t0, t1);
145 t1 = u64x2_shl(t1, 8 - 1);
146 t0 = v128_xor(t0, t2);
147 t0 = v128_xor(t0, t1);
148 t3 = u64x2_shr(x[7], 6);
149 t2 = u64x2_shl(x[7], 64 - 61);
150 x[0] = i64x2_add(x[0], t0);
151 t1 = u64x2_shr(x[7], 19);
152 t3 = v128_xor(t3, t2);
153 t2 = u64x2_shl(t2, 61 - 19);
154 t3 = v128_xor(t3, t1);
155 t1 = u64x2_shr(t1, 61 - 19);
156 t3 = v128_xor(t3, t2);
157 t3 = v128_xor(t3, t1);
158 x[0] = i64x2_add(x[0], t3);
159 let temp = x[0];
160 x[0] = x[1];
161 x[1] = x[2];
162 x[2] = x[3];
163 x[3] = x[4];
164 x[4] = x[5];
165 x[5] = x[6];
166 x[6] = x[7];
167 x[7] = temp;
168 i64x2_add(x[7], k64)
169 }
170
171 type State = [u64; SHA512_HASH_WORDS_NUM];
172 type MsgSchedule = [v128; SHA512_BLOCK_WORDS_NUM / 2];
173
174 const SHA512_BLOCK_BYTE_LEN: usize = 128;
175 const SHA512_HASH_BYTE_LEN: usize = 64;
176 const SHA512_HASH_WORDS_NUM: usize = SHA512_HASH_BYTE_LEN / size_of::<u64>();
177 const SHA512_BLOCK_WORDS_NUM: usize = SHA512_BLOCK_BYTE_LEN / size_of::<u64>();