From 95ff9b2c54a8cd2279bc574c4d942e153ab5fdcf Mon Sep 17 00:00:00 2001 From: Alexandru Gheorghe <49718502+alexggh@users.noreply.github.com> Date: Mon, 22 Jan 2024 23:38:34 +0200 Subject: [PATCH 1/2] approval-voting: Move verbose log from debug to tracing (#3019) ... it was like that before it was accidentally changed in https://github.com/paritytech/polkadot-sdk/commit/a84dd0dba58d51503b8942360aa4fb30a5a96af5 Signed-off-by: Alexandru Gheorghe --- polkadot/node/core/approval-voting/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/polkadot/node/core/approval-voting/src/lib.rs b/polkadot/node/core/approval-voting/src/lib.rs index 9116725c2911..3161d6186a1e 100644 --- a/polkadot/node/core/approval-voting/src/lib.rs +++ b/polkadot/node/core/approval-voting/src/lib.rs @@ -2583,7 +2583,7 @@ where _ => {}, } - gum::debug!( + gum::trace!( target: LOG_TARGET, validator_index = approval.validator.0, candidate_hash = ?approved_candidate_hash, @@ -2700,7 +2700,7 @@ where let is_approved = check.is_approved(tick_now.saturating_sub(APPROVAL_DELAY)); if status.last_no_shows != 0 { metrics.on_observed_no_shows(status.last_no_shows); - gum::debug!( + gum::trace!( target: LOG_TARGET, ?candidate_hash, ?block_hash, From 4c10fd2a41ace275ff32c154acfe32c44b054719 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 23 Jan 2024 00:36:14 +0100 Subject: [PATCH 2/2] Move cryptographic hashing procedures to crypto folder. (#2306) Step towards https://github.com/paritytech/polkadot-sdk/issues/1975 As reported https://github.com/paritytech/polkadot-sdk/issues/1975#issuecomment-1774534225 I'd like to encapsulate crypto related stuff in a dedicated folder. Currently all cryptographic primitive wrappers are all sparsed in `substrate/core` which contains "misc core" stuff. To simplify the process, as the first step with this PR I propose to move the cryptographic hashing there. The `substrate/crypto` folder was already created to contains `ec-utils` crate. Notes: - rename `sp-core-hashing` to `sp-crypto-hashing` - rename `sp-core-hashing-proc-macro` to `sp-crypto-hashing-proc-macro` - As the crates name is changed I took the freedom to restart fresh from version 0.1.0 for both crates --------- Co-authored-by: Robert Hambrock --- Cargo.lock | 80 +++++++++++++----- Cargo.toml | 6 +- .../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/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 +- 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/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 | 2 +- substrate/client/consensus/grandpa/Cargo.toml | 1 + .../client/consensus/grandpa/src/import.rs | 7 +- 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/sysinfo/Cargo.toml | 1 + substrate/client/sysinfo/src/sysinfo.rs | 2 +- substrate/client/transaction-pool/Cargo.toml | 1 + .../client/transaction-pool/benches/basics.rs | 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 | 22 +++++ substrate/deprecated/hashing/src/lib.rs | 22 +++++ substrate/frame/alliance/Cargo.toml | 8 +- substrate/frame/alliance/src/benchmarking.rs | 2 +- substrate/frame/alliance/src/mock.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 | 3 +- substrate/frame/support/procedural/Cargo.toml | 4 +- .../procedural/src/pallet/parse/helper.rs | 2 +- substrate/frame/support/src/lib.rs | 4 +- substrate/frame/support/src/storage/mod.rs | 2 +- .../frame/support/src/traits/metadata.rs | 2 +- substrate/primitives/arithmetic/Cargo.toml | 4 +- .../arithmetic/src/helpers_128bit.rs | 2 +- .../primitives/consensus/beefy/Cargo.toml | 2 + .../consensus/beefy/src/commitment.rs | 3 +- .../primitives/consensus/beefy/src/lib.rs | 3 +- .../consensus/beefy/src/test_utils.rs | 4 +- .../primitives/consensus/beefy/src/witness.rs | 3 +- substrate/primitives/core/Cargo.toml | 9 +-- substrate/primitives/core/benches/bench.rs | 71 +--------------- 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 | 24 +++--- substrate/primitives/core/src/ed25519.rs | 2 +- substrate/primitives/core/src/hasher.rs | 4 +- substrate/primitives/core/src/hashing.rs | 66 --------------- substrate/primitives/core/src/lib.rs | 8 +- .../{core => crypto}/hashing/Cargo.toml | 19 ++++- .../crypto/hashing/benches/bench.rs | 81 +++++++++++++++++++ .../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/io/Cargo.toml | 3 +- substrate/primitives/io/src/lib.rs | 16 ++-- substrate/primitives/keystore/src/testing.rs | 7 +- .../primitives/statement-store/Cargo.toml | 2 + .../primitives/statement-store/src/lib.rs | 4 +- substrate/primitives/version/Cargo.toml | 2 +- substrate/primitives/version/src/lib.rs | 2 +- substrate/test-utils/runtime/Cargo.toml | 2 + substrate/test-utils/runtime/src/lib.rs | 6 +- .../benchmarking-cli/src/machine/README.md | 2 +- .../frame/remote-externalities/Cargo.toml | 1 + .../frame/remote-externalities/src/lib.rs | 10 +-- 96 files changed, 484 insertions(+), 326 deletions(-) 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 delete mode 100644 substrate/primitives/core/src/hashing.rs rename substrate/primitives/{core => crypto}/hashing/Cargo.toml (72%) create mode 100644 substrate/primitives/crypto/hashing/benches/bench.rs rename substrate/primitives/{core => crypto}/hashing/proc-macro/Cargo.toml (58%) 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 8fc7386215e3..07a1e9950310 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3775,7 +3775,7 @@ dependencies = [ "sc-client-api", "scale-info", "sp-api", - "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-runtime", "sp-state-machine", @@ -3913,6 +3913,7 @@ dependencies = [ "sc-client-api", "scale-info", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-inherents", "sp-io", @@ -5652,6 +5653,7 @@ dependencies = [ "parity-scale-codec", "serde", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-state-machine", @@ -5689,7 +5691,8 @@ dependencies = [ "sp-api", "sp-arithmetic", "sp-core", - "sp-core-hashing-proc-macro", + "sp-crypto-hashing", + "sp-crypto-hashing-proc-macro", "sp-debug-derive 8.0.0", "sp-genesis-builder", "sp-inherents", @@ -5720,7 +5723,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "sp-core-hashing", + "sp-crypto-hashing", "syn 2.0.48", ] @@ -8789,6 +8792,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-io", "sp-keyring", @@ -9065,7 +9069,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-core-hashing", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -10161,6 +10165,7 @@ dependencies = [ "serde", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -10687,6 +10692,7 @@ dependencies = [ "scale-info", "sp-consensus-sassafras", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -10798,6 +10804,7 @@ dependencies = [ "scale-info", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -12340,6 +12347,7 @@ dependencies = [ "sp-authority-discovery", "sp-consensus-babe", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-tracing 10.0.0", @@ -12753,6 +12761,7 @@ dependencies = [ "sc-executor-wasmtime", "seccompiler", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-io", "sp-tracing 10.0.0", @@ -13329,6 +13338,7 @@ dependencies = [ "sp-application-crypto", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-io", "sp-keyring", @@ -15459,6 +15469,7 @@ dependencies = [ "sp-blockchain", "sp-consensus-babe", "sp-core", + "sp-crypto-hashing", "sp-genesis-builder", "sp-io", "sp-keyring", @@ -15674,6 +15685,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-slots", "sp-core", + "sp-crypto-hashing", "sp-inherents", "sp-keyring", "sp-keystore", @@ -15743,6 +15755,7 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-mmr-primitives", @@ -15827,6 +15840,7 @@ dependencies = [ "sp-consensus", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-keyring", "sp-keystore", "sp-runtime", @@ -15966,6 +15980,7 @@ dependencies = [ "schnellru", "sp-api", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-io", "sp-maybe-compressed-blob", @@ -16147,7 +16162,7 @@ dependencies = [ "sc-network", "sp-blockchain", "sp-consensus", - "sp-core", + "sp-crypto-hashing", "sp-runtime", "substrate-test-runtime", "substrate-test-runtime-client", @@ -16399,6 +16414,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-keystore", "sp-offchain", @@ -16669,6 +16685,7 @@ dependencies = [ "serde", "serde_json", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", @@ -16755,6 +16772,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-crypto-hashing", "sp-runtime", "sp-tracing 10.0.0", "sp-transaction-pool", @@ -17688,6 +17706,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", + "sp-crypto-hashing", "sp-runtime", ] @@ -18077,7 +18096,7 @@ dependencies = [ "rand 0.8.5", "scale-info", "serde", - "sp-core", + "sp-crypto-hashing", "sp-std 8.0.0", "static_assertions", ] @@ -18211,6 +18230,7 @@ dependencies = [ "sp-api", "sp-application-crypto", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-mmr-primitives", "sp-runtime", @@ -18308,8 +18328,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", @@ -18335,23 +18354,16 @@ dependencies = [ [[package]] name = "sp-core-hashing" -version = "9.0.0" +version = "15.0.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 = "15.0.0" dependencies = [ - "quote", - "sp-core-hashing", - "syn 2.0.48", + "sp-crypto-hashing-proc-macro", ] [[package]] @@ -18395,6 +18407,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" @@ -18479,6 +18514,7 @@ dependencies = [ "rustversion", "secp256k1", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-keystore", "sp-runtime-interface 17.0.0", @@ -18811,6 +18847,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", @@ -18960,7 +18997,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", @@ -19190,6 +19227,7 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-inherents", "sp-io", @@ -19633,6 +19671,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-grandpa", "sp-core", + "sp-crypto-hashing", "sp-externalities 0.19.0", "sp-genesis-builder", "sp-inherents", @@ -22068,6 +22107,7 @@ dependencies = [ "polkadot-runtime-parachains", "sp-arithmetic", "sp-core", + "sp-crypto-hashing", "sp-io", "sp-runtime", "sp-std 8.0.0", diff --git a/Cargo.toml b/Cargo.toml index 3afccc24992f..991b282ab158 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -287,6 +287,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", @@ -432,9 +434,9 @@ members = [ "substrate/primitives/consensus/slots", "substrate/primitives/core", "substrate/primitives/core/fuzz", - "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/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/node/core/pvf/common/Cargo.toml b/polkadot/node/core/pvf/common/Cargo.toml index f2ec505e623c..1f75e1190c50 100644 --- a/polkadot/node/core/pvf/common/Cargo.toml +++ b/polkadot/node/core/pvf/common/Cargo.toml @@ -27,6 +27,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 2d8f6430187b..3f5b4d7ca70c 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 9ad7292b0fdc..4fb78ee05033 100644 --- a/polkadot/node/network/gossip-support/Cargo.toml +++ b/polkadot/node/network/gossip-support/Cargo.toml @@ -13,6 +13,7 @@ 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 22417795d5ea..a6f0fdf75bb7 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/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..9ca8b8ef7265 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/chain-spec/Cargo.toml b/substrate/client/chain-spec/Cargo.toml index 765e3e9f7c77..14ade4cc8484 100644 --- a/substrate/client/chain-spec/Cargo.toml +++ b/substrate/client/chain-spec/Cargo.toml @@ -28,6 +28,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 d6ef99fafdd0..8766dd5c5ad2 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 40c69d5780a5..e5fb7f923f4d 100644 --- a/substrate/client/consensus/babe/Cargo.toml +++ b/substrate/client/consensus/babe/Cargo.toml @@ -42,6 +42,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 fb1722398012..11f5233abc6b 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 c54452faebe9..bdb66e0a4cc3 100644 --- a/substrate/client/consensus/beefy/Cargo.toml +++ b/substrate/client/consensus/beefy/Cargo.toml @@ -36,6 +36,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..75c44de3324c 100644 --- a/substrate/client/consensus/beefy/src/keystore.rs +++ b/substrate/client/consensus/beefy/src/keystore.rs @@ -17,7 +17,7 @@ // 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_crypto_hashing::keccak_256; use sp_keystore::KeystorePtr; use log::warn; diff --git a/substrate/client/consensus/grandpa/Cargo.toml b/substrate/client/consensus/grandpa/Cargo.toml index a6aacd564854..a260e6993c70 100644 --- a/substrate/client/consensus/grandpa/Cargo.toml +++ b/substrate/client/consensus/grandpa/Cargo.toml @@ -49,6 +49,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/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/sysinfo/Cargo.toml b/substrate/client/sysinfo/Cargo.toml index 18ac161f1ee1..1e8dc91568e3 100644 --- a/substrate/client/sysinfo/Cargo.toml +++ b/substrate/client/sysinfo/Cargo.toml @@ -28,6 +28,7 @@ serde = { version = "1.0.195", features = ["derive"] } serde_json = "1.0.111" 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/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/deprecated/hashing/Cargo.toml b/substrate/deprecated/hashing/Cargo.toml new file mode 100644 index 000000000000..8695ccc8fca2 --- /dev/null +++ b/substrate/deprecated/hashing/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "sp-core-hashing" +version = "15.0.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..aa78809241f0 --- /dev/null +++ b/substrate/deprecated/hashing/proc-macro/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "sp-core-hashing-proc-macro" +version = "15.0.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..21494b2c515b --- /dev/null +++ b/substrate/deprecated/hashing/proc-macro/src/lib.rs @@ -0,0 +1,22 @@ +// 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. + +//! 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 new file mode 100644 index 000000000000..b054312ae2ee --- /dev/null +++ b/substrate/deprecated/hashing/src/lib.rs @@ -0,0 +1,22 @@ +// 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. + +//! This package has been deprecated. Please use `sp-crypto-hashing`. +//! +//! Removal scheduled after June 2024. + +pub use sp_crypto_hashing::*; diff --git a/substrate/frame/alliance/Cargo.toml b/substrate/frame/alliance/Cargo.toml index 39f5a6ceb756..bd3e92ebe497 100644 --- a/substrate/frame/alliance/Cargo.toml +++ b/substrate/frame/alliance/Cargo.toml @@ -24,7 +24,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 } @@ -37,7 +37,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" } @@ -53,8 +53,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", @@ -67,7 +67,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 cb2a04f17c57..b69d0156ec4b 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 22aea9005efa..65cebb985d7d 100644 --- a/substrate/frame/alliance/src/mock.rs +++ b/substrate/frame/alliance/src/mock.rs @@ -388,7 +388,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/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 dafb86bd660e..10de781946fb 100644 --- a/substrate/frame/support/Cargo.toml +++ b/substrate/frame/support/Cargo.toml @@ -42,7 +42,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 } @@ -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/procedural/Cargo.toml b/substrate/frame/support/procedural/Cargo.toml index 25debb7fc2bf..e2391505c649 100644 --- a/substrate/frame/support/procedural/Cargo.toml +++ b/substrate/frame/support/procedural/Cargo.toml @@ -29,14 +29,14 @@ 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", default-features = false } +sp-crypto-hashing = { path = "../../../primitives/crypto/hashing", default-features = false } [dev-dependencies] 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! 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 95478d1a8cca..894d2e1b73ab 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; #[cfg(feature = "std")] pub use sp_io::TestExternalities; @@ -330,7 +330,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/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/arithmetic/Cargo.toml b/substrate/primitives/arithmetic/Cargo.toml index 47d2902e267d..31245c8f676b 100644 --- a/substrate/primitives/arithmetic/Cargo.toml +++ b/substrate/primitives/arithmetic/Cargo.toml @@ -31,7 +31,7 @@ sp-std = { path = "../std", default-features = false } [dev-dependencies] criterion = "0.4.0" primitive-types = "0.12.0" -sp-core = { path = "../core", features = ["full_crypto"] } +sp-crypto-hashing = { path = "../crypto/hashing" } rand = "0.8.5" [features] @@ -41,7 +41,7 @@ std = [ "num-traits/std", "scale-info/std", "serde/std", - "sp-core/std", + "sp-crypto-hashing/std", "sp-std/std", ] # Serde support without relying on std features. 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/consensus/beefy/Cargo.toml b/substrate/primitives/consensus/beefy/Cargo.toml index 93a9be17747c..27450ebca58d 100644 --- a/substrate/primitives/consensus/beefy/Cargo.toml +++ b/substrate/primitives/consensus/beefy/Cargo.toml @@ -21,6 +21,7 @@ serde = { version = "1.0.195", default-features = false, optional = true, featur sp-api = { path = "../../api", default-features = false } sp-application-crypto = { path = "../../application-crypto", default-features = false } sp-core = { path = "../../core", default-features = false } +sp-crypto-hashing = { path = "../../crypto/hashing", default-features = false } sp-io = { path = "../../io", default-features = false } sp-mmr-primitives = { path = "../../merkle-mountain-range", default-features = false } sp-runtime = { path = "../../runtime", default-features = false } @@ -41,6 +42,7 @@ std = [ "sp-api/std", "sp-application-crypto/std", "sp-core/std", + "sp-crypto-hashing/std", "sp-io/std", "sp-mmr-primitives/std", "sp-runtime/std", 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/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/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/Cargo.toml b/substrate/primitives/core/Cargo.toml index 73de8057d80b..a167f12929eb 100644 --- a/substrate/primitives/core/Cargo.toml +++ b/substrate/primitives/core/Cargo.toml @@ -53,7 +53,7 @@ libsecp256k1 = { version = "0.7", default-features = false, features = ["static- schnorrkel = { version = "0.11.4", features = ["preaudit_deprecated"], default-features = false } merlin = { version = "3.0", default-features = false } secp256k1 = { version = "0.28.0", default-features = false, features = ["alloc", "recovery"], optional = true } -sp-core-hashing = { path = "hashing", default-features = false, optional = true } +sp-crypto-hashing = { path = "../crypto/hashing", default-features = false, optional = true } sp-runtime-interface = { path = "../runtime-interface", default-features = false } # bls crypto @@ -66,7 +66,6 @@ criterion = "0.4.0" serde_json = "1.0.111" lazy_static = "1.4.0" regex = "1.6.0" -sp-core-hashing-proc-macro = { path = "hashing/proc-macro" } [[bench]] name = "bench" @@ -110,7 +109,7 @@ std = [ "secp256k1/std", "secrecy/alloc", "serde/std", - "sp-core-hashing/std", + "sp-crypto-hashing/std", "sp-debug-derive/std", "sp-externalities/std", "sp-runtime-interface/std", @@ -136,7 +135,7 @@ serde = [ "primitive-types/serde_no_std", "scale-info/serde", "secrecy/alloc", - "sp-core-hashing", + "sp-crypto-hashing", "sp-storage/serde", ] @@ -149,7 +148,7 @@ full_crypto = [ "ed25519-zebra", "libsecp256k1", "secp256k1", - "sp-core-hashing", + "sp-crypto-hashing", "sp-runtime-interface/disable_target_static_assertions", ] 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/bandersnatch.rs b/substrate/primitives/core/src/bandersnatch.rs index 463d49fd8890..96b0ff19e561 100644 --- a/substrate/primitives/core/src/bandersnatch.rs +++ b/substrate/primitives/core/src/bandersnatch.rs @@ -258,7 +258,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 58d8616368eb..d8436d2a96cb 100644 --- a/substrate/primitives/core/src/crypto.rs +++ b/substrate/primitives/core/src/crypto.rs @@ -152,7 +152,7 @@ impl DeriveJunction { let mut cc: [u8; JUNCTION_ID_LEN] = Default::default(); index.using_encoded(|data| { if data.len() > JUNCTION_ID_LEN { - cc.copy_from_slice(&sp_core_hashing::blake2_256(data)); + cc.copy_from_slice(&sp_crypto_hashing::blake2_256(data)); } else { cc[0..data.len()].copy_from_slice(data); } diff --git a/substrate/primitives/core/src/ecdsa.rs b/substrate/primitives/core/src/ecdsa.rs index 471714582a6b..1b63db7af7f2 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. @@ -365,7 +362,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. @@ -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()?; @@ -766,7 +764,7 @@ mod test { // using pre-hashed `msg` works let msg = b"this should be hashed"; - let sig1 = pair.sign_prehashed(&blake2_256(msg)); + let sig1 = pair.sign_prehashed(&sp_crypto_hashing::blake2_256(msg)); let sig2 = pair.sign(msg); assert_eq!(sig1, sig2); } @@ -776,12 +774,12 @@ mod test { let (pair, _, _) = Pair::generate_with_phrase(Some("password")); // `msg` and `sig` match - let msg = blake2_256(b"this should be hashed"); + let msg = sp_crypto_hashing::blake2_256(b"this should be hashed"); let sig = pair.sign_prehashed(&msg); assert!(Pair::verify_prehashed(&sig, &msg, &pair.public())); // `msg` and `sig` don't match - let msg = blake2_256(b"this is a different message"); + let msg = sp_crypto_hashing::blake2_256(b"this is a different message"); assert!(!Pair::verify_prehashed(&sig, &msg, &pair.public())); } @@ -790,7 +788,7 @@ mod test { let (pair, _, _) = Pair::generate_with_phrase(Some("password")); // recovered key matches signing key - let msg = blake2_256(b"this should be hashed"); + let msg = sp_crypto_hashing::blake2_256(b"this should be hashed"); let sig = pair.sign_prehashed(&msg); let key = sig.recover_prehashed(&msg).unwrap(); assert_eq!(pair.public(), key); @@ -799,7 +797,7 @@ mod test { assert!(Pair::verify_prehashed(&sig, &msg, &key)); // recovered key and signing key don't match - let msg = blake2_256(b"this is a different message"); + let msg = sp_crypto_hashing::blake2_256(b"this is a different message"); let key = sig.recover_prehashed(&msg).unwrap(); assert_ne!(pair.public(), key); } 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/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/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 c7232563cb73..0d43eea99629 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -47,10 +47,12 @@ pub use sp_debug_derive::RuntimeDebug; pub use impl_serde::serialize as bytes; #[cfg(feature = "full_crypto")] -pub mod hashing; +#[deprecated( + since = "27.0.0", + note = "`sp-crypto-hashing` re-exports will be removed after June 2024. Use `sp-crypto-hashing` instead." +)] +pub use sp_crypto_hashing::{self as hashing, *}; -#[cfg(feature = "full_crypto")] -pub use hashing::{blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64}; pub mod const_hex2array; pub mod crypto; pub mod hexdisplay; diff --git a/substrate/primitives/core/hashing/Cargo.toml b/substrate/primitives/crypto/hashing/Cargo.toml similarity index 72% rename from substrate/primitives/core/hashing/Cargo.toml rename to substrate/primitives/crypto/hashing/Cargo.toml index 011d312ba90f..096650e231c8 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" [lints] workspace = true @@ -23,6 +23,17 @@ sha2 = { version = "0.10.7", default-features = false } sha3 = { version = "0.10.0", default-features = false } twox-hash = { version = "1.6.3", default-features = false, features = ["digest_0_10"] } +[dev-dependencies] +criterion = "0.4.0" +sp-crypto-hashing-proc-macro = { path = "proc-macro" } + +[[bench]] +name = "bench" +harness = false + +[lib] +bench = false + [features] default = ["std"] std = [ 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); diff --git a/substrate/primitives/core/hashing/proc-macro/Cargo.toml b/substrate/primitives/crypto/hashing/proc-macro/Cargo.toml similarity index 58% rename from substrate/primitives/core/hashing/proc-macro/Cargo.toml rename to substrate/primitives/crypto/hashing/proc-macro/Cargo.toml index 5c215bc77993..1b84bf02284f 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" [lints] workspace = true @@ -21,4 +21,4 @@ proc-macro = true [dependencies] quote = "1.0.28" syn = { version = "2.0.48", features = ["full", "parsing"] } -sp-core-hashing = { path = "..", default-features = false } +sp-crypto-hashing = { path = "..", default-features = false } 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/io/Cargo.toml b/substrate/primitives/io/Cargo.toml index 47de957e6bf9..19e0f78e008e 100644 --- a/substrate/primitives/io/Cargo.toml +++ b/substrate/primitives/io/Cargo.toml @@ -17,11 +17,11 @@ workspace = true [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] - [dependencies] bytes = { version = "1.1.0", default-features = false } codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["bytes"] } sp-core = { path = "../core", default-features = false } +sp-crypto-hashing = { path = "../crypto/hashing", default-features = false } sp-keystore = { path = "../keystore", default-features = false, optional = true } sp-std = { path = "../std", default-features = false } libsecp256k1 = { version = "0.7", optional = true } @@ -52,6 +52,7 @@ std = [ "log/std", "secp256k1", "sp-core/std", + "sp-crypto-hashing/std", "sp-externalities/std", "sp-keystore/std", "sp-runtime-interface/std", diff --git a/substrate/primitives/io/src/lib.rs b/substrate/primitives/io/src/lib.rs index a300152ee66d..20ea56bc26c9 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/keystore/src/testing.rs b/substrate/primitives/keystore/src/testing.rs index 585efba02831..2879c458b4f6 100644 --- a/substrate/primitives/keystore/src/testing.rs +++ b/substrate/primitives/keystore/src/testing.rs @@ -479,16 +479,17 @@ mod tests { let suri = "//Alice"; let pair = ecdsa::Pair::from_string(suri, None).unwrap(); - let msg = sp_core::keccak_256(b"this should be a hashed message"); + // Let's pretend this to be the hash output as content doesn't really matter here. + let hash = [0xff; 32]; // no key in key store - let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap(); + let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &hash).unwrap(); assert!(res.is_none()); // insert key, sign again store.insert(ECDSA, suri, pair.public().as_ref()).unwrap(); - let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap(); + let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &hash).unwrap(); assert!(res.is_some()); } diff --git a/substrate/primitives/statement-store/Cargo.toml b/substrate/primitives/statement-store/Cargo.toml index cacfd08f3ebf..9df4fc00c590 100644 --- a/substrate/primitives/statement-store/Cargo.toml +++ b/substrate/primitives/statement-store/Cargo.toml @@ -19,6 +19,7 @@ targets = ["x86_64-unknown-linux-gnu"] codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] } scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } sp-core = { path = "../core", default-features = false } +sp-crypto-hashing = { path = "../crypto/hashing", default-features = false } sp-runtime = { path = "../runtime", default-features = false } sp-std = { path = "../std", default-features = false } sp-api = { path = "../api", default-features = false } @@ -52,6 +53,7 @@ std = [ "sp-api/std", "sp-application-crypto/std", "sp-core/std", + "sp-crypto-hashing/std", "sp-externalities/std", "sp-runtime-interface/std", "sp-runtime/std", diff --git a/substrate/primitives/statement-store/src/lib.rs b/substrate/primitives/statement-store/src/lib.rs index 67e7a7b3896b..04175f6d6160 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. @@ -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 )) ); diff --git a/substrate/primitives/version/Cargo.toml b/substrate/primitives/version/Cargo.toml index 1ceda4e700f8..258d0925ac4b 100644 --- a/substrate/primitives/version/Cargo.toml +++ b/substrate/primitives/version/Cargo.toml @@ -23,7 +23,7 @@ parity-wasm = { version = "0.45", optional = true } scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } serde = { version = "1.0.195", default-features = false, features = ["alloc", "derive"], optional = true } thiserror = { version = "1.0.48", optional = true } -sp-core-hashing-proc-macro = { path = "../core/hashing/proc-macro" } +sp-crypto-hashing-proc-macro = { path = "../crypto/hashing/proc-macro" } sp-runtime = { path = "../runtime", default-features = false } sp-std = { path = "../std", default-features = false } sp-version-proc-macro = { path = "proc-macro", default-features = false } 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/test-utils/runtime/Cargo.toml b/substrate/test-utils/runtime/Cargo.toml index 1eb50771a2ce..b9e5f564cbfc 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 } @@ -93,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", 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() diff --git a/substrate/utils/frame/benchmarking-cli/src/machine/README.md b/substrate/utils/frame/benchmarking-cli/src/machine/README.md index d3c9a0ec8406..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_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 diff --git a/substrate/utils/frame/remote-externalities/Cargo.toml b/substrate/utils/frame/remote-externalities/Cargo.toml index ba0e8e869ccc..ae5d0097d5e6 100644 --- a/substrate/utils/frame/remote-externalities/Cargo.toml +++ b/substrate/utils/frame/remote-externalities/Cargo.toml @@ -20,6 +20,7 @@ codec = { package = "parity-scale-codec", version = "3.6.1" } log = "0.4.17" serde = "1.0.195" 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 5c7a36867ff6..ce6b25da80e0 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}, @@ -999,10 +998,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());