forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
415 lines (327 loc) · 10.8 KB
/
element.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
package rlwe
import (
"bufio"
"fmt"
"io"
"github.com/google/go-cmp/cmp"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/ring/ringqp"
"github.com/tuneinsight/lattigo/v5/utils/buffer"
"github.com/tuneinsight/lattigo/v5/utils/sampling"
"github.com/tuneinsight/lattigo/v5/utils/structs"
)
// ElementInterface is a common interface for Ciphertext and Plaintext types.
type ElementInterface[T ring.Poly | ringqp.Poly] interface {
El() *Element[T]
Degree() int
Level() int
}
// Element is a generic struct to store a vector of T along with some metadata.
type Element[T ring.Poly | ringqp.Poly] struct {
*MetaData
Value structs.Vector[T]
}
// NewElement allocates a new Element[ring.Poly].
func NewElement(params ParameterProvider, degree int, levelQ ...int) *Element[ring.Poly] {
p := params.GetRLWEParameters()
lvlq, _ := p.UnpackLevelParams(levelQ)
ringQ := p.RingQ().AtLevel(lvlq)
Value := make([]ring.Poly, degree+1)
for i := range Value {
Value[i] = ringQ.NewPoly()
}
return &Element[ring.Poly]{
Value: Value,
MetaData: &MetaData{
CiphertextMetaData: CiphertextMetaData{
IsNTT: p.NTTFlag(),
},
},
}
}
// NewElementExtended allocates a new Element[ringqp.Poly].
func NewElementExtended(params ParameterProvider, degree, levelQ, levelP int) *Element[ringqp.Poly] {
p := params.GetRLWEParameters()
ringQP := p.RingQP().AtLevel(levelQ, levelP)
Value := make([]ringqp.Poly, degree+1)
for i := range Value {
Value[i] = ringQP.NewPoly()
}
return &Element[ringqp.Poly]{
Value: Value,
MetaData: &MetaData{
CiphertextMetaData: CiphertextMetaData{
IsNTT: p.NTTFlag(),
},
},
}
}
// NewElementAtLevelFromPoly constructs a new Element at a specific level
// where the message is set to the passed poly. No checks are performed on poly and
// the returned Element will share its backing array of coefficients.
// Returned Element's MetaData is nil.
func NewElementAtLevelFromPoly(level int, poly []ring.Poly) (*Element[ring.Poly], error) {
Value := make([]ring.Poly, len(poly))
for i := range Value {
if len(poly[i].Coeffs) < level+1 {
return nil, fmt.Errorf("cannot NewElementAtLevelFromPoly: provided ring.Poly[%d] level is too small", i)
}
Value[i].Coeffs = poly[i].Coeffs[:level+1]
}
return &Element[ring.Poly]{Value: Value}, nil
}
// Equal performs a deep equal.
func (op Element[T]) Equal(other *Element[T]) bool {
return cmp.Equal(op.MetaData, other.MetaData) && cmp.Equal(op.Value, other.Value)
}
// Degree returns the degree of the target Element.
func (op Element[T]) Degree() int {
return len(op.Value) - 1
}
// Level returns the level of the target Element.
func (op Element[T]) Level() int {
return op.LevelQ()
}
func (op Element[T]) LevelQ() int {
switch el := any(op.Value[0]).(type) {
case ring.Poly:
return el.Level()
case ringqp.Poly:
return el.LevelQ()
default:
// Sanity check
panic("invalid Element[type]")
}
}
func (op Element[T]) LevelP() int {
switch el := any(op.Value[0]).(type) {
case ring.Poly:
panic("cannot levelP on Element[ring.Poly]")
case ringqp.Poly:
return el.LevelP()
default:
// Sanity check
panic("invalid Element[type]")
}
}
func (op *Element[T]) El() *Element[T] {
return op
}
// Resize resizes the degree of the target element.
// Sets the NTT flag of the added poly equal to the NTT flag
// to the poly at degree zero.
func (op *Element[T]) Resize(degree, level int) {
switch op := any(op).(type) {
case *Element[ring.Poly]:
if op.Level() != level {
for i := range op.Value {
op.Value[i].Resize(level)
}
}
if op.Degree() > degree {
op.Value = op.Value[:degree+1]
} else if op.Degree() < degree {
for op.Degree() < degree {
op.Value = append(op.Value, []ring.Poly{ring.NewPoly(op.Value[0].N(), level)}...)
}
}
default:
// Sanity check
panic(fmt.Errorf("can only resize Element[ring.Poly] but is %T", op))
}
}
// CopyNew creates a deep copy of the object and returns it.
func (op Element[T]) CopyNew() *Element[T] {
return &Element[T]{Value: op.Value.CopyNew(), MetaData: op.MetaData.CopyNew()}
}
// Copy copies opCopy on op, up to the capacity of op (similarely to copy([]byte, []byte)).
func (op *Element[T]) Copy(opCopy *Element[T]) {
if op != opCopy {
switch any(op.Value).(type) {
case structs.Vector[ring.Poly]:
op0 := any(op.Value).(structs.Vector[ring.Poly])
op1 := any(opCopy.Value).(structs.Vector[ring.Poly])
for i := range opCopy.Value {
op0[i].Copy(op1[i])
}
case structs.Vector[ringqp.Poly]:
op0 := any(op.Value).(structs.Vector[ringqp.Poly])
op1 := any(opCopy.Value).(structs.Vector[ringqp.Poly])
for i := range opCopy.Value {
op0[i].Copy(op1[i])
}
}
if opCopy.MetaData != nil {
if op.MetaData == nil {
op.MetaData = &MetaData{}
}
*op.MetaData = *opCopy.MetaData
}
}
}
// GetSmallestLargest returns the provided element that has the smallest degree as a first
// returned value and the largest degree as second return value. If the degree match, the
// order is the same as for the input.
func GetSmallestLargest[T ring.Poly | ringqp.Poly](el0, el1 *Element[T]) (smallest, largest *Element[T], sameDegree bool) {
switch {
case el0.Degree() > el1.Degree():
return el1, el0, false
case el0.Degree() < el1.Degree():
return el0, el1, false
}
return el0, el1, true
}
// PopulateElementRandom creates a new rlwe.Element with random coefficients.
func PopulateElementRandom(prng sampling.PRNG, params ParameterProvider, ct *Element[ring.Poly]) {
sampler := ring.NewUniformSampler(prng, params.GetRLWEParameters().RingQ()).AtLevel(ct.Level())
for i := range ct.Value {
sampler.Read(ct.Value[i])
}
}
// SwitchCiphertextRingDegreeNTT changes the ring degree of ctIn to the one of opOut.
// Maps Y^{N/n} -> X^{N} or X^{N} -> Y^{N/n}.
// If the ring degree of opOut is larger than the one of ctIn, then the ringQ of opOut
// must be provided (otherwise, a nil pointer).
// The ctIn must be in the NTT domain and opOut will be in the NTT domain.
func SwitchCiphertextRingDegreeNTT(ctIn *Element[ring.Poly], ringQLargeDim *ring.Ring, opOut *Element[ring.Poly]) {
NIn, NOut := len(ctIn.Value[0].Coeffs[0]), len(opOut.Value[0].Coeffs[0])
if NIn > NOut {
gap := NIn / NOut
buff := make([]uint64, NIn)
for i := range opOut.Value {
for j := range opOut.Value[i].Coeffs {
tmpIn, tmpOut := ctIn.Value[i].Coeffs[j], opOut.Value[i].Coeffs[j]
ringQLargeDim.SubRings[j].INTT(tmpIn, buff)
for w0, w1 := 0, 0; w0 < NOut; w0, w1 = w0+1, w1+gap {
tmpOut[w0] = buff[w1]
}
s := ringQLargeDim.SubRings[j]
switch ringQLargeDim.Type() {
case ring.Standard:
ring.NTTStandard(tmpOut, tmpOut, NOut, s.Modulus, s.MRedConstant, s.BRedConstant, s.RootsForward)
case ring.ConjugateInvariant:
ring.NTTConjugateInvariant(tmpOut, tmpOut, NOut, s.Modulus, s.MRedConstant, s.BRedConstant, s.RootsForward)
}
}
}
} else {
for i := range opOut.Value {
ring.MapSmallDimensionToLargerDimensionNTT(ctIn.Value[i], opOut.Value[i])
}
}
*opOut.MetaData = *ctIn.MetaData
}
// SwitchCiphertextRingDegree changes the ring degree of ctIn to the one of opOut.
// Maps Y^{N/n} -> X^{N} or X^{N} -> Y^{N/n}.
// If the ring degree of opOut is larger than the one of ctIn, then the ringQ of ctIn
// must be provided (otherwise, a nil pointer).
func SwitchCiphertextRingDegree(ctIn, opOut *Element[ring.Poly]) {
NIn, NOut := len(ctIn.Value[0].Coeffs[0]), len(opOut.Value[0].Coeffs[0])
gapIn, gapOut := NOut/NIn, 1
if NIn > NOut {
gapIn, gapOut = 1, NIn/NOut
}
for i := range opOut.Value {
for j := range opOut.Value[i].Coeffs {
tmp0, tmp1 := opOut.Value[i].Coeffs[j], ctIn.Value[i].Coeffs[j]
for w0, w1 := 0, 0; w0 < NOut; w0, w1 = w0+gapIn, w1+gapOut {
tmp0[w0] = tmp1[w1]
}
}
}
*opOut.MetaData = *ctIn.MetaData
}
// BinarySize returns the serialized size of the object in bytes.
func (op Element[T]) BinarySize() (size int) {
size++ // Whether or not there is metadata
if op.MetaData != nil {
size += op.MetaData.BinarySize()
}
return size + op.Value.BinarySize()
}
// WriteTo writes the object on an io.Writer. It implements the io.WriterTo
// interface, and will write exactly object.BinarySize() bytes on w.
//
// Unless w implements the buffer.Writer interface (see lattigo/utils/buffer/writer.go),
// it will be wrapped into a bufio.Writer. Since this requires allocations, it
// is preferable to pass a buffer.Writer directly:
//
// - When writing multiple times to a io.Writer, it is preferable to first wrap the
// io.Writer in a pre-allocated bufio.Writer.
// - When writing to a pre-allocated var b []byte, it is preferable to pass
// buffer.NewBuffer(b) as w (see lattigo/utils/buffer/buffer.go).
func (op Element[T]) WriteTo(w io.Writer) (n int64, err error) {
switch w := w.(type) {
case buffer.Writer:
var inc int64
if op.MetaData != nil {
if inc, err = buffer.WriteUint8(w, 1); err != nil {
return n, err
}
n += inc
if inc, err = op.MetaData.WriteTo(w); err != nil {
return n, err
}
n += inc
} else {
if inc, err = buffer.WriteUint8(w, 0); err != nil {
return n, err
}
n += inc
}
inc, err = op.Value.WriteTo(w)
return n + inc, err
default:
return op.WriteTo(bufio.NewWriter(w))
}
}
// ReadFrom reads on the object from an io.Writer. It implements the
// io.ReaderFrom interface.
//
// Unless r implements the buffer.Reader interface (see see lattigo/utils/buffer/reader.go),
// it will be wrapped into a bufio.Reader. Since this requires allocation, it
// is preferable to pass a buffer.Reader directly:
//
// - When reading multiple values from a io.Reader, it is preferable to first
// first wrap io.Reader in a pre-allocated bufio.Reader.
// - When reading from a var b []byte, it is preferable to pass a buffer.NewBuffer(b)
// as w (see lattigo/utils/buffer/buffer.go).
func (op *Element[T]) ReadFrom(r io.Reader) (n int64, err error) {
switch r := r.(type) {
case buffer.Reader:
if op == nil {
return 0, fmt.Errorf("cannot ReadFrom: target object is nil")
}
var inc int64
var hasMetaData uint8
if inc, err = buffer.ReadUint8(r, &hasMetaData); err != nil {
return n, err
}
n += inc
if hasMetaData == 1 {
if op.MetaData == nil {
op.MetaData = &MetaData{}
}
if inc, err = op.MetaData.ReadFrom(r); err != nil {
return n, err
}
n += inc
}
inc, err = op.Value.ReadFrom(r)
return n + inc, err
default:
return op.ReadFrom(bufio.NewReader(r))
}
}
// MarshalBinary encodes the object into a binary form on a newly allocated slice of bytes.
func (op Element[T]) MarshalBinary() (data []byte, err error) {
buf := buffer.NewBufferSize(op.BinarySize())
_, err = op.WriteTo(buf)
return buf.Bytes(), err
}
// UnmarshalBinary decodes a slice of bytes generated by
// MarshalBinary or WriteTo on the object.
func (op *Element[T]) UnmarshalBinary(p []byte) (err error) {
_, err = op.ReadFrom(buffer.NewBuffer(p))
return
}