vfhe.polycom

Polynomial commitments for the oracles of vfhe.piop: Basefold over a foldable Reed-Solomon code, FoldableRS over the ring or FieldFoldableRS over a field, with the evaluation protocols BasefoldEval and BatchFoldEval.

scheme.commit(f) returns a BasefoldCommitment, the root a statement carries, and a BasefoldOpening, the prover’s data. BasefoldEval, registered for Relation_Eval, proves an evaluation claim by a sumcheck interleaved with folds of the codeword, then answers queries at query_positions. Fiat-Shamir comes from IOP(fiat_shamir=True). Under development: the API changes between releases.

class vfhe.polycom.Basefold(code: FoldableRS | FieldFoldableRS, schedule: list[int] | None = None)

The basefold PCS [ZCF24, Def. 8] over the domain of its code — R_q (FoldableRS) or a field (FieldFoldableRS): this object is the scheme (its code is the Setup output); commit / open below, and Eval is the BasefoldEval protocol, registered for Relation_Eval.

commit returns the committed oracle (piop.CommittedOracle: the commitment, the variables and this scheme, what a statement names) and the opening. openings is the prover-side map (commitment -> BasefoldOpening) BasefoldEval falls back to, filled by the commits made without a prover; a verifier’s copy of a scheme simply never holds one, and the in-process sharing of this object is the same convention that lets a verifier hold table oracles it may only query.

schedule groups the code’s d folds into layers: schedule[i] folds make layer i, and only the codeword each layer starts from is committed, by a Merkle tree whose leaves are its fibers of 2^schedule[i] adjacent symbols (the symbols those folds reduce to one). The counts are positive and sum to d; the default [1] * d commits every folded codeword over pair leaves. The commitment is the first layer’s tree, so it depends on schedule[0].

levels

The code level each layer starts from, top first, then the level the last layer ends at (0).

property domain

The code’s ring or field, where the committed tables’ coefficients live.

digest() → bytes

A 32-byte digest of the scheme’s public parameters: the code (FoldableRS.digest / FieldFoldableRS.digest) and the layer schedule.

merkle_commit(word: Any, folds: int | None = None) → Merkle

The Merkle tree commitment to a codeword — the vector-commitment step of the scheme, one leaf per fiber of 2^folds adjacent symbols (digested by the code, leaf_digests), folds the first layer’s count by default; its root is what travels (as the polynomial commitment or as a round message).

The digests reach the tree packed, which is why nothing here builds a Python object per leaf. A codeword has a leaf per pair, so an object per leaf would be one on the way in and another on the way back out when the builder rejoined them – at a large codeword more than either the hashing or the tree costs, and paid once per fold level as well as once per commit.

commit(f: MLE, prover: Prover | None = None) → tuple[CommittedOracle, BasefoldOpening]

Commit to f: encode, build the Merkle tree, return the committed oracle – the root as the commitment, with the variables and this scheme – and the codeword plus tree as the prover’s opening data.

Both costs are paid here, once. With prover given, the opening is installed on it and nowhere else: witnesses[commitment] = opening is what the evaluation protocol proves against, and witnesses[oracle] = f is the table a sumcheck prover folds when the oracle is one of its factors (piop.resolve_table); both live as long as that prover. Without one, the opening is recorded on the scheme (openings), where the evaluation protocol of any prover finds it, for as long as the scheme lives – or store it yourself on whichever provers will run. Mutating the table after the commit breaks the binding.

open(commitment: BasefoldCommitment, f) → bool

The Open algorithm [ZCF24, Def. 8]: does commitment open to f? Re-encode, rebuild the tree, compare roots — so it is exactly as binding as the hash is collision-resistant.

class vfhe.polycom.BasefoldCommitment(variables: list, root: bytes)

A commitment to a multilinear polynomial: public instance data, created once by Basefold.commit and referenced by any number of Relation_Eval statements (across IOP runs — it never touches a transcript).

root is the Merkle root of the level-d codeword’s pair leaves, and variables the committed variables, whose order is the canonical order of evaluation points. Nothing else: the codeword itself is prover data (BasefoldOpening).

digest() → bytes

The piop.element_digest hook: what a Fiat-Shamir chain absorbs when this commitment sits in a statement (the root already binds the codeword; the variables fix the point order).

class vfhe.polycom.BasefoldEval(scheme: Basefold, rep: int = 32)

Basefold’s Eval protocol: discharges Relation_Eval statements against the scheme’s code, replacing the terminal oracle query.

Register with iop.register(Relation_Eval, BasefoldEval(scheme)); the registry keys it by scheme, so it receives the evaluation claims on the oracles committed under that scheme (CommittedOracle`s, as `commit returns them) and proves each against the commitment its oracle carries. A claim on any other oracle is a TypeError. This is [CHMMVW20]’s “queries become opening claims” done in place — no separate relation, since the commitment is just the oracle’s other form [ZCF24, §4].

It is a bundle protocol, and its bundle is every pending claim on the oracles committed under its scheme (bundle_key): the driver hands them over once no other statement can produce another. A single claim is opened by one basefold run; several, at any points and on any of the scheme’s commitments, are opened together by one sumcheck to a common point and one basefold run on a random linear combination (_prove_batch, [CCCFGS26, §7, Figs. 3-4]).

The prover’s witness is the BasefoldOpening, from prover.witnesses[commitment] (falling back to the scheme’s own records, which commit() filled); the verifier works from the root alone. The code’s dimension k_d must equal 2^num_vars of the commitment, and its depth d must not exceed num_vars (kappa = num_vars - d variables remain in the clear base table).

reduces_from

alias of Relation_Eval

domain

The ring or field of the committed tables’ coefficients, where every challenge of this protocol is drawn, whatever the IOP’s domain is.

parameters() → tuple

The public parameters of this protocol, in a fixed order; empty by default.

soundness_error(statement: Statement, domain, gamma: float, delta: float, claims: int = 1, polynomials: int = 1) → float | None

The information-theoretic part, 2d / (gamma^3 |A|) + (1 - delta + gamma * d)^rep [ZCF24], per RNS-prime component (|A| = the residue field size). gamma and delta are the caller’s, subject to the theorem’s conditions (polycom.md): delta is the proximity parameter, which must satisfy delta < J_gamma(J_gamma(Delta)) and 3 delta - d gamma < Delta for the code’s relative distance Delta (self.code.relative_distance()); basefold_parameters gives the largest one. None if |A| is unknown.

For a bundle of claims evaluation claims on polynomials distinct polynomials, opened together, the batching adds (2l + claims - 1) / |A| for the common-point sumcheck when claims > 1 [CCCFGS26, §7.1] (l = the number of variables; the bound holds whether or not the points coincide) and (polynomials - 1) / |A| for the random linear combination [CCCFGS26, §7.2, Lemma 5].

With Merkle commitments this is no longer the whole story: binding is computational, so the argument’s error also carries the hash’s collision term ([ZCF24, Thm. 4] reduces one to the other). That term is a property of BLAKE3 and the security parameter, not of these protocol parameters, so it is deliberately not folded in here.

The bound is stated for [ZCF24]’s protocol, which commits every folded codeword: the default schedule. Under a schedule that groups several folds into a layer, the codewords inside a layer are not committed; the same expression is returned, and that case is not covered by the cited theorem.

query_positions(seed: bytes) → tuple[int, ...]

The rep spot-check positions derived from a bit-string challenge: leaf indices of the commitment’s tree, in [0, n_d / 2^schedule[0]) – pair indices at level d under the default schedule.

This scheme’s geometry handed to polycom.query_positions, which is where the sampler and its rejection rule are: a leaf index is a position of the codeword the first layer ends at, so a walk from it consumes that level’s levels[1] index bits to reach the level-0 codeword, and the positions are rejection-sampled to land there pairwise distinct. The constructor’s rep < n0 guarantees termination.

Deterministic in the seed, so both parties derive the same positions from the published challenge — the prover to answer, the verifier to check.

bundle_key(oracle) → Any

Claims on every oracle committed under this protocol’s scheme form one bundle, which prove opens together; any other oracle keeps a bundle of its own.

class vfhe.polycom.BasefoldMessages(schemes: list[Basefold], sent: Iterable[int] = ())

Oracle messages compiled with basefold: a table a protocol sends is committed, and the message is the commitment’s Merkle root.

Built from one Basefold scheme per table size, since a scheme’s code has one dimension; send commits under the scheme whose k_d is the table’s size and installs the opening on the prover (Basefold.commit), and receive reads the root and names the CommittedOracle. The evaluation claims on those oracles are discharged by each scheme’s BasefoldEval, which the caller registers for Relation_Eval.

Tables of 2^n entries for n in sent are not committed: their message is the entries, as in OracleMessages, and both parties name the table by a public TableOracle, since the verifier holds it whole; an evaluation claim on it is then decided by the verifier’s own evaluation. basefold_messages sends the sizes that have no basefold parameters for a target soundness this way.

sent

The table sizes, as variable counts, whose entries are the message.

classmethod from_code(code: FoldableRS | FieldFoldableRS, num_vars: list[int]) → BasefoldMessages

One scheme per table size 2^n, for n in num_vars, each under the default schedule over code truncated to the depth that size needs (truncated): one code’s tables serve every size. Each n is at least log2(k0) + 1 and at most log2(k_d).

digest() → bytes

The schemes by table size and the sizes sent in the clear.

send(prover: Prover, label: str, table: MLE) → Oracle

Write table under label and return the prover’s oracle to it.

async receive(verifier: Verifier, label: str, variables: list, field: Any) → Oracle

Read the table sent under label, a table over field with variables, and return the verifier’s oracle to it.

class vfhe.polycom.BasefoldOpening(polynomial: MLE, word: Any, tree: Merkle)

The prover’s data for one commitment — the witness of the evaluation claim, stored as prover.witnesses[commitment].

Holds the polynomial plus what commit already paid for and every evaluation proof reuses: the level-d codeword and its Merkle tree. The shape mirrors the commit -> (Commitment, ProverData) split of PCS implementations (arkworks’ poly-commit, plonky3’s Pcs).

class vfhe.polycom.BasefoldParameters(k0: int, c: int, d: int, rep: int, schedule: tuple[int, ...], distance: float, gamma: float, delta: float, soundness_error: float, proof_bits: float)

What basefold_parameters chose.

FieldFoldableRS(field, k0, c, d) or FoldableRS(ring, k0, c, d) is the code, Basefold(code, schedule) the scheme and BasefoldEval(scheme, rep) the evaluation protocol. distance is the code’s relative_distance, gamma and delta the arguments of BasefoldEval.soundness_error that give soundness_error, and proof_bits the size estimate the choice minimized.

property n0: int

The length of the base codeword.

property num_vars: int

The variables of the committed polynomials.

class vfhe.polycom.BatchFoldEval(scheme: Basefold, rep: int = 32)

BatchFold’s Eval protocol: discharges Relation_Eval statements on oracles committed under a Basefold scheme over R_q (a FoldableRS code, with its twists in either field), by basefold runs over the residue fields of R_q (module docstring).

Register with iop.register(Relation_Eval, BatchFoldEval(scheme)) in place of BasefoldEval: the commitments are the same, so a polynomial committed once may be opened by either. As with BasefoldEval, the bundle is every pending claim on the oracles committed under the scheme, and a single claim is a bundle of one.

rep is the number of query positions, shared by the runs at every prime.

slots

k = N / split_degree, the number of CRT components at each prime.

fields: list[Field]

Per prime p_i, the field F_i the components at p_i lie in.

residue_protocols

Per prime p_i, basefold over the residue code at p_i with this scheme’s schedule. These run the per-prime folds and are never registered with an IOP.

soundness_error(statement: Statement, domain, gamma: float, delta: float, claims: int = 1, polynomials: int = 1) → float | None

The information-theoretic soundness error for a bundle of claims evaluation claims on polynomials distinct polynomials: 2d / (gamma^3 |F|) + (2l + k claims - 1) / |F| + (k polynomials - 1) / |F| + (1 - delta + gamma d)^rep, one prime’s error: the common-point sumcheck [CCCFGS26, §7.1], the random linear combination [CCCFGS26, §7.2, Lemma 5] and basefold over a residue code [ZCF24]. Here |F| = min_i p_i^d is the size of the smallest residue field, which is also the size of the set the shared challenges come from, l the number of variables, k the number of components at a prime, and d the code’s depth (not the split degree). gamma and delta are the caller’s, as in BasefoldEval.soundness_error, with the smallest of the residue codes’ relative distances (min(p.code.relative_distance() for p in residue_protocols)) as the distance Delta that bounds delta. None if a field’s size is unknown.

Why one prime’s error, not L times it. The ring commitment fixes the committed codeword before the protocol, and the statement fixes the claims, so whether the components at prime i are codewords whose polynomials satisfy the component claims is decided, for every i, before any challenge or query is drawn. When the bundle is false, some prime i fails in this way, and the verifier accepts only if that prime’s checks pass, which happens with probability at most that prime’s error. Each prime’s error is bounded by the expression above: its challenges come after the messages they answer and are uniform over a set of size |F| (they are one ring element’s components, so the primes’ challenges are correlated, which no prime’s argument looks at), and its query positions are uniform and drawn after every oracle they check is committed. The same holds round by round, which Fiat-Shamir needs: the composition is doomed when that prime’s run is ([BCFW25] proves it for parallel copies of an interactive oracle reduction). The ring sumcheck over R_q, which through the CRT is the per-prime sumchecks on one shared challenge, likewise has no factor for the number of primes [CCCFGS25, Thm. 2.6]. [CCCFGS26, §7] states its bound with a factor L + 1, for its L + 1 primes run independently.

The paper commits to the k*L component polynomials separately and opens each prime with its own query positions and challenges; here the ring commitments serve every prime and the primes share both, as well as one tree per later layer. A ring leaf holds every component at its position, and a later layer’s leaf every prime’s fiber at its position, which includes what the paper’s verifier reads there; and every layer’s tree is committed before the queries are drawn.

Two terms rest on assumptions. The fold term 2d / (gamma^3 |F|) is [ZCF24]’s for fold challenges uniform over the whole field; here they are uniform over a subset of size |F|, which keeps the bound if it comes from counting the challenges that fold badly (the ring BasefoldEval makes the same assumption). And as in BasefoldEval.soundness_error, the bound is stated for the default schedule, and the hash’s collision term is not included.

class vfhe.polycom.FieldFoldableRS(field: Field, k0: int, c: int, d: int, *, instantiation: str = 'general', seed: bytes = b'vfhe.polycom foldable code', twist_field: str = 'field', root_field: str = 'prime')

A depth-d foldable code over field, with base dimension k0 and inverse rate c: level l encodes k0 * 2^l field elements into n_l = c * k0 * 2^l. encode infers the level from the message length; fold / fold_at implement the verifier-checkable fold that takes the level-l codeword of P to the level-(l-1) codeword of P_even + r * P_odd (see code.FoldableRS for the construction and the tables).

instantiation selects the code (the module docstring describes both):

  • "general" (the default): seeded twists above a Reed-Solomon base code, [ZCF24]’s construction. Any field, any length; the base encoder is a transform when the field has a 2 n0-th root of unity and a Vandermonde product otherwise.

  • "rs": every level a Reed-Solomon code on roots of unity, encoded by one transform per level. Needs 2 n_d | p - 1, i.e. log2(n_d) + 1 <= field.two_adicity; faster, and with the exact distance relative_distance reports for it.

seed is the public parameter every table of the general code is derived from – two parties building the code from the same arguments hold the same code. It is ignored by the "rs" instantiation.

twist_field says where the general code’s twists are drawn from: "field" (the default), the whole field, or "prime", its prime subfield F_p. Prime-subfield twists make every lift and fold multiply plane-wise – d prime-field products per element in place of a full extension product – at the price of the distance bound running on log2 p bits rather than log2 p^d, which is more queries for the same soundness. Over a prime field the two coincide, and the "rs" instantiation’s twists are roots of unity in F_p whatever is asked.

root_field says where the general code’s base transform may find its 2 n0-th root of unity: "prime" (the default), only in F_p, so a field whose p - 1 lacks the 2-adicity gets the Vandermonde base; or "field", in F_p where it has one and in the extension otherwise, for a transform whose butterflies are extension products rather than plane-wise products in F_p. Both encode the same code on their points. It plays no part over a prime field or in the "rs" instantiation, whose transforms always have their roots in F_p.

classmethod residue_code(code: FoldableRS, index: int) → FieldFoldableRS

The code that code, a foldable code over R_q, induces on the residue field R_q/P_0 at the prime p = code.ring.primes[index] (RNSRing.residue_field): the code whose codewords are the CRT components at p (RNSRing.crt_components) of code’s codewords.

A ring code with twists in the residue fields (twist_field = "field") is built from one such code per prime, and that code is returned.

A ring code with integer twists applies the same F_p-linear maps to every coordinate of its symbols, since its twists and base points at p are integers mod p, and the CRT components at p are ring homomorphisms that fix F_p. So component s of code.encode(m) is the encoding of component s of m under the same tables, and the code returned has code’s instantiation and parameters, its twists and base points at p as tables over the prime subfield (twist_field = "prime"), and a fold that commutes with code.fold component by component. Its base level is encoded by the field’s transform when that transform’s root in F_p is the ring transform’s, and by a Vandermonde product on the same points otherwise. For the "rs" instantiation every level’s transform must have the ring code’s root; a ValueError is raised when it does not.

digest() → bytes

A 32-byte digest of the code’s public parameters: the field, k0, c, d, the instantiation, the seed, the twist field and the root field. Two codes with equal digests encode, fold and hash identically.

truncated(depth: int) → FieldFoldableRS

The depth-depth code with this code’s field, k0, c, seed and options: its levels 0 to depth.

A foldable code’s level l is defined by the levels below it and level l’s twists [ZCF24, Def. 5], and here a level’s twists are drawn from the seed, the level and its length alone, as the base code is from the seed and n0. So the code of a smaller depth is this one’s first levels, and it is returned sharing their tables and transforms rather than sampling them again; it encodes, folds and hashes exactly as the code built with depth does.

relative_distance(security_bits: int = 128, bound: str = 'cccfgs26') → float

A lower bound on the relative minimum distance at every level: the distance Delta that bounds the proximity parameter delta of BasefoldEval.soundness_error. It is a property of the code rather than of the protocol.

For the "rs" instantiation every level is a Reed-Solomon code on distinct points, so level l has distance exactly 1 - k_l/n_l + 1/n_l; the rate is 1/c throughout, so the longest codeword’s value is the bound and the other arguments play no part.

For the general code it is code.foldable_relative_distance – [CCCFGS26]’s bound unless bound names [ZCF24]’s – which holds with probability at least 1 - d * 2^-security_bits over the twists; the field size in it is the order of twists_domain, the field the twists are drawn from.

property twists: list[list[FieldElement]]

T_l[i], the even-position twist of the pair i folding level l+1 to l, per level, as elements of twists_domain; x_i = psi_{l+1}^(2 brv(i) + 1) for the "rs" instantiation.

property twists_odd: list[list[FieldElement]]

T'_l[i] = -T_l[i], the odd-position twist, per level; -x_i for the "rs" instantiation. Derived, not stored.

level_of(message) → int

The code level a message of this length belongs to.

encode(message) → FieldVector

The codeword of a coefficient vector (level inferred from its length). The operand is left untouched.

encode_batch(messages, count: int) → FieldVector

The codewords of count messages of one level, in one pass.

messages holds them interleaved – coefficient i of message c at i * count + c – and the result holds their codewords end to end, message c’s at c * n_l. count is a power of two.

For the general code the interleaved messages are one message of log2(count) more levels, whose base messages are those of all count messages. Encoding it but stopping after the lifts of the messages’ own levels leaves exactly their codewords, in this order. For the "rs" instantiation each message is transformed on its own.

decode(word) → tuple[bool, FieldVector]

(is_codeword, message) for a codeword: the level’s inverse transform plus the degree check that rejects vectors outside the code (the level is inferred from the length).

fold_pair(lo, hi, r, level: int, i: int)

The folded value at position i of a level-level codeword, from that position’s pair alone: (lo, hi) = (word[2i], word[2i + 1]).

This is the form a Merkle verifier uses — it holds one authenticated pair per queried position, never a whole codeword.

fold_pairs(los, his, r, level: int, indices) → list

fold_pair at many positions at once.

los[k], his[k] are the pair at indices[k], and the result is that position’s folded value – exactly what fold_pair returns for it, one entry per position.

A verifier holds one pair per queried position and folds it at every level, so the scalar form costs a handful of element operations per (query, level), each its own crossing into C. Here a level’s pairs are one vector folded by one FieldVector.fold_twisted call, and the table is read with one gather rather than an index at a time.

fold_batch(words, challenges, level: int) → FieldVector

fold of len(challenges) level-level codewords laid end to end, codeword c with challenges[c]; the folded codewords come out end to end in the same order. It is one fold_twisted_blocks call, in which every codeword reads the level’s table. challenges is a vector over the field or a sequence of its elements.

fold_fibers_batch(values, challenges, level: int, indices) → FieldVector

fold_fibers for count codewords at once, at the same positions: values holds count blocks end to end, block c the fibers of codeword c above indices (as fold_fibers takes them), and challenges[k] holds the count challenges of fold k, one per codeword. The result holds, block by block, fold_fibers of each. Each fold is one fold_twisted_blocks call, with the level’s table gathered at the fibers’ pairs.

fold_fibers(values, challenges, level: int, indices) → FieldVector

Fold fibers of a level-level codeword s = len(challenges) times, one challenge per fold in order, down to the one symbol each determines.

The fiber above position j of the codeword s folds below is the 2^s adjacent symbols word[j 2^s : (j + 1) 2^s], since every fold sends pair (2i, 2i + 1) to position i. values holds fibers end to end and indices[k] is the position below fiber k; the result has one symbol per fiber, entry k the value at indices[k] of the level-(level - s) codeword. Each fold is one FieldVector.fold_twisted call over all the fibers, with the level’s table gathered at their pairs’ positions. With one challenge it is fold_pairs on the pairs of values.

fiber_at(word: FieldVector, i: int, size: int)

The size adjacent symbols word[i size : (i + 1) size]: the Merkle leaf i of a tree over size-symbol fibers. A pair is pair_at’s tuple, a larger fiber a vector.

fibers_at(word: FieldVector, positions, size: int) → FieldVector

Gather the fibers of size symbols at fiber indices positions into one vector, end to end in that order: for each j of positions, the symbols of fiber_at(word, j, size).

For example, positions = [2, 0] and size = 2 give the vector [word[4], word[5], word[0], word[1]].

check_fibers(fibers, count: int, size: int) → FieldVector

Check that fibers is count fibers of size symbols end to end, as fibers_at gathers them: a vector of count * size elements of this code’s field. Returns it.

Raises ValueError otherwise.

pair_at(word: FieldVector, i: int) → tuple[FieldElement, FieldElement]

Position i’s pair, (word[2i], word[2i + 1]) — the unit the fold reads, and the Merkle leaf (see leaf_digest).

leaf_digests_of(pairs) → memoryview

The leaf digests of pairs, packed – leaf_digest for a set of pairs rather than one.

The pairs go into one vector and are digested with the adjacent windows leaf_digests uses on a codeword, so entry k is leaf_digest(pairs[k]) and the lane-parallel hashing applies to a handful of pairs as much as to a whole codeword.

pair_leaves(word: FieldVector) → list[tuple]

word as the list of adjacent pairs its Merkle tree commits to.

leaf_digest(pair: tuple) → bytes

The Merkle leaf digest of one pair: the vector digest of the two elements in order, so it equals the matching entry of leaf_digests and binds the pair as an ordered unit.

A verifier recomputes this once per query, where the vector it used to build was most of the cost and none of the hashing.

leaf_digests(word: FieldVector, size: int = 2) → memoryview

The leaf digests of every adjacent pair of word, packed, in one pass over the codeword (FieldVector.hash_elements with adjacent windows). Leaf i is digests[32 * i : 32 * (i + 1)], and equals leaf_digest of pair_at(word, i). With size, the leaves are the fibers of that many symbols instead, fiber_at(word, i, size). Raises ValueError when size does not divide the length of word.

A read-only view of the kernel’s buffer, not a copy of it – so a codeword reaches its root without its digests being duplicated on the way. bytes(...) is how a caller takes a copy.

fold_at(word: FieldVector, r, level: int, i: int)

Position i of the fold of the level-level codeword word with challenge r — the value the folded codeword must hold there.

fold(word: FieldVector, r, level: int) → FieldVector

The full fold of a level-level codeword with challenge r: the level-(level-1) codeword of the r-folded message, as one call (FieldVector.fold_twisted with this level’s 1 / (2 T) table).

class vfhe.polycom.FoldableRS(ring: RNSRing, k0: int, c: int, d: int, *, instantiation: str = 'general', seed: bytes = b'vfhe.polycom foldable code', twist_field: str | None = None)

A depth-d foldable code over ring, with base dimension k0 and inverse rate c: level l encodes k0 * 2^l ring elements into n_l = c * k0 * 2^l. encode infers the level from the message length; fold / fold_at implement the verifier-checkable fold taking the level-l codeword of P to the level-(l-1) codeword of P_even + r * P_odd (the module docstring has the construction and both instantiations).

Everything is per RNS prime: the twists are per-prime integers applied through Polynomial * list, their inverses per-prime modular inverses, and there are no ring inversions anywhere.

The ring’s primes satisfy p = 1 mod 2N/split_degree, so a negacyclic transform of length n exists exactly when n | N/split_degree. The "rs" instantiation needs one of length n_d; the general code uses one of length n0 when it exists and a Vandermonde product otherwise.

seed is the public parameter every table of the general code is derived from; the "rs" instantiation ignores it.

twist_field says where the general code’s twists live: "prime", the per-prime integers above; or "field", the residue fields R_q/P of the ring’s primes (RNSRing.residue_field, F_(p^d) with d the split degree). Left as None, it is "field" when the ring’s split degree is above 1 and "prime" otherwise, where the residue fields are the F_p and the two give the same distance. With "field" the code is defined by one code per prime – FieldFoldableRS over that residue field with full-field twists, its own seeded tables and base, built from seed and the prime – and a codeword of R_q is the element-wise CRT lift of their codewords: its CRT components at a prime (RNSRing.crt_components) are that prime’s code’s codewords of the message’s components. Encoding and folding run in the residue fields, so a twist multiply costs an F_(p^d) product, d F_p products per coefficient where an integer twist costs one; the distance bound runs on log2 p^d bits instead of log2 p. Either way the base code’s transform has its root in F_p when the primes carry one of order 2 n0, i.e. when n0 <= N/d. The "rs" instantiation’s twists are roots of unity in F_p whatever is asked.

roots: list[list[int]]

one row per level for the "rs" instantiation, the base transform’s alone for the general code (empty when its base is a Vandermonde product).

Type:

The roots of the transforms in use, [level][prime]

residue_codes: list[FieldFoldableRS]

With full-field twists, the code over each prime’s residue field that defines this one (in ring.primes order); empty otherwise.

property twists_odd: list[list[list[int]]]

T'_l[j] = -T_l[j] per level, position and prime – the odd-position twist. Derived from twists, not stored.

digest() → bytes

A 32-byte digest of the code’s public parameters: the ring, k0, c, d, the instantiation, the seed and the twist field. Two codes with equal digests encode, fold and hash identically.

truncated(depth: int) → FoldableRS

The depth-depth code with this code’s ring, k0, c, seed and options: its levels 0 to depth.

A foldable code’s level l is defined by the levels below it and level l’s twists [ZCF24, Def. 5], and here a level’s twists are drawn from the seed, the level and its length alone, as the base code is from the seed and n0 (and a transform’s root from its length). So the code of a smaller depth is this one’s first levels, and it is returned sharing their tables, plans and residue codes rather than building them again; it encodes, folds and hashes exactly as the code built with depth does.

relative_distance(security_bits: int = 128, bound: str = 'cccfgs26') → float

A lower bound on the relative minimum distance at every level, per RNS-prime component: the distance Delta that bounds the proximity parameter delta of BasefoldEval.soundness_error. It is a property of the code rather than of the protocol.

For the "rs" instantiation every level is a Reed-Solomon code on distinct points, so level l has distance exactly 1 - k_l/n_l + 1/n_l; the rate is 1/c throughout, so the longest codeword’s value is the bound and the other arguments play no part.

For the general code it is foldable_relative_distance at the smallest prime – [CCCFGS26]’s bound unless bound names [ZCF24]’s – which holds with probability at least 1 - d * 2^-security_bits over the twists.

level_of(message: list) → int

The code level a message of this length belongs to.

encode(message: list) → list

The codeword of a coefficient vector (level inferred from its length).

decode(word: list) → tuple[bool, list]

(is_codeword, message) for a codeword: the encoding inverted, plus the check that rejects vectors outside the code (the level is inferred from the length).

fold_pair(lo: RNSPolynomial, hi: RNSPolynomial, r: RNSPolynomial, level: int, i: int) → RNSPolynomial

The folded value at position i of a level-level codeword, from that position’s pair alone: (lo, hi) = (word[2i], word[2i + 1]).

This is the form a Merkle verifier uses — it holds one authenticated pair per queried position, never a whole codeword.

fold_pairs(los, his, r, level: int, indices) → list

fold_pair at many positions at once; see FieldFoldableRS, which has a vectorised one. Over a ring the entries are polynomials with their own arithmetic, so this is the loop a caller would write – the uniform name is what lets a verifier fold a level without knowing which code family answers.

With full-field twists the pairs are folded component by component: at each prime, component s of every pair by that prime’s code with component s of r, and the results lifted back.

fold_fibers(values, challenges, level: int, indices) → list

Fold fibers of a level-level codeword s = len(challenges) times, one challenge per fold in order, down to the one symbol each determines; see FieldFoldableRS, which has a vectorised one. Here each fold is fold_pairs over the pairs of every fiber.

values holds fibers of 2^s adjacent symbols end to end, and indices[k] is the position below fiber k in the codeword s folds below; entry k of the result is the value there.

fiber_at(word: list, i: int, size: int) → tuple[RNSPolynomial, ...]

The size adjacent symbols word[i size : (i + 1) size]: the Merkle leaf i of a tree over size-symbol fibers. A pair is pair_at’s tuple.

fibers_at(word: list, positions, size: int) → list[RNSPolynomial]

Gather the fibers of size symbols at fiber indices positions, end to end in that order: for each j of positions, the symbols of fiber_at(word, j, size). The entries are word’s own elements, not copies.

For example, positions = [2, 0] and size = 2 give [word[4], word[5], word[0], word[1]].

check_fibers(fibers, count: int, size: int) → list[RNSPolynomial]

Check that fibers is count fibers of size symbols end to end, as fibers_at gathers them: a list or tuple of count * size elements of this code’s ring. Returns them as a list.

Raises ValueError otherwise.

pair_at(word: list, i: int) → tuple[RNSPolynomial, RNSPolynomial]

Position i’s pair, (word[2i], word[2i + 1]) — the unit the fold reads, and the Merkle leaf (see pair_leaves).

leaf_digests_of(pairs: list[tuple[RNSPolynomial, RNSPolynomial]]) → bytes | memoryview

The leaf digests of pairs, packed: entry k is leaf_digest(pairs[k]). The pairs are digested together, as leaf_digests digests a codeword’s.

pair_leaves(word: list) → list[tuple[RNSPolynomial, RNSPolynomial]]

word as the leaf vector its Merkle tree commits to: one leaf per adjacent pair, so a single path authenticates both operands of a fold check ([ZCF24, Remark 9]’s packed leaves).

leaf_digest(pair: tuple[RNSPolynomial, RNSPolynomial]) → bytes

The Merkle leaf digest of one adjacent pair (pair_digest).

leaf_digests(word: list, size: int = 2) → bytes | memoryview

The leaf digests of every adjacent pair of word, packed: leaf i is digests[32 * i : 32 * (i + 1)], and equals leaf_digest of pair_at(word, i). With size, the leaves are the fibers of that many symbols instead, fiber_at(word, i, size). Raises ValueError when size does not divide the length of word.

One native call (rs_leaf_digests). The entries are hashed in NTT form, as get_hash hashes them, and are left in the form they are in. The result is a read-only view of the kernel’s buffer.

fold_at(word: list, r: RNSPolynomial, level: int, i: int) → RNSPolynomial

Position i of the fold of the level-level codeword word with challenge r — the value the folded codeword must hold there. The pair is adjacent: (word[2i], word[2i + 1]).

fold(word: list, r: RNSPolynomial, level: int) → list

The full fold of a level-level codeword with challenge r (the level-(level-1) codeword of the r-folded message). With full-field twists, at each prime every component is folded by that prime’s code with the same component of r (FieldFoldableRS.fold_batch), and the results lifted back.

class vfhe.polycom.LayerAnswers(leaves: tuple, paths: bytes)

The answers of one committed layer to a run’s query positions: the leaves at those positions and their authentication paths.

A leaf is a fiber of adjacent symbols of a codeword (fiber_at). leaves holds one word per codeword under the layer’s tree, in the codeword’s own form (a FieldVector for a field code, a list of ring elements for a ring code): the queried fibers end to end, in query order (fibers_at). A tree over one codeword has one word; a tree whose leaf joins the leaves of several codewords has one per codeword. paths holds the leaves’ authentication paths end to end, in query order, as Merkle.open_many writes them.

For example, for queries at leaf indices [5, 2] of a codeword w with fibers of 2 symbols, the word is [w[10], w[11], w[4], w[5]], and paths is the path of leaf 5 followed by the path of leaf 2.

Its transcript digest hashes each word whole, and the paths’ bytes once.

digest() → bytes

Return the transcript digest: a tag, one hash per word of leaves, then one hash of the paths’ bytes. leaves other than a tuple, which no honest prover sends, is digested whole as any other message is.

vfhe.polycom.basefold_messages(domain: Field | RNSRing, sizes: Iterable[int], security_bits: int = 128, *, twist_field: str | None = None, **options) → tuple[BasefoldMessages, list[BasefoldEval]]

Oracle messages for tables of 2^n entries over domain, for n in sizes, at soundness error 2^-security_bits, and the evaluation protocols of their schemes, which the caller registers for Relation_Eval.

Each size gets the scheme and the query count basefold_parameters chooses for it (with twist_field and options passed on), over the general foldable code. The sizes below the smallest one that has parameters are sent as their entries (BasefoldMessages sent). An opening needs fewer queries than its base codeword has positions, which small tables do not allow.

Raises ValueError when no size has parameters, giving basefold_parameters’ reason for the largest, and when a size above one that has parameters has none.

vfhe.polycom.basefold_parameters(domain: Field | RNSRing, num_vars: int, security_bits: int = 128, *, claims: int = 1, polynomials: int = 1, twist_field: str | None = None, bound: str = 'cccfgs26', max_k0: int = 256, inverse_rates: tuple[int, ...] = (2, 4), digest_bits: int = 256) → BasefoldParameters

The basefold parameters for opening num_vars-variate multilinear polynomials over domain – a field, or R_q as an RNSRing – with soundness error at most 2^-security_bits, by the general foldable code [ZCF24] and the default schedule, one fold per committed codeword, which is what the soundness theorem covers.

The search is the one of the parameter estimator of [CCCFGS26], run on the library’s bounds. For every base dimension k0 = 2^i <= max_k0 that leaves d = num_vars - i >= 1 folds, and every inverse rate c in inverse_rates:

  • Delta is foldable_relative_distance(k0, c, d, ...) with bound, the value relative_distance reports on the code these parameters build; its field is the one the twists are drawn from, chosen by twist_field as the code classes do (their own default when None: the whole field for FieldFoldableRS, and for FoldableRS the residue fields F_(p^d) on a split ring and F_p otherwise);

  • gamma = gamma_0 + g for every g in a half-decade grid from 10^-2 to 10^-7.5, where gamma_0 makes 2d / (gamma^3 |A|) the whole budget left by the batching terms;

  • delta = (1 - 2^-20) min((Delta + d gamma) / 3, J_gamma(J_gamma(Delta))), just below the supremum the theorem admits, so that both of its conditions hold strictly;

  • rep is the fewest queries for which basefold_soundness_error, the expression BasefoldEval.soundness_error reports, is at most 2^-security_bits, and it must be below n0 = c k0, as BasefoldEval requires.

The candidate with the smallest proof_bits is returned: the round messages, the roots, the base table and the queries’ pairs and paths of one opening, with elements of log2 |F| bits over a field and N log2 q over R_q, and digests of digest_bits. |A| is the domain’s exceptional_set_size, the set the challenges come from.

claims and polynomials describe a bundle opened together, as soundness_error takes them. They enter the bound only through its batching terms, (2 num_vars + claims - 1) / |A| when claims > 1 and (polynomials - 1) / |A|: the parameters chosen for one claim, opened as a bundle, have the error reported plus those terms. max_k0 bounds the base table the verifier re-encodes in full; with inverse_rates, its default is the estimator’s search space.

Raises ValueError when no candidate reaches the target, which is always the case when |A| is below about 2^security_bits: the first term of the bound then exceeds the target for every gamma < 1.

vfhe.polycom.batch_inverse(values: list[int], p: int) → list[int]

The inverses modulo the prime p of every entry of values, by Montgomery’s trick: one exponentiation and three multiplications per entry in place of an exponentiation each. Raises ValueError on a zero.

vfhe.polycom.bit_reverse(i: int, bits: int) → int

i with its low bits bits reversed — the index permutation ntt_forward (CT_NR) leaves in its output.

Public because it is not this code family’s: any code built on that transform needs it to say which evaluation point a position holds, and a second one carrying its own copy is a second place for the convention to drift from arith’s.

vfhe.polycom.bit_reverse_permutation(bits: int) → list[int]

[bit_reverse(i, bits) for i in range(1 << bits)], in one pass.

Each entry follows from the one at half its index – the low bit becomes the high one, and the rest is the shorter reversal already computed – so the whole permutation costs O(n) where calling bit_reverse per index costs O(n log n). That difference is the whole cost of a twist table once the modular exponentiations are gone.

vfhe.polycom.foldable_relative_distance(k0: int, c: int, d: int, field_bits: float, security_bits: int = 128, bound: str = 'cccfgs26') → float

A lower bound on the relative minimum distance of the general foldable code with base dimension k0, inverse rate c, depth d and twists uniform in F^x for a field of 2^field_bits elements, holding with probability at least 1 - d * 2^-security_bits over the twists.

Both bounds have the shape of [ZCF24, Thm. 2]: with t_0 = k0 and n_i = c k0 2^i, level i may add l_i zeros to a codeword’s worst case, t_i = 2 t_{i-1} + l_i, and distance >= 1 - t_d / n_d. They differ in l_i:

"cccfgs26": l_i = (lambda + 2 t_{i-1} (1 + log2(``|F|`` / (``|F|`` - 1)))
                   + 0.585 n_i) / (log2(``|F|`` - 1) - 1)
"zcf24":    l_i = (2 log2(n_{i-1}) + lambda + 2.002 t_{i-1} + 0.6 n_i)
                   / (log2|F| - 1.001)

The default is [CCCFGS26]’s (its appendix on BaseFold, the theorem on the codes’ minimum distance), a tightening of [ZCF24, Thm. 2] for this very code – T' = -T, diag(T) uniform in F^x, an MDS base: it drops the 2 (i-1) log2(n0) term and sharpens 0.6 to 0.585, and it is stated for every field with more than three elements where the [ZCF24] form assumes |F| >= 2^10. It is never smaller, and the difference shows where the field is small relative to the depth (about two percentage points at log2|F| = 61, d = 20). The [ZCF24] bound stays reachable by name, as the one the literature quotes.

t_0 = k0 is the base code’s distance as an MDS code, so the base must be a Reed-Solomon code on distinct points for either bound to apply. The value is clamped at 0: a 0 means the theorem gives nothing at these parameters, not that the code has no distance.

vfhe.polycom.pair_digest(pair: tuple[RNSPolynomial, RNSPolynomial]) → bytes

The Merkle leaf digest of one adjacent pair.

Both ring elements are hashed with their own digest and the two digests hashed together, so the leaf binds the pair as an ordered unit. digest hashes the NTT form and leaves the entry alone, so hashing a codeword never disturbs it.

vfhe.polycom.query_positions(seed: bytes, span: int, shift: int, rep: int) → tuple[int, ...]

rep distinct spot-check positions in [0, span), derived from seed.

Parameters:
  • seed – The published bit-string challenge. The positions are a deterministic function of it, so prover and verifier derive the same ones – the prover to answer them, the verifier to check.

  • span – How many positions the top oracle has. A power of two, so that masking a uniform word is unbiased and no rejection is needed for the range itself.

  • shift – How many low index bits one walk consumes, i.e. how far a position must be shifted right to give the position its walk ends at. 0 makes every position its own endpoint and the rejection below a plain distinctness check.

  • rep – How many positions to draw.

The seed is expanded with BLAKE3 in counter mode (BLAKE3(seed || counter), eight candidates per digest), and a candidate is rejected on its projection candidate >> shift: two queries that meet at the end of their walks run the same final check twice, so sampling with replacement there buys fewer effective repetitions than rep claims while reporting rep. That is a silent loss of soundness, which is why it is rejected rather than tolerated.

Distinct endpoints imply distinct positions all the way up, since a position’s projection at any height extends its endpoint. Termination needs at least rep endpoints to exist, which is checked.