Skip to content

Commit

Permalink
he collapse
Browse files Browse the repository at this point in the history
  • Loading branch information
qantik committed Jul 5, 2024
1 parent 1e90d53 commit d11cc7f
Show file tree
Hide file tree
Showing 95 changed files with 337 additions and 14,442 deletions.
7 changes: 3 additions & 4 deletions circuits/bootstrapping/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/tuneinsight/lattigo/v5/circuits/mod1"
"github.com/tuneinsight/lattigo/v5/circuits/polynomial/polyfloat"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/schemes/ckks"
"github.com/tuneinsight/lattigo/v5/utils"
Expand Down Expand Up @@ -73,9 +72,9 @@ func NewEvaluator(btpParams Parameters, evk *EvaluationKeys) (eval *Evaluator, e
eval.Parameters = btpParams

if paramsN1.N() != paramsN2.N() {
eval.xPow2N1 = he.GenXPow2NTT(paramsN1.RingQ().AtLevel(0), paramsN2.LogN(), false)
eval.xPow2N2 = he.GenXPow2NTT(paramsN2.RingQ().AtLevel(0), paramsN2.LogN(), false)
eval.xPow2InvN2 = he.GenXPow2NTT(paramsN2.RingQ(), paramsN2.LogN(), true)
eval.xPow2N1 = rlwe.GenXPow2NTT(paramsN1.RingQ().AtLevel(0), paramsN2.LogN(), false)
eval.xPow2N2 = rlwe.GenXPow2NTT(paramsN2.RingQ().AtLevel(0), paramsN2.LogN(), false)
eval.xPow2InvN2 = rlwe.GenXPow2NTT(paramsN2.RingQ(), paramsN2.LogN(), true)
}

if btpParams.Mod1ParametersLiteral.Mod1Type == mod1.SinContinuous && btpParams.Mod1ParametersLiteral.DoubleAngle != 0 {
Expand Down
7 changes: 3 additions & 4 deletions circuits/bootstrapping/parameters_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/tuneinsight/lattigo/v5/circuits/mod1"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he/hefloat"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/utils"
)
Expand Down Expand Up @@ -112,7 +111,7 @@ import (
// When using a small ratio (i.e. 2^4), for example if ct.PlaintextScale is close to Q[0] is small or if |m| is large, the Mod1InvDegree can be set to
// a non zero value (i.e. 5 or 7). This will greatly improve the precision of the bootstrapping, at the expense of slightly increasing its depth.
//
// Mod1Type: the type of approximation for the modular reduction polynomial. By default set to hefloat.CosDiscrete.
// Mod1Type: the type of approximation for the modular reduction polynomial. By default set to mod1.CosDiscrete.
//
// K: the range of the approximation interval, by default set to 16.
//
Expand All @@ -132,7 +131,7 @@ type ParametersLiteral struct {
EvalModLogScale *int // Default: 60
EphemeralSecretWeight *int // Default: 32
IterationsParameters *IterationsParameters // Default: nil (default starting level of 0 and 1 iteration)
Mod1Type mod1.Mod1Type // Default: hefloat.CosDiscrete
Mod1Type mod1.Mod1Type // Default: mod1.CosDiscrete
LogMessageRatio *int // Default: 8
K *int // Default: 16
Mod1Degree *int // Default: 30
Expand Down Expand Up @@ -166,7 +165,7 @@ const (
// DefaultIterations is the default number of bootstrapping iterations.
DefaultIterations = 1
// DefaultMod1Type is the default function and approximation technique for the homomorphic modular reduction polynomial.
DefaultMod1Type = hefloat.CosDiscrete
DefaultMod1Type = mod1.CosDiscrete
// DefaultLogMessageRatio is the default ratio between Q[0] and |m|.
DefaultLogMessageRatio = 8
// DefaultK is the default interval [-K+1, K-1] for the polynomial approximation of the homomorphic modular reduction.
Expand Down
6 changes: 3 additions & 3 deletions circuits/dft/dft.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"math/big"
"slices"

"github.com/tuneinsight/lattigo/v5/circuits/linear_transformation"
"github.com/tuneinsight/lattigo/v5/circuits/linear_transformation/ltfloat"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/schemes/ckks"
"github.com/tuneinsight/lattigo/v5/utils"
Expand Down Expand Up @@ -118,7 +118,7 @@ func (d DFTMatrixLiteral) GaloisElements(params ckks.Parameters) (galEls []uint6

// Coeffs to Slots rotations
for i, pVec := range indexCtS {
N1 := he.FindBestBSGSRatio(utils.GetKeys(pVec), dslots, d.LogBSGSRatio)
N1 := linear_transformation.FindBestBSGSRatio(utils.GetKeys(pVec), dslots, d.LogBSGSRatio)
rotations = addMatrixRotToList(pVec, rotations, N1, slots, d.Type == HomomorphicDecode && logSlots < logN-1 && i == 0 && imgRepack)
}

Expand Down Expand Up @@ -200,7 +200,7 @@ func NewDFTMatrixFromLiteral(params ckks.Parameters, d DFTMatrixLiteral, encoder

mat := ltfloat.NewLinearTransformation(params, ltparams)

if err := ltfloat.EncodeLinearTransformation[*bignum.Complex](encoder, pVecDFT[idx], mat); err != nil {
if err := ltfloat.EncodeLinearTransformation(encoder, pVecDFT[idx], mat); err != nil {
return DFTMatrix{}, fmt.Errorf("cannot NewDFTMatrixFromLiteral: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions circuits/inverse/inverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"math"

"github.com/tuneinsight/lattigo/v5/circuits/bootstrapping"
"github.com/tuneinsight/lattigo/v5/circuits/comparison"
"github.com/tuneinsight/lattigo/v5/circuits/minimax"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he"
"github.com/tuneinsight/lattigo/v5/schemes/ckks"
"github.com/tuneinsight/lattigo/v5/utils"
)
Expand Down Expand Up @@ -310,7 +310,7 @@ func (eval InverseEvaluator) GoldschmidtDivisionNew(ct *rlwe.Ciphertext, log2min
// The normalization factor is independant to each slot:
// - values smaller than 1 will have a normalization factor that tends to 1
// - values greater than 1 will have a normalization factor that tends to 1/x
func (eval InverseEvaluator) IntervalNormalization(ct *rlwe.Ciphertext, log2Max float64, btp he.Bootstrapper[rlwe.Ciphertext]) (ctNorm, ctNormFac *rlwe.Ciphertext, err error) {
func (eval InverseEvaluator) IntervalNormalization(ct *rlwe.Ciphertext, log2Max float64, btp bootstrapping.Bootstrapper) (ctNorm, ctNormFac *rlwe.Ciphertext, err error) {

ctNorm = ct.CopyNew()

Expand Down
4 changes: 2 additions & 2 deletions circuits/inverse/inverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/tuneinsight/lattigo/v5/circuits/bootstrapping"
"github.com/tuneinsight/lattigo/v5/circuits/comparison"
"github.com/tuneinsight/lattigo/v5/circuits/minimax"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he/hefloat"
"github.com/tuneinsight/lattigo/v5/ring"
"github.com/tuneinsight/lattigo/v5/schemes/ckks"
"github.com/tuneinsight/lattigo/v5/utils/bignum"
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestInverse(t *testing.T) {

invEval := NewInverseEvaluator(tc.Params, minEvl)

cInv, err := invEval.EvaluateFullDomainNew(ct, logmin, logmax, minimax.NewMinimaxCompositePolynomial(hefloat.DefaultMinimaxCompositePolynomialForSign))
cInv, err := invEval.EvaluateFullDomainNew(ct, logmin, logmax, minimax.NewMinimaxCompositePolynomial(comparison.DefaultMinimaxCompositePolynomialForSign))
require.NoError(t, err)

have := make([]*big.Float, tc.Params.MaxSlots())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package ltfloat
import (
"github.com/tuneinsight/lattigo/v5/circuits/linear_transformation"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he"
"github.com/tuneinsight/lattigo/v5/schemes"
"github.com/tuneinsight/lattigo/v5/schemes/ckks"
"github.com/tuneinsight/lattigo/v5/utils"
)

// Diagonals is a wrapper of [he.Diagonals].
// See [he.Diagonals] for the documentation.
type Diagonals[T ckks.Float] he.Diagonals[T]
type Diagonals[T ckks.Float] linear_transformation.Diagonals[T]

// DiagonalsIndexList returns the list of the non-zero diagonals of the square matrix.
// A non zero diagonals is a diagonal with a least one non-zero element.
func (m Diagonals[T]) DiagonalsIndexList() (indexes []int) {
return he.Diagonals[T](m).DiagonalsIndexList()
return linear_transformation.Diagonals[T](m).DiagonalsIndexList()
}

// Evaluate evaluates the linear transformation on the provided vector.
Expand All @@ -28,9 +27,9 @@ func (m Diagonals[T]) Evaluate(vector []T, newVec func(size int) []T, add func(a

keys := utils.GetKeys(m)

N1 := he.FindBestBSGSRatio(keys, slots, 1)
N1 := linear_transformation.FindBestBSGSRatio(keys, slots, 1)

index, _, _ := he.BSGSIndex(keys, slots, N1)
index, _, _ := linear_transformation.BSGSIndex(keys, slots, N1)

res = newVec(slots)

Expand Down
2 changes: 1 addition & 1 deletion he/hefloat/README.md → circuits/minimax/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## References

1. Minimax Approximation of Sign Function by Composite Polynomial for Homomorphic Comparison (<https://ieeexplore.ieee.org/abstract/document/9517029>)
1. Minimax Approximation of Sign Function by Composite Polynomial for Homomorphic Comparison (<https://ieeexplore.ieee.org/abstract/document/9517029>)
3 changes: 1 addition & 2 deletions circuits/polynomial/polyint/polynomial_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/tuneinsight/lattigo/v5/circuits/polynomial"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/he"
"github.com/tuneinsight/lattigo/v5/schemes"
"github.com/tuneinsight/lattigo/v5/schemes/bfv"
"github.com/tuneinsight/lattigo/v5/schemes/bgv"
Expand Down Expand Up @@ -78,7 +77,7 @@ func (eval PolynomialEvaluator) Evaluate(ct *rlwe.Ciphertext, p interface{}, tar
// EvaluateFromPowerBasis evaluates a polynomial using the provided PowerBasis, holding pre-computed powers of X.
// This method is the same as Evaluate except that the encrypted input is a PowerBasis.
// See Evaluate for additional information.
func (eval PolynomialEvaluator) EvaluateFromPowerBasis(pb he.PowerBasis, p interface{}, targetScale rlwe.Scale) (opOut *rlwe.Ciphertext, err error) {
func (eval PolynomialEvaluator) EvaluateFromPowerBasis(pb polynomial.PowerBasis, p interface{}, targetScale rlwe.Scale) (opOut *rlwe.Ciphertext, err error) {

var phe interface{}
switch p := p.(type) {
Expand Down
12 changes: 6 additions & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Application examples are examples showcasing specific capabilities of the librar
- `high_precision`: an example showcasing high-precision bootstrapping.
- `slim`: an example showcasing slim bootstrapping, i.e. re-ordering the steps of the bootstrapping.

- `reals_scheme_switching`: an example showcasing scheme switching between `hefloat` and `hebin` to complement fixed-point arithmetic with lookup tables.
- `reals_scheme_switching`: an example showcasing scheme switching between `ckks` and `rgsw` to complement fixed-point arithmetic with lookup tables.
- `reals_sigmoid_chebyshev`: an example showcasing polynomial evaluation of a Chebyshev approximation of the sigmoid.
- `reals_sigmoid_minimax`: an example showcasing polynomial evaluation of a minimax approximation of the sigmoid.
- `reals_vectorized_polynomial_evaluation`: an example showcasing vectorized polynomial evaluation, i.e. evaluating different polynomials in parallel on specific slots.
Expand All @@ -29,14 +29,14 @@ Application examples are examples showcasing specific capabilities of the librar

Templates are files containing the basic instantiation, i.e. parameters, key-generation, encoding, encryption and decryption.

- `reals`: a template for `hefloat`.
- `int`: a template for `heint`.
- `reals`: a template for `ckks`.
- `int`: a template for `bgv`.

## Tutorials

Tutorials are examples showcasing the basic capabilities of the library.

- `reals`: a tutorial on all the basic capabilities of the package `hefloat`.
- `reals`: a tutorial on all the basic capabilities of the package `ckks`.

# Multi Party Examples

Expand All @@ -46,8 +46,8 @@ Tutorials are examples showcasing the basic capabilities of the library.

## Parameters

The `params.go` file contains several sets of example parameters for both `heint` and `hefloat`.
The `params.go` file contains several sets of example parameters for both `bgv` and `ckks`.
These parameter are chosen to represent several degrees of homomorphic capacity for a fixed 128-bit security
(according to the standard estimates at the time of writing). They do not represent a set of default parameters
to be used in real HE applications. Rather, they are meant to facilitate quick tests and experimentation
with the library.
with the library.
28 changes: 14 additions & 14 deletions examples/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ package examples
import (
"testing"

"github.com/tuneinsight/lattigo/v5/he/hefloat"
"github.com/tuneinsight/lattigo/v5/he/heint"
"github.com/tuneinsight/lattigo/v5/schemes/bgv"
"github.com/tuneinsight/lattigo/v5/schemes/ckks"
)

func TestExampleParams(t *testing.T) {
for _, pl := range HEIntParams {
p, err := heint.NewParametersFromLiteral(pl)
for _, pl := range BGVParams {
p, err := bgv.NewParametersFromLiteral(pl)
if err != nil {
t.Fatal(err)
}
p.RingQ()
t.Logf("HEIntParams: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
t.Logf("BGVParams: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
}

for _, pl := range HEIntScaleInvariantParams {
p, err := heint.NewParametersFromLiteral(pl)
for _, pl := range BGVScaleInvariantParams {
p, err := bgv.NewParametersFromLiteral(pl)
if err != nil {
t.Fatal(err)
}
p.RingQ()
t.Logf("HEIntScaleInvariantParams: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
t.Logf("BGVScaleInvariantParams: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
}

for _, pl := range HEFloatComplexParams {
p, err := hefloat.NewParametersFromLiteral(pl)
for _, pl := range CKKSComplexParams {
p, err := ckks.NewParametersFromLiteral(pl)
if err != nil {
t.Fatal(err)
}
p.RingQ()
t.Logf("HEFloatComplex: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
t.Logf("CKKSComplex: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
}

for _, pl := range HEFloatRealParams {
p, err := hefloat.NewParametersFromLiteral(pl)
for _, pl := range CKKSRealParams {
p, err := ckks.NewParametersFromLiteral(pl)
if err != nil {
t.Fatal(err)
}
p.RingQ()
t.Logf("HEFloatReal: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
t.Logf("CKKSReal: LogN: %d - LogQP: %12.7f - LogSlots: %d", p.LogN(), p.LogQP(), p.LogMaxSlots())
}
}
Loading

0 comments on commit d11cc7f

Please sign in to comment.