From b1e27ca99059b91c132aad30b26fcb7a867a26fd Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 28 Nov 2022 05:01:47 +0200 Subject: [PATCH 1/2] Remove `blake2-rfc` dependency from crates that didn't use it anyway --- Cargo.lock | 2 -- crates/subspace-archiving/Cargo.toml | 3 --- crates/subspace-farmer-components/Cargo.toml | 1 - 3 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7f39d904a..b83d99b4d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8625,7 +8625,6 @@ dependencies = [ name = "subspace-archiving" version = "0.1.0" dependencies = [ - "blake2-rfc", "criterion", "merkle_light", "parity-scale-codec", @@ -8715,7 +8714,6 @@ name = "subspace-farmer-components" version = "0.1.0" dependencies = [ "async-trait", - "blake2-rfc", "criterion", "fs2", "futures 0.3.25", diff --git a/crates/subspace-archiving/Cargo.toml b/crates/subspace-archiving/Cargo.toml index eeb640c36d..3104108b20 100644 --- a/crates/subspace-archiving/Cargo.toml +++ b/crates/subspace-archiving/Cargo.toml @@ -17,8 +17,6 @@ include = [ bench = false [dependencies] -# Not using `blake2` crate due to https://github.com/RustCrypto/hashes/issues/360 -blake2-rfc = { version = "0.2.18", default-features = false } merkle_light = { version = "0.4.0", default-features = false } parity-scale-codec = { version = "3.2.1", default-features = false, features = ["derive"] } reed-solomon-erasure = { version = "6.0.0", default-features = false } @@ -33,7 +31,6 @@ rand = { version = "0.8.5", features = ["min_const_gen"] } [features] default = ["std"] std = [ - "blake2-rfc/std", "merkle_light/std", "parity-scale-codec/std", "reed-solomon-erasure/simd-accel", diff --git a/crates/subspace-farmer-components/Cargo.toml b/crates/subspace-farmer-components/Cargo.toml index 34c3b761f5..fb03038e97 100644 --- a/crates/subspace-farmer-components/Cargo.toml +++ b/crates/subspace-farmer-components/Cargo.toml @@ -17,7 +17,6 @@ bench = false [dependencies] async-trait = "0.1.58" -blake2-rfc = "0.2.18" fs2 = "0.4.3" libc = "0.2.131" parity-scale-codec = "3.2.1" From e969ba93ba9bd3761dc50c3500cc87f4c42df718 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 28 Nov 2022 05:17:59 +0200 Subject: [PATCH 2/2] Replace `blake2-rfc` with `blake2` dependency such that we have one less crate to build --- Cargo.lock | 34 +++---------------- crates/subspace-core-primitives/Cargo.toml | 5 ++- crates/subspace-core-primitives/src/crypto.rs | 25 ++++++++------ crates/subspace-farmer/Cargo.toml | 1 - .../src/bin/subspace-farmer/ss58.rs | 12 +++---- domains/client/domain-executor/Cargo.toml | 3 +- .../client/domain-executor/src/merkle_tree.rs | 17 ++++------ 7 files changed, 33 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b83d99b4d9..119c601f78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -443,15 +443,6 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" -[[package]] -name = "arrayvec" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" -dependencies = [ - "nodrop", -] - [[package]] name = "arrayvec" version = "0.5.2" @@ -743,23 +734,13 @@ dependencies = [ [[package]] name = "blake2" -version = "0.10.4" +version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9cf849ee05b2ee5fba5e36f97ff8ec2533916700fc0758d40d92136a42f3388" +checksum = "b12e5fd123190ce1c2e559308a94c9bacad77907d4c6005d9e58fe1a0689e55e" dependencies = [ "digest 0.10.3", ] -[[package]] -name = "blake2-rfc" -version = "0.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" -dependencies = [ - "arrayvec 0.4.12", - "constant_time_eq", -] - [[package]] name = "blake2b_simd" version = "1.0.0" @@ -1877,7 +1858,7 @@ dependencies = [ name = "domain-client-executor" version = "0.1.0" dependencies = [ - "blake2-rfc", + "blake2", "crossbeam", "domain-block-builder", "domain-client-executor-gossip", @@ -4737,12 +4718,6 @@ dependencies = [ "libc", ] -[[package]] -name = "nodrop" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" - [[package]] name = "nohash-hasher" version = "0.2.0" @@ -8642,7 +8617,7 @@ dependencies = [ "ark-bls12-381", "ark-ff", "ark-poly", - "blake2-rfc", + "blake2", "criterion", "derive_more", "dusk-bls12_381", @@ -8669,7 +8644,6 @@ dependencies = [ "anyhow", "async-trait", "base58", - "blake2-rfc", "bytesize", "clap 4.0.26", "derive_more", diff --git a/crates/subspace-core-primitives/Cargo.toml b/crates/subspace-core-primitives/Cargo.toml index e0f6684bcb..147a45669b 100644 --- a/crates/subspace-core-primitives/Cargo.toml +++ b/crates/subspace-core-primitives/Cargo.toml @@ -19,8 +19,7 @@ bench = false ark-bls12-381 = "0.3.0" ark-ff = "0.3.0" ark-poly = "0.3.0" -# Not using `blake2` crate due to https://github.com/RustCrypto/hashes/issues/360 -blake2-rfc = { version = "0.2.18", default-features = false } +blake2 = { version = "0.10.5", default-features = false } derive_more = "0.99.17" dusk-bls12_381 = { version = "0.11.2", default-features = false, features = ["alloc", "groups", "pairings", "endo"] } dusk-bytes = "0.1" @@ -47,7 +46,7 @@ std = [ "ark-bls12-381/std", "ark-ff/std", "ark-poly/std", - "blake2-rfc/std", + "blake2/std", "dusk-bls12_381/std", "dusk-plonk/std", "hex/serde", diff --git a/crates/subspace-core-primitives/src/crypto.rs b/crates/subspace-core-primitives/src/crypto.rs index 1d660f0084..b84ce92081 100644 --- a/crates/subspace-core-primitives/src/crypto.rs +++ b/crates/subspace-core-primitives/src/crypto.rs @@ -17,19 +17,23 @@ pub mod kzg; -use crate::{Blake2b256Hash, BLAKE2B_256_HASH_SIZE}; -use blake2_rfc::blake2b::{blake2b, Blake2b}; +use crate::Blake2b256Hash; +use blake2::digest::typenum::U32; +use blake2::digest::{FixedOutput, Update}; +use blake2::{Blake2b, Blake2bMac, Digest}; /// BLAKE2b-256 hashing of a single value. pub fn blake2b_256_hash(data: &[u8]) -> Blake2b256Hash { - blake2b_256_hash_with_key(data, &[]) + let mut state = Blake2b::::new(); + Update::update(&mut state, data); + state.finalize_fixed().into() } /// BLAKE2b-256 hashing of a single value truncated to 254 bits. /// /// TODO: We probably wouldn't need this eventually pub fn blake2b_256_254_hash(data: &[u8]) -> Blake2b256Hash { - let mut hash = blake2b_256_hash_with_key(data, &[]); + let mut hash = blake2b_256_hash(data); // Erase last 2 bits to effectively truncate the hash (number is interpreted as little-endian) hash[31] &= 0b00111111; hash @@ -39,21 +43,20 @@ pub fn blake2b_256_254_hash(data: &[u8]) -> Blake2b256Hash { /// /// PANIC: Panics if key is longer than 64 bytes. pub fn blake2b_256_hash_with_key(data: &[u8], key: &[u8]) -> Blake2b256Hash { - blake2b(BLAKE2B_256_HASH_SIZE, key, data) - .as_bytes() - .try_into() - .expect("Initialized with correct length; qed") + let mut state = Blake2bMac::::new_with_salt_and_personal(key, &[], &[]) + .expect("Only panics when key is over 64 bytes as specified in function description"); + Update::update(&mut state, data); + state.finalize_fixed().into() } /// BLAKE2b-256 hashing of a list of values. pub fn blake2b_256_hash_list(data: &[&[u8]]) -> Blake2b256Hash { - let mut state = Blake2b::new(BLAKE2B_256_HASH_SIZE); + let mut state = Blake2b::::new(); for d in data { - state.update(d); + Update::update(&mut state, d); } state .finalize() - .as_bytes() .try_into() .expect("Initialized with correct length; qed") } diff --git a/crates/subspace-farmer/Cargo.toml b/crates/subspace-farmer/Cargo.toml index 9ca9bedc98..41b495d3af 100644 --- a/crates/subspace-farmer/Cargo.toml +++ b/crates/subspace-farmer/Cargo.toml @@ -15,7 +15,6 @@ include = [ anyhow = "1.0.66" async-trait = "0.1.58" base58 = "0.2.0" -blake2-rfc = "0.2.18" bytesize = "1.1.0" clap = { version = "4.0.26", features = ["color", "derive"] } derive_more = "0.99.17" diff --git a/crates/subspace-farmer/src/bin/subspace-farmer/ss58.rs b/crates/subspace-farmer/src/bin/subspace-farmer/ss58.rs index 4cbaa152db..9b04ddf4c0 100644 --- a/crates/subspace-farmer/src/bin/subspace-farmer/ss58.rs +++ b/crates/subspace-farmer/src/bin/subspace-farmer/ss58.rs @@ -19,7 +19,8 @@ use base58::FromBase58; use ss58_registry::Ss58AddressFormat; -use subspace_core_primitives::{PublicKey, PUBLIC_KEY_LENGTH}; +use subspace_core_primitives::crypto::blake2b_256_hash_list; +use subspace_core_primitives::{Blake2b256Hash, PublicKey, PUBLIC_KEY_LENGTH}; use thiserror::Error; const PREFIX: &[u8] = b"SS58PRE"; @@ -74,7 +75,7 @@ pub(crate) fn parse_ss58_reward_address(s: &str) -> Result Result 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]) -> Blake2b256Hash { + blake2b_256_hash_list(&[PREFIX, data]) } diff --git a/domains/client/domain-executor/Cargo.toml b/domains/client/domain-executor/Cargo.toml index 48e6f83a06..23052a5e08 100644 --- a/domains/client/domain-executor/Cargo.toml +++ b/domains/client/domain-executor/Cargo.toml @@ -5,8 +5,7 @@ authors = ["Parity Technologies "] edition = "2021" [dependencies] -# Not using `blake2` crate due to https://github.com/RustCrypto/hashes/issues/360 -blake2-rfc = "0.2.18" +blake2 = "0.10.5" codec = { package = "parity-scale-codec", version = "3.2.1", features = [ "derive" ] } crossbeam = "0.8.2" domain-block-builder = { version = "0.1.0", path = "../block-builder" } diff --git a/domains/client/domain-executor/src/merkle_tree.rs b/domains/client/domain-executor/src/merkle_tree.rs index 119ad11cd1..1cdb7d8e5d 100644 --- a/domains/client/domain-executor/src/merkle_tree.rs +++ b/domains/client/domain-executor/src/merkle_tree.rs @@ -1,14 +1,16 @@ -use blake2_rfc::blake2b::Blake2b; +use blake2::digest::typenum::U32; +use blake2::digest::FixedOutput; +use blake2::{Blake2b, Digest}; use merkletree::hash::Algorithm; use std::hash::Hasher; -use subspace_core_primitives::{Blake2b256Hash, BLAKE2B_256_HASH_SIZE}; +use subspace_core_primitives::Blake2b256Hash; #[derive(Clone)] -pub(super) struct Blake2b256Algorithm(Blake2b); +pub(super) struct Blake2b256Algorithm(Blake2b); impl Default for Blake2b256Algorithm { fn default() -> Self { - Self(Blake2b::new(BLAKE2B_256_HASH_SIZE)) + Self(Blake2b::new()) } } @@ -27,12 +29,7 @@ impl Hasher for Blake2b256Algorithm { impl Algorithm for Blake2b256Algorithm { #[inline] fn hash(&mut self) -> Blake2b256Hash { - self.0 - .clone() - .finalize() - .as_bytes() - .try_into() - .expect("Initialized with correct length; qed") + self.0.clone().finalize_fixed().into() } #[inline]