diff --git a/mktfhe/binary_evaluator.go b/mktfhe/binary_evaluator.go index 99050a7..dbe7c48 100644 --- a/mktfhe/binary_evaluator.go +++ b/mktfhe/binary_evaluator.go @@ -20,8 +20,8 @@ type BinaryEvaluator[T tfhe.TorusInt] struct { // NewBinaryEvaluator allocates an empty BinaryEvaluator based on parameters. // This does not copy evaluation keys, since they are large. func NewBinaryEvaluator[T tfhe.TorusInt](params Parameters[T], evk map[int]EvaluationKey[T]) *BinaryEvaluator[T] { - signLUT := tfhe.NewLookUpTableCustom[T](params.polyDegree) - vec.Fill(signLUT.Value, 1<<(params.logQ-3)) + signLUT := tfhe.NewLookUpTableCustom[T](params.PolyDegree()) + vec.Fill(signLUT.Value, 1<<(params.LogQ()-3)) return &BinaryEvaluator[T]{ BinaryEncoder: tfhe.NewBinaryEncoder(params.singleKeyParameters), @@ -73,7 +73,7 @@ func (e *BinaryEvaluator[T]) ANDAssign(ct0, ct1, ctOut LWECiphertext[T]) { for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = ct0.Value[i] + ct1.Value[i] } - ctOut.Value[0] -= 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] -= 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTAssign(ctOut, e.signLUT, ctOut) } @@ -92,7 +92,7 @@ func (e *BinaryEvaluator[T]) ANDParallelAssign(ct0, ct1, ctOut LWECiphertext[T]) for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = ct0.Value[i] + ct1.Value[i] } - ctOut.Value[0] -= 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] -= 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTParallelAssign(ctOut, e.signLUT, ctOut) } @@ -111,7 +111,7 @@ func (e *BinaryEvaluator[T]) NANDAssign(ct0, ct1, ctOut LWECiphertext[T]) { for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = -ct0.Value[i] - ct1.Value[i] } - ctOut.Value[0] += 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] += 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTAssign(ctOut, e.signLUT, ctOut) } @@ -130,7 +130,7 @@ func (e *BinaryEvaluator[T]) NANDParallelAssign(ct0, ct1, ctOut LWECiphertext[T] for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = -ct0.Value[i] - ct1.Value[i] } - ctOut.Value[0] += 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] += 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTParallelAssign(ctOut, e.signLUT, ctOut) } @@ -149,7 +149,7 @@ func (e *BinaryEvaluator[T]) ORAssign(ct0, ct1, ctOut LWECiphertext[T]) { for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = ct0.Value[i] + ct1.Value[i] } - ctOut.Value[0] += 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] += 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTAssign(ctOut, e.signLUT, ctOut) } @@ -168,7 +168,7 @@ func (e *BinaryEvaluator[T]) ORParallelAssign(ct0, ct1, ctOut LWECiphertext[T]) for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = ct0.Value[i] + ct1.Value[i] } - ctOut.Value[0] += 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] += 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTParallelAssign(ctOut, e.signLUT, ctOut) } @@ -187,7 +187,7 @@ func (e *BinaryEvaluator[T]) NORAssign(ct0, ct1, ctOut LWECiphertext[T]) { for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = -ct0.Value[i] - ct1.Value[i] } - ctOut.Value[0] -= 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] -= 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTAssign(ctOut, e.signLUT, ctOut) } @@ -206,7 +206,7 @@ func (e *BinaryEvaluator[T]) NORParallelAssign(ct0, ct1, ctOut LWECiphertext[T]) for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = -ct0.Value[i] - ct1.Value[i] } - ctOut.Value[0] -= 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] -= 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTParallelAssign(ctOut, e.signLUT, ctOut) } @@ -225,7 +225,7 @@ func (e *BinaryEvaluator[T]) XORAssign(ct0, ct1, ctOut LWECiphertext[T]) { for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = 2 * (ct0.Value[i] + ct1.Value[i]) } - ctOut.Value[0] += 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] += 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTAssign(ctOut, e.signLUT, ctOut) } @@ -244,7 +244,7 @@ func (e *BinaryEvaluator[T]) XORParallelAssign(ct0, ct1, ctOut LWECiphertext[T]) for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = 2 * (ct0.Value[i] + ct1.Value[i]) } - ctOut.Value[0] += 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] += 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTParallelAssign(ctOut, e.signLUT, ctOut) } @@ -263,7 +263,7 @@ func (e *BinaryEvaluator[T]) XNORAssign(ct0, ct1, ctOut LWECiphertext[T]) { for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = 2 * (-ct0.Value[i] - ct1.Value[i]) } - ctOut.Value[0] -= 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] -= 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTAssign(ctOut, e.signLUT, ctOut) } @@ -282,7 +282,7 @@ func (e *BinaryEvaluator[T]) XNORParallelAssign(ct0, ct1, ctOut LWECiphertext[T] for i := 0; i < e.Parameters.DefaultLWEDimension()+1; i++ { ctOut.Value[i] = 2 * (-ct0.Value[i] - ct1.Value[i]) } - ctOut.Value[0] -= 1 << (e.Parameters.logQ - 3) + ctOut.Value[0] -= 1 << (e.Parameters.LogQ() - 3) e.BaseEvaluator.BootstrapLUTParallelAssign(ctOut, e.signLUT, ctOut) } diff --git a/mktfhe/bootstrap.go b/mktfhe/bootstrap.go index 0219275..31d83d1 100644 --- a/mktfhe/bootstrap.go +++ b/mktfhe/bootstrap.go @@ -28,7 +28,7 @@ func (e *Evaluator[T]) BootstrapLUT(ct LWECiphertext[T], lut tfhe.LookUpTable[T] // BootstrapLUTAssign bootstraps LWE ciphertext with respect to given LUT and writes it to ctOut. func (e *Evaluator[T]) BootstrapLUTAssign(ct LWECiphertext[T], lut tfhe.LookUpTable[T], ctOut LWECiphertext[T]) { - switch e.Parameters.bootstrapOrder { + switch e.Parameters.BootstrapOrder() { case tfhe.OrderKeySwitchBlindRotate: e.KeySwitchForBootstrapAssign(ct, e.buffer.ctKeySwitchForBootstrap) e.BlindRotateAssign(e.buffer.ctKeySwitchForBootstrap, lut, e.buffer.ctRotate) @@ -61,7 +61,7 @@ func (e *Evaluator[T]) BootstrapLUTParallel(ct LWECiphertext[T], lut tfhe.LookUp // BootstrapLUTParallelAssign bootstraps LWE ciphertext with respect to given LUT and writes it to ctOut in parallel. func (e *Evaluator[T]) BootstrapLUTParallelAssign(ct LWECiphertext[T], lut tfhe.LookUpTable[T], ctOut LWECiphertext[T]) { - switch e.Parameters.bootstrapOrder { + switch e.Parameters.BootstrapOrder() { case tfhe.OrderKeySwitchBlindRotate: e.KeySwitchForBootstrapAssign(ct, e.buffer.ctKeySwitchForBootstrap) e.BlindRotateParallelAssign(e.buffer.ctKeySwitchForBootstrap, lut, e.buffer.ctRotate) @@ -89,7 +89,7 @@ func (e *Evaluator[T]) BlindRotateAssign(ct LWECiphertext[T], lut tfhe.LookUpTab for i, ok := range e.PartyBitMap { if ok { e.buffer.ctRotateInputs[i].Value[0] = 0 - vec.CopyAssign(ct.Value[1+i*e.Parameters.singleLWEDimension:1+(i+1)*e.Parameters.singleLWEDimension], e.buffer.ctRotateInputs[i].Value[1:]) + vec.CopyAssign(ct.Value[1+i*e.Parameters.singleKeyParameters.LWEDimension():1+(i+1)*e.Parameters.singleKeyParameters.LWEDimension()], e.buffer.ctRotateInputs[i].Value[1:]) for j := 0; j < e.Parameters.accumulatorParameters.Level(); j++ { e.SingleKeyEvaluators[i].BlindRotateAssign(e.buffer.ctRotateInputs[i], e.buffer.gadgetLUTs[j], e.buffer.ctAccs[i]) e.SingleKeyEvaluators[i].ToFourierGLWECiphertextAssign(e.buffer.ctAccs[i], e.buffer.ctFourierAccs[i].Value[j]) @@ -119,7 +119,7 @@ func (e *Evaluator[T]) BlindRotateParallelAssign(ct LWECiphertext[T], lut tfhe.L wg.Add(1) go func(i int) { e.buffer.ctRotateInputs[i].Value[0] = 0 - vec.CopyAssign(ct.Value[1+i*e.Parameters.singleLWEDimension:1+(i+1)*e.Parameters.singleLWEDimension], e.buffer.ctRotateInputs[i].Value[1:]) + vec.CopyAssign(ct.Value[1+i*e.Parameters.singleKeyParameters.LWEDimension():1+(i+1)*e.Parameters.singleKeyParameters.LWEDimension()], e.buffer.ctRotateInputs[i].Value[1:]) for j := 0; j < e.Parameters.accumulatorParameters.Level(); j++ { e.SingleKeyEvaluators[i].BlindRotateAssign(e.buffer.ctRotateInputs[i], e.buffer.gadgetLUTs[j], e.buffer.ctAccs[i]) e.SingleKeyEvaluators[i].ToFourierGLWECiphertextAssign(e.buffer.ctAccs[i], e.buffer.ctFourierAccs[i].Value[j]) @@ -141,7 +141,7 @@ func (e *Evaluator[T]) BlindRotateParallelAssign(ct LWECiphertext[T], lut tfhe.L // Input ciphertext should be of length GLWEDimension + 1. // Output ciphertext will be of length LWEDimension + 1. func (e *Evaluator[T]) KeySwitchForBootstrap(ct LWECiphertext[T]) LWECiphertext[T] { - ctOut := NewLWECiphertextCustom[T](e.Parameters.singleLWEDimension) + ctOut := NewLWECiphertextCustom[T](e.Parameters.singleKeyParameters.LWEDimension()) e.KeySwitchForBootstrapAssign(ct, ctOut) return ctOut } @@ -150,18 +150,18 @@ func (e *Evaluator[T]) KeySwitchForBootstrap(ct LWECiphertext[T]) LWECiphertext[ // Input ciphertext should be of length GLWEDimension + 1. // Output ciphertext should be of length LWEDimension + 1. func (e *Evaluator[T]) KeySwitchForBootstrapAssign(ct, ctOut LWECiphertext[T]) { - scalarDecomposed := e.Decomposer.ScalarDecomposedBuffer(e.Parameters.keySwitchParameters) + scalarDecomposed := e.Decomposer.ScalarDecomposedBuffer(e.Parameters.KeySwitchParameters()) ctOut.Value[0] = ct.Value[0] for i, ok := range e.PartyBitMap { - ctMask := ct.Value[1+i*e.Parameters.singleGLWEDimension : 1+(i+1)*e.Parameters.singleGLWEDimension] - ctOutMask := ctOut.Value[1+i*e.Parameters.singleLWEDimension : 1+(i+1)*e.Parameters.singleLWEDimension] + ctMask := ct.Value[1+i*e.Parameters.singleKeyParameters.GLWEDimension() : 1+(i+1)*e.Parameters.singleKeyParameters.GLWEDimension()] + ctOutMask := ctOut.Value[1+i*e.Parameters.singleKeyParameters.LWEDimension() : 1+(i+1)*e.Parameters.singleKeyParameters.LWEDimension()] if ok { vec.CopyAssign(ctMask, ctOutMask) - for j, jj := e.Parameters.singleLWEDimension, 0; j < e.Parameters.singleGLWEDimension; j, jj = j+1, jj+1 { - e.SingleKeyEvaluators[i].Decomposer.DecomposeScalarAssign(ctMask[j], e.Parameters.keySwitchParameters, scalarDecomposed) - for k := 0; k < e.Parameters.keySwitchParameters.Level(); k++ { + for j, jj := e.Parameters.singleKeyParameters.LWEDimension(), 0; j < e.Parameters.singleKeyParameters.GLWEDimension(); j, jj = j+1, jj+1 { + e.SingleKeyEvaluators[i].Decomposer.DecomposeScalarAssign(ctMask[j], e.Parameters.KeySwitchParameters(), scalarDecomposed) + for k := 0; k < e.Parameters.KeySwitchParameters().Level(); k++ { vec.ScalarMulAddAssign(e.EvaluationKeys[i].KeySwitchKey.Value[jj].Value[k].Value[1:], scalarDecomposed[k], ctOutMask) ctOut.Value[0] += scalarDecomposed[k] * e.EvaluationKeys[i].KeySwitchKey.Value[jj].Value[k].Value[0] } diff --git a/mktfhe/decryptor.go b/mktfhe/decryptor.go index 861fd57..2965c90 100644 --- a/mktfhe/decryptor.go +++ b/mktfhe/decryptor.go @@ -130,7 +130,7 @@ func (d *Decryptor[T]) DecryptLWEPlaintext(ct LWECiphertext[T]) tfhe.LWEPlaintex pt := ct.Value[0] for i, ok := range d.PartyBitMap { if ok { - ctMask := ct.Value[1+i*d.Parameters.SingleDefaultLWEDimension() : 1+(i+1)*d.Parameters.SingleDefaultLWEDimension()] + ctMask := ct.Value[1+i*d.Parameters.singleKeyParameters.DefaultLWEDimension() : 1+(i+1)*d.Parameters.singleKeyParameters.DefaultLWEDimension()] pt += vec.Dot(ctMask, d.SingleKeyDecryptors[i].DefaultLWESecretKey().Value) } } diff --git a/mktfhe/evaluator.go b/mktfhe/evaluator.go index 9c1dda9..1d37e93 100644 --- a/mktfhe/evaluator.go +++ b/mktfhe/evaluator.go @@ -90,8 +90,8 @@ func NewEvaluator[T tfhe.TorusInt](params Parameters[T], evk map[int]EvaluationK partyBitMap[i] = true } - decomposer := tfhe.NewDecomposer[T](params.polyDegree) - decomposer.ScalarDecomposedBuffer(params.keySwitchParameters) + decomposer := tfhe.NewDecomposer[T](params.PolyDegree()) + decomposer.ScalarDecomposedBuffer(params.KeySwitchParameters()) decomposer.PolyDecomposedBuffer(params.relinKeyParameters) decomposer.PolyFourierDecomposedBuffer(params.relinKeyParameters) @@ -103,7 +103,7 @@ func NewEvaluator[T tfhe.TorusInt](params Parameters[T], evk map[int]EvaluationK SingleKeyEvaluators: singleEvals, Decomposer: decomposer, - PolyEvaluator: poly.NewEvaluator[T](params.polyDegree), + PolyEvaluator: poly.NewEvaluator[T](params.PolyDegree()), Parameters: params, @@ -125,7 +125,7 @@ func newEvaluationBuffer[T tfhe.TorusInt](params Parameters[T]) evaluationBuffer ctRotateInputs := make([]tfhe.LWECiphertext[T], params.partyCount) for i := 0; i < params.partyCount; i++ { - ctRotateInputs[i] = tfhe.NewLWECiphertextCustom[T](params.singleLWEDimension) + ctRotateInputs[i] = tfhe.NewLWECiphertextCustom[T](params.singleKeyParameters.LWEDimension()) } gadgetLUTs := make([]tfhe.LookUpTable[T], params.accumulatorParameters.Level()) @@ -145,8 +145,8 @@ func newEvaluationBuffer[T tfhe.TorusInt](params Parameters[T]) evaluationBuffer ctProd: NewGLWECiphertext(params), ctFourierProd: NewFourierGLWECiphertext(params), - ctProdSingle: poly.NewPoly[T](params.polyDegree), - ctFourierProdSingle: poly.NewFourierPoly(params.polyDegree), + ctProdSingle: poly.NewPoly[T](params.PolyDegree()), + ctFourierProdSingle: poly.NewFourierPoly(params.PolyDegree()), ctRelin: ctRelin, ctRelinTransposed: ctRelinTransposed, diff --git a/mktfhe/fourier_glwe_conv.go b/mktfhe/fourier_glwe_conv.go index 4927f18..158ed3c 100644 --- a/mktfhe/fourier_glwe_conv.go +++ b/mktfhe/fourier_glwe_conv.go @@ -18,7 +18,7 @@ type GLWETransformer[T tfhe.TorusInt] struct { func NewGLWETransformer[T tfhe.TorusInt](params Parameters[T]) *GLWETransformer[T] { return &GLWETransformer[T]{ Parameters: params, - PolyEvaluator: poly.NewEvaluator[T](params.polyDegree), + PolyEvaluator: poly.NewEvaluator[T](params.PolyDegree()), } } @@ -33,35 +33,35 @@ func (e *GLWETransformer[T]) ShallowCopy() *GLWETransformer[T] { // ToFourierGLWESecretKey transforms GLWE secret key to Fourier GLWE secret key. func (e *GLWETransformer[T]) ToFourierGLWESecretKey(sk tfhe.GLWESecretKey[T]) tfhe.FourierGLWESecretKey[T] { - skOut := tfhe.NewFourierGLWESecretKeyCustom[T](e.Parameters.GLWERank(), e.Parameters.polyDegree) + skOut := tfhe.NewFourierGLWESecretKeyCustom[T](e.Parameters.GLWERank(), e.Parameters.PolyDegree()) e.ToFourierGLWESecretKeyAssign(sk, skOut) return skOut } // ToFourierGLWESecretKeyAssign transforms GLWE secret key to Fourier GLWE secret key and writes it to skOut. func (e *GLWETransformer[T]) ToFourierGLWESecretKeyAssign(skIn tfhe.GLWESecretKey[T], skOut tfhe.FourierGLWESecretKey[T]) { - for i := 0; i < e.Parameters.SingleGLWERank(); i++ { + for i := 0; i < e.Parameters.singleKeyParameters.GLWERank(); i++ { e.PolyEvaluator.ToFourierPolyAssign(skIn.Value[i], skOut.Value[i]) } } // ToGLWESecretKey transforms Fourier GLWE secret key to GLWE secret key. func (e *GLWETransformer[T]) ToGLWESecretKey(sk tfhe.FourierGLWESecretKey[T]) tfhe.GLWESecretKey[T] { - skOut := tfhe.NewGLWESecretKeyCustom[T](e.Parameters.SingleGLWERank(), e.Parameters.polyDegree) + skOut := tfhe.NewGLWESecretKeyCustom[T](e.Parameters.singleKeyParameters.GLWERank(), e.Parameters.PolyDegree()) e.ToGLWESecretKeyAssign(sk, skOut) return skOut } // ToGLWESecretKeyAssign transforms Fourier GLWE secret key to GLWE secret key and writes it to skOut. func (e *GLWETransformer[T]) ToGLWESecretKeyAssign(skIn tfhe.FourierGLWESecretKey[T], skOut tfhe.GLWESecretKey[T]) { - for i := 0; i < e.Parameters.SingleGLWERank(); i++ { + for i := 0; i < e.Parameters.singleKeyParameters.GLWERank(); i++ { e.PolyEvaluator.ToPolyAssign(skIn.Value[i], skOut.Value[i]) } } // ToFourierGLWECiphertext transforms GLWE ciphertext to Fourier GLWE ciphertext. func (e *GLWETransformer[T]) ToFourierGLWECiphertext(ct GLWECiphertext[T]) FourierGLWECiphertext[T] { - ctOut := NewFourierGLWECiphertextCustom[T](e.Parameters.GLWERank(), e.Parameters.polyDegree) + ctOut := NewFourierGLWECiphertextCustom[T](e.Parameters.GLWERank(), e.Parameters.PolyDegree()) e.ToFourierGLWECiphertextAssign(ct, ctOut) return ctOut } @@ -75,7 +75,7 @@ func (e *GLWETransformer[T]) ToFourierGLWECiphertextAssign(ctIn GLWECiphertext[T // ToGLWECiphertext transforms Fourier GLWE ciphertext to GLWE ciphertext. func (e *GLWETransformer[T]) ToGLWECiphertext(ct FourierGLWECiphertext[T]) GLWECiphertext[T] { - ctOut := NewGLWECiphertextCustom[T](e.Parameters.GLWERank(), e.Parameters.polyDegree) + ctOut := NewGLWECiphertextCustom[T](e.Parameters.GLWERank(), e.Parameters.PolyDegree()) e.ToGLWECiphertextAssign(ct, ctOut) return ctOut } @@ -89,7 +89,7 @@ func (e *GLWETransformer[T]) ToGLWECiphertextAssign(ctIn FourierGLWECiphertext[T // ToFourierUniEncryption transforms UniEncryption to Fourier UniEncryption. func (e *GLWETransformer[T]) ToFourierUniEncryption(ct UniEncryption[T]) FourierUniEncryption[T] { - ctOut := NewFourierUniEncryptionCustom(e.Parameters.polyDegree, ct.GadgetParameters) + ctOut := NewFourierUniEncryptionCustom(e.Parameters.PolyDegree(), ct.GadgetParameters) e.ToFourierUniEncryptionAssign(ct, ctOut) return ctOut } @@ -107,7 +107,7 @@ func (e *GLWETransformer[T]) ToFourierUniEncryptionAssign(ctIn UniEncryption[T], // ToUniEncryption transforms Fourier UniEncryption to UniEncryption. func (e *GLWETransformer[T]) ToUniEncryption(ct FourierUniEncryption[T]) UniEncryption[T] { - ctOut := NewUniEncryptionCustom(e.Parameters.polyDegree, ct.GadgetParameters) + ctOut := NewUniEncryptionCustom(e.Parameters.PolyDegree(), ct.GadgetParameters) e.ToUniEncryptionAssign(ct, ctOut) return ctOut } diff --git a/mktfhe/fourier_glwe_enc.go b/mktfhe/fourier_glwe_enc.go index 746b719..23992d3 100644 --- a/mktfhe/fourier_glwe_enc.go +++ b/mktfhe/fourier_glwe_enc.go @@ -15,9 +15,9 @@ func (e *Encryptor[T]) FourierUniEncrypt(messages []int, gadgetParams tfhe.Gadge // FourierUniEncryptAssign encrypts integer messages to FourierUniEncryption and writes it to ctOut. func (e *Encryptor[T]) FourierUniEncryptAssign(messages []int, gadgetParams tfhe.GadgetParameters[T], ctOut FourierUniEncryption[T]) { - length := num.Min(e.Parameters.polyDegree, len(messages)) + length := num.Min(e.Parameters.PolyDegree(), len(messages)) for i := 0; i < length; i++ { - e.buffer.ptGLWE.Value.Coeffs[i] = T(messages[i]) % e.Parameters.messageModulus + e.buffer.ptGLWE.Value.Coeffs[i] = T(messages[i]) % e.Parameters.MessageModulus() } vec.Fill(e.buffer.ptGLWE.Value.Coeffs[length:], 0) @@ -55,7 +55,7 @@ func (e *Encryptor[T]) FourierUniEncryptPlaintextAssign(pt tfhe.GLWEPlaintext[T] // FourierUniDecrypt decrypts FourierUniEncryption to integer messages. func (e *Encryptor[T]) FourierUniDecrypt(ct FourierUniEncryption[T]) []int { - messages := make([]int, e.Parameters.polyDegree) + messages := make([]int, e.Parameters.PolyDegree()) e.FourierUniDecryptAssign(ct, messages) return messages } @@ -64,9 +64,9 @@ func (e *Encryptor[T]) FourierUniDecrypt(ct FourierUniEncryption[T]) []int { func (e *Encryptor[T]) FourierUniDecryptAssign(ct FourierUniEncryption[T], messagesOut []int) { e.FourierUniDecryptPlaintextAssign(ct, e.buffer.ptGLWE) - length := num.Min(e.Parameters.polyDegree, len(messagesOut)) + length := num.Min(e.Parameters.PolyDegree(), len(messagesOut)) for i := 0; i < length; i++ { - messagesOut[i] = int(num.DivRoundBits(e.buffer.ptGLWE.Value.Coeffs[i], ct.GadgetParameters.LogLastBaseQ()) % e.Parameters.messageModulus) + messagesOut[i] = int(num.DivRoundBits(e.buffer.ptGLWE.Value.Coeffs[i], ct.GadgetParameters.LogLastBaseQ()) % e.Parameters.MessageModulus()) } } @@ -80,7 +80,7 @@ func (e *Encryptor[T]) FourierUniDecryptPlaintext(ct FourierUniEncryption[T]) tf // FourierUniDecryptPlaintextAssign decrypts FourierUniEncryption to GLWE plaintext and writes it to ptOut. func (e *Encryptor[T]) FourierUniDecryptPlaintextAssign(ct FourierUniEncryption[T], ptOut tfhe.GLWEPlaintext[T]) { e.SingleKeyEncryptor.DecryptFourierGLevPlaintextAssign(ct.Value[1], tfhe.GLWEPlaintext[T]{Value: e.buffer.auxKey.Value[0]}) - for i := 0; i < e.Parameters.polyDegree; i++ { + for i := 0; i < e.Parameters.PolyDegree(); i++ { e.buffer.auxKey.Value[0].Coeffs[i] = num.DivRoundBits(e.buffer.auxKey.Value[0].Coeffs[i], ct.GadgetParameters.LogLastBaseQ()) & (1< BlindRotate -> SampleExtract - // - // and LWE keys and ciphertexts will have size according to GLWEDimension. - // - // Otherwise, if this is set to OrderBlindRotateKeySwitch, the order is: - // - // BlindRotate -> SampleExtract -> KeySwitch - // - // and LWE keys and ciphertexts will have size according to LWEDimension. - // - // Essentially, there is a time-memory tradeoff: - // performing keyswitching first means that it will consume more memory, - // but it allows to use smaller parameters which will result in faster computation. - // - // Moreover, public key encryption is supported only with OrderKeySwitchBlindRotate. - // - // If zero, then it is set to OrderKeySwitchBlindRotate. - BootstrapOrder tfhe.BootstrapOrder } // WithPartyCount sets the PartyCount and returns the new ParametersLiteral. @@ -79,51 +38,51 @@ func (p ParametersLiteral[T]) WithPartyCount(partyCount int) ParametersLiteral[T return p } -// WithSingleLWEDimension sets the LWEDimension and returns the new ParametersLiteral. -func (p ParametersLiteral[T]) WithSingleLWEDimension(singleLWEDimension int) ParametersLiteral[T] { - p.SingleLWEDimension = singleLWEDimension +// WithSingleKeyLWEDimension sets the single-key LWEDimension and returns the new ParametersLiteral. +func (p ParametersLiteral[T]) WithSingleKeyLWEDimension(singleKeyLWEDimension int) ParametersLiteral[T] { + p.SingleKeyParametersLiteral.LWEDimension = singleKeyLWEDimension return p } // WithPolyDegree sets the PolyDegree and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithPolyDegree(polyDegree int) ParametersLiteral[T] { - p.PolyDegree = polyDegree + p.SingleKeyParametersLiteral.PolyDegree = polyDegree return p } // WithLWEStdDev sets the LWEStdDev and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithLWEStdDev(lweStdDev float64) ParametersLiteral[T] { - p.LWEStdDev = lweStdDev + p.SingleKeyParametersLiteral.LWEStdDev = lweStdDev return p } // WithGLWEStdDev sets the GLWEStdDev and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithGLWEStdDev(glweStdDev float64) ParametersLiteral[T] { - p.GLWEStdDev = glweStdDev + p.SingleKeyParametersLiteral.GLWEStdDev = glweStdDev return p } // WithBlockSize sets the BlockSize and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithBlockSize(blockSize int) ParametersLiteral[T] { - p.BlockSize = blockSize + p.SingleKeyParametersLiteral.BlockSize = blockSize return p } // WithMessageModulus sets the MessageModulus and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithMessageModulus(messageModulus T) ParametersLiteral[T] { - p.MessageModulus = messageModulus + p.SingleKeyParametersLiteral.MessageModulus = messageModulus return p } // WithBlindRotateParameters sets the BlindRotateParameters and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithBlindRotateParameters(blindRotateParameters tfhe.GadgetParametersLiteral[T]) ParametersLiteral[T] { - p.BlindRotateParameters = blindRotateParameters + p.SingleKeyParametersLiteral.BlindRotateParameters = blindRotateParameters return p } // WithKeySwitchParameters sets the KeySwitchParameters and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithKeySwitchParameters(keySwitchParameters tfhe.GadgetParametersLiteral[T]) ParametersLiteral[T] { - p.KeySwitchParameters = keySwitchParameters + p.SingleKeyParametersLiteral.KeySwitchParameters = keySwitchParameters return p } @@ -141,32 +100,10 @@ func (p ParametersLiteral[T]) WithRelinKeyParameters(relinKeyParameters tfhe.Gad // WithBootstrapOrder sets the BootstrapOrder and returns the new ParametersLiteral. func (p ParametersLiteral[T]) WithBootstrapOrder(bootstrapOrder tfhe.BootstrapOrder) ParametersLiteral[T] { - p.BootstrapOrder = bootstrapOrder + p.SingleKeyParametersLiteral.BootstrapOrder = bootstrapOrder return p } -// SingleKeyParametersLiteral extracts the single-key parameters from the multi-key parameters. -func (p ParametersLiteral[T]) SingleKeyParametersLiteral() tfhe.ParametersLiteral[T] { - return tfhe.ParametersLiteral[T]{ - LWEDimension: p.SingleLWEDimension, - GLWERank: 1, - PolyDegree: p.PolyDegree, - LookUpTableSize: p.PolyDegree, - - LWEStdDev: p.LWEStdDev, - GLWEStdDev: p.GLWEStdDev, - - BlockSize: p.BlockSize, - - MessageModulus: p.MessageModulus, - - BlindRotateParameters: p.BlindRotateParameters, - KeySwitchParameters: p.KeySwitchParameters, - - BootstrapOrder: p.BootstrapOrder, - } -} - // Compile transforms ParametersLiteral to read-only Parameters. // If there is any invalid parameter in the literal, it panics. // Default parameters are guaranteed to be compiled without panics. @@ -178,56 +115,22 @@ func (p ParametersLiteral[T]) SingleKeyParametersLiteral() tfhe.ParametersLitera // Unless you are a cryptographic expert, DO NOT set parameters by yourself; // always use the default parameters provided. func (p ParametersLiteral[T]) Compile() Parameters[T] { - if p.BlockSize == 0 { - p.BlockSize = 1 - } - switch { case p.PartyCount <= 0: panic("PartyCount smaller than zero") - case p.SingleLWEDimension > p.PolyDegree: - panic("LWEDimension larger than GLWEDimension") - case p.LWEStdDev <= 0: - panic("LWEStdDev smaller than zero") - case p.GLWEStdDev <= 0: - panic("GLWEStdDev smaller than zero") - case p.BlockSize <= 0: - panic("BlockSize smaller than zero") - case p.SingleLWEDimension%p.BlockSize != 0: - panic("LWEDimension not multiple of BlockSize") - case !num.IsPowerOfTwo(p.PolyDegree): - panic("PolyDegree not power of two") - case !(p.BootstrapOrder == tfhe.OrderKeySwitchBlindRotate || p.BootstrapOrder == tfhe.OrderBlindRotateKeySwitch): - panic("BootstrapOrder not valid") + case p.SingleKeyParametersLiteral.GLWERank != 1: + panic("Multi-Key TFHE only supports GLWE dimension 1") + case p.SingleKeyParametersLiteral.LookUpTableSize != 0 && p.SingleKeyParametersLiteral.LookUpTableSize != p.SingleKeyParametersLiteral.PolyDegree: + panic("Multi-Key TFHE only supports LookUpTableSize equal to PolyDegree") } return Parameters[T]{ - singleKeyParameters: p.SingleKeyParametersLiteral().Compile(), + singleKeyParameters: p.SingleKeyParametersLiteral.Compile(), partyCount: p.PartyCount, - singleLWEDimension: p.SingleLWEDimension, - singleGLWEDimension: p.PolyDegree, - polyDegree: p.PolyDegree, - - lweStdDev: p.LWEStdDev, - glweStdDev: p.GLWEStdDev, - - blockSize: p.BlockSize, - blockCount: p.SingleLWEDimension / p.BlockSize, - - messageModulus: p.MessageModulus, - scale: num.DivRound(1<<(num.SizeT[T]()-1), p.MessageModulus), - - logQ: num.SizeT[T](), - floatQ: math.Exp2(float64(num.SizeT[T]())), - - blindRotateParameters: p.BlindRotateParameters.Compile(), - keySwitchParameters: p.KeySwitchParameters.Compile(), accumulatorParameters: p.AccumulatorParameters.Compile(), relinKeyParameters: p.RelinKeyParameters.Compile(), - - bootstrapOrder: p.BootstrapOrder, } } @@ -240,47 +143,10 @@ type Parameters[T tfhe.TorusInt] struct { // that this parameter supports. partyCount int - // SingleLWEDimension is the dimension of LWE lattice used. Usually this is denoted by n. - singleLWEDimension int - // SingleGLWEDimension is the dimension of GLWE lattice used, which is GLWERank * PolyDegree. - singleGLWEDimension int - // PolyDegree is the degree of polynomials in GLWE entities. Usually this is denoted by N. - polyDegree int - // LogPolyDegree equals log(PolyDegree). - logPolyDegree int - - // LWEStdDev is the normalized standard deviation used for gaussian error sampling in LWE encryption. - lweStdDev float64 - // GLWEStdDev is the normalized standard deviation used for gaussian error sampling in GLWE encryption. - glweStdDev float64 - - // BlockSize is the size of block to be used for LWE key sampling. - blockSize int - // BlockCount is a number of blocks in LWESecretkey. Equal to LWEDimension / BlockSize. - blockCount int - - // MessageModulus is the modulus of the encoded message. - messageModulus T - // Scale is the scaling factor used for message encoding. - // The lower log(Scale) bits are reserved for errors. - scale T - - // logQ is the value of log(Q), where Q is the modulus of the ciphertext. - logQ int - // floatQ is the value of Q as float64. - floatQ float64 - - // blindRotateParameters is the gadget parameters for Blind Rotation. - blindRotateParameters tfhe.GadgetParameters[T] - // KeySwitchParameters is the gadget parameters for KeySwitching. - keySwitchParameters tfhe.GadgetParameters[T] // AccumulatorParameters is the gadget parameters for the accumulator. accumulatorParameters tfhe.GadgetParameters[T] // RelinKeyParameters is the gadget parameters for the relinearization key. relinKeyParameters tfhe.GadgetParameters[T] - - // bootstrapOrder is the order of Programmable Bootstrapping. - bootstrapOrder tfhe.BootstrapOrder } // PartyCount returns the number of maximum parties @@ -291,12 +157,12 @@ func (p Parameters[T]) PartyCount() int { // DefaultLWEDimension returns the default dimension of multi-key LWE entities. func (p Parameters[T]) DefaultLWEDimension() int { - return p.partyCount * p.SingleDefaultLWEDimension() + return p.partyCount * p.singleKeyParameters.DefaultLWEDimension() } // LWEDimension returns the dimension of multi-key LWE entities. func (p Parameters[T]) LWEDimension() int { - return p.partyCount * p.singleLWEDimension + return p.partyCount * p.singleKeyParameters.LWEDimension() } // GLWERank returns the rank of GLWE entities. @@ -307,41 +173,17 @@ func (p Parameters[T]) GLWERank() int { // GLWEDimension returns the glwe dimension of multi-key LWE entities. func (p Parameters[T]) GLWEDimension() int { - return p.partyCount * p.singleGLWEDimension -} - -// SingleKeyDefaultLWEDimension returns the default dimension of single-key LWE entities. -func (p Parameters[T]) SingleDefaultLWEDimension() int { - if p.bootstrapOrder == tfhe.OrderBlindRotateKeySwitch { - return p.singleLWEDimension - } - return p.singleGLWEDimension -} - -// SingleKeyLWEDimension returns the dimension of single-key LWE entities. -func (p Parameters[T]) SingleLWEDimension() int { - return p.singleLWEDimension -} - -// SingleKeyGLWEDimension returns the glwe dimension of single-key LWE entities. -func (p Parameters[T]) SingleGLWEDimension() int { - return p.singleGLWEDimension -} - -// SingleGLWERank returns the rank of single-key GLWE entities. -// In multi-key TFHE, this is always 1. -func (p Parameters[T]) SingleGLWERank() int { - return 1 + return p.partyCount * p.singleKeyParameters.GLWEDimension() } // PolyDegree returns the degree of polynomials in GLWE entities. func (p Parameters[T]) PolyDegree() int { - return p.polyDegree + return p.singleKeyParameters.PolyDegree() } // LogPolyDegree equals log(PolyDegree). func (p Parameters[T]) LogPolyDegree() int { - return p.logPolyDegree + return p.singleKeyParameters.LogPolyDegree() } // DefaultLWEStdDev returns the default standard deviation for LWE entities. @@ -351,18 +193,12 @@ func (p Parameters[T]) LogPolyDegree() int { // This is a normlized standard deviation. // For actual sampling, use [Parameters.DefaultLWEStdDevQ]. func (p Parameters[T]) DefaultLWEStdDev() float64 { - if p.bootstrapOrder == tfhe.OrderBlindRotateKeySwitch { - return p.lweStdDev - } - return p.glweStdDev + return p.singleKeyParameters.DefaultLWEStdDev() } // DefaultLWEStdDevQ returns DefaultLWEStdDev * Q. func (p Parameters[T]) DefaultLWEStdDevQ() float64 { - if p.bootstrapOrder == tfhe.OrderBlindRotateKeySwitch { - return p.lweStdDev * p.floatQ - } - return p.glweStdDev * p.floatQ + return p.singleKeyParameters.DefaultLWEStdDevQ() } // LWEStdDev is the standard deviation used for gaussian error sampling in LWE encryption. @@ -370,12 +206,12 @@ func (p Parameters[T]) DefaultLWEStdDevQ() float64 { // This is a normlized standard deviation. // For actual sampling, use [Parameters.LWEStdDevQ]. func (p Parameters[T]) LWEStdDev() float64 { - return p.lweStdDev + return p.singleKeyParameters.LWEStdDev() } // LWEStdDevQ returns LWEStdDev * Q. func (p Parameters[T]) LWEStdDevQ() float64 { - return p.lweStdDev * p.floatQ + return p.singleKeyParameters.LWEStdDevQ() } // GLWEStdDev is the standard deviation used for gaussian error sampling in GLWE encryption. @@ -383,48 +219,48 @@ func (p Parameters[T]) LWEStdDevQ() float64 { // This is a normlized standard deviation. // For actual sampling, use [Parameters.GLWEStdDevQ]. func (p Parameters[T]) GLWEStdDev() float64 { - return p.glweStdDev + return p.singleKeyParameters.GLWEStdDev() } // GLWEStdDevQ returns GLWEStdDev * Q. func (p Parameters[T]) GLWEStdDevQ() float64 { - return p.glweStdDev * p.floatQ + return p.singleKeyParameters.GLWEStdDevQ() } // BlockSize is the size of block to be used for LWE key sampling. func (p Parameters[T]) BlockSize() int { - return p.blockSize + return p.singleKeyParameters.BlockSize() } // BlockCount is a number of blocks in LWESecretkey. Equal to LWEDimension / BlockSize. func (p Parameters[T]) BlockCount() int { - return p.blockCount + return p.singleKeyParameters.BlockCount() } // Scale is the scaling factor used for message encoding. // The lower log(Scale) bits are reserved for errors. func (p Parameters[T]) Scale() T { - return p.scale + return p.singleKeyParameters.Scale() } // MessageModulus is the modulus of the encoded message. func (p Parameters[T]) MessageModulus() T { - return p.messageModulus + return p.singleKeyParameters.MessageModulus() } // LogQ is the value of log(Q), where Q is the modulus of the ciphertext. func (p Parameters[T]) LogQ() int { - return p.logQ + return p.singleKeyParameters.LogQ() } // BlindRotateParameters is the gadget parameters for Blind Rotation. func (p Parameters[T]) BlindRotateParameters() tfhe.GadgetParameters[T] { - return p.blindRotateParameters + return p.singleKeyParameters.BlindRotateParameters() } // KeySwitchParameters is the gadget parameters for KeySwitching. func (p Parameters[T]) KeySwitchParameters() tfhe.GadgetParameters[T] { - return p.keySwitchParameters + return p.singleKeyParameters.KeySwitchParameters() } // AccumulatorParameters returns the gadget parameters for the accumulator. @@ -439,30 +275,18 @@ func (p Parameters[T]) RelinKeyParameters() tfhe.GadgetParameters[T] { // BootstrapOrder is the order of Programmable Bootstrapping. func (p Parameters[T]) BootstrapOrder() tfhe.BootstrapOrder { - return p.bootstrapOrder + return p.singleKeyParameters.BootstrapOrder() } // Literal returns a literal representation of the parameters. func (p Parameters[T]) Literal() ParametersLiteral[T] { return ParametersLiteral[T]{ - PartyCount: p.partyCount, - - SingleLWEDimension: p.singleLWEDimension, - PolyDegree: p.polyDegree, + SingleKeyParametersLiteral: p.singleKeyParameters.Literal(), - LWEStdDev: p.lweStdDev, - GLWEStdDev: p.glweStdDev, - - BlockSize: p.blockSize, - - MessageModulus: p.messageModulus, + PartyCount: p.partyCount, - BlindRotateParameters: p.blindRotateParameters.Literal(), - KeySwitchParameters: p.keySwitchParameters.Literal(), AccumulatorParameters: p.accumulatorParameters.Literal(), RelinKeyParameters: p.relinKeyParameters.Literal(), - - BootstrapOrder: p.bootstrapOrder, } } @@ -473,89 +297,34 @@ func (p Parameters[T]) SingleKeyParameters() tfhe.Parameters[T] { // ByteSize returns the size of the parameters in bytes. func (p Parameters[T]) ByteSize() int { - return 8*7 + p.blindRotateParameters.ByteSize() + p.keySwitchParameters.ByteSize() + p.accumulatorParameters.ByteSize() + p.relinKeyParameters.ByteSize() + 1 + return p.singleKeyParameters.ByteSize() + 8 + p.accumulatorParameters.ByteSize() + p.relinKeyParameters.ByteSize() } // WriteTo implements the [io.WriterTo] interface. // // The encoded form is as follows: // +// SingleKeyParameters // [ 8] PartyCount -// [ 8] SingleLWEDimension -// [ 8] PolyDegree -// [ 8] LWEStdDev -// [ 8] GLWEStdDev -// [ 8] BlockSize -// [ 8] MessageModulus -// BlindRotateParameters -// KeySwitchParameters // AccumulatorParameters // RelinKeyParameters -// [ 1] BootstrapOrder func (p Parameters[T]) WriteTo(w io.Writer) (n int64, err error) { var nWrite int var nWrite64 int64 var buf [8]byte - partyCount := p.partyCount - binary.BigEndian.PutUint64(buf[:], uint64(partyCount)) - if nWrite, err = w.Write(buf[:]); err != nil { - return n + int64(nWrite), err - } - n += int64(nWrite) - - singleLWEDimension := p.singleLWEDimension - binary.BigEndian.PutUint64(buf[:], uint64(singleLWEDimension)) - if nWrite, err = w.Write(buf[:]); err != nil { - return n + int64(nWrite), err - } - n += int64(nWrite) - - polyDegree := p.polyDegree - binary.BigEndian.PutUint64(buf[:], uint64(polyDegree)) - if nWrite, err = w.Write(buf[:]); err != nil { - return n + int64(nWrite), err - } - n += int64(nWrite) - - lweStdDev := math.Float64bits(p.lweStdDev) - binary.BigEndian.PutUint64(buf[:], lweStdDev) - if nWrite, err = w.Write(buf[:]); err != nil { - return n + int64(nWrite), err - } - n += int64(nWrite) - - glweStdDev := math.Float64bits(p.glweStdDev) - binary.BigEndian.PutUint64(buf[:], glweStdDev) - if nWrite, err = w.Write(buf[:]); err != nil { - return n + int64(nWrite), err - } - n += int64(nWrite) - - blockSize := p.blockSize - binary.BigEndian.PutUint64(buf[:], uint64(blockSize)) - if nWrite, err = w.Write(buf[:]); err != nil { - return n + int64(nWrite), err + if nWrite64, err = p.singleKeyParameters.WriteTo(w); err != nil { + return n + nWrite64, err } - n += int64(nWrite) + n += nWrite64 - messageModulus := p.messageModulus - binary.BigEndian.PutUint64(buf[:], uint64(messageModulus)) + partyCount := p.partyCount + binary.BigEndian.PutUint64(buf[:], uint64(partyCount)) if nWrite, err = w.Write(buf[:]); err != nil { return n + int64(nWrite), err } n += int64(nWrite) - if nWrite64, err = p.blindRotateParameters.WriteTo(w); err != nil { - return n + nWrite64, err - } - n += nWrite64 - - if nWrite64, err = p.keySwitchParameters.WriteTo(w); err != nil { - return n + nWrite64, err - } - n += nWrite64 - if nWrite64, err = p.accumulatorParameters.WriteTo(w); err != nil { return n + nWrite64, err } @@ -566,12 +335,6 @@ func (p Parameters[T]) WriteTo(w io.Writer) (n int64, err error) { } n += nWrite64 - bootstrapOrder := p.bootstrapOrder - if nWrite, err = w.Write([]byte{byte(bootstrapOrder)}); err != nil { - return n + int64(nWrite), err - } - n += int64(nWrite) - if n < int64(p.ByteSize()) { return n, io.ErrShortWrite } @@ -585,59 +348,16 @@ func (p *Parameters[T]) ReadFrom(r io.Reader) (n int64, err error) { var nRead64 int64 var buf [8]byte - if nRead, err = io.ReadFull(r, buf[:]); err != nil { - return n + int64(nRead), err - } - n += int64(nRead) - partyCount := int(binary.BigEndian.Uint64(buf[:])) - - if nRead, err = io.ReadFull(r, buf[:]); err != nil { - return n + int64(nRead), err - } - n += int64(nRead) - singleLWEDimension := int(binary.BigEndian.Uint64(buf[:])) - - if nRead, err = io.ReadFull(r, buf[:]); err != nil { - return n + int64(nRead), err - } - n += int64(nRead) - polyDegree := int(binary.BigEndian.Uint64(buf[:])) - - if nRead, err = io.ReadFull(r, buf[:]); err != nil { - return n + int64(nRead), err - } - n += int64(nRead) - lweStdDev := math.Float64frombits(binary.BigEndian.Uint64(buf[:])) - - if nRead, err = io.ReadFull(r, buf[:]); err != nil { - return n + int64(nRead), err - } - n += int64(nRead) - glweStdDev := math.Float64frombits(binary.BigEndian.Uint64(buf[:])) - - if nRead, err = io.ReadFull(r, buf[:]); err != nil { - return n + int64(nRead), err + if nRead64, err = p.singleKeyParameters.ReadFrom(r); err != nil { + return n + nRead64, err } - n += int64(nRead) - blockSize := int(binary.BigEndian.Uint64(buf[:])) + n += nRead64 if nRead, err = io.ReadFull(r, buf[:]); err != nil { return n + int64(nRead), err } n += int64(nRead) - messageModulus := T(binary.BigEndian.Uint64(buf[:])) - - var blindRotateParameters tfhe.GadgetParameters[T] - if nRead64, err = blindRotateParameters.ReadFrom(r); err != nil { - return n + nRead64, err - } - n += nRead64 - - var keySwitchParameters tfhe.GadgetParameters[T] - if nRead64, err = keySwitchParameters.ReadFrom(r); err != nil { - return n + nRead64, err - } - n += nRead64 + partyCount := int(binary.BigEndian.Uint64(buf[:])) var accumulatorParameters tfhe.GadgetParameters[T] if nRead64, err = accumulatorParameters.ReadFrom(r); err != nil { @@ -651,31 +371,13 @@ func (p *Parameters[T]) ReadFrom(r io.Reader) (n int64, err error) { } n += nRead64 - if nRead, err = io.ReadFull(r, buf[:1]); err != nil { - return n + nRead64, err - } - n += int64(nRead) - bootstrapOrder := tfhe.BootstrapOrder(buf[0]) - *p = ParametersLiteral[T]{ - PartyCount: partyCount, - - SingleLWEDimension: singleLWEDimension, - PolyDegree: polyDegree, + SingleKeyParametersLiteral: p.singleKeyParameters.Literal(), - LWEStdDev: lweStdDev, - GLWEStdDev: glweStdDev, - - BlockSize: blockSize, - - MessageModulus: messageModulus, + PartyCount: partyCount, - BlindRotateParameters: blindRotateParameters.Literal(), - KeySwitchParameters: keySwitchParameters.Literal(), AccumulatorParameters: accumulatorParameters.Literal(), RelinKeyParameters: relinKeyParameters.Literal(), - - BootstrapOrder: bootstrapOrder, }.Compile() return diff --git a/mktfhe/params_list.go b/mktfhe/params_list.go index 0171460..a72b970 100644 --- a/mktfhe/params_list.go +++ b/mktfhe/params_list.go @@ -5,26 +5,32 @@ import "github.com/sp301415/tfhe-go/tfhe" var ( // ParamsBinaryParty2 is a parameter set for binary TFHE with 2 parties. ParamsBinaryParty2 = ParametersLiteral[uint64]{ - PartyCount: 2, + SingleKeyParametersLiteral: tfhe.ParametersLiteral[uint64]{ + LWEDimension: 560, + GLWERank: 1, + PolyDegree: 2048, - SingleLWEDimension: 560, - PolyDegree: 2048, + LWEStdDev: 0.0000305, + GLWEStdDev: 0.00000000000000000463, - LWEStdDev: 0.0000305, - GLWEStdDev: 0.00000000000000000463, + BlockSize: 2, - BlockSize: 2, + MessageModulus: 1 << 1, - MessageModulus: 1 << 1, + BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 12, + Level: 3, + }, + KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 2, + Level: 8, + }, - BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 12, - Level: 3, - }, - KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 2, - Level: 8, + BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, }, + + PartyCount: 2, + AccumulatorParameters: tfhe.GadgetParametersLiteral[uint64]{ Base: 1 << 6, Level: 2, @@ -33,31 +39,36 @@ var ( Base: 1 << 10, Level: 3, }, - BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, } // ParamsBinaryParty4 is a parameter set for binary TFHE with 4 parties. ParamsBinaryParty4 = ParametersLiteral[uint64]{ - PartyCount: 4, + SingleKeyParametersLiteral: tfhe.ParametersLiteral[uint64]{ + LWEDimension: 560, + GLWERank: 1, + PolyDegree: 2048, - SingleLWEDimension: 560, - PolyDegree: 2048, + LWEStdDev: 0.0000305, + GLWEStdDev: 0.00000000000000000463, - LWEStdDev: 0.0000305, - GLWEStdDev: 0.00000000000000000463, + BlockSize: 2, - BlockSize: 2, + MessageModulus: 1 << 1, - MessageModulus: 1 << 1, + BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 8, + Level: 5, + }, + KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 2, + Level: 8, + }, - BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 8, - Level: 5, - }, - KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 2, - Level: 8, + BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, }, + + PartyCount: 4, + AccumulatorParameters: tfhe.GadgetParametersLiteral[uint64]{ Base: 1 << 8, Level: 2, @@ -66,32 +77,36 @@ var ( Base: 1 << 6, Level: 7, }, - - BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, } // ParamsBinaryParty8 is a parameter set for binary TFHE with 8 parties. ParamsBinaryParty8 = ParametersLiteral[uint64]{ - PartyCount: 8, + SingleKeyParametersLiteral: tfhe.ParametersLiteral[uint64]{ + LWEDimension: 560, + GLWERank: 1, + PolyDegree: 2048, - SingleLWEDimension: 560, - PolyDegree: 2048, + LWEStdDev: 0.0000305, + GLWEStdDev: 0.00000000000000000463, - LWEStdDev: 0.0000305, - GLWEStdDev: 0.00000000000000000463, + BlockSize: 2, - BlockSize: 2, + MessageModulus: 1 << 1, - MessageModulus: 1 << 1, + BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 9, + Level: 4, + }, + KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 2, + Level: 8, + }, - BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 9, - Level: 4, - }, - KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 2, - Level: 8, + BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, }, + + PartyCount: 8, + AccumulatorParameters: tfhe.GadgetParametersLiteral[uint64]{ Base: 1 << 6, Level: 3, @@ -100,33 +115,36 @@ var ( Base: 1 << 4, Level: 8, }, - - BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, } // ParamsBinaryParty16 is a parameter set for binary TFHE with 16 parties. ParamsBinaryParty16 = ParametersLiteral[uint64]{ - PartyCount: 16, + SingleKeyParametersLiteral: tfhe.ParametersLiteral[uint64]{ + LWEDimension: 560, + GLWERank: 1, + PolyDegree: 2048, - SingleLWEDimension: 560, - PolyDegree: 2048, + LWEStdDev: 0.0000305, + GLWEStdDev: 0.00000000000000000463, - LWEStdDev: 0.0000305, - GLWEStdDev: 0.00000000000000000463, + BlockSize: 2, - BlockSize: 2, + MessageModulus: 1 << 1, - MessageModulus: 1 << 1, + BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 8, + Level: 5, + }, + KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 2, + Level: 8, + }, - BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 8, - Level: 5, - }, - KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 2, - Level: 8, + BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, }, + PartyCount: 16, + AccumulatorParameters: tfhe.GadgetParametersLiteral[uint64]{ Base: 1 << 6, Level: 3, @@ -135,31 +153,36 @@ var ( Base: 1 << 4, Level: 9, }, - BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, } // ParamsBinaryParty32 is a parameter set for binary TFHE with 32 parties. ParamsBinaryParty32 = ParametersLiteral[uint64]{ - PartyCount: 32, + SingleKeyParametersLiteral: tfhe.ParametersLiteral[uint64]{ + LWEDimension: 560, + GLWERank: 1, + PolyDegree: 2048, - SingleLWEDimension: 560, - PolyDegree: 2048, + LWEStdDev: 0.0000305, + GLWEStdDev: 0.00000000000000000463, - LWEStdDev: 0.0000305, - GLWEStdDev: 0.00000000000000000463, + BlockSize: 2, - BlockSize: 2, + MessageModulus: 1 << 1, - MessageModulus: 1 << 1, + BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 7, + Level: 6, + }, + KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ + Base: 1 << 2, + Level: 8, + }, - BlindRotateParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 7, - Level: 6, - }, - KeySwitchParameters: tfhe.GadgetParametersLiteral[uint64]{ - Base: 1 << 2, - Level: 8, + BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, }, + + PartyCount: 32, + AccumulatorParameters: tfhe.GadgetParametersLiteral[uint64]{ Base: 1 << 7, Level: 3, @@ -168,7 +191,5 @@ var ( Base: 1 << 2, Level: 16, }, - - BootstrapOrder: tfhe.OrderBlindRotateKeySwitch, } )