forked from tuneinsight/lattigo
/
precision.go
247 lines (201 loc) · 6.44 KB
/
precision.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
package ckks
import (
"fmt"
"math"
"sort"
"github.com/fedejinich/lattigo/v6/rlwe"
)
// PrecisionStats is a struct storing statistic about the precision of a CKKS plaintext
type PrecisionStats struct {
MaxDelta Stats
MinDelta Stats
MaxPrecision Stats
MinPrecision Stats
MeanDelta Stats
MeanPrecision Stats
MedianDelta Stats
MedianPrecision Stats
STDFreq float64
STDTime float64
RealDist, ImagDist, L2Dist []struct {
Prec float64
Count int
}
cdfResol int
}
// Stats is a struct storing the real, imaginary and L2 norm (modulus)
// about the precision of a complex value.
type Stats struct {
Real, Imag, L2 float64
}
func (prec PrecisionStats) String() string {
return fmt.Sprintf(`
┌─────────┬───────┬───────┬───────┐
│ Log2 │ REAL │ IMAG │ L2 │
├─────────┼───────┼───────┼───────┤
│MIN Prec │ %5.2f │ %5.2f │ %5.2f │
│MAX Prec │ %5.2f │ %5.2f │ %5.2f │
│AVG Prec │ %5.2f │ %5.2f │ %5.2f │
│MED Prec │ %5.2f │ %5.2f │ %5.2f │
└─────────┴───────┴───────┴───────┘
Err STD Slots : %5.2f Log2
Err STD Coeffs : %5.2f Log2
`,
prec.MinPrecision.Real, prec.MinPrecision.Imag, prec.MinPrecision.L2,
prec.MaxPrecision.Real, prec.MaxPrecision.Imag, prec.MaxPrecision.L2,
prec.MeanPrecision.Real, prec.MeanPrecision.Imag, prec.MeanPrecision.L2,
prec.MedianPrecision.Real, prec.MedianPrecision.Imag, prec.MedianPrecision.L2,
math.Log2(prec.STDFreq),
math.Log2(prec.STDTime))
}
// GetPrecisionStats generates a PrecisionStats struct from the reference values and the decrypted values
// vWant.(type) must be either []complex128 or []float64
// element.(type) must be either *Plaintext, *Ciphertext, []complex128 or []float64. If not *Ciphertext, then decryptor can be nil.
func GetPrecisionStats(params Parameters, encoder Encoder, decryptor rlwe.Decryptor, vWant, element interface{}, logSlots int, sigma float64) (prec PrecisionStats) {
var valuesTest []complex128
switch element := element.(type) {
case *rlwe.Ciphertext:
valuesTest = encoder.DecodePublic(decryptor.DecryptNew(element), logSlots, sigma)
case *rlwe.Plaintext:
valuesTest = encoder.DecodePublic(element, logSlots, sigma)
case []complex128:
valuesTest = element
case []float64:
valuesTest = make([]complex128, len(element))
for i := range element {
valuesTest[i] = complex(element[i], 0)
}
}
var valuesWant []complex128
switch element := vWant.(type) {
case []complex128:
valuesWant = element
case []float64:
valuesWant = make([]complex128, len(element))
for i := range element {
valuesWant[i] = complex(element[i], 0)
}
}
var deltaReal, deltaImag, deltaL2 float64
slots := len(valuesWant)
diff := make([]Stats, slots)
prec.MaxDelta = Stats{0, 0, 0}
prec.MinDelta = Stats{1, 1, 1}
prec.MeanDelta = Stats{0, 0, 0}
prec.cdfResol = 500
prec.RealDist = make([]struct {
Prec float64
Count int
}, prec.cdfResol)
prec.ImagDist = make([]struct {
Prec float64
Count int
}, prec.cdfResol)
prec.L2Dist = make([]struct {
Prec float64
Count int
}, prec.cdfResol)
precReal := make([]float64, len(valuesWant))
precImag := make([]float64, len(valuesWant))
precL2 := make([]float64, len(valuesWant))
for i := range valuesWant {
deltaReal = math.Abs(real(valuesTest[i]) - real(valuesWant[i]))
deltaImag = math.Abs(imag(valuesTest[i]) - imag(valuesWant[i]))
deltaL2 = math.Sqrt(deltaReal*deltaReal + deltaImag*deltaImag)
precReal[i] = math.Log2(1 / deltaReal)
precImag[i] = math.Log2(1 / deltaImag)
precL2[i] = math.Log2(1 / deltaL2)
diff[i].Real = deltaReal
diff[i].Imag = deltaImag
diff[i].L2 = deltaL2
prec.MeanDelta.Real += deltaReal
prec.MeanDelta.Imag += deltaImag
prec.MeanDelta.L2 += deltaL2
if deltaReal > prec.MaxDelta.Real {
prec.MaxDelta.Real = deltaReal
}
if deltaImag > prec.MaxDelta.Imag {
prec.MaxDelta.Imag = deltaImag
}
if deltaL2 > prec.MaxDelta.L2 {
prec.MaxDelta.L2 = deltaL2
}
if deltaReal < prec.MinDelta.Real {
prec.MinDelta.Real = deltaReal
}
if deltaImag < prec.MinDelta.Imag {
prec.MinDelta.Imag = deltaImag
}
if deltaL2 < prec.MinDelta.L2 {
prec.MinDelta.L2 = deltaL2
}
}
prec.calcCDF(precReal, prec.RealDist)
prec.calcCDF(precImag, prec.ImagDist)
prec.calcCDF(precL2, prec.L2Dist)
prec.MinPrecision = deltaToPrecision(prec.MaxDelta)
prec.MaxPrecision = deltaToPrecision(prec.MinDelta)
prec.MeanDelta.Real /= float64(slots)
prec.MeanDelta.Imag /= float64(slots)
prec.MeanDelta.L2 /= float64(slots)
prec.MeanPrecision = deltaToPrecision(prec.MeanDelta)
prec.MedianDelta = calcmedian(diff)
prec.MedianPrecision = deltaToPrecision(prec.MedianDelta)
prec.STDFreq = encoder.GetErrSTDSlotDomain(valuesWant[:], valuesTest[:], params.DefaultScale())
prec.STDTime = encoder.GetErrSTDCoeffDomain(valuesWant, valuesTest, params.DefaultScale())
return prec
}
func deltaToPrecision(c Stats) Stats {
return Stats{math.Log2(1 / c.Real), math.Log2(1 / c.Imag), math.Log2(1 / c.L2)}
}
func (prec *PrecisionStats) calcCDF(precs []float64, res []struct {
Prec float64
Count int
}) {
sortedPrecs := make([]float64, len(precs))
copy(sortedPrecs, precs)
sort.Float64s(sortedPrecs)
minPrec := sortedPrecs[0]
maxPrec := sortedPrecs[len(sortedPrecs)-1]
for i := 0; i < prec.cdfResol; i++ {
curPrec := minPrec + float64(i)*(maxPrec-minPrec)/float64(prec.cdfResol)
for countSmaller, p := range sortedPrecs {
if p >= curPrec {
res[i].Prec = curPrec
res[i].Count = countSmaller
break
}
}
}
}
func calcmedian(values []Stats) (median Stats) {
tmp := make([]float64, len(values))
for i := range values {
tmp[i] = values[i].Real
}
sort.Float64s(tmp)
for i := range values {
values[i].Real = tmp[i]
}
for i := range values {
tmp[i] = values[i].Imag
}
sort.Float64s(tmp)
for i := range values {
values[i].Imag = tmp[i]
}
for i := range values {
tmp[i] = values[i].L2
}
sort.Float64s(tmp)
for i := range values {
values[i].L2 = tmp[i]
}
index := len(values) / 2
if len(values)&1 == 1 || index+1 == len(values) {
return Stats{values[index].Real, values[index].Imag, values[index].L2}
}
return Stats{(values[index-1].Real + values[index].Real) / 2,
(values[index-1].Imag + values[index].Imag) / 2,
(values[index-1].L2 + values[index].L2) / 2}
}