From 1da2a7045680c84a1d12df3cdfc1f767b6c13143 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 14 Nov 2023 10:11:33 +0100 Subject: [PATCH 01/15] Move cryptographic hashing procedures to crypto folder. --- Cargo.lock | 54 +++++++-------- Cargo.toml | 4 +- substrate/frame/alliance/Cargo.toml | 8 +-- substrate/frame/alliance/src/benchmarking.rs | 2 +- substrate/frame/alliance/src/mock.rs | 2 +- substrate/frame/support/Cargo.toml | 2 +- substrate/frame/support/procedural/Cargo.toml | 2 +- .../procedural/src/pallet/parse/helper.rs | 2 +- substrate/frame/support/src/lib.rs | 4 +- substrate/primitives/core/Cargo.toml | 9 ++- substrate/primitives/core/src/bandersnatch.rs | 2 +- substrate/primitives/core/src/bls.rs | 2 +- substrate/primitives/core/src/crypto.rs | 2 +- substrate/primitives/core/src/ecdsa.rs | 2 +- substrate/primitives/core/src/ed25519.rs | 2 +- substrate/primitives/core/src/hashing.rs | 66 ------------------- substrate/primitives/core/src/lib.rs | 7 +- .../{core => crypto}/hashing/Cargo.toml | 11 ++-- .../hashing/proc-macro/Cargo.toml | 10 +-- .../hashing/proc-macro/src/impls.rs | 16 ++--- .../hashing/proc-macro/src/lib.rs | 20 +++--- .../{core => crypto}/hashing/src/lib.rs | 41 ++++++++++++ substrate/primitives/version/Cargo.toml | 2 +- substrate/primitives/version/src/lib.rs | 2 +- .../benchmarking-cli/src/machine/README.md | 2 +- 25 files changed, 126 insertions(+), 150 deletions(-) delete mode 100644 substrate/primitives/core/src/hashing.rs rename substrate/primitives/{core => crypto}/hashing/Cargo.toml (78%) rename substrate/primitives/{core => crypto}/hashing/proc-macro/Cargo.toml (56%) rename substrate/primitives/{core => crypto}/hashing/proc-macro/src/impls.rs (85%) rename substrate/primitives/{core => crypto}/hashing/proc-macro/src/lib.rs (89%) rename substrate/primitives/{core => crypto}/hashing/src/lib.rs (76%) diff --git a/Cargo.lock b/Cargo.lock index 4a45d5c602e8..7a9e627b229c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5798,7 +5798,7 @@ dependencies = [ "sp-api", "sp-arithmetic", "sp-core", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing-proc-macro", "sp-debug-derive 8.0.0", "sp-genesis-builder", "sp-inherents", @@ -5828,7 +5828,7 @@ dependencies = [ "proc-macro-warning", "proc-macro2", "quote", - "sp-core-hashing", + "sp-crypto-hashing", "syn 2.0.38", ] @@ -9179,7 +9179,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-core-hashing", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -17418,8 +17418,7 @@ dependencies = [ "secrecy", "serde", "serde_json", - "sp-core-hashing", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing", "sp-debug-derive 8.0.0", "sp-externalities 0.19.0", "sp-runtime-interface 17.0.0", @@ -17433,27 +17432,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "sp-core-hashing" -version = "9.0.0" -dependencies = [ - "blake2b_simd", - "byteorder", - "digest 0.10.7", - "sha2 0.10.7", - "sha3", - "twox-hash", -] - -[[package]] -name = "sp-core-hashing-proc-macro" -version = "9.0.0" -dependencies = [ - "quote", - "sp-core-hashing", - "syn 2.0.38", -] - [[package]] name = "sp-crypto-ec-utils" version = "0.4.1" @@ -17495,6 +17473,28 @@ dependencies = [ "sp-std 8.0.0 (git+https://github.com/paritytech/polkadot-sdk)", ] +[[package]] +name = "sp-crypto-hashing" +version = "0.1.0" +dependencies = [ + "blake2b_simd", + "byteorder", + "digest 0.10.7", + "sha2 0.10.7", + "sha3", + "sp-crypto-hashing-proc-macro", + "twox-hash", +] + +[[package]] +name = "sp-crypto-hashing-proc-macro" +version = "0.1.0" +dependencies = [ + "quote", + "sp-crypto-hashing", + "syn 2.0.38", +] + [[package]] name = "sp-database" version = "4.0.0-dev" @@ -18058,7 +18058,7 @@ dependencies = [ "parity-wasm", "scale-info", "serde", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing-proc-macro", "sp-runtime", "sp-std 8.0.0", "sp-version-proc-macro", diff --git a/Cargo.toml b/Cargo.toml index 30445bd59450..d9ed894e9401 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -410,9 +410,9 @@ members = [ "substrate/primitives/consensus/sassafras", "substrate/primitives/consensus/slots", "substrate/primitives/core", - "substrate/primitives/core/hashing", - "substrate/primitives/core/hashing/proc-macro", "substrate/primitives/crypto/ec-utils", + "substrate/primitives/crypto/hashing", + "substrate/primitives/crypto/hashing/proc-macro", "substrate/primitives/database", "substrate/primitives/debug-derive", "substrate/primitives/externalities", diff --git a/substrate/frame/alliance/Cargo.toml b/substrate/frame/alliance/Cargo.toml index d7d7352975ae..5b30f318c7d2 100644 --- a/substrate/frame/alliance/Cargo.toml +++ b/substrate/frame/alliance/Cargo.toml @@ -21,7 +21,7 @@ scale-info = { version = "2.10.0", default-features = false, features = ["derive sp-std = { path = "../../primitives/std", default-features = false} sp-core = { path = "../../primitives/core", default-features = false} -sp-core-hashing = { path = "../../primitives/core/hashing", default-features = false, optional = true } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing", default-features = false, optional = true } sp-io = { path = "../../primitives/io", default-features = false} sp-runtime = { path = "../../primitives/runtime", default-features = false} @@ -34,7 +34,7 @@ pallet-collective = { path = "../collective", default-features = false, optional [dev-dependencies] array-bytes = "6.1" -sp-core-hashing = { path = "../../primitives/core/hashing", default-features = false} +sp-crypto-hashing = { path = "../../primitives/crypto/hashing", default-features = false} pallet-balances = { path = "../balances" } pallet-collective = { path = "../collective" } @@ -50,8 +50,8 @@ std = [ "pallet-collective?/std", "pallet-identity/std", "scale-info/std", - "sp-core-hashing?/std", "sp-core/std", + "sp-crypto-hashing?/std", "sp-io/std", "sp-runtime/std", "sp-std/std", @@ -64,7 +64,7 @@ runtime-benchmarks = [ "pallet-balances/runtime-benchmarks", "pallet-collective/runtime-benchmarks", "pallet-identity/runtime-benchmarks", - "sp-core-hashing", + "sp-crypto-hashing", "sp-runtime/runtime-benchmarks", ] try-runtime = [ diff --git a/substrate/frame/alliance/src/benchmarking.rs b/substrate/frame/alliance/src/benchmarking.rs index 37cc3314037e..c67262889a66 100644 --- a/substrate/frame/alliance/src/benchmarking.rs +++ b/substrate/frame/alliance/src/benchmarking.rs @@ -42,7 +42,7 @@ fn assert_last_event, I: 'static>(generic_event: >:: } fn cid(input: impl AsRef<[u8]>) -> Cid { - let result = sp_core_hashing::sha2_256(input.as_ref()); + let result = sp_crypto_hashing::sha2_256(input.as_ref()); Cid::new_v0(result) } diff --git a/substrate/frame/alliance/src/mock.rs b/substrate/frame/alliance/src/mock.rs index ace5214f145f..ccad2e327679 100644 --- a/substrate/frame/alliance/src/mock.rs +++ b/substrate/frame/alliance/src/mock.rs @@ -363,7 +363,7 @@ pub fn new_bench_ext() -> sp_io::TestExternalities { } pub fn test_cid() -> Cid { - let result = sp_core_hashing::sha2_256(b"hello world"); + let result = sp_crypto_hashing::sha2_256(b"hello world"); Cid::new_v0(result) } diff --git a/substrate/frame/support/Cargo.toml b/substrate/frame/support/Cargo.toml index b8e21e60761a..255c30043096 100644 --- a/substrate/frame/support/Cargo.toml +++ b/substrate/frame/support/Cargo.toml @@ -39,7 +39,7 @@ bitflags = "1.3" impl-trait-for-tuples = "0.2.2" smallvec = "1.11.0" log = { version = "0.4.17", default-features = false } -sp-core-hashing-proc-macro = { path = "../../primitives/core/hashing/proc-macro" } +sp-crypto-hashing-proc-macro = { path = "../../primitives/crypto/hashing/proc-macro" } k256 = { version = "0.13.1", default-features = false, features = ["ecdsa"] } environmental = { version = "1.1.4", default-features = false } sp-genesis-builder = { path = "../../primitives/genesis-builder", default-features=false} diff --git a/substrate/frame/support/procedural/Cargo.toml b/substrate/frame/support/procedural/Cargo.toml index 45ed1750a528..91f41788c11a 100644 --- a/substrate/frame/support/procedural/Cargo.toml +++ b/substrate/frame/support/procedural/Cargo.toml @@ -26,7 +26,7 @@ frame-support-procedural-tools = { path = "tools" } macro_magic = { version = "0.5.0", features = ["proc_support"] } proc-macro-warning = { version = "1.0.0", default-features = false } expander = "2.0.0" -sp-core-hashing = { path = "../../../primitives/core/hashing" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } [features] default = [ "std" ] diff --git a/substrate/frame/support/procedural/src/pallet/parse/helper.rs b/substrate/frame/support/procedural/src/pallet/parse/helper.rs index 446ec203d2ba..538226a8745f 100644 --- a/substrate/frame/support/procedural/src/pallet/parse/helper.rs +++ b/substrate/frame/support/procedural/src/pallet/parse/helper.rs @@ -613,7 +613,7 @@ pub fn check_pallet_call_return_type(type_: &syn::Type) -> syn::Result<()> { } pub(crate) fn two128_str(s: &str) -> TokenStream { - bytes_to_array(sp_core_hashing::twox_128(s.as_bytes()).into_iter()) + bytes_to_array(sp_crypto_hashing::twox_128(s.as_bytes()).into_iter()) } pub(crate) fn bytes_to_array(bytes: impl IntoIterator) -> TokenStream { diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 2ec3b24db0ce..93bccf5f97b4 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -45,7 +45,7 @@ pub mod __private { pub use scale_info; pub use serde; pub use sp_core::{OpaqueMetadata, Void}; - pub use sp_core_hashing_proc_macro; + pub use sp_crypto_hashing_proc_macro; pub use sp_inherents; pub use sp_io::{self, hashing, storage::root as storage_root}; pub use sp_metadata_ir as metadata_ir; @@ -328,7 +328,7 @@ macro_rules! parameter_types { impl< $($ty_params),* > $name< $($ty_params),* > { /// Returns the key for this parameter type. pub fn key() -> [u8; 16] { - $crate::__private::sp_core_hashing_proc_macro::twox_128!(b":", $name, b":") + $crate::__private::sp_crypto_hashing_proc_macro::twox_128!(b":", $name, b":") } /// Set the value of this parameter type in the storage. diff --git a/substrate/primitives/core/Cargo.toml b/substrate/primitives/core/Cargo.toml index 79df81e62c66..15b8cbe01061 100644 --- a/substrate/primitives/core/Cargo.toml +++ b/substrate/primitives/core/Cargo.toml @@ -52,7 +52,7 @@ libsecp256k1 = { version = "0.7", default-features = false, features = ["static- schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated", "u64_backend"], default-features = false } merlin = { version = "2.0", default-features = false } secp256k1 = { version = "0.24.0", default-features = false, features = ["recovery", "alloc"], 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 @@ -63,7 +63,6 @@ bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf", rev = "cbc342e", [dev-dependencies] criterion = "0.4.0" serde_json = "1.0.108" -sp-core-hashing-proc-macro = { path = "hashing/proc-macro" } [[bench]] name = "bench" @@ -109,7 +108,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", @@ -135,7 +134,7 @@ serde = [ "primitive-types/serde_no_std", "scale-info/serde", "secrecy/alloc", - "sp-core-hashing", + "sp-crypto-hashing", "sp-storage/serde", ] @@ -148,7 +147,7 @@ full_crypto = [ "ed25519-zebra", "libsecp256k1", "secp256k1", - "sp-core-hashing", + "sp-crypto-hashing", "sp-runtime-interface/disable_target_static_assertions", ] diff --git a/substrate/primitives/core/src/bandersnatch.rs b/substrate/primitives/core/src/bandersnatch.rs index 78b7f12f9ffd..ea695f7c218f 100644 --- a/substrate/primitives/core/src/bandersnatch.rs +++ b/substrate/primitives/core/src/bandersnatch.rs @@ -250,7 +250,7 @@ impl TraitPair for Pair { _seed: Option, ) -> Result<(Pair, Option), 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(); diff --git a/substrate/primitives/core/src/bls.rs b/substrate/primitives/core/src/bls.rs index e519ba1806c4..452c6372d16b 100644 --- a/substrate/primitives/core/src/bls.rs +++ b/substrate/primitives/core/src/bls.rs @@ -428,7 +428,7 @@ trait HardJunctionId { /// Derive a single hard junction. #[cfg(feature = "full_crypto")] fn derive_hard_junction(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")] diff --git a/substrate/primitives/core/src/crypto.rs b/substrate/primitives/core/src/crypto.rs index d369de5a1c01..4ba0aaae3719 100644 --- a/substrate/primitives/core/src/crypto.rs +++ b/substrate/primitives/core/src/crypto.rs @@ -142,7 +142,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); } diff --git a/substrate/primitives/core/src/ecdsa.rs b/substrate/primitives/core/src/ecdsa.rs index 603fa515a30e..fcb22fc165e0 100644 --- a/substrate/primitives/core/src/ecdsa.rs +++ b/substrate/primitives/core/src/ecdsa.rs @@ -365,7 +365,7 @@ impl From 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. diff --git a/substrate/primitives/core/src/ed25519.rs b/substrate/primitives/core/src/ed25519.rs index 151a7229315e..aa0d77510bd8 100644 --- a/substrate/primitives/core/src/ed25519.rs +++ b/substrate/primitives/core/src/ed25519.rs @@ -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")] diff --git a/substrate/primitives/core/src/hashing.rs b/substrate/primitives/core/src/hashing.rs deleted file mode 100644 index e71d0b543380..000000000000 --- a/substrate/primitives/core/src/hashing.rs +++ /dev/null @@ -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"[..]), - ); - } -} diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index ec0641c54668..1f865c7eeec4 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -47,10 +47,9 @@ pub use sp_debug_derive::RuntimeDebug; pub use impl_serde::serialize as bytes; #[cfg(feature = "full_crypto")] -pub mod hashing; - -#[cfg(feature = "full_crypto")] -pub use hashing::{blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64}; +pub use sp_crypto_hashing::{ + self as hashing, blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64, +}; pub mod crypto; pub mod hexdisplay; pub use paste; diff --git a/substrate/primitives/core/hashing/Cargo.toml b/substrate/primitives/crypto/hashing/Cargo.toml similarity index 78% rename from substrate/primitives/core/hashing/Cargo.toml rename to substrate/primitives/crypto/hashing/Cargo.toml index bd22bd79e7d5..98fc0975588e 100644 --- a/substrate/primitives/core/hashing/Cargo.toml +++ b/substrate/primitives/crypto/hashing/Cargo.toml @@ -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" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] @@ -20,6 +20,9 @@ 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] +sp-crypto-hashing-proc-macro = { path = "proc-macro" } + [features] default = [ "std" ] std = [ diff --git a/substrate/primitives/core/hashing/proc-macro/Cargo.toml b/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml similarity index 56% rename from substrate/primitives/core/hashing/proc-macro/Cargo.toml rename to substrate/primitives/crypto/hashing/proc-macro/Cargo.toml index 187b5559b931..eb41469e3e91 100644 --- a/substrate/primitives/core/hashing/proc-macro/Cargo.toml +++ b/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml @@ -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" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] @@ -18,4 +18,4 @@ proc-macro = true [dependencies] quote = "1.0.28" syn = { version = "2.0.38", features = ["full", "parsing"] } -sp-core-hashing = { path = "..", default-features = false} +sp-crypto-hashing = { path = "..", default-features = false} diff --git a/substrate/primitives/core/hashing/proc-macro/src/impls.rs b/substrate/primitives/crypto/hashing/proc-macro/src/impls.rs similarity index 85% rename from substrate/primitives/core/hashing/proc-macro/src/impls.rs rename to substrate/primitives/crypto/hashing/proc-macro/src/impls.rs index 714852ae3594..5eba7760b3de 100644 --- a/substrate/primitives/core/hashing/proc-macro/src/impls.rs +++ b/substrate/primitives/crypto/hashing/proc-macro/src/impls.rs @@ -83,35 +83,35 @@ impl Parse for MultipleInputBytes { } pub(super) fn twox_64(bytes: Vec) -> 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) -> 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) -> 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) -> 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) -> 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) -> 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) -> 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) -> 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) -> TokenStream { diff --git a/substrate/primitives/core/hashing/proc-macro/src/lib.rs b/substrate/primitives/crypto/hashing/proc-macro/src/lib.rs similarity index 89% rename from substrate/primitives/core/hashing/proc-macro/src/lib.rs rename to substrate/primitives/crypto/hashing/proc-macro/src/lib.rs index 69668cadb4e2..0ebd30c8b409 100644 --- a/substrate/primitives/core/hashing/proc-macro/src/lib.rs +++ b/substrate/primitives/crypto/hashing/proc-macro/src/lib.rs @@ -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"), //! ); //! ``` diff --git a/substrate/primitives/core/hashing/src/lib.rs b/substrate/primitives/crypto/hashing/src/lib.rs similarity index 76% rename from substrate/primitives/core/hashing/src/lib.rs rename to substrate/primitives/crypto/hashing/src/lib.rs index 33d777f85b01..04b7bb6f4de2 100644 --- a/substrate/primitives/core/hashing/src/lib.rs +++ b/substrate/primitives/crypto/hashing/src/lib.rs @@ -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"[..]), + ); + } +} diff --git a/substrate/primitives/version/Cargo.toml b/substrate/primitives/version/Cargo.toml index 41a83f01f66a..0a354c52d313 100644 --- a/substrate/primitives/version/Cargo.toml +++ b/substrate/primitives/version/Cargo.toml @@ -20,7 +20,7 @@ parity-wasm = { version = "0.45", optional = true } scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } serde = { version = "1.0.188", default-features = false, features = ["derive", "alloc"], 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} diff --git a/substrate/primitives/version/src/lib.rs b/substrate/primitives/version/src/lib.rs index 13f4520f6e69..9b14a809ac10 100644 --- a/substrate/primitives/version/src/lib.rs +++ b/substrate/primitives/version/src/lib.rs @@ -297,7 +297,7 @@ fn has_api_with bool>(apis: &ApisVec, id: &ApiId, predicate: P) -> /// Returns the version of the `Core` runtime api. pub fn core_version_from_apis(apis: &ApisVec) -> Option { - 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) } diff --git a/substrate/utils/frame/benchmarking-cli/src/machine/README.md b/substrate/utils/frame/benchmarking-cli/src/machine/README.md index d3c9a0ec8406..1ccc2ad1e4f6 100644 --- a/substrate/utils/frame/benchmarking-cli/src/machine/README.md +++ b/substrate/utils/frame/benchmarking-cli/src/machine/README.md @@ -78,6 +78,6 @@ License: Apache-2.0 [Blake2-256]: https://www.blake2.net/ -[blake2_256 function]: https://crates.parity.io/sp_core/hashing/fn.blake2_256.html +[blake2_256 function]: https://crates.parity.io/sp_crypto/hashing/fn.blake2_256.html [Curve25519]: https://en.wikipedia.org/wiki/Curve25519 [`--base-path`]: ../shared/README.md#arguments From ff7e172c4945fd59d19d3a76fefd39b6853e34c8 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 14 Nov 2023 10:55:09 +0100 Subject: [PATCH 02/15] Introduce direct dependency on sp-crypto-hashing --- Cargo.lock | 11 ++++++++++ polkadot/node/core/pvf/common/Cargo.toml | 1 + polkadot/node/core/pvf/common/src/pvf.rs | 3 +-- .../node/network/gossip-support/Cargo.toml | 1 + .../node/network/gossip-support/src/lib.rs | 2 +- substrate/client/chain-spec/Cargo.toml | 1 + .../chain-spec/src/genesis_config_builder.rs | 2 +- substrate/client/consensus/babe/Cargo.toml | 1 + .../client/consensus/babe/src/authorship.rs | 3 +-- substrate/client/consensus/beefy/Cargo.toml | 1 + .../client/consensus/beefy/src/keystore.rs | 3 +-- substrate/client/consensus/grandpa/Cargo.toml | 1 + .../client/consensus/grandpa/src/import.rs | 7 ++++-- substrate/client/sysinfo/Cargo.toml | 1 + substrate/client/sysinfo/src/sysinfo.rs | 2 +- .../primitives/consensus/beefy/Cargo.toml | 1 + .../consensus/beefy/src/test_utils.rs | 4 ++-- substrate/primitives/core/src/ecdsa.rs | 22 +++++++++---------- substrate/primitives/core/src/hasher.rs | 4 ++-- substrate/primitives/core/src/lib.rs | 8 +++---- substrate/primitives/io/Cargo.toml | 2 ++ substrate/primitives/io/src/lib.rs | 16 +++++++------- .../primitives/statement-store/Cargo.toml | 2 ++ .../primitives/statement-store/src/lib.rs | 2 +- .../frame/remote-externalities/Cargo.toml | 1 + .../frame/remote-externalities/src/lib.rs | 10 ++++----- 26 files changed, 67 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a9e627b229c..f30a18c28c17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5761,6 +5761,7 @@ dependencies = [ "parity-scale-codec", "serde", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-state-machine", @@ -12132,6 +12133,7 @@ dependencies = [ "sp-authority-discovery", "sp-consensus-babe", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-tracing 10.0.0", @@ -12538,6 +12540,7 @@ dependencies = [ "sc-executor-wasmtime", "seccompiler", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-io", "sp-tracing 10.0.0", @@ -14988,6 +14991,7 @@ dependencies = [ "sp-blockchain", "sp-consensus-babe", "sp-core", + "sp-crypto-hashing", "sp-genesis-builder", "sp-io", "sp-keyring", @@ -15203,6 +15207,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-slots", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-keyring", "sp-keystore", @@ -15272,6 +15277,7 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-mmr-primitives", @@ -15356,6 +15362,7 @@ dependencies = [ "sp-consensus", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-runtime", @@ -16193,6 +16200,7 @@ dependencies = [ "serde", "serde_json", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -17321,6 +17329,7 @@ dependencies = [ "sp-api", "sp-application-crypto", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-mmr-primitives", "sp-runtime", @@ -17579,6 +17588,7 @@ dependencies = [ "rustversion", "secp256k1", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-keystore", "sp-runtime-interface 17.0.0", @@ -17909,6 +17919,7 @@ dependencies = [ "sp-api", "sp-application-crypto", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-runtime", "sp-runtime-interface 17.0.0", diff --git a/polkadot/node/core/pvf/common/Cargo.toml b/polkadot/node/core/pvf/common/Cargo.toml index 7dc8d307026e..ad5886f379b5 100644 --- a/polkadot/node/core/pvf/common/Cargo.toml +++ b/polkadot/node/core/pvf/common/Cargo.toml @@ -23,6 +23,7 @@ sc-executor-common = { path = "../../../../../substrate/client/executor/common" sc-executor-wasmtime = { path = "../../../../../substrate/client/executor/wasmtime" } sp-core = { path = "../../../../../substrate/primitives/core" } +sp-crypto-hashing = { path = "../../../../../substrate/primitives/crypto/hashing" } sp-externalities = { path = "../../../../../substrate/primitives/externalities" } sp-io = { path = "../../../../../substrate/primitives/io" } sp-tracing = { path = "../../../../../substrate/primitives/tracing" } diff --git a/polkadot/node/core/pvf/common/src/pvf.rs b/polkadot/node/core/pvf/common/src/pvf.rs index 0cc86434c195..56c8dd11eb57 100644 --- a/polkadot/node/core/pvf/common/src/pvf.rs +++ b/polkadot/node/core/pvf/common/src/pvf.rs @@ -18,7 +18,6 @@ use crate::prepare::PrepareJobKind; use parity_scale_codec::{Decode, Encode}; use polkadot_parachain_primitives::primitives::ValidationCodeHash; use polkadot_primitives::ExecutorParams; -use sp_core::blake2_256; use std::{ cmp::{Eq, PartialEq}, fmt, @@ -53,7 +52,7 @@ impl PvfPrepData { prep_kind: PrepareJobKind, ) -> Self { let code = Arc::new(code); - let code_hash = blake2_256(&code).into(); + let code_hash = sp_crypto_hashing::blake2_256(&code).into(); let executor_params = Arc::new(executor_params); Self { code, code_hash, executor_params, prep_timeout, prep_kind } } diff --git a/polkadot/node/network/gossip-support/Cargo.toml b/polkadot/node/network/gossip-support/Cargo.toml index a9f68261addf..93b4ae88af9a 100644 --- a/polkadot/node/network/gossip-support/Cargo.toml +++ b/polkadot/node/network/gossip-support/Cargo.toml @@ -10,6 +10,7 @@ license.workspace = true sp-application-crypto = { path = "../../../../substrate/primitives/application-crypto" } sp-keystore = { path = "../../../../substrate/primitives/keystore" } sp-core = { path = "../../../../substrate/primitives/core" } +sp-crypto-hashing = { path = "../../../../substrate/primitives/crypto/hashing" } sc-network = { path = "../../../../substrate/client/network" } sc-network-common = { path = "../../../../substrate/client/network/common" } diff --git a/polkadot/node/network/gossip-support/src/lib.rs b/polkadot/node/network/gossip-support/src/lib.rs index 674c86e5ce27..264d29b79b87 100644 --- a/polkadot/node/network/gossip-support/src/lib.rs +++ b/polkadot/node/network/gossip-support/src/lib.rs @@ -593,7 +593,7 @@ async fn update_gossip_topology( let mut subject = [0u8; 40]; subject[..8].copy_from_slice(b"gossipsu"); subject[8..].copy_from_slice(&randomness); - sp_core::blake2_256(&subject) + sp_crypto_hashing::blake2_256(&subject) }; // shuffle the validators and create the index mapping diff --git a/substrate/client/chain-spec/Cargo.toml b/substrate/client/chain-spec/Cargo.toml index 5b7cdda8ebe4..099aeec58288 100644 --- a/substrate/client/chain-spec/Cargo.toml +++ b/substrate/client/chain-spec/Cargo.toml @@ -25,6 +25,7 @@ sc-network = { path = "../network" } sc-telemetry = { path = "../telemetry" } sp-blockchain = { path = "../../primitives/blockchain" } sp-core = { path = "../../primitives/core" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } sp-genesis-builder = { path = "../../primitives/genesis-builder" } sp-runtime = { path = "../../primitives/runtime" } sp-state-machine = { path = "../../primitives/state-machine" } diff --git a/substrate/client/chain-spec/src/genesis_config_builder.rs b/substrate/client/chain-spec/src/genesis_config_builder.rs index 9ccf6b4efb20..e79c9ae9eb78 100644 --- a/substrate/client/chain-spec/src/genesis_config_builder.rs +++ b/substrate/client/chain-spec/src/genesis_config_builder.rs @@ -62,7 +62,7 @@ where pub fn new(code: &'a [u8]) -> Self { GenesisConfigBuilderRuntimeCaller { code: code.into(), - code_hash: sp_core::blake2_256(code).to_vec(), + code_hash: sp_crypto_hashing::blake2_256(code).to_vec(), executor: WasmExecutor::<(sp_io::SubstrateHostFunctions, EHF)>::builder() .with_allow_missing_host_functions(true) .build(), diff --git a/substrate/client/consensus/babe/Cargo.toml b/substrate/client/consensus/babe/Cargo.toml index c8cff0981b36..4cbbe2fea31f 100644 --- a/substrate/client/consensus/babe/Cargo.toml +++ b/substrate/client/consensus/babe/Cargo.toml @@ -39,6 +39,7 @@ sp-consensus = { path = "../../../primitives/consensus/common" } sp-consensus-babe = { path = "../../../primitives/consensus/babe" } sp-consensus-slots = { path = "../../../primitives/consensus/slots" } sp-core = { path = "../../../primitives/core" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } sp-inherents = { path = "../../../primitives/inherents" } sp-keystore = { path = "../../../primitives/keystore" } sp-runtime = { path = "../../../primitives/runtime" } diff --git a/substrate/client/consensus/babe/src/authorship.rs b/substrate/client/consensus/babe/src/authorship.rs index 3580caba7461..1d97916ac1ef 100644 --- a/substrate/client/consensus/babe/src/authorship.rs +++ b/substrate/client/consensus/babe/src/authorship.rs @@ -27,7 +27,6 @@ use sp_consensus_babe::{ make_vrf_sign_data, AuthorityId, BabeAuthorityWeight, Randomness, Slot, }; use sp_core::{ - blake2_256, crypto::{ByteArray, Wraps}, U256, }; @@ -109,7 +108,7 @@ pub(super) fn secondary_slot_author( return None } - let rand = U256::from((randomness, slot).using_encoded(blake2_256)); + let rand = U256::from((randomness, slot).using_encoded(sp_crypto_hashing::blake2_256)); let authorities_len = U256::from(authorities.len()); let idx = rand % authorities_len; diff --git a/substrate/client/consensus/beefy/Cargo.toml b/substrate/client/consensus/beefy/Cargo.toml index aae5a44d7fa2..5515c6fd0216 100644 --- a/substrate/client/consensus/beefy/Cargo.toml +++ b/substrate/client/consensus/beefy/Cargo.toml @@ -33,6 +33,7 @@ sp-blockchain = { path = "../../../primitives/blockchain" } sp-consensus = { path = "../../../primitives/consensus/common" } sp-consensus-beefy = { path = "../../../primitives/consensus/beefy" } sp-core = { path = "../../../primitives/core" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } sp-keystore = { path = "../../../primitives/keystore" } sp-mmr-primitives = { path = "../../../primitives/merkle-mountain-range" } sp-runtime = { path = "../../../primitives/runtime" } diff --git a/substrate/client/consensus/beefy/src/keystore.rs b/substrate/client/consensus/beefy/src/keystore.rs index 925bb0882822..7bc9fe7f04d5 100644 --- a/substrate/client/consensus/beefy/src/keystore.rs +++ b/substrate/client/consensus/beefy/src/keystore.rs @@ -17,7 +17,6 @@ // along with this program. If not, see . use sp_application_crypto::{key_types::BEEFY as BEEFY_KEY_TYPE, RuntimeAppPublic}; -use sp_core::keccak_256; use sp_keystore::KeystorePtr; use log::warn; @@ -74,7 +73,7 @@ impl BeefyKeystore { pub fn sign(&self, public: &Public, message: &[u8]) -> Result { let store = self.0.clone().ok_or_else(|| error::Error::Keystore("no Keystore".into()))?; - let msg = keccak_256(message); + let msg = sp_crypto_hashing::keccak_256(message); let public = public.as_ref(); let sig = store diff --git a/substrate/client/consensus/grandpa/Cargo.toml b/substrate/client/consensus/grandpa/Cargo.toml index 85f98e7546e0..18c0f76e0a0f 100644 --- a/substrate/client/consensus/grandpa/Cargo.toml +++ b/substrate/client/consensus/grandpa/Cargo.toml @@ -46,6 +46,7 @@ sp-arithmetic = { path = "../../../primitives/arithmetic" } sp-blockchain = { path = "../../../primitives/blockchain" } sp-consensus = { path = "../../../primitives/consensus/common" } sp-core = { path = "../../../primitives/core" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa" } sp-keystore = { path = "../../../primitives/keystore" } sp-runtime = { path = "../../../primitives/runtime" } diff --git a/substrate/client/consensus/grandpa/src/import.rs b/substrate/client/consensus/grandpa/src/import.rs index ca5b7c400bfb..bc2983569c53 100644 --- a/substrate/client/consensus/grandpa/src/import.rs +++ b/substrate/client/consensus/grandpa/src/import.rs @@ -32,7 +32,6 @@ use sp_api::{Core, RuntimeApiInfo}; use sp_blockchain::BlockStatus; use sp_consensus::{BlockOrigin, Error as ConsensusError, SelectChain}; use sp_consensus_grandpa::{ConsensusLog, GrandpaApi, ScheduledChange, SetId, GRANDPA_ENGINE_ID}; -use sp_core::hashing::twox_128; use sp_runtime::{ generic::OpaqueDigestItemId, traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero}, @@ -438,7 +437,11 @@ where // The new API is not supported in this runtime. Try reading directly from storage. // This code may be removed once warp sync to an old runtime is no longer needed. for prefix in ["GrandpaFinality", "Grandpa"] { - let k = [twox_128(prefix.as_bytes()), twox_128(b"CurrentSetId")].concat(); + let k = [ + sp_crypto_hashing::twox_128(prefix.as_bytes()), + sp_crypto_hashing::twox_128(b"CurrentSetId"), + ] + .concat(); if let Ok(Some(id)) = self.inner.storage(hash, &sc_client_api::StorageKey(k.to_vec())) { diff --git a/substrate/client/sysinfo/Cargo.toml b/substrate/client/sysinfo/Cargo.toml index 86f03050c44e..bed53f86a6fa 100644 --- a/substrate/client/sysinfo/Cargo.toml +++ b/substrate/client/sysinfo/Cargo.toml @@ -25,6 +25,7 @@ serde = { version = "1.0.188", features = ["derive"] } serde_json = "1.0.108" sc-telemetry = { path = "../telemetry" } sp-core = { path = "../../primitives/core" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } sp-io = { path = "../../primitives/io" } sp-std = { path = "../../primitives/std" } diff --git a/substrate/client/sysinfo/src/sysinfo.rs b/substrate/client/sysinfo/src/sysinfo.rs index bef87a83e46f..3fa583cf7aca 100644 --- a/substrate/client/sysinfo/src/sysinfo.rs +++ b/substrate/client/sysinfo/src/sysinfo.rs @@ -365,7 +365,7 @@ pub fn benchmark_cpu(limit: ExecutionLimit) -> Throughput { let run = || -> Result<(), ()> { clobber_slice(&mut buffer); - hash = sp_core::hashing::blake2_256(&buffer); + hash = sp_crypto_hashing::blake2_256(&buffer); clobber_slice(&mut hash); Ok(()) diff --git a/substrate/primitives/consensus/beefy/Cargo.toml b/substrate/primitives/consensus/beefy/Cargo.toml index 5ff0a2ebc70f..0a0ee61b6cbb 100644 --- a/substrate/primitives/consensus/beefy/Cargo.toml +++ b/substrate/primitives/consensus/beefy/Cargo.toml @@ -18,6 +18,7 @@ serde = { version = "1.0.188", default-features = false, optional = true, featu 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} diff --git a/substrate/primitives/consensus/beefy/src/test_utils.rs b/substrate/primitives/consensus/beefy/src/test_utils.rs index b83f657af38e..a6e65e5bff0b 100644 --- a/substrate/primitives/consensus/beefy/src/test_utils.rs +++ b/substrate/primitives/consensus/beefy/src/test_utils.rs @@ -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() } diff --git a/substrate/primitives/core/src/ecdsa.rs b/substrate/primitives/core/src/ecdsa.rs index fcb22fc165e0..83cdedd5d840 100644 --- a/substrate/primitives/core/src/ecdsa.rs +++ b/substrate/primitives/core/src/ecdsa.rs @@ -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>(&self, message: M) -> Option { - 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. @@ -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>(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()?; @@ -771,7 +769,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); } @@ -781,12 +779,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())); } @@ -795,7 +793,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); @@ -804,7 +802,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); } diff --git a/substrate/primitives/core/src/hasher.rs b/substrate/primitives/core/src/hasher.rs index 5c4c40cba8e2..fc613dba161e 100644 --- a/substrate/primitives/core/src/hasher.rs +++ b/substrate/primitives/core/src/hasher.rs @@ -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() } } } diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 1f865c7eeec4..434a83c76e02 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -46,10 +46,10 @@ pub use sp_debug_derive::RuntimeDebug; #[cfg(feature = "serde")] pub use impl_serde::serialize as bytes; -#[cfg(feature = "full_crypto")] -pub use sp_crypto_hashing::{ - self as hashing, blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64, -}; +// #[cfg(feature = "full_crypto")] +// pub use sp_crypto_hashing::{ +// self as hashing, blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64, +// }; pub mod crypto; pub mod hexdisplay; pub use paste; diff --git a/substrate/primitives/io/Cargo.toml b/substrate/primitives/io/Cargo.toml index 445104b736e0..7a893d74bf18 100644 --- a/substrate/primitives/io/Cargo.toml +++ b/substrate/primitives/io/Cargo.toml @@ -19,6 +19,7 @@ targets = ["x86_64-unknown-linux-gnu"] 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 } @@ -49,6 +50,7 @@ std = [ "log/std", "secp256k1", "sp-core/std", + "sp-crypto-hashing/std", "sp-externalities/std", "sp-keystore/std", "sp-runtime-interface/std", diff --git a/substrate/primitives/io/src/lib.rs b/substrate/primitives/io/src/lib.rs index c4182d6ab3a0..8ab552a53f91 100644 --- a/substrate/primitives/io/src/lib.rs +++ b/substrate/primitives/io/src/lib.rs @@ -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) } } diff --git a/substrate/primitives/statement-store/Cargo.toml b/substrate/primitives/statement-store/Cargo.toml index 658229cef220..92813a2db3b9 100644 --- a/substrate/primitives/statement-store/Cargo.toml +++ b/substrate/primitives/statement-store/Cargo.toml @@ -16,6 +16,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} @@ -49,6 +50,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", diff --git a/substrate/primitives/statement-store/src/lib.rs b/substrate/primitives/statement-store/src/lib.rs index 67e7a7b3896b..5e2e3b8bc18e 100644 --- a/substrate/primitives/statement-store/src/lib.rs +++ b/substrate/primitives/statement-store/src/lib.rs @@ -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. diff --git a/substrate/utils/frame/remote-externalities/Cargo.toml b/substrate/utils/frame/remote-externalities/Cargo.toml index 7067aed238ac..92f25874edee 100644 --- a/substrate/utils/frame/remote-externalities/Cargo.toml +++ b/substrate/utils/frame/remote-externalities/Cargo.toml @@ -17,6 +17,7 @@ codec = { package = "parity-scale-codec", version = "3.6.1" } log = "0.4.17" serde = "1.0.188" sp-core = { path = "../../../primitives/core" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } sp-state-machine = { path = "../../../primitives/state-machine" } sp-io = { path = "../../../primitives/io" } sp-runtime = { path = "../../../primitives/runtime" } diff --git a/substrate/utils/frame/remote-externalities/src/lib.rs b/substrate/utils/frame/remote-externalities/src/lib.rs index 71e9320ebeeb..dfd6a90534e2 100644 --- a/substrate/utils/frame/remote-externalities/src/lib.rs +++ b/substrate/utils/frame/remote-externalities/src/lib.rs @@ -29,7 +29,6 @@ use jsonrpsee::{ use log::*; use serde::de::DeserializeOwned; use sp_core::{ - hashing::twox_128, hexdisplay::HexDisplay, storage::{ well_known_keys::{is_default_child_storage_key, DEFAULT_CHILD_STORAGE_KEY_PREFIX}, @@ -902,10 +901,11 @@ where // Then, a few transformation that we want to perform in the online config: let online_config = self.as_online_mut(); - online_config - .pallets - .iter() - .for_each(|p| online_config.hashed_prefixes.push(twox_128(p.as_bytes()).to_vec())); + online_config.pallets.iter().for_each(|p| { + online_config + .hashed_prefixes + .push(sp_crypto_hashing::twox_128(p.as_bytes()).to_vec()) + }); if online_config.child_trie { online_config.hashed_prefixes.push(DEFAULT_CHILD_STORAGE_KEY_PREFIX.to_vec()); From 9b51b288771546387d9dcaa281b623ac5b4766c3 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Wed, 22 Nov 2023 12:56:32 +0100 Subject: [PATCH 03/15] Facade for deprecated crates --- Cargo.lock | 14 +++++++++++ Cargo.toml | 2 ++ substrate/deprecated/hashing/Cargo.toml | 23 +++++++++++++++++++ substrate/deprecated/hashing/README.md | 3 +++ .../deprecated/hashing/proc-macro/Cargo.toml | 19 +++++++++++++++ .../deprecated/hashing/proc-macro/README.md | 3 +++ .../deprecated/hashing/proc-macro/src/lib.rs | 18 +++++++++++++++ substrate/deprecated/hashing/src/lib.rs | 18 +++++++++++++++ 8 files changed, 100 insertions(+) create mode 100644 substrate/deprecated/hashing/Cargo.toml create mode 100644 substrate/deprecated/hashing/README.md create mode 100644 substrate/deprecated/hashing/proc-macro/Cargo.toml create mode 100644 substrate/deprecated/hashing/proc-macro/README.md create mode 100644 substrate/deprecated/hashing/proc-macro/src/lib.rs create mode 100644 substrate/deprecated/hashing/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f30a18c28c17..90008d279023 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17441,6 +17441,20 @@ dependencies = [ "zeroize", ] +[[package]] +name = "sp-core-hashing" +version = "9.1.0" +dependencies = [ + "sp-crypto-hashing", +] + +[[package]] +name = "sp-core-hashing-proc-macro" +version = "9.1.0" +dependencies = [ + "sp-crypto-hashing-proc-macro", +] + [[package]] name = "sp-crypto-ec-utils" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index d9ed894e9401..45c6e7e4d92e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -270,6 +270,8 @@ members = [ "substrate/client/transaction-pool", "substrate/client/transaction-pool/api", "substrate/client/utils", + "substrate/deprecated/hashing", + "substrate/deprecated/hashing/proc-macro", "substrate/frame", "substrate/frame/alliance", "substrate/frame/asset-conversion", diff --git a/substrate/deprecated/hashing/Cargo.toml b/substrate/deprecated/hashing/Cargo.toml new file mode 100644 index 000000000000..ba41a9b6e1f4 --- /dev/null +++ b/substrate/deprecated/hashing/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "sp-core-hashing" +version = "9.1.0" +authors.workspace = true +edition.workspace = true +license = "Apache-2.0" +homepage = "https://substrate.io" +repository.workspace = true +description = "Hashing primitives (deprecated: use sp-crypto-hashing for new applications)" +documentation = "https://docs.rs/sp-crypto-hashing" + +[badges] +maintenance = { status = "deprecated" } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } + +[features] +default = [ "std" ] +std = [ "sp-crypto-hashing/std" ] diff --git a/substrate/deprecated/hashing/README.md b/substrate/deprecated/hashing/README.md new file mode 100644 index 000000000000..73b83fd451be --- /dev/null +++ b/substrate/deprecated/hashing/README.md @@ -0,0 +1,3 @@ +# Hashing + +This package has been deprecated. Please use `sp-crypto-hashing`. diff --git a/substrate/deprecated/hashing/proc-macro/Cargo.toml b/substrate/deprecated/hashing/proc-macro/Cargo.toml new file mode 100644 index 000000000000..ba300f673a2b --- /dev/null +++ b/substrate/deprecated/hashing/proc-macro/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "sp-core-hashing-proc-macro" +version = "9.1.0" +authors.workspace = true +edition.workspace = true +license = "Apache-2.0" +homepage = "https://substrate.io" +repository.workspace = true +description = "Procedural macros for calculating static hashes (deprecated in favor of `sp-crypto-hashing-proc-macro`)." +documentation = "https://docs.rs/sp-crypto-hashing-proc-macro" + +[badges] +maintenance = { status = "deprecated" } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +sp-crypto-hashing-proc-macro = { path = "../../../primitives/crypto/hashing/proc-macro" } diff --git a/substrate/deprecated/hashing/proc-macro/README.md b/substrate/deprecated/hashing/proc-macro/README.md new file mode 100644 index 000000000000..c2d9df728702 --- /dev/null +++ b/substrate/deprecated/hashing/proc-macro/README.md @@ -0,0 +1,3 @@ +# Hashing Macros + +This package has been deprecated. Please use `sp-crypto-hashing-proc-macro`. diff --git a/substrate/deprecated/hashing/proc-macro/src/lib.rs b/substrate/deprecated/hashing/proc-macro/src/lib.rs new file mode 100644 index 000000000000..bbcde33990d3 --- /dev/null +++ b/substrate/deprecated/hashing/proc-macro/src/lib.rs @@ -0,0 +1,18 @@ +// 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. + +pub use sp_crypto_hashing_proc_macro::*; diff --git a/substrate/deprecated/hashing/src/lib.rs b/substrate/deprecated/hashing/src/lib.rs new file mode 100644 index 000000000000..4c6da58f4cc7 --- /dev/null +++ b/substrate/deprecated/hashing/src/lib.rs @@ -0,0 +1,18 @@ +// 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. + +pub use sp_crypto_hashing::*; From a75c4663c84492c25b1a8dc9050c9ee653aa5534 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 23 Nov 2023 17:55:26 +0100 Subject: [PATCH 04/15] Refactor hashing benches --- Cargo.lock | 3 +- substrate/primitives/arithmetic/Cargo.toml | 4 +- .../arithmetic/src/helpers_128bit.rs | 2 +- substrate/primitives/core/benches/bench.rs | 71 +--------------- substrate/primitives/core/src/lib.rs | 4 +- .../primitives/crypto/hashing/Cargo.toml | 8 ++ .../crypto/hashing/benches/bench.rs | 81 +++++++++++++++++++ 7 files changed, 98 insertions(+), 75 deletions(-) create mode 100644 substrate/primitives/crypto/hashing/benches/bench.rs diff --git a/Cargo.lock b/Cargo.lock index 81839c94a564..b3807c15bee3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17329,7 +17329,7 @@ dependencies = [ "rand 0.8.5", "scale-info", "serde", - "sp-core", + "sp-crypto-hashing", "sp-std 8.0.0", "static_assertions", ] @@ -17646,6 +17646,7 @@ version = "0.1.0" dependencies = [ "blake2b_simd", "byteorder", + "criterion 0.4.0", "digest 0.10.7", "sha2 0.10.7", "sha3", diff --git a/substrate/primitives/arithmetic/Cargo.toml b/substrate/primitives/arithmetic/Cargo.toml index 249aebec68fd..ed7016ad0b86 100644 --- a/substrate/primitives/arithmetic/Cargo.toml +++ b/substrate/primitives/arithmetic/Cargo.toml @@ -28,7 +28,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] @@ -38,7 +38,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. diff --git a/substrate/primitives/arithmetic/src/helpers_128bit.rs b/substrate/primitives/arithmetic/src/helpers_128bit.rs index 9b9c74ba5577..4e234d4026c9 100644 --- a/substrate/primitives/arithmetic/src/helpers_128bit.rs +++ b/substrate/primitives/arithmetic/src/helpers_128bit.rs @@ -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] diff --git a/substrate/primitives/core/benches/bench.rs b/substrate/primitives/core/benches/bench.rs index ffca819b638e..0527c2b178cb 100644 --- a/substrate/primitives/core/benches/bench.rs +++ b/substrate/primitives/core/benches/bench.rs @@ -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 { - 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) { - b.iter(|| { - let _a = blake2_128(black_box(key)); - }); -} - -fn bench_twox_128(b: &mut Bencher, key: &Vec) { - 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); diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 40d332f7feaf..798617f2f5e5 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -47,9 +47,7 @@ pub use sp_debug_derive::RuntimeDebug; pub use impl_serde::serialize as bytes; // #[cfg(feature = "full_crypto")] -// pub use sp_crypto_hashing::{ -// self as hashing, blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64, -// }; +// pub use sp_crypto_hashing as hashing; pub mod crypto; pub mod hexdisplay; pub use paste; diff --git a/substrate/primitives/crypto/hashing/Cargo.toml b/substrate/primitives/crypto/hashing/Cargo.toml index 98fc0975588e..22a0a95a56fa 100644 --- a/substrate/primitives/crypto/hashing/Cargo.toml +++ b/substrate/primitives/crypto/hashing/Cargo.toml @@ -21,8 +21,16 @@ 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 = [ diff --git a/substrate/primitives/crypto/hashing/benches/bench.rs b/substrate/primitives/crypto/hashing/benches/bench.rs new file mode 100644 index 000000000000..9afbcd30e322 --- /dev/null +++ b/substrate/primitives/crypto/hashing/benches/bench.rs @@ -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) { + b.iter(|| { + let _a = sp_crypto_hashing::blake2_128(black_box(buf)); + }); +} + +fn bench_twox_128(b: &mut Bencher, buf: &Vec) { + b.iter(|| { + let _a = sp_crypto_hashing::twox_128(black_box(buf)); + }); +} + +fn bench_blake2_256(b: &mut Bencher, buf: &Vec) { + b.iter(|| { + let _a = sp_crypto_hashing::blake2_256(black_box(buf)); + }); +} + +fn bench_twox_256(b: &mut Bencher, buf: &Vec) { + b.iter(|| { + let _a = sp_crypto_hashing::twox_256(black_box(buf)); + }); +} + +fn bench_sha_256(b: &mut Bencher, buf: &Vec) { + b.iter(|| { + let _a = sp_crypto_hashing::sha2_256(black_box(buf)); + }); +} + +fn bench_keccak_256(b: &mut Bencher, buf: &Vec) { + 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); From c6fba6fa584f08c68dd23d91c0ac155bcc993e6a Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Fri, 24 Nov 2023 10:30:36 +0100 Subject: [PATCH 05/15] Fix some tests --- substrate/primitives/consensus/beefy/src/commitment.rs | 3 ++- substrate/primitives/consensus/beefy/src/lib.rs | 3 ++- substrate/primitives/consensus/beefy/src/witness.rs | 3 ++- substrate/primitives/core/src/lib.rs | 10 ++++++++-- substrate/primitives/keystore/src/testing.rs | 7 ++++--- substrate/primitives/statement-store/src/lib.rs | 2 +- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/substrate/primitives/consensus/beefy/src/commitment.rs b/substrate/primitives/consensus/beefy/src/commitment.rs index 5b6ef9ae5ab3..37be1a4f6fc3 100644 --- a/substrate/primitives/consensus/beefy/src/commitment.rs +++ b/substrate/primitives/consensus/beefy/src/commitment.rs @@ -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; diff --git a/substrate/primitives/consensus/beefy/src/lib.rs b/substrate/primitives/consensus/beefy/src/lib.rs index e31c53237be2..68eeeb3c6800 100644 --- a/substrate/primitives/consensus/beefy/src/lib.rs +++ b/substrate/primitives/consensus/beefy/src/lib.rs @@ -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] diff --git a/substrate/primitives/consensus/beefy/src/witness.rs b/substrate/primitives/consensus/beefy/src/witness.rs index 3f2c2bcbe282..b633453340bb 100644 --- a/substrate/primitives/consensus/beefy/src/witness.rs +++ b/substrate/primitives/consensus/beefy/src/witness.rs @@ -77,7 +77,8 @@ impl #[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; diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 798617f2f5e5..29cf312433f4 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -46,8 +46,14 @@ pub use sp_debug_derive::RuntimeDebug; #[cfg(feature = "serde")] pub use impl_serde::serialize as bytes; -// #[cfg(feature = "full_crypto")] -// pub use sp_crypto_hashing as hashing; +// https://github.com/rust-lang/rust/issues/85388 +#[cfg(feature = "full_crypto")] +#[deprecated( + since = "21.0.0", + note = "hashing re-export is scheduled for removal, please use `sp-crypto-hashing` instead" +)] +pub use sp_crypto_hashing as hashing; + pub mod crypto; pub mod hexdisplay; pub use paste; diff --git a/substrate/primitives/keystore/src/testing.rs b/substrate/primitives/keystore/src/testing.rs index 08110e8e4979..cc5de148256b 100644 --- a/substrate/primitives/keystore/src/testing.rs +++ b/substrate/primitives/keystore/src/testing.rs @@ -478,16 +478,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"); + // Lets 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()); } diff --git a/substrate/primitives/statement-store/src/lib.rs b/substrate/primitives/statement-store/src/lib.rs index 5e2e3b8bc18e..04175f6d6160 100644 --- a/substrate/primitives/statement-store/src/lib.rs +++ b/substrate/primitives/statement-store/src/lib.rs @@ -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 )) ); From 7d8374262d42c005c7c64a8345210dc7bd9a57da Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Fri, 24 Nov 2023 10:58:07 +0100 Subject: [PATCH 06/15] Re-export hashing in core --- substrate/primitives/core/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index 29cf312433f4..f063c970c947 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -52,7 +52,7 @@ pub use impl_serde::serialize as bytes; since = "21.0.0", note = "hashing re-export is scheduled for removal, please use `sp-crypto-hashing` instead" )] -pub use sp_crypto_hashing as hashing; +pub use sp_crypto_hashing::{self as hashing, *}; pub mod crypto; pub mod hexdisplay; From 511718dd8ef38ad9a293ed700fcd20db99cc6e25 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Fri, 24 Nov 2023 11:19:18 +0100 Subject: [PATCH 07/15] Fix import --- substrate/client/consensus/beefy/src/keystore.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/substrate/client/consensus/beefy/src/keystore.rs b/substrate/client/consensus/beefy/src/keystore.rs index 7bc9fe7f04d5..75c44de3324c 100644 --- a/substrate/client/consensus/beefy/src/keystore.rs +++ b/substrate/client/consensus/beefy/src/keystore.rs @@ -17,6 +17,7 @@ // along with this program. If not, see . use sp_application_crypto::{key_types::BEEFY as BEEFY_KEY_TYPE, RuntimeAppPublic}; +use sp_crypto_hashing::keccak_256; use sp_keystore::KeystorePtr; use log::warn; @@ -73,7 +74,7 @@ impl BeefyKeystore { pub fn sign(&self, public: &Public, message: &[u8]) -> Result { let store = self.0.clone().ok_or_else(|| error::Error::Keystore("no Keystore".into()))?; - let msg = sp_crypto_hashing::keccak_256(message); + let msg = keccak_256(message); let public = public.as_ref(); let sig = store From 88c39540dd4e49ca5181c35deaf05266dbfe5d07 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Fri, 24 Nov 2023 11:32:16 +0100 Subject: [PATCH 08/15] Zepter fix --- substrate/primitives/consensus/beefy/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/primitives/consensus/beefy/Cargo.toml b/substrate/primitives/consensus/beefy/Cargo.toml index 7faee27d34b9..dbd9566c5b98 100644 --- a/substrate/primitives/consensus/beefy/Cargo.toml +++ b/substrate/primitives/consensus/beefy/Cargo.toml @@ -39,6 +39,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", From f785460ae8c2b50adae3179b4b4d31595b4237e9 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 09:14:26 +0100 Subject: [PATCH 09/15] Post master merge fix --- Cargo.lock | 62 +++++++++++++------ substrate/deprecated/hashing/Cargo.toml | 2 +- .../deprecated/hashing/proc-macro/Cargo.toml | 2 +- substrate/frame/support/procedural/Cargo.toml | 2 +- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d404a2c231b..e3005cc30bfd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5651,6 +5651,7 @@ dependencies = [ "parity-scale-codec", "serde", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-state-machine", @@ -5688,7 +5689,7 @@ dependencies = [ "sp-api", "sp-arithmetic", "sp-core", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing-proc-macro", "sp-debug-derive 8.0.0", "sp-genesis-builder", "sp-inherents", @@ -5719,7 +5720,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "sp-core-hashing", + "sp-crypto-hashing", "syn 2.0.48", ] @@ -9055,7 +9056,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-core-hashing", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -12330,6 +12331,7 @@ dependencies = [ "sp-authority-discovery", "sp-consensus-babe", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-tracing 10.0.0", @@ -12743,6 +12745,7 @@ dependencies = [ "sc-executor-wasmtime", "seccompiler", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-io", "sp-tracing 10.0.0", @@ -15449,6 +15452,7 @@ dependencies = [ "sp-blockchain", "sp-consensus-babe", "sp-core", + "sp-crypto-hashing", "sp-genesis-builder", "sp-io", "sp-keyring", @@ -15664,6 +15668,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-slots", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-keyring", "sp-keystore", @@ -15733,6 +15738,7 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-mmr-primitives", @@ -15817,6 +15823,7 @@ dependencies = [ "sp-consensus", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-runtime", @@ -16659,6 +16666,7 @@ dependencies = [ "serde", "serde_json", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -18067,7 +18075,7 @@ dependencies = [ "rand 0.8.5", "scale-info", "serde", - "sp-core", + "sp-crypto-hashing", "sp-std 8.0.0", "static_assertions", ] @@ -18201,6 +18209,7 @@ dependencies = [ "sp-api", "sp-application-crypto", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-mmr-primitives", "sp-runtime", @@ -18298,8 +18307,7 @@ dependencies = [ "secrecy", "serde", "serde_json", - "sp-core-hashing", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing", "sp-debug-derive 8.0.0", "sp-externalities 0.19.0", "sp-runtime-interface 17.0.0", @@ -18325,23 +18333,16 @@ dependencies = [ [[package]] name = "sp-core-hashing" -version = "9.0.0" +version = "9.1.0" dependencies = [ - "blake2b_simd", - "byteorder", - "digest 0.10.7", - "sha2 0.10.7", - "sha3", - "twox-hash", + "sp-crypto-hashing", ] [[package]] name = "sp-core-hashing-proc-macro" -version = "9.0.0" +version = "9.1.0" dependencies = [ - "quote", - "sp-core-hashing", - "syn 2.0.48", + "sp-crypto-hashing-proc-macro", ] [[package]] @@ -18385,6 +18386,29 @@ dependencies = [ "sp-std 8.0.0 (git+https://github.com/paritytech/polkadot-sdk)", ] +[[package]] +name = "sp-crypto-hashing" +version = "0.1.0" +dependencies = [ + "blake2b_simd", + "byteorder", + "criterion 0.4.0", + "digest 0.10.7", + "sha2 0.10.7", + "sha3", + "sp-crypto-hashing-proc-macro", + "twox-hash", +] + +[[package]] +name = "sp-crypto-hashing-proc-macro" +version = "0.1.0" +dependencies = [ + "quote", + "sp-crypto-hashing", + "syn 2.0.48", +] + [[package]] name = "sp-database" version = "4.0.0-dev" @@ -18469,6 +18493,7 @@ dependencies = [ "rustversion", "secp256k1", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-keystore", "sp-runtime-interface 17.0.0", @@ -18801,6 +18826,7 @@ dependencies = [ "sp-api", "sp-application-crypto", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-runtime", "sp-runtime-interface 17.0.0", @@ -18950,7 +18976,7 @@ dependencies = [ "parity-wasm", "scale-info", "serde", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing-proc-macro", "sp-runtime", "sp-std 8.0.0", "sp-version-proc-macro", diff --git a/substrate/deprecated/hashing/Cargo.toml b/substrate/deprecated/hashing/Cargo.toml index ba41a9b6e1f4..3d756690cdee 100644 --- a/substrate/deprecated/hashing/Cargo.toml +++ b/substrate/deprecated/hashing/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-core-hashing" -version = "9.1.0" +version = "15.0.0" authors.workspace = true edition.workspace = true license = "Apache-2.0" diff --git a/substrate/deprecated/hashing/proc-macro/Cargo.toml b/substrate/deprecated/hashing/proc-macro/Cargo.toml index ba300f673a2b..aa78809241f0 100644 --- a/substrate/deprecated/hashing/proc-macro/Cargo.toml +++ b/substrate/deprecated/hashing/proc-macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-core-hashing-proc-macro" -version = "9.1.0" +version = "15.0.0" authors.workspace = true edition.workspace = true license = "Apache-2.0" diff --git a/substrate/frame/support/procedural/Cargo.toml b/substrate/frame/support/procedural/Cargo.toml index 9051d97501e4..e2391505c649 100644 --- a/substrate/frame/support/procedural/Cargo.toml +++ b/substrate/frame/support/procedural/Cargo.toml @@ -36,7 +36,7 @@ regex = "1" [features] default = ["std"] -std = ["sp-core-hashing/std"] +std = ["sp-crypto-hashing/std"] no-metadata-docs = [] # Generate impl-trait for tuples with the given number of tuples. Will be needed as the number of # pallets in a runtime grows. Does increase the compile time! From e5da52a9527e13181a1ef7424bcf135a60acf40f Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 12:24:04 +0100 Subject: [PATCH 10/15] Do not hashing procedures re-exported by sp-core (deprecated) --- Cargo.lock | 22 +++++++++--- .../outbound-queue/merkle-tree/Cargo.toml | 1 + .../outbound-queue/merkle-tree/src/lib.rs | 2 +- cumulus/client/parachain-inherent/Cargo.toml | 2 +- cumulus/client/parachain-inherent/src/mock.rs | 2 +- cumulus/pallets/parachain-system/Cargo.toml | 1 + cumulus/pallets/parachain-system/src/tests.rs | 2 +- cumulus/xcm/xcm-emulator/Cargo.toml | 1 + cumulus/xcm/xcm-emulator/src/lib.rs | 3 +- polkadot/runtime/parachains/Cargo.toml | 1 + polkadot/runtime/parachains/src/ump_tests.rs | 10 +++--- substrate/bin/node/cli/Cargo.toml | 1 + substrate/bin/node/cli/tests/common.rs | 2 +- substrate/bin/node/testing/Cargo.toml | 6 ++-- substrate/bin/node/testing/src/bench.rs | 5 +-- substrate/bin/node/testing/src/keyring.rs | 20 ++++++----- substrate/client/executor/Cargo.toml | 1 + .../executor/src/integration_tests/mod.rs | 35 +++++++------------ substrate/client/network/bitswap/Cargo.toml | 2 +- substrate/client/network/bitswap/src/lib.rs | 2 +- substrate/client/rpc/Cargo.toml | 1 + substrate/client/rpc/src/author/tests.rs | 2 +- substrate/client/rpc/src/state/tests.rs | 6 ++-- substrate/client/transaction-pool/Cargo.toml | 1 + .../client/transaction-pool/benches/basics.rs | 2 +- substrate/frame/message-queue/Cargo.toml | 1 + substrate/frame/message-queue/src/tests.rs | 2 +- substrate/frame/sassafras/Cargo.toml | 1 + substrate/frame/sassafras/src/mock.rs | 2 +- substrate/frame/society/Cargo.toml | 1 + substrate/frame/society/src/tests.rs | 2 +- substrate/frame/support/Cargo.toml | 1 + substrate/frame/support/src/storage/mod.rs | 2 +- .../frame/support/src/traits/metadata.rs | 2 +- substrate/primitives/core/src/lib.rs | 4 +-- substrate/test-utils/runtime/Cargo.toml | 1 + substrate/test-utils/runtime/src/lib.rs | 6 ++-- 37 files changed, 87 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e3005cc30bfd..8709017bcaff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3774,7 +3774,7 @@ dependencies = [ "sc-client-api", "scale-info", "sp-api", - "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-runtime", "sp-state-machine", @@ -3912,6 +3912,7 @@ dependencies = [ "sc-client-api", "scale-info", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-inherents", "sp-io", @@ -5689,6 +5690,7 @@ dependencies = [ "sp-api", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-crypto-hashing-proc-macro", "sp-debug-derive 8.0.0", "sp-genesis-builder", @@ -8780,6 +8782,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-io", "sp-keyring", @@ -10152,6 +10155,7 @@ dependencies = [ "serde", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -10678,6 +10682,7 @@ dependencies = [ "scale-info", "sp-consensus-sassafras", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -10789,6 +10794,7 @@ dependencies = [ "scale-info", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -13322,6 +13328,7 @@ dependencies = [ "sp-application-crypto", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-io", "sp-keyring", @@ -15963,6 +15970,7 @@ dependencies = [ "schnellru", "sp-api", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-io", "sp-maybe-compressed-blob", @@ -16144,7 +16152,7 @@ dependencies = [ "sc-network", "sp-blockchain", "sp-consensus", - "sp-core", + "sp-crypto-hashing", "sp-runtime", "substrate-test-runtime", "substrate-test-runtime-client", @@ -16396,6 +16404,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-keystore", "sp-offchain", @@ -16753,6 +16762,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-crypto-hashing", "sp-runtime", "sp-tracing 10.0.0", "sp-transaction-pool", @@ -17686,6 +17696,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", + "sp-crypto-hashing", "sp-runtime", ] @@ -18333,14 +18344,14 @@ dependencies = [ [[package]] name = "sp-core-hashing" -version = "9.1.0" +version = "15.0.0" dependencies = [ "sp-crypto-hashing", ] [[package]] name = "sp-core-hashing-proc-macro" -version = "9.1.0" +version = "15.0.0" dependencies = [ "sp-crypto-hashing-proc-macro", ] @@ -19206,6 +19217,7 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-inherents", "sp-io", @@ -19649,6 +19661,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-genesis-builder", "sp-inherents", @@ -22093,6 +22106,7 @@ dependencies = [ "polkadot-runtime-parachains", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml index 27c4ae02e52e..7d62f0856406 100644 --- a/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml @@ -26,6 +26,7 @@ hex-literal = { version = "0.4.1" } env_logger = "0.9" hex = "0.4" array-bytes = "4.1" +sp-crypto-hashing = { path = "../../../../../../substrate/primitives/crypto/hashing" } [features] default = ["std"] diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs index d03eb578ef4d..8c91ccd04d9a 100644 --- a/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs @@ -325,7 +325,7 @@ where mod tests { use super::*; use hex_literal::hex; - use sp_core::keccak_256; + use sp_crypto_hashing::keccak_256; use sp_runtime::traits::Keccak256; fn make_leaves(count: u64) -> Vec { diff --git a/cumulus/client/parachain-inherent/Cargo.toml b/cumulus/client/parachain-inherent/Cargo.toml index b6d477519ecc..e00f3ba26066 100644 --- a/cumulus/client/parachain-inherent/Cargo.toml +++ b/cumulus/client/parachain-inherent/Cargo.toml @@ -15,7 +15,7 @@ tracing = { version = "0.1.37" } # Substrate sc-client-api = { path = "../../../substrate/client/api" } sp-api = { path = "../../../substrate/primitives/api" } -sp-core = { path = "../../../substrate/primitives/core" } +sp-crypto-hashing = { path = "../../../substrate/primitives/crypto/hashing" } sp-inherents = { path = "../../../substrate/primitives/inherents" } sp-runtime = { path = "../../../substrate/primitives/runtime" } sp-state-machine = { path = "../../../substrate/primitives/state-machine" } diff --git a/cumulus/client/parachain-inherent/src/mock.rs b/cumulus/client/parachain-inherent/src/mock.rs index 7af10a661e09..22691006f93e 100644 --- a/cumulus/client/parachain-inherent/src/mock.rs +++ b/cumulus/client/parachain-inherent/src/mock.rs @@ -21,7 +21,7 @@ use cumulus_primitives_core::{ }; use cumulus_primitives_parachain_inherent::MessageQueueChain; use sc_client_api::{Backend, StorageProvider}; -use sp_core::twox_128; +use sp_crypto_hashing::twox_128; use sp_inherents::{InherentData, InherentDataProvider}; use sp_runtime::traits::Block; use std::collections::BTreeMap; diff --git a/cumulus/pallets/parachain-system/Cargo.toml b/cumulus/pallets/parachain-system/Cargo.toml index d24fdfe101e9..cb4873593b00 100644 --- a/cumulus/pallets/parachain-system/Cargo.toml +++ b/cumulus/pallets/parachain-system/Cargo.toml @@ -55,6 +55,7 @@ futures = "0.3.28" # Substrate sc-client-api = { path = "../../../substrate/client/api" } sp-keyring = { path = "../../../substrate/primitives/keyring" } +sp-crypto-hashing = { path = "../../../substrate/primitives/crypto/hashing" } sp-tracing = { path = "../../../substrate/primitives/tracing" } sp-version = { path = "../../../substrate/primitives/version" } diff --git a/cumulus/pallets/parachain-system/src/tests.rs b/cumulus/pallets/parachain-system/src/tests.rs index 7528d3d9fe8d..5ff15036fb6e 100755 --- a/cumulus/pallets/parachain-system/src/tests.rs +++ b/cumulus/pallets/parachain-system/src/tests.rs @@ -1125,7 +1125,7 @@ fn upgrade_version_checks_should_work() { ext.register_extension(sp_core::traits::ReadRuntimeVersionExt::new(read_runtime_version)); ext.execute_with(|| { let new_code = vec![1, 2, 3, 4]; - let new_code_hash = H256(sp_core::blake2_256(&new_code)); + let new_code_hash = H256(sp_crypto_hashing::blake2_256(&new_code)); #[allow(deprecated)] let _authorize = ParachainSystem::authorize_upgrade(RawOrigin::Root.into(), new_code_hash, true); diff --git a/cumulus/xcm/xcm-emulator/Cargo.toml b/cumulus/xcm/xcm-emulator/Cargo.toml index 0f10221d6006..82466327b092 100644 --- a/cumulus/xcm/xcm-emulator/Cargo.toml +++ b/cumulus/xcm/xcm-emulator/Cargo.toml @@ -21,6 +21,7 @@ frame-support = { path = "../../../substrate/frame/support" } frame-system = { path = "../../../substrate/frame/system" } sp-io = { path = "../../../substrate/primitives/io" } sp-core = { path = "../../../substrate/primitives/core" } +sp-crypto-hashing = { path = "../../../substrate/primitives/crypto/hashing" } sp-std = { path = "../../../substrate/primitives/std" } sp-runtime = { path = "../../../substrate/primitives/runtime" } sp-arithmetic = { path = "../../../substrate/primitives/arithmetic" } diff --git a/cumulus/xcm/xcm-emulator/src/lib.rs b/cumulus/xcm/xcm-emulator/src/lib.rs index c5cc632574bc..babb318a9950 100644 --- a/cumulus/xcm/xcm-emulator/src/lib.rs +++ b/cumulus/xcm/xcm-emulator/src/lib.rs @@ -38,7 +38,8 @@ pub use frame_system::{Config as SystemConfig, Pallet as SystemPallet}; pub use pallet_balances::AccountData; pub use pallet_message_queue; pub use sp_arithmetic::traits::Bounded; -pub use sp_core::{blake2_256, parameter_types, sr25519, storage::Storage, Pair}; +pub use sp_core::{parameter_types, sr25519, storage::Storage, Pair}; +pub use sp_crypto_hashing::blake2_256; pub use sp_io::TestExternalities; pub use sp_runtime::BoundedSlice; pub use sp_std::{cell::RefCell, collections::vec_deque::VecDeque, fmt::Debug}; diff --git a/polkadot/runtime/parachains/Cargo.toml b/polkadot/runtime/parachains/Cargo.toml index dcfb7108dd25..9dfa3511bf13 100644 --- a/polkadot/runtime/parachains/Cargo.toml +++ b/polkadot/runtime/parachains/Cargo.toml @@ -66,6 +66,7 @@ frame-support-test = { path = "../../../substrate/frame/support/test" } sc-keystore = { path = "../../../substrate/client/keystore" } test-helpers = { package = "polkadot-primitives-test-helpers", path = "../../primitives/test-helpers" } sp-tracing = { path = "../../../substrate/primitives/tracing" } +sp-crypto-hashing = { path = "../../../substrate/primitives/crypto/hashing" } thousands = "0.2.0" assert_matches = "1" serde_json = "1.0.111" diff --git a/polkadot/runtime/parachains/src/ump_tests.rs b/polkadot/runtime/parachains/src/ump_tests.rs index 426993ffa65a..2ed1d64336b3 100644 --- a/polkadot/runtime/parachains/src/ump_tests.rs +++ b/polkadot/runtime/parachains/src/ump_tests.rs @@ -31,8 +31,7 @@ use frame_support::{ weights::Weight, }; use primitives::{well_known_keys, Id as ParaId, UpwardMessage}; -use sp_core::twox_64; -use sp_io::hashing::blake2_256; +use sp_crypto_hashing::{blake2_256, twox_64}; use sp_runtime::traits::Bounded; use sp_std::prelude::*; @@ -505,6 +504,10 @@ fn overweight_queue_works() { let a_msg_2 = (501u32, "a_msg_2").encode(); let a_msg_3 = (501u32, "a_msg_3").encode(); + let hash_1 = blake2_256(&a_msg_1[..]); + let hash_2 = blake2_256(&a_msg_2[..]); + let hash_3 = blake2_256(&a_msg_3[..]); + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { // HACK: Start with the block number 1. This is needed because should an event be // emitted during the genesis block they will be implicitly wiped. @@ -517,9 +520,6 @@ fn overweight_queue_works() { queue_upward_msg(para_a, a_msg_3.clone()); MessageQueue::service_queues(Weight::from_parts(500, 500)); - let hash_1 = blake2_256(&a_msg_1[..]); - let hash_2 = blake2_256(&a_msg_2[..]); - let hash_3 = blake2_256(&a_msg_3[..]); assert_last_events( [ pallet_message_queue::Event::::Processed { diff --git a/substrate/bin/node/cli/Cargo.toml b/substrate/bin/node/cli/Cargo.toml index 061c9684c226..6803d2def43b 100644 --- a/substrate/bin/node/cli/Cargo.toml +++ b/substrate/bin/node/cli/Cargo.toml @@ -128,6 +128,7 @@ sc-service-test = { path = "../../../client/service/test" } sc-block-builder = { path = "../../../client/block-builder" } sp-tracing = { path = "../../../primitives/tracing" } sp-blockchain = { path = "../../../primitives/blockchain" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } futures = "0.3.21" tempfile = "3.1.0" assert_cmd = "2.0.2" diff --git a/substrate/bin/node/cli/tests/common.rs b/substrate/bin/node/cli/tests/common.rs index 9019594ff627..2d74cdd5a041 100644 --- a/substrate/bin/node/cli/tests/common.rs +++ b/substrate/bin/node/cli/tests/common.rs @@ -112,7 +112,7 @@ pub fn executor_call( let heap_pages = t.storage(sp_core::storage::well_known_keys::HEAP_PAGES); let runtime_code = RuntimeCode { code_fetcher: &sp_core::traits::WrappedRuntimeCode(code.as_slice().into()), - hash: sp_core::blake2_256(&code).to_vec(), + hash: sp_crypto_hashing::blake2_256(&code).to_vec(), heap_pages: heap_pages.and_then(|hp| Decode::decode(&mut &hp[..]).ok()), }; sp_tracing::try_init_simple(); diff --git a/substrate/bin/node/testing/Cargo.toml b/substrate/bin/node/testing/Cargo.toml index 76188ed446c0..eab1a80ee500 100644 --- a/substrate/bin/node/testing/Cargo.toml +++ b/substrate/bin/node/testing/Cargo.toml @@ -35,15 +35,13 @@ sc-client-api = { path = "../../../client/api" } sc-client-db = { path = "../../../client/db", features = ["rocksdb"] } sc-consensus = { path = "../../../client/consensus/common" } sc-executor = { path = "../../../client/executor" } -sc-service = { path = "../../../client/service", features = [ - "rocksdb", - "test-helpers", -] } +sc-service = { path = "../../../client/service", features = [ "rocksdb", "test-helpers", ] } sp-api = { path = "../../../primitives/api" } sp-block-builder = { path = "../../../primitives/block-builder" } sp-blockchain = { path = "../../../primitives/blockchain" } sp-consensus = { path = "../../../primitives/consensus/common" } sp-core = { path = "../../../primitives/core" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } sp-inherents = { path = "../../../primitives/inherents" } sp-io = { path = "../../../primitives/io" } sp-keyring = { path = "../../../primitives/keyring" } diff --git a/substrate/bin/node/testing/src/bench.rs b/substrate/bin/node/testing/src/bench.rs index 98d3b968a358..df302a6453b9 100644 --- a/substrate/bin/node/testing/src/bench.rs +++ b/substrate/bin/node/testing/src/bench.rs @@ -47,7 +47,8 @@ use sc_executor::{WasmExecutionMethod, WasmtimeInstantiationStrategy}; use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_consensus::BlockOrigin; -use sp_core::{blake2_256, ed25519, sr25519, traits::SpawnNamed, Pair, Public}; +use sp_core::{ed25519, sr25519, traits::SpawnNamed, Pair, Public}; +use sp_crypto_hashing::blake2_256; use sp_inherents::InherentData; use sp_runtime::{ traits::{Block as BlockT, IdentifyAccount, Verify}, @@ -574,7 +575,7 @@ impl BenchKeyring { let key = self.accounts.get(&signed).expect("Account id not found in keyring"); let signature = payload.using_encoded(|b| { if b.len() > 256 { - key.sign(&sp_io::hashing::blake2_256(b)) + key.sign(&blake2_256(b)) } else { key.sign(b) } diff --git a/substrate/bin/node/testing/src/keyring.rs b/substrate/bin/node/testing/src/keyring.rs index 6c885cc039a1..f712191bed69 100644 --- a/substrate/bin/node/testing/src/keyring.rs +++ b/substrate/bin/node/testing/src/keyring.rs @@ -23,6 +23,7 @@ use kitchensink_runtime::{CheckedExtrinsic, SessionKeys, SignedExtra, UncheckedE use node_cli::chain_spec::get_from_seed; use node_primitives::{AccountId, Balance, Nonce}; use sp_core::{ecdsa, ed25519, sr25519}; +use sp_crypto_hashing::blake2_256; use sp_keyring::AccountKeyring; use sp_runtime::generic::Era; @@ -96,15 +97,16 @@ pub fn sign( let payload = (xt.function, extra.clone(), spec_version, tx_version, genesis_hash, genesis_hash); let key = AccountKeyring::from_account_id(&signed).unwrap(); - let signature = payload - .using_encoded(|b| { - if b.len() > 256 { - key.sign(&sp_io::hashing::blake2_256(b)) - } else { - key.sign(b) - } - }) - .into(); + let signature = + payload + .using_encoded(|b| { + if b.len() > 256 { + key.sign(&blake2_256(b)) + } else { + key.sign(b) + } + }) + .into(); UncheckedExtrinsic { signature: Some((sp_runtime::MultiAddress::Id(signed), signature, extra)), function: payload.0, diff --git a/substrate/client/executor/Cargo.toml b/substrate/client/executor/Cargo.toml index aa8e8c9abf29..8508fbb98913 100644 --- a/substrate/client/executor/Cargo.toml +++ b/substrate/client/executor/Cargo.toml @@ -40,6 +40,7 @@ assert_matches = "1.3.0" wat = "1.0" sc-runtime-test = { path = "runtime-test" } substrate-test-runtime = { path = "../../test-utils/runtime" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } sp-state-machine = { path = "../../primitives/state-machine" } sp-runtime = { path = "../../primitives/runtime" } sp-maybe-compressed-blob = { path = "../../primitives/maybe-compressed-blob" } diff --git a/substrate/client/executor/src/integration_tests/mod.rs b/substrate/client/executor/src/integration_tests/mod.rs index 0bd080c24357..7f91b3ffe764 100644 --- a/substrate/client/executor/src/integration_tests/mod.rs +++ b/substrate/client/executor/src/integration_tests/mod.rs @@ -25,12 +25,13 @@ use sc_executor_common::{ }; use sc_runtime_test::wasm_binary_unwrap; use sp_core::{ - blake2_128, blake2_256, ed25519, map, + ed25519, map, offchain::{testing, OffchainDbExt, OffchainWorkerExt}, sr25519, traits::Externalities, Pair, }; +use sp_crypto_hashing::{blake2_128, blake2_256, sha2_256, twox_128, twox_256}; use sp_runtime::traits::BlakeTwo256; use sp_state_machine::TestExternalities as CoreTestExternalities; use sp_trie::{LayoutV1 as Layout, TrieConfiguration}; @@ -224,12 +225,12 @@ fn blake2_256_should_work(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( call_in_wasm("test_blake2_256", &[0], wasm_method, &mut ext,).unwrap(), - blake2_256(&b""[..]).to_vec().encode(), + blake2_256(b"").to_vec().encode(), ); assert_eq!( call_in_wasm("test_blake2_256", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,) .unwrap(), - blake2_256(&b"Hello world!"[..]).to_vec().encode(), + blake2_256(b"Hello world!").to_vec().encode(), ); } @@ -239,12 +240,12 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( call_in_wasm("test_blake2_128", &[0], wasm_method, &mut ext,).unwrap(), - blake2_128(&b""[..]).to_vec().encode(), + blake2_128(b"").to_vec().encode(), ); assert_eq!( call_in_wasm("test_blake2_128", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,) .unwrap(), - blake2_128(&b"Hello world!"[..]).to_vec().encode(), + blake2_128(b"Hello world!").to_vec().encode(), ); } @@ -254,18 +255,12 @@ fn sha2_256_should_work(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( call_in_wasm("test_sha2_256", &[0], wasm_method, &mut ext,).unwrap(), - array_bytes::hex2bytes_unchecked( - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - ) - .encode(), + sha2_256(b"").to_vec().encode(), ); assert_eq!( call_in_wasm("test_sha2_256", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,) .unwrap(), - array_bytes::hex2bytes_unchecked( - "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a" - ) - .encode(), + sha2_256(b"Hello world!").to_vec().encode(), ); } @@ -275,18 +270,12 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( call_in_wasm("test_twox_256", &[0], wasm_method, &mut ext,).unwrap(), - array_bytes::hex2bytes_unchecked( - "99e9d85137db46ef4bbea33613baafd56f963c64b1f3685a4eb4abd67ff6203a" - ) - .encode(), + twox_256(b"").to_vec().encode() ); assert_eq!( call_in_wasm("test_twox_256", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,) .unwrap(), - array_bytes::hex2bytes_unchecked( - "b27dfd7f223f177f2a13647b533599af0c07f68bda23d96d059da2b451a35a74" - ) - .encode(), + twox_256(b"Hello world!").to_vec().encode() ); } @@ -296,12 +285,12 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) { let mut ext = ext.ext(); assert_eq!( call_in_wasm("test_twox_128", &[0], wasm_method, &mut ext,).unwrap(), - array_bytes::hex2bytes_unchecked("99e9d85137db46ef4bbea33613baafd5").encode(), + twox_128(b"").to_vec().encode() ); assert_eq!( call_in_wasm("test_twox_128", &b"Hello world!".to_vec().encode(), wasm_method, &mut ext,) .unwrap(), - array_bytes::hex2bytes_unchecked("b27dfd7f223f177f2a13647b533599af").encode(), + twox_128(b"Hello world!").to_vec().encode() ); } diff --git a/substrate/client/network/bitswap/Cargo.toml b/substrate/client/network/bitswap/Cargo.toml index b004c03e0256..cc04418c3938 100644 --- a/substrate/client/network/bitswap/Cargo.toml +++ b/substrate/client/network/bitswap/Cargo.toml @@ -36,7 +36,7 @@ sp-runtime = { path = "../../../primitives/runtime" } tokio = { version = "1.22.0", features = ["full"] } sc-block-builder = { path = "../../block-builder" } sc-consensus = { path = "../../consensus/common" } -sp-core = { path = "../../../primitives/core" } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" } sp-consensus = { path = "../../../primitives/consensus/common" } substrate-test-runtime = { path = "../../../test-utils/runtime" } substrate-test-runtime-client = { path = "../../../test-utils/runtime/client" } diff --git a/substrate/client/network/bitswap/src/lib.rs b/substrate/client/network/bitswap/src/lib.rs index 9200c2fa7726..0586354d6a0d 100644 --- a/substrate/client/network/bitswap/src/lib.rs +++ b/substrate/client/network/bitswap/src/lib.rs @@ -500,7 +500,7 @@ mod tests { 0x70, cid::multihash::Multihash::wrap( u64::from(cid::multihash::Code::Blake2b256), - &sp_core::hashing::blake2_256(&ext.encode()[pattern_index..]), + &sp_crypto_hashing::blake2_256(&ext.encode()[pattern_index..]), ) .unwrap(), ) diff --git a/substrate/client/rpc/Cargo.toml b/substrate/client/rpc/Cargo.toml index 47425c6d3549..2b4366b7cfc6 100644 --- a/substrate/client/rpc/Cargo.toml +++ b/substrate/client/rpc/Cargo.toml @@ -51,6 +51,7 @@ sc-network = { path = "../network" } sc-network-common = { path = "../network/common" } sc-transaction-pool = { path = "../transaction-pool" } sp-consensus = { path = "../../primitives/consensus/common" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } tokio = "1.22.0" sp-io = { path = "../../primitives/io" } substrate-test-runtime-client = { path = "../../test-utils/runtime/client" } diff --git a/substrate/client/rpc/src/author/tests.rs b/substrate/client/rpc/src/author/tests.rs index f48b2f957142..3e3db4b03b3d 100644 --- a/substrate/client/rpc/src/author/tests.rs +++ b/substrate/client/rpc/src/author/tests.rs @@ -29,13 +29,13 @@ use jsonrpsee::{ use sc_transaction_pool::{BasicPool, FullChainApi}; use sc_transaction_pool_api::TransactionStatus; use sp_core::{ - blake2_256, bytes::to_hex, crypto::{ByteArray, Pair}, ed25519, testing::{ED25519, SR25519}, H256, }; +use sp_crypto_hashing::blake2_256; use sp_keystore::{testing::MemoryKeystore, Keystore}; use sp_runtime::Perbill; use std::sync::Arc; diff --git a/substrate/client/rpc/src/state/tests.rs b/substrate/client/rpc/src/state/tests.rs index 594f51efba68..663d511d43e7 100644 --- a/substrate/client/rpc/src/state/tests.rs +++ b/substrate/client/rpc/src/state/tests.rs @@ -251,9 +251,9 @@ async fn should_send_initial_storage_changes_and_notifications() { let (api, _child) = new_full(client.clone(), test_executor(), DenyUnsafe::No); let alice_balance_key = [ - sp_core::hashing::twox_128(b"System"), - sp_core::hashing::twox_128(b"Account"), - sp_core::hashing::blake2_128(&AccountKeyring::Alice.public()), + sp_crypto_hashing::twox_128(b"System"), + sp_crypto_hashing::twox_128(b"Account"), + sp_crypto_hashing::blake2_128(&AccountKeyring::Alice.public()), ] .concat() .iter() diff --git a/substrate/client/transaction-pool/Cargo.toml b/substrate/client/transaction-pool/Cargo.toml index 8832c0bf5080..0a539c5efb72 100644 --- a/substrate/client/transaction-pool/Cargo.toml +++ b/substrate/client/transaction-pool/Cargo.toml @@ -32,6 +32,7 @@ sc-utils = { path = "../utils" } sp-api = { path = "../../primitives/api" } sp-blockchain = { path = "../../primitives/blockchain" } sp-core = { path = "../../primitives/core" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } sp-runtime = { path = "../../primitives/runtime" } sp-tracing = { path = "../../primitives/tracing" } sp-transaction-pool = { path = "../../primitives/transaction-pool" } diff --git a/substrate/client/transaction-pool/benches/basics.rs b/substrate/client/transaction-pool/benches/basics.rs index 0caf00bf2955..65c83f090535 100644 --- a/substrate/client/transaction-pool/benches/basics.rs +++ b/substrate/client/transaction-pool/benches/basics.rs @@ -24,7 +24,7 @@ use futures::{ future::{ready, Ready}, }; use sc_transaction_pool::*; -use sp_core::blake2_256; +use sp_crypto_hashing::blake2_256; use sp_runtime::{ generic::BlockId, traits::{Block as BlockT, NumberFor}, diff --git a/substrate/frame/message-queue/Cargo.toml b/substrate/frame/message-queue/Cargo.toml index e3ab370727ea..62a000dc8d14 100644 --- a/substrate/frame/message-queue/Cargo.toml +++ b/substrate/frame/message-queue/Cargo.toml @@ -30,6 +30,7 @@ frame-support = { path = "../support", default-features = false } frame-system = { path = "../system", default-features = false } [dev-dependencies] +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } sp-tracing = { path = "../../primitives/tracing" } rand = "0.8.5" rand_distr = "0.4.3" diff --git a/substrate/frame/message-queue/src/tests.rs b/substrate/frame/message-queue/src/tests.rs index 9198e65e2f9c..86a8b79fe8bd 100644 --- a/substrate/frame/message-queue/src/tests.rs +++ b/substrate/frame/message-queue/src/tests.rs @@ -23,7 +23,7 @@ use crate::{mock::*, *}; use frame_support::{assert_noop, assert_ok, assert_storage_noop, StorageNoopGuard}; use rand::{rngs::StdRng, Rng, SeedableRng}; -use sp_core::blake2_256; +use sp_crypto_hashing::blake2_256; #[test] fn mocked_weight_works() { diff --git a/substrate/frame/sassafras/Cargo.toml b/substrate/frame/sassafras/Cargo.toml index ad4c0ba12f0b..5f4900b5a29d 100644 --- a/substrate/frame/sassafras/Cargo.toml +++ b/substrate/frame/sassafras/Cargo.toml @@ -31,6 +31,7 @@ sp-std = { path = "../../primitives/std", default-features = false } [dev-dependencies] array-bytes = "6.1" sp-core = { path = "../../primitives/core" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } [features] default = ["std"] diff --git a/substrate/frame/sassafras/src/mock.rs b/substrate/frame/sassafras/src/mock.rs index 5aca815cc214..82ea2fe1ef29 100644 --- a/substrate/frame/sassafras/src/mock.rs +++ b/substrate/frame/sassafras/src/mock.rs @@ -209,7 +209,7 @@ pub fn make_ticket_body(attempt_idx: u32, pair: &AuthorityPair) -> (TicketId, Ti } pub fn make_dummy_ticket_body(attempt_idx: u32) -> (TicketId, TicketBody) { - let hash = sp_core::hashing::blake2_256(&attempt_idx.to_le_bytes()); + let hash = sp_crypto_hashing::blake2_256(&attempt_idx.to_le_bytes()); let erased_public = EphemeralPublic::unchecked_from(hash); let revealed_public = erased_public; diff --git a/substrate/frame/society/Cargo.toml b/substrate/frame/society/Cargo.toml index 46b4f7a7d662..148c662a4cf4 100644 --- a/substrate/frame/society/Cargo.toml +++ b/substrate/frame/society/Cargo.toml @@ -33,6 +33,7 @@ frame-system = { path = "../system", default-features = false } frame-support-test = { path = "../support/test" } pallet-balances = { path = "../balances" } sp-core = { path = "../../primitives/core" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } sp-io = { path = "../../primitives/io" } [features] diff --git a/substrate/frame/society/src/tests.rs b/substrate/frame/society/src/tests.rs index 2163575a8608..940643168fb4 100644 --- a/substrate/frame/society/src/tests.rs +++ b/substrate/frame/society/src/tests.rs @@ -22,7 +22,7 @@ use migrations::old; use mock::*; use frame_support::{assert_noop, assert_ok}; -use sp_core::blake2_256; +use sp_crypto_hashing::blake2_256; use sp_runtime::traits::BadOrigin; use BidKind::*; use VouchingStatus::*; diff --git a/substrate/frame/support/Cargo.toml b/substrate/frame/support/Cargo.toml index b8e4e04c063b..10de781946fb 100644 --- a/substrate/frame/support/Cargo.toml +++ b/substrate/frame/support/Cargo.toml @@ -56,6 +56,7 @@ aquamarine = { version = "0.5.0" } assert_matches = "1.3.0" pretty_assertions = "1.2.1" frame-system = { path = "../system" } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } [features] default = ["std"] diff --git a/substrate/frame/support/src/storage/mod.rs b/substrate/frame/support/src/storage/mod.rs index c77de1f976f6..8ebe7b31da80 100644 --- a/substrate/frame/support/src/storage/mod.rs +++ b/substrate/frame/support/src/storage/mod.rs @@ -1712,7 +1712,7 @@ mod test { use bounded_vec::BoundedVec; use frame_support::traits::ConstU32; use generator::StorageValue as _; - use sp_core::hashing::twox_128; + use sp_crypto_hashing::twox_128; use sp_io::TestExternalities; use weak_bounded_vec::WeakBoundedVec; diff --git a/substrate/frame/support/src/traits/metadata.rs b/substrate/frame/support/src/traits/metadata.rs index 0af8d06719fe..586af20511a8 100644 --- a/substrate/frame/support/src/traits/metadata.rs +++ b/substrate/frame/support/src/traits/metadata.rs @@ -302,7 +302,7 @@ pub trait GetStorageVersion { #[cfg(test)] mod tests { use super::*; - use sp_core::twox_128; + use sp_crypto_hashing::twox_128; struct Pallet1; impl PalletInfoAccess for Pallet1 { diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index c3ff3c2eec69..0d43eea99629 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -46,12 +46,10 @@ pub use sp_debug_derive::RuntimeDebug; #[cfg(feature = "serde")] pub use impl_serde::serialize as bytes; -// https://github.com/rust-lang/rust/issues/85388 -// https://github.com/rust-lang/rust/issues/84584 #[cfg(feature = "full_crypto")] #[deprecated( since = "27.0.0", - note = "hashing re-export is scheduled for removal, please use `sp-crypto-hashing` instead" + 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, *}; diff --git a/substrate/test-utils/runtime/Cargo.toml b/substrate/test-utils/runtime/Cargo.toml index 1eb50771a2ce..cea724da5b53 100644 --- a/substrate/test-utils/runtime/Cargo.toml +++ b/substrate/test-utils/runtime/Cargo.toml @@ -27,6 +27,7 @@ sp-inherents = { path = "../../primitives/inherents", default-features = false } sp-keyring = { path = "../../primitives/keyring", optional = true } sp-offchain = { path = "../../primitives/offchain", default-features = false } sp-core = { path = "../../primitives/core", default-features = false } +sp-crypto-hashing = { path = "../../primitives/crypto/hashing", default-features = false } sp-std = { path = "../../primitives/std", default-features = false } sp-io = { path = "../../primitives/io", default-features = false } frame-support = { path = "../../frame/support", default-features = false } diff --git a/substrate/test-utils/runtime/src/lib.rs b/substrate/test-utils/runtime/src/lib.rs index 16ab467772f2..97bbeaf15428 100644 --- a/substrate/test-utils/runtime/src/lib.rs +++ b/substrate/test-utils/runtime/src/lib.rs @@ -845,11 +845,11 @@ pub mod storage_key_generator { } fn concat_hashes(input: &Vec<&[u8]>) -> String { - input.iter().map(|s| sp_core::hashing::twox_128(s)).map(hex).collect() + input.iter().map(|s| sp_crypto_hashing::twox_128(s)).map(hex).collect() } fn twox_64_concat(x: &[u8]) -> Vec { - sp_core::hashing::twox_64(x).iter().chain(x.iter()).cloned().collect::>() + sp_crypto_hashing::twox_64(x).iter().chain(x.iter()).cloned().collect() } /// Generate the hashed storage keys from the raw literals. These keys are expected to be be in @@ -890,7 +890,7 @@ pub mod storage_key_generator { AccountKeyring::Charlie.public().to_vec(), ]) .map(|pubkey| { - sp_core::hashing::blake2_128(&pubkey) + sp_crypto_hashing::blake2_128(&pubkey) .iter() .chain(pubkey.iter()) .cloned() From 38a5b55ef00ffa1529450ff2de04c7235ef3d2e2 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 13:20:13 +0100 Subject: [PATCH 11/15] More deprecation warnings --- substrate/deprecated/hashing/proc-macro/src/lib.rs | 4 ++++ substrate/deprecated/hashing/src/lib.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/substrate/deprecated/hashing/proc-macro/src/lib.rs b/substrate/deprecated/hashing/proc-macro/src/lib.rs index bbcde33990d3..21494b2c515b 100644 --- a/substrate/deprecated/hashing/proc-macro/src/lib.rs +++ b/substrate/deprecated/hashing/proc-macro/src/lib.rs @@ -15,4 +15,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! This package has been deprecated. Please use `sp-crypto-hashing-proc-macro`. +//! +//! Removal scheduled after June 2024. + pub use sp_crypto_hashing_proc_macro::*; diff --git a/substrate/deprecated/hashing/src/lib.rs b/substrate/deprecated/hashing/src/lib.rs index 4c6da58f4cc7..b054312ae2ee 100644 --- a/substrate/deprecated/hashing/src/lib.rs +++ b/substrate/deprecated/hashing/src/lib.rs @@ -15,4 +15,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! This package has been deprecated. Please use `sp-crypto-hashing`. +//! +//! Removal scheduled after June 2024. + pub use sp_crypto_hashing::*; From 19c4b4d851dc07f132e2fc38a62e2b1ef578eac4 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 13:21:57 +0100 Subject: [PATCH 12/15] Make taplo happy --- substrate/bin/node/testing/Cargo.toml | 2 +- substrate/deprecated/hashing/Cargo.toml | 4 ++-- substrate/frame/alliance/Cargo.toml | 2 +- substrate/primitives/crypto/hashing/proc-macro/Cargo.toml | 2 +- substrate/primitives/statement-store/Cargo.toml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/substrate/bin/node/testing/Cargo.toml b/substrate/bin/node/testing/Cargo.toml index eab1a80ee500..9ca8b8ef7265 100644 --- a/substrate/bin/node/testing/Cargo.toml +++ b/substrate/bin/node/testing/Cargo.toml @@ -35,7 +35,7 @@ sc-client-api = { path = "../../../client/api" } sc-client-db = { path = "../../../client/db", features = ["rocksdb"] } sc-consensus = { path = "../../../client/consensus/common" } sc-executor = { path = "../../../client/executor" } -sc-service = { path = "../../../client/service", features = [ "rocksdb", "test-helpers", ] } +sc-service = { path = "../../../client/service", features = ["rocksdb", "test-helpers"] } sp-api = { path = "../../../primitives/api" } sp-block-builder = { path = "../../../primitives/block-builder" } sp-blockchain = { path = "../../../primitives/blockchain" } diff --git a/substrate/deprecated/hashing/Cargo.toml b/substrate/deprecated/hashing/Cargo.toml index 3d756690cdee..8695ccc8fca2 100644 --- a/substrate/deprecated/hashing/Cargo.toml +++ b/substrate/deprecated/hashing/Cargo.toml @@ -19,5 +19,5 @@ targets = ["x86_64-unknown-linux-gnu"] sp-crypto-hashing = { path = "../../primitives/crypto/hashing" } [features] -default = [ "std" ] -std = [ "sp-crypto-hashing/std" ] +default = ["std"] +std = ["sp-crypto-hashing/std"] diff --git a/substrate/frame/alliance/Cargo.toml b/substrate/frame/alliance/Cargo.toml index 0cfadfdd749c..bd3e92ebe497 100644 --- a/substrate/frame/alliance/Cargo.toml +++ b/substrate/frame/alliance/Cargo.toml @@ -37,7 +37,7 @@ pallet-collective = { path = "../collective", default-features = false, optional [dev-dependencies] array-bytes = "6.1" -sp-crypto-hashing = { path = "../../primitives/crypto/hashing", default-features = false} +sp-crypto-hashing = { path = "../../primitives/crypto/hashing", default-features = false } pallet-balances = { path = "../balances" } pallet-collective = { path = "../collective" } diff --git a/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml b/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml index 9d3478dd3fd2..1b84bf02284f 100644 --- a/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml +++ b/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml @@ -21,4 +21,4 @@ proc-macro = true [dependencies] quote = "1.0.28" syn = { version = "2.0.48", features = ["full", "parsing"] } -sp-crypto-hashing = { path = "..", default-features = false} +sp-crypto-hashing = { path = "..", default-features = false } diff --git a/substrate/primitives/statement-store/Cargo.toml b/substrate/primitives/statement-store/Cargo.toml index e29b3313575d..9df4fc00c590 100644 --- a/substrate/primitives/statement-store/Cargo.toml +++ b/substrate/primitives/statement-store/Cargo.toml @@ -19,7 +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-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 } From 17696fb60172d94dce05b88bcdd643cfe83f4cde Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 13:23:26 +0100 Subject: [PATCH 13/15] Fix feature propagation --- substrate/test-utils/runtime/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/test-utils/runtime/Cargo.toml b/substrate/test-utils/runtime/Cargo.toml index cea724da5b53..b9e5f564cbfc 100644 --- a/substrate/test-utils/runtime/Cargo.toml +++ b/substrate/test-utils/runtime/Cargo.toml @@ -94,6 +94,7 @@ std = [ "sp-consensus-babe/std", "sp-consensus-grandpa/std", "sp-core/std", + "sp-crypto-hashing/std", "sp-externalities/std", "sp-genesis-builder/std", "sp-inherents/std", From 29e9ca3e6de694823ef4bbc09895ad2114360518 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 14:32:35 +0100 Subject: [PATCH 14/15] Update substrate/primitives/keystore/src/testing.rs Co-authored-by: Robert Hambrock --- substrate/primitives/keystore/src/testing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/primitives/keystore/src/testing.rs b/substrate/primitives/keystore/src/testing.rs index fae56fb69365..2879c458b4f6 100644 --- a/substrate/primitives/keystore/src/testing.rs +++ b/substrate/primitives/keystore/src/testing.rs @@ -479,7 +479,7 @@ mod tests { let suri = "//Alice"; let pair = ecdsa::Pair::from_string(suri, None).unwrap(); - // Lets pretend this to be the hash output as content doesn't really matter here. + // 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 From 72e50bd995384b15afd6bb5f51d3d0ba436fbb0f Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 22 Jan 2024 18:26:50 +0100 Subject: [PATCH 15/15] Fix link --- substrate/utils/frame/benchmarking-cli/src/machine/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/utils/frame/benchmarking-cli/src/machine/README.md b/substrate/utils/frame/benchmarking-cli/src/machine/README.md index 1ccc2ad1e4f6..97740b6845ac 100644 --- a/substrate/utils/frame/benchmarking-cli/src/machine/README.md +++ b/substrate/utils/frame/benchmarking-cli/src/machine/README.md @@ -78,6 +78,6 @@ License: Apache-2.0 [Blake2-256]: https://www.blake2.net/ -[blake2_256 function]: https://crates.parity.io/sp_crypto/hashing/fn.blake2_256.html +[blake2_256 function]: https://crates.parity.io/sp_crypto_hashing/fn.blake2_256.html [Curve25519]: https://en.wikipedia.org/wiki/Curve25519 [`--base-path`]: ../shared/README.md#arguments