mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 18:11:10 +00:00
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:
@@ -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();
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"[..]),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user