Implementations and backends

class vfhe.arith.Spec(implementation: str, backend: str, parent_cls: type, element_cls: type | None = None, vector_cls: type | None = None, capabilities: Capability = <Capability.CORE: 1>, constraints: Constraints = <factory>, rank: int = 0)

One registered (implementation, backend) pair and what it provides.

parent_cls is the class the resolver instantiates; element_cls is the type of its elements, or None where an implementation has no separate element type. vector_cls is the type holding many elements in one buffer, or None where the implementation offers no such type – its presence is what tells a caller vectors are available at all.

rank: int = 0

Preference among backends of one implementation, best first; the resolver picks the lowest rank whose constraints the parameters meet.

has(capability: Capability) → bool

Whether every flag in capability is present.

class vfhe.arith.Domain(*values)

Which representation an element currently holds.

Every implementation offers two, and an element is always in one of them (or EMPTY, meaning allocated but never written):

CANONICAL

A canonical form: one representative per ring element. Equality, hashing and serialization are only defined here, because only here does equal value imply equal bits.

MUL

The image under an injective ring homomorphism chosen so that multiplication is cheap - the NTT/CRT decomposition for RNS, evaluation form for an FFT backend, and Montgomery-like forms in general. Need not be canonical.

The two may coincide (see Capability.DOMAINS_COINCIDE), in which case conversion is the identity and every element reports CANONICAL.

class vfhe.arith.Capability(*values)

Which groups of operations an implementation supports.

Bit positions are the contract shared with the C method table, so a value can be handed across the boundary unchanged. A caller that wants to fail early tests the flag; one that calls anyway gets the implementation’s not-implemented answer for that slot.

CORE = 1

Allocate, copy, zero, add, sub, negate, scale, multiply, convert domains. Every implementation has these.

QUOTIENT_POLY_RING = 2

The element is a polynomial in a quotient ring, so Galois automorphisms and monomial multiplication are meaningful. Absent for a field, whose elements are scalars.

TOWER = 4

round/floor division, modulus reduction, lifting to a larger modulus.

Type:

Movement within a modulus tower

SAMPLING = 8

uniform, gaussian, and exceptional-set elements.

Type:

Sampling

EXACT = 16

Results are exact. Absent means an approximate representation whose error the caller must account for.

DOMAINS_COINCIDE = 32

The canonical and mul domains are the same representation, so domain conversion is free and the domain flag never changes.

class vfhe.arith.Constraints(max_prime_bits: int | None = None, min_transform_length: int | None = None)

Limits a backend places on the parameters of a parent built on it.

The resolver applies these before parameters are chosen, so a backend that can only hold narrow primes gets narrow primes rather than an error after the fact. None means the backend does not constrain that parameter.

max_prime_bits: int | None = None

Largest prime bit width the backend’s storage and kernels accept.

min_transform_length: int | None = None

Smallest transform length the backend handles; shorter parents must use another backend.

accepts_prime_bits(bits: int) → bool

Whether a prime of this bit width fits the backend.

vfhe.arith.resolve(implementation: str | None = None, backend: str | None = None, prime_bits: int | None = None, requires: Capability | None = None) → Spec

The spec matching a caller’s request, choosing what was left open.

implementation defaults to the only registered one when there is no ambiguity, so a caller that does not care never has to name it. backend defaults to the best-ranked backend of that implementation whose constraints prime_bits satisfies. requires filters to specs carrying every named capability.

Raises LookupError naming the available choices when nothing matches.

vfhe.arith.registered() → dict[tuple[str, str], Spec]

Every registered spec, keyed by (implementation, backend).

vfhe.arith.register_conversion(source: tuple[str, str], target: tuple[str, str], convert: Callable, implicit: bool = False, cost: float = 1.0) → Conversion

Declare how to move a value from source to target.

vfhe.arith.implementations() → list[str]

Names of every registered implementation, sorted.

vfhe.arith.backends(implementation: str) → list[str]

Backend names registered for implementation, best-ranked first.

vfhe.arith.reset_state() → None

Empty every registered implementation’s caches.