-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathup_cloud.c
314 lines (224 loc) · 11.6 KB
/
up_cloud.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include<stdlib.h>
#include <tfhe/tfhe.h>
#include <tfhe/tfhe_io.h>
#include"bitadder.h"
#include<time.h>
#define row_num 10
#define data_size 16
struct ciphertext
{
LweSample* ci_pregnancies;
LweSample* ci_glucose;
LweSample* ci_blood_p;
LweSample* ci_skin_thik;
LweSample* ci_insulin;
LweSample* ci_BMI;
LweSample* ci_diabeties_pedig;
LweSample* ci_age;
LweSample* ci_outcome;
};
struct query
{
LweSample* query1;
LweSample* query2;
};
struct query query;
struct ciphertext ciphertext[row_num];
LweSample* tmps;
LweSample* tmps1;
void subtract(LweSample* result, const LweSample* a, const LweSample* b, const int nb_bits, const TFheGateBootstrappingCloudKeySet* bk) {
//printf("adder funcrion");
//initialize the carry to 0
bootsCONSTANT(&tmps[0], 0, bk);
//run the elementary comparator gate n times
for (int i=0; i<nb_bits; i++)
{
compare_bit(&result[i], &a[i], &b[i], &tmps[0], &tmps[1], bk);
}
}
void subtract1(LweSample* result, const LweSample* a, const LweSample* b, const int nb_bits, const TFheGateBootstrappingCloudKeySet* bk) {
//printf("adder funcrion");
//initialize the carry to 0
bootsCONSTANT(&tmps1[0], 0, bk);
//run the elementary comparator gate n times
for (int i=0; i<nb_bits; i++)
{
compare_bit(&result[i], &a[i], &b[i], &tmps1[0], &tmps1[1], bk);
}
}
void compare(LweSample* output,LweSample* first_input,LweSample* second_input,LweSample* select_line,const int nb_bit, const TFheGateBootstrappingCloudKeySet* bk)
{
int m=0;
for(int i=0;i<nb_bit;i++)
{
bootsMUX(&output[i],&select_line[m],&second_input[i],&first_input[i],bk);
}
}
void Adder(LweSample* top1, const LweSample* a6, const LweSample* b6, const int nb_bits, const TFheGateBootstrappingCloudKeySet* bk)
{
//printf("adder funcrion");
LweSample* tmps6 = new_gate_bootstrapping_ciphertext_array(2, bk->params);
//initialize the carry to 0
bootsCONSTANT(&tmps6[0], 0, bk);
//run the elementary comparator gate n times
for (int i=0; i<nb_bits; i++)
{
Addition(&top1[i], &a6[i], &b6[i], &tmps6[0], &tmps6[1], bk);
}
delete_gate_bootstrapping_ciphertext_array(2, tmps6);
}
int main()
{
//printf("\n Reading the key");
time_t start_time = clock();
FILE* cloud_key = fopen("cloud.key","rb");
TFheGateBootstrappingCloudKeySet* bk = new_tfheGateBootstrappingCloudKeySet_fromFile(cloud_key);
fclose(cloud_key);
//if necessary, the params are inside the key
const TFheGateBootstrappingParameterSet* params = bk->params;
// variable declarartion
tmps = new_gate_bootstrapping_ciphertext_array(2, params);
tmps1 = new_gate_bootstrapping_ciphertext_array(2, params);
query.query1 = new_gate_bootstrapping_ciphertext_array(data_size, params);
query.query2 = new_gate_bootstrapping_ciphertext_array(data_size, params);
for(int i=0;i<row_num;i++)
{
ciphertext[i].ci_pregnancies = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_glucose = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_blood_p = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_skin_thik = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_insulin = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_BMI = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_diabeties_pedig = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_age = new_gate_bootstrapping_ciphertext_array(data_size, params);
ciphertext[i].ci_outcome = new_gate_bootstrapping_ciphertext_array(data_size, params);
}
LweSample* result = new_gate_bootstrapping_ciphertext_array(data_size, params);
LweSample* result1 = new_gate_bootstrapping_ciphertext_array(data_size, params);
LweSample* result2 = new_gate_bootstrapping_ciphertext_array(data_size, params);
LweSample* result3 = new_gate_bootstrapping_ciphertext_array(data_size, params);
LweSample* result4 = new_gate_bootstrapping_ciphertext_array(data_size, params);
LweSample* computation = new_gate_bootstrapping_ciphertext_array(data_size, params);
LweSample* ans= new_gate_bootstrapping_ciphertext_array(1,params);
LweSample* ans1= new_gate_bootstrapping_ciphertext_array(1,params);
LweSample* complement = new_gate_bootstrapping_ciphertext_array(1,params);//complement 0
LweSample* complement1 = new_gate_bootstrapping_ciphertext_array(1,params);//complement 0
LweSample* AC_out = new_gate_bootstrapping_ciphertext_array(1,params);
printf("reading the query...\n");
FILE* query_data = fopen("query.data","rb");
for(int i=0;i<data_size;i++)
{
import_gate_bootstrapping_ciphertext_fromFile(query_data, &query.query1[i], params);
import_gate_bootstrapping_ciphertext_fromFile(query_data, &query.query2[i], params);
}
fclose(query_data);
printf("reading the cloud data...\n");
FILE* cloud_data = fopen("cloud.data","rb");
for(int j=0;j<row_num;j++)
{
for (int i=0; i<data_size; i++)
{
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_pregnancies[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_glucose[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_blood_p[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_skin_thik[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_insulin[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_BMI[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_diabeties_pedig[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_age[i], params);
import_gate_bootstrapping_ciphertext_fromFile(cloud_data, &ciphertext[j].ci_outcome[i], params);
}
}
fclose(cloud_data);
printf("doing the homomorphic computation...\n");
//do some operations on the ciphertexts: here, we will compute the
//addition of two no.
//FILE* secret_key = fopen("secret.key","rb");
//TFheGateBootstrappingSecretKeySet* key = new_tfheGateBootstrappingSecretKeySet_fromFile(secret_key);
for(int j=0;j<row_num;j++)
{ //equality check 2
subtract(result2, query.query2, ciphertext[j].ci_age, data_size, bk);
subtract1(result3,ciphertext[j].ci_age,query.query2,data_size,bk);
bootsOR(ans1,tmps,tmps1,bk);
bootsNOT(complement1,ans1,bk);
//int com;
//int com1;
//com=bootsSymDecrypt(&complement[0],key)>0;
//com1=bootsSymDecrypt(&complement1[0],key)>0;
//printf("\nage compare=%d \t ",com1);
// bitwise AND
// LweSample* E_out= new_gate_bootstrapping_ciphertext_array(1,params);
//bootsAND(E_out,AC_out,complement1,bk);
//int and;
//and=bootsSymDecrypt(&E_out[0],key)>0;
//printf("\tE_out for MUX=%d\n",and);
//Fhe addition
//Adder(computation,ciphertext[j].ci_outcome,query.query3,data_size,bk);
//compare
compare(result4,ciphertext[j].ci_BMI,query.query1,complement1,data_size,bk);
// copy result4 value to ciphertext[j].ci_outcome
int16_t compute=0;
int out1;
int16_t answer=0;
int out;
for(int i=0;i<data_size;i++)
{
bootsCOPY(&ciphertext[j].ci_BMI[i],&result4[i],bk);
//out=bootsSymDecrypt(&ciphertext[j].ci_outcome[i],key)>0;
//answer|=(out<<i);
//out1=bootsSymDecrypt(&computation[i],key)>0;
//compute|=(out1<<i);
}
//printf("\n \n updated_outcome= %d\t Adder_compute=%d\n" , answer,compute);
}
cloud_data=fopen("cloud.data","wb");
for(int j=0;j<row_num;j++){
for(int n=0;n<16;n++)
{
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_pregnancies[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_glucose[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_blood_p[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_skin_thik[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_insulin[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_BMI[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_diabeties_pedig[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_age[n],params);
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext[j].ci_outcome[n],params);
}
}
fclose(cloud_data);
time_t end_time = clock();
printf("\n......computation of the 16 binary + 32 mux gates took: %ld microsecs\n",end_time-start_time);
// fclose(secret_key);
for(int i=0;i<row_num;i++)
{
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_pregnancies);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_glucose);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_blood_p);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_skin_thik);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_insulin);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_BMI);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_diabeties_pedig);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_age);
delete_gate_bootstrapping_ciphertext_array(data_size, ciphertext[i].ci_outcome);
}
delete_gate_bootstrapping_ciphertext_array(1,ans1);
delete_gate_bootstrapping_ciphertext_array(1, complement1);
delete_gate_bootstrapping_ciphertext_array(1,ans);
delete_gate_bootstrapping_ciphertext_array(1, complement);
delete_gate_bootstrapping_ciphertext_array(data_size, query.query1);
delete_gate_bootstrapping_ciphertext_array(data_size, query.query2);
// delete_gate_bootstrapping_ciphertext_array(data_size, query.query3);
delete_gate_bootstrapping_ciphertext_array(data_size, result);
delete_gate_bootstrapping_ciphertext_array(data_size, result1);
delete_gate_bootstrapping_ciphertext_array(data_size, result2);
delete_gate_bootstrapping_ciphertext_array(data_size, result3);
delete_gate_bootstrapping_ciphertext_array(data_size, result4);
delete_gate_bootstrapping_ciphertext_array(data_size, computation);
delete_gate_bootstrapping_ciphertext_array(1, AC_out);
delete_gate_bootstrapping_ciphertext_array(2, tmps1);
delete_gate_bootstrapping_ciphertext_array(2, tmps);
delete_gate_bootstrapping_cloud_keyset(bk);
printf("\n......complete");
}