Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement much faster sha256 and sha512. #41

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sha2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,15 @@ keywords = ["crypto", "sha2", "asm"]
categories = ["cryptography", "no-std"]
edition = "2018"

[dependencies]
cpufeatures = "0.2.1"
0xdeafbeef marked this conversation as resolved.
Show resolved Hide resolved

[build-dependencies]
cc = "1.0"

[dev-dependencies]
criterion = {version= "0.3.5",features=["html_reports"] }
0xdeafbeef marked this conversation as resolved.
Show resolved Hide resolved

[[bench]]
name = "sha2"
harness = false
31 changes: 0 additions & 31 deletions sha2/benches/lib.rs

This file was deleted.

17 changes: 17 additions & 0 deletions sha2/benches/sha2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn criterion_benchmark(c: &mut Criterion) {
let mut state = Default::default();
let data = [[0u8; 64]];
c.bench_function("sha256", |b| {
b.iter(|| sha2_asm::compress256(black_box(&mut state), black_box(&data)))
});
let mut state = Default::default();
let data = [[0u8; 128]];
c.bench_function("sha512", |b| {
b.iter(|| sha2_asm::compress512(black_box(&mut state), black_box(&data)))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
23 changes: 17 additions & 6 deletions sha2/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@ fn main() {

let mut build256 = cc::Build::new();
let (sha256_path, sha512_path) = if target_arch == "x86" {
("src/sha256_x86.S", "src/sha512_x86.S")
(["src/sha256_x86.S"].to_vec(), ["src/sha512_x86.S"].to_vec())
0xdeafbeef marked this conversation as resolved.
Show resolved Hide resolved
} else if target_arch == "x86_64" {
("src/sha256_x64.S", "src/sha512_x64.S")
let sha512 = ["src/sha512_x64_avx2.S", "src/sha512_x64.S"].to_vec();
// Prioritizing sha-ni, cause it's fastest
let sha256 = [
"src/sha256_x64_ni.S",
"src/sha256_x64_avx2.S",
"src/sha256_x64.S",
]
.to_vec();
(sha256, sha512)
} else if target_arch == "aarch64" && target_vendor == "apple" {
build256.flag("-march=armv8-a+crypto");
("src/sha256_aarch64_apple.S", "")
(["src/sha256_aarch64_apple.S"].to_vec(), [""].to_vec())
} else if target_arch == "aarch64" {
build256.flag("-march=armv8-a+crypto");
("src/sha256_aarch64.S", "")
(["src/sha256_aarch64.S"].to_vec(), [""].to_vec())
} else {
panic!("Unsupported target architecture");
};

if target_arch != "aarch64" {
cc::Build::new()
.flag("-c")
.file(sha512_path)
.files(sha512_path)
.compile("libsha512.a");
}
build256.flag("-c").file(sha256_path).compile("libsha256.a");
build256
.flag("-c")
.files(sha256_path)
.compile("libsha256.a");
}
39 changes: 35 additions & 4 deletions sha2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,47 @@
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
compile_error!("crate can only be used on x86, x86-64 and aarch64 architectures");

cpufeatures::new!(cpuid_avx2, "avx2");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gate this line on #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]. Otherwise it causes compilation failure on Aarch64 targets.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to modify the compress256 function (see the CI failure). Currently it tries to use the cpuid_avx2 module on all targets. I think the easiest solution would be to introduce two function with the same name one gated on x86(-64) and another one on AArch64.

cpufeatures::new!(cpuid_sha, "sha");

#[link(name = "sha256", kind = "static")]
#[allow(dead_code)]
extern "C" {
fn sha256_compress(state: &mut [u32; 8], block: &[u8; 64]);
// setting usize is safe, cause we are on 64 bit platform
fn sha256_transform_rorx(state: &mut [u32; 8], block: *const [u8; 64], num_blocks: usize);
0xdeafbeef marked this conversation as resolved.
Show resolved Hide resolved
fn sha256_ni_transform(digest: &mut [u32; 8], data: *const [u8; 64], nblk: usize);
}

/// Safe wrapper around assembly implementation of SHA256 compression function
///
#[inline]
pub fn compress256(state: &mut [u32; 8], blocks: &[[u8; 64]]) {
for block in blocks {
unsafe { sha256_compress(state, block) }
let token_sha = cpuid_sha::init();
if token_sha.get() {
if !blocks.is_empty() {
unsafe { sha256_ni_transform(state, blocks.as_ptr(), blocks.len()) }
}
return;
}
let token: cpuid_avx2::InitToken = cpuid_avx2::init();

if token.get() {
0xdeafbeef marked this conversation as resolved.
Show resolved Hide resolved
if !blocks.is_empty() {
unsafe { sha256_transform_rorx(state, blocks.as_ptr(), blocks.len()) }
}
} else {
for block in blocks {
unsafe { sha256_compress(state, block) }
}
}
}

#[cfg(not(target_arch = "aarch64"))]
#[link(name = "sha512", kind = "static")]
extern "C" {
fn sha512_compress(state: &mut [u64; 8], block: &[u8; 128]);
fn sha512_transform_rorx(state: &mut [u64; 8], block: *const [u8; 128], num_blocks: usize);
}

/// Safe wrapper around assembly implementation of SHA512 compression function
Expand All @@ -38,7 +62,14 @@ extern "C" {
#[cfg(not(target_arch = "aarch64"))]
#[inline]
pub fn compress512(state: &mut [u64; 8], blocks: &[[u8; 128]]) {
for block in blocks {
unsafe { sha512_compress(state, block) }
let token: cpuid_avx2::InitToken = cpuid_avx2::init();
if token.get() {
if !blocks.is_empty() {
unsafe { sha512_transform_rorx(state, blocks.as_ptr(), blocks.len()) }
}
} else {
for block in blocks {
unsafe { sha512_compress(state, block) }
}
}
}
Loading