1+ // Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+ //! Library of macros and functions for FFI of a voting coordinator in ABC
4+ //! solution, targeting C/C++ compatible architectures (including iOS).
5+
6+ // C/C++ FFI: C-style interfaces will be generated.
7+
8+ use wedpr_s_protos:: generated:: abv:: { CandidateList , CounterSystemParametersStorage , RegistrationRequest , DecryptedResultPartStorage
9+ } ;
10+
11+ use libc:: c_char;
12+ use protobuf:: { self , Message } ;
13+ use std:: { ffi:: CString , panic, ptr} ;
14+ use wedpr_ffi_common:: utils:: {
15+ bytes_to_string, c_char_pointer_to_string, string_to_bytes,
16+ } ;
17+ use std:: os:: raw:: { c_int, c_long, c_uint} ;
18+
19+
20+ /// C interface for 'wedpr_abv_make_system_parameters'.
21+ #[ no_mangle]
22+ pub extern "C" fn wedpr_abv_make_system_parameters (
23+ candidates_cstring : * mut c_char ,
24+ counter_storage_cstring : * mut c_char ,
25+ ) -> * mut c_char {
26+ let result = panic:: catch_unwind ( || {
27+ let candidates =
28+ c_safe_c_char_pointer_to_proto ! ( candidates_cstring, CandidateList ) ;
29+
30+ let counter_storage =
31+ c_safe_c_char_pointer_to_proto ! ( counter_storage_cstring, CounterSystemParametersStorage ) ;
32+
33+ let system_parameters_storage =
34+ match wedpr_s_anonymous_bounded_voting:: coordinator:: make_system_parameters (
35+ & candidates,
36+ & counter_storage,
37+ ) {
38+ Ok ( v) => v,
39+ Err ( _) => return ptr:: null_mut ( ) ,
40+ } ;
41+ c_safe_proto_to_c_char_pointer ! ( system_parameters_storage)
42+ } ) ;
43+ c_safe_return ! ( result)
44+ }
45+
46+ /// C interface for 'wedpr_abv_certify_bounded_voter'.
47+ #[ no_mangle]
48+ pub extern "C" fn wedpr_abv_certify_bounded_voter (
49+ secret_key_cstring : * mut c_char ,
50+ blank_vote_value : c_uint ,
51+ registration_request_cstring : * mut c_char ,
52+ ) -> * mut c_char {
53+ let result = panic:: catch_unwind ( || {
54+ let registration_request =
55+ c_safe_c_char_pointer_to_proto ! ( registration_request_cstring, RegistrationRequest ) ;
56+ let secret_key =
57+ c_safe_c_char_pointer_to_bytes ! ( secret_key_cstring) ;
58+ let value = blank_vote_value as u32 ;
59+
60+ let response =
61+ match wedpr_s_anonymous_bounded_voting:: coordinator:: certify_bounded_voter (
62+ & secret_key,
63+ value,
64+ & registration_request,
65+ ) {
66+ Ok ( v) => v,
67+ Err ( _) => return ptr:: null_mut ( ) ,
68+ } ;
69+ c_safe_proto_to_c_char_pointer ! ( response)
70+ } ) ;
71+ c_safe_return ! ( result)
72+ }
73+
74+ /// C interface for 'wedpr_abv_aggregate_decrypted_part_sum'.
75+ #[ no_mangle]
76+ pub extern "C" fn wedpr_abv_aggregate_decrypted_part_sum (
77+ param_cstring : * mut c_char ,
78+ decrypted_result_part_storage_cstring : * mut c_char ,
79+ counting_result_sum_cstring : * mut c_char ,
80+ ) -> * mut c_char {
81+ let result = panic:: catch_unwind ( || {
82+ let param =
83+ c_safe_c_char_pointer_to_proto ! ( param_cstring, SystemParametersStorage ) ;
84+
85+ let decrypted_result_part_storage =
86+ c_safe_c_char_pointer_to_proto ! ( decrypted_result_part_storage_cstring, DecryptedResultPartStorage ) ;
87+
88+ let mut counting_result_sum =
89+ c_safe_c_char_pointer_to_proto ! ( counting_result_sum_cstring, DecryptedResultPartStorage ) ;
90+
91+
92+ let b_result =
93+ match wedpr_s_anonymous_bounded_voting:: coordinator:: aggregate_decrypted_part_sum (
94+ & param,
95+ & decrypted_result_part_storage,
96+ & mut counting_result_sum,
97+ ) {
98+ Ok ( v) => v,
99+ Err ( _) => return ptr:: null_mut ( ) ,
100+ } ;
101+ if !b_result {
102+ return ptr:: null_mut ( ) ;
103+ }
104+ c_safe_proto_to_c_char_pointer ! ( counting_result_sum)
105+ } ) ;
106+ c_safe_return ! ( result)
107+ }
0 commit comments