Skip to content

Commit e969ba9

Browse files
committed
Replace blake2-rfc with blake2 dependency such that we have one less crate to build
1 parent b1e27ca commit e969ba9

File tree

7 files changed

+33
-64
lines changed

7 files changed

+33
-64
lines changed

Cargo.lock

Lines changed: 4 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/subspace-core-primitives/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ bench = false
1919
ark-bls12-381 = "0.3.0"
2020
ark-ff = "0.3.0"
2121
ark-poly = "0.3.0"
22-
# Not using `blake2` crate due to https://github.com/RustCrypto/hashes/issues/360
23-
blake2-rfc = { version = "0.2.18", default-features = false }
22+
blake2 = { version = "0.10.5", default-features = false }
2423
derive_more = "0.99.17"
2524
dusk-bls12_381 = { version = "0.11.2", default-features = false, features = ["alloc", "groups", "pairings", "endo"] }
2625
dusk-bytes = "0.1"
@@ -47,7 +46,7 @@ std = [
4746
"ark-bls12-381/std",
4847
"ark-ff/std",
4948
"ark-poly/std",
50-
"blake2-rfc/std",
49+
"blake2/std",
5150
"dusk-bls12_381/std",
5251
"dusk-plonk/std",
5352
"hex/serde",

crates/subspace-core-primitives/src/crypto.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717
1818
pub mod kzg;
1919

20-
use crate::{Blake2b256Hash, BLAKE2B_256_HASH_SIZE};
21-
use blake2_rfc::blake2b::{blake2b, Blake2b};
20+
use crate::Blake2b256Hash;
21+
use blake2::digest::typenum::U32;
22+
use blake2::digest::{FixedOutput, Update};
23+
use blake2::{Blake2b, Blake2bMac, Digest};
2224

2325
/// BLAKE2b-256 hashing of a single value.
2426
pub fn blake2b_256_hash(data: &[u8]) -> Blake2b256Hash {
25-
blake2b_256_hash_with_key(data, &[])
27+
let mut state = Blake2b::<U32>::new();
28+
Update::update(&mut state, data);
29+
state.finalize_fixed().into()
2630
}
2731

2832
/// BLAKE2b-256 hashing of a single value truncated to 254 bits.
2933
///
3034
/// TODO: We probably wouldn't need this eventually
3135
pub fn blake2b_256_254_hash(data: &[u8]) -> Blake2b256Hash {
32-
let mut hash = blake2b_256_hash_with_key(data, &[]);
36+
let mut hash = blake2b_256_hash(data);
3337
// Erase last 2 bits to effectively truncate the hash (number is interpreted as little-endian)
3438
hash[31] &= 0b00111111;
3539
hash
@@ -39,21 +43,20 @@ pub fn blake2b_256_254_hash(data: &[u8]) -> Blake2b256Hash {
3943
///
4044
/// PANIC: Panics if key is longer than 64 bytes.
4145
pub fn blake2b_256_hash_with_key(data: &[u8], key: &[u8]) -> Blake2b256Hash {
42-
blake2b(BLAKE2B_256_HASH_SIZE, key, data)
43-
.as_bytes()
44-
.try_into()
45-
.expect("Initialized with correct length; qed")
46+
let mut state = Blake2bMac::<U32>::new_with_salt_and_personal(key, &[], &[])
47+
.expect("Only panics when key is over 64 bytes as specified in function description");
48+
Update::update(&mut state, data);
49+
state.finalize_fixed().into()
4650
}
4751

4852
/// BLAKE2b-256 hashing of a list of values.
4953
pub fn blake2b_256_hash_list(data: &[&[u8]]) -> Blake2b256Hash {
50-
let mut state = Blake2b::new(BLAKE2B_256_HASH_SIZE);
54+
let mut state = Blake2b::<U32>::new();
5155
for d in data {
52-
state.update(d);
56+
Update::update(&mut state, d);
5357
}
5458
state
5559
.finalize()
56-
.as_bytes()
5760
.try_into()
5861
.expect("Initialized with correct length; qed")
5962
}

crates/subspace-farmer/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ include = [
1515
anyhow = "1.0.66"
1616
async-trait = "0.1.58"
1717
base58 = "0.2.0"
18-
blake2-rfc = "0.2.18"
1918
bytesize = "1.1.0"
2019
clap = { version = "4.0.26", features = ["color", "derive"] }
2120
derive_more = "0.99.17"

crates/subspace-farmer/src/bin/subspace-farmer/ss58.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
2020
use base58::FromBase58;
2121
use ss58_registry::Ss58AddressFormat;
22-
use subspace_core_primitives::{PublicKey, PUBLIC_KEY_LENGTH};
22+
use subspace_core_primitives::crypto::blake2b_256_hash_list;
23+
use subspace_core_primitives::{Blake2b256Hash, PublicKey, PUBLIC_KEY_LENGTH};
2324
use thiserror::Error;
2425

2526
const PREFIX: &[u8] = b"SS58PRE";
@@ -74,7 +75,7 @@ pub(crate) fn parse_ss58_reward_address(s: &str) -> Result<PublicKey, Ss58Parsin
7475
}
7576

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

92-
fn ss58hash(data: &[u8]) -> blake2_rfc::blake2b::Blake2bResult {
93-
let mut context = blake2_rfc::blake2b::Blake2b::new(64);
94-
context.update(PREFIX);
95-
context.update(data);
96-
context.finalize()
93+
fn ss58hash(data: &[u8]) -> Blake2b256Hash {
94+
blake2b_256_hash_list(&[PREFIX, data])
9795
}

domains/client/domain-executor/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ authors = ["Parity Technologies <[email protected]>"]
55
edition = "2021"
66

77
[dependencies]
8-
# Not using `blake2` crate due to https://github.com/RustCrypto/hashes/issues/360
9-
blake2-rfc = "0.2.18"
8+
blake2 = "0.10.5"
109
codec = { package = "parity-scale-codec", version = "3.2.1", features = [ "derive" ] }
1110
crossbeam = "0.8.2"
1211
domain-block-builder = { version = "0.1.0", path = "../block-builder" }

domains/client/domain-executor/src/merkle_tree.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use blake2_rfc::blake2b::Blake2b;
1+
use blake2::digest::typenum::U32;
2+
use blake2::digest::FixedOutput;
3+
use blake2::{Blake2b, Digest};
24
use merkletree::hash::Algorithm;
35
use std::hash::Hasher;
4-
use subspace_core_primitives::{Blake2b256Hash, BLAKE2B_256_HASH_SIZE};
6+
use subspace_core_primitives::Blake2b256Hash;
57

68
#[derive(Clone)]
7-
pub(super) struct Blake2b256Algorithm(Blake2b);
9+
pub(super) struct Blake2b256Algorithm(Blake2b<U32>);
810

911
impl Default for Blake2b256Algorithm {
1012
fn default() -> Self {
11-
Self(Blake2b::new(BLAKE2B_256_HASH_SIZE))
13+
Self(Blake2b::new())
1214
}
1315
}
1416

@@ -27,12 +29,7 @@ impl Hasher for Blake2b256Algorithm {
2729
impl Algorithm<Blake2b256Hash> for Blake2b256Algorithm {
2830
#[inline]
2931
fn hash(&mut self) -> Blake2b256Hash {
30-
self.0
31-
.clone()
32-
.finalize()
33-
.as_bytes()
34-
.try_into()
35-
.expect("Initialized with correct length; qed")
32+
self.0.clone().finalize_fixed().into()
3633
}
3734

3835
#[inline]

0 commit comments

Comments
 (0)