-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrapper.go
275 lines (202 loc) · 8.46 KB
/
bootstrapper.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
package ckks
import (
"fmt"
"math"
"math/cmplx"
"github.com/dwkim606/test_lattigo/ckks/bettersine"
"github.com/dwkim606/test_lattigo/rlwe"
"github.com/dwkim606/test_lattigo/utils"
)
// Bootstrapper is a struct to stores a memory pool the plaintext matrices
// the polynomial approximation and the keys for the bootstrapping.
type Bootstrapper struct {
*evaluator
BootstrappingParameters
*BootstrappingKey
params Parameters
dslots int // Number of plaintext slots after the re-encoding
logdslots int
encoder Encoder // Encoder
prescale float64 // Q[0]/(Q[0]/|m|)
postscale float64 // Qi sineeval/(Q[0]/|m|)
sinescale float64 // Qi sineeval
sqrt2pi float64 // (1/2pi)^{-2^r}
scFac float64 // 2^{r}
sineEvalPoly *ChebyshevInterpolation // Coefficients of the Chebyshev Interpolation of sin(2*pi*x) or cos(2*pi*x/r)
arcSinePoly *Poly // Coefficients of the Taylor series of arcsine(x)
coeffsToSlotsDiffScale complex128 // Matrice rescaling
slotsToCoeffsDiffScale complex128 // Matrice rescaling
pDFT []*PtDiagMatrix // Matrice vectors
pDFTInv []*PtDiagMatrix // Matrice vectors
rotKeyIndex []int // a list of the required rotation keys
}
func sin2pi2pi(x complex128) complex128 {
return cmplx.Sin(6.283185307179586*x) / 6.283185307179586
}
func cos2pi(x complex128) complex128 {
return cmplx.Cos(6.283185307179586 * x)
}
// NewBootstrapper creates a new Bootstrapper.
func NewBootstrapper(params Parameters, btpParams *BootstrappingParameters, btpKey BootstrappingKey) (btp *Bootstrapper, err error) {
if btpParams.SinType == SinType(Sin) && btpParams.SinRescal != 0 {
return nil, fmt.Errorf("cannot use double angle formul for SinType = Sin -> must use SinType = Cos")
}
btp = newBootstrapper(params, btpParams)
btp.BootstrappingKey = &BootstrappingKey{btpKey.Rlk, btpKey.Rtks}
if err = btp.CheckKeys(); err != nil {
return nil, fmt.Errorf("invalid bootstrapping key: %w", err)
}
btp.evaluator = btp.evaluator.WithKey(rlwe.EvaluationKey{Rlk: btpKey.Rlk, Rtks: btpKey.Rtks}).(*evaluator)
return btp, nil
}
// NewBootstrapper creates a new Bootstrapper.
// modified by DWKIM to set coefficients multiplied on StoC matrices to be 1.0
func NewBootstrapper_mod(params Parameters, btpParams *BootstrappingParameters, btpKey BootstrappingKey) (btp *Bootstrapper, err error) {
if btpParams.SinType == SinType(Sin) && btpParams.SinRescal != 0 {
return nil, fmt.Errorf("cannot use double angle formul for SinType = Sin -> must use SinType = Cos")
}
btp = newBootstrapper(params, btpParams)
btp.pDFT = btp.BootstrappingParameters.GenSlotsToCoeffsMatrix(1.0, btp.encoder)
btp.BootstrappingKey = &BootstrappingKey{btpKey.Rlk, btpKey.Rtks}
if err = btp.CheckKeys(); err != nil {
return nil, fmt.Errorf("invalid bootstrapping key: %w", err)
}
btp.evaluator = btp.evaluator.WithKey(rlwe.EvaluationKey{Rlk: btpKey.Rlk, Rtks: btpKey.Rtks}).(*evaluator)
return btp, nil
}
// newBootstrapper is a constructor of "dummy" bootstrapper to enable the generation of bootstrapping-related constants
// without providing a bootstrapping key. To be replaced by a proper factorization of the bootstrapping pre-computations.
func newBootstrapper(params Parameters, btpParams *BootstrappingParameters) (btp *Bootstrapper) {
btp = new(Bootstrapper)
btp.params = params
btp.BootstrappingParameters = *btpParams.Copy()
btp.dslots = params.Slots()
btp.logdslots = params.LogSlots()
if params.LogSlots() < params.MaxLogSlots() {
btp.dslots <<= 1
btp.logdslots++
}
btp.prescale = math.Exp2(math.Round(math.Log2(float64(params.Q()[0]) / btp.MessageRatio)))
btp.sinescale = math.Exp2(math.Round(math.Log2(btp.SineEvalModuli.ScalingFactor)))
btp.postscale = btp.sinescale / btp.MessageRatio
btp.encoder = NewEncoder(params)
btp.evaluator = NewEvaluator(params, rlwe.EvaluationKey{}).(*evaluator) // creates an evaluator without keys for genDFTMatrices
btp.genSinePoly()
btp.genDFTMatrices()
btp.ctxpool = NewCiphertext(params, 1, params.MaxLevel(), 0)
return btp
}
// CheckKeys checks if all the necessary keys are present
func (btp *Bootstrapper) CheckKeys() (err error) {
if btp.Rlk == nil {
return fmt.Errorf("relinearization key is nil")
}
if btp.Rtks == nil {
return fmt.Errorf("rotation key is nil")
}
rotMissing := []int{}
for _, i := range btp.rotKeyIndex {
galEl := btp.params.GaloisElementForColumnRotationBy(int(i))
if _, generated := btp.Rtks.Keys[galEl]; !generated {
rotMissing = append(rotMissing, i)
}
}
if len(rotMissing) != 0 {
return fmt.Errorf("rotation key(s) missing: %d", rotMissing)
}
return nil
}
// AddMatrixRotToList adds the rotations neede to evaluate pVec to the list rotations
func AddMatrixRotToList(pVec *PtDiagMatrix, rotations []int, slots int, repack bool) []int {
if pVec.naive {
for j := range pVec.Vec {
if !utils.IsInSliceInt(j, rotations) {
rotations = append(rotations, j)
}
}
} else {
var index int
for j := range pVec.Vec {
N1 := pVec.N1
index = ((j / N1) * N1)
if repack {
// Sparse repacking, occurring during the first DFT matrix of the CoeffsToSlots.
index &= 2*slots - 1
} else {
// Other cases
index &= slots - 1
}
if index != 0 && !utils.IsInSliceInt(index, rotations) {
rotations = append(rotations, index)
}
index = j & (N1 - 1)
if index != 0 && !utils.IsInSliceInt(index, rotations) {
rotations = append(rotations, index)
}
}
}
return rotations
}
func (btp *Bootstrapper) genDFTMatrices() {
a := real(btp.sineEvalPoly.a)
b := real(btp.sineEvalPoly.b)
n := float64(btp.params.N())
qDiff := float64(btp.params.Q()[0]) / math.Exp2(math.Round(math.Log2(float64(btp.params.Q()[0]))))
// Change of variable for the evaluation of the Chebyshev polynomial + cancelling factor for the DFT and SubSum + evantual scaling factor for the double angle formula
btp.coeffsToSlotsDiffScale = complex(math.Pow(2.0/((b-a)*n*btp.scFac*qDiff), 1.0/float64(btp.CtSDepth(false))), 0)
// Rescaling factor to set the final ciphertext to the desired scale
btp.slotsToCoeffsDiffScale = complex(math.Pow((qDiff*btp.params.Scale())/btp.postscale, 1.0/float64(btp.StCDepth(false))), 0)
// CoeffsToSlots vectors
btp.pDFTInv = btp.BootstrappingParameters.GenCoeffsToSlotsMatrix(btp.coeffsToSlotsDiffScale, btp.encoder)
// SlotsToCoeffs vectors
btp.pDFT = btp.BootstrappingParameters.GenSlotsToCoeffsMatrix(btp.slotsToCoeffsDiffScale, btp.encoder)
// List of the rotation key values to needed for the bootstrapp
btp.rotKeyIndex = []int{}
//SubSum rotation needed X -> Y^slots rotations
for i := btp.params.LogSlots(); i < btp.params.MaxLogSlots(); i++ {
if !utils.IsInSliceInt(1<<i, btp.rotKeyIndex) {
btp.rotKeyIndex = append(btp.rotKeyIndex, 1<<i)
}
}
// Coeffs to Slots rotations
for _, pVec := range btp.pDFTInv {
btp.rotKeyIndex = AddMatrixRotToList(pVec, btp.rotKeyIndex, btp.params.Slots(), false)
}
// Slots to Coeffs rotations
for i, pVec := range btp.pDFT {
btp.rotKeyIndex = AddMatrixRotToList(pVec, btp.rotKeyIndex, btp.params.Slots(), (i == 0) && (btp.params.LogSlots() < btp.params.MaxLogSlots()))
}
}
func (btp *Bootstrapper) genSinePoly() {
K := int(btp.SinRange)
deg := int(btp.SinDeg)
btp.scFac = float64(int(1 << btp.SinRescal))
if btp.ArcSineDeg > 0 {
btp.sqrt2pi = 1.0
coeffs := make([]complex128, btp.ArcSineDeg+1)
coeffs[1] = 0.15915494309189535
for i := 3; i < btp.ArcSineDeg+1; i += 2 {
coeffs[i] = coeffs[i-2] * complex(float64(i*i-4*i+4)/float64(i*i-i), 0)
}
btp.arcSinePoly = NewPoly(coeffs)
} else {
btp.sqrt2pi = math.Pow(0.15915494309189535, 1.0/btp.scFac)
}
if btp.SinType == Sin {
btp.sineEvalPoly = Approximate(sin2pi2pi, -complex(float64(K)/btp.scFac, 0), complex(float64(K)/btp.scFac, 0), deg)
} else if btp.SinType == Cos1 {
btp.sineEvalPoly = new(ChebyshevInterpolation)
btp.sineEvalPoly.coeffs = bettersine.Approximate(K, deg, btp.MessageRatio, int(btp.SinRescal))
btp.sineEvalPoly.maxDeg = btp.sineEvalPoly.Degree()
btp.sineEvalPoly.a = complex(float64(-K)/btp.scFac, 0)
btp.sineEvalPoly.b = complex(float64(K)/btp.scFac, 0)
btp.sineEvalPoly.lead = true
} else if btp.SinType == Cos2 {
btp.sineEvalPoly = Approximate(cos2pi, -complex(float64(K)/btp.scFac, 0), complex(float64(K)/btp.scFac, 0), deg)
} else {
panic("Bootstrapper -> invalid sineType")
}
for i := range btp.sineEvalPoly.coeffs {
btp.sineEvalPoly.coeffs[i] *= complex(btp.sqrt2pi, 0)
}
}