Files
lattigo/mhe/refresh.go
Jean-Philippe Bossuat cc821be097 fixes #23
2023-12-21 09:31:29 +01:00

104 lines
3.1 KiB
Go

package mhe
import (
"bufio"
"io"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/utils/buffer"
)
// RefreshShare is a struct storing the decryption and recryption shares.
type RefreshShare struct {
EncToShareShare KeySwitchShare
ShareToEncShare KeySwitchShare
MetaData rlwe.MetaData
}
// BinarySize returns the serialized size of the object in bytes.
func (share RefreshShare) BinarySize() int {
return share.EncToShareShare.BinarySize() + share.ShareToEncShare.BinarySize() + share.MetaData.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 (share RefreshShare) WriteTo(w io.Writer) (n int64, err error) {
switch w := w.(type) {
case buffer.Writer:
if n, err = share.MetaData.WriteTo(w); err != nil {
return
}
var inc int64
if inc, err = share.EncToShareShare.WriteTo(w); err != nil {
return n + inc, err
}
n += inc
inc, err = share.ShareToEncShare.WriteTo(w)
return n + inc, err
default:
return share.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 (share *RefreshShare) ReadFrom(r io.Reader) (n int64, err error) {
switch r := r.(type) {
case buffer.Reader:
if n, err = share.MetaData.ReadFrom(r); err != nil {
return
}
var inc int64
if inc, err = share.EncToShareShare.ReadFrom(r); err != nil {
return
}
n += inc
inc, err = share.ShareToEncShare.ReadFrom(r)
return n + inc, err
default:
return share.ReadFrom(bufio.NewReader(r))
}
}
// MarshalBinary encodes the object into a binary form on a newly allocated slice of bytes.
func (share RefreshShare) MarshalBinary() (p []byte, err error) {
buf := buffer.NewBufferSize(share.BinarySize())
_, err = share.WriteTo(buf)
return buf.Bytes(), err
}
// UnmarshalBinary decodes a slice of bytes generated by
// MarshalBinary or WriteTo on the object.
func (share *RefreshShare) UnmarshalBinary(p []byte) (err error) {
_, err = share.ReadFrom(buffer.NewBuffer(p))
return
}