-
Notifications
You must be signed in to change notification settings - Fork 222
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
Host function generic_hash()
with blake2b/blake3 support.
#4412
Closed
koxu1996
wants to merge
15
commits into
casper-network:feat-1.6
from
koxu1996:feature/generic-hash-ffi-1.6
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2dc1b1a
Install `blake3` package in execution_engine.
koxu1996 930d0b8
Add blake3 hasher to execution engine.
koxu1996 356b5ca
Add `HashAlgoType` to base types.
koxu1996 eb3eb45
Implement host function `GenericHash`.
koxu1996 267873a
Charge default cost for using `generic_hash`.
koxu1996 f27876d
Introduce `casper_generic_hash` FFI in contract.
koxu1996 ef78d59
Add `crypto::generic_hash()` to contract API.
koxu1996 5d400ec
Update contract API changelog.
koxu1996 8bd8fbb
Use default features for `blake3` to utilize SIMD.
koxu1996 d38140f
Update `HashAlgoType` description.
koxu1996 2c70942
Pass `hash_algo_type` directly by value.
koxu1996 4ec1f31
Fix signature (arguments length) in WASM resolver.
koxu1996 85885d0
Change error type for `HashAlgoType::try_from`.
koxu1996 d064e2c
Return `ApiError::InvalidArgument` for invalid `hash_algo_type`.
koxu1996 4b85568
Fix formatting.
koxu1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/// The number of bytes in a Blake3 hash. | ||
/// NOTE: It does not make sense to use different lengths. | ||
const BLAKE3_DIGEST_LENGTH: usize = 32; | ||
|
||
#[doc(hidden)] | ||
pub fn blake3<T: AsRef<[u8]>>(data: T) -> [u8; BLAKE3_DIGEST_LENGTH] { | ||
let mut result = [0; BLAKE3_DIGEST_LENGTH]; | ||
let mut hasher = blake3::Hasher::new(); | ||
|
||
hasher.update(data.as_ref()); | ||
let hash = hasher.finalize(); | ||
let hash_bytes: &[u8; BLAKE3_DIGEST_LENGTH] = hash.as_bytes(); | ||
result.copy_from_slice(hash_bytes); | ||
result | ||
} |
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
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
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,22 @@ | ||
//! Functions with cryptographic utils. | ||
|
||
use casper_types::{api_error, HashAlgoType, BLAKE2B_DIGEST_LENGTH}; | ||
|
||
use crate::{ext_ffi, unwrap_or_revert::UnwrapOrRevert}; | ||
|
||
/// Computes digest hash, using provided algorithm type. | ||
pub fn generic_hash<T: AsRef<[u8]>>(input: T, algo: HashAlgoType) -> [u8; 32] { | ||
let mut ret = [0; 32]; | ||
|
||
let result = unsafe { | ||
ext_ffi::casper_generic_hash( | ||
input.as_ref().as_ptr(), | ||
input.as_ref().len(), | ||
algo as u8, | ||
ret.as_mut_ptr(), | ||
BLAKE2B_DIGEST_LENGTH, | ||
) | ||
}; | ||
api_error::result_from(result).unwrap_or_revert(); | ||
ret | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something I just noticed - we've done hashing already, but we're checking the size after the fact. Since the output of the hash is constant, we could also move this check above the
checked_memory_slice
line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the output is always expected to be 32 bytes, those hashes use different constants for their digest size:
BLAKE2B_DIGEST_LENGTH
BLAKE3_DIGEST_LENGTH
Unless we implement all the necessary hash functions (e.g., sha256, keccak, poseidon) and generalize it with something like
GENERIC_HASH_DIGEST_LENGTH
, I would prefer to avoid using hardcoded pre-checks.