Move cryptographic hashing procedures to crypto folder. (#2306)

Step towards https://github.com/paritytech/polkadot-sdk/issues/1975

As reported
https://github.com/paritytech/polkadot-sdk/issues/1975#issuecomment-1774534225
I'd like to encapsulate crypto related stuff in a dedicated folder.

Currently all cryptographic primitive wrappers are all sparsed in
`substrate/core` which contains "misc core" stuff.

To simplify the process, as the first step with this PR I propose to
move the cryptographic hashing there.

The `substrate/crypto` folder was already created to contains `ec-utils`
crate.

Notes:
- rename `sp-core-hashing` to `sp-crypto-hashing`
- rename `sp-core-hashing-proc-macro` to `sp-crypto-hashing-proc-macro`
- As the crates name is changed I took the freedom to restart fresh from
version 0.1.0 for both crates

---------

Co-authored-by: Robert Hambrock <roberthambrock@gmail.com>
This commit is contained in:
Davide Galassi
2024-01-23 00:36:14 +01:00
committed by GitHub
parent 95ff9b2c54
commit 4c10fd2a41
96 changed files with 484 additions and 326 deletions
+2 -2
View File
@@ -31,7 +31,7 @@ sp-std = { path = "../std", default-features = false }
[dev-dependencies]
criterion = "0.4.0"
primitive-types = "0.12.0"
sp-core = { path = "../core", features = ["full_crypto"] }
sp-crypto-hashing = { path = "../crypto/hashing" }
rand = "0.8.5"
[features]
@@ -41,7 +41,7 @@ std = [
"num-traits/std",
"scale-info/std",
"serde/std",
"sp-core/std",
"sp-crypto-hashing/std",
"sp-std/std",
]
# Serde support without relying on std features.
@@ -287,7 +287,7 @@ mod tests {
}
fn random_u128(seed: u32) -> u128 {
u128::decode(&mut &seed.using_encoded(sp_core::hashing::twox_128)[..]).unwrap_or(0)
u128::decode(&mut &seed.using_encoded(sp_crypto_hashing::twox_128)[..]).unwrap_or(0)
}
#[test]
@@ -21,6 +21,7 @@ serde = { version = "1.0.195", default-features = false, optional = true, featur
sp-api = { path = "../../api", default-features = false }
sp-application-crypto = { path = "../../application-crypto", default-features = false }
sp-core = { path = "../../core", default-features = false }
sp-crypto-hashing = { path = "../../crypto/hashing", default-features = false }
sp-io = { path = "../../io", default-features = false }
sp-mmr-primitives = { path = "../../merkle-mountain-range", default-features = false }
sp-runtime = { path = "../../runtime", default-features = false }
@@ -41,6 +42,7 @@ std = [
"sp-api/std",
"sp-application-crypto/std",
"sp-core/std",
"sp-crypto-hashing/std",
"sp-io/std",
"sp-mmr-primitives/std",
"sp-runtime/std",
@@ -253,7 +253,8 @@ mod tests {
use super::*;
use crate::{ecdsa_crypto::Signature as EcdsaSignature, known_payloads};
use codec::Decode;
use sp_core::{keccak_256, Pair};
use sp_core::Pair;
use sp_crypto_hashing::keccak_256;
#[cfg(feature = "bls-experimental")]
use crate::bls_crypto::Signature as BlsSignature;
@@ -437,7 +437,8 @@ sp_api::decl_runtime_apis! {
mod tests {
use super::*;
use sp_application_crypto::ecdsa::{self, Public};
use sp_core::{blake2_256, crypto::Wraps, keccak_256, Pair};
use sp_core::crypto::{Pair, Wraps};
use sp_crypto_hashing::{blake2_256, keccak_256};
use sp_runtime::traits::{BlakeTwo256, Keccak256};
#[test]
@@ -19,7 +19,7 @@
use crate::{ecdsa_crypto, Commitment, EquivocationProof, Payload, ValidatorSetId, VoteMessage};
use codec::Encode;
use sp_core::{ecdsa, keccak_256, Pair};
use sp_core::{ecdsa, Pair};
use std::collections::HashMap;
use strum::IntoEnumIterator;
@@ -41,7 +41,7 @@ impl Keyring {
/// Sign `msg`.
pub fn sign(self, msg: &[u8]) -> ecdsa_crypto::Signature {
// todo: use custom signature hashing type
let msg = keccak_256(msg);
let msg = sp_crypto_hashing::keccak_256(msg);
ecdsa::Pair::from(self).sign_prehashed(&msg).into()
}
@@ -77,7 +77,8 @@ impl<TBlockNumber, TSignatureAccumulator>
#[cfg(test)]
mod tests {
use sp_core::{keccak_256, Pair};
use sp_core::Pair;
use sp_crypto_hashing::keccak_256;
use super::*;
use codec::Decode;
+4 -5
View File
@@ -53,7 +53,7 @@ libsecp256k1 = { version = "0.7", default-features = false, features = ["static-
schnorrkel = { version = "0.11.4", features = ["preaudit_deprecated"], default-features = false }
merlin = { version = "3.0", default-features = false }
secp256k1 = { version = "0.28.0", default-features = false, features = ["alloc", "recovery"], optional = true }
sp-core-hashing = { path = "hashing", default-features = false, optional = true }
sp-crypto-hashing = { path = "../crypto/hashing", default-features = false, optional = true }
sp-runtime-interface = { path = "../runtime-interface", default-features = false }
# bls crypto
@@ -66,7 +66,6 @@ criterion = "0.4.0"
serde_json = "1.0.111"
lazy_static = "1.4.0"
regex = "1.6.0"
sp-core-hashing-proc-macro = { path = "hashing/proc-macro" }
[[bench]]
name = "bench"
@@ -110,7 +109,7 @@ std = [
"secp256k1/std",
"secrecy/alloc",
"serde/std",
"sp-core-hashing/std",
"sp-crypto-hashing/std",
"sp-debug-derive/std",
"sp-externalities/std",
"sp-runtime-interface/std",
@@ -136,7 +135,7 @@ serde = [
"primitive-types/serde_no_std",
"scale-info/serde",
"secrecy/alloc",
"sp-core-hashing",
"sp-crypto-hashing",
"sp-storage/serde",
]
@@ -149,7 +148,7 @@ full_crypto = [
"ed25519-zebra",
"libsecp256k1",
"secp256k1",
"sp-core-hashing",
"sp-crypto-hashing",
"sp-runtime-interface/disable_target_static_assertions",
]
+3 -68
View File
@@ -12,66 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use sp_core::{
crypto::Pair as _,
hashing::{blake2_128, twox_128},
};
const MAX_KEY_SIZE: u32 = 32;
fn get_key(key_size: u32) -> Vec<u8> {
use rand::{Rng, SeedableRng};
let rnd: [u8; 32] = rand::rngs::StdRng::seed_from_u64(12).gen();
let mut rnd = rnd.iter().cycle();
(0..key_size).map(|_| *rnd.next().unwrap()).collect()
}
fn bench_blake2_128(b: &mut Bencher, key: &Vec<u8>) {
b.iter(|| {
let _a = blake2_128(black_box(key));
});
}
fn bench_twox_128(b: &mut Bencher, key: &Vec<u8>) {
b.iter(|| {
let _a = twox_128(black_box(key));
});
}
fn bench_hash_128_fix_size(c: &mut Criterion) {
let mut group = c.benchmark_group("fix size hashing");
let key = get_key(MAX_KEY_SIZE);
group.bench_with_input("blake2_128", &key, bench_blake2_128);
group.bench_with_input("twox_128", &key, bench_twox_128);
group.finish();
}
fn bench_hash_128_dyn_size(c: &mut Criterion) {
let mut group = c.benchmark_group("dyn size hashing");
for i in (2..MAX_KEY_SIZE).step_by(4) {
let key = get_key(i);
group.bench_with_input(
BenchmarkId::new("blake2_128", format!("{}", i)),
&key,
bench_blake2_128,
);
group.bench_with_input(
BenchmarkId::new("twox_128", format!("{}", i)),
&key,
bench_twox_128,
);
}
group.finish();
}
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use sp_core::crypto::Pair as _;
fn bench_ed25519(c: &mut Criterion) {
let mut group = c.benchmark_group("ed25519");
@@ -145,12 +87,5 @@ fn bench_ecdsa(c: &mut Criterion) {
group.finish();
}
criterion_group!(
benches,
bench_hash_128_fix_size,
bench_hash_128_dyn_size,
bench_ed25519,
bench_sr25519,
bench_ecdsa,
);
criterion_group!(benches, bench_ed25519, bench_sr25519, bench_ecdsa,);
criterion_main!(benches);
@@ -258,7 +258,7 @@ impl TraitPair for Pair {
_seed: Option<Seed>,
) -> Result<(Pair, Option<Seed>), DeriveError> {
let derive_hard = |seed, cc| -> Seed {
("bandersnatch-vrf-HDKD", seed, cc).using_encoded(sp_core_hashing::blake2_256)
("bandersnatch-vrf-HDKD", seed, cc).using_encoded(sp_crypto_hashing::blake2_256)
};
let mut seed = self.seed();
+1 -1
View File
@@ -428,7 +428,7 @@ trait HardJunctionId {
/// Derive a single hard junction.
#[cfg(feature = "full_crypto")]
fn derive_hard_junction<T: HardJunctionId>(secret_seed: &Seed, cc: &[u8; 32]) -> Seed {
(T::ID, secret_seed, cc).using_encoded(sp_core_hashing::blake2_256)
(T::ID, secret_seed, cc).using_encoded(sp_crypto_hashing::blake2_256)
}
#[cfg(feature = "full_crypto")]
+1 -1
View File
@@ -152,7 +152,7 @@ impl DeriveJunction {
let mut cc: [u8; JUNCTION_ID_LEN] = Default::default();
index.using_encoded(|data| {
if data.len() > JUNCTION_ID_LEN {
cc.copy_from_slice(&sp_core_hashing::blake2_256(data));
cc.copy_from_slice(&sp_crypto_hashing::blake2_256(data));
} else {
cc[0..data.len()].copy_from_slice(data);
}
+11 -13
View File
@@ -27,10 +27,7 @@ use crate::crypto::{
ByteArray, CryptoType, CryptoTypeId, Derive, Public as TraitPublic, UncheckedFrom,
};
#[cfg(feature = "full_crypto")]
use crate::{
crypto::{DeriveError, DeriveJunction, Pair as TraitPair, SecretStringError},
hashing::blake2_256,
};
use crate::crypto::{DeriveError, DeriveJunction, Pair as TraitPair, SecretStringError};
#[cfg(all(feature = "full_crypto", not(feature = "std")))]
use secp256k1::Secp256k1;
#[cfg(feature = "std")]
@@ -328,7 +325,7 @@ impl Signature {
/// Recover the public key from this signature and a message.
#[cfg(feature = "full_crypto")]
pub fn recover<M: AsRef<[u8]>>(&self, message: M) -> Option<Public> {
self.recover_prehashed(&blake2_256(message.as_ref()))
self.recover_prehashed(&sp_crypto_hashing::blake2_256(message.as_ref()))
}
/// Recover the public key from this signature and a pre-hashed message.
@@ -365,7 +362,7 @@ impl From<RecoverableSignature> for Signature {
/// 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(sp_core_hashing::blake2_256)
("Secp256k1HDKD", secret_seed, cc).using_encoded(sp_crypto_hashing::blake2_256)
}
/// A key pair.
@@ -423,7 +420,7 @@ impl TraitPair for Pair {
/// Sign a message.
fn sign(&self, message: &[u8]) -> Signature {
self.sign_prehashed(&blake2_256(message))
self.sign_prehashed(&sp_crypto_hashing::blake2_256(message))
}
/// Verify a signature on a message. Returns true if the signature is good.
@@ -481,7 +478,8 @@ impl Pair {
/// Parses Signature using parse_overflowing_slice.
#[deprecated(note = "please use `verify` instead")]
pub fn verify_deprecated<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &Public) -> bool {
let message = libsecp256k1::Message::parse(&blake2_256(message.as_ref()));
let message =
libsecp256k1::Message::parse(&sp_crypto_hashing::blake2_256(message.as_ref()));
let parse_signature_overflowing = |x: [u8; SIGNATURE_SERIALIZED_SIZE]| {
let sig = libsecp256k1::Signature::parse_overflowing_slice(&x[..64]).ok()?;
@@ -766,7 +764,7 @@ mod test {
// using pre-hashed `msg` works
let msg = b"this should be hashed";
let sig1 = pair.sign_prehashed(&blake2_256(msg));
let sig1 = pair.sign_prehashed(&sp_crypto_hashing::blake2_256(msg));
let sig2 = pair.sign(msg);
assert_eq!(sig1, sig2);
}
@@ -776,12 +774,12 @@ mod test {
let (pair, _, _) = Pair::generate_with_phrase(Some("password"));
// `msg` and `sig` match
let msg = blake2_256(b"this should be hashed");
let msg = sp_crypto_hashing::blake2_256(b"this should be hashed");
let sig = pair.sign_prehashed(&msg);
assert!(Pair::verify_prehashed(&sig, &msg, &pair.public()));
// `msg` and `sig` don't match
let msg = blake2_256(b"this is a different message");
let msg = sp_crypto_hashing::blake2_256(b"this is a different message");
assert!(!Pair::verify_prehashed(&sig, &msg, &pair.public()));
}
@@ -790,7 +788,7 @@ mod test {
let (pair, _, _) = Pair::generate_with_phrase(Some("password"));
// recovered key matches signing key
let msg = blake2_256(b"this should be hashed");
let msg = sp_crypto_hashing::blake2_256(b"this should be hashed");
let sig = pair.sign_prehashed(&msg);
let key = sig.recover_prehashed(&msg).unwrap();
assert_eq!(pair.public(), key);
@@ -799,7 +797,7 @@ mod test {
assert!(Pair::verify_prehashed(&sig, &msg, &key));
// recovered key and signing key don't match
let msg = blake2_256(b"this is a different message");
let msg = sp_crypto_hashing::blake2_256(b"this is a different message");
let key = sig.recover_prehashed(&msg).unwrap();
assert_ne!(pair.public(), key);
}
+1 -1
View File
@@ -372,7 +372,7 @@ impl Derive for Public {}
/// 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(sp_core_hashing::blake2_256)
("Ed25519HDKD", secret_seed, cc).using_encoded(sp_crypto_hashing::blake2_256)
}
#[cfg(feature = "full_crypto")]
+2 -2
View File
@@ -32,7 +32,7 @@ pub mod blake2 {
const LENGTH: usize = 32;
fn hash(x: &[u8]) -> Self::Out {
crate::hashing::blake2_256(x).into()
sp_crypto_hashing::blake2_256(x).into()
}
}
}
@@ -52,7 +52,7 @@ pub mod keccak {
const LENGTH: usize = 32;
fn hash(x: &[u8]) -> Self::Out {
crate::hashing::keccak_256(x).into()
sp_crypto_hashing::keccak_256(x).into()
}
}
}
-66
View File
@@ -1,66 +0,0 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Hashing functions.
//!
//! This module is gated by `full-crypto` feature. If you intend to use any of the functions
//! defined here within your runtime, you should most likely rather use `sp_io::hashing` instead,
//! unless you know what you're doing. Using `sp_io` will be more performant, since instead of
//! computing the hash in WASM it delegates that computation to the host client.
pub use sp_core_hashing::*;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn blake2b() {
assert_eq!(sp_core_hashing_proc_macro::blake2b_64!(b""), blake2_64(b"")[..]);
assert_eq!(sp_core_hashing_proc_macro::blake2b_256!(b"test"), blake2_256(b"test")[..]);
assert_eq!(sp_core_hashing_proc_macro::blake2b_512!(b""), blake2_512(b"")[..]);
}
#[test]
fn keccak() {
assert_eq!(sp_core_hashing_proc_macro::keccak_256!(b"test"), keccak_256(b"test")[..]);
assert_eq!(sp_core_hashing_proc_macro::keccak_512!(b"test"), keccak_512(b"test")[..]);
}
#[test]
fn sha2() {
assert_eq!(sp_core_hashing_proc_macro::sha2_256!(b"test"), sha2_256(b"test")[..]);
}
#[test]
fn twox() {
assert_eq!(sp_core_hashing_proc_macro::twox_128!(b"test"), twox_128(b"test")[..]);
assert_eq!(sp_core_hashing_proc_macro::twox_64!(b""), twox_64(b"")[..]);
}
#[test]
fn twox_concats() {
assert_eq!(
sp_core_hashing_proc_macro::twox_128!(b"test", b"123", b"45", b"", b"67890"),
super::twox_128(&b"test1234567890"[..]),
);
assert_eq!(
sp_core_hashing_proc_macro::twox_128!(b"test", test, b"45", b"", b"67890"),
super::twox_128(&b"testtest4567890"[..]),
);
}
}
+5 -3
View File
@@ -47,10 +47,12 @@ pub use sp_debug_derive::RuntimeDebug;
pub use impl_serde::serialize as bytes;
#[cfg(feature = "full_crypto")]
pub mod hashing;
#[deprecated(
since = "27.0.0",
note = "`sp-crypto-hashing` re-exports will be removed after June 2024. Use `sp-crypto-hashing` instead."
)]
pub use sp_crypto_hashing::{self as hashing, *};
#[cfg(feature = "full_crypto")]
pub use hashing::{blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64};
pub mod const_hex2array;
pub mod crypto;
pub mod hexdisplay;
@@ -1,13 +1,13 @@
[package]
name = "sp-core-hashing"
version = "9.0.0"
name = "sp-crypto-hashing"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
repository.workspace = true
description = "Primitive core crate hashing implementation."
documentation = "https://docs.rs/sp-core-hashing"
description = "Hashing primitives."
documentation = "https://docs.rs/sp-crypto-hashing"
[lints]
workspace = true
@@ -23,6 +23,17 @@ sha2 = { version = "0.10.7", default-features = false }
sha3 = { version = "0.10.0", default-features = false }
twox-hash = { version = "1.6.3", default-features = false, features = ["digest_0_10"] }
[dev-dependencies]
criterion = "0.4.0"
sp-crypto-hashing-proc-macro = { path = "proc-macro" }
[[bench]]
name = "bench"
harness = false
[lib]
bench = false
[features]
default = ["std"]
std = [
@@ -0,0 +1,81 @@
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
// Min 32 bytes buffer
const MIN_EXP: usize = 5;
// Max 1 MB buffer
const MAX_EXP: usize = 20;
fn bench_blake2_128(b: &mut Bencher, buf: &Vec<u8>) {
b.iter(|| {
let _a = sp_crypto_hashing::blake2_128(black_box(buf));
});
}
fn bench_twox_128(b: &mut Bencher, buf: &Vec<u8>) {
b.iter(|| {
let _a = sp_crypto_hashing::twox_128(black_box(buf));
});
}
fn bench_blake2_256(b: &mut Bencher, buf: &Vec<u8>) {
b.iter(|| {
let _a = sp_crypto_hashing::blake2_256(black_box(buf));
});
}
fn bench_twox_256(b: &mut Bencher, buf: &Vec<u8>) {
b.iter(|| {
let _a = sp_crypto_hashing::twox_256(black_box(buf));
});
}
fn bench_sha_256(b: &mut Bencher, buf: &Vec<u8>) {
b.iter(|| {
let _a = sp_crypto_hashing::sha2_256(black_box(buf));
});
}
fn bench_keccak_256(b: &mut Bencher, buf: &Vec<u8>) {
b.iter(|| {
let _a = sp_crypto_hashing::keccak_256(black_box(buf));
});
}
fn bench_hash(c: &mut Criterion) {
let mut group = c.benchmark_group("hashing-128");
let buf = vec![0u8; 1 << MAX_EXP];
for i in MIN_EXP..=MAX_EXP {
let size = 1 << i;
group.bench_with_input(BenchmarkId::new("blake2-128", size), &buf, bench_blake2_128);
group.bench_with_input(BenchmarkId::new("twox-128", size), &buf, bench_twox_128);
}
group.finish();
let mut group = c.benchmark_group("hashing-256");
for i in MIN_EXP..=MAX_EXP {
let size = 1 << i;
group.bench_with_input(BenchmarkId::new("blake2-256", size), &buf, bench_blake2_256);
group.bench_with_input(BenchmarkId::new("twox-256", size), &buf, bench_twox_256);
group.bench_with_input(BenchmarkId::new("sha-256", size), &buf, bench_sha_256);
group.bench_with_input(BenchmarkId::new("keccak-256", size), &buf, bench_keccak_256);
}
group.finish();
}
criterion_group!(benches, bench_hash);
criterion_main!(benches);
@@ -1,13 +1,13 @@
[package]
name = "sp-core-hashing-proc-macro"
version = "9.0.0"
name = "sp-crypto-hashing-proc-macro"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
repository.workspace = true
description = "This crate provides procedural macros for calculating static hash."
documentation = "https://docs.rs/sp-core-hashing-proc-macro"
description = "Procedural macros for calculating static hashes."
documentation = "https://docs.rs/sp-crypto-hashing-proc-macro"
[lints]
workspace = true
@@ -21,4 +21,4 @@ proc-macro = true
[dependencies]
quote = "1.0.28"
syn = { version = "2.0.48", features = ["full", "parsing"] }
sp-core-hashing = { path = "..", default-features = false }
sp-crypto-hashing = { path = "..", default-features = false }
@@ -83,35 +83,35 @@ impl Parse for MultipleInputBytes {
}
pub(super) fn twox_64(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::twox_64(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::twox_64(bytes.as_slice()))
}
pub(super) fn twox_128(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::twox_128(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::twox_128(bytes.as_slice()))
}
pub(super) fn blake2b_512(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::blake2_512(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::blake2_512(bytes.as_slice()))
}
pub(super) fn blake2b_256(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::blake2_256(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::blake2_256(bytes.as_slice()))
}
pub(super) fn blake2b_64(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::blake2_64(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::blake2_64(bytes.as_slice()))
}
pub(super) fn keccak_256(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::keccak_256(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::keccak_256(bytes.as_slice()))
}
pub(super) fn keccak_512(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::keccak_512(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::keccak_512(bytes.as_slice()))
}
pub(super) fn sha2_256(bytes: Vec<u8>) -> TokenStream {
bytes_to_array(sp_core_hashing::sha2_256(bytes.as_slice()))
bytes_to_array(sp_crypto_hashing::sha2_256(bytes.as_slice()))
}
fn bytes_to_array(bytes: impl IntoIterator<Item = u8>) -> TokenStream {
@@ -30,24 +30,24 @@
//!
//! ```rust
//! assert_eq!(
//! sp_core_hashing_proc_macro::blake2b_256!(b"test"),
//! sp_core_hashing::blake2_256(b"test"),
//! sp_crypto_hashing_proc_macro::blake2b_256!(b"test"),
//! sp_crypto_hashing::blake2_256(b"test"),
//! );
//! assert_eq!(
//! sp_core_hashing_proc_macro::blake2b_256!([1u8]),
//! sp_core_hashing::blake2_256(&[1u8]),
//! sp_crypto_hashing_proc_macro::blake2b_256!([1u8]),
//! sp_crypto_hashing::blake2_256(&[1u8]),
//! );
//! assert_eq!(
//! sp_core_hashing_proc_macro::blake2b_256!([1, 2, 3]),
//! sp_core_hashing::blake2_256(&[1, 2, 3]),
//! sp_crypto_hashing_proc_macro::blake2b_256!([1, 2, 3]),
//! sp_crypto_hashing::blake2_256(&[1, 2, 3]),
//! );
//! assert_eq!(
//! sp_core_hashing_proc_macro::blake2b_256!(identifier),
//! sp_core_hashing::blake2_256(b"identifier"),
//! sp_crypto_hashing_proc_macro::blake2b_256!(identifier),
//! sp_crypto_hashing::blake2_256(b"identifier"),
//! );
//! assert_eq!(
//! sp_core_hashing_proc_macro::blake2b_256!(identifier, b"/string"),
//! sp_core_hashing::blake2_256(b"identifier/string"),
//! sp_crypto_hashing_proc_macro::blake2b_256!(identifier, b"/string"),
//! sp_crypto_hashing::blake2_256(b"identifier/string"),
//! );
//! ```
@@ -121,3 +121,44 @@ pub fn keccak_512(data: &[u8]) -> [u8; 64] {
pub fn sha2_256(data: &[u8]) -> [u8; 32] {
sha2::Sha256::digest(data).into()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn blake2b() {
assert_eq!(sp_crypto_hashing_proc_macro::blake2b_64!(b""), blake2_64(b"")[..]);
assert_eq!(sp_crypto_hashing_proc_macro::blake2b_256!(b"test"), blake2_256(b"test")[..]);
assert_eq!(sp_crypto_hashing_proc_macro::blake2b_512!(b""), blake2_512(b"")[..]);
}
#[test]
fn keccak() {
assert_eq!(sp_crypto_hashing_proc_macro::keccak_256!(b"test"), keccak_256(b"test")[..]);
assert_eq!(sp_crypto_hashing_proc_macro::keccak_512!(b"test"), keccak_512(b"test")[..]);
}
#[test]
fn sha2() {
assert_eq!(sp_crypto_hashing_proc_macro::sha2_256!(b"test"), sha2_256(b"test")[..]);
}
#[test]
fn twox() {
assert_eq!(sp_crypto_hashing_proc_macro::twox_128!(b"test"), twox_128(b"test")[..]);
assert_eq!(sp_crypto_hashing_proc_macro::twox_64!(b""), twox_64(b"")[..]);
}
#[test]
fn twox_concats() {
assert_eq!(
sp_crypto_hashing_proc_macro::twox_128!(b"test", b"123", b"45", b"", b"67890"),
twox_128(&b"test1234567890"[..]),
);
assert_eq!(
sp_crypto_hashing_proc_macro::twox_128!(b"test", test, b"45", b"", b"67890"),
twox_128(&b"testtest4567890"[..]),
);
}
}
+2 -1
View File
@@ -17,11 +17,11 @@ workspace = true
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
bytes = { version = "1.1.0", default-features = false }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["bytes"] }
sp-core = { path = "../core", default-features = false }
sp-crypto-hashing = { path = "../crypto/hashing", default-features = false }
sp-keystore = { path = "../keystore", default-features = false, optional = true }
sp-std = { path = "../std", default-features = false }
libsecp256k1 = { version = "0.7", optional = true }
@@ -52,6 +52,7 @@ std = [
"log/std",
"secp256k1",
"sp-core/std",
"sp-crypto-hashing/std",
"sp-externalities/std",
"sp-keystore/std",
"sp-runtime-interface/std",
+8 -8
View File
@@ -1251,42 +1251,42 @@ pub trait Crypto {
pub trait Hashing {
/// Conduct a 256-bit Keccak hash.
fn keccak_256(data: &[u8]) -> [u8; 32] {
sp_core::hashing::keccak_256(data)
sp_crypto_hashing::keccak_256(data)
}
/// Conduct a 512-bit Keccak hash.
fn keccak_512(data: &[u8]) -> [u8; 64] {
sp_core::hashing::keccak_512(data)
sp_crypto_hashing::keccak_512(data)
}
/// Conduct a 256-bit Sha2 hash.
fn sha2_256(data: &[u8]) -> [u8; 32] {
sp_core::hashing::sha2_256(data)
sp_crypto_hashing::sha2_256(data)
}
/// Conduct a 128-bit Blake2 hash.
fn blake2_128(data: &[u8]) -> [u8; 16] {
sp_core::hashing::blake2_128(data)
sp_crypto_hashing::blake2_128(data)
}
/// Conduct a 256-bit Blake2 hash.
fn blake2_256(data: &[u8]) -> [u8; 32] {
sp_core::hashing::blake2_256(data)
sp_crypto_hashing::blake2_256(data)
}
/// Conduct four XX hashes to give a 256-bit result.
fn twox_256(data: &[u8]) -> [u8; 32] {
sp_core::hashing::twox_256(data)
sp_crypto_hashing::twox_256(data)
}
/// Conduct two XX hashes to give a 128-bit result.
fn twox_128(data: &[u8]) -> [u8; 16] {
sp_core::hashing::twox_128(data)
sp_crypto_hashing::twox_128(data)
}
/// Conduct two XX hashes to give a 64-bit result.
fn twox_64(data: &[u8]) -> [u8; 8] {
sp_core::hashing::twox_64(data)
sp_crypto_hashing::twox_64(data)
}
}
+4 -3
View File
@@ -479,16 +479,17 @@ mod tests {
let suri = "//Alice";
let pair = ecdsa::Pair::from_string(suri, None).unwrap();
let msg = sp_core::keccak_256(b"this should be a hashed message");
// Let's pretend this to be the hash output as content doesn't really matter here.
let hash = [0xff; 32];
// no key in key store
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap();
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &hash).unwrap();
assert!(res.is_none());
// insert key, sign again
store.insert(ECDSA, suri, pair.public().as_ref()).unwrap();
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap();
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &hash).unwrap();
assert!(res.is_some());
}
@@ -19,6 +19,7 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
sp-core = { path = "../core", default-features = false }
sp-crypto-hashing = { path = "../crypto/hashing", default-features = false }
sp-runtime = { path = "../runtime", default-features = false }
sp-std = { path = "../std", default-features = false }
sp-api = { path = "../api", default-features = false }
@@ -52,6 +53,7 @@ std = [
"sp-api/std",
"sp-application-crypto/std",
"sp-core/std",
"sp-crypto-hashing/std",
"sp-externalities/std",
"sp-runtime-interface/std",
"sp-runtime/std",
@@ -87,7 +87,7 @@ mod ecdsa {
/// Returns blake2-256 hash for the encoded statement.
#[cfg(feature = "std")]
pub fn hash_encoded(data: &[u8]) -> [u8; 32] {
sp_core::hashing::blake2_256(data)
sp_crypto_hashing::blake2_256(data)
}
/// Statement proof.
@@ -632,7 +632,7 @@ mod test {
statement.sign_ecdsa_private(&secp256k1_kp);
assert_eq!(
statement.verify_signature(),
SignatureVerificationResult::Valid(sp_core::hashing::blake2_256(
SignatureVerificationResult::Valid(sp_crypto_hashing::blake2_256(
&secp256k1_kp.public().0
))
);
+1 -1
View File
@@ -23,7 +23,7 @@ parity-wasm = { version = "0.45", optional = true }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.195", default-features = false, features = ["alloc", "derive"], optional = true }
thiserror = { version = "1.0.48", optional = true }
sp-core-hashing-proc-macro = { path = "../core/hashing/proc-macro" }
sp-crypto-hashing-proc-macro = { path = "../crypto/hashing/proc-macro" }
sp-runtime = { path = "../runtime", default-features = false }
sp-std = { path = "../std", default-features = false }
sp-version-proc-macro = { path = "proc-macro", default-features = false }
+1 -1
View File
@@ -297,7 +297,7 @@ fn has_api_with<P: Fn(u32) -> bool>(apis: &ApisVec, id: &ApiId, predicate: P) ->
/// Returns the version of the `Core` runtime api.
pub fn core_version_from_apis(apis: &ApisVec) -> Option<u32> {
let id = sp_core_hashing_proc_macro::blake2b_64!(b"Core");
let id = sp_crypto_hashing_proc_macro::blake2b_64!(b"Core");
apis.iter().find(|(s, _v)| s == &id).map(|(_s, v)| *v)
}