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

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

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

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

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

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

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

---------

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