forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator_gadget_product.go
472 lines (378 loc) · 14.2 KB
/
evaluator_gadget_product.go
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package rlwe
import (
"fmt"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/ring/ringqp"
"github.com/tuneinsight/lattigo/v5/utils"
)
// GadgetProduct evaluates poly x Gadget -> RLWE where
//
// ct = [<decomp(cx), gadget[0]>, <decomp(cx), gadget[1]>] mod Q
//
// Expects the flag IsNTT of ct to correctly reflect the domain of cx.
func (eval Evaluator) GadgetProduct(levelQ int, cx ring.Poly, gadgetCt *GadgetCiphertext, ct *Ciphertext) {
levelQ = utils.Min(levelQ, gadgetCt.LevelQ())
levelP := gadgetCt.LevelP()
ctTmp := &Element[ringqp.Poly]{}
ctTmp.Value = []ringqp.Poly{{Q: ct.Value[0], P: eval.BuffQP[0].P}, {Q: ct.Value[1], P: eval.BuffQP[1].P}}
ctTmp.MetaData = ct.MetaData
eval.GadgetProductLazy(levelQ, cx, gadgetCt, ctTmp)
eval.ModDown(levelQ, levelP, ctTmp, ct)
}
// ModDown takes ctQP (mod QP) and returns ct = (ctQP/P) (mod Q).
func (eval Evaluator) ModDown(levelQ, levelP int, ctQP *Element[ringqp.Poly], ct *Ciphertext) {
ringQP := eval.params.RingQP().AtLevel(levelQ, levelP)
if levelP != -1 {
if ctQP.IsNTT {
if ct.IsNTT {
// NTT -> NTT
eval.BasisExtender.ModDownQPtoQNTT(levelQ, levelP, ctQP.Value[0].Q, ctQP.Value[0].P, ct.Value[0])
eval.BasisExtender.ModDownQPtoQNTT(levelQ, levelP, ctQP.Value[1].Q, ctQP.Value[1].P, ct.Value[1])
} else {
// NTT -> INTT
ringQP := eval.params.RingQP().AtLevel(levelQ, levelP)
ringQP.INTTLazy(ctQP.Value[0], ctQP.Value[0])
ringQP.INTTLazy(ctQP.Value[1], ctQP.Value[1])
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ctQP.Value[0].Q, ctQP.Value[0].P, ct.Value[0])
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ctQP.Value[1].Q, ctQP.Value[1].P, ct.Value[1])
}
} else {
if ct.IsNTT {
// INTT -> NTT
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ctQP.Value[0].Q, ctQP.Value[0].P, ct.Value[0])
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ctQP.Value[1].Q, ctQP.Value[1].P, ct.Value[1])
ringQP.RingQ.NTT(ct.Value[0], ct.Value[0])
ringQP.RingQ.NTT(ct.Value[1], ct.Value[1])
} else {
// INTT -> INTT
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ctQP.Value[0].Q, ctQP.Value[0].P, ct.Value[0])
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ctQP.Value[1].Q, ctQP.Value[1].P, ct.Value[1])
}
}
} else {
if ctQP.IsNTT {
if ct.IsNTT {
// NTT -> NTT
ctQP.Value[0].Q.CopyLvl(levelQ, ct.Value[0])
ctQP.Value[1].Q.CopyLvl(levelQ, ct.Value[1])
} else {
// NTT -> INTT
ringQP.RingQ.INTT(ctQP.Value[0].Q, ct.Value[0])
ringQP.RingQ.INTT(ctQP.Value[1].Q, ct.Value[1])
}
} else {
if ct.IsNTT {
// INTT -> NTT
ringQP.RingQ.NTT(ctQP.Value[0].Q, ct.Value[0])
ringQP.RingQ.NTT(ctQP.Value[1].Q, ct.Value[1])
} else {
// INTT -> INTT
ctQP.Value[0].Q.CopyLvl(levelQ, ct.Value[0])
ctQP.Value[1].Q.CopyLvl(levelQ, ct.Value[1])
}
}
}
}
// GadgetProductLazy evaluates poly x Gadget -> RLWE where
//
// ct = [<decomp(cx), gadget[0]>, <decomp(cx), gadget[1]>] mod QP
//
// Expects the flag IsNTT of ct to correctly reflect the domain of cx.
//
// Result NTT domain is returned according to the NTT flag of ct.
func (eval Evaluator) GadgetProductLazy(levelQ int, cx ring.Poly, gadgetCt *GadgetCiphertext, ct *Element[ringqp.Poly]) {
if gadgetCt.LevelP() > 0 {
eval.gadgetProductMultiplePLazy(levelQ, cx, gadgetCt, ct)
} else {
eval.gadgetProductSinglePAndBitDecompLazy(levelQ, cx, gadgetCt, ct)
}
if !ct.IsNTT {
ringQP := eval.params.RingQP().AtLevel(levelQ, gadgetCt.LevelP())
ringQP.INTT(ct.Value[0], ct.Value[0])
ringQP.INTT(ct.Value[1], ct.Value[1])
}
}
func (eval Evaluator) gadgetProductMultiplePLazy(levelQ int, cx ring.Poly, gadgetCt *GadgetCiphertext, ct *Element[ringqp.Poly]) {
levelP := gadgetCt.LevelP()
ringQP := eval.params.RingQP().AtLevel(levelQ, levelP)
ringQ := ringQP.RingQ
ringP := ringQP.RingP
c2QP := eval.BuffDecompQP[0]
var cxNTT, cxInvNTT ring.Poly
if ct.IsNTT {
cxNTT = cx
cxInvNTT = eval.BuffInvNTT
ringQ.INTT(cxNTT, cxInvNTT)
} else {
cxNTT = eval.BuffInvNTT
cxInvNTT = cx
ringQ.NTT(cxInvNTT, cxNTT)
}
BaseRNSDecompositionVectorSize := eval.params.BaseRNSDecompositionVectorSize(levelQ, levelP)
QiOverF := eval.params.QiOverflowMargin(levelQ) >> 1
PiOverF := eval.params.PiOverflowMargin(levelP) >> 1
el := gadgetCt.Value
// Re-encryption with CRT decomposition for the Qi
var reduce int
for i := 0; i < BaseRNSDecompositionVectorSize; i++ {
eval.DecomposeSingleNTT(levelQ, levelP, levelP+1, i, cxNTT, cxInvNTT, c2QP.Q, c2QP.P)
if i == 0 {
ringQP.MulCoeffsMontgomeryLazy(el[i][0][0], c2QP, ct.Value[0])
ringQP.MulCoeffsMontgomeryLazy(el[i][0][1], c2QP, ct.Value[1])
} else {
ringQP.MulCoeffsMontgomeryLazyThenAddLazy(el[i][0][0], c2QP, ct.Value[0])
ringQP.MulCoeffsMontgomeryLazyThenAddLazy(el[i][0][1], c2QP, ct.Value[1])
}
if reduce%QiOverF == QiOverF-1 {
ringQ.Reduce(ct.Value[0].Q, ct.Value[0].Q)
ringQ.Reduce(ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF == PiOverF-1 {
ringP.Reduce(ct.Value[0].P, ct.Value[0].P)
ringP.Reduce(ct.Value[1].P, ct.Value[1].P)
}
reduce++
}
if reduce%QiOverF != 0 {
ringQ.Reduce(ct.Value[0].Q, ct.Value[0].Q)
ringQ.Reduce(ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF != 0 {
ringP.Reduce(ct.Value[0].P, ct.Value[0].P)
ringP.Reduce(ct.Value[1].P, ct.Value[1].P)
}
}
func (eval Evaluator) gadgetProductSinglePAndBitDecompLazy(levelQ int, cx ring.Poly, gadgetCt *GadgetCiphertext, ct *Element[ringqp.Poly]) {
levelP := gadgetCt.LevelP()
ringQP := eval.params.RingQP().AtLevel(levelQ, levelP)
ringQ := ringQP.RingQ
ringP := ringQP.RingP
var cxInvNTT ring.Poly
if ct.IsNTT {
cxInvNTT = eval.BuffInvNTT
ringQ.INTT(cx, cxInvNTT)
} else {
cxInvNTT = cx
}
pw2 := gadgetCt.BaseTwoDecomposition
BaseRNSDecompositionVectorSize := levelQ + 1
BaseTwoDecompositionVectorSize := gadgetCt.BaseTwoDecompositionVectorSize()
mask := uint64(((1 << pw2) - 1))
cw := eval.BuffDecompQP[0].Q.Coeffs[0]
cwNTT := eval.BuffBitDecomp
QiOverF := eval.params.QiOverflowMargin(levelQ) >> 1
PiOverF := eval.params.PiOverflowMargin(levelP) >> 1
el := gadgetCt.Value
c2QP := eval.BuffDecompQP[0]
// Re-encryption with CRT decomposition for the Qi
var reduce int
for i := 0; i < BaseRNSDecompositionVectorSize; i++ {
// Only centers the coefficients if the mask is 0
// As centering doesn't help reduce the noise if
// the power of two decomposition is applied on top
// of the RNS decomposition
if mask == 0 {
eval.Decomposer.DecomposeAndSplit(levelQ, levelP, levelP+1, i, cxInvNTT, c2QP.Q, c2QP.P)
}
for j := 0; j < BaseTwoDecompositionVectorSize[i]; j++ {
if i == 0 && j == 0 {
for u, s := range ringQ.SubRings[:levelQ+1] {
if mask == 0 {
s.NTTLazy(c2QP.Q.Coeffs[u], cwNTT)
} else {
ring.MaskVec(cxInvNTT.Coeffs[i], j*pw2, mask, cw)
s.NTTLazy(cw, cwNTT)
}
s.MulCoeffsMontgomeryLazy(el[i][j][0].Q.Coeffs[u], cwNTT, ct.Value[0].Q.Coeffs[u])
s.MulCoeffsMontgomeryLazy(el[i][j][1].Q.Coeffs[u], cwNTT, ct.Value[1].Q.Coeffs[u])
}
if ringP != nil {
for u, s := range ringP.SubRings[:levelP+1] {
if mask == 0 {
s.NTTLazy(c2QP.P.Coeffs[u], cwNTT)
} else {
ring.MaskVec(cxInvNTT.Coeffs[i], j*pw2, mask, cw)
s.NTTLazy(cw, cwNTT)
}
s.MulCoeffsMontgomeryLazy(el[i][j][0].P.Coeffs[u], cwNTT, ct.Value[0].P.Coeffs[u])
s.MulCoeffsMontgomeryLazy(el[i][j][1].P.Coeffs[u], cwNTT, ct.Value[1].P.Coeffs[u])
}
}
} else {
for u, s := range ringQ.SubRings[:levelQ+1] {
if mask == 0 {
s.NTTLazy(c2QP.Q.Coeffs[u], cwNTT)
} else {
ring.MaskVec(cxInvNTT.Coeffs[i], j*pw2, mask, cw)
s.NTTLazy(cw, cwNTT)
}
s.MulCoeffsMontgomeryLazyThenAddLazy(el[i][j][0].Q.Coeffs[u], cwNTT, ct.Value[0].Q.Coeffs[u])
s.MulCoeffsMontgomeryLazyThenAddLazy(el[i][j][1].Q.Coeffs[u], cwNTT, ct.Value[1].Q.Coeffs[u])
}
if ringP != nil {
for u, s := range ringP.SubRings[:levelP+1] {
if mask == 0 {
s.NTTLazy(c2QP.P.Coeffs[u], cwNTT)
} else {
ring.MaskVec(cxInvNTT.Coeffs[i], j*pw2, mask, cw)
s.NTTLazy(cw, cwNTT)
}
s.MulCoeffsMontgomeryLazyThenAddLazy(el[i][j][0].P.Coeffs[u], cwNTT, ct.Value[0].P.Coeffs[u])
s.MulCoeffsMontgomeryLazyThenAddLazy(el[i][j][1].P.Coeffs[u], cwNTT, ct.Value[1].P.Coeffs[u])
}
}
}
if reduce%QiOverF == QiOverF-1 {
ringQ.Reduce(ct.Value[0].Q, ct.Value[0].Q)
ringQ.Reduce(ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF == PiOverF-1 {
ringP.Reduce(ct.Value[0].P, ct.Value[0].P)
ringP.Reduce(ct.Value[1].P, ct.Value[1].P)
}
reduce++
}
}
if reduce%QiOverF != 0 {
ringQ.Reduce(ct.Value[0].Q, ct.Value[0].Q)
ringQ.Reduce(ct.Value[1].Q, ct.Value[1].Q)
}
if ringP != nil {
if reduce%PiOverF != 0 {
ringP.Reduce(ct.Value[0].P, ct.Value[0].P)
ringP.Reduce(ct.Value[1].P, ct.Value[1].P)
}
}
}
// GadgetProductHoisted applies the key-switch to the decomposed polynomial c2 mod QP (BuffQPDecompQP)
// and divides the result by P, reducing the basis from QP to Q.
//
// ct = [<BuffQPDecompQP, gadgetCt[0]) mod Q
//
// BuffQPDecompQP is expected to be in the NTT domain.
//
// Result NTT domain is returned according to the NTT flag of ct.
func (eval Evaluator) GadgetProductHoisted(levelQ int, BuffQPDecompQP []ringqp.Poly, gadgetCt *GadgetCiphertext, ct *Ciphertext) {
ctQP := &Element[ringqp.Poly]{}
ctQP.Value = []ringqp.Poly{
{Q: ct.Value[0], P: eval.BuffQP[0].P},
{Q: ct.Value[1], P: eval.BuffQP[1].P},
}
ctQP.MetaData = ct.MetaData
eval.GadgetProductHoistedLazy(levelQ, BuffQPDecompQP, gadgetCt, ctQP)
eval.ModDown(levelQ, gadgetCt.LevelP(), ctQP, ct)
}
// GadgetProductHoistedLazy applies the gadget product to the decomposed polynomial c2 mod QP (BuffQPDecompQ and BuffQPDecompP)
//
// BuffQP2 = dot(BuffQPDecompQ||BuffQPDecompP * gadgetCt[0]) mod QP
// BuffQP3 = dot(BuffQPDecompQ||BuffQPDecompP * gadgetCt[1]) mod QP
//
// BuffQPDecompQP is expected to be in the NTT domain.
//
// Result NTT domain is returned according to the NTT flag of ct.
func (eval Evaluator) GadgetProductHoistedLazy(levelQ int, BuffQPDecompQP []ringqp.Poly, gadgetCt *GadgetCiphertext, ct *Element[ringqp.Poly]) {
// Sanity check for invalid parameters.
if gadgetCt.BaseTwoDecomposition != 0 {
panic(fmt.Errorf("cannot GadgetProductHoistedLazy: method is unsupported for BaseTwoDecomposition != 0"))
}
eval.gadgetProductMultiplePLazyHoisted(levelQ, BuffQPDecompQP, gadgetCt, ct)
if !ct.IsNTT {
ringQP := eval.params.RingQP().AtLevel(levelQ, gadgetCt.LevelP())
ringQP.INTT(ct.Value[0], ct.Value[0])
ringQP.INTT(ct.Value[1], ct.Value[1])
}
}
func (eval Evaluator) gadgetProductMultiplePLazyHoisted(levelQ int, BuffQPDecompQP []ringqp.Poly, gadgetCt *GadgetCiphertext, ct *Element[ringqp.Poly]) {
levelP := gadgetCt.LevelP()
ringQP := eval.params.RingQP().AtLevel(levelQ, levelP)
ringQ := ringQP.RingQ
ringP := ringQP.RingP
c0QP := ct.Value[0]
c1QP := ct.Value[1]
BaseRNSDecompositionVectorSize := eval.params.BaseRNSDecompositionVectorSize(levelQ, levelP)
QiOverF := eval.params.QiOverflowMargin(levelQ) >> 1
PiOverF := eval.params.PiOverflowMargin(levelP) >> 1
// Key switching with CRT decomposition for the Qi
var reduce int
for i := 0; i < BaseRNSDecompositionVectorSize; i++ {
gct := gadgetCt.Value[i][0]
if i == 0 {
ringQP.MulCoeffsMontgomeryLazy(gct[0], BuffQPDecompQP[i], c0QP)
ringQP.MulCoeffsMontgomeryLazy(gct[1], BuffQPDecompQP[i], c1QP)
} else {
ringQP.MulCoeffsMontgomeryLazyThenAddLazy(gct[0], BuffQPDecompQP[i], c0QP)
ringQP.MulCoeffsMontgomeryLazyThenAddLazy(gct[1], BuffQPDecompQP[i], c1QP)
}
if reduce%QiOverF == QiOverF-1 {
ringQ.Reduce(c0QP.Q, c0QP.Q)
ringQ.Reduce(c1QP.Q, c1QP.Q)
}
if reduce%PiOverF == PiOverF-1 {
ringP.Reduce(c0QP.P, c0QP.P)
ringP.Reduce(c1QP.P, c1QP.P)
}
reduce++
}
if reduce%QiOverF != 0 {
ringQ.Reduce(c0QP.Q, c0QP.Q)
ringQ.Reduce(c1QP.Q, c1QP.Q)
}
if reduce%PiOverF != 0 {
ringP.Reduce(c0QP.P, c0QP.P)
ringP.Reduce(c1QP.P, c1QP.P)
}
}
// DecomposeNTT applies the full RNS basis decomposition on c2.
// Expects the IsNTT flag of c2 to correctly reflect the domain of c2.
// BuffQPDecompQ and BuffQPDecompQ are vectors of polynomials (mod Q and mod P) that store the
// special RNS decomposition of c2 (in the NTT domain)
func (eval Evaluator) DecomposeNTT(levelQ, levelP, nbPi int, c2 ring.Poly, c2IsNTT bool, decompQP []ringqp.Poly) {
ringQ := eval.params.RingQ().AtLevel(levelQ)
var polyNTT, polyInvNTT ring.Poly
if c2IsNTT {
polyNTT = c2
polyInvNTT = eval.BuffInvNTT
ringQ.INTT(polyNTT, polyInvNTT)
} else {
polyNTT = eval.BuffInvNTT
polyInvNTT = c2
ringQ.NTT(polyInvNTT, polyNTT)
}
BaseRNSDecompositionVectorSize := eval.params.BaseRNSDecompositionVectorSize(levelQ, levelP)
for i := 0; i < BaseRNSDecompositionVectorSize; i++ {
eval.DecomposeSingleNTT(levelQ, levelP, nbPi, i, polyNTT, polyInvNTT, decompQP[i].Q, decompQP[i].P)
}
}
// DecomposeSingleNTT takes the input polynomial c2 (c2NTT and c2InvNTT, respectively in the NTT and out of the NTT domain)
// modulo the RNS basis, and returns the result on c2QiQ and c2QiP, the receiver polynomials respectively mod Q and mod P (in the NTT domain)
func (eval Evaluator) DecomposeSingleNTT(levelQ, levelP, nbPi, BaseRNSDecompositionVectorSize int, c2NTT, c2InvNTT, c2QiQ, c2QiP ring.Poly) {
ringQ := eval.params.RingQ().AtLevel(levelQ)
ringP := eval.params.RingP().AtLevel(levelP)
eval.Decomposer.DecomposeAndSplit(levelQ, levelP, nbPi, BaseRNSDecompositionVectorSize, c2InvNTT, c2QiQ, c2QiP)
p0idxst := BaseRNSDecompositionVectorSize * nbPi
p0idxed := p0idxst + nbPi
// c2_qi = cx mod qi mod qi
for x := 0; x < levelQ+1; x++ {
if p0idxst <= x && x < p0idxed {
copy(c2QiQ.Coeffs[x], c2NTT.Coeffs[x])
} else {
ringQ.SubRings[x].NTT(c2QiQ.Coeffs[x], c2QiQ.Coeffs[x])
}
}
if ringP != nil {
// c2QiP = c2 mod qi mod pj
ringP.NTT(c2QiP, c2QiP)
}
}
/*
type DecompositionBuffer [][]ringqp.Poly
func (eval Evaluator) ALlocateDecompositionBuffer(levelQ, levelP, Pow2Base int) (DecompositionBuffer){
decompQP := make([][]ringqp.Poly, BaseRNSDecompositionVectorSize)
for i := 0; i < BaseRNSDecompositionVectorSize; i++ {
for j := 0; j < BaseTwoDecompositionVectorSize; j++{
DecompositionBuffer[i][j] = ringQP.NewPoly()
}
}
return decompQPs
}
*/