-
Notifications
You must be signed in to change notification settings - Fork 172
/
utils.go
336 lines (250 loc) · 6.58 KB
/
utils.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
package ckks
import (
"math"
"math/big"
"github.com/tuneinsight/lattigo/v3/ring"
)
// StandardDeviation computes the scaled standard deviation of the input vector.
func StandardDeviation(vec []float64, scale float64) (std float64) {
// We assume that the error is centered around zero
var err, tmp, mean, n float64
n = float64(len(vec))
for _, c := range vec {
mean += c
}
mean /= n
for _, c := range vec {
tmp = c - mean
err += tmp * tmp
}
return math.Sqrt(err/n) * scale
}
// NttAndMontgomeryLvl takes the polynomial polIn Z[Y] outside of the NTT domain to the polynomial Z[X] in the NTT domain where Y = X^(gap).
// This method is used to accelerate the NTT of polynomials that encode sparse plaintexts.
func NttAndMontgomeryLvl(level int, logSlots int, ringQ *ring.Ring, montgomery bool, pol *ring.Poly) {
if 1<<logSlots == ringQ.NthRoot>>2 {
ringQ.NTTLvl(level, pol, pol)
if montgomery {
ringQ.MFormLvl(level, pol, pol)
}
} else {
var n int
var NTT func(coeffsIn, coeffsOut []uint64, N int, nttPsi []uint64, Q, QInv uint64, bredParams []uint64)
switch ringQ.Type() {
case ring.Standard:
n = 2 << logSlots
NTT = ring.NTT
case ring.ConjugateInvariant:
n = 1 << logSlots
NTT = ring.NTTConjugateInvariant
}
N := ringQ.N
gap := N / n
for i := 0; i < level+1; i++ {
coeffs := pol.Coeffs[i]
// NTT in dimension n
NTT(coeffs[:n], coeffs[:n], n, ringQ.NttPsi[i], ringQ.Modulus[i], ringQ.MredParams[i], ringQ.BredParams[i])
if montgomery {
ring.MFormVec(coeffs[:n], coeffs[:n], ringQ.Modulus[i], ringQ.BredParams[i])
}
// Maps NTT in dimension n to NTT in dimension N
for j := n - 1; j >= 0; j-- {
c := coeffs[j]
for w := 0; w < gap; w++ {
coeffs[j*gap+w] = c
}
}
}
}
}
func interfaceMod(x interface{}, qi uint64) uint64 {
switch x := x.(type) {
case uint64:
return x % qi
case int64:
if x > 0 {
return uint64(x)
} else if x < 0 {
return uint64(int64(qi) + x%int64(qi))
}
return 0
case *big.Int:
if x.Cmp(ring.NewUint(0)) != 0 {
return new(big.Int).Mod(x, ring.NewUint(qi)).Uint64()
}
return 0
default:
panic("constant must either be uint64, int64 or *big.Int")
}
}
func complexToFixedPointCRT(level int, values []complex128, scale float64, ringQ *ring.Ring, coeffs [][]uint64, isRingStandard bool) {
for i, v := range values {
singleFloatToFixedPointCRT(level, i, real(v), scale, ringQ, coeffs)
}
if isRingStandard {
slots := len(values)
for i, v := range values {
singleFloatToFixedPointCRT(level, i+slots, imag(v), scale, ringQ, coeffs)
}
}
}
func floatToFixedPointCRT(level int, values []float64, scale float64, ringQ *ring.Ring, coeffs [][]uint64) {
for i, v := range values {
singleFloatToFixedPointCRT(level, i, v, scale, ringQ, coeffs)
}
}
func singleFloatToFixedPointCRT(level, i int, value float64, scale float64, ringQ *ring.Ring, coeffs [][]uint64) {
var isNegative bool
var xFlo *big.Float
var xInt *big.Int
tmp := new(big.Int)
var c uint64
isNegative = false
if value < 0 {
isNegative = true
scale *= -1
}
value *= scale
moduli := ringQ.Modulus
if value > 1.8446744073709552e+19 {
xFlo = big.NewFloat(value)
xFlo.Add(xFlo, big.NewFloat(0.5))
xInt = new(big.Int)
xFlo.Int(xInt)
for j := range moduli[:level+1] {
tmp.Mod(xInt, ring.NewUint(moduli[j]))
if isNegative {
coeffs[j][i] = moduli[j] - tmp.Uint64()
} else {
coeffs[j][i] = tmp.Uint64()
}
}
} else {
bredParams := ringQ.BredParams
c = uint64(value + 0.5)
if isNegative {
for j, qi := range moduli[:level+1] {
if c > qi {
coeffs[j][i] = qi - ring.BRedAdd(c, qi, bredParams[j])
} else {
coeffs[j][i] = qi - c
}
}
} else {
for j, qi := range moduli[:level+1] {
if c > 0x1fffffffffffffff {
coeffs[j][i] = ring.BRedAdd(c, qi, bredParams[j])
} else {
coeffs[j][i] = c
}
}
}
}
}
func scaleUpExact(value float64, n float64, q uint64) (res uint64) {
var isNegative bool
var xFlo *big.Float
var xInt *big.Int
isNegative = false
if value < 0 {
isNegative = true
xFlo = big.NewFloat(-n * value)
} else {
xFlo = big.NewFloat(n * value)
}
xFlo.Add(xFlo, big.NewFloat(0.5))
xInt = new(big.Int)
xFlo.Int(xInt)
xInt.Mod(xInt, ring.NewUint(q))
res = xInt.Uint64()
if isNegative {
res = q - res
}
return
}
func scaleUpVecExactBigFloat(values []*big.Float, scale float64, moduli []uint64, coeffs [][]uint64) {
prec := int(values[0].Prec())
xFlo := ring.NewFloat(0, prec)
xInt := new(big.Int)
tmp := new(big.Int)
zero := ring.NewFloat(0, prec)
scaleFlo := ring.NewFloat(scale, prec)
half := ring.NewFloat(0.5, prec)
for i := range values {
xFlo.Mul(scaleFlo, values[i])
if values[i].Cmp(zero) < 0 {
xFlo.Sub(xFlo, half)
} else {
xFlo.Add(xFlo, half)
}
xFlo.Int(xInt)
for j := range moduli {
Q := ring.NewUint(moduli[j])
tmp.Mod(xInt, Q)
if values[i].Cmp(zero) < 0 {
tmp.Add(tmp, Q)
}
coeffs[j][i] = tmp.Uint64()
}
}
}
// SliceBitReverseInPlaceComplex128 applies an in-place bit-reverse permuation on the input slice.
func SliceBitReverseInPlaceComplex128(slice []complex128, N int) {
var bit, j int
for i := 1; i < N; i++ {
bit = N >> 1
for j >= bit {
j -= bit
bit >>= 1
}
j += bit
if i < j {
slice[i], slice[j] = slice[j], slice[i]
}
}
}
// SliceBitReverseInPlaceRingComplex applies an in-place bit-reverse permuation on the input slice.
func SliceBitReverseInPlaceRingComplex(slice []*ring.Complex, N int) {
var bit, j int
for i := 1; i < N; i++ {
bit = N >> 1
for j >= bit {
j -= bit
bit >>= 1
}
j += bit
if i < j {
slice[i], slice[j] = slice[j], slice[i]
}
}
}
// Divides x by n^2, returns a float
func scaleDown(coeff *big.Int, n float64) (x float64) {
x, _ = new(big.Float).SetInt(coeff).Float64()
x /= n
return
}
func genBigIntChain(Q []uint64) (bigintChain []*big.Int) {
bigintChain = make([]*big.Int, len(Q))
bigintChain[0] = ring.NewUint(Q[0])
for i := 1; i < len(Q); i++ {
bigintChain[i] = ring.NewUint(Q[i])
bigintChain[i].Mul(bigintChain[i], bigintChain[i-1])
}
return
}
// GenSwitchkeysRescalingParams generates the parameters for rescaling the switching keys
func GenSwitchkeysRescalingParams(Q, P []uint64) (params []uint64) {
params = make([]uint64, len(Q))
PBig := ring.NewUint(1)
for _, pj := range P {
PBig.Mul(PBig, ring.NewUint(pj))
}
tmp := ring.NewUint(0)
for i := 0; i < len(Q); i++ {
params[i] = tmp.Mod(PBig, ring.NewUint(Q[i])).Uint64()
params[i] = ring.ModExp(params[i], Q[i]-2, Q[i])
params[i] = ring.MForm(params[i], Q[i], ring.BRedParams(Q[i]))
}
return
}