Skip to content

Commit

Permalink
sha2(wasm): factor out cast_ms
Browse files Browse the repository at this point in the history
  • Loading branch information
max-te committed Nov 1, 2024
1 parent 601ca6e commit 4478789
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
24 changes: 11 additions & 13 deletions sha2/src/sha256/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ unsafe fn rounds_0_47(current_state: &mut State, x: &mut [v128; 4], ms: &mut Msg
let y = sha256_update_x(x, k32);

{
let ms = cast_ms(ms);
sha_round(current_state, ms[4 * j]);
sha_round(current_state, ms[4 * j + 1]);
sha_round(current_state, ms[4 * j + 2]);
sha_round(current_state, ms[4 * j + 3]);
let ms = ms[j];
sha_round(current_state, u32x4_extract_lane::<0>(ms));
sha_round(current_state, u32x4_extract_lane::<1>(ms));
sha_round(current_state, u32x4_extract_lane::<2>(ms));
sha_round(current_state, u32x4_extract_lane::<3>(ms));
}

ms[j] = y;
Expand All @@ -65,9 +65,12 @@ unsafe fn rounds_0_47(current_state: &mut State, x: &mut [v128; 4], ms: &mut Msg

#[inline(always)]
fn rounds_48_63(current_state: &mut State, ms: &MsgSchedule) {
let ms = cast_ms(ms);
for i in 48..64 {
sha_round(current_state, ms[i & 0xf]);
for j in 0..4 {
let ms = ms[j];
sha_round(current_state, u32x4_extract_lane::<0>(ms));
sha_round(current_state, u32x4_extract_lane::<1>(ms));
sha_round(current_state, u32x4_extract_lane::<2>(ms));
sha_round(current_state, u32x4_extract_lane::<3>(ms));
}
}

Expand Down Expand Up @@ -178,11 +181,6 @@ unsafe fn sha256_update_x(x: &mut [v128; 4], k32: v128) -> v128 {
u32x4_add(x[3], k32)
}

#[inline(always)]
fn cast_ms(ms: &MsgSchedule) -> &[u32; SHA256_BLOCK_WORDS_NUM] {
unsafe { &*(ms.as_ptr().cast()) }
}

type State = [u32; SHA256_HASH_WORDS_NUM];
type MsgSchedule = [v128; SHA256_BLOCK_WORDS_NUM / 4];

Expand Down
18 changes: 7 additions & 11 deletions sha2/src/sha512/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ unsafe fn rounds_0_63(current_state: &mut State, x: &mut [v128; 8], ms: &mut Msg
let y = sha512_update_x(x, k64);

{
let ms = cast_ms(ms);
sha_round(current_state, ms[2 * j]);
sha_round(current_state, ms[2 * j + 1]);
let ms = ms[j];
sha_round(current_state, u64x2_extract_lane::<0>(ms));
sha_round(current_state, u64x2_extract_lane::<1>(ms));
}

ms[j] = y;
Expand All @@ -62,9 +62,10 @@ unsafe fn rounds_0_63(current_state: &mut State, x: &mut [v128; 8], ms: &mut Msg

#[inline(always)]
fn rounds_64_79(current_state: &mut State, ms: &MsgSchedule) {
let ms = cast_ms(ms);
for i in 64..80 {
sha_round(current_state, ms[i & 0xf]);
for j in 0..8 {
let ms = ms[j];
sha_round(current_state, u64x2_extract_lane::<0>(ms));
sha_round(current_state, u64x2_extract_lane::<1>(ms));
}
}

Expand Down Expand Up @@ -162,11 +163,6 @@ unsafe fn sha512_update_x(x: &mut [v128; 8], k64: v128) -> v128 {
i64x2_add(x[7], k64)
}

#[inline(always)]
fn cast_ms(ms: &MsgSchedule) -> &[u64; SHA512_BLOCK_WORDS_NUM] {
unsafe { &*(ms.as_ptr().cast()) }
}

type State = [u64; SHA512_HASH_WORDS_NUM];
type MsgSchedule = [v128; SHA512_BLOCK_WORDS_NUM / 2];

Expand Down

0 comments on commit 4478789

Please sign in to comment.