forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly.go
298 lines (237 loc) · 8.86 KB
/
poly.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
package ring
import (
"encoding/binary"
"errors"
)
// Poly is the structure that contains the coefficients of a polynomial.
type Poly struct {
Coeffs [][]uint64 // Dimension-2 slice of coefficients (re-slice of Buff)
Buff []uint64 // Dimension-1 slice of coefficient
}
// NewPoly creates a new polynomial with N coefficients set to zero and Level+1 moduli.
func NewPoly(N, Level int) (pol *Poly) {
pol = new(Poly)
pol.Buff = make([]uint64, N*(Level+1))
pol.Coeffs = make([][]uint64, Level+1)
for i := 0; i < Level+1; i++ {
pol.Coeffs[i] = pol.Buff[i*N : (i+1)*N]
}
return
}
// Resize resizes the level of the target polynomial to the provided level.
// If the provided level is larger than the current level, then allocates zero
// coefficients, otherwise dereferences the coefficients above the provided level.
func (pol *Poly) Resize(level int) {
N := pol.N()
if pol.Level() > level {
pol.Buff = pol.Buff[:N*(level+1)]
pol.Coeffs = pol.Coeffs[:level+1]
} else if level > pol.Level() {
pol.Buff = append(pol.Buff, make([]uint64, N*(level-pol.Level()))...)
pol.Coeffs = append(pol.Coeffs, make([][]uint64, level-pol.Level())...)
for i := 0; i < level+1; i++ {
pol.Coeffs[i] = pol.Buff[i*N : (i+1)*N]
}
}
}
// N returns the number of coefficients of the polynomial, which equals the degree of the Ring cyclotomic polynomial.
func (pol *Poly) N() int {
return len(pol.Coeffs[0])
}
// Level returns the current number of moduli minus 1.
func (pol *Poly) Level() int {
return len(pol.Coeffs) - 1
}
// Zero sets all coefficients of the target polynomial to 0.
func (pol *Poly) Zero() {
ZeroVec(pol.Buff)
}
// CopyNew creates an exact copy of the target polynomial.
func (pol *Poly) CopyNew() (p1 *Poly) {
p1 = NewPoly(pol.N(), pol.Level())
copy(p1.Buff, pol.Buff)
return
}
// Copy copies the coefficients of p0 on p1 within the given Ring. It requires p1 to be at least as big p0.
// Expects the degree of both polynomials to be identical.
func Copy(p0, p1 *Poly) {
copy(p1.Buff, p0.Buff)
}
// CopyLvl copies the coefficients of p0 on p1 within the given Ring.
// Copies for up to level+1 moduli.
// Expects the degree of both polynomials to be identical.
func CopyLvl(level int, p0, p1 *Poly) {
copy(p1.Buff[:p1.N()*(level+1)], p0.Buff)
}
// CopyValues copies the coefficients of p1 on the target polynomial.
// Onyl copies minLevel(pol, p1) levels.
// Expects the degree of both polynomials to be identical.
func (pol *Poly) CopyValues(p1 *Poly) {
if pol != p1 {
copy(pol.Buff, p1.Buff)
}
}
// Copy copies the coefficients of p1 on the target polynomial.
// Onyl copies minLevel(pol, p1) levels.
func (pol *Poly) Copy(p1 *Poly) {
pol.CopyValues(p1)
}
// Equals returns true if the receiver Poly is equal to the provided other Poly.
// This function checks for strict equality between the polynomial coefficients
// (i.e., it does not consider congruence as equality within the ring like
// `Ring.Equals` does).
func (pol *Poly) Equals(other *Poly) bool {
if pol == other {
return true
}
if pol != nil && other != nil && len(pol.Buff) == len(other.Buff) {
for i := range pol.Buff {
if other.Buff[i] != pol.Buff[i] {
return false
}
}
return true
}
return false
}
// MarshalBinarySize64 returns the number of bytes a polynomial of N coefficients
// with Level+1 moduli will take when converted to a slice of bytes.
// Assumes that each coefficient will be encoded on 8 bytes.
func MarshalBinarySize64(N, Level int) (cnt int) {
return 5 + N*(Level+1)<<3
}
// MarshalBinarySize64 returns the number of bytes the polynomial will take when written to data.
// Assumes that each coefficient takes 8 bytes.
func (pol *Poly) MarshalBinarySize64() (cnt int) {
return MarshalBinarySize64(pol.N(), pol.Level())
}
// MarshalBinary encodes the target polynomial on a slice of bytes.
// Encodes each coefficient on 8 bytes.
func (pol *Poly) MarshalBinary() (data []byte, err error) {
data = make([]byte, pol.MarshalBinarySize64())
_, err = pol.Encode64(data)
return
}
// UnmarshalBinary decodes a slice of byte on the target polynomial.
// Assumes each coefficient is encoded on 8 bytes.
func (pol *Poly) UnmarshalBinary(data []byte) (err error) {
N := int(binary.BigEndian.Uint32(data))
Level := int(data[4])
ptr := 5
if ((len(data) - ptr) >> 3) != N*(Level+1) {
return errors.New("invalid polynomial encoding")
}
if _, err = pol.Decode64(data); err != nil {
return err
}
return nil
}
// Encode64 writes the given poly to the data array, using 8 bytes per coefficient.
// It returns the number of written bytes, and the corresponding error, if it occurred.
func (pol *Poly) Encode64(data []byte) (int, error) {
N := pol.N()
Level := pol.Level()
if len(data) < pol.MarshalBinarySize64() {
// The data is not big enough to write all the information
return 0, errors.New("data array is too small to write ring.Poly")
}
binary.BigEndian.PutUint32(data, uint32(N))
data[4] = uint8(Level)
return Encode64(5, pol.Buff, data)
}
// Encode64 converts a matrix of coefficients to a byte array, using 8 bytes per coefficient.
func Encode64(ptr int, coeffs []uint64, data []byte) (int, error) {
for i, j := 0, ptr; i < len(coeffs); i, j = i+1, j+8 {
binary.BigEndian.PutUint64(data[j:], coeffs[i])
}
return ptr + len(coeffs)*8, nil
}
// Decode64 decodes a slice of bytes in the target polynomial 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 is of the wrong size, the method will allocate the correct buffer.
// Assumes that each coefficient is encoded on 8 bytes.
func (pol *Poly) Decode64(data []byte) (ptr int, err error) {
N := int(binary.BigEndian.Uint32(data))
Level := int(data[4])
ptr = 5
if pol.Buff == nil || len(pol.Buff) != N*(Level+1) {
pol.Buff = make([]uint64, N*(Level+1))
}
if ptr, err = Decode64(ptr, pol.Buff, data); err != nil {
return ptr, err
}
// Reslice
pol.Coeffs = make([][]uint64, Level+1)
for i := 0; i < Level+1; i++ {
pol.Coeffs[i] = pol.Buff[i*N : (i+1)*N]
}
return ptr, nil
}
// Decode64 converts a byte array to a matrix of coefficients.
// Assumes that each coefficient is encoded on 8 bytes.
func Decode64(ptr int, coeffs []uint64, data []byte) (int, error) {
for i, j := 0, ptr; i < len(coeffs); i, j = i+1, j+8 {
coeffs[i] = binary.BigEndian.Uint64(data[j:])
}
return ptr + len(coeffs)*8, nil
}
// Encode32 writes the given poly to the data array.
// Encodes each coefficient on 4 bytes.
// It returns the number of written bytes, and the corresponding error, if it occurred.
func (pol *Poly) Encode32(data []byte) (int, error) {
N := pol.N()
Level := pol.Level()
if len(data) < pol.MarshalBinarySize32() {
//The data is not big enough to write all the information
return 0, errors.New("data array is too small to write ring.Poly")
}
binary.BigEndian.PutUint32(data, uint32(N))
data[4] = uint8(Level)
return Encode32(5, pol.Buff, data)
}
// Encode32 converts a matrix of coefficients to a byte array, using 4 bytes per coefficient.
func Encode32(ptr int, coeffs []uint64, data []byte) (int, error) {
for i, j := 0, ptr; i < len(coeffs); i, j = i+1, j+4 {
binary.BigEndian.PutUint32(data[j:], uint32(coeffs[i]))
}
return ptr + len(coeffs)*4, nil
}
// MarshalBinarySize32 returns the number of bytes a polynomial of N coefficients
// with Level+1 moduli will take when converted to a slice of bytes.
// Assumes that each coefficient will be encoded on 4 bytes.
func MarshalBinarySize32(N, Level int) (cnt int) {
return 5 + N*(Level+1)<<2
}
// MarshalBinarySize32 returns the number of bytes the polynomial will take when written to data.
// Assumes that each coefficient is encoded on 4 bytes.
func (pol *Poly) MarshalBinarySize32() (cnt int) {
return MarshalBinarySize32(pol.N(), pol.Level())
}
// Decode32 decodes a slice of bytes in the target polynomial 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 is of the wrong size, the method will allocate the correct buffer.
// Assumes that each coefficient is encoded on 8 bytes.
func (pol *Poly) Decode32(data []byte) (ptr int, err error) {
N := int(binary.BigEndian.Uint32(data))
Level := int(data[4])
ptr = 5
if pol.Buff == nil || len(pol.Buff) != N*(Level+1) {
pol.Buff = make([]uint64, N*(Level+1))
}
if ptr, err = Decode32(ptr, pol.Buff, data); err != nil {
return ptr, err
}
pol.Coeffs = make([][]uint64, Level+1)
for i := 0; i < Level+1; i++ {
pol.Coeffs[i] = pol.Buff[i*N : (i+1)*N]
}
return ptr, nil
}
// Decode32 converts a byte array to a matrix of coefficients.
// Assumes that each coefficient is encoded on 4 bytes.
func Decode32(ptr int, coeffs []uint64, data []byte) (int, error) {
for i, j := 0, ptr; i < len(coeffs); i, j = i+1, j+4 {
coeffs[i] = uint64(binary.BigEndian.Uint32(data[j:]))
}
return ptr + len(coeffs)*4, nil
}