Documentation
¶
Overview ¶
Package accel provides hardware acceleration for ZK operations.
Backend selection uses a priority-based registry:
- Pure Go (priority 0): Always available, no dependencies
- MLX (priority 100): CGO path - Metal/CUDA/CPU fallback via luxcpp
- FPGA (priority 200): Optional, requires fpga build tag
With CGO_ENABLED=0: Pure Go backend With CGO_ENABLED=1: MLX backend (highest priority, handles GPU/CPU internally)
Package accel provides hardware acceleration for ZK operations.
Two backends:
- Pure Go: Always available, portable
- MLX: CGO path with Metal/CUDA/CPU fallback via luxcpp
With CGO_ENABLED=0: Pure Go With CGO_ENABLED=1: MLX (auto-selects Metal, CUDA, or optimized CPU)
Index ¶
- Variables
- func BackendInfo(name string) string
- func GetAvailableBackends() []string
- func Register(name string, priority int, ctor func(Config) (Accelerator, error))
- type Accelerator
- type Backend
- type BenchmarkResult
- type Capabilities
- type Ciphertext
- type Config
- type FieldElement
- type GoAccelerator
- func (a *GoAccelerator) AggregateProofs(proofs []*Proof) (*Proof, error)
- func (a *GoAccelerator) Backend() Backend
- func (a *GoAccelerator) BatchHash(inputs [][]FieldElement, config HashConfig) ([]FieldElement, error)
- func (a *GoAccelerator) Benchmark(ops int) BenchmarkResult
- func (a *GoAccelerator) Capabilities() Capabilities
- func (a *GoAccelerator) Close() error
- func (a *GoAccelerator) Device() string
- func (a *GoAccelerator) FHEAdd(x, y *Ciphertext) (*Ciphertext, error)
- func (a *GoAccelerator) FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)
- func (a *GoAccelerator) FHEMul(x, y *Ciphertext) (*Ciphertext, error)
- func (a *GoAccelerator) FHESub(x, y *Ciphertext) (*Ciphertext, error)
- func (a *GoAccelerator) GenerateProof(witness []FieldElement, publicInput []FieldElement, pk *ProvingKey) (*Proof, error)
- func (a *GoAccelerator) Hash(input []FieldElement, config HashConfig) (FieldElement, error)
- func (a *GoAccelerator) INTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
- func (a *GoAccelerator) IsGPUAvailable() bool
- func (a *GoAccelerator) MSM(points []Point, scalars []FieldElement, config MSMConfig) (Point, error)
- func (a *GoAccelerator) NTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
- func (a *GoAccelerator) VerifyProof(proof *Proof, publicInput []FieldElement, vk *VerifyingKey) (bool, error)
- type HashConfig
- type MLXAccelerator
- func (a *MLXAccelerator) AggregateProofs(proofs []*Proof) (*Proof, error)
- func (a *MLXAccelerator) Backend() Backend
- func (a *MLXAccelerator) BatchHash(inputs [][]FieldElement, config HashConfig) ([]FieldElement, error)
- func (a *MLXAccelerator) Benchmark(ops int) BenchmarkResult
- func (a *MLXAccelerator) Capabilities() Capabilities
- func (a *MLXAccelerator) Close() error
- func (a *MLXAccelerator) Device() string
- func (a *MLXAccelerator) FHEAdd(x, y *Ciphertext) (*Ciphertext, error)
- func (a *MLXAccelerator) FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)
- func (a *MLXAccelerator) FHEMul(x, y *Ciphertext) (*Ciphertext, error)
- func (a *MLXAccelerator) FHESub(x, y *Ciphertext) (*Ciphertext, error)
- func (a *MLXAccelerator) GenerateProof(witness []FieldElement, publicInput []FieldElement, pk *ProvingKey) (*Proof, error)
- func (a *MLXAccelerator) Hash(input []FieldElement, config HashConfig) (FieldElement, error)
- func (a *MLXAccelerator) INTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
- func (a *MLXAccelerator) IsGPUAvailable() bool
- func (a *MLXAccelerator) MSM(points []Point, scalars []FieldElement, config MSMConfig) (Point, error)
- func (a *MLXAccelerator) NTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
- func (a *MLXAccelerator) VerifyProof(proof *Proof, publicInput []FieldElement, vk *VerifyingKey) (bool, error)
- type MSMConfig
- type NTTConfig
- type Point
- type Polynomial
- type Proof
- type ProvingKey
- type VerifyingKey
Constants ¶
This section is empty.
Variables ¶
var ( ErrProofGeneration = errors.New("proof generation failed") ErrProofVerification = errors.New("proof verification failed") ErrNTTComputation = errors.New("NTT computation failed") ErrMSMComputation = errors.New("MSM computation failed") ErrHashComputation = errors.New("hash computation failed") ErrFHEOperation = errors.New("FHE operation failed") )
var (
ErrNoBackend = errors.New("no accelerator backend registered")
)
Functions ¶
func BackendInfo ¶
BackendInfo returns description of a backend
func GetAvailableBackends ¶
func GetAvailableBackends() []string
GetAvailableBackends returns names of all registered backends, sorted by priority
Types ¶
type Accelerator ¶
type Accelerator interface {
// Info
Backend() Backend
Device() string
IsGPUAvailable() bool
Capabilities() Capabilities
Close() error
// Core ZK Operations
NTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
INTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
MSM(points []Point, scalars []FieldElement, config MSMConfig) (Point, error)
Hash(input []FieldElement, config HashConfig) (FieldElement, error)
BatchHash(inputs [][]FieldElement, config HashConfig) ([]FieldElement, error)
// Proof Operations
GenerateProof(witness []FieldElement, publicInput []FieldElement, pk *ProvingKey) (*Proof, error)
VerifyProof(proof *Proof, publicInput []FieldElement, vk *VerifyingKey) (bool, error)
AggregateProofs(proofs []*Proof) (*Proof, error)
// FHE Operations (optional, only if EnableFHE)
FHEAdd(a, b *Ciphertext) (*Ciphertext, error)
FHESub(a, b *Ciphertext) (*Ciphertext, error)
FHEMul(a, b *Ciphertext) (*Ciphertext, error)
FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)
// Benchmarking
Benchmark(ops int) BenchmarkResult
}
Accelerator interface for ZK operations
func NewAccelerator ¶
func NewAccelerator(config Config) (Accelerator, error)
NewAccelerator creates the best available accelerator. Uses LUX_ZK_BACKEND env var to force a specific backend, otherwise picks highest priority.
type BenchmarkResult ¶
type BenchmarkResult struct {
Backend Backend
Device string
NTTOpsPerSec float64
MSMOpsPerSec float64
HashOpsPerSec float64
ProofsPerSec float64
LatencyNs int64
}
BenchmarkResult contains benchmark results
func (BenchmarkResult) String ¶
func (b BenchmarkResult) String() string
String returns a string representation of the benchmark
type Capabilities ¶
type Capabilities struct {
SupportsNTT bool
SupportsMSM bool
SupportsGroth16 bool
SupportsPLONK bool
SupportsSTARK bool
SupportsFHE bool
MaxPolynomialDeg int
MaxMSMPoints int
MaxBatchSize int
ParallelProofs int
}
Capabilities describes what the accelerator supports
type Ciphertext ¶
Ciphertext represents an FHE ciphertext
type Config ¶
type Config struct {
Backend Backend `json:"backend"`
Device string `json:"device"`
MaxBatchSize int `json:"maxBatchSize"`
NumThreads int `json:"numThreads"`
EnableFHE bool `json:"enableFHE"`
ProofSystem string `json:"proofSystem"` // groth16, plonk, stark
SecurityLevel int `json:"securityLevel"` // 128, 192, 256
}
Config for ZK accelerator
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns default accelerator configuration
type FieldElement ¶
type FieldElement struct {
Limbs [4]uint64 // 256-bit field element
}
FieldElement represents an element in the finite field
type GoAccelerator ¶
type GoAccelerator struct {
// contains filtered or unexported fields
}
GoAccelerator is the pure Go implementation of the ZK accelerator
func NewGoAccelerator ¶
func NewGoAccelerator(config Config) (*GoAccelerator, error)
NewGoAccelerator creates a pure Go accelerator
func (*GoAccelerator) AggregateProofs ¶
func (a *GoAccelerator) AggregateProofs(proofs []*Proof) (*Proof, error)
AggregateProofs aggregates multiple proofs into one
func (*GoAccelerator) Backend ¶
func (a *GoAccelerator) Backend() Backend
func (*GoAccelerator) BatchHash ¶
func (a *GoAccelerator) BatchHash(inputs [][]FieldElement, config HashConfig) ([]FieldElement, error)
BatchHash computes hashes in parallel
func (*GoAccelerator) Benchmark ¶
func (a *GoAccelerator) Benchmark(ops int) BenchmarkResult
Benchmark runs performance benchmarks
func (*GoAccelerator) Capabilities ¶
func (a *GoAccelerator) Capabilities() Capabilities
func (*GoAccelerator) Close ¶
func (a *GoAccelerator) Close() error
func (*GoAccelerator) Device ¶
func (a *GoAccelerator) Device() string
func (*GoAccelerator) FHEAdd ¶
func (a *GoAccelerator) FHEAdd(x, y *Ciphertext) (*Ciphertext, error)
FHEAdd performs homomorphic addition on RLWE ciphertexts. For RLWE: ct_add = (c0_x + c0_y mod Q, c1_x + c1_y mod Q)
func (*GoAccelerator) FHEBootstrap ¶
func (a *GoAccelerator) FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)
FHEBootstrap performs noise reduction via bootstrapping. This is a simplified modulus switching operation that reduces noise at the cost of precision. Full bootstrapping requires evaluation keys.
func (*GoAccelerator) FHEMul ¶
func (a *GoAccelerator) FHEMul(x, y *Ciphertext) (*Ciphertext, error)
FHEMul performs homomorphic multiplication on RLWE ciphertexts. This performs polynomial multiplication (convolution in coefficient domain). The result is a degree-2 ciphertext that should be relinearized.
func (*GoAccelerator) FHESub ¶
func (a *GoAccelerator) FHESub(x, y *Ciphertext) (*Ciphertext, error)
FHESub performs homomorphic subtraction on RLWE ciphertexts. For RLWE: ct_sub = (c0_x - c0_y mod Q, c1_x - c1_y mod Q)
func (*GoAccelerator) GenerateProof ¶
func (a *GoAccelerator) GenerateProof(witness []FieldElement, publicInput []FieldElement, pk *ProvingKey) (*Proof, error)
GenerateProof generates a ZK proof
func (*GoAccelerator) Hash ¶
func (a *GoAccelerator) Hash(input []FieldElement, config HashConfig) (FieldElement, error)
Hash computes a cryptographic hash using Poseidon2
func (*GoAccelerator) INTT ¶
func (a *GoAccelerator) INTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
INTT performs Inverse Number Theoretic Transform
func (*GoAccelerator) IsGPUAvailable ¶
func (a *GoAccelerator) IsGPUAvailable() bool
func (*GoAccelerator) MSM ¶
func (a *GoAccelerator) MSM(points []Point, scalars []FieldElement, config MSMConfig) (Point, error)
MSM performs Multi-Scalar Multiplication using Pippenger's algorithm
func (*GoAccelerator) NTT ¶
func (a *GoAccelerator) NTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
NTT performs Number Theoretic Transform (FFT over finite field)
func (*GoAccelerator) VerifyProof ¶
func (a *GoAccelerator) VerifyProof(proof *Proof, publicInput []FieldElement, vk *VerifyingKey) (bool, error)
VerifyProof verifies a ZK proof
type HashConfig ¶
type HashConfig struct {
Algorithm string // poseidon, poseidon2, keccak256, blake3
Rate int // Sponge rate
Capacity int // Sponge capacity
}
HashConfig configuration for hash operations
type MLXAccelerator ¶ added in v1.22.77
type MLXAccelerator struct {
// contains filtered or unexported fields
}
MLXAccelerator uses Apple Silicon Metal GPU via luxfi/mlx
func NewMLXAccelerator ¶ added in v1.22.77
func NewMLXAccelerator(config Config) (*MLXAccelerator, error)
NewMLXAccelerator creates an MLX-accelerated ZK prover
func (*MLXAccelerator) AggregateProofs ¶ added in v1.22.77
func (a *MLXAccelerator) AggregateProofs(proofs []*Proof) (*Proof, error)
AggregateProofs aggregates multiple proofs
func (*MLXAccelerator) Backend ¶ added in v1.22.77
func (a *MLXAccelerator) Backend() Backend
func (*MLXAccelerator) BatchHash ¶ added in v1.22.77
func (a *MLXAccelerator) BatchHash(inputs [][]FieldElement, config HashConfig) ([]FieldElement, error)
BatchHash computes multiple hashes in parallel on GPU
func (*MLXAccelerator) Benchmark ¶ added in v1.22.77
func (a *MLXAccelerator) Benchmark(ops int) BenchmarkResult
Benchmark runs GPU performance benchmarks
func (*MLXAccelerator) Capabilities ¶ added in v1.22.77
func (a *MLXAccelerator) Capabilities() Capabilities
func (*MLXAccelerator) Close ¶ added in v1.22.77
func (a *MLXAccelerator) Close() error
func (*MLXAccelerator) Device ¶ added in v1.22.77
func (a *MLXAccelerator) Device() string
func (*MLXAccelerator) FHEAdd ¶ added in v1.22.77
func (a *MLXAccelerator) FHEAdd(x, y *Ciphertext) (*Ciphertext, error)
FHE Operations - delegated to Go for now
func (*MLXAccelerator) FHEBootstrap ¶ added in v1.22.77
func (a *MLXAccelerator) FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)
func (*MLXAccelerator) FHEMul ¶ added in v1.22.77
func (a *MLXAccelerator) FHEMul(x, y *Ciphertext) (*Ciphertext, error)
func (*MLXAccelerator) FHESub ¶ added in v1.22.77
func (a *MLXAccelerator) FHESub(x, y *Ciphertext) (*Ciphertext, error)
func (*MLXAccelerator) GenerateProof ¶ added in v1.22.77
func (a *MLXAccelerator) GenerateProof(witness []FieldElement, publicInput []FieldElement, pk *ProvingKey) (*Proof, error)
GenerateProof generates a ZK proof with GPU acceleration
func (*MLXAccelerator) Hash ¶ added in v1.22.77
func (a *MLXAccelerator) Hash(input []FieldElement, config HashConfig) (FieldElement, error)
Hash computes GPU-accelerated Poseidon hash
func (*MLXAccelerator) INTT ¶ added in v1.22.77
func (a *MLXAccelerator) INTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
INTT performs GPU-accelerated Inverse NTT
func (*MLXAccelerator) IsGPUAvailable ¶ added in v1.22.77
func (a *MLXAccelerator) IsGPUAvailable() bool
func (*MLXAccelerator) MSM ¶ added in v1.22.77
func (a *MLXAccelerator) MSM(points []Point, scalars []FieldElement, config MSMConfig) (Point, error)
MSM performs GPU-accelerated Multi-Scalar Multiplication
func (*MLXAccelerator) NTT ¶ added in v1.22.77
func (a *MLXAccelerator) NTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)
NTT performs GPU-accelerated Number Theoretic Transform
func (*MLXAccelerator) VerifyProof ¶ added in v1.22.77
func (a *MLXAccelerator) VerifyProof(proof *Proof, publicInput []FieldElement, vk *VerifyingKey) (bool, error)
VerifyProof verifies a ZK proof
type NTTConfig ¶
type NTTConfig struct {
LogN int // log2 of polynomial degree
Modulus uint64 // Field modulus
Forward bool // Forward or inverse NTT
InPlace bool // Modify input in place
}
NTTConfig configuration for Number Theoretic Transform
type Point ¶
type Point struct {
X, Y FieldElement
Z FieldElement // Projective coordinate (optional)
}
Point represents a point on an elliptic curve
type Polynomial ¶
type Polynomial struct {
Coeffs []FieldElement
Degree int
}
Polynomial represents a polynomial over the field
type Proof ¶
type Proof struct {
System string // groth16, plonk, stark
ProofBytes []byte
PublicIO []FieldElement
Metadata map[string]interface{}
}
Proof represents a ZK proof
type ProvingKey ¶
ProvingKey represents a proving key
type VerifyingKey ¶
VerifyingKey represents a verification key