Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization for compressed evaluation keys #509

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions core/rlwe/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,110 @@ func (evk EvaluationKey) Expand(params ParameterProvider, buffer *GadgetCipherte
return nil
}

// BinarySize returns the serialized size of the object in bytes.
func (evk EvaluationKey) BinarySize() (size int) {
return evk.GadgetCiphertext.BinarySize() + len(evk.Seed)
}

// 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 (evk EvaluationKey) WriteTo(w io.Writer) (n int64, err error) {
switch w := w.(type) {
case buffer.Writer:

var inc int64
var incInt int

if inc, err = evk.GadgetCiphertext.WriteTo(w); err != nil {
return n + inc, err
}

n += inc

if evk.IsCompressed() {
if incInt, err = w.Write(evk.Seed); err != nil {
lehugueni marked this conversation as resolved.
Show resolved Hide resolved
return n + int64(incInt), err
}

// Sanity check, should not happen unless the size of the seed has been modified in the code
if incInt != 32 {
lehugueni marked this conversation as resolved.
Show resolved Hide resolved
return n + int64(incInt), fmt.Errorf("writing compressed evaluation key: the size of the seed=%d != 32", incInt)
}

n += int64(incInt)
}

w.Flush()
lehugueni marked this conversation as resolved.
Show resolved Hide resolved
return

default:
return evk.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 (evk *EvaluationKey) ReadFrom(r io.Reader) (n int64, err error) {
switch r := r.(type) {
case buffer.Reader:

var inc int64
var incInt int

if inc, err = evk.GadgetCiphertext.ReadFrom(r); err != nil {
return n + inc, err
}

n += inc

if evk.IsCompressed() {
seed := make([]byte, 32)
if incInt, err = r.Read(seed); err != nil {
return n + int64(incInt), err
}
evk.Seed = seed

n += int64(incInt)
}

return
default:
return evk.ReadFrom(bufio.NewReader(r))
}
}

// MarshalBinary encodes the object into a binary form on a newly allocated slice of bytes.
func (evk EvaluationKey) MarshalBinary() (p []byte, err error) {
buf := buffer.NewBufferSize(evk.BinarySize())
_, err = evk.WriteTo(buf)
return buf.Bytes(), err
}

// UnmarshalBinary decodes a slice of bytes generated by
// [EvaluationKey.MarshalBinary] or [EvaluationKey.WriteTo] on the object.
func (evk *EvaluationKey) UnmarshalBinary(p []byte) (err error) {
_, err = evk.ReadFrom(buffer.NewBuffer(p))
return
}

// CopyNew creates a deep copy of the target [EvaluationKey] and returns it.
func (evk EvaluationKey) CopyNew() *EvaluationKey {
return &EvaluationKey{GadgetCiphertext: *evk.GadgetCiphertext.CopyNew()}
Expand Down
4 changes: 4 additions & 0 deletions core/rlwe/rlwe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,10 @@ func testWriteAndRead(tc *TestContext, bpw2 int, t *testing.T) {
buffer.RequireSerializerCorrect(t, tc.kgen.GenEvaluationKeyNew(sk, sk))
})

t.Run(testString(params, levelQ, levelP, bpw2, "WriteAndRead/EvaluationKey/Compressed=True"), func(t *testing.T) {
buffer.RequireSerializerCorrect(t, tc.kgen.GenEvaluationKeyNew(sk, sk, EvaluationKeyParameters{Compressed: true}))
})

t.Run(testString(params, levelQ, levelP, bpw2, "WriteAndRead/RelinearizationKey"), func(t *testing.T) {
buffer.RequireSerializerCorrect(t, tc.kgen.GenRelinearizationKeyNew(tc.sk))
})
Expand Down
Loading