From 77c62e64218555c616efdb2ab7b500d3c6c7d5da Mon Sep 17 00:00:00 2001 From: lehugueni Date: Thu, 12 Dec 2024 11:22:39 +0100 Subject: [PATCH] bufferpool uint64 in ring --- core/rlwe/ciphertext.go | 12 ++++--- core/rlwe/evaluator.go | 38 ++++++++++------------ core/rlwe/params.go | 10 ++++-- ring/basis_extension.go | 24 +++++++++----- ring/poly.go | 2 +- ring/ring.go | 51 +++++++++++++++++++++++++----- ring/ring_test.go | 10 ++++-- ring/ringqp/ring.go | 14 ++++---- schemes/ckks/encoder.go | 15 ++++++--- utils/structs/concurrent_buffer.go | 12 +++---- 10 files changed, 122 insertions(+), 66 deletions(-) diff --git a/core/rlwe/ciphertext.go b/core/rlwe/ciphertext.go index bfe1d721..d92ad4eb 100644 --- a/core/rlwe/ciphertext.go +++ b/core/rlwe/ciphertext.go @@ -5,7 +5,6 @@ import ( "github.com/tuneinsight/lattigo/v6/ring" "github.com/tuneinsight/lattigo/v6/utils/sampling" - "github.com/tuneinsight/lattigo/v6/utils/structs" ) // Ciphertext is a generic type for RLWE ciphertexts. @@ -71,12 +70,14 @@ func (ct Ciphertext) Equal(other *Ciphertext) bool { return ct.Element.Equal(&other.Element) } -func NewCiphertextFromUintPool(pool structs.BufferPool[*[]uint64], params ParameterProvider, degree int, level int) *Ciphertext { +func NewCiphertextFromUintPool(params ParameterProvider, degree int, levelQ int) *Ciphertext { p := params.GetRLWEParameters() + ringQ := p.RingQ().AtLevel(levelQ) + Value := make([]ring.Poly, degree+1) for i := range Value { - Value[i] = *ring.NewPolyFromUintPool(pool, p.N(), level) + Value[i] = *ringQ.NewPolyFromUintPool() } el := Element[ring.Poly]{ @@ -90,9 +91,10 @@ func NewCiphertextFromUintPool(pool structs.BufferPool[*[]uint64], params Parame return &Ciphertext{el} } -func RecycleCiphertextInUintPool(pool structs.BufferPool[*[]uint64], ct *Ciphertext) { +func RecycleCiphertextInUintPool(params ParameterProvider, ct *Ciphertext) { + ringQ := params.GetRLWEParameters().ringQ for i := range ct.Value { - ring.RecyclePolyInUintPool(pool, &ct.Value[i]) + ringQ.RecyclePolyInUintPool(&ct.Value[i]) } ct = nil return diff --git a/core/rlwe/evaluator.go b/core/rlwe/evaluator.go index 193d4f1a..0bf0a6c2 100644 --- a/core/rlwe/evaluator.go +++ b/core/rlwe/evaluator.go @@ -38,37 +38,33 @@ func newBuffer[T any](f func() T) structs.BufferPool[T] { func NewEvaluatorBuffersWithUintPool(params Parameters) *EvaluatorBuffers { buff := new(EvaluatorBuffers) ringQP := params.RingQP() + ringQ := params.ringQ - buffUint := newBuffer(func() *[]uint64 { - buff := make([]uint64, params.RingQ().N()) - return &buff - }) - - buff.BuffQPPool = structs.NewBuffFromUintPool(buffUint, - func(bp structs.BufferPool[*[]uint64]) *ringqp.Poly { - return ringQP.NewPolyQPFromUintPool(bp) + buff.BuffQPPool = structs.NewBuffFromUintPool( + func() *ringqp.Poly { + return ringQP.NewPolyQPFromUintPool() }, - func(bp structs.BufferPool[*[]uint64], poly *ringqp.Poly) { - ringqp.RecyclePolyQPFromUintPool(bp, poly) + func(poly *ringqp.Poly) { + ringQP.RecyclePolyQPFromUintPool(poly) }, ) - buff.BuffQPool = structs.NewBuffFromUintPool(buffUint, - func(bp structs.BufferPool[*[]uint64]) *ring.Poly { - return ring.NewPolyFromUintPool(bp, params.ringQ.N(), params.ringQ.Level()) + buff.BuffQPool = structs.NewBuffFromUintPool( + func() *ring.Poly { + return ringQ.NewPolyFromUintPool() }, - func(bp structs.BufferPool[*[]uint64], poly *ring.Poly) { - ring.RecyclePolyInUintPool(bp, poly) + func(poly *ring.Poly) { + ringQ.RecyclePolyInUintPool(poly) }, ) - buff.BuffCtPool = structs.NewBuffFromUintPool(buffUint, - func(bp structs.BufferPool[*[]uint64]) *Ciphertext { - return NewCiphertextFromUintPool(bp, params, 2, params.MaxLevel()) + buff.BuffCtPool = structs.NewBuffFromUintPool( + func() *Ciphertext { + return NewCiphertextFromUintPool(params, 2, params.MaxLevel()) }, - func(bp structs.BufferPool[*[]uint64], ct *Ciphertext) { - RecycleCiphertextInUintPool(bp, ct) + func(ct *Ciphertext) { + RecycleCiphertextInUintPool(params, ct) }, ) - buff.BuffBitPool = buffUint + buff.BuffBitPool = ringQ.BufferPool() return buff } diff --git a/core/rlwe/params.go b/core/rlwe/params.go index 18b2467f..bffc52a5 100644 --- a/core/rlwe/params.go +++ b/core/rlwe/params.go @@ -15,6 +15,7 @@ import ( "github.com/tuneinsight/lattigo/v6/ring/ringqp" "github.com/tuneinsight/lattigo/v6/utils" "github.com/tuneinsight/lattigo/v6/utils/buffer" + "github.com/tuneinsight/lattigo/v6/utils/structs" ) // MaxLogN is the log2 of the largest supported polynomial modulus degree. @@ -855,11 +856,16 @@ func GenModuli(LogNthRoot int, logQ, logP []int) (q, p []uint64, err error) { } func (p *Parameters) initRings() (err error) { - if p.ringQ, err = ring.NewRingFromType(1<>1) return &buff }) - ecd.BuffPolyPool = structs.NewSyncPool(func() *ring.Poly { - poly := parameters.RingQ().NewPoly() - return &poly - }) + + ringQ := parameters.RingQ() + + ecd.BuffPolyPool = structs.NewBuffFromUintPool( + func() *ring.Poly { + return ringQ.NewPolyFromUintPool() + }, + func(poly *ring.Poly) { + ringQ.RecyclePolyInUintPool(poly) + }, + ) if prec <= 53 { diff --git a/utils/structs/concurrent_buffer.go b/utils/structs/concurrent_buffer.go index 5342d755..37cc46a1 100644 --- a/utils/structs/concurrent_buffer.go +++ b/utils/structs/concurrent_buffer.go @@ -29,25 +29,23 @@ func (spool *SyncPool[T]) Put(buff T) { } type BuffFromUintPool[T any] struct { - uintPool BufferPool[*[]uint64] // Pool that must contain *[]uint64 objects - createObject func(BufferPool[*[]uint64]) T - recycleObject func(BufferPool[*[]uint64], T) + createObject func() T + recycleObject func(T) } -func NewBuffFromUintPool[T any](pool BufferPool[*[]uint64], create func(BufferPool[*[]uint64]) T, recycle func(BufferPool[*[]uint64], T)) *BuffFromUintPool[T] { +func NewBuffFromUintPool[T any](create func() T, recycle func(T)) *BuffFromUintPool[T] { return &BuffFromUintPool[T]{ - uintPool: pool, createObject: create, recycleObject: recycle, } } func (bu *BuffFromUintPool[T]) Get() T { - return bu.createObject(bu.uintPool) + return bu.createObject() } func (bu *BuffFromUintPool[T]) Put(obj T) { - bu.recycleObject(bu.uintPool, obj) + bu.recycleObject(obj) } type FreeList[T any] struct {