Skip to content

Commit 50a7ea6

Browse files
authored
sha2: refactor riscv_zknh backends (#929)
By following the software backend code the changes remove (unreachable) panic branches from the compact variants and reduce total amount of generated binary code. Unroll backends now unroll round loops fully instead of partially and prevent unnecessary caching of round constants to stack. Resulting assembly: | | Old | New | |------------------|--------------------------------------|--------------------------------------| | SHA-256 compact | https://rust.godbolt.org/z/784781hhE | https://rust.godbolt.org/z/MabKT4ssW | | SHA-256 unrolled | https://rust.godbolt.org/z/5Gb6rTzqM | https://rust.godbolt.org/z/1ccjaeEYE | | SHA-512 compact | https://rust.godbolt.org/z/4TPoG98j1 | https://rust.godbolt.org/z/T14dovc6s | | SHA-512 unrolled | https://rust.godbolt.org/z/1sGrKjxE8 | https://rust.godbolt.org/z/3nffnb15T |
1 parent 7a1f8db commit 50a7ea6

7 files changed

Lines changed: 445 additions & 273 deletions

File tree

‎sha2/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Respect precedence of the `sha2_backend` configuration flag over `sha2_256/512_backend` ([#948])
1212

1313
### Changed
14+
- Refactored `riscv_zknh` backends ([#929])
1415
- Improved performance of the SHA-512 `x86-avx2` backend ([#940], [#943])
1516

17+
[#929]: https://github.com/RustCrypto/hashes/pull/929
1618
[#935]: https://github.com/RustCrypto/hashes/pull/935
1719
[#940]: https://github.com/RustCrypto/hashes/pull/940
1820
[#943]: https://github.com/RustCrypto/hashes/pull/943

‎sha2/src/sha256/riscv_zknh.rs‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,9 @@ use core::arch::riscv64::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
99
cfg_if::cfg_if! {
1010
if #[cfg(sha2_backend_riscv_zknh = "compact")] {
1111
mod compact;
12-
use compact::compress_block;
12+
pub(super) use compact::compress;
1313
} else {
1414
mod unroll;
15-
use unroll::compress_block;
15+
pub(super) use unroll::compress;
1616
}
1717
}
18-
19-
#[target_feature(enable = "zknh")]
20-
pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
21-
for block in blocks.iter().map(load_block) {
22-
compress_block(state, block);
23-
}
24-
}
25-
26-
fn load_block(block: &[u8; 64]) -> [u32; 16] {
27-
let (chunks, tail) = block.as_chunks();
28-
assert!(tail.is_empty());
29-
core::array::from_fn(|i| u32::from_be_bytes(chunks[i]))
30-
}
Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,82 @@
1+
//! RISC-V backend which uses the Zknh extension and does not perform manual loop unrolling.
2+
//!
3+
//! Generated assembly (for `opt-level=s`):
4+
//! RV32: https://rust.godbolt.org/z/MabKT4ssW
5+
//! RV64: https://rust.godbolt.org/z/bf7sf1zEe
16
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
27
use crate::consts::K32;
38

4-
#[inline]
9+
const ROUNDS: usize = K32.len();
10+
511
#[target_feature(enable = "zknh")]
6-
pub(super) fn compress_block(state: &mut [u32; 8], mut block: [u32; 16]) {
7-
let mut s = *state;
8-
9-
for r in 0..64 {
10-
round(&mut s, &block, r);
11-
if r < 48 {
12-
schedule(&mut block, r)
13-
}
14-
}
12+
pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
13+
// Unforuntately, LLVM is unable to remove `memset` for
14+
// `fn message_schedule(block: &[u8; 64]) -> [u32; ROUNDS] { ... }`
15+
// See: https://rust.godbolt.org/z/s61sP3GKj
16+
// So to prevent unnecessary `memset` call for each block we reuse the buffer.
17+
let mut msg = [0u32; ROUNDS];
1518

16-
for i in 0..8 {
17-
state[i] = state[i].wrapping_add(s[i]);
19+
for block in blocks {
20+
// Pre-computing the whole message schedule instead of doing it during each round
21+
// with a ring buffer results in a smaller binary with less branching
22+
// at the cost of using additional stack space
23+
schedule(block, &mut msg);
24+
rounds(state, &msg);
1825
}
1926
}
2027

2128
#[inline]
2229
#[target_feature(enable = "zknh")]
23-
fn schedule(block: &mut [u32; 16], r: usize) {
24-
block[r % 16] = block[r % 16]
25-
.wrapping_add(sha256sig1(block[(r + 14) % 16]))
26-
.wrapping_add(block[(r + 9) % 16])
27-
.wrapping_add(sha256sig0(block[(r + 1) % 16]));
30+
fn schedule(block: &[u8; 64], msg: &mut [u32; ROUNDS]) {
31+
let (block_chunks, tail) = block.as_chunks();
32+
assert!(tail.is_empty());
33+
34+
let n = block_chunks.len();
35+
for i in 0..n {
36+
msg[i] = u32::from_be_bytes(block_chunks[i]);
37+
}
38+
39+
for i in n..ROUNDS {
40+
msg[i] = msg[i - 16]
41+
.wrapping_add(sha256sig0(msg[i - 15]))
42+
.wrapping_add(msg[i - 7])
43+
.wrapping_add(sha256sig1(msg[i - 2]));
44+
}
2845
}
2946

3047
#[inline]
3148
#[target_feature(enable = "zknh")]
32-
fn round(state: &mut [u32; 8], block: &[u32; 16], r: usize) {
33-
let n = K32.len() - r;
34-
let [a, b, c, d, e, f, g, h] = core::array::from_fn(|i| (n + i) % 8);
35-
36-
state[h] = state[h]
37-
.wrapping_add(sha256sum1(state[e]))
38-
.wrapping_add(ch(state[e], state[f], state[g]))
39-
.wrapping_add(K32[r])
40-
.wrapping_add(block[r % 16]);
41-
state[d] = state[d].wrapping_add(state[h]);
42-
state[h] = state[h]
43-
.wrapping_add(sha256sum0(state[a]))
44-
.wrapping_add(maj(state[a], state[b], state[c]))
45-
}
49+
fn rounds(state: &mut [u32; 8], msg: &[u32; ROUNDS]) {
50+
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
4651

47-
#[inline(always)]
48-
fn ch(x: u32, y: u32, z: u32) -> u32 {
49-
(x & y) ^ (!x & z)
50-
}
52+
for i in 0..ROUNDS {
53+
let s1 = sha256sum1(e);
54+
let ch = (e & f) ^ ((!e) & g);
55+
let t1 = s1
56+
.wrapping_add(ch)
57+
.wrapping_add(K32[i])
58+
.wrapping_add(msg[i])
59+
.wrapping_add(h);
60+
let s0 = sha256sum0(a);
61+
let maj = (a & b) ^ (a & c) ^ (b & c);
62+
let t2 = s0.wrapping_add(maj);
63+
64+
h = g;
65+
g = f;
66+
f = e;
67+
e = d.wrapping_add(t1);
68+
d = c;
69+
c = b;
70+
b = a;
71+
a = t1.wrapping_add(t2);
72+
}
5173

52-
#[inline(always)]
53-
fn maj(x: u32, y: u32, z: u32) -> u32 {
54-
(x & y) ^ (x & z) ^ (y & z)
74+
state[0] = state[0].wrapping_add(a);
75+
state[1] = state[1].wrapping_add(b);
76+
state[2] = state[2].wrapping_add(c);
77+
state[3] = state[3].wrapping_add(d);
78+
state[4] = state[4].wrapping_add(e);
79+
state[5] = state[5].wrapping_add(f);
80+
state[6] = state[6].wrapping_add(g);
81+
state[7] = state[7].wrapping_add(h);
5582
}

0 commit comments

Comments
 (0)