-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
372 lines (275 loc) · 10.2 KB
/
bootstrap.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
package ckks
import (
"fmt"
"math"
"time"
"github.com/dwkim606/test_lattigo/ring"
)
// Bootstrapp re-encrypt a ciphertext at lvl Q0 to a ciphertext at MaxLevel-k where k is the depth of the bootstrapping circuit.
// If the input ciphertext level is zero, the input scale must be an exact power of two smaller or equal to round(Q0/2^{10}).
// If the input ciphertext is at level one or more, the input scale does not need to be an exact power of two as one level
// can be used to do a scale matching.
func (btp *Bootstrapper) Bootstrapp(ct *Ciphertext) *Ciphertext {
//var t time.Time
var ct0, ct1 *Ciphertext
// Drops the level to 1
for ct.Level() > 1 {
btp.evaluator.DropLevel(ct, 1)
}
// Brings the ciphertext scale to Q0/2^{10}
if ct.Level() == 1 {
// if one level is available, then uses it to match the scale
btp.evaluator.SetScale(ct, btp.prescale)
// then drops to level 0
for ct.Level() != 0 {
btp.evaluator.DropLevel(ct, 1)
}
} else {
// else drop to level 0
for ct.Level() != 0 {
btp.evaluator.DropLevel(ct, 1)
}
// and does an integer constant mult by round((Q0/Delta_m)/ctscle)
if btp.prescale < ct.Scale {
panic("ciphetext scale > Q[0]/(Q[0]/Delta_m)")
}
btp.evaluator.ScaleUp(ct, math.Round(btp.prescale/ct.Scale), ct)
}
// ModUp ct_{Q_0} -> ct_{Q_L}
t := time.Now()
ct = btp.modUp(ct)
fmt.Println("After ModUp :", time.Since(t), ct.Level(), math.Log2(ct.Scale))
// Brings the ciphertext scale to sineQi/(Q0/scale) if its under
btp.evaluator.ScaleUp(ct, math.Round(btp.postscale/ct.Scale), ct)
//SubSum X -> (N/dslots) * Y^dslots
t = time.Now()
ct = btp.subSum(ct)
fmt.Println("After SubSum :", time.Since(t))
// Part 1 : Coeffs to slots
t = time.Now()
ct0, ct1 = CoeffsToSlots(ct, btp.pDFTInv, btp.evaluator)
fmt.Println("After CtS :", time.Since(t))
// Part 2 : SineEval
t = time.Now()
ct0, ct1 = btp.evaluateSine(ct0, ct1)
fmt.Println("After Sine :", time.Since(t))
// Part 3 : Slots to coeffs
t = time.Now()
ct0 = SlotsToCoeffs(ct0, ct1, btp.pDFT, btp.evaluator)
// ct0.Scale = math.Exp2(math.Round(math.Log2(ct0.Scale))) // rounds to the nearest power of two
fmt.Println("After StC :", time.Since(t))
return ct0
}
// Bootstrapp modified for Conv operation (added by DWKim)
// If the input ciphertext level is zero, the input scale must be an exact power of two smaller or equal to round(Q0/2^{10}).
// If the input ciphertext is at level one or more, the input scale does not need to be an exact power of two as one level
// can be used to do a scale matching.
func (btp *Bootstrapper) BootstrappConv_CtoS(ct *Ciphertext) (*Ciphertext, *Ciphertext, float64) {
//var t time.Time
var ct0, ct1 *Ciphertext
// Drops the level to 1
for ct.Level() > 1 {
btp.evaluator.DropLevel(ct, 1)
}
// Brings the ciphertext scale to Q0/2^{10}
if ct.Level() == 1 {
// if one level is available, then uses it to match the scale
btp.evaluator.SetScale(ct, btp.prescale)
// then drops to level 0
for ct.Level() != 0 {
btp.evaluator.DropLevel(ct, 1)
}
} else {
// else drop to level 0
for ct.Level() != 0 {
btp.evaluator.DropLevel(ct, 1)
}
// and does an integer constant mult by round((Q0/Delta_m)/ctscle)
if btp.prescale < ct.Scale {
panic("ciphetext scale > Q[0]/(Q[0]/Delta_m)")
}
btp.evaluator.ScaleUp(ct, math.Round(btp.prescale/ct.Scale), ct)
}
// fmt.Println("btp scale: ", math.Log2(ct.Scale))
// ModUp ct_{Q_0} -> ct_{Q_L}
//t = time.Now()
ct = btp.modUp(ct)
//log.Println("After ModUp :", time.Now().Sub(t), ct.Level(), ct.Scale())
// Brings the ciphertext scale to sineQi/(Q0/scale) if its under
btp.evaluator.ScaleUp(ct, math.Round(btp.postscale/ct.Scale), ct)
//SubSum X -> (N/dslots) * Y^dslots
//t = time.Now()
ct = btp.subSum(ct)
//log.Println("After SubSum :", time.Now().Sub(t), ct.Level(), ct.Scale())
// Part 1 : Coeffs to slots
//t = time.Now()
ct0, ct1 = CoeffsToSlots(ct, btp.pDFTInv, btp.evaluator)
//log.Println("After CtS :", time.Now().Sub(t), ct0.Level(), ct0.Scale())
// Part 2 : SineEval
//t = time.Now()
ct0, ct1 = btp.evaluateSine(ct0, ct1)
//log.Println("After Sine :", time.Now().Sub(t), ct0.Level(), ct0.Scale())
// Rescaling factor to set the final ciphertext to the desired scale
qDiff := float64(btp.params.Q()[0]) / math.Exp2(math.Round(math.Log2(float64(btp.params.Q()[0]))))
scale_StoC := qDiff * btp.params.Scale() / btp.postscale
// TO BE combined with BN // divided by 2^pow to make all inputs from ~ N(0,1) to be contained in [-1, 1]
btp.evaluator.MultByConst(ct0, scale_StoC, ct0)
// btp.evaluator.MultByConst(ct0, scale_StoC/math.Pow(2, pow), ct0)
btp.evaluator.Rescale(ct0, btp.params.Scale(), ct0)
// CAN be removed if po2 input is used!!
if ct1 != nil{
btp.evaluator.MultByConst(ct1, scale_StoC, ct1)
// btp.evaluator.MultByConst(ct1, scale_StoC/math.Pow(2, pow), ct1)
btp.evaluator.Rescale(ct1, btp.params.Scale(), ct1)
}
// btp.evaluator.MultByConst(ct1, scale_StoC, ct1)
// fmt.Print("scle_StoC: ", scale_StoC)
// Part 3 : Slots to coeffs
//t = time.Now()
// ct0 = SlotsToCoeffs(ct0, ct1, btp.pDFT, btp.evaluator)
// ct0.Scale = math.Exp2(math.Round(math.Log2(ct0.Scale))) // rounds to the nearest power of two
//log.Println("After StC :", time.Now().Sub(t), ct0.Level(), ct0.Scale())
return ct0, ct1, scale_StoC
}
func (btp *Bootstrapper) BootstrappConv_StoC(ct0, ct1 *Ciphertext) *Ciphertext {
// Part 3 : Slots to coeffs
// t = time.Now()
ct0 = SlotsToCoeffs(ct0, ct1, btp.pDFT, btp.evaluator)
// ct0.Scale = math.Exp2(math.Round(math.Log2(ct0.Scale))) // rounds to the nearest power of two
//log.Println("After StC :", time.Now().Sub(t), ct0.Level(), ct0.Scale())
return ct0
}
func (btp *Bootstrapper) subSum(ct *Ciphertext) *Ciphertext {
for i := btp.params.LogSlots(); i < btp.params.MaxLogSlots(); i++ {
btp.evaluator.Rotate(ct, 1<<i, btp.evaluator.ctxpool)
btp.evaluator.Add(ct, btp.evaluator.ctxpool, ct)
}
return ct
}
func (btp *Bootstrapper) modUp(ct *Ciphertext) *Ciphertext {
ringQ := btp.evaluator.ringQ
for i := range ct.Value {
ringQ.InvNTTLvl(ct.Level(), ct.Value[i], ct.Value[i])
}
// Extend the ciphertext with zero polynomials.
for u := range ct.Value {
ct.Value[u].Coeffs = append(ct.Value[u].Coeffs, make([][]uint64, btp.params.MaxLevel())...)
for i := 1; i < btp.params.MaxLevel()+1; i++ {
ct.Value[u].Coeffs[i] = make([]uint64, btp.params.N())
}
}
//Centers the values around Q0 and extends the basis from Q0 to QL
Q := ringQ.Modulus[0]
bredparams := ringQ.BredParams
var coeff, qi uint64
for u := range ct.Value {
for j := 0; j < btp.params.N(); j++ {
coeff = ct.Value[u].Coeffs[0][j]
for i := 1; i < btp.params.MaxLevel()+1; i++ {
qi = ringQ.Modulus[i]
if coeff > (Q >> 1) {
ct.Value[u].Coeffs[i][j] = qi - ring.BRedAdd(Q-coeff, qi, bredparams[i])
} else {
ct.Value[u].Coeffs[i][j] = ring.BRedAdd(coeff, qi, bredparams[i])
}
}
}
}
for i := range ct.Value {
ringQ.NTTLvl(ct.Level(), ct.Value[i], ct.Value[i])
}
return ct
}
// CoeffsToSlots applies the homomorphic encoding
func CoeffsToSlots(vec *Ciphertext, pDFTInv []*PtDiagMatrix, eval Evaluator) (ct0, ct1 *Ciphertext) {
var zV, zVconj *Ciphertext
zV = dft(vec, pDFTInv, true, eval)
zVconj = eval.ConjugateNew(zV)
// The real part is stored in ct0
ct0 = eval.AddNew(zV, zVconj)
// The imaginary part is stored in ct1
ct1 = eval.SubNew(zV, zVconj)
eval.DivByi(ct1, ct1)
// If repacking, then ct0 and ct1 right n/2 slots are zero.
if eval.(*evaluator).params.LogSlots() < eval.(*evaluator).params.LogN()-1 {
eval.Rotate(ct1, eval.(*evaluator).params.Slots(), ct1)
eval.Add(ct0, ct1, ct0)
return ct0, nil
}
zV = nil
zVconj = nil
return ct0, ct1
}
// SlotsToCoeffs applies the homomorphic decoding
func SlotsToCoeffs(ct0, ct1 *Ciphertext, pDFT []*PtDiagMatrix, eval Evaluator) (ct *Ciphertext) {
// If full packing, the repacking can be done directly using ct0 and ct1.
if ct1 != nil {
eval.MultByi(ct1, ct1)
eval.Add(ct0, ct1, ct0)
}
ct1 = nil
return dft(ct0, pDFT, false, eval)
}
func dft(vec *Ciphertext, plainVectors []*PtDiagMatrix, forward bool, eval Evaluator) *Ciphertext {
// Sequentially multiplies w with the provided dft matrices.
for _, plainVector := range plainVectors {
scale := vec.Scale
vec = eval.LinearTransform(vec, plainVector)[0]
if err := eval.Rescale(vec, scale, vec); err != nil {
panic(err)
}
}
return vec
}
// Sine Evaluation ct0 = Q/(2pi) * sin((2pi/Q) * ct0)
func (btp *Bootstrapper) evaluateSine(ct0, ct1 *Ciphertext) (*Ciphertext, *Ciphertext) {
ct0.Scale *= btp.MessageRatio
btp.evaluator.scale = btp.sinescale // Reference scale is changed to the Qi used for the SineEval (which is also close to the new ciphetext scale)
ct0 = btp.evaluateCheby(ct0)
ct0.Scale /= (btp.MessageRatio * btp.postscale / btp.params.Scale())
if ct1 != nil {
ct1.Scale *= btp.MessageRatio
ct1 = btp.evaluateCheby(ct1)
ct1.Scale /= (btp.MessageRatio * btp.postscale / btp.params.Scale())
}
// Reference scale is changed back to the current ciphertext's scale.
btp.evaluator.scale = ct0.Scale
return ct0, ct1
}
func (btp *Bootstrapper) evaluateCheby(ct *Ciphertext) *Ciphertext {
var err error
cheby := btp.sineEvalPoly
targetScale := btp.sinescale
// Compute the scales that the ciphertext should have before the double angle
// formula such that after it it has the scale it had before the polynomial
// evaluation
for i := 0; i < btp.SinRescal; i++ {
targetScale = math.Sqrt(targetScale * float64(btp.SineEvalModuli.Qi[i]))
}
// Division by 1/2^r and change of variable for the Chebysehev evaluation
if btp.SinType == Cos1 || btp.SinType == Cos2 {
btp.AddConst(ct, -0.5/(complex(btp.scFac, 0)*(cheby.b-cheby.a)), ct)
}
// Chebyshev evaluation
if ct, err = btp.EvaluateCheby(ct, cheby, targetScale); err != nil {
panic(err)
}
// Double angle
sqrt2pi := btp.sqrt2pi
for i := 0; i < btp.SinRescal; i++ {
sqrt2pi *= sqrt2pi
btp.MulRelin(ct, ct, ct)
btp.Add(ct, ct, ct)
btp.AddConst(ct, -sqrt2pi, ct)
if err := btp.Rescale(ct, btp.evaluator.scale, ct); err != nil {
panic(err)
}
}
// ArcSine
if btp.ArcSineDeg > 0 {
if ct, err = btp.EvaluatePoly(ct, btp.arcSinePoly, ct.Scale); err != nil {
panic(err)
}
}
return ct
}