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

* sp-core-hashing: use sha3 instead of tiny-keccak

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* use blake2 instead of blake2-rfc

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* improve som hashing of sp-core and sp-api-proc-macro

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Some nits

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* cargo fmt

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2022-02-16 16:26:53 +08:00
committed by GitHub
parent ee6223327c
commit 962cab60d6
8 changed files with 74 additions and 89 deletions
+36 -18
View File
@@ -641,6 +641,15 @@ dependencies = [
"opaque-debug 0.3.0",
]
[[package]]
name = "blake2"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94ba84325db59637ffc528bbe8c7f86c02c57cff5c0e2b9b00f9a851f42f309"
dependencies = [
"digest 0.10.2",
]
[[package]]
name = "blake2-rfc"
version = "0.2.18"
@@ -1383,9 +1392,9 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "crypto-common"
version = "0.1.1"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0"
checksum = "a4600d695eb3f6ce1cd44e6e291adceb2cc3ab12f20a33777ecd0bf6eba34e06"
dependencies = [
"generic-array 0.14.4",
]
@@ -1603,13 +1612,13 @@ dependencies = [
[[package]]
name = "digest"
version = "0.10.1"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b"
checksum = "8cb780dce4f9a8f5c087362b3a4595936b2019e7c8b30f2c3e9a7e94e6ae9837"
dependencies = [
"block-buffer 0.10.0",
"crypto-common",
"generic-array 0.14.4",
"subtle",
]
[[package]]
@@ -3852,7 +3861,7 @@ dependencies = [
"pin-project 1.0.10",
"rand 0.7.3",
"salsa20",
"sha3",
"sha3 0.9.1",
]
[[package]]
@@ -4491,7 +4500,7 @@ dependencies = [
"generic-array 0.14.4",
"multihash-derive",
"sha2 0.9.8",
"sha3",
"sha3 0.9.1",
"unsigned-varint 0.5.1",
]
@@ -9210,13 +9219,13 @@ dependencies = [
[[package]]
name = "sha2"
version = "0.10.0"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900d964dd36bb15bcf2f2b35694c072feab74969a54f2bbeec7a2d725d2bdcb6"
checksum = "99c3bd8169c58782adad9290a9af5939994036b76187f7b4f0e6de91dbbfc0ec"
dependencies = [
"cfg-if 1.0.0",
"cpufeatures 0.2.1",
"digest 0.10.1",
"digest 0.10.2",
]
[[package]]
@@ -9231,6 +9240,16 @@ dependencies = [
"opaque-debug 0.3.0",
]
[[package]]
name = "sha3"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31f935e31cf406e8c0e96c2815a5516181b7004ae8c5f296293221e9b1e356bd"
dependencies = [
"digest 0.10.2",
"keccak",
]
[[package]]
name = "sharded-slab"
version = "0.1.1"
@@ -9317,7 +9336,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6142f7c25e94f6fd25a32c3348ec230df9109b463f59c8c7acc4bd34936babb7"
dependencies = [
"aes-gcm",
"blake2",
"blake2 0.9.1",
"chacha20poly1305",
"rand 0.8.4",
"rand_core 0.6.2",
@@ -9401,7 +9420,7 @@ dependencies = [
name = "sp-api-proc-macro"
version = "4.0.0-dev"
dependencies = [
"blake2-rfc",
"blake2 0.10.2",
"proc-macro-crate 1.1.0",
"proc-macro2",
"quote",
@@ -9655,7 +9674,6 @@ dependencies = [
"secrecy",
"serde",
"serde_json",
"sha2 0.10.0",
"sp-core-hashing",
"sp-core-hashing-proc-macro",
"sp-debug-derive",
@@ -9668,8 +9686,6 @@ dependencies = [
"substrate-bip39",
"thiserror",
"tiny-bip39",
"tiny-keccak",
"twox-hash",
"wasmi",
"zeroize",
]
@@ -9678,11 +9694,12 @@ dependencies = [
name = "sp-core-hashing"
version = "4.0.0"
dependencies = [
"blake2-rfc",
"blake2 0.10.2",
"byteorder",
"sha2 0.10.0",
"digest 0.10.2",
"sha2 0.10.1",
"sha3 0.10.0",
"sp-std",
"tiny-keccak",
"twox-hash",
]
@@ -11153,6 +11170,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ee73e6e4924fe940354b8d4d98cad5231175d615cd855b758adc658c0aac6a0"
dependencies = [
"cfg-if 1.0.0",
"digest 0.10.2",
"rand 0.8.4",
"static_assertions",
]
@@ -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
@@ -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";
@@ -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 ),* ]; )
}
-9
View File
@@ -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 }
@@ -98,7 +95,6 @@ std = [
"hash-db/std",
"sp-std/std",
"serde",
"twox-hash/std",
"blake2-rfc/std",
"ed25519-dalek/std",
"hex/std",
@@ -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",
@@ -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",
+8 -6
View File
@@ -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",
]
+24 -42
View File
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -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);
}
@@ -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);
}
@@ -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);
@@ -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
}
+1 -5
View File
@@ -348,11 +348,7 @@ impl<'a> TryFrom<&'a Signature> for (libsecp256k1::Signature, libsecp256k1::Reco
/// Derive a single hard junction.
#[cfg(feature = "full_crypto")]
fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed {
("Secp256k1HDKD", secret_seed, cc).using_encoded(|data| {
let mut res = [0u8; 32];
res.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
res
})
("Secp256k1HDKD", secret_seed, cc).using_encoded(|data| sp_core_hashing::blake2_256(data))
}
/// An error when deriving a key.
+1 -5
View File
@@ -396,11 +396,7 @@ impl From<&Public> for CryptoTypePublicPair {
/// Derive a single hard junction.
#[cfg(feature = "full_crypto")]
fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed {
("Ed25519HDKD", secret_seed, cc).using_encoded(|data| {
let mut res = [0u8; 32];
res.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
res
})
("Ed25519HDKD", secret_seed, cc).using_encoded(|data| sp_core_hashing::blake2_256(data))
}
/// An error when deriving a key.