dharitri_sc_wasm_adapter/api/managed_types/
elliptic_curve_api_node.rs

1use dharitri_sc::{api::EllipticCurveApiImpl, types::heap::BoxedBytes};
2
3unsafe extern "C" {
4    fn createEC(dataOffset: i32, dataLength: i32) -> i32;
5
6    fn managedCreateEC(nameHandle: i32) -> i32;
7
8    fn ellipticCurveGetValues(
9        ecHandle: i32,
10        fieldOrderHandle: i32,
11        basePointOrderHandle: i32,
12        eqConstantHandle: i32,
13        xBasePointHandle: i32,
14        yBasePointHandle: i32,
15    ) -> i32;
16
17    fn getCurveLengthEC(ecHandle: i32) -> i32;
18
19    fn getPrivKeyByteLengthEC(ecHandle: i32) -> i32;
20
21    fn addEC(
22        xResultHandle: i32,
23        yResultHandle: i32,
24        ecHandle: i32,
25        fstPointXHandle: i32,
26        fstPointYHandle: i32,
27        sndPointXHandle: i32,
28        sndPointYHandle: i32,
29    );
30
31    fn doubleEC(
32        xResultHandle: i32,
33        yResultHandle: i32,
34        ecHandle: i32,
35        pointXHandle: i32,
36        pointYHandle: i32,
37    );
38
39    fn isOnCurveEC(ecHandle: i32, pointXHandle: i32, pointYHandle: i32) -> i32;
40
41    fn scalarMultEC(
42        xResultHandle: i32,
43        yResultHandle: i32,
44        ecHandle: i32,
45        pointXHandle: i32,
46        pointYHandle: i32,
47        dataOffset: *const u8,
48        length: i32,
49    ) -> i32;
50
51    fn managedScalarMultEC(
52        xResultHandle: i32,
53        yResultHandle: i32,
54        ecHandle: i32,
55        pointXHandle: i32,
56        pointYHandle: i32,
57        dataHandle: i32,
58    ) -> i32;
59
60    fn scalarBaseMultEC(
61        xResultHandle: i32,
62        yResultHandle: i32,
63        ecHandle: i32,
64        dataOffset: *const u8,
65        length: i32,
66    ) -> i32;
67
68    fn managedScalarBaseMultEC(
69        xResultHandle: i32,
70        yResultHandle: i32,
71        ecHandle: i32,
72        dataHandle: i32,
73    ) -> i32;
74
75    fn marshalEC(xPairHandle: i32, yPairHandle: i32, ecHandle: i32, resultOffset: *mut u8) -> i32;
76
77    fn managedMarshalEC(
78        xPairHandle: i32,
79        yPairHandle: i32,
80        ecHandle: i32,
81        resultHandle: i32,
82    ) -> i32;
83
84    fn marshalCompressedEC(
85        xPairHandle: i32,
86        yPairHandle: i32,
87        ecHandle: i32,
88        resultOffset: *mut u8,
89    ) -> i32;
90
91    fn managedMarshalCompressedEC(
92        xPairHandle: i32,
93        yPairHandle: i32,
94        ecHandle: i32,
95        resultHandle: i32,
96    ) -> i32;
97
98    fn unmarshalEC(
99        xResultHandle: i32,
100        yResultHandle: i32,
101        ecHandle: i32,
102        dataOffset: *const u8,
103        length: i32,
104    ) -> i32;
105
106    fn managedUnmarshalEC(
107        xResultHandle: i32,
108        yResultHandle: i32,
109        ecHandle: i32,
110        dataHandle: i32,
111    ) -> i32;
112
113    fn unmarshalCompressedEC(
114        xResultHandle: i32,
115        yResultHandle: i32,
116        ecHandle: i32,
117        dataOffset: *const u8,
118        length: i32,
119    ) -> i32;
120
121    fn managedUnmarshalCompressedEC(
122        xResultHandle: i32,
123        yResultHandle: i32,
124        ecHandle: i32,
125        dataHandle: i32,
126    ) -> i32;
127
128    fn generateKeyEC(
129        xPubKeyHandle: i32,
130        yPubKeyHandle: i32,
131        ecHandle: i32,
132        resultOffset: *mut u8,
133    ) -> i32;
134
135    fn managedGenerateKeyEC(
136        xPubKeyHandle: i32,
137        yPubKeyHandle: i32,
138        ecHandle: i32,
139        resultHandle: i32,
140    ) -> i32;
141
142}
143
144impl EllipticCurveApiImpl for crate::api::VmApiImpl {
145    fn ec_create_from_name_bytes(&self, name: &[u8]) -> Self::EllipticCurveHandle {
146        unsafe { createEC(name.as_ptr() as i32, name.len() as i32) }
147    }
148
149    fn ec_create_from_name_mb(
150        &self,
151        name_handle: Self::ManagedBufferHandle,
152    ) -> Self::ManagedBufferHandle {
153        unsafe { managedCreateEC(name_handle) }
154    }
155
156    fn ec_get_values(
157        &self,
158        ec_handle: Self::EllipticCurveHandle,
159        field_order_handle: Self::BigIntHandle,
160        base_point_order_handle: Self::BigIntHandle,
161        eq_constant_handle: Self::BigIntHandle,
162        x_base_point_handle: Self::BigIntHandle,
163        y_base_point_handle: Self::BigIntHandle,
164    ) {
165        unsafe {
166            let _ = ellipticCurveGetValues(
167                ec_handle,
168                field_order_handle,
169                base_point_order_handle,
170                eq_constant_handle,
171                x_base_point_handle,
172                y_base_point_handle,
173            );
174        }
175    }
176
177    fn ec_curve_length(&self, ec_handle: Self::EllipticCurveHandle) -> u32 {
178        unsafe { getCurveLengthEC(ec_handle) as u32 }
179    }
180
181    fn ec_private_key_byte_length(&self, ec_handle: Self::EllipticCurveHandle) -> u32 {
182        unsafe { getPrivKeyByteLengthEC(ec_handle) as u32 }
183    }
184
185    fn ec_add(
186        &self,
187        x_result_handle: Self::BigIntHandle,
188        y_result_handle: Self::BigIntHandle,
189        ec_handle: Self::EllipticCurveHandle,
190        x_first_point: Self::BigIntHandle,
191        y_first_point: Self::BigIntHandle,
192        x_second_point: Self::BigIntHandle,
193        y_second_point: Self::BigIntHandle,
194    ) {
195        unsafe {
196            addEC(
197                x_result_handle,
198                y_result_handle,
199                ec_handle,
200                x_first_point,
201                y_first_point,
202                x_second_point,
203                y_second_point,
204            );
205        }
206    }
207
208    fn ec_double(
209        &self,
210        x_result_handle: Self::BigIntHandle,
211        y_result_handle: Self::BigIntHandle,
212        ec_handle: Self::EllipticCurveHandle,
213        x_point_handle: Self::BigIntHandle,
214        y_point_handle: Self::BigIntHandle,
215    ) {
216        unsafe {
217            doubleEC(
218                x_result_handle,
219                y_result_handle,
220                ec_handle,
221                x_point_handle,
222                y_point_handle,
223            );
224        }
225    }
226
227    fn ec_is_on_curve(
228        &self,
229        ec_handle: Self::EllipticCurveHandle,
230        x_point_handle: Self::BigIntHandle,
231        y_point_handle: Self::BigIntHandle,
232    ) -> bool {
233        unsafe { isOnCurveEC(ec_handle, x_point_handle, y_point_handle) > 0 }
234    }
235
236    fn ec_scalar_mult_legacy(
237        &self,
238        x_result_handle: Self::BigIntHandle,
239        y_result_handle: Self::BigIntHandle,
240        ec_handle: Self::EllipticCurveHandle,
241        x_point_handle: Self::BigIntHandle,
242        y_point_handle: Self::BigIntHandle,
243        data: &[u8],
244    ) {
245        unsafe {
246            scalarMultEC(
247                x_result_handle,
248                y_result_handle,
249                ec_handle,
250                x_point_handle,
251                y_point_handle,
252                data.as_ptr(),
253                data.len() as i32,
254            );
255        }
256    }
257
258    fn ec_scalar_mult(
259        &self,
260        x_result_handle: Self::BigIntHandle,
261        y_result_handle: Self::BigIntHandle,
262        ec_handle: Self::EllipticCurveHandle,
263        x_point_handle: Self::BigIntHandle,
264        y_point_handle: Self::BigIntHandle,
265        data: Self::ManagedBufferHandle,
266    ) {
267        unsafe {
268            managedScalarMultEC(
269                x_result_handle,
270                y_result_handle,
271                ec_handle,
272                x_point_handle,
273                y_point_handle,
274                data,
275            );
276        }
277    }
278
279    fn ec_scalar_base_mult_legacy(
280        &self,
281        x_result_handle: Self::BigIntHandle,
282        y_result_handle: Self::BigIntHandle,
283        ec_handle: Self::EllipticCurveHandle,
284        data: &[u8],
285    ) {
286        unsafe {
287            scalarBaseMultEC(
288                x_result_handle,
289                y_result_handle,
290                ec_handle,
291                data.as_ptr(),
292                data.len() as i32,
293            );
294        }
295    }
296
297    fn ec_scalar_base_mult(
298        &self,
299        x_result_handle: Self::BigIntHandle,
300        y_result_handle: Self::BigIntHandle,
301        ec_handle: Self::EllipticCurveHandle,
302        data_handle: Self::ManagedBufferHandle,
303    ) {
304        unsafe {
305            managedScalarBaseMultEC(x_result_handle, y_result_handle, ec_handle, data_handle);
306        }
307    }
308
309    fn ec_marshal_legacy(
310        &self,
311        ec_handle: Self::EllipticCurveHandle,
312        x_pair_handle: Self::BigIntHandle,
313        y_pair_handle: Self::BigIntHandle,
314    ) -> BoxedBytes {
315        unsafe {
316            let byte_length = (getCurveLengthEC(ec_handle) + 7) / 8;
317            let mut result = BoxedBytes::zeros(1 + 2 * byte_length as usize);
318            marshalEC(x_pair_handle, y_pair_handle, ec_handle, result.as_mut_ptr());
319            result
320        }
321    }
322
323    fn ec_marshal(
324        &self,
325        ec_handle: Self::EllipticCurveHandle,
326        x_pair_handle: Self::BigIntHandle,
327        y_pair_handle: Self::BigIntHandle,
328        result_handle: Self::ManagedBufferHandle,
329    ) {
330        unsafe {
331            managedMarshalEC(x_pair_handle, y_pair_handle, ec_handle, result_handle);
332        }
333    }
334
335    fn ec_marshal_compressed_legacy(
336        &self,
337        ec_handle: Self::EllipticCurveHandle,
338        x_pair_handle: Self::BigIntHandle,
339        y_pair_handle: Self::BigIntHandle,
340    ) -> BoxedBytes {
341        unsafe {
342            let byte_length = (getCurveLengthEC(ec_handle) + 7) / 8;
343            let mut result = BoxedBytes::zeros(1 + byte_length as usize);
344            marshalCompressedEC(x_pair_handle, y_pair_handle, ec_handle, result.as_mut_ptr());
345            result
346        }
347    }
348
349    fn ec_marshal_compressed(
350        &self,
351        ec_handle: Self::EllipticCurveHandle,
352        x_pair_handle: Self::BigIntHandle,
353        y_pair_handle: Self::BigIntHandle,
354        result_handle: Self::ManagedBufferHandle,
355    ) {
356        unsafe {
357            managedMarshalCompressedEC(x_pair_handle, y_pair_handle, ec_handle, result_handle);
358        }
359    }
360
361    fn ec_unmarshal_legacy(
362        &self,
363        x_result_handle: Self::BigIntHandle,
364        y_result_handle: Self::BigIntHandle,
365        ec_handle: Self::EllipticCurveHandle,
366        data: &[u8],
367    ) {
368        unsafe {
369            unmarshalEC(
370                x_result_handle,
371                y_result_handle,
372                ec_handle,
373                data.as_ptr(),
374                data.len() as i32,
375            );
376        }
377    }
378
379    fn ec_unmarshal(
380        &self,
381        x_result_handle: Self::BigIntHandle,
382        y_result_handle: Self::BigIntHandle,
383        ec_handle: Self::EllipticCurveHandle,
384        data_handle: Self::ManagedBufferHandle,
385    ) {
386        unsafe {
387            managedUnmarshalEC(x_result_handle, y_result_handle, ec_handle, data_handle);
388        }
389    }
390
391    fn ec_unmarshal_compressed_legacy(
392        &self,
393        x_result_handle: Self::BigIntHandle,
394        y_result_handle: Self::BigIntHandle,
395        ec_handle: Self::EllipticCurveHandle,
396        data: &[u8],
397    ) {
398        unsafe {
399            unmarshalCompressedEC(
400                x_result_handle,
401                y_result_handle,
402                ec_handle,
403                data.as_ptr(),
404                data.len() as i32,
405            );
406        }
407    }
408
409    fn ec_unmarshal_compressed(
410        &self,
411        x_result_handle: Self::BigIntHandle,
412        y_result_handle: Self::BigIntHandle,
413        ec_handle: Self::EllipticCurveHandle,
414        data_handle: Self::ManagedBufferHandle,
415    ) {
416        unsafe {
417            managedUnmarshalCompressedEC(x_result_handle, y_result_handle, ec_handle, data_handle);
418        }
419    }
420
421    fn ec_generate_key_legacy(
422        &self,
423        x_pub_key_handle: Self::BigIntHandle,
424        y_pub_key_handle: Self::BigIntHandle,
425        ec_handle: Self::EllipticCurveHandle,
426    ) -> BoxedBytes {
427        unsafe {
428            let priv_key_length = getPrivKeyByteLengthEC(ec_handle);
429            let mut private_key = BoxedBytes::zeros(priv_key_length as usize);
430            generateKeyEC(
431                x_pub_key_handle,
432                y_pub_key_handle,
433                ec_handle,
434                private_key.as_mut_ptr(),
435            );
436            private_key
437        }
438    }
439
440    fn ec_generate_key(
441        &self,
442        x_pub_key_handle: Self::BigIntHandle,
443        y_pub_key_handle: Self::BigIntHandle,
444        ec_handle: Self::EllipticCurveHandle,
445        result_handle: Self::ManagedBufferHandle,
446    ) {
447        unsafe {
448            managedGenerateKeyEC(x_pub_key_handle, y_pub_key_handle, ec_handle, result_handle);
449        }
450    }
451}