fhe

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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 DecodeBFV

func DecodeBFV(params Params, pt *Plaintext) ([]int64, error)

DecodeBFV decodes a BFV plaintext to integers.

func DecodeCKKS

func DecodeCKKS(params Params, pt *Plaintext) ([]float64, error)

DecodeCKKS decodes a CKKS plaintext to floats.

func KeyGen

func KeyGen(params Params) (*SecretKey, *PublicKey, error)

KeyGen generates a secret/public key pair.

func SetDevice

func SetDevice(device int) error

SetDevice returns an error when accel is not available.

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

type GPUMemoryInfo struct {
	FreeMem  int64
	TotalMem int64
}

GPUMemoryInfo holds memory information for a GPU.

type GaloisKey

type GaloisKey struct {
	Data  []uint64
	Steps []int // Rotation steps this key supports
}

GaloisKey for rotation operations.

func GenGaloisKey

func GenGaloisKey(params Params, sk *SecretKey, steps []int) (*GaloisKey, error)

GenGaloisKey generates Galois keys for rotation.

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 (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.

func EncodeBFV

func EncodeBFV(params Params, values []int64) (*Plaintext, error)

EncodeBFV encodes integers into a BFV plaintext.

func EncodeCKKS

func EncodeCKKS(params Params, values []float64) (*Plaintext, error)

EncodeCKKS encodes floats into a CKKS plaintext.

type PublicKey

type PublicKey struct {
	Data []uint64
}

PublicKey for encryption.

type RelinKey

type RelinKey struct {
	Data []uint64
}

RelinKey for relinearization after multiplication.

func GenRelinKey

func GenRelinKey(params Params, sk *SecretKey) (*RelinKey, error)

GenRelinKey generates a relinearization key.

type Scheme

type Scheme uint8

Scheme identifies the FHE scheme.

const (
	SchemeBFV  Scheme = iota // BFV: exact integer arithmetic
	SchemeCKKS               // CKKS: approximate real arithmetic
)

type SecretKey

type SecretKey struct {
	Data []uint64
}

SecretKey for decryption.

type WorkDistribution

type WorkDistribution struct {
	WorkPerGPU []uint32
	Offsets    []uint32
}

WorkDistribution describes how work is distributed across GPUs.

Jump to

Keyboard shortcuts

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