cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cache provides caching for expensive HE operations.

Package cache provides caching for expensive HE operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchCentroidCache

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

BatchCentroidCache packs multiple centroids into single plaintexts for SIMD operations. This dramatically reduces the number of HE operations by leveraging CKKS slot packing.

Example with 128-dim vectors and 16384 slots:

  • Pack 128 centroids per plaintext (16384/128 = 128)
  • For 64 centroids: single plaintext, single HE operation!
  • Speedup: 64x fewer HE multiplications

Layout in slots: [c0[0..dim], c1[0..dim], c2[0..dim], ...] Query packing: [q[0..dim], q[0..dim], q[0..dim], ...] After multiply: [c0*q[0..dim], c1*q[0..dim], ...] After sum: [dot(c0,q), ?, ?, ..., dot(c1,q), ?, ?, ...] Extract at: positions [0, dim, 2*dim, ...]

func NewBatchCentroidCache

func NewBatchCentroidCache(params hefloat.Parameters, encoder *hefloat.Encoder, dimension int) *BatchCentroidCache

NewBatchCentroidCache creates a new batch centroid cache.

func (*BatchCentroidCache) Clear

func (c *BatchCentroidCache) Clear()

Clear removes all packed plaintexts.

func (*BatchCentroidCache) GetCentroidsPerPack

func (c *BatchCentroidCache) GetCentroidsPerPack() int

GetCentroidsPerPack returns how many centroids are packed per plaintext.

func (*BatchCentroidCache) GetDimension

func (c *BatchCentroidCache) GetDimension() int

GetDimension returns the vector dimension.

func (*BatchCentroidCache) GetNumCentroids

func (c *BatchCentroidCache) GetNumCentroids() int

GetNumCentroids returns the total number of centroids.

func (*BatchCentroidCache) GetNumPacks

func (c *BatchCentroidCache) GetNumPacks() int

GetNumPacks returns the number of packed plaintexts.

func (*BatchCentroidCache) GetPackedPlaintexts

func (c *BatchCentroidCache) GetPackedPlaintexts() []*rlwe.Plaintext

GetPackedPlaintexts returns all packed plaintexts.

func (*BatchCentroidCache) LoadCentroids

func (c *BatchCentroidCache) LoadCentroids(centroids [][]float64, level int) error

LoadCentroids packs all centroids into batch plaintexts. Each plaintext contains centroidsPerPack centroids laid out sequentially.

func (*BatchCentroidCache) PackQuery

func (c *BatchCentroidCache) PackQuery(query []float64) []float64

PackQuery creates a packed query vector matching the centroid layout. The query is replicated centroidsPerPack times to enable SIMD multiply.

func (*BatchCentroidCache) Size

func (c *BatchCentroidCache) Size() int

Size returns the number of packed plaintexts.

type CentroidCache

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

CentroidCache caches pre-encoded centroids for faster HE operations. Encoding centroids as HE plaintexts is expensive, so we cache them.

Cache invalidation:

  • Centroids rarely change (only when data is added/removed)
  • Track last update time to detect staleness
  • Caller can force refresh when needed

func NewCentroidCache

func NewCentroidCache(params hefloat.Parameters, encoder *hefloat.Encoder) *CentroidCache

NewCentroidCache creates a new centroid cache. The params and encoder should be from the HE Engine.

func (*CentroidCache) Clear

func (c *CentroidCache) Clear()

Clear removes all cached centroids.

func (*CentroidCache) Get

func (c *CentroidCache) Get(index int) *rlwe.Plaintext

Get returns a pre-encoded centroid, or nil if not cached.

func (*CentroidCache) GetAll

func (c *CentroidCache) GetAll(count int) []*rlwe.Plaintext

GetAll returns all cached centroids as a slice. Returns nil entries for missing indices.

func (*CentroidCache) IsStale

func (c *CentroidCache) IsStale(maxAge time.Duration) bool

IsStale checks if the cache is older than maxAge.

func (*CentroidCache) LastUpdate

func (c *CentroidCache) LastUpdate() time.Time

LastUpdate returns when the cache was last updated.

func (*CentroidCache) LoadCentroids

func (c *CentroidCache) LoadCentroids(centroids [][]float64, level int) error

LoadCentroids pre-encodes all centroids into the cache. This should be called once when the client starts or when centroids change.

func (*CentroidCache) NeedsRefresh

func (c *CentroidCache) NeedsRefresh(centroids [][]float64) bool

NeedsRefresh checks if any centroid has changed from the cached original.

func (*CentroidCache) Size

func (c *CentroidCache) Size() int

Size returns the number of cached centroids.

func (*CentroidCache) Version

func (c *CentroidCache) Version() int64

Version returns the current cache version (incremented on each load).

type CentroidCacheConfig

type CentroidCacheConfig struct {
	// MaxStaleDuration is how long cached centroids are considered valid.
	// Default: 1 hour
	MaxStaleDuration time.Duration
}

CentroidCacheConfig contains configuration for the cache.

func DefaultCentroidCacheConfig

func DefaultCentroidCacheConfig() CentroidCacheConfig

DefaultCentroidCacheConfig returns sensible defaults.

Jump to

Keyboard shortcuts

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