multiversx_chain_vm/vm_hooks/vh_handler/
vh_crypto.rs

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
use crate::{crypto_functions, types::RawHandle, vm_hooks::VMHooksHandlerSource};

pub trait VMHooksCrypto: VMHooksHandlerSource {
    fn sha256_managed(&self, dest: RawHandle, data_handle: RawHandle) {
        // default implementation used in debugger
        // the VM has a dedicated hook
        let mut types = self.m_types_lock();
        let data = types.mb_get(data_handle);
        let result_bytes = crypto_functions::sha256(data);
        types.mb_set(dest, result_bytes[..].to_vec());
    }

    fn keccak256_managed(&self, dest: RawHandle, data_handle: RawHandle) {
        // default implementation used in debugger
        // the VM has a dedicated hook
        let mut types = self.m_types_lock();
        let data = types.mb_get(data_handle);
        let result_bytes = crypto_functions::keccak256(data);
        types.mb_set(dest, result_bytes[..].to_vec());
    }

    /// Should crash if the signature is invalid.
    fn verify_ed25519_managed(&self, key: RawHandle, message: RawHandle, signature: RawHandle) {
        let types = self.m_types_lock();
        let key = types.mb_get(key);
        let message = types.mb_get(message);
        let signature = types.mb_get(signature);
        let sig_valid = crypto_functions::verify_ed25519(key, message, signature);
        if !sig_valid {
            self.vm_error("invalid signature");
        }
    }
}