Documentation
¶
Overview ¶
Package fhe provides GPU-accelerated Fully Homomorphic Encryption operations.
Supports BFV (exact integer arithmetic) and CKKS (approximate real arithmetic) schemes. All operations use the unified accel backend selection and fallback to CPU. Backend is selected at runtime via LUX_ACCEL_BACKEND env or auto-detection.
Package fhe provides Fully Homomorphic Encryption operations. This is the pure Go fallback when accel build tag is not present.
Copyright (c) 2024-2025 Lux Industries Inc. SPDX-License-Identifier: Apache-2.0
Index ¶
- Variables
- func DecodeBFV(params Params, pt *Plaintext) ([]int64, error)
- func DecodeCKKS(params Params, pt *Plaintext) ([]float64, error)
- func KeyGen(params Params) (*SecretKey, *PublicKey, error)
- func SetDevice(device int) error
- type BootstrapKey
- type Ciphertext
- func Add(params Params, ct1, ct2 *Ciphertext) (*Ciphertext, error)
- func AddPlain(params Params, ct *Ciphertext, pt *Plaintext) (*Ciphertext, error)
- func BatchEncrypt(params Params, pk *PublicKey, pts []*Plaintext) ([]*Ciphertext, error)
- func Bootstrap(params Params, ct *Ciphertext, bsk *BootstrapKey) (*Ciphertext, error)
- func DistributedBatchEncrypt(mgpu *MultiGPUContext, params Params, pk *PublicKey, pts []*Plaintext) ([]*Ciphertext, error)
- func DistributedBatchMultiply(mgpu *MultiGPUContext, params Params, ct1s, ct2s []*Ciphertext, rlk *RelinKey) ([]*Ciphertext, error)
- func Encrypt(params Params, pk *PublicKey, pt *Plaintext) (*Ciphertext, error)
- func ModSwitch(params Params, ct *Ciphertext) (*Ciphertext, error)
- func Multiply(params Params, ct1, ct2 *Ciphertext, rlk *RelinKey) (*Ciphertext, error)
- func MultiplyPlain(params Params, ct *Ciphertext, pt *Plaintext) (*Ciphertext, error)
- func Rescale(params Params, ct *Ciphertext) (*Ciphertext, error)
- func Rotate(params Params, ct *Ciphertext, gk *GaloisKey, steps int) (*Ciphertext, error)
- func Sub(params Params, ct1, ct2 *Ciphertext) (*Ciphertext, error)
- type DistributedNTTData
- type GPUMemoryInfo
- type GaloisKey
- type MultiGPUContext
- func (ctx *MultiGPUContext) Barrier()
- func (ctx *MultiGPUContext) Close()
- func (ctx *MultiGPUContext) DistributeWork(totalElements uint32) (*WorkDistribution, error)
- func (ctx *MultiGPUContext) GPUCount() int
- func (ctx *MultiGPUContext) GetMemoryInfo(device int) (*GPUMemoryInfo, error)
- func (ctx *MultiGPUContext) GetStream(device int) unsafe.Pointer
- func (ctx *MultiGPUContext) HasNVLink() bool
- func (ctx *MultiGPUContext) NTTForward(data *DistributedNTTData) error
- func (ctx *MultiGPUContext) NTTInverse(data *DistributedNTTData) error
- func (ctx *MultiGPUContext) P2PCopy(srcDevice int, srcPtr unsafe.Pointer, dstDevice int, dstPtr unsafe.Pointer, ...) error
- func (ctx *MultiGPUContext) SyncAll() error
- func (ctx *MultiGPUContext) SyncDevice(device int) error
- type Params
- type Plaintext
- func BatchDecrypt(params Params, sk *SecretKey, cts []*Ciphertext) ([]*Plaintext, error)
- func Decrypt(params Params, sk *SecretKey, ct *Ciphertext) (*Plaintext, error)
- func EncodeBFV(params Params, values []int64) (*Plaintext, error)
- func EncodeCKKS(params Params, values []float64) (*Plaintext, error)
- type PublicKey
- type RelinKey
- type Scheme
- type SecretKey
- type WorkDistribution
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidParams = errors.New("fhe: invalid parameters") ErrInvalidInput = errors.New("fhe: invalid input") ErrKeyMismatch = errors.New("fhe: key does not match ciphertext") ErrLevelExhausted = errors.New("fhe: modulus levels exhausted") ErrScaleMismatch = errors.New("fhe: scale mismatch") ErrRotationKey = errors.New("fhe: rotation key not available for requested steps") ErrBootstrapFailed = errors.New("fhe: bootstrap operation failed") )
Functions ¶
func DecodeCKKS ¶
DecodeCKKS decodes a CKKS plaintext to floats.
Types ¶
type BootstrapKey ¶
type BootstrapKey struct {
Data []uint64
}
BootstrapKey for refreshing ciphertext noise.
func GenBootstrapKey ¶
func GenBootstrapKey(params Params, sk *SecretKey) (*BootstrapKey, error)
GenBootstrapKey generates a bootstrapping key.
type Ciphertext ¶
type Ciphertext struct {
Data []uint64 // Polynomial coefficients in RNS form
Scheme Scheme // BFV or CKKS
Level int // Current modulus level
Scale float64 // Current scale (CKKS only)
}
Ciphertext represents an encrypted value.
func Add ¶
func Add(params Params, ct1, ct2 *Ciphertext) (*Ciphertext, error)
Add performs homomorphic addition of two ciphertexts.
func AddPlain ¶
func AddPlain(params Params, ct *Ciphertext, pt *Plaintext) (*Ciphertext, error)
AddPlain adds a plaintext to a ciphertext.
func BatchEncrypt ¶
func BatchEncrypt(params Params, pk *PublicKey, pts []*Plaintext) ([]*Ciphertext, error)
BatchEncrypt encrypts multiple plaintexts in parallel.
func Bootstrap ¶
func Bootstrap(params Params, ct *Ciphertext, bsk *BootstrapKey) (*Ciphertext, error)
Bootstrap refreshes a ciphertext by reducing noise. Requires a bootstrapping key and consumes multiple levels.
func DistributedBatchEncrypt ¶
func DistributedBatchEncrypt(mgpu *MultiGPUContext, params Params, pk *PublicKey, pts []*Plaintext) ([]*Ciphertext, error)
DistributedBatchEncrypt falls back to single-threaded CPU encryption.
func DistributedBatchMultiply ¶
func DistributedBatchMultiply(mgpu *MultiGPUContext, params Params, ct1s, ct2s []*Ciphertext, rlk *RelinKey) ([]*Ciphertext, error)
DistributedBatchMultiply falls back to single-threaded CPU multiplication.
func Encrypt ¶
func Encrypt(params Params, pk *PublicKey, pt *Plaintext) (*Ciphertext, error)
Encrypt encrypts a plaintext.
func ModSwitch ¶
func ModSwitch(params Params, ct *Ciphertext) (*Ciphertext, error)
ModSwitch switches to a smaller modulus without rescaling. Used to match levels before homomorphic operations.
func Multiply ¶
func Multiply(params Params, ct1, ct2 *Ciphertext, rlk *RelinKey) (*Ciphertext, error)
Multiply performs homomorphic multiplication with relinearization.
func MultiplyPlain ¶
func MultiplyPlain(params Params, ct *Ciphertext, pt *Plaintext) (*Ciphertext, error)
MultiplyPlain multiplies a ciphertext by a plaintext.
func Rescale ¶
func Rescale(params Params, ct *Ciphertext) (*Ciphertext, error)
Rescale rescales a CKKS ciphertext after multiplication. Reduces scale and drops one modulus level.
func Rotate ¶
func Rotate(params Params, ct *Ciphertext, gk *GaloisKey, steps int) (*Ciphertext, error)
Rotate rotates ciphertext slots. Positive steps rotate left, negative rotate right.
func Sub ¶
func Sub(params Params, ct1, ct2 *Ciphertext) (*Ciphertext, error)
Sub performs homomorphic subtraction of two ciphertexts.
type DistributedNTTData ¶
type DistributedNTTData struct {
DataPerGPU []unsafe.Pointer
TwiddlesPerGPU []unsafe.Pointer
NPerGPU []uint32
Q uint64
}
DistributedNTTData holds data for NTT operations.
type GPUMemoryInfo ¶
GPUMemoryInfo holds memory information for a GPU.
type MultiGPUContext ¶
type MultiGPUContext struct {
// contains filtered or unexported fields
}
MultiGPUContext is a stub for multi-GPU coordination when accel is not available. All operations fall back to single-threaded CPU execution.
func NewMultiGPUContext ¶
func NewMultiGPUContext() (*MultiGPUContext, error)
NewMultiGPUContext returns a stub context when accel is not available.
func (*MultiGPUContext) Barrier ¶
func (ctx *MultiGPUContext) Barrier()
Barrier is a no-op when accel is not available.
func (*MultiGPUContext) Close ¶
func (ctx *MultiGPUContext) Close()
Close is a no-op for the stub context.
func (*MultiGPUContext) DistributeWork ¶
func (ctx *MultiGPUContext) DistributeWork(totalElements uint32) (*WorkDistribution, error)
DistributeWork returns all work for a single CPU when accel is not available.
func (*MultiGPUContext) GPUCount ¶
func (ctx *MultiGPUContext) GPUCount() int
GPUCount returns 0 when accel is not available.
func (*MultiGPUContext) GetMemoryInfo ¶
func (ctx *MultiGPUContext) GetMemoryInfo(device int) (*GPUMemoryInfo, error)
GetMemoryInfo returns an error when accel is not available.
func (*MultiGPUContext) GetStream ¶
func (ctx *MultiGPUContext) GetStream(device int) unsafe.Pointer
GetStream returns nil when accel is not available.
func (*MultiGPUContext) HasNVLink ¶
func (ctx *MultiGPUContext) HasNVLink() bool
HasNVLink returns false when accel is not available.
func (*MultiGPUContext) NTTForward ¶
func (ctx *MultiGPUContext) NTTForward(data *DistributedNTTData) error
NTTForward returns an error when accel is not available.
func (*MultiGPUContext) NTTInverse ¶
func (ctx *MultiGPUContext) NTTInverse(data *DistributedNTTData) error
NTTInverse returns an error when accel is not available.
func (*MultiGPUContext) P2PCopy ¶
func (ctx *MultiGPUContext) P2PCopy(srcDevice int, srcPtr unsafe.Pointer, dstDevice int, dstPtr unsafe.Pointer, size int) error
P2PCopy returns an error when accel is not available.
func (*MultiGPUContext) SyncAll ¶
func (ctx *MultiGPUContext) SyncAll() error
SyncAll returns an error when accel is not available.
func (*MultiGPUContext) SyncDevice ¶
func (ctx *MultiGPUContext) SyncDevice(device int) error
SyncDevice returns an error when accel is not available.
type Params ¶
type Params struct {
Scheme Scheme // BFV or CKKS
PolyDegree uint32 // N: polynomial degree (power of 2, e.g., 4096, 8192, 16384)
CoeffMods []uint64 // q: coefficient modulus chain
PlainMod uint64 // t: plaintext modulus (BFV only)
Scale float64 // Delta: encoding scale (CKKS only)
}
Params contains FHE scheme parameters.
type Plaintext ¶
type Plaintext struct {
Data []uint64 // Encoded polynomial
Scheme Scheme // BFV or CKKS
Scale float64 // Encoding scale (CKKS only)
}
Plaintext represents a plaintext value.
func BatchDecrypt ¶
func BatchDecrypt(params Params, sk *SecretKey, cts []*Ciphertext) ([]*Plaintext, error)
BatchDecrypt decrypts multiple ciphertexts in parallel.
func Decrypt ¶
func Decrypt(params Params, sk *SecretKey, ct *Ciphertext) (*Plaintext, error)
Decrypt decrypts a ciphertext.
type RelinKey ¶
type RelinKey struct {
Data []uint64
}
RelinKey for relinearization after multiplication.
type WorkDistribution ¶
WorkDistribution describes how work is distributed across GPUs.