ntt

package
v1.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package ntt is the canonical Number-Theoretic-Transform interface for luxfi/math.

LP-107 §"NTT" — the canonical motivation. Production callers (luxfi/lattice, luxfi/pulsar, luxfi/fhe) consume this package's Service abstraction; concrete kernels live behind a Backend interface so AVX2 / NEON / CUDA / Metal / WGSL realizations are interchangeable.

Phase 2 (this file): defines the public surface. The pure-Go reference Backend wraps github.com/luxfi/lattice/v7/ring's SubRing.NTT — the canonical Lattigo-derived Montgomery NTT — so callers see no behavior change. Phase 3 (LP-107) inverts the dependency: lattice/ring imports this package, and the Lattigo kernel body lives here.

Determinism contract: for a fixed (Params, input []uint64), every registered Backend MUST produce byte-equal output. KATs in luxfi/math/ntt/test/kat enforce this across runtimes.

Index

Constants

View Source
const KATSchemaV1 = codec.KATSchemaV1

KATSchemaV1 is the canonical bundle schema string.

Variables

View Source
var ErrUnsupportedParams = errors.New("ntt: backend does not support these Params")

ErrUnsupportedParams is returned by a Backend that does not support the requested Params (e.g. CUDA backend asked for N=32 when it only implements N >= 256).

Functions

func HexDecode added in v1.4.0

func HexDecode(s string) ([]byte, error)

HexDecode is the inverse.

func HexEncode added in v1.4.0

func HexEncode(b []byte) string

HexEncode is the canonical hex encoder shared with codec.

func Register

func Register(b Backend)

Register installs a Backend under its ID. Re-registration replaces. Backends MUST be idempotent — registering the same ID twice with different bodies is a programming error caught at process start when two libraries each try to register the same ID.

func Unregister

func Unregister(id params.BackendID)

Unregister removes a Backend. Used in tests.

func WriteKATBundleFile added in v1.4.0

func WriteKATBundleFile(path string, b *KATBundle) error

WriteKATBundleFile writes a bundle to disk; thin pass-through.

Types

type Backend

type Backend interface {
	// ID returns the BackendID for this backend.
	ID() params.BackendID
	// Supports reports whether this backend can handle p.
	Supports(p *Params) bool
	// Forward applies the forward NTT in-place. dst must have length
	// batch*p.N. Returns ErrUnsupportedParams if Supports returns false.
	Forward(dst []uint64, p *Params, batch uint32) error
	// Inverse applies the inverse NTT in-place. Same length contract.
	Inverse(dst []uint64, p *Params, batch uint32) error
}

Backend is the kernel interface every NTT realization implements. Forward and Inverse operate in-place and MUST produce byte-equal output across all registered backends for the same (Params, input).

func PureGoBackend

func PureGoBackend() Backend

PureGoBackend returns the singleton pure-Go NTT backend. Always available; registered automatically by init().

type KATBundle added in v1.4.0

type KATBundle = codec.KATBundle

KATBundle is an alias for codec.KATBundle.

func ReadKATBundleFile added in v1.4.0

func ReadKATBundleFile(path string) (*KATBundle, error)

ReadKATBundleFile reads a bundle from disk; thin pass-through.

type KATEntry added in v1.4.0

type KATEntry = codec.KATEntry

KATEntry is an alias for codec.KATEntry.

type Params

type Params struct {
	// N is the ring dimension. Must be a power of two.
	N uint32
	// Q is the prime modulus. Must satisfy (Q - 1) | 2N (NTT-friendly).
	Q uint64
	// ID is the canonical parameter identifier (e.g. NTTPulsarN256).
	ID params.NTTParamID
}

Params identifies one NTT instance: ring degree N, modulus Q, and the canonical parameter ID for KAT lookup.

func (*Params) Validate

func (p *Params) Validate() error

Validate ensures (N is a power of two) AND ((Q - 1) | 2N).

type Service

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

Service binds a Params to a chosen Backend (resolved via dispatch policy) and exposes the public Forward / Inverse methods every downstream caller uses.

func NewService

func NewService(p *Params, policy backend.Policy) (*Service, error)

NewService builds a Service for p under the given dispatch policy. The Backend is resolved at construction time from the registered backends; if no backend supports p, returns an error.

func (*Service) Backend

func (s *Service) Backend() params.BackendID

Backend returns the resolved BackendID. Callers print this in logs to record which path executed.

func (*Service) Forward

func (s *Service) Forward(dst []uint64, batch uint32) error

Forward applies the forward NTT in-place via the resolved backend.

func (*Service) Inverse

func (s *Service) Inverse(dst []uint64, batch uint32) error

Inverse applies the inverse NTT in-place via the resolved backend.

func (*Service) Params

func (s *Service) Params() *Params

Params returns the bound parameter set.

Directories

Path Synopsis
Package canonical owns the canonical Lattigo-derived Montgomery NTT body — SubRing layout, root-table generator, scalar primitives (MRed/MRedLazy/MForm/BRed/CRed/...), the loop-unrolled-by-16 NTT kernel, and the SIMD AVX2 dispatch hooks.
Package canonical owns the canonical Lattigo-derived Montgomery NTT body — SubRing layout, root-table generator, scalar primitives (MRed/MRedLazy/MForm/BRed/CRed/...), the loop-unrolled-by-16 NTT kernel, and the SIMD AVX2 dispatch hooks.
cmd
emit_ntt_kat command

Jump to

Keyboard shortcuts

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