Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Replace 'blake2-rfc with rust-crypto 'blake2' crate
  • Loading branch information
davxy committed Sep 14, 2022
commit 7b142218c15ca9a40fac395313928d01865259c6
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ members = [
# This list is ordered alphabetically.
[profile.dev.package]
blake2 = { opt-level = 3 }
blake2-rfc = { opt-level = 3 }
blake2b_simd = { opt-level = 3 }
chacha20poly1305 = { opt-level = 3 }
cranelift-codegen = { opt-level = 3 }
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.2", default-features = false, optional = true }
schnorrkel = { version = "0.9.1", features = [
"preaudit_deprecated",
"u64_backend",
Expand Down Expand Up @@ -96,7 +96,7 @@ std = [
"hash-db/std",
"sp-std/std",
"serde",
"blake2-rfc/std",
"blake2/std",
"ed25519-zebra",
"hex/std",
"base58",
Expand Down Expand Up @@ -128,7 +128,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
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")]
})
);
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