mirror of
https://github.com/tuneinsight/lattigo.git
synced 2025-09-13 03:27:14 +00:00
[circuits/float]: tweeks
This commit is contained in:
@@ -13,20 +13,20 @@ type SecretKeyBootstrapper struct {
|
||||
*ckks.Encoder
|
||||
*rlwe.Decryptor
|
||||
*rlwe.Encryptor
|
||||
sk *rlwe.SecretKey
|
||||
Values []*bignum.Complex
|
||||
Counter int // records the number of bootstrapping
|
||||
sk *rlwe.SecretKey
|
||||
Values []*bignum.Complex
|
||||
Counter int // records the number of bootstrapping
|
||||
MinLevel int
|
||||
}
|
||||
|
||||
func NewSecretKeyBootstrapper(params ckks.Parameters, sk *rlwe.SecretKey) rlwe.Bootstrapper {
|
||||
func NewSecretKeyBootstrapper(params ckks.Parameters, sk *rlwe.SecretKey) *SecretKeyBootstrapper {
|
||||
return &SecretKeyBootstrapper{
|
||||
params,
|
||||
ckks.NewEncoder(params),
|
||||
ckks.NewDecryptor(params, sk),
|
||||
ckks.NewEncryptor(params, sk),
|
||||
sk,
|
||||
make([]*bignum.Complex, params.N()),
|
||||
0}
|
||||
Parameters: params,
|
||||
Encoder: ckks.NewEncoder(params),
|
||||
Decryptor: ckks.NewDecryptor(params, sk),
|
||||
Encryptor: ckks.NewEncryptor(params, sk),
|
||||
sk: sk,
|
||||
Values: make([]*bignum.Complex, params.N())}
|
||||
}
|
||||
|
||||
func (d *SecretKeyBootstrapper) Bootstrap(ct *rlwe.Ciphertext) (*rlwe.Ciphertext, error) {
|
||||
@@ -57,7 +57,7 @@ func (d SecretKeyBootstrapper) Depth() int {
|
||||
}
|
||||
|
||||
func (d SecretKeyBootstrapper) MinimumInputLevel() int {
|
||||
return 0
|
||||
return d.MinLevel
|
||||
}
|
||||
|
||||
func (d SecretKeyBootstrapper) OutputLevel() int {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package float_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
@@ -10,7 +9,6 @@ import (
|
||||
"github.com/tuneinsight/lattigo/v4/ckks"
|
||||
"github.com/tuneinsight/lattigo/v4/ring"
|
||||
"github.com/tuneinsight/lattigo/v4/rlwe"
|
||||
"github.com/tuneinsight/lattigo/v4/utils/bignum"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -71,13 +69,11 @@ func TestComparisons(t *testing.T) {
|
||||
eval := tc.evaluator.WithKey(rlwe.NewMemEvaluationKeySet(kgen.GenRelinearizationKeyNew(sk), galKeys...))
|
||||
polyEval := float.NewPolynomialEvaluator(params, eval)
|
||||
|
||||
PWFEval := float.NewMinimaxCompositePolynomialEvaluator(params, eval, polyEval, btp)
|
||||
MCPEval := float.NewMinimaxCompositePolynomialEvaluator(params, eval, polyEval, btp)
|
||||
|
||||
polys := float.NewMinimaxCompositePolynomial(CoeffsMinimaxCompositePolynomialSignAlpha30Err35Prec20x4Cheby)
|
||||
|
||||
CmpEval := float.NewComparisonEvaluator(PWFEval, polys)
|
||||
|
||||
threshold := bignum.NewFloat(math.Exp2(-30), params.EncodingPrecision())
|
||||
CmpEval := float.NewComparisonEvaluator(MCPEval, polys)
|
||||
|
||||
t.Run(GetTestName(params, "Sign"), func(t *testing.T) {
|
||||
|
||||
@@ -94,14 +90,7 @@ func TestComparisons(t *testing.T) {
|
||||
want := make([]*big.Float, params.MaxSlots())
|
||||
|
||||
for i := range have {
|
||||
|
||||
if new(big.Float).Abs(values[i][0]).Cmp(threshold) == -1 {
|
||||
want[i] = new(big.Float).Set(values[i][0])
|
||||
} else if values[i][0].Cmp(new(big.Float)) == -1 {
|
||||
want[i] = bignum.NewFloat(-1, params.EncodingPrecision())
|
||||
} else {
|
||||
want[i] = bignum.NewFloat(1, params.EncodingPrecision())
|
||||
}
|
||||
want[i] = polys.Evaluate(values[i])[0]
|
||||
}
|
||||
|
||||
ckks.VerifyTestVectors(params, ecd, nil, want, have, params.LogDefaultScale(), nil, *printPrecisionStats, t)
|
||||
@@ -121,15 +110,12 @@ func TestComparisons(t *testing.T) {
|
||||
|
||||
want := make([]*big.Float, params.MaxSlots())
|
||||
|
||||
for i := range have {
|
||||
half := new(big.Float).SetFloat64(0.5)
|
||||
|
||||
if new(big.Float).Abs(values[i][0]).Cmp(threshold) == -1 {
|
||||
want[i] = new(big.Float).Set(values[i][0])
|
||||
} else if values[i][0].Cmp(new(big.Float)) == -1 {
|
||||
want[i] = bignum.NewFloat(0, params.EncodingPrecision())
|
||||
} else {
|
||||
want[i] = bignum.NewFloat(1, params.EncodingPrecision())
|
||||
}
|
||||
for i := range have {
|
||||
want[i] = polys.Evaluate(values[i])[0]
|
||||
want[i].Mul(want[i], half)
|
||||
want[i].Add(want[i], half)
|
||||
}
|
||||
|
||||
ckks.VerifyTestVectors(params, ecd, nil, want, have, params.LogDefaultScale(), nil, *printPrecisionStats, t)
|
||||
|
||||
@@ -43,6 +43,16 @@ func (mcp MinimaxCompositePolynomial) MaxDepth() (depth int) {
|
||||
return
|
||||
}
|
||||
|
||||
func (mcp MinimaxCompositePolynomial) Evaluate(x interface{}) (y *bignum.Complex) {
|
||||
y = mcp[0].Evaluate(x)
|
||||
|
||||
for _, p := range mcp[1:] {
|
||||
y = p.Evaluate(y)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CoeffsSignX2Cheby (from https://eprint.iacr.org/2019/1234.pdf) are the coefficients
|
||||
// of 1.5*x - 0.5*x^3 in Chebyshev basis.
|
||||
// Evaluating this polynomial on values already close to -1, or 1 ~doubles the number of
|
||||
|
||||
@@ -179,8 +179,12 @@ func (p *Polynomial) Evaluate(x interface{}) (y *Complex) {
|
||||
xcmplx = ToComplex(x, x.Prec())
|
||||
case *Complex:
|
||||
xcmplx = ToComplex(x, x.Prec())
|
||||
case complex128:
|
||||
xcmplx = ToComplex(x, 64)
|
||||
case float64:
|
||||
xcmplx = ToComplex(x, 64)
|
||||
default:
|
||||
panic(fmt.Errorf("cannot Evaluate: accepted x.(type) are *big.Float and *Complex but x is %T", x))
|
||||
panic(fmt.Errorf("cannot Evaluate: accepted x.(type) are *big.Float, *Complex, float64 and complex128 but x is %T", x))
|
||||
}
|
||||
|
||||
coeffs := p.Coeffs
|
||||
|
||||
Reference in New Issue
Block a user