Skip to content

Commit

Permalink
[gh-511] add ripemd-160 hash support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Jul 19, 2023
1 parent deaae34 commit d85de32
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
25 changes: 25 additions & 0 deletions crates/rooch-framework/doc/hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Move standard library and wrap the functions at here.
- [Function `sha3_256`](#0x3_hash_sha3_256)
- [Function `blake2b256`](#0x3_hash_blake2b256)
- [Function `keccak256`](#0x3_hash_keccak256)
- [Function `ripemd160`](#0x3_hash_ripemd160)


<pre><code><b>use</b> <a href="../doc/hash.md#0x1_hash">0x1::hash</a>;
Expand Down Expand Up @@ -112,4 +113,28 @@ Hash the input bytes using keccak256 and returns 32 bytes.



</details>

<a name="0x3_hash_ripemd160"></a>

## Function `ripemd160`

@param data: Arbitrary binary data to hash
Hash the input bytes using ripemd160 and returns 20 bytes.


<pre><code><b>public</b> <b>fun</b> <a href="hash.md#0x3_hash_ripemd160">ripemd160</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>public</b> <b>fun</b> <a href="hash.md#0x3_hash_ripemd160">ripemd160</a>(data: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;;
</code></pre>



</details>
19 changes: 17 additions & 2 deletions crates/rooch-framework/sources/crypto/hash.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
/// Move standard library and wrap the functions at here.
module rooch_framework::hash {

// TODO #511 add a ripemd 160 hash here

public fun sha2_256(data: vector<u8>): vector<u8> {
std::hash::sha2_256(data)
}
Expand All @@ -20,6 +18,10 @@ module rooch_framework::hash {
/// Hash the input bytes using keccak256 and returns 32 bytes.
native public fun keccak256(data: &vector<u8>): vector<u8>;

/// @param data: Arbitrary binary data to hash
/// Hash the input bytes using ripemd160 and returns 20 bytes.
native public fun ripemd160(data: &vector<u8>): vector<u8>;

#[test]
fun test_keccak256_hash() {
let msg = b"hello world!";
Expand All @@ -45,4 +47,17 @@ module rooch_framework::hash {
let long_msg = b"57caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd6";
let _ = blake2b256(&long_msg);
}

#[test]
fun test_ripemd160_hash() {
let msg = b"Hello, World!";
let hashed_msg_bytes = x"527a6a4b9a6da75607546842e0e00105350b1aaf";
let hashed_msg = ripemd160(&msg);
assert!(hashed_msg == hashed_msg_bytes, 0);

let empty_msg = b"";
let _ = ripemd160(&empty_msg);
let long_msg = b"57caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd657caa176af1ac0433c5df30e8dabcd2ec1af1e92a26eced5f719b88458777cd6";
let _ = ripemd160(&long_msg);
}
}
25 changes: 23 additions & 2 deletions crates/rooch-framework/src/natives/rooch_framework/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::natives::helpers::{make_module_natives, make_native};
// TODO #511 add a ripemd 160 hash here
use fastcrypto::hash::{Blake2b256, HashFunction, Keccak256};
use fastcrypto::hash::{Blake2b256, HashFunction, Keccak256, Ripemd160};
use move_binary_format::errors::PartialVMResult;
use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
use move_vm_types::{
Expand Down Expand Up @@ -67,6 +66,22 @@ pub fn native_blake2b256(
hash::<Blake2b256, 32>(context, ty_args, args)
}

/***************************************************************************************************
* native fun ripemd160
* Implementation of the Move native function `hash::ripemd160(data: &vector<u8>): vector<u8>`
* gas cost: hash_ripemd160_cost_base | base cost for function call and fixed opers
* + hash_ripemd160_data_cost_per_byte * msg.len() | cost depends on length of message
* + hash_ripemd160_data_cost_per_block * num_blocks | cost depends on number of blocks in message
**************************************************************************************************/
pub fn native_ripemd160(
_gas_params: &FromBytesGasParameters,
context: &mut NativeContext,
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
hash::<Ripemd160, 20>(context, ty_args, args)
}

#[derive(Debug, Clone)]
pub struct FromBytesGasParameters {}

Expand All @@ -84,13 +99,15 @@ impl FromBytesGasParameters {
pub struct GasParameters {
pub keccak256: FromBytesGasParameters,
pub blake2b256: FromBytesGasParameters,
pub ripemd160: FromBytesGasParameters,
}

impl GasParameters {
pub fn zeros() -> Self {
Self {
keccak256: FromBytesGasParameters::zeros(),
blake2b256: FromBytesGasParameters::zeros(),
ripemd160: FromBytesGasParameters::zeros(),
}
}
}
Expand All @@ -105,6 +122,10 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
"blake2b256",
make_native(gas_params.blake2b256, native_blake2b256),
),
(
"ripemd160",
make_native(gas_params.ripemd160, native_ripemd160),
),
];

make_module_natives(natives)
Expand Down
Binary file modified crates/rooch-genesis/genesis/genesis
Binary file not shown.

0 comments on commit d85de32

Please sign in to comment.