| 1 |
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1}; |
| 2 |
use crate::consts::K32; |
| 3 |
|
| 4 |
#[target_feature(enable = "zknh")] |
| 5 |
pub(in super::super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) { |
| 6 |
for block in blocks.iter().map(super::utils::load_block) { |
| 7 |
compress_block(state, block); |
| 8 |
} |
| 9 |
} |
| 10 |
|
| 11 |
#[target_feature(enable = "zknh")] |
| 12 |
fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) { |
| 13 |
let mut s = *state; |
| 14 |
|
| 15 |
for r in 0..64 { |
| 16 |
round(&mut s, &block, r); |
| 17 |
if r < 48 { |
| 18 |
schedule(&mut block, r) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
for i in 0..8 { |
| 23 |
state[i] = state[i].wrapping_add(s[i]); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
#[target_feature(enable = "zknh")] |
| 28 |
fn schedule(block: &mut [u32; 16], r: usize) { |
| 29 |
block[r % 16] = block[r % 16] |
| 30 |
.wrapping_add(sha256sig1(block[(r + 14) % 16])) |
| 31 |
.wrapping_add(block[(r + 9) % 16]) |
| 32 |
.wrapping_add(sha256sig0(block[(r + 1) % 16])); |
| 33 |
} |
| 34 |
|
| 35 |
#[target_feature(enable = "zknh")] |
| 36 |
fn round(state: &mut [u32; 8], block: &[u32; 16], r: usize) { |
| 37 |
let n = K32.len() - r; |
| 38 |
#[allow(clippy::identity_op)] |
| 39 |
let a = (n + 0) % 8; |
| 40 |
let b = (n + 1) % 8; |
| 41 |
let c = (n + 2) % 8; |
| 42 |
let d = (n + 3) % 8; |
| 43 |
let e = (n + 4) % 8; |
| 44 |
let f = (n + 5) % 8; |
| 45 |
let g = (n + 6) % 8; |
| 46 |
let h = (n + 7) % 8; |
| 47 |
|
| 48 |
state[h] = state[h] |
| 49 |
.wrapping_add(sha256sum1(state[e])) |
| 50 |
.wrapping_add(ch(state[e], state[f], state[g])) |
| 51 |
.wrapping_add(K32[r]) |
| 52 |
.wrapping_add(block[r % 16]); |
| 53 |
state[d] = state[d].wrapping_add(state[h]); |
| 54 |
state[h] = state[h] |
| 55 |
.wrapping_add(sha256sum0(state[a])) |
| 56 |
.wrapping_add(maj(state[a], state[b], state[c])) |
| 57 |
} |
| 58 |
|
| 59 |
#[inline(always)] |
| 60 |
fn ch(x: u32, y: u32, z: u32) -> u32 { |
| 61 |
(x & y) ^ (!x & z) |
| 62 |
} |
| 63 |
|
| 64 |
#[inline(always)] |
| 65 |
fn maj(x: u32, y: u32, z: u32) -> u32 { |
| 66 |
(x & y) ^ (x & z) ^ (y & z) |
| 67 |
} |