Skip to content

Commit e8b3abe

Browse files
feat(sha2): use latest sha2-asm and enable M1 (#261)
1 parent ee361ed commit e8b3abe

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

Cargo.lock

Lines changed: 16 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ members = [
2020

2121
[profile.dev]
2222
opt-level = 2
23+
24+
[patch.crates-io]
25+
sha2-asm = { git = "https://github.com/RustCrypto/asm-hashes" }

sha2/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ digest = "0.9"
1919
block-buffer = "0.9"
2020
opaque-debug = "0.3"
2121
cfg-if = "1.0"
22-
sha2-asm = { version = "0.5", optional = true }
22+
sha2-asm = { version = "0.6", optional = true }
2323

2424
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
2525
cpuid-bool = "0.2"
@@ -38,3 +38,4 @@ asm = ["sha2-asm", "libc"]
3838
compress = [] # Expose compress function
3939
force-soft = [] # Force software implementation
4040
asm-aarch64 = ["asm"] # DEPRECATED: use `asm` instead
41+

sha2/src/sha256.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,12 @@ cfg_if::cfg_if! {
148148
#[cfg(feature = "asm")]
149149
mod soft {
150150
pub(crate) fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
151-
for block in blocks {
152-
sha2_asm::compress256(state, block);
153-
}
151+
sha2_asm::compress256(state, blocks);
154152
}
155153
}
156154
mod x86;
157155
use x86::compress;
158-
} else if #[cfg(all(feature = "asm", target_arch = "aarch64", target_os = "linux"))] {
156+
} else if #[cfg(all(feature = "asm", target_arch = "aarch64", any(target_os = "macos", target_os = "linux")))] {
159157
mod soft;
160158
mod aarch64;
161159
use aarch64::compress;

sha2/src/sha256/aarch64.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
use libc::{getauxval, AT_HWCAP, HWCAP_SHA2};
2-
1+
#[cfg(target_os = "linux")]
32
#[inline(always)]
43
pub fn sha2_supported() -> bool {
4+
use libc::{getauxval, AT_HWCAP, HWCAP_SHA2};
5+
56
let hwcaps: u64 = unsafe { getauxval(AT_HWCAP) };
67
(hwcaps & HWCAP_SHA2) != 0
78
}
89

10+
#[cfg(target_os = "macos")]
11+
#[inline(always)]
12+
pub fn sha2_supported() -> bool {
13+
// TODO: Use cpufeatures once support lands
14+
true
15+
}
16+
917
pub fn compress(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
1018
// TODO: Replace with https://github.com/rust-lang/rfcs/pull/2725
1119
// after stabilization
1220
if sha2_supported() {
13-
for block in blocks {
14-
sha2_asm::compress256(state, block);
15-
}
21+
sha2_asm::compress256(state, blocks);
1622
} else {
1723
super::soft::compress(state, blocks);
1824
}

sha2/src/sha512.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ cfg_if::cfg_if! {
233233
use soft::compress;
234234
} else if #[cfg(all(feature = "asm", any(target_arch = "x86", target_arch = "x86_64")))] {
235235
fn compress(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
236-
for block in blocks {
237-
sha2_asm::compress512(state, block);
238-
}
236+
sha2_asm::compress512(state, blocks);
239237
}
240238
} else {
241239
mod soft;

0 commit comments

Comments
 (0)