Files
lattigo/core/rlwe/plaintext.go
2024-08-06 14:46:08 +02:00

91 lines
2.9 KiB
Go

package rlwe
import (
"io"
"github.com/tuneinsight/lattigo/v6/ring"
"github.com/tuneinsight/lattigo/v6/utils/sampling"
)
// Plaintext is a common base type for RLWE plaintexts.
type Plaintext struct {
Element[ring.Poly]
Value ring.Poly
}
// NewPlaintext creates a new [Plaintext] at the specified level from the parameters.
func NewPlaintext(params ParameterProvider, level ...int) (pt *Plaintext) {
op := *NewElement(params, 0, level...)
return &Plaintext{Element: op, Value: op.Value[0]}
}
// NewPlaintextAtLevelFromPoly constructs a new [Plaintext] at a specific level
// where the message is set to the passed poly. No checks are performed on poly and
// the returned [Plaintext] will share its backing array of coefficients.
// Returned plaintext's [MetaData] is allocated but empty.
func NewPlaintextAtLevelFromPoly(level int, poly ring.Poly) (pt *Plaintext, err error) {
Element, err := NewElementAtLevelFromPoly(level, []ring.Poly{poly})
if err != nil {
return nil, err
}
Element.MetaData = &MetaData{}
return &Plaintext{Element: *Element, Value: Element.Value[0]}, nil
}
// Copy copies the supplied plaintext value into the receiver plaintext.
func (pt Plaintext) Copy(other *Plaintext) {
pt.Element.Copy(&other.Element)
pt.Value = other.Element.Value[0]
}
func (pt Plaintext) CopyNew() (ptCpy *Plaintext) {
ptCpy = new(Plaintext)
ptCpy.Element = *pt.Element.CopyNew()
ptCpy.Value = ptCpy.Element.Value[0]
return
}
// Equal performs a deep equal.
func (pt Plaintext) Equal(other *Plaintext) bool {
return pt.Element.Equal(&other.Element) && pt.Value.Equal(&other.Value)
}
// NewPlaintextRandom generates a new uniformly distributed [Plaintext].
func NewPlaintextRandom(prng sampling.PRNG, params ParameterProvider, level int) (pt *Plaintext) {
pt = NewPlaintext(params, level)
PopulateElementRandom(prng, params, pt.El())
return
}
// 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 (pt *Plaintext) ReadFrom(r io.Reader) (n int64, err error) {
if n, err = pt.Element.ReadFrom(r); err != nil {
return
}
pt.Value = pt.Element.Value[0]
return
}
// UnmarshalBinary decodes a slice of bytes generated by MarshalBinary
// or Read on the objeop.
func (pt *Plaintext) UnmarshalBinary(p []byte) (err error) {
if err = pt.Element.UnmarshalBinary(p); err != nil {
return
}
pt.Value = pt.Element.Value[0]
return
}