accel

package
v1.22.21 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package accel provides cross-platform hardware acceleration for Z-Chain ZK operations. Supports pure Go (default), MLX (Apple Silicon), CGO/C++ (Linux), and CUDA (future).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedBackend = errors.New("unsupported acceleration backend")
	ErrBackendNotCompiled = errors.New("backend not compiled into binary")
)
View Source
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")
)

Functions

func BackendInfo

func BackendInfo(backend Backend) string

BackendInfo returns information about a backend

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 appropriate ZK accelerator based on configuration Priority (if Backend is Auto):

  1. FPGA (if compiled with -tags fpga and hardware available)
  2. MLX (if on Apple Silicon)
  3. CGO (if compiled with CGO_ENABLED=1 on Linux)
  4. Go (always available)

type Backend

type Backend string

Backend represents the compute backend for ZK acceleration

const (
	BackendGo   Backend = "Go"   // Pure Go implementation
	BackendMLX  Backend = "MLX"  // Apple Silicon Metal via MLX
	BackendCGO  Backend = "CGO"  // C++ via CGO (Linux/CUDA)
	BackendCUDA Backend = "CUDA" // Direct CUDA (future)
	BackendFPGA Backend = "FPGA" // FPGA acceleration (future)
	BackendAuto Backend = "Auto" // Auto-detect best backend
)

func GetAvailableBackends

func GetAvailableBackends() []Backend

GetAvailableBackends returns all backends compiled into the binary

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 CGOAccelerator

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

CGOAccelerator uses C++ with OpenMP for ZK acceleration

func NewCGOAccelerator

func NewCGOAccelerator(config Config) (*CGOAccelerator, error)

NewCGOAccelerator creates a CGO-accelerated ZK prover

func (*CGOAccelerator) AggregateProofs

func (a *CGOAccelerator) AggregateProofs(proofs []*Proof) (*Proof, error)

AggregateProofs aggregates multiple proofs

func (*CGOAccelerator) Backend

func (a *CGOAccelerator) Backend() Backend

func (*CGOAccelerator) BatchHash

func (a *CGOAccelerator) BatchHash(inputs [][]FieldElement, config HashConfig) ([]FieldElement, error)

BatchHash computes multiple hashes in parallel

func (*CGOAccelerator) Benchmark

func (a *CGOAccelerator) Benchmark(ops int) BenchmarkResult

Benchmark runs CPU performance benchmarks

func (*CGOAccelerator) Capabilities

func (a *CGOAccelerator) Capabilities() Capabilities

func (*CGOAccelerator) Close

func (a *CGOAccelerator) Close() error

func (*CGOAccelerator) Device

func (a *CGOAccelerator) Device() string

func (*CGOAccelerator) FHEAdd

func (a *CGOAccelerator) FHEAdd(x, y *Ciphertext) (*Ciphertext, error)

FHE Operations - delegated to Go for now

func (*CGOAccelerator) FHEBootstrap

func (a *CGOAccelerator) FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)

func (*CGOAccelerator) FHEMul

func (a *CGOAccelerator) FHEMul(x, y *Ciphertext) (*Ciphertext, error)

func (*CGOAccelerator) FHESub

func (a *CGOAccelerator) FHESub(x, y *Ciphertext) (*Ciphertext, error)

func (*CGOAccelerator) GenerateProof

func (a *CGOAccelerator) GenerateProof(witness []FieldElement, publicInput []FieldElement, pk *ProvingKey) (*Proof, error)

GenerateProof generates a ZK proof with CGO acceleration

func (*CGOAccelerator) Hash

func (a *CGOAccelerator) Hash(input []FieldElement, config HashConfig) (FieldElement, error)

Hash computes OpenMP-accelerated Poseidon hash

func (*CGOAccelerator) INTT

func (a *CGOAccelerator) INTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)

INTT performs OpenMP-accelerated Inverse NTT

func (*CGOAccelerator) IsGPUAvailable

func (a *CGOAccelerator) IsGPUAvailable() bool

func (*CGOAccelerator) MSM

func (a *CGOAccelerator) MSM(points []Point, scalars []FieldElement, config MSMConfig) (Point, error)

MSM performs OpenMP-accelerated Multi-Scalar Multiplication

func (*CGOAccelerator) NTT

func (a *CGOAccelerator) NTT(input []FieldElement, config NTTConfig) ([]FieldElement, error)

NTT performs OpenMP-accelerated Number Theoretic Transform

func (*CGOAccelerator) VerifyProof

func (a *CGOAccelerator) VerifyProof(proof *Proof, publicInput []FieldElement, vk *VerifyingKey) (bool, error)

VerifyProof verifies a ZK proof

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

type Ciphertext struct {
	Data       []byte
	Scheme     string // TFHE, BFV, CKKS
	NoiseLevel uint16
}

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)

FHE Operations (stub implementations)

func (*GoAccelerator) FHEBootstrap

func (a *GoAccelerator) FHEBootstrap(ct *Ciphertext) (*Ciphertext, error)

func (*GoAccelerator) FHEMul

func (a *GoAccelerator) FHEMul(x, y *Ciphertext) (*Ciphertext, error)

func (*GoAccelerator) FHESub

func (a *GoAccelerator) FHESub(x, y *Ciphertext) (*Ciphertext, error)

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 MSMConfig

type MSMConfig struct {
	NumPoints  int
	ScalarBits int
	WindowSize int
	UsePrecomp bool
}

MSMConfig configuration for Multi-Scalar Multiplication

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

type ProvingKey struct {
	System   string
	KeyBytes []byte
}

ProvingKey represents a proving key

type VerifyingKey

type VerifyingKey struct {
	System   string
	KeyBytes []byte
}

VerifyingKey represents a verification key

Jump to

Keyboard shortcuts

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