Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Switch to the blake2b_simd crate in sp-core-hashing #13548

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions primitives/core/hashing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ documentation = "https://docs.rs/sp-core-hashing"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
blake2 = { version = "0.10.4", default-features = false }
blake2b_simd = { version = "1.0.1", default-features = false }
byteorder = { version = "1.3.2", default-features = false }
digest = { version = "0.10.3", default-features = false }
sha2 = { version = "0.10.2", default-features = false }
Expand All @@ -25,7 +25,7 @@ sp-std = { version = "5.0.0", default-features = false, path = "../../std" }
default = ["std"]
std = [
"digest/std",
"blake2/std",
"blake2b_simd/std",
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
"byteorder/std",
"sha2/std",
"sha3/std",
Expand Down
25 changes: 16 additions & 9 deletions primitives/core/hashing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,41 @@
use core::hash::Hasher;

use byteorder::{ByteOrder, LittleEndian};
use digest::{
consts::{U16, U32, U8},
Digest,
};
use digest::Digest;

#[inline(always)]
fn blake2<const N: usize>(data: &[u8]) -> [u8; N] {
blake2b_simd::Params::new()
.hash_length(N)
.hash(data)
.as_bytes()
.try_into()
.expect("slice is always the necessary length")
}

/// Do a Blake2 512-bit hash and place result in `dest`.
pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) {
dest.copy_from_slice(blake2::Blake2b512::digest(data).as_slice());
*dest = blake2(data);
}

/// Do a Blake2 512-bit hash and return result.
pub fn blake2_512(data: &[u8]) -> [u8; 64] {
blake2::Blake2b512::digest(data).into()
blake2(data)
}

/// Do a Blake2 256-bit hash and return result.
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
blake2::Blake2b::<U32>::digest(data).into()
blake2(data)
}

/// Do a Blake2 128-bit hash and return result.
pub fn blake2_128(data: &[u8]) -> [u8; 16] {
blake2::Blake2b::<U16>::digest(data).into()
blake2(data)
}

/// Do a Blake2 64-bit hash and return result.
pub fn blake2_64(data: &[u8]) -> [u8; 8] {
blake2::Blake2b::<U8>::digest(data).into()
blake2(data)
}

/// Do a XX 64-bit hash and place result in `dest`.
Expand Down