-
Notifications
You must be signed in to change notification settings - Fork 1
/
encryptor.go
68 lines (59 loc) · 3.2 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
package ckks
import (
"github.com/dwkim606/test_lattigo/ring"
"github.com/dwkim606/test_lattigo/rlwe"
)
// Encryptor an encryption interface for the CKKS scheme.
type Encryptor interface {
Encrypt(plaintext *Plaintext, ciphertext *Ciphertext)
EncryptNew(plaintext *Plaintext) *Ciphertext
EncryptFromCRP(plaintext *Plaintext, crp *ring.Poly, ciphertext *Ciphertext)
EncryptFromCRPNew(plaintext *Plaintext, crp *ring.Poly) *Ciphertext
}
type encryptor struct {
rlwe.Encryptor
params Parameters
}
// NewEncryptor instatiates a new Encryptor for the CKKS scheme. The key argument can
// be either a *rlwe.PublicKey or a *rlwe.SecretKey.
func NewEncryptor(params Parameters, key interface{}) Encryptor {
return &encryptor{rlwe.NewEncryptor(params.Parameters, key), params}
}
// NewFastEncryptor instantiates a new Encryptor for the CKKS scheme.
// This encryptor's Encrypt method first encrypts zero in Q and then adds the plaintext.
// This method is faster than the normal encryptor but result in a noisier ciphertext.
func NewFastEncryptor(params Parameters, key *rlwe.PublicKey) Encryptor {
return &encryptor{rlwe.NewFastEncryptor(params.Parameters, key), params}
}
// Encrypt encrypts the input plaintext and write the result on ctOut. The encryption
// algorithm depends on how the receiver encryptor was initialized (see NewEncryptor
// and NewFastEncryptor).
// The level of the output ciphertext is min(plaintext.Level(), ciphertext.Level()).
func (enc *encryptor) Encrypt(plaintext *Plaintext, ctOut *Ciphertext) {
enc.Encryptor.Encrypt(&rlwe.Plaintext{Value: plaintext.Value}, &rlwe.Ciphertext{Value: ctOut.Value})
ctOut.Scale = plaintext.Scale
}
// EncryptNew encrypts the input plaintext returns the result as a newly allocated ciphertext.
// The encryption algorithm depends on how the receiver encryptor was initialized (see
// NewEncryptor and NewFastEncryptor).
// The level of the output ciphertext is min(plaintext.Level(), ciphertext.Level()).
func (enc *encryptor) EncryptNew(plaintext *Plaintext) *Ciphertext {
ct := NewCiphertext(enc.params, 1, plaintext.Level(), plaintext.Scale)
enc.Encryptor.Encrypt(&rlwe.Plaintext{Value: plaintext.Value}, ct.Ciphertext)
return ct
}
// EncryptFromCRP encrypts the input plaintext and writes the result in ctOut.
// The passed crp is always treated as being in the NTT domain and the level of the output ciphertext is
// min(plaintext.Level(), ciphertext.Level()).
func (enc *encryptor) EncryptFromCRP(plaintext *Plaintext, crp *ring.Poly, ctOut *Ciphertext) {
enc.Encryptor.EncryptFromCRP(&rlwe.Plaintext{Value: plaintext.Value}, crp, &rlwe.Ciphertext{Value: ctOut.Value})
ctOut.Scale = plaintext.Scale
}
// EncryptFromCRPNew encrypts the input plaintext and returns the result as a newly allocated ciphertext.
// The passed crp is always treated as being in the NTT domain and the level of the output ciphertext is
// min(plaintext.Level(), ciphertext.Level()).
func (enc *encryptor) EncryptFromCRPNew(plaintext *Plaintext, crp *ring.Poly) *Ciphertext {
ct := NewCiphertext(enc.params, 1, plaintext.Level(), plaintext.Scale)
enc.Encryptor.EncryptFromCRP(&rlwe.Plaintext{Value: plaintext.Value}, crp, ct.Ciphertext)
return &Ciphertext{Ciphertext: ct.Ciphertext, Scale: plaintext.Scale}
}