-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudf_paillier.c
69 lines (50 loc) · 2.08 KB
/
udf_paillier.c
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
#include <mysql.h>
#include <string.h>
#include <stdio.h>
#include <gmp.h>
#define ORDER -1
#define DeclareAndInit(n) mpz_t n; mpz_init(n)
#include<libhcs.h>
char* PaillierAddition(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
my_bool PaillierAddition_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void PaillierAddition_deinit(UDF_INIT *initid);
char* PaillierAddition(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error)
{
long long int A_int = *((long long int*) args->args[0]);
long long int B_int = *((long long int*) args->args[1]);
DeclareAndInit(A); // mpz_t declaration
DeclareAndInit(B);
DeclareAndInit(C);
pcs_public_key *pubKey = pcs_init_public_key(); // init public key
pcs_private_key *privKey = pcs_init_private_key(); // init private key
hcs_random *hcsRand = hcs_init_random(); // generate random big int
pcs_generate_key_pair(pubKey, privKey, hcsRand, 2048); // generate encryption keypair
mpz_set_si(A, A_int); // assign cast integer to big integer
mpz_set_si(B,B_int);
pcs_encrypt(pubKey, hcsRand, A, A); // encrypt number
pcs_encrypt(pubKey, hcsRand, B, B);
pcs_ee_add(pubKey, C, A, B); // C = E(AxB) ~ a+b
result = mpz_get_str(NULL, 10, C); // result stored as string
mpz_clear(A); // clear dynamic memory
mpz_clear(B);
mpz_clear(C);
pcs_free_public_key(pubKey); // clear dynamic memory
pcs_free_private_key(privKey);
hcs_free_random(hcsRand);
return result;
}
my_bool PaillierAddition_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
initid->maybe_null = 0;
if (args->arg_count != 2 ||
args->arg_type[0] != INT_RESULT ||
args->arg_type[1] != INT_RESULT)
{
strcpy(message, "PaillierAddition requires 2 arguments of type [INT]");
return 1;
}
return 0;
}
void PaillierAddition_deinit(UDF_INIT *initid)
{
}