| 1 |
use super::{sha512sig0, sha512sig1, sha512sum0, sha512sum1}; |
| 2 |
use crate::consts::K64; |
| 3 |
|
| 4 |
#[target_feature(enable = "zknh")] |
| 5 |
pub(in super::super) fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) { |
| 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 [u64; 8], mut block: [u64; 16]) { |
| 13 |
let s = &mut state.clone(); |
| 14 |
let b = &mut block; |
| 15 |
|
| 16 |
for i in 0..4 { |
| 17 |
let k = &K64[16 * i..]; |
| 18 |
round_schedule::<0>(s, b, k); |
| 19 |
round_schedule::<1>(s, b, k); |
| 20 |
round_schedule::<2>(s, b, k); |
| 21 |
round_schedule::<3>(s, b, k); |
| 22 |
round_schedule::<4>(s, b, k); |
| 23 |
round_schedule::<5>(s, b, k); |
| 24 |
round_schedule::<6>(s, b, k); |
| 25 |
round_schedule::<7>(s, b, k); |
| 26 |
round_schedule::<8>(s, b, k); |
| 27 |
round_schedule::<9>(s, b, k); |
| 28 |
round_schedule::<10>(s, b, k); |
| 29 |
round_schedule::<11>(s, b, k); |
| 30 |
round_schedule::<12>(s, b, k); |
| 31 |
round_schedule::<13>(s, b, k); |
| 32 |
round_schedule::<14>(s, b, k); |
| 33 |
round_schedule::<15>(s, b, k); |
| 34 |
} |
| 35 |
|
| 36 |
let k = &K64[64..]; |
| 37 |
round::<0>(s, b, k); |
| 38 |
round::<1>(s, b, k); |
| 39 |
round::<2>(s, b, k); |
| 40 |
round::<3>(s, b, k); |
| 41 |
round::<4>(s, b, k); |
| 42 |
round::<5>(s, b, k); |
| 43 |
round::<6>(s, b, k); |
| 44 |
round::<7>(s, b, k); |
| 45 |
round::<8>(s, b, k); |
| 46 |
round::<9>(s, b, k); |
| 47 |
round::<10>(s, b, k); |
| 48 |
round::<11>(s, b, k); |
| 49 |
round::<12>(s, b, k); |
| 50 |
round::<13>(s, b, k); |
| 51 |
round::<14>(s, b, k); |
| 52 |
round::<15>(s, b, k); |
| 53 |
|
| 54 |
for i in 0..8 { |
| 55 |
state[i] = state[i].wrapping_add(s[i]); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[target_feature(enable = "zknh")] |
| 60 |
fn round_schedule<const R: usize>(state: &mut [u64; 8], block: &mut [u64; 16], k: &[u64]) { |
| 61 |
round::<R>(state, block, k); |
| 62 |
|
| 63 |
block[R] = block[R] |
| 64 |
.wrapping_add(sha512sig1(block[(R + 14) % 16])) |
| 65 |
.wrapping_add(block[(R + 9) % 16]) |
| 66 |
.wrapping_add(sha512sig0(block[(R + 1) % 16])); |
| 67 |
} |
| 68 |
|
| 69 |
#[target_feature(enable = "zknh")] |
| 70 |
fn round<const R: usize>(state: &mut [u64; 8], block: &[u64; 16], k: &[u64]) { |
| 71 |
let n = K64.len() - R; |
| 72 |
#[allow(clippy::identity_op)] |
| 73 |
let a = (n + 0) % 8; |
| 74 |
let b = (n + 1) % 8; |
| 75 |
let c = (n + 2) % 8; |
| 76 |
let d = (n + 3) % 8; |
| 77 |
let e = (n + 4) % 8; |
| 78 |
let f = (n + 5) % 8; |
| 79 |
let g = (n + 6) % 8; |
| 80 |
let h = (n + 7) % 8; |
| 81 |
|
| 82 |
state[h] = state[h] |
| 83 |
.wrapping_add(sha512sum1(state[e])) |
| 84 |
.wrapping_add(ch(state[e], state[f], state[g])) |
| 85 |
.wrapping_add(super::utils::opaque_load::<R>(k)) |
| 86 |
.wrapping_add(block[R]); |
| 87 |
state[d] = state[d].wrapping_add(state[h]); |
| 88 |
state[h] = state[h] |
| 89 |
.wrapping_add(sha512sum0(state[a])) |
| 90 |
.wrapping_add(maj(state[a], state[b], state[c])) |
| 91 |
} |
| 92 |
|
| 93 |
#[inline(always)] |
| 94 |
fn ch(x: u64, y: u64, z: u64) -> u64 { |
| 95 |
(x & y) ^ (!x & z) |
| 96 |
} |
| 97 |
|
| 98 |
#[inline(always)] |
| 99 |
fn maj(x: u64, y: u64, z: u64) -> u64 { |
| 100 |
(x & y) ^ (x & z) ^ (y & z) |
| 101 |
} |