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

sp-core-hashing: use the digest::Digest trait to handle the hashing function uniformly #10835

Merged
merged 6 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 36 additions & 18 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion primitives/api/proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc-macro = true
quote = "1.0.10"
syn = { version = "1.0.82", features = ["full", "fold", "extra-traits", "visit"] }
proc-macro2 = "1.0.36"
blake2-rfc = { version = "0.2.18", default-features = false }
blake2 = { version = "0.10.2", default-features = false }
proc-macro-crate = "1.1.0"

# Required for the doc tests
Expand Down
6 changes: 3 additions & 3 deletions primitives/api/proc-macro/src/decl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ use syn::{

use std::collections::HashMap;

use blake2_rfc;

/// The ident used for the block generic parameter.
const BLOCK_GENERIC_IDENT: &str = "Block";

Expand Down Expand Up @@ -750,8 +748,10 @@ fn parse_runtime_api_version(version: &Attribute) -> Result<u64> {
/// Generates the identifier as const variable for the given `trait_name`
/// by hashing the `trait_name`.
fn generate_runtime_api_id(trait_name: &str) -> TokenStream {
use blake2::digest::{consts::U8, Digest};

let mut res = [0; 8];
res.copy_from_slice(blake2_rfc::blake2b::blake2b(8, &[], trait_name.as_bytes()).as_bytes());
res.copy_from_slice(blake2::Blake2b::<U8>::digest(trait_name).as_slice());

quote!( const ID: [u8; 8] = [ #( #res ),* ]; )
}
Expand Down
9 changes: 0 additions & 9 deletions primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ bitflags = "1.3"
# full crypto
ed25519-dalek = { version = "1.0.1", default-features = false, features = ["u64_backend", "alloc"], optional = true }
blake2-rfc = { version = "0.2.18", default-features = false, optional = true }
tiny-keccak = { version = "2.0.1", features = ["keccak"], optional = true }
schnorrkel = { version = "0.9.1", features = [
"preaudit_deprecated",
"u64_backend",
], default-features = false, optional = true }
sha2 = { version = "0.10.0", default-features = false, optional = true }
hex = { version = "0.4", default-features = false, optional = true }
twox-hash = { version = "1.6.2", default-features = false, optional = true }
libsecp256k1 = { version = "0.7", default-features = false, features = ["hmac", "static-context"], optional = true }
merlin = { version = "2.0", default-features = false, optional = true }
ss58-registry = { version = "1.11.0", default-features = false }
Expand Down Expand Up @@ -98,7 +95,6 @@ std = [
"hash-db/std",
"sp-std/std",
"serde",
"twox-hash/std",
"blake2-rfc/std",
"ed25519-dalek/std",
"hex/std",
Expand All @@ -107,11 +103,9 @@ std = [
"tiny-bip39",
"byteorder/std",
"rand",
"sha2/std",
"schnorrkel/std",
"regex",
"num-traits/std",
"tiny-keccak",
"sp-core-hashing/std",
"sp-debug-derive/std",
"sp-externalities",
Expand All @@ -132,11 +126,8 @@ std = [
full_crypto = [
"ed25519-dalek",
"blake2-rfc",
"tiny-keccak",
"schnorrkel",
"hex",
"sha2",
"twox-hash",
"libsecp256k1",
"sp-core-hashing",
"sp-runtime-interface/disable_target_static_assertions",
Expand Down
14 changes: 8 additions & 6 deletions primitives/core/hashing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ targets = ["x86_64-unknown-linux-gnu"]
sp-std = { version = "4.0.0", default-features = false, path = "../../std" }
byteorder = { version = "1.3.2", default-features = false }

blake2-rfc = { version = "0.2.18", default-features = false }
tiny-keccak = { version = "2.0.1", features = ["keccak"] }
sha2 = { version = "0.10.0", default-features = false }
twox-hash = { version = "1.6.2", default-features = false }
digest = { version = "0.10.2", default-features = false }
blake2 = { version = "0.10.2", default-features = false }
sha2 = { version = "0.10.1", default-features = false }
sha3 = { version = "0.10.0", default-features = false }
twox-hash = { version = "1.6.2", default-features = false, features = ["digest_0_10"] }

[features]
default = ["std"]
std = [
"blake2-rfc/std",
"sha2/std",
"sp-std/std",
"blake2/std",
"sha2/std",
"sha3/std",
"twox-hash/std",
]
66 changes: 24 additions & 42 deletions primitives/core/hashing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

use sha2::{Digest, Sha256};
use tiny_keccak::{Hasher, Keccak};
use core::hash::Hasher;

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

/// 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_rfc::blake2b::blake2b(64, &[], data).as_bytes());
dest.copy_from_slice(blake2::Blake2b512::digest(data).as_slice());
}

/// Do a Blake2 512-bit hash and return result.
Expand All @@ -37,7 +42,8 @@ pub fn blake2_512(data: &[u8]) -> [u8; 64] {

/// Do a Blake2 256-bit hash and place result in `dest`.
pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
type Blake2b256 = blake2::Blake2b<U32>;
dest.copy_from_slice(Blake2b256::digest(data).as_slice());
}

/// Do a Blake2 256-bit hash and return result.
Expand All @@ -49,7 +55,8 @@ pub fn blake2_256(data: &[u8]) -> [u8; 32] {

/// Do a Blake2 128-bit hash and place result in `dest`.
pub fn blake2_128_into(data: &[u8], dest: &mut [u8; 16]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(16, &[], data).as_bytes());
type Blake2b128 = blake2::Blake2b<U16>;
dest.copy_from_slice(Blake2b128::digest(data).as_slice());
}

/// Do a Blake2 128-bit hash and return result.
Expand All @@ -61,7 +68,8 @@ pub fn blake2_128(data: &[u8]) -> [u8; 16] {

/// Do a Blake2 64-bit hash and place result in `dest`.
pub fn blake2_64_into(data: &[u8], dest: &mut [u8; 8]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(8, &[], data).as_bytes());
type Blake2b64 = blake2::Blake2b<U8>;
dest.copy_from_slice(Blake2b64::digest(data).as_slice());
}

/// Do a Blake2 64-bit hash and return result.
Expand All @@ -73,11 +81,7 @@ pub fn blake2_64(data: &[u8]) -> [u8; 8] {

/// Do a XX 64-bit hash and place result in `dest`.
pub fn twox_64_into(data: &[u8], dest: &mut [u8; 8]) {
use core::hash::Hasher;
let mut h0 = twox_hash::XxHash::with_seed(0);
h0.write(data);
let r0 = h0.finish();
use byteorder::{ByteOrder, LittleEndian};
let r0 = twox_hash::XxHash::with_seed(0).chain_update(data).finish();
LittleEndian::write_u64(&mut dest[0..8], r0);
}

Expand All @@ -90,14 +94,8 @@ pub fn twox_64(data: &[u8]) -> [u8; 8] {

/// Do a XX 128-bit hash and place result in `dest`.
pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) {
use core::hash::Hasher;
let mut h0 = twox_hash::XxHash::with_seed(0);
let mut h1 = twox_hash::XxHash::with_seed(1);
h0.write(data);
h1.write(data);
let r0 = h0.finish();
let r1 = h1.finish();
use byteorder::{ByteOrder, LittleEndian};
let r0 = twox_hash::XxHash::with_seed(0).chain_update(data).finish();
let r1 = twox_hash::XxHash::with_seed(1).chain_update(data).finish();
LittleEndian::write_u64(&mut dest[0..8], r0);
LittleEndian::write_u64(&mut dest[8..16], r1);
}
Expand All @@ -111,20 +109,10 @@ pub fn twox_128(data: &[u8]) -> [u8; 16] {

/// Do a XX 256-bit hash and place result in `dest`.
pub fn twox_256_into(data: &[u8], dest: &mut [u8; 32]) {
use ::core::hash::Hasher;
use byteorder::{ByteOrder, LittleEndian};
let mut h0 = twox_hash::XxHash::with_seed(0);
let mut h1 = twox_hash::XxHash::with_seed(1);
let mut h2 = twox_hash::XxHash::with_seed(2);
let mut h3 = twox_hash::XxHash::with_seed(3);
h0.write(data);
h1.write(data);
h2.write(data);
h3.write(data);
let r0 = h0.finish();
let r1 = h1.finish();
let r2 = h2.finish();
let r3 = h3.finish();
let r0 = twox_hash::XxHash::with_seed(0).chain_update(data).finish();
let r1 = twox_hash::XxHash::with_seed(1).chain_update(data).finish();
let r2 = twox_hash::XxHash::with_seed(2).chain_update(data).finish();
let r3 = twox_hash::XxHash::with_seed(3).chain_update(data).finish();
LittleEndian::write_u64(&mut dest[0..8], r0);
LittleEndian::write_u64(&mut dest[8..16], r1);
LittleEndian::write_u64(&mut dest[16..24], r2);
Expand All @@ -140,27 +128,21 @@ pub fn twox_256(data: &[u8]) -> [u8; 32] {

/// Do a keccak 256-bit hash and return result.
pub fn keccak_256(data: &[u8]) -> [u8; 32] {
let mut keccak = Keccak::v256();
keccak.update(data);
let mut output = [0u8; 32];
keccak.finalize(&mut output);
output.copy_from_slice(sha3::Keccak256::digest(data).as_slice());
output
}

/// Do a keccak 512-bit hash and return result.
pub fn keccak_512(data: &[u8]) -> [u8; 64] {
let mut keccak = Keccak::v512();
keccak.update(data);
let mut output = [0u8; 64];
keccak.finalize(&mut output);
output.copy_from_slice(sha3::Keccak512::digest(data).as_slice());
output
}

/// Do a sha2 256-bit hash and return result.
pub fn sha2_256(data: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(data);
let mut output = [0u8; 32];
output.copy_from_slice(&hasher.finalize());
output.copy_from_slice(sha2::Sha256::digest(data).as_slice());
output
}
Loading