From abcdfcf2e6411a3c9d68801dcce851471b05af71 Mon Sep 17 00:00:00 2001 From: Hwang In Tak Date: Mon, 22 Apr 2024 09:41:41 +0900 Subject: [PATCH] feat: Remove ClearBit --- mktfhe/decryptor.go | 12 ++++++------ mktfhe/encryptor_public.go | 2 +- mktfhe/evaluator.go | 22 +++++++++++----------- tfhe/bootstrap.go | 20 -------------------- tfhe/evaluator.go | 7 +------ 5 files changed, 19 insertions(+), 44 deletions(-) diff --git a/mktfhe/decryptor.go b/mktfhe/decryptor.go index 51ef64c..3b600ec 100644 --- a/mktfhe/decryptor.go +++ b/mktfhe/decryptor.go @@ -54,24 +54,24 @@ type decryptionBuffer[T tfhe.TorusInt] struct { // NewDecryptor allocates an empty Decryptor. // Only indices between 0 and params.PartyCount is valid for sk. func NewDecryptor[T tfhe.TorusInt](params Parameters[T], sk map[int]tfhe.SecretKey[T]) *Decryptor[T] { - encs := make([]*tfhe.Encryptor[T], len(sk)) - sks := make([]tfhe.SecretKey[T], len(sk)) + singleEncs := make([]*tfhe.Encryptor[T], len(sk)) + singleKeys := make([]tfhe.SecretKey[T], len(sk)) partyBitMap := make([]bool, params.PartyCount()) for i := range sk { - encs[i] = tfhe.NewEncryptorWithKey(params.Parameters, sk[i]) - sks[i] = sk[i] + singleEncs[i] = tfhe.NewEncryptorWithKey(params.Parameters, sk[i]) + singleKeys[i] = sk[i] partyBitMap[i] = true } return &Decryptor[T]{ Encoder: tfhe.NewEncoder(params.Parameters), GLWETransformer: NewGLWETransformer(params), - SingleKeyDecryptors: encs, + SingleKeyDecryptors: singleEncs, Parameters: params, PartyBitMap: partyBitMap, - SecretKeys: sks, + SecretKeys: singleKeys, buffer: newDecryptionBuffer(params), } diff --git a/mktfhe/encryptor_public.go b/mktfhe/encryptor_public.go index 5509184..66da58d 100644 --- a/mktfhe/encryptor_public.go +++ b/mktfhe/encryptor_public.go @@ -46,7 +46,7 @@ func NewPublicEncryptor[T tfhe.TorusInt](params Parameters[T], idx int, publicKe } return &PublicEncryptor[T]{ - Encoder: tfhe.NewEncoder[T](params.Parameters), + Encoder: tfhe.NewEncoder(params.Parameters), GLWETransformer: NewGLWETransformer(params), SingleKeyPublicEncryptor: tfhe.NewPublicEncryptor(params.Parameters, publicKey), diff --git a/mktfhe/evaluator.go b/mktfhe/evaluator.go index 9dfa7a9..980593a 100644 --- a/mktfhe/evaluator.go +++ b/mktfhe/evaluator.go @@ -84,12 +84,12 @@ type evaluationBuffer[T tfhe.TorusInt] struct { // NewEvaluator allocates an empty Evaluator. // Only indices between 0 and params.PartyCount is valid for evk. func NewEvaluator[T tfhe.TorusInt](params Parameters[T], evk map[int]EvaluationKey[T]) *Evaluator[T] { - evals := make([]*tfhe.Evaluator[T], params.PartyCount()) - evks := make([]EvaluationKey[T], params.PartyCount()) + singleEvals := make([]*tfhe.Evaluator[T], params.PartyCount()) + singleKeys := make([]EvaluationKey[T], params.PartyCount()) partyBitMap := make([]bool, params.PartyCount()) for i := range evk { - evals[i] = tfhe.NewEvaluator(params.Parameters, evk[i].EvaluationKey) - evks[i] = evk[i] + singleEvals[i] = tfhe.NewEvaluator(params.Parameters, evk[i].EvaluationKey) + singleKeys[i] = evk[i] partyBitMap[i] = true } @@ -97,12 +97,12 @@ func NewEvaluator[T tfhe.TorusInt](params Parameters[T], evk map[int]EvaluationK Encoder: tfhe.NewEncoder(params.Parameters), GLWETransformer: tfhe.NewGLWETransformer(params.Parameters), BaseSingleKeyEvaluator: tfhe.NewEvaluatorWithoutKey(params.Parameters), - SingleKeyEvaluators: evals, + SingleKeyEvaluators: singleEvals, Parameters: params, PartyBitMap: partyBitMap, - EvaluationKeys: evks, + EvaluationKeys: singleKeys, buffer: newEvaluationBuffer(params), } @@ -176,10 +176,10 @@ func (e *Evaluator[T]) AddEvaluationKey(idx int, evk EvaluationKey[T]) { // ShallowCopy returns a shallow copy of this Evaluator. // Returned Evaluator is safe for concurrent use. func (e *Evaluator[T]) ShallowCopy() *Evaluator[T] { - evals := make([]*tfhe.Evaluator[T], e.Parameters.partyCount) - for i, eval := range e.SingleKeyEvaluators { - if eval != nil { - evals[i] = eval.ShallowCopy() + singleEvals := make([]*tfhe.Evaluator[T], e.Parameters.partyCount) + for i := range e.SingleKeyEvaluators { + if e.SingleKeyEvaluators[i] != nil { + singleEvals[i] = e.SingleKeyEvaluators[i].ShallowCopy() } } @@ -187,7 +187,7 @@ func (e *Evaluator[T]) ShallowCopy() *Evaluator[T] { Encoder: e.Encoder, GLWETransformer: e.GLWETransformer.ShallowCopy(), BaseSingleKeyEvaluator: e.BaseSingleKeyEvaluator.ShallowCopy(), - SingleKeyEvaluators: evals, + SingleKeyEvaluators: singleEvals, Parameters: e.Parameters, PartyBitMap: vec.Copy(e.PartyBitMap), diff --git a/tfhe/bootstrap.go b/tfhe/bootstrap.go index 688bc6a..d32b853 100644 --- a/tfhe/bootstrap.go +++ b/tfhe/bootstrap.go @@ -82,26 +82,6 @@ func (e *Evaluator[T]) GenLookUpTableFullAssign(f func(int) T, lutOut LookUpTabl } } -// ClearPaddingBit clears the padding bit of the LWE ciphertext. -func (e *Evaluator[T]) ClearPaddingBit(ct LWECiphertext[T]) LWECiphertext[T] { - ctOut := NewLWECiphertext(e.Parameters) - e.ClearPaddingBitAssign(ct, ctOut) - return ctOut -} - -// ClearPaddingBitAssign clears the padding bit of the LWE ciphertext and writes it to ctOut. -func (e *Evaluator[T]) ClearPaddingBitAssign(ct LWECiphertext[T], ctOut LWECiphertext[T]) { - e.GenLookUpTableFullAssign(func(x int) T { return 1 << (e.Parameters.logQ - 2) }, e.buffer.lut) - e.BootstrapLUTAssign(ct, e.buffer.lut, e.buffer.ctPadding) - e.SubLWEAssign(ct, e.buffer.ctPadding, ctOut) - ctOut.Value[0] += 1 << (e.Parameters.logQ - 2) -} - -// ClearPaddingBitInPlace clears the padding bit of the LWE ciphertext in place. -func (e *Evaluator[T]) ClearPaddingBitInPlace(ct LWECiphertext[T]) { - e.ClearPaddingBitAssign(ct, ct) -} - // BootstrapFunc returns a bootstrapped LWE ciphertext with resepect to given function. func (e *Evaluator[T]) BootstrapFunc(ct LWECiphertext[T], f func(int) int) LWECiphertext[T] { e.GenLookUpTableAssign(f, e.buffer.lut) diff --git a/tfhe/evaluator.go b/tfhe/evaluator.go index d6c6191..f048729 100644 --- a/tfhe/evaluator.go +++ b/tfhe/evaluator.go @@ -76,9 +76,6 @@ type evaluationBuffer[T TorusInt] struct { // ctKeySwitch holds LWEDimension sized ciphertext from keyswitching. ctKeySwitch LWECiphertext[T] - // ctPadding holds the padding bit of a ciphertext. - ctPadding LWECiphertext[T] - // lut is an empty lut, used for BlindRotateFunc. lut LookUpTable[T] } @@ -104,7 +101,7 @@ func NewEvaluator[T TorusInt](params Parameters[T], evk EvaluationKey[T]) *Evalu // NewEvaluatorWithoutKey allocates an empty Evaluator based on parameters, but without evaluation keys. // This will panic if any operation that requires evaluation key is called. func NewEvaluatorWithoutKey[T TorusInt](params Parameters[T]) *Evaluator[T] { - return NewEvaluator[T](params, EvaluationKey[T]{}) + return NewEvaluator(params, EvaluationKey[T]{}) } // newEvaluationBuffer allocates an empty evaluationBuffer. @@ -155,8 +152,6 @@ func newEvaluationBuffer[T TorusInt](params Parameters[T]) evaluationBuffer[T] { ctExtract: NewLWECiphertextCustom[T](params.lweLargeDimension), ctKeySwitch: NewLWECiphertextCustom[T](params.lweDimension), - ctPadding: NewLWECiphertext(params), - lut: NewLookUpTable(params), } }