-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFHE-SI.cpp
289 lines (226 loc) · 7.14 KB
/
FHE-SI.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
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
#include "DoubleCRT.h"
#include "FHE-SI.h"
#include <vector>
#include <NTL/vec_ZZ.h>
#include "Plaintext.h"
#include "Util.h"
#include "Ciphertext.h"
#include "Serialization.h"
void FHESIPubKey::Encrypt(Ciphertext &ctxt_, const Plaintext &ptxt) const {
Ciphertext &ctxt = (Ciphertext &)ctxt_;
ctxt.Initialize(2, context);
ZZX small;
for (unsigned i = 0; i < context.zMstar.phiM(); i++) {
SetCoeff(small, i, RandomBnd(2));
}
DoubleCRT r(small, context);
DoubleCRT e(context);
vector<DoubleCRT> ciphertext = publicKey;
for (size_t i = 0; i < ciphertext.size(); i++) {
e.sampleGaussian();
e *= context.ModulusP();
ciphertext[i] *= r;
ciphertext[i] += e;
ciphertext[i].toPoly(ctxt[i].poly);
}
ctxt[0] += (context.modulusQ / context.ModulusP()) * to_ZZX(ptxt.message);
for (size_t i = 0; i < ciphertext.size(); i++) {
ReduceCoefficients(ctxt[i].poly, context.logQ);
}
}
const FHEcontext &FHESIPubKey::GetContext() const {
return context;
}
void FHESIPubKey::Init(const FHESISecKey &secKey_) {
const FHESISecKey &secKey = (const FHESISecKey &)secKey_;
ZZX c0, c1;
sampleGaussian(c0, context.zMstar.phiM(), context.stdev);
SampleRandom(c1, context.modulusQ, context.zMstar.phiM());
ZZX tmp;
secKey.GetRepresentation()[1].toPoly(tmp);
tmp *= c1;
c0 += tmp;
rem(c0, c0, context.zMstar.PhimX());
c1 *= -1;
ReduceCoefficients(c0, context.logQ);
ReduceCoefficients(c1, context.logQ);
publicKey.clear();
publicKey.push_back(DoubleCRT(c0));
publicKey.push_back(DoubleCRT(c1));
}
const vector<DoubleCRT> &FHESIPubKey::GetRepresentation() const {
return publicKey;
}
void FHESIPubKey::UpdateRepresentation(const vector<DoubleCRT> &rep) {
publicKey = rep;
}
void FHESIPubKey::Export(ofstream &out) const {
::Export(out, publicKey);
}
void FHESIPubKey::Import(ifstream &in) {
::Import(in, publicKey);
}
ostream &operator<<(ostream &os, const FHESIPubKey &pubKey_) {
const FHESIPubKey &pubKey = (const FHESIPubKey &)pubKey_;
return (os << pubKey.publicKey[0] << ", " <<
pubKey.publicKey[1]);
}
void FHESISecKey::Init(const FHEcontext &context) {
sKeys.assign(2, DoubleCRT(context));
sKeys[0] = 1;
sKeys[1].sampleHWt(64);
}
void FHESISecKey::Decrypt(Plaintext &ptxt, const Ciphertext &ctxt_) const {
const Ciphertext &ctxt = (const Ciphertext &)ctxt_;
vector<DoubleCRT> ctxtPolys, sKeyPolys;
for (size_t i = 0; i < sKeys.size(); i++) {
ctxtPolys.push_back(DoubleCRT(ctxt.GetPart(i).poly));
sKeyPolys.push_back(DoubleCRT(sKeys[i]));
}
DoubleCRT tmp;
DotProduct(tmp, ctxtPolys, sKeyPolys);
ZZX zPtxt;
tmp.toPoly(zPtxt);
ZZ modulusP;
modulusP = context.ModulusP();
ZZ q = context.modulusQ;
ZZ q2 = 2*q;
for(long i = 0; i <= deg(zPtxt); i++) {
zPtxt.rep[i] *= 2*modulusP;
zPtxt.rep[i] += q;
zPtxt.rep[i] /= q2;
SetCoeff(ptxt.message, i, to_ZZ_p(zPtxt.rep[i]));
}
}
const vector<DoubleCRT> &FHESISecKey::GetRepresentation() const {
return sKeys;
}
const FHEcontext &FHESISecKey::GetContext() const {
return context;
}
size_t FHESISecKey::GetSize() const {
return sKeys.size();
}
void FHESISecKey::UpdateRepresentation(vector<DoubleCRT> &rep) {
sKeys = rep;
}
void FHESISecKey::Export(ofstream &out) const {
::Export(out, sKeys);
}
void FHESISecKey::Import(ifstream &in) {
::Import(in, sKeys);
}
ostream &operator<<(ostream &os, const FHESISecKey &secKey_) {
const FHESISecKey &secKey = (const FHESISecKey &)secKey_;
for (auto it = secKey.sKeys.begin(); it != secKey.sKeys.end(); it++) {
os << *it;
}
return os;
}
void KeySwitchSI::Init(const FHESISecKey &src_, const FHESISecKey &dst_) {
const FHESISecKey &src = (const FHESISecKey &)src_;
const FHESISecKey &dst = (const FHESISecKey &)dst_;
#ifdef DEBUG
assert(&src.GetContext() == &dst.GetContext());
assert(dst.GetSize() == 2); // Target keys should be in canonical form: (1,t)
#endif
vector<DoubleCRT> s = src.GetRepresentation();
vector<ZZX> sCoeff(s.size());
for (size_t i = 0; i < sCoeff.size(); i++) {
s[i].toPoly(sCoeff[i]);
}
DoubleCRT t = dst.GetRepresentation()[1];
size_t n = src.GetSize();
vector<DoubleCRT> A(context.ndigits*n);
vector<DoubleCRT> b(context.ndigits*n);
vector<ZZX> errors(context.ndigits*n);
unsigned ind = 0;
for (unsigned i = 0; i < n; i++) {
for (unsigned j = 0; j < context.ndigits; j++, ind++) {
ZZX poly;
SampleRandom(poly, context.modulusQ, context.zMstar.phiM());
A[ind] = DoubleCRT(poly, context);
b[ind] = A[ind];
A[ind] *= -1;
b[ind] *= t;
ZZX bCoeff;
b[ind].toPoly(bCoeff);
ZZX err;
sampleGaussian(err, context.zMstar.phiM(), context.stdev);
bCoeff += err;
errors[ind] = err;
bCoeff += sCoeff[i];
for (long k = 0; k <= deg(sCoeff[i]); k++) {
sCoeff[i].rep[k] <<= (8*context.decompSize);
}
ReduceCoefficients(bCoeff, context.logQ);
b[ind] = DoubleCRT(bCoeff);
}
}
keySwitchMatrix.resize(2);
keySwitchMatrix[0] = b;
keySwitchMatrix[1] = A;
}
void KeySwitchSI::InitS2(const FHESISecKey &s_) {
const FHESISecKey &s = (const FHESISecKey &)s_;
vector<DoubleCRT> sKeys = s.GetRepresentation();
vector<DoubleCRT> tKeys;
tKeys.assign(sKeys.size()*2-1, sKeys[1]);
tKeys[0] = sKeys[0];
for (unsigned i = 2; i < tKeys.size(); i++) {
tKeys[i] *= tKeys[i-1];
}
FHESISecKey tensoredKey = FHESISecKey(s.GetContext());
tensoredKey.UpdateRepresentation(tKeys);
Init(tensoredKey, s);
}
void KeySwitchSI::InitAutomorph(const FHESISecKey &s_, unsigned k) {
const FHESISecKey &s = (const FHESISecKey &)s_;
vector<DoubleCRT> sKeys = s.GetRepresentation();
FHESISecKey automorphedKey(s.GetContext());
for (unsigned i = 0; i < sKeys.size(); i++) {
sKeys[i].automorph(k);
}
automorphedKey.UpdateRepresentation(sKeys);
Init(automorphedKey, s);
}
void KeySwitchSI::ApplyKeySwitch(Ciphertext &ctxt_) const {
Ciphertext &ctxt = (Ciphertext &)ctxt_;
ctxt.ScaleDown();
ctxt.ByteDecomp();
vector<DoubleCRT> byteDecomp(ctxt.parts.size());
for (size_t i = 0; i < byteDecomp.size(); i++) {
byteDecomp[i] = DoubleCRT(ctxt.parts[i].poly);
}
vector<CiphertextPart> newCtxt(keySwitchMatrix.size());
for (size_t i = 0; i < keySwitchMatrix.size(); i++) {
DoubleCRT dotProd;
DotProduct(dotProd, keySwitchMatrix[i], byteDecomp);
dotProd.toPoly(newCtxt[i].poly);
ReduceCoefficients(newCtxt[i].poly, context.logQ);
}
ctxt.parts = newCtxt;
}
const vector<vector<DoubleCRT>> &KeySwitchSI::GetRepresentation() const {
return keySwitchMatrix;
}
void KeySwitchSI::UpdateRepresentation(const vector<vector<DoubleCRT>> &rep) {
keySwitchMatrix = rep;
}
void KeySwitchSI::Export(ofstream &out) const {
::Export(out, keySwitchMatrix);
}
void KeySwitchSI::Import(ifstream &in) {
::Import(in, keySwitchMatrix);
}
KeySwitchSI &KeySwitchSI::operator=(const KeySwitchSI &other) {
if (&context != &other.context) {
Error("KeySwitchSI assignment: context mismatch");
}
keySwitchMatrix = other.keySwitchMatrix;
return *this;
}
ostream &operator<<(ostream &os, const KeySwitchSI &keySwitch) {
PrintVector(keySwitch.keySwitchMatrix, os);
return os;
}