forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptor.go
637 lines (526 loc) · 18.7 KB
/
encryptor.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package rlwe
import (
"fmt"
"reflect"
"github.com/jzhchu/lattigo/ring"
"github.com/jzhchu/lattigo/rlwe/ringqp"
"github.com/jzhchu/lattigo/utils"
)
// Encryptor a generic RLWE encryption interface.
type Encryptor interface {
Encrypt(pt *Plaintext, ct interface{})
EncryptZero(ct interface{})
EncryptZeroNew(level int) (ct *Ciphertext)
EncryptNew(pt *Plaintext) (ct *Ciphertext)
ShallowCopy() Encryptor
WithKey(key interface{}) Encryptor
}
// PRNGEncryptor is an interface for encrypting RLWE ciphertexts from a secret-key and
// a pre-determined PRNG. An Encryptor constructed from a secret-key complies to this
// interface.
type PRNGEncryptor interface {
Encryptor
WithPRNG(prng utils.PRNG) PRNGEncryptor
}
type LPEncryptor interface {
Encryptor
MarshalEncParams() ([]byte, []byte, []byte)
}
type encryptorBase struct {
params Parameters
*encryptorBuffers
prng utils.PRNG
gaussianSampler *ring.GaussianSampler
ternarySampler *ring.TernarySampler
basisextender *ring.BasisExtender
}
type pkEncryptor struct {
*encryptorBase
pk *PublicKey
}
type skEncryptor struct {
encryptorBase
sk *SecretKey
uniformSampler ringqp.UniformSampler
}
type lpkEncryptor struct {
*encryptorBase
pk *PublicKey
u *ring.Poly
e1 *ring.Poly
e2 *ring.Poly
}
// NewEncryptor creates a new Encryptor
// Accepts either a secret-key or a public-key.
func NewEncryptor(params Parameters, key interface{}) Encryptor {
switch key := key.(type) {
case *PublicKey, PublicKey:
return newPkEncryptor(params, key)
case *SecretKey, SecretKey:
return newSkEncryptor(params, key)
default:
panic("cannot NewEncryptor: key must be either *rlwe.PublicKey or *rlwe.SecretKey")
}
}
// NewPRNGEncryptor creates a new PRNGEncryptor instance.
func NewPRNGEncryptor(params Parameters, key *SecretKey) PRNGEncryptor {
return newSkEncryptor(params, key)
}
func NewLPEncryptor(params Parameters, key *PublicKey) LPEncryptor {
return newLpkEncryptor(params, key)
}
func newEncryptorBase(params Parameters) *encryptorBase {
prng, err := utils.NewPRNG()
if err != nil {
panic(err)
}
var bc *ring.BasisExtender
if params.PCount() != 0 {
bc = ring.NewBasisExtender(params.RingQ(), params.RingP())
}
return &encryptorBase{
params: params,
prng: prng,
gaussianSampler: ring.NewGaussianSampler(prng, params.RingQ(), params.Sigma(), int(6*params.Sigma())),
ternarySampler: ring.NewTernarySamplerWithHammingWeight(prng, params.ringQ, params.h, false),
encryptorBuffers: newEncryptorBuffers(params),
basisextender: bc,
}
}
func newSkEncryptor(params Parameters, key interface{}) (enc *skEncryptor) {
prng, err := utils.NewPRNG()
if err != nil {
panic(fmt.Errorf("cannot newSkEncryptor: could not create PRNG for symmetric encryptor: %s", err))
}
enc = &skEncryptor{*newEncryptorBase(params), nil, ringqp.NewUniformSampler(prng, *params.RingQP())}
if enc.sk, err = enc.checkSk(key); err != nil {
panic(err)
}
return enc
}
func newPkEncryptor(params Parameters, key interface{}) (enc *pkEncryptor) {
var err error
enc = &pkEncryptor{newEncryptorBase(params), nil}
enc.pk, err = enc.checkPk(key)
if err != nil {
panic(err)
}
return enc
}
func newLpkEncryptor(params Parameters, key *PublicKey) (enc *lpkEncryptor) {
enc = new(lpkEncryptor)
enc.encryptorBase = newEncryptorBase(params)
enc.pk, _ = enc.checkPk(key)
ringQ := params.RingQ()
enc.u = ringQ.NewPoly()
enc.e1 = ringQ.NewPoly()
enc.e2 = ringQ.NewPoly()
return enc
}
type encryptorBuffers struct {
buffQ [2]*ring.Poly
buffP [3]*ring.Poly
buffQP ringqp.Poly
}
func newEncryptorBuffers(params Parameters) *encryptorBuffers {
ringQ := params.RingQ()
ringP := params.RingP()
var buffP [3]*ring.Poly
if params.PCount() != 0 {
buffP = [3]*ring.Poly{ringP.NewPoly(), ringP.NewPoly(), ringP.NewPoly()}
}
return &encryptorBuffers{
buffQ: [2]*ring.Poly{ringQ.NewPoly(), ringQ.NewPoly()},
buffP: buffP,
buffQP: params.RingQP().NewPoly(),
}
}
// Encrypt encrypts the input plaintext using the stored public-key and writes the result on ct.
// The encryption procedure first samples a new encryption of zero under the public-key and
// then adds the Plaintext.
// The encryption procedure depends on the parameters: If the auxiliary modulus P is defined, the
// encryption of zero is sampled in QP before being rescaled by P; otherwise, it is directly sampled in Q.
// The method accepts only *rlwe.Ciphertext as input.
// If a Plaintext is given, then the output Ciphertext MetaData will match the Plaintext MetaData.
func (enc *pkEncryptor) Encrypt(pt *Plaintext, ct interface{}) {
if pt == nil {
enc.EncryptZero(ct)
} else {
switch ct := ct.(type) {
case *Ciphertext:
ct.MetaData = pt.MetaData
enc.EncryptZero(ct)
enc.params.RingQ().AddLvl(ct.Level(), ct.Value[0], pt.Value, ct.Value[0])
default:
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type %s is not supported", reflect.TypeOf(ct)))
}
}
}
// EncryptNew encrypts the input plaintext using the stored public-key and returns the result on a new Ciphertext.
// The encryption procedure first samples a new encryption of zero under the public-key and
// then adds the Plaintext.
// The encryption procedure depends on the parameters: If the auxiliary modulus P is defined, the
// encryption of zero is sampled in QP before being rescaled by P; otherwise, it is directly sampled in Q.
// If a Plaintext is given, then the output ciphertext MetaData will match the Plaintext MetaData.
func (enc *pkEncryptor) EncryptNew(pt *Plaintext) (ct *Ciphertext) {
ct = NewCiphertext(enc.params, 1, pt.Level())
enc.Encrypt(pt, ct)
return
}
// EncryptZeroNew generates an encryption of zero under the stored public-key and returns it on a new Ciphertext.
// The encryption procedure depends on the parameters: If the auxiliary modulus P is defined, the
// encryption of zero is sampled in QP before being rescaled by P; otherwise, it is directly sampled in Q.
// The method accepts only *rlwe.Ciphertext as input.
// The zero encryption is generated according to the given Ciphertext MetaData.
func (enc *pkEncryptor) EncryptZeroNew(level int) (ct *Ciphertext) {
ct = NewCiphertext(enc.params, 1, level)
enc.EncryptZero(ct)
return
}
// EncryptZero generates an encryption of zero under the stored public-key and writes the result on ct.
// The encryption procedure depends on the parameters: If the auxiliary modulus P is defined, the
// encryption of zero is sampled in QP before being rescaled by P; otherwise, it is directly sampled in Q.
// The method accepts only *rlwe.Ciphertext as input.
// The zero encryption is generated according to the given Ciphertext MetaData.
func (enc *pkEncryptor) EncryptZero(ct interface{}) {
switch ct := ct.(type) {
case *Ciphertext:
if enc.params.PCount() > 0 {
enc.encryptZero(ct)
} else {
enc.encryptZeroNoP(ct)
}
default:
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type %s is not supported", reflect.TypeOf(ct)))
}
}
func (enc *pkEncryptor) encryptZero(ct *Ciphertext) {
ringQP := enc.params.RingQP()
levelQ := ct.Level()
levelP := 0
buffQ0 := enc.buffQ[0]
buffP0 := enc.buffP[0]
buffP1 := enc.buffP[1]
buffP2 := enc.buffP[2]
u := ringqp.Poly{Q: buffQ0, P: buffP2}
// We sample a RLWE instance (encryption of zero) over the extended ring (ciphertext ring + special prime)
enc.ternarySampler.ReadLvl(levelQ, u.Q)
ringQP.ExtendBasisSmallNormAndCenter(u.Q, levelP, nil, u.P)
// (#Q + #P) NTT
ringQP.NTTLvl(levelQ, levelP, u, u)
ct0QP := ringqp.Poly{Q: ct.Value[0], P: buffP0}
ct1QP := ringqp.Poly{Q: ct.Value[1], P: buffP1}
// ct0 = u*pk0
// ct1 = u*pk1
ringQP.MulCoeffsMontgomeryLvl(levelQ, levelP, u, enc.pk.Value[0], ct0QP)
ringQP.MulCoeffsMontgomeryLvl(levelQ, levelP, u, enc.pk.Value[1], ct1QP)
// 2*(#Q + #P) NTT
ringQP.InvNTTLvl(levelQ, levelP, ct0QP, ct0QP)
ringQP.InvNTTLvl(levelQ, levelP, ct1QP, ct1QP)
e := ringqp.Poly{Q: buffQ0, P: buffP2}
enc.gaussianSampler.ReadLvl(levelQ, e.Q)
ringQP.ExtendBasisSmallNormAndCenter(e.Q, levelP, nil, e.P)
ringQP.AddLvl(levelQ, levelP, ct0QP, e, ct0QP)
enc.gaussianSampler.ReadLvl(levelQ, e.Q)
ringQP.ExtendBasisSmallNormAndCenter(e.Q, levelP, nil, e.P)
ringQP.AddLvl(levelQ, levelP, ct1QP, e, ct1QP)
// ct0 = (u*pk0 + e0)/P
enc.basisextender.ModDownQPtoQ(levelQ, levelP, ct0QP.Q, ct0QP.P, ct.Value[0])
// ct1 = (u*pk1 + e1)/P
enc.basisextender.ModDownQPtoQ(levelQ, levelP, ct1QP.Q, ct1QP.P, ct.Value[1])
if ct.IsNTT {
ringQP.RingQ.NTTLvl(levelQ, ct.Value[0], ct.Value[0])
ringQP.RingQ.NTTLvl(levelQ, ct.Value[1], ct.Value[1])
}
}
func (enc *pkEncryptor) encryptZeroNoP(ct *Ciphertext) {
ringQ := enc.params.RingQ()
levelQ := ct.Level()
buffQ0 := enc.buffQ[0]
enc.ternarySampler.ReadLvl(levelQ, buffQ0)
ringQ.NTTLvl(levelQ, buffQ0, buffQ0)
c0, c1 := ct.Value[0], ct.Value[1]
// ct0 = NTT(u*pk0)
ringQ.MulCoeffsMontgomeryLvl(levelQ, buffQ0, enc.pk.Value[0].Q, c0)
// ct1 = NTT(u*pk1)
ringQ.MulCoeffsMontgomeryLvl(levelQ, buffQ0, enc.pk.Value[1].Q, c1)
// c0
if ct.IsNTT {
enc.gaussianSampler.ReadLvl(levelQ, buffQ0)
ringQ.NTTLvl(levelQ, buffQ0, buffQ0)
ringQ.AddLvl(levelQ, c0, buffQ0, c0)
} else {
ringQ.InvNTTLvl(levelQ, c0, c0)
enc.gaussianSampler.ReadAndAddLvl(levelQ, c0)
}
// c1
if ct.IsNTT {
enc.gaussianSampler.ReadLvl(levelQ, buffQ0)
ringQ.NTTLvl(levelQ, buffQ0, buffQ0)
ringQ.AddLvl(levelQ, c1, buffQ0, c1)
} else {
ringQ.InvNTTLvl(levelQ, c1, c1)
enc.gaussianSampler.ReadAndAddLvl(levelQ, c1)
}
}
// Encrypt encrypts the input plaintext using the stored secret-key and writes the result on ct.
// The method accepts only *rlwe.Ciphertext or *rgsw.Ciphertext as input and will panic otherwise.
// If a plaintext is given, the encryptor only accepts *rlwe.Ciphertext, and the generated Ciphertext
// MetaData will match the given Plaintext MetaData.
func (enc *skEncryptor) Encrypt(pt *Plaintext, ct interface{}) {
if pt == nil {
enc.EncryptZero(ct)
} else {
switch ct := ct.(type) {
case *Ciphertext:
ct.MetaData = pt.MetaData
ct.Resize(ct.Degree(), utils.MinInt(pt.Level(), ct.Level()))
enc.EncryptZero(ct)
enc.params.RingQ().AddLvl(ct.Level(), ct.Value[0], pt.Value, ct.Value[0])
default:
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type %s is not supported", reflect.TypeOf(ct)))
}
}
}
// Encrypt encrypts the input plaintext using the stored secret-key and returns the result on a new Ciphertext.
// MetaData will match the given Plaintext MetaData.
func (enc *skEncryptor) EncryptNew(pt *Plaintext) (ct *Ciphertext) {
ct = NewCiphertext(enc.params, 1, pt.Level())
enc.Encrypt(pt, ct)
return
}
// EncryptZero generates an encryption of zero using the stored secret-key and writes the result on ct.
// The method accepts only *rlwe.Ciphertext or *rgsw.Ciphertext as input and will panic otherwise.
// The zero encryption is generated according to the given Ciphertext MetaData.
func (enc *skEncryptor) EncryptZero(ct interface{}) {
switch ct := ct.(type) {
case *Ciphertext:
var c1 *ring.Poly
if ct.Degree() == 1 {
c1 = ct.Value[1]
} else {
c1 = enc.buffQ[1]
}
enc.uniformSampler.ReadLvl(ct.Level(), -1, ringqp.Poly{Q: c1})
enc.encryptZero(ct, c1)
case *CiphertextQP:
enc.encryptZeroQP(*ct)
default:
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type %s is not supported", reflect.TypeOf(ct)))
}
}
// EncryptZeroNew generates an encryption of zero using the stored secret-key and writes the result on ct.
// The method accepts only *rlwe.Ciphertext or *rgsw.Ciphertext as input and will panic otherwise.
// The zero encryption is generated according to the given Ciphertext MetaData.
func (enc *skEncryptor) EncryptZeroNew(level int) (ct *Ciphertext) {
ct = NewCiphertext(enc.params, 1, level)
enc.EncryptZero(ct)
return
}
func (enc *skEncryptor) encryptZero(ct *Ciphertext, c1 *ring.Poly) {
ringQ := enc.params.RingQ()
levelQ := ct.Level()
c0 := ct.Value[0]
ringQ.MulCoeffsMontgomeryLvl(levelQ, c1, enc.sk.Value.Q, c0) // c0 = NTT(sc1)
ringQ.NegLvl(levelQ, c0, c0) // c0 = NTT(-sc1)
if ct.IsNTT {
enc.gaussianSampler.ReadLvl(levelQ, enc.buffQ[0]) // e
ringQ.NTTLvl(levelQ, enc.buffQ[0], enc.buffQ[0]) // NTT(e)
ringQ.AddLvl(levelQ, c0, enc.buffQ[0], c0) // c0 = NTT(-sc1 + e)
} else {
ringQ.InvNTTLvl(levelQ, c0, c0) // c0 = -sc1
if ct.Degree() == 1 {
ringQ.InvNTTLvl(levelQ, c1, c1) // c1 = c1
}
enc.gaussianSampler.ReadAndAddLvl(levelQ, c0) // c0 = -sc1 + e
}
}
// EncryptZeroSeeded generates en encryption of zero under sk.
// levelQ : level of the modulus Q
// levelP : level of the modulus P
// sk : secret key
// sampler: uniform sampler; if `sampler` is nil, then the internal sampler will be used.
// montgomery: returns the result in the Montgomery domain.
func (enc *skEncryptor) encryptZeroQP(ct CiphertextQP) {
c0, c1 := ct.Value[0], ct.Value[1]
levelQ, levelP := c0.LevelQ(), c1.LevelP()
ringQP := enc.params.RingQP()
// ct = (e, 0)
enc.gaussianSampler.ReadLvl(levelQ, c0.Q)
if levelP != -1 {
ringQP.ExtendBasisSmallNormAndCenter(c0.Q, levelP, nil, c0.P)
}
ringQP.NTTLvl(levelQ, levelP, c0, c0)
// ct[1] is assumed to be sampled in of the Montgomery domain,
// thus -as will also be in the Montgomery domain (s is by default), therefore 'e'
// must be switched to the Montgomery domain.
ringQP.MFormLvl(levelQ, levelP, c0, c0)
// ct = (e, a)
enc.uniformSampler.ReadLvl(levelQ, levelP, c1)
// (-a*sk + e, a)
ringQP.MulCoeffsMontgomeryAndSubLvl(levelQ, levelP, c1, enc.sk.Value, c0)
if !ct.IsNTT {
ringQP.InvNTTLvl(levelQ, levelP, c0, c0)
ringQP.InvNTTLvl(levelQ, levelP, c1, c1)
}
}
// ShallowCopy creates a shallow copy of this skEncryptor in which all the read-only data-structures are
// shared with the receiver and the temporary buffers are reallocated. The receiver and the returned
// Encryptors can be used concurrently.
func (enc *pkEncryptor) ShallowCopy() Encryptor {
return NewEncryptor(enc.params, enc.pk)
}
// ShallowCopy creates a shallow copy of this skEncryptor in which all the read-only data-structures are
// shared with the receiver and the temporary buffers are reallocated. The receiver and the returned
// Encryptors can be used concurrently.
func (enc *skEncryptor) ShallowCopy() Encryptor {
return NewEncryptor(enc.params, enc.sk)
}
// WithKey returns this encryptor with a new key.
func (enc *skEncryptor) WithKey(key interface{}) Encryptor {
skPtr, err := enc.checkSk(key)
if err != nil {
panic(err)
}
return &skEncryptor{enc.encryptorBase, skPtr, enc.uniformSampler}
}
// WithKey returns this encryptor with a new key.
func (enc *pkEncryptor) WithKey(key interface{}) Encryptor {
pkPtr, err := enc.checkPk(key)
if err != nil {
panic(err)
}
return &pkEncryptor{enc.encryptorBase, pkPtr}
}
// WithPRNG returns this encryptor with prng as its source of randomness for the uniform
// element c1.
func (enc skEncryptor) WithPRNG(prng utils.PRNG) PRNGEncryptor {
return &skEncryptor{enc.encryptorBase, enc.sk, enc.uniformSampler.WithPRNG(prng)}
}
func (enc *lpkEncryptor) Encrypt(pt *Plaintext, ct interface{}) {
if pt == nil {
enc.EncryptZero(ct)
} else {
switch ct := ct.(type) {
case *Ciphertext:
ct.MetaData = pt.MetaData
enc.EncryptZero(ct)
enc.params.RingQ().AddLvl(ct.Level(), ct.Value[0], pt.Value, ct.Value[0])
default:
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type %s is not supported", reflect.TypeOf(ct)))
}
}
}
func (enc *lpkEncryptor) EncryptZero(ct interface{}) {
switch ct := ct.(type) {
case *Ciphertext:
if enc.params.PCount() > 0 {
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type contains p"))
} else {
enc.encryptZeroNoP(ct)
}
default:
panic(fmt.Sprintf("cannot Encrypt: input ciphertext type %s is not supported", reflect.TypeOf(ct)))
}
}
func (enc *lpkEncryptor) encryptZeroNoP(ct *Ciphertext) {
ringQ := enc.params.RingQ()
levelQ := ct.Level()
buffQ0 := enc.buffQ[0]
enc.ternarySampler.ReadLvl(levelQ, buffQ0)
ringQ.NTTLvl(levelQ, buffQ0, buffQ0)
c0, c1 := ct.Value[0], ct.Value[1]
ringQ.MulCoeffsMontgomeryLvl(levelQ, buffQ0, enc.pk.Value[0].Q, c0)
ringQ.MulCoeffsMontgomeryLvl(levelQ, buffQ0, enc.pk.Value[1].Q, c1)
enc.u = buffQ0.CopyNew()
ringQ.InvNTTLvl(levelQ, enc.u, enc.u)
if ct.IsNTT {
enc.gaussianSampler.ReadLvl(levelQ, buffQ0)
enc.e1 = buffQ0.CopyNew()
ringQ.InvNTTLvl(levelQ, enc.e1, enc.e1)
ringQ.NTTLvl(levelQ, buffQ0, buffQ0)
ringQ.AddLvl(levelQ, c0, buffQ0, c0)
} else {
ringQ.InvNTTLvl(levelQ, c0, c0)
nc0 := c0.CopyNew()
enc.gaussianSampler.ReadAndAddLvl(levelQ, c0)
enc.e1 = enc.readAndSub(c0, nc0)
}
if ct.IsNTT {
enc.gaussianSampler.ReadLvl(levelQ, buffQ0)
enc.e2 = buffQ0.CopyNew()
ringQ.InvNTTLvl(levelQ, enc.e2, enc.e2)
ringQ.NTTLvl(levelQ, buffQ0, buffQ0)
ringQ.AddLvl(levelQ, c1, buffQ0, c1)
} else {
ringQ.InvNTTLvl(levelQ, c1, c1)
nc1 := c1.CopyNew()
enc.gaussianSampler.ReadAndAddLvl(levelQ, c1)
enc.e2 = enc.readAndSub(c1, nc1)
}
}
func (enc *lpkEncryptor) readAndSub(pol1, pol2 *ring.Poly) (res *ring.Poly) {
res = pol1.CopyNew()
for i := 0; i < pol1.N(); i++ {
for j, qi := range enc.params.Q() {
res.Coeffs[j][i] = subMod(pol1.Coeffs[j][i], pol2.Coeffs[j][i], qi)
}
}
return
}
func (enc *lpkEncryptor) EncryptZeroNew(level int) (ct *Ciphertext) {
ct = NewCiphertext(enc.params, 1, level)
enc.EncryptZero(ct)
return
}
func (enc *lpkEncryptor) EncryptNew(pt *Plaintext) (ct *Ciphertext) {
ct = NewCiphertext(enc.params, 1, pt.Level())
enc.Encrypt(pt, ct)
return
}
func (enc *lpkEncryptor) ShallowCopy() Encryptor {
return NewLPEncryptor(enc.params, enc.pk)
}
func (enc *lpkEncryptor) WithKey(key interface{}) Encryptor {
pkPtr, err := enc.checkPk(key)
if err != nil {
panic(err)
}
return &lpkEncryptor{enc.encryptorBase, pkPtr, nil, nil, nil}
}
func (enc *lpkEncryptor) MarshalEncParams() ([]byte, []byte, []byte) {
uBytes, _ := enc.u.MarshalBinary()
e1Bytes, _ := enc.e1.MarshalBinary()
e2Bytes, _ := enc.e2.MarshalBinary()
return uBytes, e1Bytes, e2Bytes
}
// checkPk checks that a given pk is correct for the parameters.
func (enc encryptorBase) checkPk(key interface{}) (pk *PublicKey, err error) {
switch key := key.(type) {
case PublicKey:
pk = &key
case *PublicKey:
pk = key
default:
return nil, fmt.Errorf("key is not a valid public key type %T", key)
}
if pk.Value[0].Q.N() != enc.params.N() || pk.Value[1].Q.N() != enc.params.N() {
return nil, fmt.Errorf("pk ring degree does not match params ring degree")
}
return pk, nil
}
// checkPk checks that a given pk is correct for the parameters.
func (enc encryptorBase) checkSk(key interface{}) (sk *SecretKey, err error) {
switch key := key.(type) {
case SecretKey:
sk = &key
case *SecretKey:
sk = key
default:
return nil, fmt.Errorf("key is not a valid public key type %T", key)
}
if sk.Value.Q.N() != enc.params.N() {
panic("cannot checkSk: sk ring degree does not match params ring degree")
}
return sk, nil
}