-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
346 lines (306 loc) · 9.55 KB
/
functions.h
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#ifndef COMMON_FUNC_H
#define COMMON_FUNC_H
#include <NTL/ZZ.h>
#include "/home/white_wolf/Desktop/Thesis/HElib-master/src/FHE.h"
#include <time.h>
#include "/home/white_wolf/Desktop/Thesis/HElib-master/src/EncryptedArray.h"
#include "/home/white_wolf/Desktop/Thesis/HElib-master/src/PAlgebra.h"
#include <NTL/lzz_pXFactoring.h>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <NTL/vec_ZZ.h>
#include <NTL/ZZ_pX.h>
#include "/home/white_wolf/Desktop/Thesis/HElib-master/src/powerful.h"
#include <math.h>
#include <algorithm>
#include <vector>
inline Ctxt addMemo(Ctxt& B,Ctxt& A,EncryptedArray& ea,long nslots);
inline void OR(Ctxt& B,Ctxt& A,Ctxt cOne);
inline Ctxt addOrdiVector(vector<Ctxt>& vArray,EncryptedArray& ea,Ctxt cOne,long nslots);
inline Ctxt mulOrdiVector(vector<Ctxt>& vArray,EncryptedArray& ea,long nslots);
inline Ctxt addMemoVector(vector<Ctxt>& vArray,EncryptedArray& ea,long nslots);
inline Ctxt isEqual(Ctxt B,Ctxt A,Ctxt cOne,EncryptedArray ea,long nslots);
inline Ctxt isEqual2(Ctxt B,Ctxt A,Ctxt cOne,EncryptedArray ea,long nslots);
inline Ctxt isEqual3(Ctxt &B,Ctxt& A,Ctxt& cOne,EncryptedArray& ea,long nslots);
inline Ctxt compare2(Ctxt& B,Ctxt& A,EncryptedArray &ea,Ctxt &cZeroOne,Ctxt &cOneZero,Ctxt &cOne,long nslots);
inline void NOT(Ctxt& B,Ctxt &cOne);
inline Ctxt compare(Ctxt &B,Ctxt &A,EncryptedArray &ea,Ctxt &cZeroOne,Ctxt &cOneZero,Ctxt &cOne,long nslots);
vector<long> convertToCoeArray(long val,long nslots);
void printV(vector<long> &vLong,long nslots);
long convertToL(vector<long> &vLong,long d);
long findL(int choice,long numVal,long elementBitLength);
//Equality recursive function at page 4
inline Ctxt z(int i,int j, vector<Ctxt> x,vector<Ctxt> y, Ctxt enc1){
if (j==1){
Ctxt g=x[i];
g+=y[i];
g+=enc1;
cout<<"here"<<endl;
return g;
}
else {
int l=ceil(j/2);
Ctxt f= z(i+l,j-l,x,y, enc1);
f*=z(i,l,x,y,enc1);
cout << "Done Compare" << endl;
return f;
}
}
//Inequality recursive function at page 4
inline Ctxt t(int i,int j, vector<Ctxt> x,vector<Ctxt> y, Ctxt enc1){
if (j==1){
Ctxt H=x[i];
H*=y[i];
H+=x[i];
cout<<"here"<<endl;
return H;
}
else {
int l1=ceil(j/2);
Ctxt we=z(i+l1,j-l1,x,y,enc1);
we*=t(i,l1,x,y,enc1);
we+=t(i+l1,j-l1,x,y,enc1);
cout << "Done Compare" << endl;
return we;
}
}
inline Ctxt addMemo(Ctxt& B,Ctxt& A,EncryptedArray& ea,long nslots){
Ctxt cTerm(A.getPubKey());
//cTerm = A;
long iter;
for(iter = 0 ; iter < nslots ; iter++){
cTerm = A;
B.addCtxt(A); // Addition in B
cTerm.multiplyBy(B); // carry in C
A.addCtxt(cTerm);
ea.shift(A,-1);
}
return B;
}
inline void OR(Ctxt& B,Ctxt& A,Ctxt cOne){
Ctxt cTerm(A.getPubKey());
cTerm = A;
cTerm.multiplyBy(B);
B.addCtxt(A);
cTerm.multiplyBy(cOne);
B.addCtxt(cTerm);
}
inline Ctxt addOrdiVector(vector<Ctxt>& vArray,EncryptedArray& ea,long nslots){
Ctxt result(vArray[0].getPubKey());
if(vArray.size() == 1){
return vArray[0];
}else{
long iter;
long size = vArray.size();
long mid = size/2;
vector<Ctxt> leftArray;
vector<Ctxt> rightArray;
for(iter = 0 ; iter < mid ; iter++){
leftArray.push_back(vArray[iter]);
}
for(iter = mid ; iter < size ; iter++){
rightArray.push_back(vArray[iter]);
}
Ctxt resultLeft = addOrdiVector(leftArray,ea,nslots);
Ctxt resultRight = addOrdiVector(rightArray,ea,nslots);
result = resultRight;
result.addCtxt(resultLeft);
return result;
}
}
inline Ctxt mulOrdiVector(vector<Ctxt>& vArray,EncryptedArray& ea,long nslots){
Ctxt result(vArray[0].getPubKey());
if(vArray.size() == 1){
return vArray[0];
}else{
long iter;
long size = vArray.size();
long mid = size/2;
vector<Ctxt> leftArray;
vector<Ctxt> rightArray;
for(iter = 0 ; iter < mid ; iter++){
leftArray.push_back(vArray[iter]);
}
for(iter = mid ; iter < size ; iter++){
rightArray.push_back(vArray[iter]);
}
Ctxt resultLeft = mulOrdiVector(leftArray,ea,nslots);
Ctxt resultRight = mulOrdiVector(rightArray,ea,nslots);
result = resultLeft;
result.multiplyBy(resultRight);
return result;
}
}
inline Ctxt orVector(vector<Ctxt>& vArray,EncryptedArray& ea,Ctxt& cOne,long nslots){
Ctxt result(vArray[0].getPubKey());
if(vArray.size() == 1){
return vArray[0];
}else{
long iter;
long size = vArray.size();
long mid = size/2;
vector<Ctxt> leftArray;
vector<Ctxt> rightArray;
for(iter = 0 ; iter < mid ; iter++){
leftArray.push_back(vArray[iter]);
}
for(iter = mid ; iter < size ; iter++){
rightArray.push_back(vArray[iter]);
}
Ctxt resultLeft = orVector(leftArray,ea,cOne,nslots);
Ctxt resultRight = orVector(rightArray,ea,cOne,nslots);
result = resultLeft;
OR(result,resultRight,cOne);
return result;
}
}
inline Ctxt addMemoVector(vector<Ctxt>& vArray,EncryptedArray& ea,long nslots){
if(vArray.size() == 2){
return addMemo(vArray[0],vArray[1],ea,nslots);
}else if(vArray.size() == 1){
return vArray[0];
}else{
long iter;
long size = vArray.size();
long mid = size/2;
vector<Ctxt> leftArray;
vector<Ctxt> rightArray;
for(iter = 0 ; iter < mid ; iter++){
leftArray.push_back(vArray[iter]);
}
for(iter = mid ; iter < size ; iter++){
rightArray.push_back(vArray[iter]);
}
Ctxt resultLeft = addMemoVector(leftArray,ea,nslots);
Ctxt resultRight = addMemoVector(rightArray,ea,nslots);
return addMemo(resultLeft,resultRight,ea,nslots);
}
}
inline Ctxt isEqual2(Ctxt B,Ctxt A,Ctxt cOne,EncryptedArray ea,long nslots){
Ctxt result(A.getPubKey());
result = A;
result.addCtxt(cOne);
result.addCtxt(B);
// A.multiplyBy(B);
// B.addCtxt(cOne);
// result.multiplyBy(B);
// result.addCtxt(A);
vector<Ctxt> shiftArray;
shiftArray.push_back(result);
long iter;
for(iter = 0 ; iter < nslots - 1 ; iter++){
Ctxt nC(result.getPubKey());
ea.rotate(result,1);
nC = result;
shiftArray.push_back(nC);
}
Ctxt resultReturn = mulOrdiVector(shiftArray,ea,nslots);
return resultReturn;
}
inline void NOT(Ctxt& B,Ctxt &cOne){
B.addCtxt(cOne);
}
inline Ctxt compare2(Ctxt& B,Ctxt& A,EncryptedArray &ea,Ctxt &cZeroOne,Ctxt &cOneZero,Ctxt &cOne,long nslots){
Ctxt cAB(A.getPubKey());
cAB = A;
cAB.multiplyBy(B);
Ctxt cX(A.getPubKey());
cX = A;
NOT(cX,cOne);
Ctxt nB(B.getPubKey());
nB = B;
NOT(nB,cOne);
cX.multiplyBy(nB);
cX.addCtxt(cAB);
Ctxt anB(A.getPubKey());
anB = A;
anB.multiplyBy(nB);
//return anB;
//result = cOneZero;
int iter;
//return cX;
vector<Ctxt> cXShiftladder;
cXShiftladder.push_back(anB);
ea.shift(cX,1);
cX.addCtxt(cZeroOne);
cXShiftladder.push_back(cX);
for(iter = 1 ; iter < nslots - 1; iter++){
Ctxt nC(cX.getPubKey());
nC = cXShiftladder[cXShiftladder.size() - 1];
ea.shift(nC,1);
nC.addCtxt(cZeroOne);
cXShiftladder.push_back(nC);
}
Ctxt result(A.getPubKey());
result = mulOrdiVector(cXShiftladder,ea,nslots);
vector<Ctxt> orArray;
orArray.push_back(result);
for(iter = 0 ; iter < nslots - 1 ; iter++){
Ctxt nC(orArray[0].getPubKey());
nC = orArray[orArray.size() - 1];
ea.shift(nC,1);
orArray.push_back(nC);
}
Ctxt returnResult(orArray[0].getPubKey());
returnResult = orVector(orArray,ea,cOne,nslots);
cout << "Done Compare" << endl;
returnResult.multiplyBy(cOneZero);
vector<Ctxt> shiftArray;
shiftArray.push_back(returnResult);
for(iter = 0 ; iter < nslots - 1 ; iter++){
Ctxt nC(returnResult.getPubKey());
ea.rotate(returnResult,1);
nC = returnResult;
shiftArray.push_back(nC);
}
Ctxt resultReturn = addOrdiVector(shiftArray,ea,nslots);
return resultReturn;
}
vector<long> convertToCoeArray(long val,long nslots){
vector<long> result(nslots,0);
long quotient;
int i = nslots - 1;
if(val == 0){
return result;
}
while(val > 0){
quotient = val%2;
result[i] = quotient;
val = (val - quotient)/2;
i--;
}
return result;
}
void printV(vector<long> &vLong,long nslots){
int iter;
cout << "[";
for(iter = 0 ; iter < nslots ; iter++){
cout << vLong[iter] << " ";
}
cout << "]" << endl;
}
long convertToL(vector<long> &vLong,long d){
int iter;
long result = 0;
long x = 1;
for(iter = d-1 ;iter >= 0 ; iter--){
result = result + vLong[iter]*x;
x = x*2;
}
return result;
}
/* Find L depends on the size of input array,calculated by Pham
* Tran Tuan*/
long findL(int choice,long numVal,long elementBitLength){
long result;
if(choice == 0){
long c = 4;
result = 8 + 3*elementBitLength + (2*elementBitLength + 1)*log2(numVal)*c + log2(elementBitLength);
}else{
long c = 1;
result = c*numVal*(log2(numVal))*(log2(numVal))*3*log2(elementBitLength);
}
return result;
}
#endif