From d0ec78a93239060ed81d3432541ce8c90066172d Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Tue, 23 Jan 2024 00:36:14 +0100 Subject: [PATCH] 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 --- 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 +-- 80 files changed, 401 insertions(+), 291 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/substrate/bin/node/cli/Cargo.toml b/substrate/bin/node/cli/Cargo.toml index 061c9684c2260..6803d2def43b7 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 9019594ff627f..2d74cdd5a0418 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 76188ed446c08..9ca8b8ef7265e 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 98d3b968a358a..df302a6453b9f 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 6c885cc039a15..f712191bed695 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 765e3e9f7c77d..14ade4cc8484b 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 d6ef99fafdd03..8766dd5c5ad28 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 40c69d5780a53..e5fb7f923f4d4 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 fb1722398012b..11f5233abc6b3 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 c54452faebe96..bdb66e0a4cc33 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 925bb08828220..75c44de3324ce 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 a6aacd564854b..a260e6993c70e 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 ca5b7c400bfb2..bc2983569c533 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 aa8e8c9abf295..8508fbb989135 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 0bd080c243574..7f91b3ffe7644 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 b004c03e0256f..cc04418c39380 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 9200c2fa7726b..0586354d6a0dd 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 47425c6d35499..2b4366b7cfc6f 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 f48b2f9571428..3e3db4b03b3d9 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 594f51efba686..663d511d43e77 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 18ac161f1ee1a..1e8dc91568e35 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 bef87a83e46f6..3fa583cf7aca1 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 8832c0bf5080d..0a539c5efb72f 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 0caf00bf29551..65c83f090535e 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 0000000000000..8695ccc8fca22 --- /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 0000000000000..73b83fd451be7 --- /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 0000000000000..aa78809241f03 --- /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 0000000000000..c2d9df7287025 --- /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 0000000000000..21494b2c515b7 --- /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 0000000000000..b054312ae2ee7 --- /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 39f5a6ceb756c..bd3e92ebe4975 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 cb2a04f17c57f..b69d0156ec4b0 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 22aea9005efaf..65cebb985d7df 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 e3ab370727ea8..62a000dc8d143 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 9198e65e2f9c0..86a8b79fe8bd0 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 ad4c0ba12f0b1..5f4900b5a29d4 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 5aca815cc2140..82ea2fe1ef29e 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 46b4f7a7d6621..148c662a4cf41 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 2163575a86080..940643168fb41 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 dafb86bd660e0..10de781946fb5 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 25debb7fc2bfe..e2391505c649e 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 446ec203d2ba5..538226a8745fe 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 95478d1a8cca7..894d2e1b73ab9 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 c77de1f976f60..8ebe7b31da80d 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 0af8d06719fe3..586af20511a89 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 47d2902e267d0..31245c8f676bd 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 9b9c74ba55774..4e234d4026c9a 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 93a9be17747cc..27450ebca58d0 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 5b6ef9ae5ab36..37be1a4f6fc3e 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 e31c53237be24..68eeeb3c68007 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 b83f657af38e3..a6e65e5bff0b0 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 3f2c2bcbe2829..b633453340bbe 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 73de8057d80ba..a167f12929eb8 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 ffca819b638e5..0527c2b178cbd 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 463d49fd88901..96b0ff19e561a 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 e519ba1806c41..452c6372d16b3 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 58d8616368eb7..d8436d2a96cb1 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 471714582a6bb..1b63db7af7f2b 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 151a7229315eb..aa0d77510bd81 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 5c4c40cba8e2d..fc613dba161eb 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 e71d0b5433803..0000000000000 --- 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 c7232563cb738..0d43eea996299 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 011d312ba90fc..096650e231c8f 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 0000000000000..9afbcd30e3226 --- /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 5c215bc779934..1b84bf02284fd 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 714852ae3594a..5eba7760b3de7 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 69668cadb4e26..0ebd30c8b4095 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 33d777f85b014..04b7bb6f4de25 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 47de957e6bf9e..19e0f78e008e9 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 a300152ee66d1..20ea56bc26c98 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 585efba02831a..2879c458b4f68 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 cacfd08f3ebf4..9df4fc00c590f 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 67e7a7b3896b5..04175f6d6160e 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 1ceda4e700f8a..258d0925ac4bd 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 13f4520f6e694..9b14a809ac100 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 1eb50771a2ce9..b9e5f564cbfc0 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 16ab467772f2f..97bbeaf154285 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 d3c9a0ec84062..97740b6845acb 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 ba0e8e869ccc2..ae5d0097d5e62 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 5c7a36867ff6e..ce6b25da80e02 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());