blake2b_simd
An implementation of the BLAKE2b hash with:
- 100% stable Rust.
- A SIMD implementation based on Samuel Neves'
blake2-avx2
. This implementation is very fast. See the Performance section below. - A portable, safe implementation for other platforms.
- Dynamic CPU feature detection. Binaries for x86 include SIMD implementations by default and use the fastest implementation the processor supports.
- All the features from the the BLAKE2 spec, like adjustable length, keying, and associated data for tree hashing.
- A clone of the Coreutils
b2sum
command line utility, provided as a sub-crate.b2sum
includes command line flags for all the BLAKE2 associated data features. no_std
support. Thestd
Cargo feature is on by default, for CPU feature detection and for implementingstd::io::Write
.- The SIMD-friendly BLAKE2bp variant. This implementation is single-threaded, but it's faster
than BLAKE2b, because it uses AVX2 more efficiently. It's available on the command line as
b2sum --blake2bp
. - Support for computing multiple BLAKE2b hashes in parallel. See
many::hash_many
andmany::update_many
. These interfaces match the efficiency of BLAKE2bp but produce BLAKE2b hashes. They're a building block for the Bao project.
Example
use ;
let expected = "ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6d\
c1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d";
let hash = blake2b;
assert_eq!;
let hash = new
.hash_length
.key
.personal
.to_state
.update
.update
.update
.finalize;
assert_eq!;
An example using the included b2sum
command line utility:
|
)
Performance
To run small benchmarks yourself, first install OpenSSL and libsodium on your machine, then:
# Use --no-default-features if you're missing OpenSSL or libsodium.
The benches/bench_multiprocess
sub-crate runs various hash functions on
long inputs in memory and tries to average over many sources of
variability. Here are the results from my laptop for cargo run --release
:
- Intel Core i5-8250U, Arch Linux, kernel version 5.0.13
- libsodium version 1.0.17
- OpenSSL version 1.1.1.b
- rustc 1.34.1
╭─────────────────────────┬────────────╮
│ blake2b_simd many::hash │ 2.223 GB/s │
│ blake2b_simd BLAKE2bp │ 2.211 GB/s │
│ sneves BLAKE2bp │ 2.143 GB/s │
│ blake2b_simd BLAKE2b │ 1.008 GB/s │
│ OpenSSL SHA-1 │ 0.971 GB/s │
│ sneves BLAKE2b │ 0.949 GB/s │
│ libsodium BLAKE2b │ 0.940 GB/s │
│ blake2b_simd portable │ 0.812 GB/s │
│ OpenSSL SHA-512 │ 0.666 GB/s │
╰─────────────────────────┴────────────╯
The benches/bench_b2sum.py
script benchmarks b2sum
against several
Coreutils hashes, on a 1 GB file of random data. Here are the results from
my laptop:
╭───────────────────────────────┬────────────╮
│ blake2b_simd b2sum --blake2bp │ 1.517 GB/s │
│ blake2b_simd b2sum │ 0.820 GB/s │
│ coreutils sha1sum │ 0.805 GB/s │
│ coreutils b2sum │ 0.668 GB/s │
│ coreutils md5sum │ 0.595 GB/s │
│ coreutils sha512sum │ 0.593 GB/s │
╰───────────────────────────────┴────────────╯