forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator_gadget_product.go
216 lines (170 loc) · 7.26 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
package rlwe
import (
"github.com/tuneinsight/lattigo/v4/ring"
"github.com/tuneinsight/lattigo/v4/rlwe/ringqp"
"github.com/tuneinsight/lattigo/v4/utils"
)
// GadgetProduct evaluates poly x Gadget -> RLWE where
//
// p0 = dot(decomp(cx) * gadget[0]) mod Q
// p1 = dot(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.MinInt(levelQ, gadgetCt.LevelQ())
levelP := gadgetCt.LevelP()
ctTmp := CiphertextQP{}
ctTmp.Value = [2]ringqp.Poly{{Q: ct.Value[0], P: eval.BuffQP[1].P}, {Q: ct.Value[1], P: eval.BuffQP[2].P}}
ctTmp.IsNTT = ct.IsNTT
if levelP > 0 {
eval.GadgetProductNoModDown(levelQ, cx, gadgetCt, ctTmp)
} else {
eval.GadgetProductSinglePAndBitDecompNoModDown(levelQ, cx, gadgetCt, ctTmp)
}
if ct.IsNTT && levelP != -1 {
eval.BasisExtender.ModDownQPtoQNTT(levelQ, levelP, ct.Value[0], ctTmp.Value[0].P, ct.Value[0])
eval.BasisExtender.ModDownQPtoQNTT(levelQ, levelP, ct.Value[1], ctTmp.Value[1].P, ct.Value[1])
} else if !ct.IsNTT {
eval.params.RingQ().InvNTTLazyLvl(levelQ, ct.Value[0], ct.Value[0])
eval.params.RingQ().InvNTTLazyLvl(levelQ, ct.Value[1], ct.Value[1])
if levelP != -1 {
eval.params.RingP().InvNTTLazyLvl(levelP, ctTmp.Value[0].P, ctTmp.Value[0].P)
eval.params.RingP().InvNTTLazyLvl(levelP, ctTmp.Value[1].P, ctTmp.Value[1].P)
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ct.Value[0], ctTmp.Value[0].P, ct.Value[0])
eval.BasisExtender.ModDownQPtoQ(levelQ, levelP, ct.Value[1], ctTmp.Value[1].P, ct.Value[1])
}
}
}
// GadgetProductNoModDown applies the gadget prodcut to the polynomial cx:
//
// ct.Value[0] = dot(decomp(cx) * gadget[0]) mod QP (encrypted input is multiplied by P factor)
// ct.Value[1] = dot(decomp(cx) * gadget[1]) mod QP (encrypted input is multiplied by P factor)
//
// Expects the flag IsNTT of ct to correctly reflect the domain of cx.
func (eval *Evaluator) GadgetProductNoModDown(levelQ int, cx *ring.Poly, gadgetCt GadgetCiphertext, ct CiphertextQP) {
ringQ := eval.params.RingQ()
ringP := eval.params.RingP()
ringQP := eval.params.RingQP()
c2QP := eval.BuffQP[0]
var cxNTT, cxInvNTT *ring.Poly
if ct.IsNTT {
cxNTT = cx
cxInvNTT = eval.BuffInvNTT
ringQ.InvNTTLvl(levelQ, cxNTT, cxInvNTT)
} else {
cxNTT = eval.BuffInvNTT
cxInvNTT = cx
ringQ.NTTLvl(levelQ, cxInvNTT, cxNTT)
}
levelP := gadgetCt.LevelP()
decompRNS := eval.params.DecompRNS(levelQ, levelP)
QiOverF := eval.params.QiOverflowMargin(levelQ) >> 1
PiOverF := eval.params.PiOverflowMargin(levelP) >> 1
el := gadgetCt.Value
// Key switching with CRT decomposition for the Qi
var reduce int
for i := 0; i < decompRNS; i++ {
eval.DecomposeSingleNTT(levelQ, levelP, levelP+1, i, cxNTT, cxInvNTT, c2QP.Q, c2QP.P)
if i == 0 {
ringQP.MulCoeffsMontgomeryConstantLvl(levelQ, levelP, el[i][0].Value[0], c2QP, ct.Value[0])
ringQP.MulCoeffsMontgomeryConstantLvl(levelQ, levelP, el[i][0].Value[1], c2QP, ct.Value[1])
} else {
ringQP.MulCoeffsMontgomeryConstantAndAddNoModLvl(levelQ, levelP, el[i][0].Value[0], c2QP, ct.Value[0])
ringQP.MulCoeffsMontgomeryConstantAndAddNoModLvl(levelQ, levelP, el[i][0].Value[1], c2QP, ct.Value[1])
}
if reduce%QiOverF == QiOverF-1 {
ringQ.ReduceLvl(levelQ, ct.Value[0].Q, ct.Value[0].Q)
ringQ.ReduceLvl(levelQ, ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF == PiOverF-1 {
ringP.ReduceLvl(levelP, ct.Value[0].P, ct.Value[0].P)
ringP.ReduceLvl(levelP, ct.Value[1].P, ct.Value[1].P)
}
reduce++
}
if reduce%QiOverF != 0 {
ringQ.ReduceLvl(levelQ, ct.Value[0].Q, ct.Value[0].Q)
ringQ.ReduceLvl(levelQ, ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF != 0 {
ringP.ReduceLvl(levelP, ct.Value[0].P, ct.Value[0].P)
ringP.ReduceLvl(levelP, ct.Value[1].P, ct.Value[1].P)
}
}
// GadgetProductSinglePAndBitDecompNoModDown applies the key-switch to the polynomial cx:
//
// ct.Value[0] = dot(decomp(cx) * evakey[0]) mod QP (encrypted input is multiplied by P factor)
// ct.Value[1] = dot(decomp(cx) * evakey[1]) mod QP (encrypted input is multiplied by P factor)
//
// Expects the flag IsNTT of ct to correctly reflect the domain of cx.
func (eval *Evaluator) GadgetProductSinglePAndBitDecompNoModDown(levelQ int, cx *ring.Poly, gadgetCt GadgetCiphertext, ct CiphertextQP) {
ringQ := eval.params.RingQ()
ringP := eval.params.RingP()
var cxInvNTT *ring.Poly
if ct.IsNTT {
cxInvNTT = eval.BuffInvNTT
ringQ.InvNTTLvl(levelQ, cx, cxInvNTT)
} else {
cxInvNTT = cx
}
levelP := gadgetCt.LevelP()
decompRNS := eval.params.DecompRNS(levelQ, levelP)
decompPw2 := eval.params.DecompPw2(levelQ, levelP)
pw2 := eval.params.pow2Base
mask := uint64(((1 << pw2) - 1))
if mask == 0 {
mask = 0xFFFFFFFFFFFFFFFF
}
cw := eval.BuffQP[0].Q.Coeffs[0]
cwNTT := eval.BuffBitDecomp
QiOverF := eval.params.QiOverflowMargin(levelQ) >> 1
PiOverF := eval.params.PiOverflowMargin(levelP) >> 1
el := gadgetCt.Value
// Key switching with CRT decomposition for the Qi
var reduce int
for i := 0; i < decompRNS; i++ {
for j := 0; j < decompPw2; j++ {
ring.MaskVec(cxInvNTT.Coeffs[i], cw, j*pw2, mask)
if i == 0 && j == 0 {
for u := 0; u < levelQ+1; u++ {
ringQ.NTTSingleLazy(u, cw, cwNTT)
ring.MulCoeffsMontgomeryConstantVec(el[i][j].Value[0].Q.Coeffs[u], cwNTT, ct.Value[0].Q.Coeffs[u], ringQ.Modulus[u], ringQ.MredParams[u])
ring.MulCoeffsMontgomeryConstantVec(el[i][j].Value[1].Q.Coeffs[u], cwNTT, ct.Value[1].Q.Coeffs[u], ringQ.Modulus[u], ringQ.MredParams[u])
}
for u := 0; u < levelP+1; u++ {
ringP.NTTSingleLazy(u, cw, cwNTT)
ring.MulCoeffsMontgomeryConstantVec(el[i][j].Value[0].P.Coeffs[u], cwNTT, ct.Value[0].P.Coeffs[u], ringP.Modulus[u], ringP.MredParams[u])
ring.MulCoeffsMontgomeryConstantVec(el[i][j].Value[1].P.Coeffs[u], cwNTT, ct.Value[1].P.Coeffs[u], ringP.Modulus[u], ringP.MredParams[u])
}
} else {
for u := 0; u < levelQ+1; u++ {
ringQ.NTTSingleLazy(u, cw, cwNTT)
ring.MulCoeffsMontgomeryConstantAndAddNoModVec(el[i][j].Value[0].Q.Coeffs[u], cwNTT, ct.Value[0].Q.Coeffs[u], ringQ.Modulus[u], ringQ.MredParams[u])
ring.MulCoeffsMontgomeryConstantAndAddNoModVec(el[i][j].Value[1].Q.Coeffs[u], cwNTT, ct.Value[1].Q.Coeffs[u], ringQ.Modulus[u], ringQ.MredParams[u])
}
for u := 0; u < levelP+1; u++ {
ringP.NTTSingleLazy(u, cw, cwNTT)
ring.MulCoeffsMontgomeryConstantAndAddNoModVec(el[i][j].Value[0].P.Coeffs[u], cwNTT, ct.Value[0].P.Coeffs[u], ringP.Modulus[u], ringP.MredParams[u])
ring.MulCoeffsMontgomeryConstantAndAddNoModVec(el[i][j].Value[1].P.Coeffs[u], cwNTT, ct.Value[1].P.Coeffs[u], ringP.Modulus[u], ringP.MredParams[u])
}
}
if reduce%QiOverF == QiOverF-1 {
ringQ.ReduceLvl(levelQ, ct.Value[0].Q, ct.Value[0].Q)
ringQ.ReduceLvl(levelQ, ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF == PiOverF-1 {
ringP.ReduceLvl(levelP, ct.Value[0].P, ct.Value[0].P)
ringP.ReduceLvl(levelP, ct.Value[1].P, ct.Value[1].P)
}
reduce++
}
}
if reduce%QiOverF != 0 {
ringQ.ReduceLvl(levelQ, ct.Value[0].Q, ct.Value[0].Q)
ringQ.ReduceLvl(levelQ, ct.Value[1].Q, ct.Value[1].Q)
}
if reduce%PiOverF != 0 {
ringP.ReduceLvl(levelP, ct.Value[0].P, ct.Value[0].P)
ringP.ReduceLvl(levelP, ct.Value[1].P, ct.Value[1].P)
}
}