Multilinear extensions

class vfhe.arith.MLE(ring: RNSRing | None = None, variables: list | None = None, evaluations: FieldVector | list | None = None, coefficients: FieldVector | list | None = None, num_vars: int | None = None, field: Field | None = None)

A multilinear extension as a dense table of 2^n coefficients.

Two properties of the table, deliberately not subclasses, because they vary independently:

  • `basis` — MLE_Basis.eval, the hypercube evaluations (pass evaluations=), or MLE_Basis.coeff, the monomial coefficients (pass coefficients=, entry b multiplying prod_{i in bits(b)} x_i, i.e. a multilinear polynomial rather than an extension table). Both bind a variable by folding (lo, hi) pairs; only the fold differs — interpolation lo + r*(hi - lo) in the evaluation basis, Horner c_lo + r*c_hi in the monomial one. to_coefficients() converts.

  • coefficient type — with a ring, entries are vfhe.arith.Polynomial over that Ring and the C kernels (mle_dense_poly_*) do the work; with a field, the table is one vfhe.arith.FieldVector over that Field and every fold is a handful of whole-vector operations; without either, entries are plain Python values (any type with + and *, e.g. ints) folded in Python. The kernels additionally require the evaluation basis, so native_table() / vector_table(), not isinstance, is what protocols gate on.

Variable order is generic: any variable may be bound at any position, in any order (evaluate). _bind dispatches on the variable’s position to the layout implemented most efficiently: adjacent entries for the first (LSB) variable, the two table halves for the last (MSB) one, and stride-computed pairs for anything in between.

table holds the entries (also keeping them alive across C calls) and table_ptr is the array of their handles the kernels take (None without a ring) — the two are always views of the same entries, replaced together by _set_table. Over a field, table is the FieldVector itself (indexable and iterable like the list it replaces).

property num_vars: int

The number of free variables; derived, so it cannot go stale.

classmethod eq(domain, point: list, variables: list | None = None) → MLE

The multilinear equality polynomial eq~(point, .) as a dense table: table[b] = prod_i (point_i * b_i + (1 - point_i) * (1 - b_i)), the chi_w Lagrange basis of [Tha22, section 3.5]. domain is the Ring or Field of the coefficients; point entries are its elements (or ints, lifted to constants); entry i pairs with variable i (the table’s LSB-first order).

classmethod from_uint64(ring: RNSRing, variables: list, values: Buffer, repr: Representation = Representation.coeff) → MLE

A table over ring in the evaluation basis, its n = 2^len(variables) entries read from values in representation repr, one after another, each laid out as RNSPolynomial.from_uint64 reads it: words (b * ell + i) * N to (b * ell + i + 1) * N - 1 are the row of entry b modulo ring.primes[i]. In Representation.coeff word (b * ell + i) * N + j is coefficient j; in Representation.ntt a row holds the N / d slots one after another, as RNSPolynomial.to_uint64 describes.

values is an array('Q') or any C-contiguous buffer of n * ell * N unsigned 64-bit integers, such as a numpy.uint64 array of shape (n, ell, N). Every word must be below its row’s prime; ValueError otherwise. The entries are held in Representation.ntt, as every native table’s; coefficient rows are transformed in the same native call that reads them.

to_uint64(repr: Representation = Representation.coeff) → array

The entries’ rows in representation repr, as one array('Q') laid out as from_uint64 reads it: entry b is words b * ell * N to (b + 1) * ell * N - 1, as RNSPolynomial.to_uint64 gives it. numpy.frombuffer(words, numpy.uint64).reshape(n, ell, N) reads it without copying. Needs a table over an RNS ring; the entries are left as they are.

to_NTT() → None

Put every entry in NTT (RNS) form, the representation the C kernels read; a no-op on a table of plain Python values.

The kernels read coeffs directly and fold the whole table as if it were uniform, so a table holding a mix of representations would give silent garbage; this establishes their precondition instead. Cheap when the table is already normalized: each entry’s to_NTT() is then just a flag check. (Entries no longer become mixed just from being read – arith’s readers convert a copy – but a table can still be assembled from coefficient-form elements.)

to_coefficients() → MLE

This MLE in the monomial basis, index-aligned with the evaluation table (coefficient of prod_{i in bits(b)} x_i at position b; LSB-first variable order). Per variable the butterfly is c_hi = e_hi - e_lo, c_lo = e_lo; self is left untouched.

classmethod linear_combination(tables: Sequence[MLE], coefficients: Sequence) → MLE

The table sum_i coefficients[i] * tables[i], entry by entry.

The tables must share their variables, in the same order, their ring or field and their basis; the result has them too. A coefficient is an element of that ring or field, or an integer. Over a ring, tables and coefficients not in NTT form are converted in place.

evaluate(point: dict | list, in_place: bool = True) → MLE

Bind the variables in point to concrete values; variables not in point stay free. Returns the folded MLE.

constant()

The single value of a fully-evaluated (0-variable) MLE.

digest() → bytes

A 32-byte digest of the table: the chain of its basis, its variables in order and one digest of all its entries.

rename(mapping: dict) → MLE

A copy of this table with its variables relabelled through mapping (old variable -> new variable; variables absent from the mapping keep their name). The same polynomial read under other variable names — how one oracle appears twice in a product, as W(x) and W(y).

class vfhe.arith.SparseMLE(variables: list | None = None, evaluations: dict | None = None, num_vars: int | None = None, ring: RNSRing | None = None, field: Field | None = None)

A sparse map of hypercube evaluations: evaluations[index] = value for the nonzero entries, index packing the variables LSB-first like a dense table’s position. The carrier of wiring predicates and other mostly-zero tables.

Independent of MLE on purpose — it supports the linear operations (add / sub / scale) and evaluation at a full point (evaluate, the sum over the nonzeros of value times the eq~ factors, O(nnz * vars)), but none of the per-variable folding a dense table exists for: binding a variable at a time would need the dense form, so a partial point raises. Defined oracles (virtual.py) bind symbolically and only ever query a constituent at a full point, which is what makes a sparse constituent sufficient there; materialize() gives the dense MLE when a fold is really wanted. Values are non-negative ints or elements of the domain (ring= / field=, or plain Python values without either).

property num_vars: int

The number of free variables; derived, so it cannot go stale.

nonzeros()

The (index, value) pairs, in insertion order.

evaluate(point: dict | list, in_place: bool = False) → SparseMLE

The value at a full point, as a 0-variable SparseMLE (so that constant() reads it, like a folded dense table): sum_{k nonzero} value_k * prod_i (point_i if k_i else 1 - point_i). A partial point raises: this form has no per-variable fold.

constant()

The single value of a fully-evaluated (0-variable) SparseMLE.

restrict(fixed: dict) → SparseMLE

The slice of this table at fixed, a map from some of its variables to the constants 0 or 1: the sparse table over the remaining variables, in their order, whose nonzeros are the entries of this one whose bits at the fixed variables equal the constants. One pass over the nonzeros. A slice, not a partial evaluation: a constant outside {0, 1}, or a variable not of this table, raises.

digest() → bytes

A 32-byte digest of the table: its variables and its nonzero entries in index order (not insertion order).

materialize(*_ignored) → MLE

The dense table with the same evaluations, in the same domain.

class vfhe.arith.MLE_Basis(*values)
class vfhe.arith.MLE_Variable(name: str)

A named variable identifier for MLEs, compared by identity.

Any hashable works as an MLE variable; this class is the default used when only num_vars is given. It is a plain name, unrelated to any protocol-level variable a consumer may also call Variable.

digest() → bytes

A 32-byte digest of the variable, by its name.