Skip to content

Commit

Permalink
POC: AVX2 sha256
Browse files Browse the repository at this point in the history
Before:
```
test bench1_10    ... bench:          39 ns/iter (+/- 2) = 256 MB/s
test bench2_100   ... bench:         349 ns/iter (+/- 23) = 286 MB/s
test bench3_1000  ... bench:       3,412 ns/iter (+/- 11) = 293 MB/s
test bench4_10000 ... bench:      34,084 ns/iter (+/- 3,183) = 293 MB/s
```

After:
```
itest bench1_10    ... bench:          27 ns/iter (+/- 1) = 370 MB/s
test bench2_100   ... bench:         232 ns/iter (+/- 5) = 431 MB/s
test bench3_1000  ... bench:       2,082 ns/iter (+/- 85) = 480 MB/s
test bench4_10000 ... bench:      20,543 ns/iter (+/- 2,686) = 486 MB/s
```
  • Loading branch information
haraldh committed Mar 18, 2021
1 parent 58345c4 commit 911d5f7
Show file tree
Hide file tree
Showing 3 changed files with 784 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sha2/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
let (sha256_path, sha512_path) = if target_arch == "x86" {
("src/sha256_x86.S", "src/sha512_x86.S")
} else if target_arch == "x86_64" {
("src/sha256_x64.S", "src/sha512_x64.S")
("src/sha256-avx2-asm.S", "src/sha512_x64.S")
} else if target_arch == "aarch64" {
build256.flag("-march=armv8-a+crypto");
("src/sha256_aarch64.S", "")
Expand Down
6 changes: 4 additions & 2 deletions sha2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
compile_error!("crate can only be used on x86, x86-64 and aarch64 architectures");

#[link(name = "sha256", kind = "static")]
#[allow(dead_code)]
extern "C" {
fn sha256_compress(state: &mut [u32; 8], block: &[u8; 64]);
fn sha256_transform_rorx(state: &mut [u32; 8], block: *const [u8; 64], num_blocks: 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) }
if !blocks.is_empty() {
unsafe { sha256_transform_rorx(state, blocks.as_ptr(), blocks.len()) }
}
}

Expand Down
Loading

0 comments on commit 911d5f7

Please sign in to comment.