Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add keccak512 hash #7428

Merged
2 commits merged into from
Oct 26, 2020
Merged
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
9 changes: 9 additions & 0 deletions primitives/core/src/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ pub fn keccak_256(data: &[u8]) -> [u8; 32] {
output
}

/// Do a keccak 512-bit hash and return result.
pub fn keccak_512(data: &[u8]) -> [u8; 64] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be exposed as a host function (sp_io)? If someone uses sp_core::hashing::keccak_512 it will compute the hash in the WASM instead.
CC @pepyakin

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, for getting native implementations one would need to put them under Hashing runtime interface in sp_io.

But this case is rather confusing, that implies that one needs to know to use sp_io instead of sp_core versions.

Copy link
Contributor

@tomusdrw tomusdrw Nov 12, 2020

Choose a reason for hiding this comment

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

Well if you look at other functions in sp_io::hashing host-side implementation it actually delegates to sp_core::hashing, so the entire file seems a bit confusing for me, cause it's in theory it should never be used in runtime directly, yet the crate compiles just fine and we jut rely on developers to use the correct stuff.

Copy link
Contributor

@tomusdrw tomusdrw Nov 12, 2020

Choose a reason for hiding this comment

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

Update: sp_core::hashing is gated under full-crypto, but I think it still deserves a bit more documentation.

let mut keccak = Keccak::v512();
keccak.update(data);
let mut output = [0u8; 64];
keccak.finalize(&mut output);
output
}

/// Do a sha2 256-bit hash and return result.
pub fn sha2_256(data: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
Expand Down