[utils/sampling]: removed panics & removed PRNG in ckks.Encoder

This commit is contained in:
Jean-Philippe Bossuat
2023-10-27 10:48:41 +02:00
parent fcae9808a3
commit 4f6ea2ded4
2 changed files with 3 additions and 24 deletions

View File

@@ -11,7 +11,6 @@ import (
"github.com/tuneinsight/lattigo/v4/rlwe/ringqp"
"github.com/tuneinsight/lattigo/v4/utils"
"github.com/tuneinsight/lattigo/v4/utils/bignum"
"github.com/tuneinsight/lattigo/v4/utils/sampling"
)
type Float interface {
@@ -62,8 +61,6 @@ type Encoder struct {
m int
rotGroup []int
prng sampling.PRNG
roots interface{}
buffCmplx interface{}
}
@@ -84,13 +81,6 @@ func NewEncoder(parameters Parameters, precision ...uint) (ecd *Encoder) {
fivePows &= (m - 1)
}
prng, err := sampling.NewPRNG()
// This error should never happen.
if err != nil {
panic(err)
}
var prec uint
if len(precision) != 0 && precision[0] != 0 {
prec = precision[0]
@@ -106,7 +96,6 @@ func NewEncoder(parameters Parameters, precision ...uint) (ecd *Encoder) {
buff: parameters.RingQ().NewPoly(),
m: m,
rotGroup: rotGroup,
prng: prng,
}
if prec <= 53 {
@@ -1183,13 +1172,6 @@ func (ecd *Encoder) polyToFloatNoCRT(coeffs []uint64, values FloatSlice, scale r
// that can be used concurrently with the original object.
func (ecd Encoder) ShallowCopy() *Encoder {
prng, err := sampling.NewPRNG()
// This error should never happen.
if err != nil {
panic(err)
}
var buffCmplx interface{}
if prec := ecd.prec; prec <= 53 {
@@ -1212,7 +1194,6 @@ func (ecd Encoder) ShallowCopy() *Encoder {
buff: *ecd.buff.CopyNew(),
m: ecd.m,
rotGroup: ecd.rotGroup,
prng: prng,
roots: ecd.roots,
buffCmplx: buffCmplx,
}

View File

@@ -2,6 +2,7 @@ package sampling
import (
"crypto/rand"
"fmt"
"io"
"golang.org/x/crypto/blake2b"
@@ -37,7 +38,7 @@ func NewPRNG() (*KeyedPRNG, error) {
prng := new(KeyedPRNG)
key := make([]byte, 64)
if _, err := rand.Read(key); err != nil {
panic("crypto rand error")
return fmt.Errorf("crypto rand error: %w", err)
}
prng.key = key
prng.xof, err = blake2b.NewXOF(blake2b.OutputLengthUnknown, key)
@@ -55,10 +56,7 @@ func (prng *KeyedPRNG) Key() (key []byte) {
// Read reads bytes from the KeyedPRNG on sum.
func (prng *KeyedPRNG) Read(sum []byte) (n int, err error) {
if n, err = prng.xof.Read(sum); err != nil {
panic(err)
}
return n, nil
return prng.xof.Read(sum)
}
// Reset resets the PRNG to its initial state.