-
Notifications
You must be signed in to change notification settings - Fork 2
/
Test_CMPcircuitFHEcomponent.cpp
203 lines (156 loc) · 4.67 KB
/
Test_CMPcircuitFHEcomponent.cpp
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
#include <HElib/FHE.h>
#include <HElib/timing.h>
#include <HElib/EncryptedArray.h>
#include <NTL/lzz_pXFactoring.h>
#include <fstream>
#include <sstream>
#include <sys/time.h>
#include <bitset>
#include "CMPcircuit.h"
int main(int argc, char **argv)
{
/* On our trusted system we generate a new key
* (or read one in) and encrypt the secret data set.
*/
long m=0, p=2, r=1; // Native plaintext space
// Computations will be 'modulo p'
long L=16; // Levels
long c=3; // Columns in key switching matrix
long w=64; // Hamming weight of secret key
long d=0;
long security = 128;
ZZX G;
m = FindM(security,L,c,p, d, 0, 0);
FHEcontext context(m, p, r);
// initialize context
buildModChain(context, L, c);
// modify the context, adding primes to the modulus chain
FHESecKey secretKey(context);
// construct a secret key structure
const FHEPubKey& publicKey = secretKey;
// an "upcast": FHESecKey is a subclass of FHEPubKey
//if(0 == d)
G = context.alMod.getFactorsOverZZ()[0];
secretKey.GenSecKey(w);
// actually generate a secret key with Hamming weight w
addSome1DMatrices(secretKey);
cout << "Generated key..." << endl;
EncryptedArray ea(context, G);
// constuct an Encrypted array object ea that is
// associated with the given context and the polynomial G
long nslots = ea.size();
cout << "Vector Size " << nslots << endl;;
//const long ctxtsize = nslots;
long x,y;
bool compare;
cout << "Enter the first long integer: ";
cin >> x;
cout << "Enter the second long integer: ";
cin >> y;
//create bitset from long integers
bitset<BIT_SIZE> bs1(x);
bitset<BIT_SIZE> bs2(y);
compare = CMPcircuit(bs1, bs2);
vector<long> v1;
//int i = 0;
int j = 1;
for (int l = 0; l< nslots ; l++){
if (l<(nslots-BIT_SIZE)){
v1.push_back(0);
}
else{
v1.push_back(long (bs1[BIT_SIZE-j]));
j++;
}
cout << v1[l];
}
cout <<endl;
vector<long> v2;
int k = 1;
for(int i = 0 ; i < nslots ; i++) {
if (i<(nslots-BIT_SIZE)){
v2.push_back(0);
}
else{
v2.push_back(long (bs2[BIT_SIZE-k]));
k++;
}
cout << v2[i];
}
cout <<endl;
vector<long> zerovector(nslots,0);
vector<long> zerovector1(nslots-1,0);
vector<Ctxt> Ctxtset1;
vector<Ctxt> Ctxtset2;
startFHEtimer("ea.encrypt1");
cout << "Bit size " << BIT_SIZE <<endl;
unsigned int i=0;
while(i<BIT_SIZE){
long tmp1 = v1.back();
v1.pop_back();
zerovector1.push_back(tmp1);
Ctxt tmpCtxt1(publicKey);
//startFHEtimer("ea.encrypt1");
ea.encrypt(tmpCtxt1, publicKey, zerovector1);
//stopFHEtimer("ea.encrypt1");
Ctxtset1.push_back(tmpCtxt1);
i++;
zerovector1.pop_back();
}
cout <<" Size of zerovector1 " << zerovector1.size() <<endl;
cout <<" Size of Ctxtset1 " << Ctxtset1.size() <<endl;
stopFHEtimer("ea.encrypt1");
startFHEtimer("ea.encrypt2");
i=0;
while(i<BIT_SIZE){
long tmp2 = v2.back();
v2.pop_back();
zerovector1.push_back(tmp2);
Ctxt tmpCtxt2(publicKey);
//startFHEtimer("ea.encrypt1");
ea.encrypt(tmpCtxt2, publicKey, zerovector1);
//stopFHEtimer("ea.encrypt1");
Ctxtset2.push_back(tmpCtxt2);
i++;
zerovector1.pop_back();
}
cout <<" Size of zerovector1 " << zerovector1.size() <<endl;
cout <<" Size of Ctxtset2 " << Ctxtset2.size() <<endl;
stopFHEtimer("ea.encrypt2");
Ctxt tmpCtxt3(publicKey);
startFHEtimer("ea.encrypt3");
ea.encrypt(tmpCtxt3, publicKey, zerovector);
stopFHEtimer("ea.encrypt3");
// On the public (untrusted) system we
// can now perform our computation
Ctxt ctCMP = Ctxtset1[0];
Ctxt ctCarry = tmpCtxt3;
//Ctxt ctProd = ct1;
startFHEtimer("comparator");
i = 0;
while(i < Ctxtset1.size()){
cout << "flag";
ctCMP = Ctxtset1[i];
ctCMP.CMPcircuit(Ctxtset2[i],ctCarry);
cout << "Test" << endl;
ctCarry = ctCMP;
i++;
}
stopFHEtimer("comparator");
vector<long> res;
startFHEtimer("decryptsum");
ea.decrypt(ctCarry, secretKey, res);
stopFHEtimer("decryptsum");
cout <<res.size() <<endl;
i = 0;
while (i<res.size()){
cout<< res[i];
i++;
}
cout <<endl;
cout << long(compare) << " = " << res[res.size()-1];
printAllTimers();
cout << endl;
cout << "All computations are modulo " << p << "." << endl;
return 0;
}