From 3e9369f2fa3068233b408036a2b9edade07484ea Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 13 Apr 2023 14:41:32 +0200 Subject: [PATCH] [rlwe]: fixed nil interface bug in evaluator --- rlwe/evaluationkeyset.go | 2 +- rlwe/evaluator.go | 2 +- utils/utils.go | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rlwe/evaluationkeyset.go b/rlwe/evaluationkeyset.go index 91b7ba37..fedb93d8 100644 --- a/rlwe/evaluationkeyset.go +++ b/rlwe/evaluationkeyset.go @@ -48,7 +48,7 @@ func (evk *EvaluationKeySet) GetGaloisKey(galEl uint64) (gk *GaloisKey, err erro // for which a Galois key exists in the object. func (evk *EvaluationKeySet) GetGaloisKeysList() (galEls []uint64) { - if evk.GaloisKeys == nil { + if evk == nil || evk.GaloisKeys == nil { return []uint64{} } diff --git a/rlwe/evaluator.go b/rlwe/evaluator.go index 2225f266..2d520c83 100644 --- a/rlwe/evaluator.go +++ b/rlwe/evaluator.go @@ -85,7 +85,7 @@ func NewEvaluator(params Parameters, evk EvaluationKeySetInterface) (eval *Evalu var AutomorphismIndex map[uint64][]uint64 - if evk != nil { + if !utils.IsNil(evk) { if galEls := evk.GetGaloisKeysList(); len(galEls) != 0 { AutomorphismIndex = make(map[uint64][]uint64) diff --git a/utils/utils.go b/utils/utils.go index c079d3c2..ebdb4463 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "math/bits" + "reflect" "golang.org/x/exp/constraints" ) @@ -23,6 +24,12 @@ func Max[V constraints.Ordered](a, b V) (r V) { return b } +// IsNil returns true either type or value are nil. +// Only interfaces or pointers to objects should be passed as argument. +func IsNil(i interface{}) bool { + return i == nil || reflect.ValueOf(i).IsNil() +} + // BitReverse64 returns the bit-reverse value of the input value, within a context of 2^bitLen. func BitReverse64[V uint64 | uint32 | int | int64](index V, bitLen int) uint64 { return bits.Reverse64(uint64(index)) >> (64 - bitLen)