Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change solution distance function #1604

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion crates/subspace-core-primitives/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn blake2b_256_254_hash_to_scalar(data: &[u8]) -> Scalar {
/// BLAKE2b-256 keyed hashing of a single value.
///
/// PANIC: Panics if key is longer than 64 bytes.
pub fn blake2b_256_hash_with_key(data: &[u8], key: &[u8]) -> Blake2b256Hash {
pub fn blake2b_256_hash_with_key(key: &[u8], data: &[u8]) -> Blake2b256Hash {
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);
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl SectorId {
* recent_history_fraction.1.in_pieces().get()
/ recent_history_fraction.0.in_pieces().get();
let input_hash =
U256::from_le_bytes(blake2b_256_hash_with_key(&self.0, &piece_offset.to_bytes()));
U256::from_le_bytes(blake2b_256_hash_with_key(&piece_offset.to_bytes(), &self.0));
let history_size_in_pieces = history_size.in_pieces().get();
let num_interleaved_pieces = 1.max(
u64::from(max_pieces_in_sector) * recent_history_fraction.0.in_pieces().get()
Expand Down
21 changes: 13 additions & 8 deletions crates/subspace-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use schnorrkel::SignatureError;
use sp_arithmetic::traits::SaturatedConversion;
use subspace_archiving::archiver;
use subspace_core_primitives::crypto::kzg::Kzg;
use subspace_core_primitives::crypto::{blake2b_256_254_hash_to_scalar, blake2b_256_hash_list};
use subspace_core_primitives::crypto::{
blake2b_256_254_hash_to_scalar, blake2b_256_hash_list, blake2b_256_hash_with_key,
};
use subspace_core_primitives::{
Blake2b256Hash, BlockNumber, BlockWeight, HistorySize, PublicKey, Randomness, Record,
RewardSignature, SectorId, SectorSlotChallenge, SegmentCommitment, SlotNumber, Solution,
Expand Down Expand Up @@ -101,15 +103,18 @@ fn calculate_solution_distance(
.next()
.expect("Solution range is smaller in size than global challenge; qed"),
);
let sector_slot_challenge_as_solution_range: SolutionRange = SolutionRange::from_le_bytes(
*sector_slot_challenge
.array_chunks::<{ mem::size_of::<SolutionRange>() }>()
.next()
.expect("Solution range is smaller in size than sector slot challenge; qed"),
);
let sector_slot_challenge_with_audit_chunk =
blake2b_256_hash_with_key(sector_slot_challenge.as_ref(), &audit_chunk.to_le_bytes());
let sector_slot_challenge_with_audit_chunk_as_solution_range: SolutionRange =
SolutionRange::from_le_bytes(
*sector_slot_challenge_with_audit_chunk
.array_chunks::<{ mem::size_of::<SolutionRange>() }>()
.next()
.expect("Solution range is smaller in size than blake2b-256 hash; qed"),
);
subspace_core_primitives::bidirectional_distance(
&global_challenge_as_solution_range,
&(audit_chunk ^ sector_slot_challenge_as_solution_range),
&sector_slot_challenge_with_audit_chunk_as_solution_range,
)
}

Expand Down