Skip to content

Commit

Permalink
address first round of v6 comments
Browse files Browse the repository at this point in the history
  • Loading branch information
qantik committed Jul 29, 2024
1 parent c5c12f3 commit 3c9bbec
Show file tree
Hide file tree
Showing 92 changed files with 773 additions and 730 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package linear_transformation
package lintrans

Check failure on line 1 in circuits/bgv/lintrans/lintrans.go

View workflow job for this annotation

GitHub Actions / Run static checks

at least one file in a package should have a package comment (ST1000)

import (
"github.com/tuneinsight/lattigo/v5/circuits/common/linear_transformation"
"github.com/tuneinsight/lattigo/v5/circuits/common/lintrans"
"github.com/tuneinsight/lattigo/v5/core/rlwe"
"github.com/tuneinsight/lattigo/v5/schemes"
"github.com/tuneinsight/lattigo/v5/schemes/bgv"
Expand All @@ -10,12 +10,12 @@ import (

// Diagonals is a wrapper of [he.Diagonals].
// See [he.Diagonals] for the documentation.
type Diagonals[T bgv.Integer] linear_transformation.Diagonals[T]
type Diagonals[T bgv.Integer] lintrans.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 linear_transformation.Diagonals[T](m).DiagonalsIndexList()
return lintrans.Diagonals[T](m).DiagonalsIndexList()
}

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

keys := utils.GetKeys(m)

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

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

res = newVec(2 * slots)

Expand Down Expand Up @@ -76,7 +76,7 @@ type Permutation[T bgv.Integer] [2][]PermutationMapping[T]

// GetDiagonals returns the non-zero diagonals of the matrix
// representation of the permutation, which can be used to
// instantiate [LinearTransformationParameters].
// instantiate [Parameters].
func (p Permutation[T]) GetDiagonals(logSlots int) Diagonals[T] {

slots := 1 << (logSlots - 1) // matrix of 2 x 2^{logSlots}
Expand Down Expand Up @@ -106,73 +106,66 @@ func (p Permutation[T]) GetDiagonals(logSlots int) Diagonals[T] {
return Diagonals[T](diagonals)
}

// LinearTransformationParameters is a wrapper of he.LinearTransformationParameters.
// See he.LinearTransformationParameters for the documentation.
type LinearTransformationParameters linear_transformation.LinearTransformationParameters
// Parameters is a wrapper of [circuits/common/lintrans/Parameters].
// See circuits/common/lintrans/Parameters for the documentation.
type Parameters lintrans.Parameters

// LinearTransformation is a wrapper of [he.LinearTransformation].
// See [he.LinearTransformation] for the documentation.
type LinearTransformation linear_transformation.LinearTransformation
// Transformation is a wrapper of [circuits/common/lintrans/Transformation].
// See circuits/common/lintrans/Transformation for the documentation.
type Transformation lintrans.Transformation

// GaloisElements returns the list of Galois elements required to evaluate the linear transformation.
func (lt LinearTransformation) GaloisElements(params rlwe.ParameterProvider) []uint64 {
return linear_transformation.LinearTransformation(lt).GaloisElements(params)
func (lt Transformation) GaloisElements(params rlwe.ParameterProvider) []uint64 {
return lintrans.Transformation(lt).GaloisElements(params)
}

// NewLinearTransformation instantiates a new [LinearTransformation] and is a wrapper of [he.LinearTransformation].
// NewTransformation instantiates a new [Transformation] and is a wrapper of [he.LinearTransformation].
// See he.LinearTransformation for the documentation.
func NewLinearTransformation(params rlwe.ParameterProvider, lt LinearTransformationParameters) LinearTransformation {
return LinearTransformation(linear_transformation.NewLinearTransformation(params, linear_transformation.LinearTransformationParameters(lt)))
func NewTransformation(params rlwe.ParameterProvider, lt Parameters) Transformation {
return Transformation(lintrans.NewTransformation(params, lintrans.Parameters(lt)))
}

// EncodeLinearTransformation is a method used to encode EncodeLinearTransformation and a wrapper of [he.EncodeLinearTransformation].
// See [he.EncodeLinearTransformation] for the documentation.
func EncodeLinearTransformation[T bgv.Integer](ecd schemes.Encoder, diagonals Diagonals[T], allocated LinearTransformation) (err error) {
return linear_transformation.EncodeLinearTransformation(
// EncodeTransformation is a method used to encode EncodeTransformation and a wrapper of [he.EncodeTransformation].
// See [he.EncodeTransformation] for the documentation.
func EncodeTransformation[T bgv.Integer](ecd schemes.Encoder, diagonals Diagonals[T], allocated Transformation) (err error) {
return lintrans.EncodeTransformation(
ecd,
linear_transformation.Diagonals[T](diagonals),
linear_transformation.LinearTransformation(allocated))
lintrans.Diagonals[T](diagonals),
lintrans.Transformation(allocated))
}

// // GaloisElementsForLinearTransformation returns the list of Galois elements required to evaluate the linear transformation.
// func GaloisElementsForLinearTransformation(params rlwe.ParameterProvider, lt LinearTransformationParameters) (galEls []uint64) {
// return linear_transformation.GaloisElementsForLinearTransformation(params, lt.DiagonalsIndexList, 1<<lt.LogDimensions.Cols, lt.LogBabyStepGianStepRatio)
// }

// LinearTransformationEvaluator is a struct for evaluating linear transformations on [rlwe.Ciphertexts].
// Evaluator is a struct for evaluating linear transformations on [rlwe.Ciphertexts].
// All fields of this struct are public, enabling custom instantiations.
type LinearTransformationEvaluator struct {
linear_transformation.LinearTransformationEvaluator
//he.EvaluatorForLinearTransformation
// he.EvaluatorForDiagonalMatrix
type Evaluator struct {
lintrans.Evaluator
}

// NewLinearTransformationEvaluator instantiates a new [LinearTransformationEvaluator] from a circuit.EvaluatorForLinearTransformation.
// NewEvaluator instantiates a new [Evaluator] from a circuit.EvaluatorForLinearTransformation.
// The default [heint.Evaluator] is compliant to the circuit.EvaluatorForLinearTransformation interface.
func NewLinearTransformationEvaluator(eval schemes.Evaluator) (linTransEval *LinearTransformationEvaluator) {
return &LinearTransformationEvaluator{
linear_transformation.LinearTransformationEvaluator{
func NewEvaluator(eval schemes.Evaluator) (linTransEval *Evaluator) {
return &Evaluator{
lintrans.Evaluator{
Evaluator: eval,
},
}
}

// EvaluateNew takes as input a ciphertext ctIn and a linear transformation M and evaluate and returns opOut: M(ctIn).
func (eval LinearTransformationEvaluator) EvaluateNew(ctIn *rlwe.Ciphertext, linearTransformation LinearTransformation) (opOut *rlwe.Ciphertext, err error) {
ops, err := eval.EvaluateManyNew(ctIn, []LinearTransformation{linearTransformation})
func (eval Evaluator) EvaluateNew(ctIn *rlwe.Ciphertext, linearTransformation Transformation) (opOut *rlwe.Ciphertext, err error) {
ops, err := eval.EvaluateManyNew(ctIn, []Transformation{linearTransformation})
if err != nil {
return nil, err
}
return ops[0], nil
}

// Evaluate takes as input a ciphertext ctIn, a linear transformation M and evaluates opOut: M(ctIn).
func (eval LinearTransformationEvaluator) Evaluate(ctIn *rlwe.Ciphertext, linearTransformation LinearTransformation, opOut *rlwe.Ciphertext) (err error) {
return eval.EvaluateLinearTransformationsMany(ctIn, []linear_transformation.LinearTransformation{linear_transformation.LinearTransformation(linearTransformation)}, []*rlwe.Ciphertext{opOut})
func (eval Evaluator) Evaluate(ctIn *rlwe.Ciphertext, linearTransformation Transformation, opOut *rlwe.Ciphertext) (err error) {
return eval.EvaluateLinearTransformationsMany(ctIn, []lintrans.Transformation{lintrans.Transformation(linearTransformation)}, []*rlwe.Ciphertext{opOut})
}

// EvaluateManyNew takes as input a ciphertext ctIn and a list of linear transformations [M0, M1, M2, ...] and returns opOut:[M0(ctIn), M1(ctIn), M2(ctInt), ...].
func (eval LinearTransformationEvaluator) EvaluateManyNew(ctIn *rlwe.Ciphertext, linearTransformations []LinearTransformation) (opOut []*rlwe.Ciphertext, err error) {
func (eval Evaluator) EvaluateManyNew(ctIn *rlwe.Ciphertext, linearTransformations []Transformation) (opOut []*rlwe.Ciphertext, err error) {
params := eval.GetRLWEParameters()
opOut = make([]*rlwe.Ciphertext, len(linearTransformations))
for i := range opOut {
Expand All @@ -183,25 +176,25 @@ func (eval LinearTransformationEvaluator) EvaluateManyNew(ctIn *rlwe.Ciphertext,

// EvaluateMany takes as input a ciphertext ctIn, a list of linear transformations [M0, M1, M2, ...] and a list of pre-allocated receiver opOut
// and evaluates opOut: [M0(ctIn), M1(ctIn), M2(ctIn), ...]
func (eval LinearTransformationEvaluator) EvaluateMany(ctIn *rlwe.Ciphertext, linearTransformations []LinearTransformation, opOut []*rlwe.Ciphertext) (err error) {
circuitLTs := make([]linear_transformation.LinearTransformation, len(linearTransformations))
func (eval Evaluator) EvaluateMany(ctIn *rlwe.Ciphertext, linearTransformations []Transformation, opOut []*rlwe.Ciphertext) (err error) {
circuitLTs := make([]lintrans.Transformation, len(linearTransformations))
for i := range circuitLTs {
circuitLTs[i] = linear_transformation.LinearTransformation(linearTransformations[i])
circuitLTs[i] = lintrans.Transformation(linearTransformations[i])
}
return eval.EvaluateLinearTransformationsMany(ctIn, circuitLTs, opOut)
}

// EvaluateSequentialNew takes as input a ciphertext ctIn and a list of linear transformations [M0, M1, M2, ...] and returns opOut:...M2(M1(M0(ctIn))
func (eval LinearTransformationEvaluator) EvaluateSequentialNew(ctIn *rlwe.Ciphertext, linearTransformations []LinearTransformation) (opOut *rlwe.Ciphertext, err error) {
func (eval Evaluator) EvaluateSequentialNew(ctIn *rlwe.Ciphertext, linearTransformations []Transformation) (opOut *rlwe.Ciphertext, err error) {
opOut = rlwe.NewCiphertext(eval.GetRLWEParameters(), 1, linearTransformations[0].LevelQ)
return opOut, eval.EvaluateSequential(ctIn, linearTransformations, opOut)
}

// EvaluateSequential takes as input a ciphertext ctIn and a list of linear transformations [M0, M1, M2, ...] and returns opOut:...M2(M1(M0(ctIn))
func (eval LinearTransformationEvaluator) EvaluateSequential(ctIn *rlwe.Ciphertext, linearTransformations []LinearTransformation, opOut *rlwe.Ciphertext) (err error) {
circuitLTs := make([]linear_transformation.LinearTransformation, len(linearTransformations))
func (eval Evaluator) EvaluateSequential(ctIn *rlwe.Ciphertext, linearTransformations []Transformation, opOut *rlwe.Ciphertext) (err error) {
circuitLTs := make([]lintrans.Transformation, len(linearTransformations))
for i := range circuitLTs {
circuitLTs[i] = linear_transformation.LinearTransformation(linearTransformations[i])
circuitLTs[i] = lintrans.Transformation(linearTransformations[i])
}
return eval.EvaluateLinearTranformationSequential(ctIn, circuitLTs, opOut)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package linear_transformation
package lintrans

import (
"encoding/json"
Expand Down Expand Up @@ -79,7 +79,7 @@ func run(tc *bgv.TestContext, t *testing.T) {
}
}

ltparams := LinearTransformationParameters{
ltparams := Parameters{
DiagonalsIndexList: diagonals.DiagonalsIndexList(),
LevelQ: ciphertext.Level(),
LevelP: params.MaxLevelP(),
Expand All @@ -89,15 +89,15 @@ func run(tc *bgv.TestContext, t *testing.T) {
}

// Allocate the linear transformation
linTransf := NewLinearTransformation(params, ltparams)
linTransf := NewTransformation(params, ltparams)

// Encode on the linear transformation
require.NoError(t, EncodeLinearTransformation(tc.Ecd, diagonals, linTransf))
require.NoError(t, EncodeTransformation(tc.Ecd, diagonals, linTransf))

galEls := linTransf.GaloisElements(params)

eval := tc.Evl.WithKey(rlwe.NewMemEvaluationKeySet(nil, tc.Kgen.GenGaloisKeysNew(galEls, tc.Sk)...))
ltEval := NewLinearTransformationEvaluator(eval)
ltEval := NewEvaluator(eval)

require.NoError(t, ltEval.Evaluate(ciphertext, linTransf, ciphertext))

Expand All @@ -122,7 +122,7 @@ func run(tc *bgv.TestContext, t *testing.T) {
}
}

ltparams := LinearTransformationParameters{
ltparams := Parameters{
DiagonalsIndexList: diagonals.DiagonalsIndexList(),
LevelQ: ciphertext.Level(),
LevelP: params.MaxLevelP(),
Expand All @@ -132,15 +132,15 @@ func run(tc *bgv.TestContext, t *testing.T) {
}

// Allocate the linear transformation
linTransf := NewLinearTransformation(params, ltparams)
linTransf := NewTransformation(params, ltparams)

// Encode on the linear transformation
require.NoError(t, EncodeLinearTransformation(tc.Ecd, diagonals, linTransf))
require.NoError(t, EncodeTransformation(tc.Ecd, diagonals, linTransf))

galEls := linTransf.GaloisElements(params)

eval := tc.Evl.WithKey(rlwe.NewMemEvaluationKeySet(nil, tc.Kgen.GenGaloisKeysNew(galEls, tc.Sk)...))
ltEval := NewLinearTransformationEvaluator(eval)
ltEval := NewEvaluator(eval)

require.NoError(t, ltEval.Evaluate(ciphertext, linTransf, ciphertext))

Expand Down Expand Up @@ -196,7 +196,7 @@ func run(tc *bgv.TestContext, t *testing.T) {

values, _, ciphertext := bgv.NewTestVector(params, tc.Ecd, tc.Enc, params.MaxLevel(), params.DefaultScale())

ltparams := LinearTransformationParameters{
ltparams := Parameters{
DiagonalsIndexList: diagonals.DiagonalsIndexList(),
LevelQ: ciphertext.Level(),
LevelP: params.MaxLevelP(),
Expand All @@ -206,16 +206,16 @@ func run(tc *bgv.TestContext, t *testing.T) {
}

// Allocate the linear transformation
linTransf := NewLinearTransformation(params, ltparams)
linTransf := NewTransformation(params, ltparams)

// Encode on the linear transformation
require.NoError(t, EncodeLinearTransformation(tc.Ecd, diagonals, linTransf))
require.NoError(t, EncodeTransformation(tc.Ecd, diagonals, linTransf))

galEls := linTransf.GaloisElements(params)

evk := rlwe.NewMemEvaluationKeySet(nil, tc.Kgen.GenGaloisKeysNew(galEls, tc.Sk)...)

ltEval := NewLinearTransformationEvaluator(tc.Evl.WithKey(evk))
ltEval := NewEvaluator(tc.Evl.WithKey(evk))

require.NoError(t, ltEval.Evaluate(ciphertext, linTransf, ciphertext))

Expand Down
8 changes: 4 additions & 4 deletions circuits/bgv/polynomial/polynomial_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type PolynomialEvaluator struct {
bgv.Parameters
polynomial.PolynomialEvaluator[uint64]
polynomial.Evaluator[uint64]
InvariantTensoring bool
}

Expand Down Expand Up @@ -43,7 +43,7 @@ func NewPolynomialEvaluator(params bgv.Parameters, eval schemes.Evaluator, Invar

return &PolynomialEvaluator{
Parameters: params,
PolynomialEvaluator: polynomial.PolynomialEvaluator[uint64]{
Evaluator: polynomial.Evaluator[uint64]{
Evaluator: evalForPoly,
CoefficientGetter: CoefficientGetter{values: make([]uint64, params.MaxSlots())},
},
Expand Down Expand Up @@ -71,7 +71,7 @@ func (eval PolynomialEvaluator) Evaluate(ct *rlwe.Ciphertext, p interface{}, tar
phe = p
}

return eval.EvaluatePolynomial(ct, phe, targetScale, 1, &simEvaluator{eval.Parameters, eval.InvariantTensoring})
return eval.Polynomial(ct, phe, targetScale, 1, &simEvaluator{eval.Parameters, eval.InvariantTensoring})
}

// EvaluateFromPowerBasis evaluates a polynomial using the provided PowerBasis, holding pre-computed powers of X.
Expand All @@ -93,7 +93,7 @@ func (eval PolynomialEvaluator) EvaluateFromPowerBasis(pb polynomial.PowerBasis,
return nil, fmt.Errorf("cannot EvaluateFromPowerBasis: X^{1} is nil")
}

return eval.EvaluatePolynomial(pb, phe, targetScale, 1, &simEvaluator{eval.Parameters, eval.InvariantTensoring})
return eval.Polynomial(pb, phe, targetScale, 1, &simEvaluator{eval.Parameters, eval.InvariantTensoring})
}

// scaleInvariantEvaluator is a struct implementing the interface he.Evaluator with
Expand Down
Loading

0 comments on commit 3c9bbec

Please sign in to comment.