-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4903: Implement generic_hash() host function to support hashing algorithms r=igor-casper a=igor-casper This PR directly addresses #4892, referencing #4411 as prior work. Adds host function called generic_hash(input, type) with support for the following types: - HashAlgorithm::Blake2b - using the existing blake2b implementation, - HashAlgorithm::Blake3 - introducing the blake3 library, - HashAlgorithm::Sha256 - introducing the sha2 library, **Example usage** ```rs #![no_std] #![no_main] extern crate alloc; use alloc::string::String; use casper_contract::contract_api::cryptography; use casper_types::crypto::HashAlgorithm; #[no_mangle] pub extern "C" fn call() { let data = "sha256 hash test"; let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA]; let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256); assert_eq!( hash, expected, "Hash mismatch" ); } ``` **Notes** - Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside - The costs were referenced from #4411 Co-authored-by: igor-casper <[email protected]> Co-authored-by: igor-casper <[email protected]>
- Loading branch information
Showing
28 changed files
with
396 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! Cryptography module containing hashing functions used internally | ||
//! by the execution engine | ||
|
||
use blake2::{ | ||
digest::{Update, VariableOutput}, | ||
Blake2bVar, | ||
}; | ||
use sha2::{Digest, Sha256}; | ||
|
||
/// The number of bytes in a hash. | ||
/// All hash functions in this module have a digest length of 32. | ||
pub const DIGEST_LENGTH: usize = 32; | ||
|
||
/// The 32-byte digest blake2b hash function | ||
pub fn blake2b<T: AsRef<[u8]>>(data: T) -> [u8; DIGEST_LENGTH] { | ||
let mut result = [0; DIGEST_LENGTH]; | ||
// NOTE: Assumed safe as `BLAKE2B_DIGEST_LENGTH` is a valid value for a hasher | ||
let mut hasher = Blake2bVar::new(DIGEST_LENGTH).expect("should create hasher"); | ||
|
||
hasher.update(data.as_ref()); | ||
|
||
// NOTE: This should never fail, because result is exactly DIGEST_LENGTH long | ||
hasher.finalize_variable(&mut result).ok(); | ||
|
||
result | ||
} | ||
|
||
/// The 32-byte digest blake3 hash function | ||
pub fn blake3<T: AsRef<[u8]>>(data: T) -> [u8; DIGEST_LENGTH] { | ||
let mut result = [0; DIGEST_LENGTH]; | ||
let mut hasher = blake3::Hasher::new(); | ||
|
||
hasher.update(data.as_ref()); | ||
let hash = hasher.finalize(); | ||
let hash_bytes: &[u8; DIGEST_LENGTH] = hash.as_bytes(); | ||
result.copy_from_slice(hash_bytes); | ||
result | ||
} | ||
|
||
/// The 32-byte digest sha256 hash function | ||
pub fn sha256<T: AsRef<[u8]>>(data: T) -> [u8; DIGEST_LENGTH] { | ||
Sha256::digest(data).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.