forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gadget.go
340 lines (271 loc) · 8.19 KB
/
gadget.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
337
338
339
340
package rlwe
import (
"github.com/jzhchu/lattigo/ring"
"github.com/jzhchu/lattigo/rlwe/ringqp"
)
// GadgetCiphertext is a struct for storing an encrypted
// plaintext times the gadget power matrix.
type GadgetCiphertext struct {
Value [][]CiphertextQP
}
// NewGadgetCiphertext returns a new Ciphertext key with pre-allocated zero-value.
// Ciphertext is always in the NTT domain.
func NewGadgetCiphertext(levelQ, levelP, decompRNS, decompBIT int, ringQP ringqp.Ring) (ct *GadgetCiphertext) {
ct = new(GadgetCiphertext)
ct.Value = make([][]CiphertextQP, decompRNS)
for i := 0; i < decompRNS; i++ {
ct.Value[i] = make([]CiphertextQP, decompBIT)
for j := 0; j < decompBIT; j++ {
ct.Value[i][j].Value[0] = ringQP.NewPolyLvl(levelQ, levelP)
ct.Value[i][j].Value[1] = ringQP.NewPolyLvl(levelQ, levelP)
ct.Value[i][j].IsNTT = true
ct.Value[i][j].IsMontgomery = true
}
}
return ct
}
// LevelQ returns the level of the modulus Q of the target Ciphertext.
func (ct *GadgetCiphertext) LevelQ() int {
return ct.Value[0][0].Value[0].Q.Level()
}
// LevelP returns the level of the modulus P of the target Ciphertext.
func (ct *GadgetCiphertext) LevelP() int {
if ct.Value[0][0].Value[0].P != nil {
return ct.Value[0][0].Value[0].P.Level()
}
return -1
}
// Equals checks two Ciphertexts for equality.
func (ct *GadgetCiphertext) Equals(other *GadgetCiphertext) bool {
if ct == other {
return true
}
if (ct == nil) != (other == nil) {
return false
}
if len(ct.Value) != len(other.Value) {
return false
}
if len(ct.Value[0]) != len(other.Value[0]) {
return false
}
for i := range ct.Value {
for j, pol := range ct.Value[i] {
if !pol.Value[0].Equals(other.Value[i][j].Value[0]) && !pol.Value[1].Equals(other.Value[i][j].Value[1]) {
return false
}
}
}
return true
}
// CopyNew creates a deep copy of the receiver Ciphertext and returns it.
func (ct *GadgetCiphertext) CopyNew() (ctCopy *GadgetCiphertext) {
if ct == nil || len(ct.Value) == 0 {
return nil
}
v := make([][]CiphertextQP, len(ct.Value))
for i := range ct.Value {
v[i] = make([]CiphertextQP, len(ct.Value[0]))
for j, el := range ct.Value[i] {
v[i][j] = *el.CopyNew()
}
}
return &GadgetCiphertext{Value: v}
}
// MarshalBinarySize returns the length in bytes of the target GadgetCiphertext.
func (ct *GadgetCiphertext) MarshalBinarySize() (dataLen int) {
dataLen = 2
for i := range ct.Value {
for _, el := range ct.Value[i] {
dataLen += el.MarshalBinarySize()
}
}
return
}
// MarshalBinary encodes the target Ciphertext on a slice of bytes.
func (ct *GadgetCiphertext) MarshalBinary() (data []byte, err error) {
data = make([]byte, ct.MarshalBinarySize())
if _, err = ct.Encode(data); err != nil {
return
}
return
}
// UnmarshalBinary decodes a slice of bytes on the target Ciphertext.
func (ct *GadgetCiphertext) UnmarshalBinary(data []byte) (err error) {
if _, err = ct.Decode(data); err != nil {
return
}
return
}
// Encode encodes the target ciphertext on a pre-allocated slice of bytes.
func (ct *GadgetCiphertext) Encode(data []byte) (ptr int, err error) {
var inc int
data[ptr] = uint8(len(ct.Value))
ptr++
data[ptr] = uint8(len(ct.Value[0]))
ptr++
for i := range ct.Value {
for _, el := range ct.Value[i] {
if inc, err = el.Encode64(data[ptr:]); err != nil {
return ptr, err
}
ptr += inc
}
}
return
}
// Decode decodes a slice of bytes on the target ciphertext.
func (ct *GadgetCiphertext) Decode(data []byte) (ptr int, err error) {
decompRNS := int(data[0])
decompBIT := int(data[1])
ptr = 2
ct.Value = make([][]CiphertextQP, decompRNS)
var inc int
for i := range ct.Value {
ct.Value[i] = make([]CiphertextQP, decompBIT)
for j := range ct.Value[i] {
if inc, err = ct.Value[i][j].Decode64(data[ptr:]); err != nil {
return
}
ptr += inc
}
}
return
}
// AddPolyTimesGadgetVectorToGadgetCiphertext takes a plaintext polynomial and a list of Ciphertexts and adds the
// plaintext times the RNS and BIT decomposition to the i-th element of the i-th Ciphertexts. This method panics if
// len(cts) > 2.
func AddPolyTimesGadgetVectorToGadgetCiphertext(pt *ring.Poly, cts []GadgetCiphertext, ringQP ringqp.Ring, logbase2 int, buff *ring.Poly) {
ringQ := ringQP.RingQ
levelQ := cts[0].LevelQ()
levelP := cts[0].LevelP()
if len(cts) > 2 {
panic("cannot AddPolyTimesGadgetVectorToGadgetCiphertext: len(cts) should be <= 2")
}
if levelP != -1 {
ringQ.MulScalarBigintLvl(levelQ, pt, ringQP.RingP.ModulusAtLevel[levelP], buff) // P * pt
} else {
levelP = 0
if pt != buff {
ring.CopyLvl(levelQ, pt, buff) // 1 * pt
}
}
RNSDecomp := len(cts[0].Value)
BITDecomp := len(cts[0].Value[0])
var index int
for j := 0; j < BITDecomp; j++ {
for i := 0; i < RNSDecomp; i++ {
// e + (m * P * w^2j) * (q_star * q_tild) mod QP
//
// q_prod = prod(q[i*#Pi+j])
// q_star = Q/qprod
// q_tild = q_star^-1 mod q_prod
//
// Therefore : (pt * P * w^2j) * (q_star * q_tild) = pt*P*w^2j mod q[i*#Pi+j], else 0
for k := 0; k < levelP+1; k++ {
index = i*(levelP+1) + k
// Handle cases where #pj does not divide #qi
if index >= levelQ+1 {
break
}
qi := ringQ.Modulus[index]
p0tmp := buff.Coeffs[index]
for u, ct := range cts {
p1tmp := ct.Value[i][j].Value[u].Q.Coeffs[index]
for w := 0; w < ringQ.N; w++ {
p1tmp[w] = ring.CRed(p1tmp[w]+p0tmp[w], qi)
}
}
}
}
// w^2j
ringQ.MulScalar(buff, 1<<logbase2, buff)
}
}
// AddPolyToGadgetMatrix takes a plaintext polynomial and a list of ringqp.Poly and adds the
// plaintext times the RNS and BIT decomposition to the list of ringqp.Poly.
func AddPolyToGadgetMatrix(pt *ring.Poly, gm [][]ringqp.Poly, ringQP ringqp.Ring, logbase2 int, buff *ring.Poly) {
ringQ := ringQP.RingQ
levelQ := gm[0][0].LevelQ()
levelP := gm[0][0].LevelP()
if levelP != -1 {
ringQ.MulScalarBigintLvl(levelQ, pt, ringQP.RingP.ModulusAtLevel[levelP], buff) // P * pt
} else {
levelP = 0
if pt != buff {
ring.CopyLvl(levelQ, pt, buff) // 1 * pt
}
}
RNSDecomp := len(gm)
BITDecomp := len(gm[0])
var index int
for j := 0; j < BITDecomp; j++ {
for i := 0; i < RNSDecomp; i++ {
// e + (m * P * w^2j) * (q_star * q_tild) mod QP
//
// q_prod = prod(q[i*#Pi+j])
// q_star = Q/qprod
// q_tild = q_star^-1 mod q_prod
//
// Therefore : (pt * P * w^2j) * (q_star * q_tild) = pt*P*w^2j mod q[i*#Pi+j], else 0
for k := 0; k < levelP+1; k++ {
index = i*(levelP+1) + k
// Handle cases where #pj does not divide #qi
if index >= levelQ+1 {
break
}
qi := ringQ.Modulus[index]
p0tmp := buff.Coeffs[index]
p1tmp := gm[i][j].Q.Coeffs[index]
for w := 0; w < ringQ.N; w++ {
p1tmp[w] = ring.CRed(p1tmp[w]+p0tmp[w], qi)
}
}
}
// w^2j
ringQ.MulScalar(buff, 1<<logbase2, buff)
}
}
// GadgetPlaintext stores a RGSW plaintext value.
type GadgetPlaintext struct {
Value []*ring.Poly
}
// NewGadgetPlaintext creates a new gadget plaintext from value, which can be either uint64, int64 or *ring.Poly.
// Plaintext is returned in the NTT and Mongtomery domain.
func NewGadgetPlaintext(value interface{}, levelQ, levelP, logBase2, decompBIT int, ringQP ringqp.Ring) (pt *GadgetPlaintext) {
ringQ := ringQP.RingQ
pt = new(GadgetPlaintext)
pt.Value = make([]*ring.Poly, decompBIT)
switch el := value.(type) {
case uint64:
pt.Value[0] = ringQ.NewPolyLvl(levelQ)
for i := range ringQ.Modulus[:levelQ+1] {
pt.Value[0].Coeffs[i][0] = el
}
case int64:
pt.Value[0] = ringQ.NewPolyLvl(levelQ)
if el < 0 {
for i, qi := range ringQ.Modulus[:levelQ+1] {
pt.Value[0].Coeffs[i][0] = qi - uint64(-el)
}
} else {
for i := range ringQ.Modulus[:levelQ+1] {
pt.Value[0].Coeffs[i][0] = uint64(el)
}
}
case *ring.Poly:
pt.Value[0] = el.CopyNew()
default:
panic("cannot NewGadgetPlaintext: unsupported type, must be wither uint64 or *ring.Poly")
}
if levelP > -1 {
ringQ.MulScalarBigintLvl(levelQ, pt.Value[0], ringQP.RingP.ModulusAtLevel[levelP], pt.Value[0])
}
ringQ.NTTLvl(levelQ, pt.Value[0], pt.Value[0])
ringQ.MFormLvl(levelQ, pt.Value[0], pt.Value[0])
for i := 1; i < len(pt.Value); i++ {
pt.Value[i] = pt.Value[0].CopyNew()
ringQ.MulByPow2Lvl(levelQ, pt.Value[i], i*logBase2, pt.Value[i])
}
return
}