Skip to content

Commit a567b76

Browse files
authored
sha2: refactor software backends (#949)
Follows changes introduced in #929.
1 parent ed176fb commit a567b76

20 files changed

Lines changed: 481 additions & 306 deletions

‎sha2/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
### Changed
1414
- Refactored `riscv_zknh` backends ([#929])
1515
- Improved performance of the SHA-512 `x86-avx2` backend ([#940], [#943], [#951])
16+
- Refactored `soft` backends ([#949])
1617

1718
[#929]: https://github.com/RustCrypto/hashes/pull/929
1819
[#935]: https://github.com/RustCrypto/hashes/pull/935
1920
[#940]: https://github.com/RustCrypto/hashes/pull/940
2021
[#943]: https://github.com/RustCrypto/hashes/pull/943
2122
[#948]: https://github.com/RustCrypto/hashes/pull/948
23+
[#949]: https://github.com/RustCrypto/hashes/pull/949
2224
[#951]: https://github.com/RustCrypto/hashes/pull/951
2325

2426
## 0.11.1 (2026-10-12)

‎sha2/src/consts.rs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
pub(crate) type State256 = [u32; 8];
22
pub(crate) type State512 = [u64; 8];
33

4+
/// Initial hash values for SHA-224
45
pub(crate) const H256_224: State256 = [
56
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
67
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
78
];
89

10+
/// Initial hash values for SHA-256
911
pub(crate) const H256_256: State256 = [
1012
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
1113
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
1214
];
1315

16+
/// Initial hash values for SHA-512/224
1417
pub(crate) const H512_224: State512 = [
1518
0x8c3d37c819544da2, 0x73e1996689dcd4d6, 0x1dfab7ae32ff9c82, 0x679dd514582f9fcf,
1619
0x0f6d2b697bd44da8, 0x77e36f7304c48942, 0x3f9d85a86a1d36c8, 0x1112e6ad91d692a1,
1720
];
1821

22+
/// Initial hash values for SHA-512/256
1923
pub(crate) const H512_256: State512 = [
2024
0x22312194fc2bf72c, 0x9f555fa3c84c64c2, 0x2393b86b6f53b151, 0x963877195940eabd,
2125
0x96283ee2a88effe3, 0xbe5e1e2553863992, 0x2b0199fc2c85b8aa, 0x0eb72ddc81c52ca2,
2226
];
2327

28+
/// Initial hash values for SHA-384
2429
pub(crate) const H512_384: State512 = [
2530
0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
2631
0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4,
2732
];
2833

34+
/// Initial hash values for SHA-512
2935
pub(crate) const H512_512: State512 = [
3036
0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
3137
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
3238
];
3339

34-
/// Round constants for SHA-256 family of digests
35-
pub(crate) static K32: [u32; 64] = [
40+
/// Number of SHA-256 rounds
41+
pub(crate) const SHA256_ROUNDS: usize = 64;
42+
43+
/// SHA-256 round constants
44+
pub(crate) static SHA256_RK: [u32; SHA256_ROUNDS] = [
3645
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
3746
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
3847
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@@ -43,8 +52,11 @@ pub(crate) static K32: [u32; 64] = [
4352
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
4453
];
4554

46-
/// Round constants for SHA-512 family of digests
47-
pub(crate) const K64: [u64; 80] = [
55+
/// Number of SHA-512 rounds
56+
pub(crate) const SHA512_ROUNDS: usize = 80;
57+
58+
/// SHA-512 round constants
59+
pub(crate) const SHA512_RK: [u64; SHA512_ROUNDS] = [
4860
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
4961
0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
5062
0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,

‎sha2/src/sha256/aarch64_sha2.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
8686
#[inline]
8787
#[target_feature(enable = "neon")]
8888
fn rk(i: usize) -> uint32x4_t {
89-
let chunk = &crate::consts::K32[i..][..4];
89+
let chunk = &crate::consts::SHA256_RK[i..][..4];
9090
unsafe { vld1q_u32(chunk.as_ptr()) }
9191
}
9292

‎sha2/src/sha256/loongarch64_asm.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub(super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
208208
in("$a0") state,
209209
inout("$a1") blocks.as_ptr() => _,
210210
inout("$a2") blocks.len() => _,
211-
in("$a3") crate::consts::K32.as_ptr(),
211+
in("$a3") crate::consts::SHA256_RK.as_ptr(),
212212

213213
// Clobbers
214214
out("$a4") _,

‎sha2/src/sha256/riscv_zknh/compact.rs‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
//! RV32: https://rust.godbolt.org/z/MabKT4ssW
55
//! RV64: https://rust.godbolt.org/z/bf7sf1zEe
66
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
7-
use crate::consts::K32;
8-
9-
const ROUNDS: usize = K32.len();
7+
use crate::consts::{SHA256_RK, SHA256_ROUNDS};
108

119
#[target_feature(enable = "zknh")]
1210
pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
1311
// Unforuntately, LLVM is unable to remove `memset` for
1412
// `fn message_schedule(block: &[u8; 64]) -> [u32; ROUNDS] { ... }`
1513
// See: https://rust.godbolt.org/z/s61sP3GKj
1614
// So to prevent unnecessary `memset` call for each block we reuse the buffer.
17-
let mut msg = [0u32; ROUNDS];
15+
let mut msg = [0u32; SHA256_ROUNDS];
1816

1917
for block in blocks {
2018
// Pre-computing the whole message schedule instead of doing it during each round
@@ -27,7 +25,7 @@ pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
2725

2826
#[inline]
2927
#[target_feature(enable = "zknh")]
30-
fn schedule(block: &[u8; 64], msg: &mut [u32; ROUNDS]) {
28+
fn schedule(block: &[u8; 64], msg: &mut [u32; SHA256_ROUNDS]) {
3129
let (block_chunks, tail) = block.as_chunks();
3230
assert!(tail.is_empty());
3331

@@ -36,7 +34,7 @@ fn schedule(block: &[u8; 64], msg: &mut [u32; ROUNDS]) {
3634
msg[i] = u32::from_be_bytes(block_chunks[i]);
3735
}
3836

39-
for i in n..ROUNDS {
37+
for i in n..SHA256_ROUNDS {
4038
msg[i] = msg[i - 16]
4139
.wrapping_add(sha256sig0(msg[i - 15]))
4240
.wrapping_add(msg[i - 7])
@@ -46,15 +44,15 @@ fn schedule(block: &[u8; 64], msg: &mut [u32; ROUNDS]) {
4644

4745
#[inline]
4846
#[target_feature(enable = "zknh")]
49-
fn rounds(state: &mut [u32; 8], msg: &[u32; ROUNDS]) {
47+
fn rounds(state: &mut [u32; 8], msg: &[u32; SHA256_ROUNDS]) {
5048
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
5149

52-
for i in 0..ROUNDS {
50+
for i in 0..SHA256_ROUNDS {
5351
let s1 = sha256sum1(e);
5452
let ch = (e & f) ^ ((!e) & g);
5553
let t1 = s1
5654
.wrapping_add(ch)
57-
.wrapping_add(K32[i])
55+
.wrapping_add(SHA256_RK[i])
5856
.wrapping_add(msg[i])
5957
.wrapping_add(h);
6058
let s0 = sha256sum0(a);

‎sha2/src/sha256/riscv_zknh/unroll.rs‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! RISC-V backend which uses the Zknh extension and fully unrolls round loop.
22
//!
33
//! Generated assembly:
4-
//! RV32: https://rust.godbolt.org/z/1ccjaeEYE
5-
//! RV64: https://rust.godbolt.org/z/Y71ovfh8P
4+
//! RV32: https://rust.godbolt.org/z/jv9jxqs9n
5+
//! RV64: https://rust.godbolt.org/z/E6M7b9zWx
66
use super::{sha256sig0, sha256sig1, sha256sum0, sha256sum1};
7-
use crate::consts::K32;
87

98
#[target_feature(enable = "zknh")]
109
pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
@@ -132,29 +131,24 @@ fn round<const R: usize>(state: &mut [u32; 8], w: u32) {
132131
let maj = (a & b) ^ (a & c) ^ (b & c);
133132
let t2 = s0.wrapping_add(maj);
134133

135-
state[7] = g;
136-
state[6] = f;
137-
state[5] = e;
138-
state[4] = d.wrapping_add(t1);
139-
state[3] = c;
140-
state[2] = b;
141-
state[1] = a;
142-
state[0] = t1.wrapping_add(t2);
134+
*state = [t1.wrapping_add(t2), a, b, c, d.wrapping_add(t1), e, f, g];
143135
}
144136

145137
// We intentionally add round constant instead of simply loading it
146138
// to prevent the compiler from applying unnecessary optimizations
147139
#[inline(always)]
148140
fn add_rk<const R: usize>(mut w: u32) -> u32 {
149-
const { assert!(R < K32.len()) };
141+
use crate::consts::SHA256_RK;
142+
143+
const { assert!(R < SHA256_RK.len()) };
150144

151145
#[cfg(target_arch = "riscv64")]
152146
unsafe {
153147
core::arch::asm!(
154148
"lwu {rk}, 4*{R}({rk_ptr})",
155149
"addw {w}, {w}, {rk}",
156150
R = const R,
157-
rk_ptr = in(reg) K32.as_ptr(),
151+
rk_ptr = in(reg) SHA256_RK.as_ptr(),
158152
rk = out(reg) _,
159153
w = inout(reg) w,
160154
options(pure, readonly, nostack, preserves_flags),
@@ -167,7 +161,7 @@ fn add_rk<const R: usize>(mut w: u32) -> u32 {
167161
"lw {rk}, 4*{R}({rk_ptr})",
168162
"add {w}, {w}, {rk}",
169163
R = const R,
170-
rk_ptr = in(reg) K32.as_ptr(),
164+
rk_ptr = in(reg) SHA256_RK.as_ptr(),
171165
rk = out(reg) _,
172166
w = inout(reg) w,
173167
options(pure, readonly, nostack, preserves_flags),

‎sha2/src/sha256/soft.rs‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,32 @@ cfg_if::cfg_if! {
88
}
99
}
1010

11-
fn to_u32s(block: &[u8; 64]) -> [u32; 16] {
12-
core::array::from_fn(|i| {
13-
let chunk = block[4 * i..][..4].try_into().unwrap();
14-
u32::from_be_bytes(chunk)
15-
})
11+
#[inline(always)]
12+
fn sig0(w: u32) -> u32 {
13+
(w.rotate_right(7)) ^ (w.rotate_right(18)) ^ (w >> 3)
14+
}
15+
16+
#[inline(always)]
17+
fn sig1(w: u32) -> u32 {
18+
(w.rotate_right(17)) ^ (w.rotate_right(19)) ^ (w >> 10)
19+
}
20+
21+
#[inline(always)]
22+
fn sum0(w: u32) -> u32 {
23+
w.rotate_right(2) ^ w.rotate_right(13) ^ w.rotate_right(22)
24+
}
25+
26+
#[inline(always)]
27+
fn sum1(w: u32) -> u32 {
28+
w.rotate_right(6) ^ w.rotate_right(11) ^ w.rotate_right(25)
29+
}
30+
31+
#[inline(always)]
32+
fn ch(e: u32, f: u32, g: u32) -> u32 {
33+
(e & f) ^ ((!e) & g)
34+
}
35+
36+
#[inline(always)]
37+
fn maj(a: u32, b: u32, c: u32) -> u32 {
38+
(a & b) ^ (a & c) ^ (b & c)
1639
}

‎sha2/src/sha256/soft/compact.rs‎

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
1-
use crate::consts::K32;
1+
//! Software backend without manual loop unrolling
2+
//!
3+
//! Generated assembly: https://rust.godbolt.org/z/fno5qd6sK
4+
use super::{ch, maj, sig0, sig1, sum0, sum1};
5+
use crate::consts::{SHA256_RK, SHA256_ROUNDS};
26

3-
fn compress_u32(state: &mut [u32; 8], mut block: [u32; 16]) {
7+
pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
8+
// Unforuntately, LLVM is unable to remove `memset` for
9+
// `fn message_schedule(block: &[u8; 64]) -> [u32; ROUNDS] { ... }`
10+
// See: https://rust.godbolt.org/z/s61sP3GKj
11+
// So to prevent unnecessary `memset` call for each block we reuse the buffer.
12+
let mut msg = [0u32; SHA256_ROUNDS];
13+
14+
for block in blocks {
15+
// Pre-computing the whole message schedule instead of doing it during each round
16+
// with a ring buffer results in a smaller binary with less branching
17+
// at the cost of using additional stack space
18+
schedule(block, &mut msg);
19+
rounds(state, &msg);
20+
}
21+
}
22+
23+
#[inline]
24+
fn schedule(block: &[u8; 64], msg: &mut [u32; SHA256_ROUNDS]) {
25+
let (block_chunks, tail) = block.as_chunks();
26+
assert!(tail.is_empty());
27+
28+
let n = block_chunks.len();
29+
for i in 0..n {
30+
msg[i] = u32::from_be_bytes(block_chunks[i]);
31+
}
32+
33+
for i in n..SHA256_ROUNDS {
34+
msg[i] = msg[i - 16]
35+
.wrapping_add(sig0(msg[i - 15]))
36+
.wrapping_add(msg[i - 7])
37+
.wrapping_add(sig1(msg[i - 2]));
38+
}
39+
}
40+
41+
#[inline]
42+
fn rounds(state: &mut [u32; 8], msg: &[u32; SHA256_ROUNDS]) {
443
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
544

6-
for i in 0..64 {
7-
let w = if i < 16 {
8-
block[i]
9-
} else {
10-
let w15 = block[(i - 15) % 16];
11-
let s0 = (w15.rotate_right(7)) ^ (w15.rotate_right(18)) ^ (w15 >> 3);
12-
let w2 = block[(i - 2) % 16];
13-
let s1 = (w2.rotate_right(17)) ^ (w2.rotate_right(19)) ^ (w2 >> 10);
14-
let new_w = block[(i - 16) % 16]
15-
.wrapping_add(s0)
16-
.wrapping_add(block[(i - 7) % 16])
17-
.wrapping_add(s1);
18-
block[i % 16] = new_w;
19-
new_w
20-
};
21-
22-
let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
23-
let ch = (e & f) ^ ((!e) & g);
24-
let t1 = s1
25-
.wrapping_add(ch)
26-
.wrapping_add(K32[i])
27-
.wrapping_add(w)
45+
for i in 0..SHA256_ROUNDS {
46+
let t1 = sum1(e)
47+
.wrapping_add(ch(e, f, g))
48+
.wrapping_add(SHA256_RK[i])
49+
.wrapping_add(msg[i])
2850
.wrapping_add(h);
29-
let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
30-
let maj = (a & b) ^ (a & c) ^ (b & c);
31-
let t2 = s0.wrapping_add(maj);
51+
let t2 = sum0(a).wrapping_add(maj(a, b, c));
3252

3353
h = g;
3454
g = f;
@@ -49,9 +69,3 @@ fn compress_u32(state: &mut [u32; 8], mut block: [u32; 16]) {
4969
state[6] = state[6].wrapping_add(g);
5070
state[7] = state[7].wrapping_add(h);
5171
}
52-
53-
pub(in super::super) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
54-
for block in blocks.iter() {
55-
compress_u32(state, super::to_u32s(block));
56-
}
57-
}

0 commit comments

Comments
 (0)