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
+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},