Skip to content

Commit

Permalink
Move cryptographic hashing procedures to crypto folder. (paritytech#2306
Browse files Browse the repository at this point in the history
)

Step towards paritytech#1975

As reported
paritytech#1975 (comment)
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>
  • Loading branch information
davxy and Lederstrumpf authored Jan 22, 2024
1 parent 8ada9b2 commit d0ec78a
Show file tree
Hide file tree
Showing 80 changed files with 401 additions and 291 deletions.
1 change: 1 addition & 0 deletions substrate/bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/cli/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions substrate/bin/node/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 3 additions & 2 deletions substrate/bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 11 additions & 9 deletions substrate/bin/node/testing/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions substrate/client/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/chain-spec/src/genesis_config_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions substrate/client/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 1 addition & 2 deletions substrate/client/consensus/babe/src/authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions substrate/client/consensus/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/beefy/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions substrate/client/consensus/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
7 changes: 5 additions & 2 deletions substrate/client/consensus/grandpa/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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()))
{
Expand Down
1 change: 1 addition & 0 deletions substrate/client/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
35 changes: 12 additions & 23 deletions substrate/client/executor/src/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
);
}

Expand All @@ -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(),
);
}

Expand All @@ -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(),
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/bitswap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion substrate/client/network/bitswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
1 change: 1 addition & 0 deletions substrate/client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/rpc/src/author/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions substrate/client/rpc/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions substrate/client/sysinfo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
2 changes: 1 addition & 1 deletion substrate/client/sysinfo/src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
1 change: 1 addition & 0 deletions substrate/client/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/transaction-pool/benches/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
23 changes: 23 additions & 0 deletions substrate/deprecated/hashing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Loading

0 comments on commit d0ec78a

Please sign in to comment.