Skip to content

Commit

Permalink
Replace most of blake2 usage with blake3
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Oct 9, 2023
1 parent fdddc4a commit 3af0e6a
Show file tree
Hide file tree
Showing 29 changed files with 134 additions and 180 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/pallet-feeds/src/feed_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use codec::{Compact, CompactLen, Decode, Encode};
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::vec;
use sp_std::vec::Vec;
use subspace_core_primitives::Blake2b256Hash;
use subspace_core_primitives::Blake3Hash;

/// Holds the offset to some portion of data within/or the object
#[derive(Debug)]
Expand All @@ -35,7 +35,7 @@ pub enum FeedObjectMapping {
}

impl FeedObjectMapping {
pub(crate) fn try_into_call_object<FeedID: Encode, Hasher: Fn(&[u8]) -> Blake2b256Hash>(
pub(crate) fn try_into_call_object<FeedID: Encode, Hasher: Fn(&[u8]) -> Blake3Hash>(
self,
feed_id: FeedID,
object: &[u8],
Expand Down
6 changes: 3 additions & 3 deletions crates/pallet-feeds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use core::mem;
pub use pallet::*;
use sp_std::vec;
use sp_std::vec::Vec;
use subspace_core_primitives::{crypto, Blake2b256Hash};
use subspace_core_primitives::{crypto, Blake3Hash};

pub mod feed_processor;
#[cfg(all(feature = "std", test))]
Expand Down Expand Up @@ -366,7 +366,7 @@ mod pallet {
#[derive(Debug)]
pub struct CallObject {
/// Key to the object located at the offset.
pub key: Blake2b256Hash,
pub key: Blake3Hash,
/// Offset of object in the encoded call.
pub offset: u32,
}
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<T: Config> Call<T> {
let mut co = object_mapping.try_into_call_object(
feed_id,
object.as_slice(),
|data| crypto::blake2b_256_hash(data),
|data| crypto::blake3_hash(data),
)?;
co.offset += base_offset;
Some(co)
Expand Down
4 changes: 2 additions & 2 deletions crates/pallet-feeds/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn create_content_feed(object: Object, kind: MockFeedProcessorKind, contents: Ve
content
);

assert_eq!(mappings[i].key, crypto::blake2b_256_hash(&content));
assert_eq!(mappings[i].key, crypto::blake3_hash(&content));
})
});
}
Expand Down Expand Up @@ -290,7 +290,7 @@ fn create_custom_content_feed(
// key should match the feed name spaced key
assert_eq!(
mappings[i].key,
crypto::blake2b_256_hash_list(&[&FEED_ID.encode(), key.as_slice()])
crypto::blake3_hash_list(&[&FEED_ID.encode(), key.as_slice()])
);
});

Expand Down
12 changes: 6 additions & 6 deletions crates/pallet-object-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ mod mock;
mod tests;

pub use pallet::*;
use subspace_core_primitives::{crypto, Blake2b256Hash};
use subspace_core_primitives::{crypto, Blake3Hash};

#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use log::debug;
use sp_std::prelude::*;
use subspace_core_primitives::{crypto, Blake2b256Hash};
use subspace_core_primitives::{crypto, Blake3Hash};

#[pallet::config]
pub trait Config: frame_system::Config {
Expand All @@ -52,7 +52,7 @@ mod pallet {
/// New object was added.
ObjectSubmitted {
who: T::AccountId,
object_id: Blake2b256Hash,
object_id: Blake3Hash,
object_size: u32,
},
}
Expand All @@ -69,7 +69,7 @@ mod pallet {

let object_size = object.len() as u32;

let object_id = crypto::blake2b_256_hash(&object);
let object_id = crypto::blake3_hash(&object);

debug!(
target: "runtime:object-store",
Expand All @@ -93,7 +93,7 @@ mod pallet {
#[derive(Debug)]
pub struct CallObject {
/// Object hash
pub hash: Blake2b256Hash,
pub hash: Blake3Hash,
/// Offset of object in the encoded call.
pub offset: u32,
}
Expand All @@ -105,7 +105,7 @@ impl<T: Config> Call<T> {
Self::put { object } => {
// `1` corresponds to `Call::put {}` enum variant encoding.
Some(CallObject {
hash: crypto::blake2b_256_hash(object),
hash: crypto::blake3_hash(object),
offset: 1,
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/pallet-object-store/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ACCOUNT_ID: u64 = 100;
fn can_do_put() {
new_test_ext().execute_with(|| {
let object = vec![1, 2, 3, 4, 5];
let object_id = crypto::blake2b_256_hash(&object);
let object_id = crypto::blake3_hash(&object);
let object_size = object.len() as u32;

assert_ok!(ObjectStore::put(RuntimeOrigin::signed(ACCOUNT_ID), object));
Expand Down
4 changes: 2 additions & 2 deletions crates/pallet-subspace/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod benchmarks {
use sp_std::vec;
use sp_std::vec::Vec;
use subspace_core_primitives::{
ArchivedBlockProgress, Blake2b256Hash, LastArchivedBlock, PotOutput, SegmentHeader,
ArchivedBlockProgress, Blake3Hash, LastArchivedBlock, PotOutput, SegmentHeader,
SegmentIndex, Solution, SolutionRange,
};

Expand Down Expand Up @@ -154,7 +154,7 @@ mod benchmarks {
SegmentHeader::V0 {
segment_index,
segment_commitment: subspace_core_primitives::SegmentCommitment::default(),
prev_segment_header_hash: Blake2b256Hash::default(),
prev_segment_header_hash: Blake3Hash::default(),
last_archived_block: LastArchivedBlock {
number: 0,
archived_progress: ArchivedBlockProgress::Complete,
Expand Down
4 changes: 2 additions & 2 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use subspace_archiving::archiver::{Archiver, NewArchivedSegment};
use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg};
use subspace_core_primitives::crypto::Scalar;
use subspace_core_primitives::{
ArchivedBlockProgress, ArchivedHistorySegment, Blake2b256Hash, BlockNumber, HistorySize,
ArchivedBlockProgress, ArchivedHistorySegment, Blake3Hash, BlockNumber, HistorySize,
LastArchivedBlock, Piece, PieceOffset, PosSeed, PotOutput, PublicKey, Record,
RecordedHistorySegment, SegmentCommitment, SegmentHeader, SegmentIndex, SlotNumber, Solution,
SolutionRange, REWARD_SIGNING_CONTEXT,
Expand Down Expand Up @@ -392,7 +392,7 @@ pub fn create_segment_header(segment_index: SegmentIndex) -> SegmentHeader {
SegmentHeader::V0 {
segment_index,
segment_commitment: SegmentCommitment::default(),
prev_segment_header_hash: Blake2b256Hash::default(),
prev_segment_header_hash: Blake3Hash::default(),
last_archived_block: LastArchivedBlock {
number: 0,
archived_progress: ArchivedBlockProgress::Complete,
Expand Down
6 changes: 3 additions & 3 deletions crates/sp-domains/src/bundle_producer_election.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use scale_info::TypeInfo;
use sp_core::crypto::{VrfPublic, Wraps};
use sp_core::sr25519::vrf::{VrfOutput, VrfSignature, VrfTranscript};
use sp_std::vec::Vec;
use subspace_core_primitives::Blake2b256Hash;
use subspace_core_primitives::Blake3Hash;

const VRF_TRANSCRIPT_LABEL: &[u8] = b"bundle_producer_election";

/// Generates a domain-specific vrf transcript from given global_challenge.
pub fn make_transcript(domain_id: DomainId, global_challenge: &Blake2b256Hash) -> VrfTranscript {
pub fn make_transcript(domain_id: DomainId, global_challenge: &Blake3Hash) -> VrfTranscript {
VrfTranscript::new(
VRF_TRANSCRIPT_LABEL,
&[
Expand Down Expand Up @@ -70,7 +70,7 @@ pub(crate) fn verify_vrf_signature(
domain_id: DomainId,
public_key: &OperatorPublicKey,
vrf_signature: &VrfSignature,
global_challenge: &Blake2b256Hash,
global_challenge: &Blake3Hash,
) -> Result<(), VrfProofError> {
if !public_key.as_inner_ref().vrf_verify(
&make_transcript(domain_id, global_challenge).into(),
Expand Down
8 changes: 4 additions & 4 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ use sp_runtime::traits::{
use sp_runtime::{DigestItem, OpaqueExtrinsic, Percent};
use sp_std::vec::Vec;
use sp_weights::Weight;
use subspace_core_primitives::crypto::blake2b_256_hash;
use subspace_core_primitives::{bidirectional_distance, Blake2b256Hash, Randomness, U256};
use subspace_core_primitives::crypto::blake3_hash;
use subspace_core_primitives::{bidirectional_distance, Blake3Hash, Randomness, U256};
use subspace_runtime_primitives::{Balance, Moment};

/// Key type for Operator.
Expand Down Expand Up @@ -505,10 +505,10 @@ impl ProofOfElection {
}

/// Computes the VRF hash.
pub fn vrf_hash(&self) -> Blake2b256Hash {
pub fn vrf_hash(&self) -> Blake3Hash {
let mut bytes = self.vrf_signature.output.encode();
bytes.append(&mut self.vrf_signature.proof.encode());
blake2b_256_hash(&bytes)
blake3_hash(&bytes)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/sp-domains/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rs_merkle::Hasher;
use scale_info::TypeInfo;
use sp_runtime::traits::{BlakeTwo256, Hash};
use sp_std::vec::Vec;
use subspace_core_primitives::Blake2b256Hash;
use subspace_core_primitives::Blake3Hash;

/// Merkle tree using [`Blake2b256Algorithm`].
pub type MerkleTree = rs_merkle::MerkleTree<Blake2b256Algorithm>;
Expand Down Expand Up @@ -37,9 +37,9 @@ impl Default for Blake2b256Algorithm {
}

impl Hasher for Blake2b256Algorithm {
type Hash = Blake2b256Hash;
type Hash = Blake3Hash;

fn hash(data: &[u8]) -> Blake2b256Hash {
fn hash(data: &[u8]) -> Blake3Hash {
let mut hasher = Blake2b::new();
hasher.update(data);
hasher.finalize_fixed().into()
Expand Down
12 changes: 6 additions & 6 deletions crates/subspace-archiving/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ use parity_scale_codec::{Compact, CompactLen, Decode, Encode, Input, Output};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use subspace_core_primitives::crypto::kzg::{Commitment, Kzg, Witness};
use subspace_core_primitives::crypto::{blake2b_256_254_hash_to_scalar, Scalar};
use subspace_core_primitives::crypto::{blake3_254_hash_to_scalar, Scalar};
use subspace_core_primitives::objects::{
BlockObject, BlockObjectMapping, PieceObject, PieceObjectMapping,
};
use subspace_core_primitives::{
ArchivedBlockProgress, ArchivedHistorySegment, Blake2b256Hash, BlockNumber, LastArchivedBlock,
ArchivedBlockProgress, ArchivedHistorySegment, Blake3Hash, BlockNumber, LastArchivedBlock,
PieceArray, RawRecord, RecordWitness, RecordedHistorySegment, SegmentCommitment, SegmentHeader,
SegmentIndex,
};
Expand Down Expand Up @@ -238,7 +238,7 @@ pub struct Archiver {
/// An index of the current segment
segment_index: SegmentIndex,
/// Hash of the segment header of the previous segment
prev_segment_header_hash: Blake2b256Hash,
prev_segment_header_hash: Blake3Hash,
/// Last archived block
last_archived_block: LastArchivedBlock,
}
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Archiver {
erasure_coding,
kzg,
segment_index: SegmentIndex::ZERO,
prev_segment_header_hash: Blake2b256Hash::default(),
prev_segment_header_hash: Blake3Hash::default(),
last_archived_block: INITIAL_LAST_ARCHIVED_BLOCK,
})
}
Expand Down Expand Up @@ -780,7 +780,7 @@ impl Archiver {
.poly(
&record_commitments
.iter()
.map(|commitment| blake2b_256_254_hash_to_scalar(&commitment.to_bytes()))
.map(|commitment| blake3_254_hash_to_scalar(&commitment.to_bytes()))
.collect::<Vec<_>>(),
)
.expect("Internally produced values must never fail; qed");
Expand Down Expand Up @@ -890,7 +890,7 @@ pub fn is_piece_valid(
return false;
};

let commitment_hash = blake2b_256_254_hash_to_scalar(commitment.as_ref());
let commitment_hash = blake3_254_hash_to_scalar(commitment.as_ref());

kzg.verify(
&segment_commitment,
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-archiving/src/piece_reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::num::NonZeroUsize;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use subspace_core_primitives::crypto::kzg::{Commitment, Kzg, Polynomial};
use subspace_core_primitives::crypto::{blake2b_256_254_hash_to_scalar, Scalar};
use subspace_core_primitives::crypto::{blake3_254_hash_to_scalar, Scalar};
use subspace_core_primitives::{ArchivedHistorySegment, Piece, RawRecord};
use subspace_erasure_coding::ErasureCoding;

Expand Down Expand Up @@ -177,7 +177,7 @@ impl PiecesReconstructor {
reconstructed_piece
.commitment_mut()
.copy_from_slice(&commitment_bytes);
blake2b_256_254_hash_to_scalar(&commitment_bytes)
blake3_254_hash_to_scalar(&commitment_bytes)
})
.collect::<Vec<_>>();

Expand Down
23 changes: 11 additions & 12 deletions crates/subspace-archiving/tests/integration/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg};
use subspace_core_primitives::crypto::Scalar;
use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping, PieceObject};
use subspace_core_primitives::{
ArchivedBlockProgress, ArchivedHistorySegment, Blake2b256Hash, LastArchivedBlock, PieceArray,
ArchivedBlockProgress, ArchivedHistorySegment, Blake3Hash, LastArchivedBlock, PieceArray,
Record, RecordedHistorySegment, SegmentCommitment, SegmentHeader, SegmentIndex,
BLAKE2B_256_HASH_SIZE,
};

fn extract_data<O: Into<u64>>(data: &[u8], offset: O) -> &[u8] {
Expand Down Expand Up @@ -82,11 +81,11 @@ fn archiver() {
let object_mapping = BlockObjectMapping {
objects: vec![
BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
offset: 0u32,
},
BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
offset: RecordedHistorySegment::SIZE as u32 / 3,
},
],
Expand Down Expand Up @@ -118,15 +117,15 @@ fn archiver() {
let object_mapping = BlockObjectMapping {
objects: vec![
BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
offset: RecordedHistorySegment::SIZE as u32 / 6,
},
BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
offset: RecordedHistorySegment::SIZE as u32 / 5,
},
BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
offset: RecordedHistorySegment::SIZE as u32 / 3 * 2 - 200,
},
],
Expand All @@ -151,7 +150,7 @@ fn archiver() {
first_archived_segment
.segment_header
.prev_segment_header_hash(),
[0u8; BLAKE2B_256_HASH_SIZE]
Blake3Hash::default(),
);
{
let last_archived_block = first_archived_segment.segment_header.last_archived_block();
Expand Down Expand Up @@ -398,7 +397,7 @@ fn invalid_usage() {
SegmentHeader::V0 {
segment_index: SegmentIndex::ZERO,
segment_commitment: SegmentCommitment::default(),
prev_segment_header_hash: Blake2b256Hash::default(),
prev_segment_header_hash: Blake3Hash::default(),
last_archived_block: LastArchivedBlock {
number: 0,
archived_progress: ArchivedBlockProgress::Partial(10),
Expand All @@ -424,7 +423,7 @@ fn invalid_usage() {
SegmentHeader::V0 {
segment_index: SegmentIndex::ZERO,
segment_commitment: SegmentCommitment::default(),
prev_segment_header_hash: Blake2b256Hash::default(),
prev_segment_header_hash: Blake3Hash::default(),
last_archived_block: LastArchivedBlock {
number: 0,
archived_progress: ArchivedBlockProgress::Partial(10),
Expand Down Expand Up @@ -521,7 +520,7 @@ fn spill_over_edge_case() {
vec![0u8; RecordedHistorySegment::SIZE],
BlockObjectMapping {
objects: vec![BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
offset: 0,
}],
},
Expand Down Expand Up @@ -566,7 +565,7 @@ fn object_on_the_edge_of_segment() {

let mut second_block = vec![0u8; RecordedHistorySegment::SIZE * 2];
let object_mapping = BlockObject::V0 {
hash: Blake2b256Hash::default(),
hash: Blake3Hash::default(),
// Offset is designed to fall exactly on the edge of the segment
offset: RecordedHistorySegment::SIZE as u32
// Segment enum variant
Expand Down
Loading

0 comments on commit 3af0e6a

Please sign in to comment.