-
Notifications
You must be signed in to change notification settings - Fork 1
/
ciphertext.go
62 lines (51 loc) · 1.83 KB
/
ciphertext.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
package ckks
import (
"github.com/dwkim606/test_lattigo/ring"
"github.com/dwkim606/test_lattigo/rlwe"
"github.com/dwkim606/test_lattigo/utils"
)
// Ciphertext is *ring.Poly array representing a polynomial of degree > 0 with coefficients in R_Q.
type Ciphertext struct {
*rlwe.Ciphertext
Scale float64
}
// NewCiphertext creates a new Ciphertext parameterized by degree, level and scale.
func NewCiphertext(params Parameters, degree, level int, scale float64) (ciphertext *Ciphertext) {
ciphertext = &Ciphertext{Ciphertext: rlwe.NewCiphertext(params.Parameters, degree, level)}
for _, pol := range ciphertext.Value {
pol.IsNTT = true
}
ciphertext.Scale = scale
return ciphertext
}
// NewCiphertextRandom generates a new uniformly distributed Ciphertext of degree, level and scale.
func NewCiphertextRandom(prng utils.PRNG, params Parameters, degree, level int, scale float64) (ciphertext *Ciphertext) {
ringQ, err := ring.NewRing(params.N(), params.Q()[:level+1])
if err != nil {
panic(err)
}
sampler := ring.NewUniformSampler(prng, ringQ)
ciphertext = NewCiphertext(params, degree, level, scale)
for i := 0; i < degree+1; i++ {
sampler.Read(ciphertext.Value[i])
}
return ciphertext
}
// ScalingFactor returns the scaling factor of the ciphertext
func (ct *Ciphertext) ScalingFactor() float64 {
return ct.Scale
}
// SetScalingFactor sets the scaling factor of the ciphertext
func (ct *Ciphertext) SetScalingFactor(scale float64) {
ct.Scale = scale
}
// Copy copies the given ciphertext ctp into the receiver ciphertext.
func (ct *Ciphertext) Copy(ctp *Ciphertext) {
ct.Ciphertext.Copy(ctp.Ciphertext)
ct.Scale = ctp.Scale
}
// CopyNew makes a deep copy of the receiver ciphertext and returns it.
func (ct *Ciphertext) CopyNew() (ctc *Ciphertext) {
ctc = &Ciphertext{Ciphertext: ct.Ciphertext.CopyNew(), Scale: ct.Scale}
return
}