Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Replace 'blake2-rfc' with rust-crypto 'blake2' crate #12266

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion primitives/api/proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc-macro = true
quote = "1.0.10"
syn = { version = "1.0.98", features = ["full", "fold", "extra-traits", "visit"] }
proc-macro2 = "1.0.37"
blake2 = { version = "0.10.2", default-features = false }
blake2 = { version = "0.10.4", default-features = false }
proc-macro-crate = "1.1.3"

# Required for the doc tests
Expand Down
6 changes: 3 additions & 3 deletions primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bitflags = "1.3"

# full crypto
ed25519-zebra = { version = "3.0.0", default-features = false, optional = true}
blake2-rfc = { version = "0.2.18", default-features = false, optional = true }
blake2 = { version = "0.10.4", default-features = false, optional = true }
schnorrkel = { version = "0.9.1", features = [
"preaudit_deprecated",
"u64_backend",
Expand Down Expand Up @@ -98,7 +98,7 @@ std = [
"hash-db/std",
"sp-std/std",
"serde",
"blake2-rfc/std",
"blake2/std",
"ed25519-zebra",
"hex/std",
"base58",
Expand Down Expand Up @@ -130,7 +130,7 @@ std = [
# For the regular wasm runtime builds this should not be used.
full_crypto = [
"ed25519-zebra",
"blake2-rfc",
"blake2",
"schnorrkel",
"hex",
"libsecp256k1",
Expand Down
2 changes: 1 addition & 1 deletion primitives/core/hashing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ documentation = "https://docs.rs/sp-core-hashing"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
blake2 = { version = "0.10.2", default-features = false }
blake2 = { version = "0.10.4", default-features = false }
byteorder = { version = "1.3.2", default-features = false }
digest = { version = "0.10.3", default-features = false }
sha2 = { version = "0.10.2", default-features = false }
Expand Down
36 changes: 26 additions & 10 deletions primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ impl DeriveJunction {
let mut cc: [u8; JUNCTION_ID_LEN] = Default::default();
index.using_encoded(|data| {
if data.len() > JUNCTION_ID_LEN {
let hash_result = blake2_rfc::blake2b::blake2b(JUNCTION_ID_LEN, &[], data);
let hash = hash_result.as_bytes();
cc.copy_from_slice(hash);
cc.copy_from_slice(&sp_core_hashing::blake2_256(data));
} else {
cc[0..data.len()].copy_from_slice(data);
}
Expand Down Expand Up @@ -292,7 +290,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
}

let hash = ss58hash(&data[0..body_len + prefix_len]);
let checksum = &hash.as_bytes()[0..CHECKSUM_LEN];
let checksum = &hash[0..CHECKSUM_LEN];
if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum {
// Invalid checksum.
return Err(PublicError::InvalidChecksum)
Expand Down Expand Up @@ -333,7 +331,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
};
v.extend(self.as_ref());
let r = ss58hash(&v);
v.extend(&r.as_bytes()[0..2]);
v.extend(&r[0..2]);
v.to_base58()
}

Expand Down Expand Up @@ -366,11 +364,13 @@ pub trait Derive: Sized {
const PREFIX: &[u8] = b"SS58PRE";

#[cfg(feature = "std")]
fn ss58hash(data: &[u8]) -> blake2_rfc::blake2b::Blake2bResult {
let mut context = blake2_rfc::blake2b::Blake2b::new(64);
context.update(PREFIX);
context.update(data);
context.finalize()
fn ss58hash(data: &[u8]) -> Vec<u8> {
use blake2::{Blake2b512, Digest};

let mut ctx = Blake2b512::new();
ctx.update(PREFIX);
ctx.update(data);
ctx.finalize().to_vec()
}

/// Default prefix number
Expand Down Expand Up @@ -1311,6 +1311,14 @@ mod tests {
path: vec![DeriveJunction::soft("DOT")]
})
);
assert_eq!(
TestPair::from_string("hello world/0123456789012345678901234567890123456789", None),
Ok(TestPair::Standard {
phrase: "hello world".to_owned(),
password: None,
path: vec![DeriveJunction::soft("0123456789012345678901234567890123456789")]
})
);
davxy marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(
TestPair::from_string("hello world//1", None),
Ok(TestPair::Standard {
Expand All @@ -1327,6 +1335,14 @@ mod tests {
path: vec![DeriveJunction::hard("DOT")]
})
);
assert_eq!(
TestPair::from_string("hello world//0123456789012345678901234567890123456789", None),
Ok(TestPair::Standard {
phrase: "hello world".to_owned(),
password: None,
path: vec![DeriveJunction::hard("0123456789012345678901234567890123456789")]
})
);
assert_eq!(
TestPair::from_string("hello world//1/DOT", None),
Ok(TestPair::Standard {
Expand Down