Debian Codegraph @

debian/rust-vendor/sha2/src/sha256/riscv_zknh/unroll.rs

raw
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 s = &mut state.clone();
14 let b = &mut block;
15
16 for i in 0..3 {
17 let k = &K32[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 = &K32[48..];
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 [u32; 8], block: &mut [u32; 16], k: &[u32]) {
61 round::<R>(state, block, k);
62
63 block[R] = block[R]
64 .wrapping_add(sha256sig1(block[(R + 14) % 16]))
65 .wrapping_add(block[(R + 9) % 16])
66 .wrapping_add(sha256sig0(block[(R + 1) % 16]));
67 }
68
69 #[target_feature(enable = "zknh")]
70 fn round<const R: usize>(state: &mut [u32; 8], block: &[u32; 16], k: &[u32]) {
71 let n = K32.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(sha256sum1(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(sha256sum0(state[a]))
90 .wrapping_add(maj(state[a], state[b], state[c]))
91 }
92
93 #[inline(always)]
94 fn ch(x: u32, y: u32, z: u32) -> u32 {
95 (x & y) ^ (!x & z)
96 }
97
98 #[inline(always)]
99 fn maj(x: u32, y: u32, z: u32) -> u32 {
100 (x & y) ^ (x & z) ^ (y & z)
101 }