| 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::K32; |
| 12 |
|
| 13 |
pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) { |
| 14 |
assert_eq!(SHA256_HASH_WORDS_NUM, 8); |
| 15 |
assert_eq!(SHA256_BLOCK_WORDS_NUM, 16); |
| 16 |
let mut ms = [u64x2(0, 0); 4]; |
| 17 |
let mut x = [u64x2(0, 0); 4]; |
| 18 |
|
| 19 |
for block in blocks { |
| 20 |
unsafe { |
| 21 |
let mut current_state = *state; |
| 22 |
load_data(&mut x, &mut ms, block.as_ptr().cast()); |
| 23 |
rounds_0_47(&mut current_state, &mut x, &mut ms); |
| 24 |
rounds_48_63(&mut current_state, &ms); |
| 25 |
accumulate_state(state, ¤t_state); |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
#[inline(always)] |
| 31 |
unsafe fn load_data(x: &mut [v128; 4], ms: &mut MsgSchedule, data: *const v128) { |
| 32 |
macro_rules! unrolled_iterations { |
| 33 |
($($i:literal),*) => {$( |
| 34 |
x[$i] = v128_load(data.add($i).cast()); |
| 35 |
x[$i] = i8x16_shuffle::<3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12>(x[$i], x[$i]); |
| 36 |
|
| 37 |
let y = i32x4_add( |
| 38 |
x[$i], |
| 39 |
v128_load(K32.as_ptr().add(4 * $i).cast()), |
| 40 |
); |
| 41 |
|
| 42 |
ms[$i] = y; |
| 43 |
)*}; |
| 44 |
} |
| 45 |
|
| 46 |
unrolled_iterations!(0, 1, 2, 3); |
| 47 |
} |
| 48 |
|
| 49 |
#[inline(always)] |
| 50 |
unsafe fn rounds_0_47(current_state: &mut State, x: &mut [v128; 4], ms: &mut MsgSchedule) { |
| 51 |
let mut k32_idx: usize = SHA256_BLOCK_WORDS_NUM; |
| 52 |
|
| 53 |
for _ in 0..3 { |
| 54 |
for j in 0..4 { |
| 55 |
let k32 = v128_load(K32.as_ptr().add(k32_idx).cast()); |
| 56 |
let y = sha256_update_x(x, k32); |
| 57 |
|
| 58 |
{ |
| 59 |
let ms = ms[j]; |
| 60 |
sha_round(current_state, u32x4_extract_lane::<0>(ms)); |
| 61 |
sha_round(current_state, u32x4_extract_lane::<1>(ms)); |
| 62 |
sha_round(current_state, u32x4_extract_lane::<2>(ms)); |
| 63 |
sha_round(current_state, u32x4_extract_lane::<3>(ms)); |
| 64 |
} |
| 65 |
|
| 66 |
ms[j] = y; |
| 67 |
k32_idx += 4; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
#[inline(always)] |
| 73 |
fn rounds_48_63(current_state: &mut State, ms: &MsgSchedule) { |
| 74 |
for j in 0..4 { |
| 75 |
let ms = ms[j]; |
| 76 |
sha_round(current_state, u32x4_extract_lane::<0>(ms)); |
| 77 |
sha_round(current_state, u32x4_extract_lane::<1>(ms)); |
| 78 |
sha_round(current_state, u32x4_extract_lane::<2>(ms)); |
| 79 |
sha_round(current_state, u32x4_extract_lane::<3>(ms)); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[inline(always)] |
| 84 |
fn sha_round(s: &mut State, x: u32) { |
| 85 |
macro_rules! big_sigma0 { |
| 86 |
($a:expr) => { |
| 87 |
$a.rotate_right(2) ^ $a.rotate_right(13) ^ $a.rotate_right(22) |
| 88 |
}; |
| 89 |
} |
| 90 |
macro_rules! big_sigma1 { |
| 91 |
($a:expr) => { |
| 92 |
$a.rotate_right(6) ^ $a.rotate_right(11) ^ $a.rotate_right(25) |
| 93 |
}; |
| 94 |
} |
| 95 |
macro_rules! bool3ary_202 { |
| 96 |
($a:expr, $b:expr, $c:expr) => { |
| 97 |
$c ^ ($a & ($b ^ $c)) |
| 98 |
}; |
| 99 |
} // Choose, MD5F, SHA1C |
| 100 |
macro_rules! bool3ary_232 { |
| 101 |
($a:expr, $b:expr, $c:expr) => { |
| 102 |
($a & $b) ^ ($a & $c) ^ ($b & $c) |
| 103 |
}; |
| 104 |
} // Majority, SHA1M |
| 105 |
|
| 106 |
macro_rules! rotate_state { |
| 107 |
($s:ident) => {{ |
| 108 |
let tmp = $s[7]; |
| 109 |
$s[7] = $s[6]; |
| 110 |
$s[6] = $s[5]; |
| 111 |
$s[5] = $s[4]; |
| 112 |
$s[4] = $s[3]; |
| 113 |
$s[3] = $s[2]; |
| 114 |
$s[2] = $s[1]; |
| 115 |
$s[1] = $s[0]; |
| 116 |
$s[0] = tmp; |
| 117 |
}}; |
| 118 |
} |
| 119 |
|
| 120 |
let t = x |
| 121 |
.wrapping_add(s[7]) |
| 122 |
.wrapping_add(big_sigma1!(s[4])) |
| 123 |
.wrapping_add(bool3ary_202!(s[4], s[5], s[6])); |
| 124 |
|
| 125 |
s[7] = t |
| 126 |
.wrapping_add(big_sigma0!(s[0])) |
| 127 |
.wrapping_add(bool3ary_232!(s[0], s[1], s[2])); |
| 128 |
s[3] = s[3].wrapping_add(t); |
| 129 |
|
| 130 |
rotate_state!(s); |
| 131 |
} |
| 132 |
|
| 133 |
#[inline(always)] |
| 134 |
fn accumulate_state(dst: &mut State, src: &State) { |
| 135 |
for i in 0..SHA256_HASH_WORDS_NUM { |
| 136 |
dst[i] = dst[i].wrapping_add(src[i]); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#[inline(always)] |
| 141 |
unsafe fn sha256_update_x(x: &mut [v128; 4], k32: v128) -> v128 { |
| 142 |
const SIGMA0_0: u32 = 7; |
| 143 |
const SIGMA0_1: u32 = 18; |
| 144 |
const SIGMA0_2: u32 = 3; |
| 145 |
const SIGMA1_0: u32 = 17; |
| 146 |
const SIGMA1_1: u32 = 19; |
| 147 |
const SIGMA1_2: u32 = 10; |
| 148 |
const SHA256_WORD_BIT_LEN: u32 = 8 * size_of::<u32>() as u32; |
| 149 |
const ZERO: v128 = u64x2(0, 0); |
| 150 |
|
| 151 |
let mut t0 = u32x4_shuffle::<1, 2, 3, 4>(x[0], x[1]); |
| 152 |
let mut t3 = u32x4_shuffle::<1, 2, 3, 4>(x[2], x[3]); |
| 153 |
let mut t2 = u32x4_shr(t0, SIGMA0_0); |
| 154 |
x[0] = u32x4_add(x[0], t3); |
| 155 |
t3 = u32x4_shr(t0, SIGMA0_2); |
| 156 |
let mut t1 = u32x4_shl(t0, SHA256_WORD_BIT_LEN - SIGMA0_1); |
| 157 |
t0 = v128_xor(t3, t2); |
| 158 |
t3 = u32x4_shuffle::<2, 2, 3, 3>(x[3], x[3]); |
| 159 |
t2 = u32x4_shr(t2, SIGMA0_1 - SIGMA0_0); |
| 160 |
t0 = v128_xor(t0, t1); |
| 161 |
t0 = v128_xor(t0, t2); |
| 162 |
t1 = u32x4_shl(t1, SIGMA0_1 - SIGMA0_0); |
| 163 |
t2 = u32x4_shr(t3, SIGMA1_2); |
| 164 |
t3 = u64x2_shr(t3, SIGMA1_0); |
| 165 |
t1 = v128_xor(t0, t1); |
| 166 |
x[0] = u32x4_add(x[0], t1); |
| 167 |
t2 = v128_xor(t2, t3); |
| 168 |
t3 = u64x2_shr(t3, SIGMA1_1 - SIGMA1_0); |
| 169 |
t2 = v128_xor(t2, t3); |
| 170 |
t2 = u32x4_shuffle::<0, 2, 7, 7>(t2, ZERO); |
| 171 |
x[0] = u32x4_add(x[0], t2); |
| 172 |
t3 = u32x4_shuffle::<0, 0, 1, 1>(x[0], x[0]); |
| 173 |
t2 = u32x4_shr(t3, SIGMA1_2); |
| 174 |
t3 = u64x2_shr(t3, SIGMA1_0); |
| 175 |
t2 = v128_xor(t2, t3); |
| 176 |
t3 = u64x2_shr(t3, SIGMA1_1 - SIGMA1_0); |
| 177 |
t2 = v128_xor(t2, t3); |
| 178 |
t2 = u32x4_shuffle::<7, 7, 0, 2>(t2, ZERO); |
| 179 |
x[0] = u32x4_add(x[0], t2); |
| 180 |
|
| 181 |
let tmp = x[0]; |
| 182 |
x[0] = x[1]; |
| 183 |
x[1] = x[2]; |
| 184 |
x[2] = x[3]; |
| 185 |
x[3] = tmp; |
| 186 |
|
| 187 |
u32x4_add(x[3], k32) |
| 188 |
} |
| 189 |
|
| 190 |
type State = [u32; SHA256_HASH_WORDS_NUM]; |
| 191 |
type MsgSchedule = [v128; SHA256_BLOCK_WORDS_NUM / 4]; |
| 192 |
|
| 193 |
const SHA256_BLOCK_BYTE_LEN: usize = 64; |
| 194 |
const SHA256_HASH_BYTE_LEN: usize = 32; |
| 195 |
const SHA256_HASH_WORDS_NUM: usize = SHA256_HASH_BYTE_LEN / size_of::<u32>(); |
| 196 |
const SHA256_BLOCK_WORDS_NUM: usize = SHA256_BLOCK_BYTE_LEN / size_of::<u32>(); |