advanced

package
v0.0.0-...-e525fad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package advanced implements advanced operations for the CKKS scheme.

Index

Constants

View Source
const (
	CoeffsToSlots = LinearTransformType(0) // Homomorphic Encoding
	SlotsToCoeffs = LinearTransformType(1) // Homomorphic Decoding
)

CoeffsToSlots and SlotsToCoeffs are two linear transformations.

View Source
const (
	Sin  = SineType(0) // Standard Chebyshev approximation of (1/2pi) * sin(2pix)
	Cos1 = SineType(1) // Special approximation (Han and Ki) of pow((1/2pi), 1/2^r) * cos(2pi(x-0.25)/2^r); this method requires a minimum degree of 2*(K-1).
	Cos2 = SineType(2) // Standard Chebyshev approximation of pow((1/2pi), 1/2^r) * cos(2pi(x-0.25)/2^r)
)

Sin and Cos are the two proposed functions for SineType

Variables

This section is empty.

Functions

func ApproximateCos

func ApproximateCos(K, degree int, dev float64, scnum int) []complex128

ApproximateCos computes a polynomial approximation of degree "degree" in Chevyshev basis of the function cos(2*pi*x/2^"scnum") in the range -"K" to "K" The nodes of the Chevyshev approximation are are located from -dev to +dev at each integer value between -K and -K

func BigintCos

func BigintCos(x *big.Float) (cosx *big.Float)

BigintCos is an iterative arbitrary precision computation of Cos(x) Iterative process with an error of ~10^{−0.60206*k} after k iterations. ref : Johansson, B. Tomas, An elementary algorithm to evaluate trigonometric functions to high precision, 2018

func BigintSin

func BigintSin(x *big.Float) (sinx *big.Float)

BigintSin is an iterative arbitrary precision computation of Sin(x)

func NewFloat

func NewFloat(x float64) (y *big.Float)

NewFloat creates a new big.Float element with 1000 bits of precision

Types

type EncodingMatrix

type EncodingMatrix struct {
	EncodingMatrixLiteral
	// contains filtered or unexported fields
}

EncodingMatrix is a struct storing the factorized DFT matrix

func NewHomomorphicEncodingMatrixFromLiteral

func NewHomomorphicEncodingMatrixFromLiteral(mParams EncodingMatrixLiteral, encoder ckks.Encoder) EncodingMatrix

NewHomomorphicEncodingMatrixFromLiteral generates the factorized encoding matrix. scaling : constant by witch the all the matrices will be multuplied by. encoder : ckks.Encoder.

type EncodingMatrixLiteral

type EncodingMatrixLiteral struct {
	LinearTransformType LinearTransformType
	RepackImag2Real     bool    // If true, the imaginary part is repacked into the right n slots of the real part
	LogN                int     // Log2(RingDegree)
	LogSlots            int     // Log2(slots)
	Scaling             float64 // Constant by which the matrix is multiplied
	LevelStart          int     // Encoding level
	BitReversed         bool    // If true, then applies the transformation bit-reversed and expects bit-reversed inputs
	BSGSRatio           float64 // n1/n2 ratio for the bsgs algo for matrix x vector eval
	ScalingFactor       [][]float64
}

EncodingMatrixLiteral is a struct storing the parameters to generate the factorized DFT matrix.

func (*EncodingMatrixLiteral) Depth

func (mParams *EncodingMatrixLiteral) Depth(actual bool) (depth int)

Depth returns the number of levels allocated. If actual == true then returns the number of moduli consumed, else returns the factorization depth.

func (*EncodingMatrixLiteral) Levels

func (mParams *EncodingMatrixLiteral) Levels() (levels []int)

Levels returns the index of the Qi used int CoeffsToSlots.

func (*EncodingMatrixLiteral) MarshalBinary

func (mParams *EncodingMatrixLiteral) MarshalBinary() (data []byte, err error)

MarshalBinary encode the target EncodingMatrixParameters on a slice of bytes.

func (*EncodingMatrixLiteral) Rotations

func (mParams *EncodingMatrixLiteral) Rotations() (rotations []int)

Rotations returns the list of rotations performed during the CoeffsToSlot operation.

func (*EncodingMatrixLiteral) UnmarshalBinary

func (mParams *EncodingMatrixLiteral) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes a slice of bytes on the target EncodingMatrixParameters.

type EvalModLiteral

type EvalModLiteral struct {
	Q             uint64   // Q to reduce by during EvalMod
	LevelStart    int      // Starting level of EvalMod
	ScalingFactor float64  // Scaling factor used during EvalMod
	SineType      SineType // Chose between [Sin(2*pi*x)] or [cos(2*pi*x/r) with double angle formula]
	MessageRatio  float64  // Ratio between Q0 and m, i.e. Q[0]/|m|
	K             int      // K parameter (interpolation in the range -K to K)
	SineDeg       int      // Degree of the interpolation
	DoubleAngle   int      // Number of rescale and double angle formula (only applies for cos)
	ArcSineDeg    int      // Degree of the Taylor arcsine composed with f(2*pi*x) (if zero then not used)
}

EvalModLiteral a struct for the parameters of the EvalMod step of the bootstrapping

func (*EvalModLiteral) Depth

func (evm *EvalModLiteral) Depth() (depth int)

Depth returns the depth of the SineEval. If true, then also counts the double angle formula.

func (*EvalModLiteral) MarshalBinary

func (evm *EvalModLiteral) MarshalBinary() (data []byte, err error)

MarshalBinary encode the target EvalModParameters on a slice of bytes.

func (*EvalModLiteral) QDiff

func (evm *EvalModLiteral) QDiff() float64

QDiff return Q/ClosestedPow2 This is the error introduced by the approximate division by Q

func (*EvalModLiteral) UnmarshalBinary

func (evm *EvalModLiteral) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary decodes a slice of bytes on the target EvalModParameters.

type EvalModPoly

type EvalModPoly struct {
	// contains filtered or unexported fields
}

EvalModPoly is a struct storing the EvalModLiteral with the polynomials.

func NewEvalModPolyFromLiteral

func NewEvalModPolyFromLiteral(evm EvalModLiteral) EvalModPoly

NewEvalModPolyFromLiteral generates an EvalModPoly from the EvalModLiteral.

func (*EvalModPoly) A

func (evp *EvalModPoly) A() float64

A returns the left bound of the sine approximation (scaled by 1/2^r).

func (*EvalModPoly) B

func (evp *EvalModPoly) B() float64

B returns the right bound of the sine approximation (scaled by 1/2^r).

func (*EvalModPoly) K

func (evp *EvalModPoly) K() float64

K return the sine approximation range.

func (*EvalModPoly) LevelStart

func (evp *EvalModPoly) LevelStart() int

LevelStart returns the starting level of the EvalMod.

func (*EvalModPoly) MessageRatio

func (evp *EvalModPoly) MessageRatio() float64

MessageRatio returns the pre-set ratio Q[0]/|m|.

func (*EvalModPoly) QDiff

func (evp *EvalModPoly) QDiff() float64

QDiff return Q/ClosestedPow2 This is the error introduced by the approximate division by Q.

func (*EvalModPoly) ScFac

func (evp *EvalModPoly) ScFac() float64

ScFac returns 1/2^r where r is the number of double angle evaluation.

func (*EvalModPoly) ScalingFactor

func (evp *EvalModPoly) ScalingFactor() float64

ScalingFactor returns scaling factor used during the EvalMod.

type Evaluator

type Evaluator interface {
	Add(ctIn *ckks.Ciphertext, op1 ckks.Operand, ctOut *ckks.Ciphertext)
	AddNoMod(ctIn *ckks.Ciphertext, op1 ckks.Operand, ctOut *ckks.Ciphertext)
	AddNew(ctIn *ckks.Ciphertext, op1 ckks.Operand) (ctOut *ckks.Ciphertext)
	AddNoModNew(ctIn *ckks.Ciphertext, op1 ckks.Operand) (ctOut *ckks.Ciphertext)
	Sub(ctIn *ckks.Ciphertext, op1 ckks.Operand, ctOut *ckks.Ciphertext)
	SubNoMod(ctIn *ckks.Ciphertext, op1 ckks.Operand, ctOut *ckks.Ciphertext)
	SubNew(ctIn *ckks.Ciphertext, op1 ckks.Operand) (ctOut *ckks.Ciphertext)
	SubNoModNew(ctIn *ckks.Ciphertext, op1 ckks.Operand) (ctOut *ckks.Ciphertext)
	Neg(ctIn *ckks.Ciphertext, ctOut *ckks.Ciphertext)
	NegNew(ctIn *ckks.Ciphertext) (ctOut *ckks.Ciphertext)
	AddConstNew(ctIn *ckks.Ciphertext, constant interface{}) (ctOut *ckks.Ciphertext)
	AddConst(ctIn *ckks.Ciphertext, constant interface{}, ctOut *ckks.Ciphertext)
	MultByConstNew(ctIn *ckks.Ciphertext, constant interface{}) (ctOut *ckks.Ciphertext)
	MultByConst(ctIn *ckks.Ciphertext, constant interface{}, ctOut *ckks.Ciphertext)
	MultByGaussianInteger(ctIn *ckks.Ciphertext, cReal, cImag interface{}, ctOut *ckks.Ciphertext)
	MultByConstAndAdd(ctIn *ckks.Ciphertext, constant interface{}, ctOut *ckks.Ciphertext)
	MultByGaussianIntegerAndAdd(ctIn *ckks.Ciphertext, cReal, cImag interface{}, ctOut *ckks.Ciphertext)
	MultByiNew(ctIn *ckks.Ciphertext) (ctOut *ckks.Ciphertext)
	MultByi(ctIn *ckks.Ciphertext, ctOut *ckks.Ciphertext)
	DivByiNew(ctIn *ckks.Ciphertext) (ctOut *ckks.Ciphertext)
	DivByi(ctIn *ckks.Ciphertext, ctOut *ckks.Ciphertext)
	ConjugateNew(ctIn *ckks.Ciphertext) (ctOut *ckks.Ciphertext)
	Conjugate(ctIn *ckks.Ciphertext, ctOut *ckks.Ciphertext)
	Mul(ctIn *ckks.Ciphertext, op1 ckks.Operand, ctOut *ckks.Ciphertext)
	MulNew(ctIn *ckks.Ciphertext, op1 ckks.Operand) (ctOut *ckks.Ciphertext)
	MulRelin(ctIn *ckks.Ciphertext, op1 ckks.Operand, ctOut *ckks.Ciphertext)
	MulRelinNew(ctIn *ckks.Ciphertext, op1 ckks.Operand) (ctOut *ckks.Ciphertext)
	RotateNew(ctIn *ckks.Ciphertext, k int) (ctOut *ckks.Ciphertext)
	Rotate(ctIn *ckks.Ciphertext, k int, ctOut *ckks.Ciphertext)
	RotateHoistedNew(ctIn *ckks.Ciphertext, rotations []int) (ctOut map[int]*ckks.Ciphertext)
	RotateHoisted(ctIn *ckks.Ciphertext, rotations []int, ctOut map[int]*ckks.Ciphertext)
	MulByPow2New(ctIn *ckks.Ciphertext, pow2 int) (ctOut *ckks.Ciphertext)
	MulByPow2(ctIn *ckks.Ciphertext, pow2 int, ctOut *ckks.Ciphertext)
	PowerOf2(ctIn *ckks.Ciphertext, logPow2 int, ctOut *ckks.Ciphertext)
	Power(ctIn *ckks.Ciphertext, degree int, ctOut *ckks.Ciphertext)
	PowerNew(ctIn *ckks.Ciphertext, degree int) (ctOut *ckks.Ciphertext)
	EvaluatePoly(input interface{}, pol *ckks.Polynomial, targetScale float64) (ctOut *ckks.Ciphertext, err error)
	EvaluatePolyVector(input interface{}, pols []*ckks.Polynomial, encoder ckks.Encoder, slotIndex map[int][]int, targetScale float64) (ctOut *ckks.Ciphertext, err error)
	InverseNew(ctIn *ckks.Ciphertext, steps int) (ctOut *ckks.Ciphertext)
	LinearTransformNew(ctIn *ckks.Ciphertext, linearTransform interface{}) (ctOut []*ckks.Ciphertext)
	LinearTransform(ctIn *ckks.Ciphertext, linearTransform interface{}, ctOut []*ckks.Ciphertext)
	MultiplyByDiagMatrix(ctIn *ckks.Ciphertext, matrix ckks.LinearTransform, c2DecompQP []ringqp.Poly, ctOut *ckks.Ciphertext)
	MultiplyByDiagMatrixBSGS(ctIn *ckks.Ciphertext, matrix ckks.LinearTransform, c2DecompQP []ringqp.Poly, ctOut *ckks.Ciphertext)
	InnerSumLog(ctIn *ckks.Ciphertext, batch, n int, ctOut *ckks.Ciphertext)
	InnerSum(ctIn *ckks.Ciphertext, batch, n int, ctOut *ckks.Ciphertext)
	ReplicateLog(ctIn *ckks.Ciphertext, batch, n int, ctOut *ckks.Ciphertext)
	Replicate(ctIn *ckks.Ciphertext, batch, n int, ctOut *ckks.Ciphertext)
	TraceNew(ctIn *ckks.Ciphertext, logSlots int) *ckks.Ciphertext
	Trace(ctIn *ckks.Ciphertext, logSlots int, ctOut *ckks.Ciphertext)
	SwitchKeysNew(ctIn *ckks.Ciphertext, switchingKey *rlwe.SwitchingKey) (ctOut *ckks.Ciphertext)
	SwitchKeys(ctIn *ckks.Ciphertext, switchingKey *rlwe.SwitchingKey, ctOut *ckks.Ciphertext)
	RelinearizeNew(ctIn *ckks.Ciphertext) (ctOut *ckks.Ciphertext)
	Relinearize(ctIn *ckks.Ciphertext, ctOut *ckks.Ciphertext)
	ScaleUpNew(ctIn *ckks.Ciphertext, scale float64) (ctOut *ckks.Ciphertext)
	ScaleUp(ctIn *ckks.Ciphertext, scale float64, ctOut *ckks.Ciphertext)
	SetScale(ctIn *ckks.Ciphertext, scale float64)
	Rescale(ctIn *ckks.Ciphertext, minScale float64, ctOut *ckks.Ciphertext) (err error)
	DropLevelNew(ctIn *ckks.Ciphertext, levels int) (ctOut *ckks.Ciphertext)
	DropLevel(ctIn *ckks.Ciphertext, levels int)
	ReduceNew(ctIn *ckks.Ciphertext) (ctOut *ckks.Ciphertext)
	Reduce(ctIn *ckks.Ciphertext, ctOut *ckks.Ciphertext) error

	CoeffsToSlotsNew(ctIn *ckks.Ciphertext, ctsMatrices EncodingMatrix) (ctReal, ctImag *ckks.Ciphertext)
	CoeffsToSlots(ctIn *ckks.Ciphertext, ctsMatrices EncodingMatrix, ctReal, ctImag *ckks.Ciphertext)
	SlotsToCoeffsNew(ctReal, ctImag *ckks.Ciphertext, stcMatrices EncodingMatrix) (ctOut *ckks.Ciphertext)
	SlotsToCoeffs(ctReal, ctImag *ckks.Ciphertext, stcMatrices EncodingMatrix, ctOut *ckks.Ciphertext)
	EvalModNew(ctIn *ckks.Ciphertext, evalModPoly EvalModPoly) (ctOut *ckks.Ciphertext)

	GetRLWEEvaluator() *rlwe.Evaluator
	BuffQ() [3]*ring.Poly
	BuffCt() *ckks.Ciphertext
	ShallowCopy() Evaluator
	WithKey(rlwe.EvaluationKey) Evaluator
}

Evaluator is an interface embedding the ckks.Evaluator interface with additional advanced arithmetic features.

func NewEvaluator

func NewEvaluator(params ckks.Parameters, evaluationKey rlwe.EvaluationKey) Evaluator

NewEvaluator creates a new Evaluator.

type LinearTransformType

type LinearTransformType int

LinearTransformType is a type used to distinguish different linear transformations.

type SineType

type SineType uint64

SineType is the type of function used during the bootstrapping for the homomorphic modular reduction

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL