-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
228 lines (164 loc) · 3.94 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
package ckks
import (
"github.com/ldsec/lattigo/v2/ring"
"math"
"math/big"
)
// 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
}
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 scaleUpVecExact(values []float64, n float64, moduli []uint64, coeffs [][]uint64) {
var isNegative bool
var xFlo *big.Float
var xInt *big.Int
tmp := new(big.Int)
for i := range values {
if n*math.Abs(values[i]) > 1.8446744073709552e+19 {
isNegative = false
if values[i] < 0 {
isNegative = true
xFlo = big.NewFloat(-n * values[i])
} else {
xFlo = big.NewFloat(n * values[i])
}
xFlo.Add(xFlo, big.NewFloat(0.5))
xInt = new(big.Int)
xFlo.Int(xInt)
for j := range moduli {
tmp.Mod(xInt, ring.NewUint(moduli[j]))
if isNegative {
coeffs[j][i] = moduli[j] - tmp.Uint64()
} else {
coeffs[j][i] = tmp.Uint64()
}
}
} else {
if values[i] < 0 {
for j := range moduli {
coeffs[j][i] = moduli[j] - (uint64(-n*values[i]+0.5) % moduli[j])
}
} else {
for j := range moduli {
coeffs[j][i] = uint64(n*values[i]+0.5) % moduli[j]
}
}
}
}
}
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()
}
}
}
// 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], int(Q[i]-2), Q[i])
params[i] = ring.MForm(params[i], Q[i], ring.BRedParams(Q[i]))
}
return
}
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]
}
}
}
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]
}
}
}