forked from tuneinsight/lattigo
/
ciphertextQP.go
102 lines (78 loc) · 2.37 KB
/
ciphertextQP.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
package rlwe
import (
"fmt"
"github.com/fedejinich/lattigo/v6/rlwe/ringqp"
)
// CiphertextQP is a generic type for RLWE ciphertexts in R_qp.
// It contains no MetaData.
type CiphertextQP struct {
MetaData
Value [2]ringqp.Poly
}
// NewCiphertextQP allocates a new CiphertextQP.
func NewCiphertextQP(params Parameters, levelQ, levelP int) CiphertextQP {
ringQ := params.RingQ().AtLevel(levelQ)
ringP := params.RingQ().AtLevel(levelP)
return CiphertextQP{
Value: [2]ringqp.Poly{
{
Q: ringQ.NewPoly(),
P: ringP.NewPoly(),
},
{
Q: ringQ.NewPoly(),
P: ringP.NewPoly(),
},
},
MetaData: MetaData{
IsNTT: params.DefaultNTTFlag(),
},
}
}
// MarshalBinarySize returns the length in bytes of the target CiphertextQP.
func (ct *CiphertextQP) MarshalBinarySize() int {
return ct.MetaData.MarshalBinarySize() + 2*ct.Value[0].MarshalBinarySize64()
}
// Encode64 encodes the target CiphertextQP on a byte array, using 8 bytes per coefficient.
// It returns the number of written bytes, and the corresponding error, if it occurred.
func (ct *CiphertextQP) Encode64(data []byte) (ptr int, err error) {
if len(data) < ct.MarshalBinarySize() {
return 0, fmt.Errorf("Encode64: len(data) is too small")
}
if ptr, err = ct.MetaData.Encode64(data); err != nil {
return
}
var inc int
if inc, err = ct.Value[0].Encode64(data[ptr:]); err != nil {
return
}
ptr += inc
if inc, err = ct.Value[1].Encode64(data[ptr:]); err != nil {
return
}
ptr += inc
return
}
// Decode64 decodes a slice of bytes in the target CiphertextQP and returns the number of bytes decoded.
// The method will first try to write on the buffer. If this step fails, either because the buffer isn't
// allocated or because it has the wrong size, the method will allocate the correct buffer.
// Assumes that each coefficient is encoded on 8 bytes.
func (ct *CiphertextQP) Decode64(data []byte) (ptr int, err error) {
if ptr, err = ct.MetaData.Decode64(data); err != nil {
return
}
var inc int
if inc, err = ct.Value[0].Decode64(data[ptr:]); err != nil {
return
}
ptr += inc
if inc, err = ct.Value[1].Decode64(data[ptr:]); err != nil {
return
}
ptr += inc
return
}
// CopyNew returns a copy of the target CiphertextQP.
func (ct *CiphertextQP) CopyNew() *CiphertextQP {
return &CiphertextQP{Value: [2]ringqp.Poly{ct.Value[0].CopyNew(), ct.Value[1].CopyNew()}, MetaData: ct.MetaData}
}