Debian Codegraph @

debian/rust-vendor/sha2/src/sha512/riscv_zknh/compact.rs

raw
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 mut s = *state;
14
15 for r in 0..80 {
16 round(&mut s, &block, r);
17 if r < 64 {
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 [u64; 16], r: usize) {
29 block[r % 16] = block[r % 16]
30 .wrapping_add(sha512sig1(block[(r + 14) % 16]))
31 .wrapping_add(block[(r + 9) % 16])
32 .wrapping_add(sha512sig0(block[(r + 1) % 16]));
33 }
34
35 #[target_feature(enable = "zknh")]
36 fn round(state: &mut [u64; 8], block: &[u64; 16], r: usize) {
37 let n = K64.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(sha512sum1(state[e]))
50 .wrapping_add(ch(state[e], state[f], state[g]))
51 .wrapping_add(K64[r])
52 .wrapping_add(block[r % 16]);
53 state[d] = state[d].wrapping_add(state[h]);
54 state[h] = state[h]
55 .wrapping_add(sha512sum0(state[a]))
56 .wrapping_add(maj(state[a], state[b], state[c]))
57 }
58
59 #[inline(always)]
60 fn ch(x: u64, y: u64, z: u64) -> u64 {
61 (x & y) ^ (!x & z)
62 }
63
64 #[inline(always)]
65 fn maj(x: u64, y: u64, z: u64) -> u64 {
66 (x & y) ^ (x & z) ^ (y & z)
67 }