forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add keccak256 and blake2b hash functions to move api (MystenLabs#7986)
- Loading branch information
Showing
12 changed files
with
178 additions
and
80 deletions.
There are no files selected for viewing
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,64 @@ | ||
|
||
<a name="0x2_hash"></a> | ||
|
||
# Module `0x2::hash` | ||
|
||
Module which defines hash functions. Note that Sha-256 and Sha3-256 is available in the std::hash module in the | ||
standard library. | ||
|
||
|
||
- [Function `blake2b256`](#0x2_hash_blake2b256) | ||
- [Function `keccak256`](#0x2_hash_keccak256) | ||
|
||
|
||
<pre><code></code></pre> | ||
|
||
|
||
|
||
<a name="0x2_hash_blake2b256"></a> | ||
|
||
## Function `blake2b256` | ||
|
||
@param data: Arbitrary binary data to hash | ||
Hash the input bytes using Blake2b-256 and returns 32 bytes. | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_blake2b256">blake2b256</a>(data: &<a href="">vector</a><u8>): <a href="">vector</a><u8> | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_blake2b256">blake2b256</a>(data: &<a href="">vector</a><u8>): <a href="">vector</a><u8>; | ||
</code></pre> | ||
|
||
|
||
|
||
</details> | ||
|
||
<a name="0x2_hash_keccak256"></a> | ||
|
||
## Function `keccak256` | ||
|
||
@param data: Arbitrary binary data to hash | ||
Hash the input bytes using keccak256 and returns 32 bytes. | ||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_keccak256">keccak256</a>(data: &<a href="">vector</a><u8>): <a href="">vector</a><u8> | ||
</code></pre> | ||
|
||
|
||
|
||
<details> | ||
<summary>Implementation</summary> | ||
|
||
|
||
<pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="hash.md#0x2_hash_keccak256">keccak256</a>(data: &<a href="">vector</a><u8>): <a href="">vector</a><u8>; | ||
</code></pre> | ||
|
||
|
||
|
||
</details> |
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,14 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/// Module which defines hash functions. Note that Sha-256 and Sha3-256 is available in the std::hash module in the | ||
/// standard library. | ||
module sui::hash { | ||
/// @param data: Arbitrary binary data to hash | ||
/// Hash the input bytes using Blake2b-256 and returns 32 bytes. | ||
native public fun blake2b256(data: &vector<u8>): vector<u8>; | ||
|
||
/// @param data: Arbitrary binary data to hash | ||
/// Hash the input bytes using keccak256 and returns 32 bytes. | ||
native public fun keccak256(data: &vector<u8>): vector<u8>; | ||
} |
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,50 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use crate::legacy_empty_cost; | ||
use fastcrypto::hash::{Blake2b256, HashFunction, Keccak256}; | ||
use move_binary_format::errors::PartialVMResult; | ||
use move_vm_runtime::native_functions::NativeContext; | ||
use move_vm_types::{ | ||
loaded_data::runtime_types::Type, | ||
natives::function::NativeResult, | ||
pop_arg, | ||
values::{Value, VectorRef}, | ||
}; | ||
use smallvec::smallvec; | ||
use std::collections::VecDeque; | ||
|
||
fn hash<H: HashFunction<DIGEST_SIZE>, const DIGEST_SIZE: usize>( | ||
_context: &mut NativeContext, | ||
ty_args: Vec<Type>, | ||
mut args: VecDeque<Value>, | ||
) -> PartialVMResult<NativeResult> { | ||
debug_assert!(ty_args.is_empty()); | ||
debug_assert!(args.len() == 1); | ||
|
||
// TODO: implement native gas cost estimation https://github.com/MystenLabs/sui/issues/3593 | ||
let cost = legacy_empty_cost(); | ||
let msg = pop_arg!(args, VectorRef); | ||
|
||
Ok(NativeResult::ok( | ||
cost, | ||
smallvec![Value::vector_u8( | ||
H::digest(msg.as_bytes_ref().as_slice()).digest | ||
)], | ||
)) | ||
} | ||
|
||
pub fn keccak256( | ||
context: &mut NativeContext, | ||
ty_args: Vec<Type>, | ||
args: VecDeque<Value>, | ||
) -> PartialVMResult<NativeResult> { | ||
hash::<Keccak256, 32>(context, ty_args, args) | ||
} | ||
|
||
pub fn blake2b256( | ||
context: &mut NativeContext, | ||
ty_args: Vec<Type>, | ||
args: VecDeque<Value>, | ||
) -> PartialVMResult<NativeResult> { | ||
hash::<Blake2b256, 32>(context, ty_args, args) | ||
} |
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
Oops, something went wrong.