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

Replace blake2-rfc with blake2 #111

Merged
merged 1 commit into from
Sep 15, 2022
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://github.com/paritytech/parity-db/"
description = "Key-value database for the blockchain"

[dependencies]
blake2-rfc = "0.2.18"
blake2 = "0.10.4"
crc32fast = "1.2.0"
fs2 = "0.4.3"
hex = "0.4.2"
Expand Down
17 changes: 14 additions & 3 deletions src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ enum IterStateOrCorrupted {

#[inline]
pub fn hash_key(key: &[u8], salt: &Salt, uniform: bool, db_version: u32) -> Key {
use blake2::{
digest::{typenum::U32, FixedOutput, Update},
Blake2bMac,
};

let mut k = Key::default();
if uniform {
if db_version <= 5 {
Expand All @@ -137,7 +142,11 @@ pub fn hash_key(key: &[u8], salt: &Salt, uniform: bool, db_version: u32) -> Key
}
}
} else {
k.copy_from_slice(blake2_rfc::blake2b::blake2b(32, salt, key).as_bytes());
let mut ctx = Blake2bMac::<U32>::new_with_salt_and_personal(salt, &[], &[])
.expect("Salt length (32) is a valid key length (<= 64)");
Comment on lines +145 to +146
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the naming is a bit misleading.

That is, with previous implementation the salt was used as the blake2b-rfc "key" argument while the key was used as the "data" argument.

The new_with_salt_and_personal has a dedicated parameter (the 2nd) for the salt. But to obtain the same MAC as before we have to pass salt as the 1st parameter

ctx.update(key);
let hash = ctx.finalize_fixed();
k.copy_from_slice(&hash);
}
k
}
Expand Down Expand Up @@ -676,6 +685,8 @@ impl HashColumn {
start_chunk: u64,
skip_preimage_indexes: bool,
) -> Result<()> {
use blake2::{digest::typenum::U32, Blake2b, Digest};

let tables = self.tables.read();
let source = &tables.index;

Expand All @@ -694,8 +705,8 @@ impl HashColumn {
} else {
value
};
let key = blake2_rfc::blake2b::blake2b(32, &[], &value);
let key = self.hash_key(key.as_bytes());
let key = Blake2b::<U32>::digest(&value);
let key = self.hash_key(&key);
let state = IterStateOrCorrupted::Item(IterState {
chunk_index: index,
key,
Expand Down
4 changes: 3 additions & 1 deletion src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,10 @@ mod test {
}

fn key(k: u32) -> Key {
use blake2::{digest::typenum::U32, Blake2b, Digest};
let mut key = Key::default();
key.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], &k.to_le_bytes()).as_bytes());
let hash = Blake2b::<U32>::digest(&k.to_le_bytes());
key.copy_from_slice(&hash);
key
}

Expand Down