multiversx_sc_wasm_adapter/api/
crypto_api_node.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::VmApiImpl;
use multiversx_sc::{
    api::{CryptoApi, CryptoApiImpl},
    types::MessageHashType,
};

extern "C" {
    fn managedSha256(inputHandle: i32, outputHandle: i32) -> i32;

    fn managedKeccak256(inputHandle: i32, outputHandle: i32) -> i32;

    fn managedRipemd160(inputHandle: i32, outputHandle: i32) -> i32;

    fn managedVerifyBLS(keyHandle: i32, messageHandle: i32, sigHandle: i32) -> i32;

    fn managedVerifyEd25519(keyHandle: i32, messageHandle: i32, sigHandle: i32) -> i32;

    fn managedVerifySecp256k1(keyHandle: i32, messageHandle: i32, sigHandle: i32) -> i32;

    fn managedVerifyCustomSecp256k1(
        keyHandle: i32,
        messageHandle: i32,
        sigHandle: i32,
        hashType: i32,
    ) -> i32;

    fn managedEncodeSecp256k1DerSignature(rHandle: i32, sHandle: i32, sigHandle: i32) -> i32;

    fn managedVerifySecp256r1(keyHandle: i32, messageHandle: i32, sigHandle: i32) -> i32;

    fn managedVerifyBLSSignatureShare(keyHandle: i32, messageHandle: i32, sigHandle: i32) -> i32;

    fn managedVerifyBLSAggregatedSignature(
        keyHandle: i32,
        messageHandle: i32,
        sigHandle: i32,
    ) -> i32;
}

impl CryptoApi for VmApiImpl {
    type CryptoApiImpl = VmApiImpl;

    #[inline]
    fn crypto_api_impl() -> Self::CryptoApiImpl {
        VmApiImpl {}
    }
}

impl CryptoApiImpl for VmApiImpl {
    fn sha256_managed(
        &self,
        result_handle: Self::ManagedBufferHandle,
        data_handle: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedSha256(data_handle, result_handle);
        }
    }

    fn keccak256_managed(
        &self,
        result_handle: Self::ManagedBufferHandle,
        data_handle: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedKeccak256(data_handle, result_handle);
        }
    }

    #[inline]
    fn ripemd160_managed(
        &self,
        dest: Self::ManagedBufferHandle,
        data_handle: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedRipemd160(data_handle, dest);
        }
    }

    #[inline]
    fn verify_bls_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedVerifyBLS(key, message, signature);
        }
    }

    #[inline]
    fn verify_ed25519_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
    ) {
        unsafe {
            managedVerifyEd25519(key, message, signature);
        }
    }

    #[inline]
    fn verify_secp256k1_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
    ) -> bool {
        unsafe { managedVerifySecp256k1(key, message, signature) == 0 }
    }

    #[inline]
    fn verify_custom_secp256k1_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
        hash_type: MessageHashType,
    ) -> bool {
        unsafe {
            managedVerifyCustomSecp256k1(key, message, signature, hash_type.as_u8() as i32) == 0
        }
    }

    fn encode_secp256k1_der_signature_managed(
        &self,
        r: Self::ManagedBufferHandle,
        s: Self::ManagedBufferHandle,
        dest_sig_handle: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedEncodeSecp256k1DerSignature(r, s, dest_sig_handle);
        }
    }

    fn verify_secp256r1_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedVerifySecp256r1(key, message, signature);
        }
    }

    fn verify_bls_signature_share_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedVerifyBLSSignatureShare(key, message, signature);
        }
    }

    fn verify_bls_aggregated_signature_managed(
        &self,
        key: Self::ManagedBufferHandle,
        message: Self::ManagedBufferHandle,
        signature: Self::ManagedBufferHandle,
    ) {
        unsafe {
            let _ = managedVerifyBLSAggregatedSignature(key, message, signature);
        }
    }
}