Skip to content

Commit

Permalink
Merge pull request #955 from subspace/remove-blake2-rfc-dependency
Browse files Browse the repository at this point in the history
Remove blake2-rfc dependency
  • Loading branch information
nazar-pc authored Nov 28, 2022
2 parents c87c613 + e969ba9 commit 3a86423
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 70 deletions.
36 changes: 4 additions & 32 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions crates/subspace-archiving/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions crates/subspace-core-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
25 changes: 14 additions & 11 deletions crates/subspace-core-primitives/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<U32>::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
Expand All @@ -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::<U32>::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::<U32>::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")
}
1 change: 0 additions & 1 deletion crates/subspace-farmer-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion crates/subspace-farmer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 5 additions & 7 deletions crates/subspace-farmer/src/bin/subspace-farmer/ss58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -74,7 +75,7 @@ pub(crate) fn parse_ss58_reward_address(s: &str) -> Result<PublicKey, Ss58Parsin
}

let hash = ss58hash(&data[0..PUBLIC_KEY_LENGTH + prefix_len]);
let checksum = &hash.as_bytes()[0..CHECKSUM_LEN];
let checksum = &hash[0..CHECKSUM_LEN];
if data[PUBLIC_KEY_LENGTH + prefix_len..PUBLIC_KEY_LENGTH + prefix_len + CHECKSUM_LEN]
!= *checksum
{
Expand All @@ -89,9 +90,6 @@ pub(crate) fn parse_ss58_reward_address(s: &str) -> Result<PublicKey, Ss58Parsin
Ok(PublicKey::from(bytes))
}

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]) -> Blake2b256Hash {
blake2b_256_hash_list(&[PREFIX, data])
}
3 changes: 1 addition & 2 deletions domains/client/domain-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ authors = ["Parity Technologies <[email protected]>"]
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" }
Expand Down
17 changes: 7 additions & 10 deletions domains/client/domain-executor/src/merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -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<U32>);

impl Default for Blake2b256Algorithm {
fn default() -> Self {
Self(Blake2b::new(BLAKE2B_256_HASH_SIZE))
Self(Blake2b::new())
}
}

Expand All @@ -27,12 +29,7 @@ impl Hasher for Blake2b256Algorithm {
impl Algorithm<Blake2b256Hash> 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]
Expand Down

0 comments on commit 3a86423

Please sign in to comment.