backend

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package backend defines how `luxfi/math` selects between CPU and GPU implementations of the same primitive.

LP-107 §"Backend dispatch" — the canonical motivation. Backends are interchangeable performance realizations of one canonical contract; KATs prove byte-equality across them; the choice of backend MUST NEVER alter transcript bytes for consensus paths.

Policy enum:

BackendPureGo       — pure-Go reference implementation. Always works.
BackendNativeCPU    — native CPU implementation (cgo + C++/SIMD).
BackendGPUPreferred — try GPU; fall back to CPU on unavailability.
BackendGPURequired  — GPU MUST be available; error if not.

Registry: each substrate package (math/ntt, math/poly, math/sample, math/codec, math/rns) exposes its own backend interface and a process-wide registry of registered backends. This package owns the shared Policy enum + lookup helper; per-primitive interfaces live in the consuming package.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnavailable = errors.New("backend: required backend unavailable")

ErrUnavailable signals that a required backend is not present in the process. Returned by ResolveOrError when PolicyGPURequired is set but no GPU backend is registered.

Functions

func Resolve

func Resolve(policy Policy, registered map[params.BackendID]bool) (params.BackendID, error)

Resolve returns the appropriate BackendID for the given policy and the set of registered backends. The order of preference is:

PolicyGPURequired:  GPU only — error if no GPU registered
PolicyGPUPreferred: GPU > native-cpu > pure-go
PolicyNativeCPU:    native-cpu > pure-go
PolicyPureGo:       pure-go only

This function is the single decision point for dispatch ordering. Per-primitive packages (math/ntt, math/poly, etc.) consult it before invoking a backend.

Types

type Policy

type Policy uint8

Policy selects a backend at dispatch time.

const (
	// PolicyPureGo forces the pure-Go reference path. Used for
	// debugging, incident response, and consensus-critical paths
	// where determinism trumps speed.
	PolicyPureGo Policy = 0

	// PolicyNativeCPU prefers the native-cpu (cgo / SIMD) backend if
	// available; falls back to pure-Go otherwise.
	PolicyNativeCPU Policy = 1

	// PolicyGPUPreferred prefers GPU if available, falls back to native-
	// CPU, then pure-Go. Used when the workload amortizes GPU dispatch.
	PolicyGPUPreferred Policy = 2

	// PolicyGPURequired demands GPU; the call errors out if no GPU
	// backend is registered.
	PolicyGPURequired Policy = 3
)

func (Policy) String

func (p Policy) String() string

String makes Policy printable.

func (Policy) Validate

func (p Policy) Validate() error

Validate reports whether p is a known policy.

Jump to

Keyboard shortcuts

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