-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgate.hpp
355 lines (330 loc) · 14.3 KB
/
gate.hpp
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
347
348
349
350
351
352
353
354
355
#pragma once
#include "cloudkey.hpp"
#include "gatebootstrapping.hpp"
#include "keyswitch.hpp"
namespace TFHEpp {
template <class brP, typename brP::targetP::T μ, class iksP, int casign,
int cbsign, std::make_signed_t<typename brP::domainP::T> offset>
inline void HomGate(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
for (int i = 0; i <= brP::domainP::k * brP::domainP::n; i++)
res[i] = casign * ca[i] + cbsign * cb[i];
res[brP::domainP::k * brP::domainP::n] += offset;
GateBootstrapping<brP, μ, iksP>(res, res, ek);
}
template <class iksP, class brP, typename brP::targetP::T μ, int casign,
int cbsign, std::make_signed_t<typename iksP::domainP::T> offset>
inline void HomGate(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
for (int i = 0; i <= iksP::domainP::k * iksP::domainP::n; i++)
res[i] = casign * ca[i] + cbsign * cb[i];
res[iksP::domainP::k * iksP::domainP::n] += offset;
GateBootstrapping<iksP, brP, μ>(res, res, ek);
}
// No input
template <class P = lvl1param>
void HomCONSTANTONE(TLWE<P> &res)
{
res = {};
res[P::k * P::n] = P::μ;
}
template <class P = lvl1param>
void HomCONSTANTZERO(TLWE<P> &res)
{
res = {};
res[P::k * P::n] = -P::μ;
}
// 1 input
template <class P = lvl1param>
void HomNOT(TLWE<P> &res, const TLWE<P> &ca)
{
for (int i = 0; i <= P::k * P::n; i++) res[i] = -ca[i];
}
template <class P = lvl1param>
void HomCOPY(TLWE<P> &res, const TLWE<P> &ca)
{
for (int i = 0; i <= P::k * P::n; i++) res[i] = ca[i];
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomNAND(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, -1, -1, brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomNAND(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, -1, -1, iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomNOR(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, -1, -1, -brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomNOR(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, -1, -1, -iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomXNOR(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, -2, -2, -2 * brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomXNOR(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, -2, -2, -2 * iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomAND(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, 1, 1, -brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomAND(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, 1, 1, -iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomOR(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, 1, 1, brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomOR(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, 1, 1, iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomXOR(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, 2, 2, 2 * brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomXOR(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, 2, 2, 2 * iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomANDNY(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, -1, 1, -brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomANDNY(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, -1, 1, -iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomANDYN(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, 1, -1, -brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomANDYN(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, 1, -1, -iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomORNY(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, -1, 1, brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomORNY(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, -1, 1, iksP::domainP::μ>(res, ca, cb, ek);
}
template <class brP = lvl01param, typename brP::targetP::T μ = lvl1param::μ,
class iksP = lvl10param>
void HomORYN(TLWE<typename iksP::targetP> &res,
const TLWE<typename brP::domainP> &ca,
const TLWE<typename brP::domainP> &cb, const EvalKey &ek)
{
HomGate<brP, μ, iksP, 1, -1, brP::domainP::μ>(res, ca, cb, ek);
}
template <class iksP = lvl10param, class brP = lvl01param,
typename brP::targetP::T μ = lvl1param::μ>
void HomORYN(TLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &ca,
const TLWE<typename iksP::domainP> &cb, const EvalKey &ek)
{
HomGate<iksP, brP, μ, 1, -1, iksP::domainP::μ>(res, ca, cb, ek);
}
// 3input
// cs?c1:c0
template <class P = lvl1param>
void HomMUX(TLWE<P> &res, const TLWE<P> &cs, const TLWE<P> &c1,
const TLWE<P> &c0, const EvalKey &ek)
{
TLWE<P> temp;
for (int i = 0; i <= P::k * P::n; i++) temp[i] = cs[i] + c1[i];
for (int i = 0; i <= P::k * P::n; i++) res[i] = -cs[i] + c0[i];
temp[P::k * P::n] -= P::μ;
res[P::k * P::n] -= P::μ;
if constexpr (std::is_same_v<P, lvl1param>) {
TLWE<lvl0param> and1, and0;
IdentityKeySwitch<lvl10param>(and1, temp, *ek.iksklvl10);
IdentityKeySwitch<lvl10param>(and0, res, *ek.iksklvl10);
GateBootstrappingTLWE2TLWEFFT<lvl01param>(
temp, and1, *ek.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
GateBootstrappingTLWE2TLWEFFT<lvl01param>(
res, and0, *ek.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
for (int i = 0; i <= P::k * lvl1param::n; i++) res[i] += temp[i];
res[P::k * P::n] += P::μ;
}
else if constexpr (std::is_same_v<P, lvl0param>) {
TLWE<lvl1param> and1, and0;
GateBootstrappingTLWE2TLWEFFT<lvl01param>(
and1, temp, *ek.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
GateBootstrappingTLWE2TLWEFFT<lvl01param>(
and0, res, *ek.bkfftlvl01, μpolygen<lvl1param, lvl1param::μ>());
for (int i = 0; i <= lvl1param::k * lvl1param::n; i++)
and0[i] += and1[i];
IdentityKeySwitch<lvl10param>(res, and0, *ek.iksklvl10);
res[P::k * P::n] += P::μ;
}
}
template <class P = lvl1param>
void HomNMUX(TLWE<P> &res, const TLWE<P> &cs, const TLWE<P> &c1,
const TLWE<P> &c0, const EvalKey &ek)
{
HomMUX<P>(res, cs, c1, c0, ek);
for (int i = 0; i <= P::k * P::n; i++) res[i] = -res[i];
}
template <class bkP>
void HomMUXwoIKSandSE(TRLWE<typename bkP::targetP> &res,
const TLWE<typename bkP::domainP> &cs,
const TLWE<typename bkP::domainP> &c1,
const TLWE<typename bkP::domainP> &c0, const EvalKey &ek)
{
TLWE<typename bkP::domainP> temp1;
TLWE<typename bkP::domainP> temp0;
for (int i = 0; i <= bkP::domainP::n; i++) temp1[i] = cs[i] + c1[i];
for (int i = 0; i <= bkP::domainP::n; i++) temp0[i] = -cs[i] + c0[i];
temp1[lvl0param::n] -= bkP::domainP::μ;
temp0[lvl0param::n] -= bkP::domainP::μ;
TRLWE<typename bkP::targetP> and0;
BlindRotate<bkP>(res, temp1, ek.getbkfft<bkP>(),
μpolygen<typename bkP::targetP, bkP::targetP::μ>());
BlindRotate<bkP>(and0, temp0, ek.getbkfft<bkP>(),
μpolygen<typename bkP::targetP, bkP::targetP::μ>());
for (int i = 0; i < bkP::targetP::n; i++) {
res[0][i] += and0[0][i];
res[1][i] += and0[1][i];
};
res[1][0] += bkP::targetP::μ;
}
template <class brP, typename brP::targetP::T μ = brP::targetP::μ>
void HomMUXwoSE(TRLWE<typename brP::targetP> &res,
const TLWE<typename brP::domainP> &cs,
const TLWE<typename brP::domainP> &c1,
const TLWE<typename brP::domainP> &c0, const EvalKey &ek)
{
TLWE<typename brP::domainP> and1, and0;
for (int i = 0; i <= brP::domainP::k * brP::domainP::n; i++)
and1[i] = cs[i] + c1[i];
for (int i = 0; i <= brP::domainP::k * brP::domainP::n; i++)
and0[i] = -cs[i] + c0[i];
and1[brP::domainP::k * brP::domainP::n] -= brP::domainP::μ;
and0[brP::domainP::k * brP::domainP::n] -= brP::domainP::μ;
TRLWE<typename brP::targetP> and0trlwe;
BlindRotate<brP>(res, and1, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());
BlindRotate<brP>(and0trlwe, and0, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());
for (int i = 0; i < brP::targetP::k * brP::targetP::n; i++) {
res[0][i] += and0trlwe[0][i];
res[1][i] += and0trlwe[1][i];
};
res[1][0] += brP::targetP::μ;
}
template <class iksP, class brP, typename brP::targetP::T μ = brP::targetP::μ>
void HomMUXwoSE(TRLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &cs,
const TLWE<typename iksP::domainP> &c1,
const TLWE<typename iksP::domainP> &c0, const EvalKey &ek)
{
TLWE<typename iksP::domainP> temp1, temp0;
for (int i = 0; i <= iksP::domainP::k * iksP::domainP::n; i++)
temp1[i] = cs[i] + c1[i];
for (int i = 0; i <= iksP::domainP::k * iksP::domainP::n; i++)
temp0[i] = -cs[i] + c0[i];
temp1[iksP::domainP::k * iksP::domainP::n] -= iksP::domainP::μ;
temp0[iksP::domainP::k * iksP::domainP::n] -= iksP::domainP::μ;
TLWE<typename iksP::targetP> and1, and0;
IdentityKeySwitch<iksP>(and1, temp1, ek.getiksk<iksP>());
IdentityKeySwitch<iksP>(and0, temp0, ek.getiksk<iksP>());
TRLWE<typename brP::targetP> and0trlwe;
BlindRotate<brP>(res, and1, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());
BlindRotate<brP>(and0trlwe, and0, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());
for (int i = 0; i < brP::targetP::k * brP::targetP::n; i++) {
res[0][i] += and0trlwe[0][i];
res[1][i] += and0trlwe[1][i];
};
res[1][0] += brP::targetP::μ;
}
void ExtractSwitchAndHomMUX(TRLWE<lvl1param> &res, const TRLWE<lvl1param> &csr,
const TRLWE<lvl1param> &c1r,
const TRLWE<lvl1param> &c0r, const EvalKey &ek);
} // namespace TFHEpp