Skip to content
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

Implement safe API for RIPEMD-160 #648

Merged
merged 1 commit into from
Dec 17, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
- Added missing pattern for only including context and vm config to `testing_env!` to remove friction
- Added `_array` suffix versions of `sha256`, `keccak256`, and `keccak512` hash functions in `env` [PR 646](https://github.com/near/near-sdk-rs/pull/646)
- These return a fixed length array instead of heap allocating with `Vec<u8>`
- Added `ripemd160_array` hash function that returns a fixed length byte array [PR 648](https://github.com/near/near-sdk-rs/pull/648)

## `4.0.0-pre.4` [10-15-2021]
- Unpin `syn` dependency in macros from `=1.0.57` to be more composable with other crates. [PR 605](https://github.com/near/near-sdk-rs/pull/605)
Binary file modified examples/callback-results/res/callback_results.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified examples/fungible-token/res/defi.wasm
Binary file not shown.
Binary file modified examples/fungible-token/res/fungible_token.wasm
Binary file not shown.
Binary file not shown.
Binary file modified examples/mission-control/res/mission_control.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/approval_receiver.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/non_fungible_token.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/token_receiver.wasm
Binary file not shown.
Binary file not shown.
Binary file modified examples/status-message/res/status_message.wasm
Binary file not shown.
Binary file modified examples/test-contract/res/test_contract.wasm
Binary file not shown.
22 changes: 22 additions & 0 deletions near-sdk/src/environment/env.rs
Original file line number Diff line number Diff line change
@@ -54,6 +54,12 @@ macro_rules! method_into_register {
//* Note: need specific length functions because const generics don't work with mem::transmute
//* https://github.com/rust-lang/rust/issues/61956

pub(crate) unsafe fn read_register_fixed_20(register_id: u64) -> [u8; 20] {
let mut hash = [MaybeUninit::<u8>::uninit(); 20];
sys::read_register(register_id, hash.as_mut_ptr() as _);
std::mem::transmute(hash)
}

pub(crate) unsafe fn read_register_fixed_32(register_id: u64) -> [u8; 32] {
let mut hash = [MaybeUninit::<u8>::uninit(); 32];
sys::read_register(register_id, hash.as_mut_ptr() as _);
@@ -279,6 +285,17 @@ pub fn keccak512_array(value: &[u8]) -> [u8; 64] {
}
}

/// Hashes the bytes using the RIPEMD-160 hash function. This returns a 20 byte hash.
pub fn ripemd160_array(value: &[u8]) -> [u8; 20] {
//* SAFETY: ripemd160 syscall will always generate 20 bytes inside of the atomic op register
//* so the read will have a sufficient buffer of 20, and can transmute from uninit
//* because all bytes are filled. This assumes a valid ripemd160 implementation.
unsafe {
sys::ripemd160(value.len() as _, value.as_ptr() as _, ATOMIC_OP_REGISTER);
read_register_fixed_20(ATOMIC_OP_REGISTER)
}
}

// ################
// # Promises API #
// ################
@@ -789,5 +806,10 @@ mod tests {
.unwrap()
.as_slice()
);

assert_eq!(
&super::ripemd160_array(b"some value"),
base64::decode("CfAl/tcE4eysj4iyvaPlaHbaA6w=").unwrap().as_slice()
);
}
}
4 changes: 4 additions & 0 deletions near-sdk/src/environment/mock/mocked_blockchain.rs
Original file line number Diff line number Diff line change
@@ -191,6 +191,10 @@ mod mock_chain {
with_mock_interface(|b| b.keccak512(value_len, value_ptr, register_id))
}
#[no_mangle]
extern "C" fn ripemd160(value_len: u64, value_ptr: u64, register_id: u64) {
with_mock_interface(|b| b.ripemd160(value_len, value_ptr, register_id))
}
#[no_mangle]
extern "C" fn value_return(value_len: u64, value_ptr: u64) {
with_mock_interface(|b| b.value_return(value_len, value_ptr))
}