1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use move_deps::{
move_binary_format::errors::PartialVMResult,
move_core_types::vm_status::sub_status::NFE_BCS_SERIALIZATION_FAILURE,
move_vm_runtime::native_functions::{NativeContext, NativeFunction},
move_vm_types::{
loaded_data::runtime_types::Type,
natives::function::NativeResult,
pop_arg,
values::{values_impl::Reference, Value},
},
};
use smallvec::smallvec;
use std::{collections::VecDeque, hash::Hasher, sync::Arc};
#[derive(Debug, Clone)]
pub struct SipHashGasParameters {
pub base_cost: u64,
pub unit_cost: u64,
}
fn native_sip_hash(
_gas_params: &SipHashGasParameters,
context: &mut NativeContext,
mut ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(args.len() == 1);
let ref_to_val = pop_arg!(args, Reference);
let arg_type = ty_args.pop().unwrap();
let serialized_value_opt = match context.type_to_type_layout(&arg_type)? {
None => None,
Some(layout) => ref_to_val.read_ref()?.simple_serialize(&layout),
};
let serialized_value = match serialized_value_opt {
None => {
return Ok(NativeResult::err(0, NFE_BCS_SERIALIZATION_FAILURE));
}
Some(serialized_value) => serialized_value,
};
let mut hasher = siphasher::sip::SipHasher::new();
hasher.write(&serialized_value);
let hash = hasher.finish();
Ok(NativeResult::ok(0, smallvec![Value::u64(hash)]))
}
pub fn make_native_sip_hash(gas_params: SipHashGasParameters) -> NativeFunction {
Arc::new(move |context, ty_args, args| native_sip_hash(&gas_params, context, ty_args, args))
}
#[derive(Debug, Clone)]
pub struct GasParameters {
pub sip_hash: SipHashGasParameters,
}
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [("sip_hash", make_native_sip_hash(gas_params.sip_hash))];
crate::natives::helpers::make_module_natives(natives)
}