multiversx_sc/contract_base/wrappers/
crypto_wrapper.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
172
173
174
175
176
177
178
179
180
181
182
183
use core::marker::PhantomData;

use crate::{
    api::{CryptoApi, CryptoApiImpl, KECCAK256_RESULT_LEN, SHA256_RESULT_LEN},
    types::{ManagedBuffer, ManagedByteArray, ManagedType, ManagedVec, MessageHashType},
};

#[derive(Default)]
pub struct CryptoWrapper<A>
where
    A: CryptoApi,
{
    _phantom: PhantomData<A>,
}

impl<A> CryptoWrapper<A>
where
    A: CryptoApi,
{
    pub fn new() -> Self {
        CryptoWrapper {
            _phantom: PhantomData,
        }
    }

    pub fn sha256<B: core::borrow::Borrow<ManagedBuffer<A>>>(
        &self,
        data: B,
    ) -> ManagedByteArray<A, SHA256_RESULT_LEN> {
        unsafe {
            let result = ManagedByteArray::new_uninit();
            A::crypto_api_impl().sha256_managed(result.get_handle(), data.borrow().get_handle());
            result
        }
    }

    pub fn keccak256<B: core::borrow::Borrow<ManagedBuffer<A>>>(
        &self,
        data: B,
    ) -> ManagedByteArray<A, KECCAK256_RESULT_LEN> {
        unsafe {
            let result = ManagedByteArray::new_uninit();
            A::crypto_api_impl().keccak256_managed(result.get_handle(), data.borrow().get_handle());
            result
        }
    }

    pub fn ripemd160<B: core::borrow::Borrow<ManagedBuffer<A>>>(
        &self,
        data: B,
    ) -> ManagedByteArray<A, { crate::api::RIPEMD_RESULT_LEN }> {
        unsafe {
            let result = ManagedByteArray::new_uninit();
            A::crypto_api_impl().ripemd160_managed(result.get_handle(), data.borrow().get_handle());
            result
        }
    }

    pub fn verify_bls(
        &self,
        key: &ManagedBuffer<A>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
    ) {
        A::crypto_api_impl().verify_bls_managed(
            key.get_handle(),
            message.get_handle(),
            signature.get_handle(),
        )
    }

    /// Calls the Vm to verify ed25519 signature.
    ///
    /// Does not return result, will fail tx directly!
    ///
    /// The error comes straight form the VM, the message is "invalid signature".
    pub fn verify_ed25519(
        &self,
        key: &ManagedBuffer<A>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
    ) {
        A::crypto_api_impl().verify_ed25519_managed(
            key.get_handle(),
            message.get_handle(),
            signature.get_handle(),
        )
    }

    /// Note: the signature is minimum 2 bytes in length,
    /// the second byte encodes the length of the remaining signature bytes.
    pub fn verify_secp256k1(
        &self,
        key: &ManagedBuffer<A>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
    ) -> bool {
        A::crypto_api_impl().verify_secp256k1_managed(
            key.get_handle(),
            message.get_handle(),
            signature.get_handle(),
        )
    }

    pub fn verify_custom_secp256k1(
        &self,
        key: &ManagedBuffer<A>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
        hash_type: MessageHashType,
    ) -> bool {
        A::crypto_api_impl().verify_custom_secp256k1_managed(
            key.get_handle(),
            message.get_handle(),
            signature.get_handle(),
            hash_type,
        )
    }

    pub fn encode_secp256k1_der_signature(
        &self,
        r: &ManagedBuffer<A>,
        s: &ManagedBuffer<A>,
    ) -> ManagedBuffer<A> {
        unsafe {
            let result = ManagedBuffer::new_uninit();
            A::crypto_api_impl().encode_secp256k1_der_signature_managed(
                r.get_handle(),
                s.get_handle(),
                result.get_handle(),
            );
            result
        }
    }

    /// Calls the Vm to verify secp256r1 signature.
    ///
    /// Does not return result, will fail tx directly!
    pub fn verify_secp256r1(
        &self,
        key: &ManagedBuffer<A>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
    ) {
        A::crypto_api_impl().verify_secp256r1_managed(
            key.get_handle(),
            message.get_handle(),
            signature.get_handle(),
        )
    }

    /// Calls the Vm to verify BLS signature share.
    ///
    /// Does not return result, will fail tx directly!
    pub fn verify_bls_signature_share(
        &self,
        key: &ManagedBuffer<A>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
    ) {
        A::crypto_api_impl().verify_bls_signature_share_managed(
            key.get_handle(),
            message.get_handle(),
            signature.get_handle(),
        )
    }

    /// Calls the Vm to verify BLS aggregated signature.
    ///
    /// Does not return result, will fail tx directly!
    pub fn verify_bls_aggregated_signature(
        &self,
        keys: &ManagedVec<A, ManagedBuffer<A>>,
        message: &ManagedBuffer<A>,
        signature: &ManagedBuffer<A>,
    ) {
        A::crypto_api_impl().verify_bls_aggregated_signature_managed(
            keys.get_handle(),
            message.get_handle(),
            signature.get_handle(),
        )
    }
}