forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
283 lines (213 loc) · 7.74 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
package rlwe
import (
"math"
"math/big"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/utils"
)
// NoisePublicKey returns the log2 of the standard deviation of the input public-key with respect to the given secret-key and parameters.
func NoisePublicKey(pk *PublicKey, sk *SecretKey, params Parameters) float64 {
pk = pk.CopyNew()
ringQP := params.RingQP().AtLevel(pk.LevelQ(), pk.LevelP())
// [-as + e] + [as]
ringQP.MulCoeffsMontgomeryThenAdd(sk.Value, pk.Value[1], pk.Value[0])
ringQP.INTT(pk.Value[0], pk.Value[0])
ringQP.IMForm(pk.Value[0], pk.Value[0])
return ringQP.Log2OfStandardDeviation(pk.Value[0])
}
// NoiseRelinearizationKey the log2 of the standard deviation of the noise of the input relinearization key with respect to the given secret-key and paramters.
func NoiseRelinearizationKey(rlk *RelinearizationKey, sk *SecretKey, params Parameters) float64 {
sk2 := sk.CopyNew()
params.RingQP().AtLevel(rlk.LevelQ(), rlk.LevelP()).MulCoeffsMontgomery(sk2.Value, sk2.Value, sk2.Value)
return NoiseEvaluationKey(&rlk.EvaluationKey, sk2, sk, params)
}
// NoiseGaloisKey the log2 of the standard deviation of the noise of the input Galois key key with respect to the given secret-key and paramters.
func NoiseGaloisKey(gk *GaloisKey, sk *SecretKey, params Parameters) float64 {
skIn := sk.CopyNew()
skOut := sk.CopyNew()
nthRoot := params.RingQ().NthRoot()
galElInv := ring.ModExp(gk.GaloisElement, nthRoot-1, nthRoot)
params.RingQP().AtLevel(gk.LevelQ(), gk.LevelP()).AutomorphismNTT(sk.Value, galElInv, skOut.Value)
return NoiseEvaluationKey(&gk.EvaluationKey, skIn, skOut, params)
}
// NoiseGadgetCiphertext returns the log2 of the standard devaition of the noise of the input gadget ciphertext with respect to the given plaintext, secret-key and parameters.
// The polynomial pt is expected to be in the NTT and Montgomery domain.
func NoiseGadgetCiphertext(gct *GadgetCiphertext, pt ring.Poly, sk *SecretKey, params Parameters) float64 {
gct = gct.CopyNew()
pt = *pt.CopyNew()
levelQ, levelP := gct.LevelQ(), gct.LevelP()
ringQP := params.RingQP().AtLevel(levelQ, levelP)
ringQ, ringP := ringQP.RingQ, ringQP.RingP
BaseTwoDecompositionVectorSize := utils.MinSlice(gct.BaseTwoDecompositionVectorSize()) // required else the check becomes very complicated
// Decrypts
// [-asIn + w*P*sOut + e, a] + [asIn]
for i := range gct.Value {
for j := range gct.Value[i] {
ringQP.MulCoeffsMontgomeryThenAdd(gct.Value[i][j][1], sk.Value, gct.Value[i][j][0])
}
}
// Sums all bases together (equivalent to multiplying with CRT decomposition of 1)
// sum([1]_w * [RNS*PW2*P*sOut + e]) = PWw*P*sOut + sum(e)
for i := range gct.Value { // RNS decomp
if i > 0 {
for j := range gct.Value[i] { // PW2 decomp
ringQP.Add(gct.Value[0][j][0], gct.Value[i][j][0], gct.Value[0][j][0])
}
}
}
if levelP != -1 {
// sOut * P
ringQ.MulScalarBigint(pt, ringP.ModulusAtLevel[levelP], pt)
}
var maxLog2Std float64
for i := 0; i < BaseTwoDecompositionVectorSize; i++ {
// P*s^i + sum(e) - P*s^i = sum(e)
ringQ.Sub(gct.Value[0][i][0].Q, pt, gct.Value[0][i][0].Q)
// Checks that the error is below the bound
// Worst error bound is N * floor(6*sigma) * #Keys
ringQP.INTT(gct.Value[0][i][0], gct.Value[0][i][0])
ringQP.IMForm(gct.Value[0][i][0], gct.Value[0][i][0])
maxLog2Std = utils.Max(maxLog2Std, ringQP.Log2OfStandardDeviation(gct.Value[0][i][0]))
// sOut * P * PW2
ringQ.MulScalar(pt, 1<<gct.BaseTwoDecomposition, pt)
}
return maxLog2Std
}
// NoiseEvaluationKey the log2 of the standard deviation of the noise of the input Galois key key with respect to the given secret-key and paramters.
func NoiseEvaluationKey(evk *EvaluationKey, skIn, skOut *SecretKey, params Parameters) float64 {
return NoiseGadgetCiphertext(&evk.GadgetCiphertext, skIn.Value.Q, skOut, params)
}
// Norm returns the log2 of the standard deviation, minimum and maximum absolute norm of
// the decrypted Ciphertext, before the decoding (i.e. including the error).
func Norm(ct *Ciphertext, dec *Decryptor) (std, min, max float64) {
params := dec.params
coeffsBigint := make([]*big.Int, params.N())
for i := range coeffsBigint {
coeffsBigint[i] = new(big.Int)
}
pt := NewPlaintext(params, ct.Level())
dec.Decrypt(ct, pt)
ringQ := params.RingQ().AtLevel(ct.Level())
if pt.IsNTT {
ringQ.INTT(pt.Value, pt.Value)
}
ringQ.PolyToBigintCentered(pt.Value, 1, coeffsBigint)
return NormStats(coeffsBigint)
}
func NormStats(vec []*big.Int) (float64, float64, float64) {
vecfloat := make([]*big.Float, len(vec))
minErr := new(big.Float).SetFloat64(0)
maxErr := new(big.Float).SetFloat64(0)
tmp := new(big.Float)
minErr.SetInt(vec[0])
minErr.Abs(minErr)
for i := range vec {
vecfloat[i] = new(big.Float)
vecfloat[i].SetInt(vec[i])
tmp.Abs(vecfloat[i])
if minErr.Cmp(tmp) == 1 {
minErr.Set(tmp)
}
if maxErr.Cmp(tmp) == -1 {
maxErr.Set(tmp)
}
}
n := new(big.Float).SetFloat64(float64(len(vec)))
mean := new(big.Float).SetFloat64(0)
for _, c := range vecfloat {
mean.Add(mean, c)
}
mean.Quo(mean, n)
err := new(big.Float).SetFloat64(0)
for _, c := range vecfloat {
tmp.Sub(c, mean)
tmp.Mul(tmp, tmp)
err.Add(err, tmp)
}
err.Quo(err, n)
err.Sqrt(err)
x, _ := err.Float64()
y, _ := minErr.Float64()
z, _ := maxErr.Float64()
return math.Log2(x), math.Log2(y), math.Log2(z)
}
// NTTSparseAndMontgomery takes a polynomial Z[Y] outside of the NTT domain and maps it to a polynomial Z[X] in the NTT domain where Y = X^(gap).
// This method is used to accelerate the NTT of polynomials that encode sparse polynomials.
func NTTSparseAndMontgomery(r *ring.Ring, metadata *MetaData, pol ring.Poly) {
if 1<<metadata.LogDimensions.Cols == r.NthRoot()>>2 {
if metadata.IsNTT {
r.NTT(pol, pol)
}
if metadata.IsMontgomery {
r.MForm(pol, pol)
}
} else {
var n int
var NTT func(p1, p2 []uint64, N int, Q, QInv uint64, BRedConstant, nttPsi []uint64)
switch r.Type() {
case ring.Standard:
n = 2 << metadata.LogDimensions.Cols
NTT = ring.NTTStandard
case ring.ConjugateInvariant:
n = 1 << metadata.LogDimensions.Cols
NTT = ring.NTTConjugateInvariant
}
N := r.N()
gap := N / n
for i, s := range r.SubRings[:r.Level()+1] {
coeffs := pol.Coeffs[i]
if metadata.IsMontgomery {
s.MForm(coeffs[:n], coeffs[:n])
}
if metadata.IsNTT {
// NTT in dimension n but with roots of N
// This is a small hack to perform at reduced cost an NTT of dimension N on a vector in Y = X^{N/n}, i.e. sparse polynomials.
NTT(coeffs[:n], coeffs[:n], n, s.Modulus, s.MRedConstant, s.BRedConstant, s.RootsForward)
// 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
}
}
} else {
for j := n - 1; j >= 0; j-- {
coeffs[j*gap] = coeffs[j]
for j := 1; j < gap; j++ {
coeffs[j*gap-j] = 0
}
}
}
}
}
}
// ExtendBasisSmallNormAndCenterNTTMontgomery extends a small-norm polynomial polQ in R_Q to a polynomial
// polP in R_P.
// This method can be used to extend from Q0 to QL.
// Input and output are in the NTT and Montgomery domain.
func ExtendBasisSmallNormAndCenterNTTMontgomery(rQ, rP *ring.Ring, polQ, buff, polP ring.Poly) {
rQ = rQ.AtLevel(0)
levelP := rP.Level()
// Switches Q[0] out of the NTT and Montgomery domain.
rQ.INTT(polQ, buff)
rQ.IMForm(buff, buff)
// Reconstruct P from Q
Q := rQ.SubRings[0].Modulus
QHalf := Q >> 1
P := rP.ModuliChain()
N := rQ.N()
var sign uint64
for j := 0; j < N; j++ {
coeff := buff.Coeffs[0][j]
sign = 1
if coeff > QHalf {
coeff = Q - coeff
sign = 0
}
for i := 0; i < levelP+1; i++ {
polP.Coeffs[i][j] = (coeff * sign) | (P[i]-coeff)*(sign^1)
}
}
rP.NTT(polP, polP)
rP.MForm(polP, polP)
}