BFV

class vfhe.fhe.BFV_Scheme(rings: list[RNSRing] | RNSRing, plaintext_primes: int = 1, module_rank: int = 1, special_primes: int = 0, special_rings: list[RNSRing] | None = None, balanced: bool = True, random: Random | None = None)

BFV, with the plaintext modulus taken from the ciphertext ring’s primes.

The plaintext ring is built from the lowest plaintext_primes primes of the ciphertext ring, so t divides q and Delta = q/t is exact rather than a rounded floor(q/t): encryption is a scaled lift, decryption a round-division, and neither approximates. The level chain drops primes from the top, so the plaintext primes survive every level.

t is a ring prime, so it is NTT-friendly and encode() batches for free – but it cannot be a power of two. Noise grows with t at every multiplication, so prefer a small one: prime_size=[17, ...].

Create a BFV scheme.

rings is either a single RNSRing (the level chain is derived automatically) or an explicit list of per-level rings paired with special_rings when special_primes > 0. See MLWE_Scheme for both modes, and for balanced.

Parameters:

plaintext_primes – How many of the ring’s lowest primes make up the plaintext modulus t.

t

The plaintext modulus.

property parameters: SchemeParameters

What this scheme is, for equality and for matching a stored scheme to a live one.

property n_slots: int

two rows of N/2.

Type:

Number of independent plaintext slots

encode(values: list[int]) → RNSPolynomial

Encodes one integer per slot into a plaintext polynomial.

The result is an element of the plaintext ring, in the coefficient domain. Pass it to encrypt(), or multiply a ciphertext by it directly for the plaintext multiplication.

decode(poly: RNSPolynomial, signed: bool = True) → list[int]

Decodes a plaintext polynomial back into one integer per slot.

signed returns each slot as a representative in (-t/2, t/2] rather than in [0, t).

delta(lvl: int = 0)

The scaling factor q/t at lvl, as scaled_lift takes it.

encrypt(message: RNSPolynomial, key: MLWE_Key, lvl: int = 0) → MLWE

Encrypts a plaintext polynomial, scaled by Delta, under key.

decrypt(ciphertext: MLWE, key: MLWE_Key) → RNSPolynomial

Decrypts a ciphertext back into a plaintext polynomial.

mod_switch(ciphertext: MLWE, lvl: int | None = None) → MLWE

Switches the ciphertext down to level lvl (the next one by default).

The round-division divides the linear decryption and Delta alike, so the message is unchanged and the noise shrinks with the modulus.

rotate(ciphertext: MLWE, k: int, ksk: MLWE_Set | list[MLWE_Set], n_threads: int = 0) → MLWE

Rotates each of the two rows of N/2 slots by k positions.

gen_rotation_key(key: MLWE_Key, k: int, n_threads: int = 0) → MLWE_Set | list[MLWE_Set]

Generates the key-switching key rotate() needs for k.

conjugate(ciphertext: MLWE, ksk: MLWE_Set | list[MLWE_Set], n_threads: int = 0) → MLWE

Swaps the two rows of slots.

gen_conjugation_key(key: MLWE_Key, n_threads: int = 0) → MLWE_Set | list[MLWE_Set]

Generates the key-switching key conjugate() needs.

multiply(in1: MLWE, in2: MLWE, ksk: MLWE_Set | list[MLWE_Set] | None = None) → MLWE

Multiplies two ciphertexts and (optionally) relinearizes.

Two Delta-scaled linear decryptions multiply to a Delta^2-scaled one, so the product is divided by Delta to come back. That division is over the integers, not modulo q: the operands move up into a ring wide enough to hold the product, are divided there, and come back. See mul_rings().

Parameters:
  • in1 – The first ciphertext.

  • in2 – The second ciphertext.

  • ksk – The relinearization key, from gen_rlk. If None, relinearization is skipped and the extended product (rank extended_rank) is returned; feed it to relinearize().

mul_rings(lvl: int) → tuple[RNSRing, RNSRing]

The two rings multiply() works in at lvl.

The first is level lvl’s ring plus auxiliary primes: the tensor product is computed there, wide enough not to wrap. The second is the first without Delta’s primes, where the round-division by Delta leaves the result.

Both follow from one bound. A product coefficient stays under N * (ell * q)^2 – ell * q rather than q because _lift_ciphertext lifts through the fast base extension. Auxiliary primes are added until the first modulus is four times that, which covers both requirements: the product stays inside half of it, so a coefficient’s centered representative is the integer itself; and the divided value stays inside a quarter of the second modulus, the margin RNSPolynomial.convert_base needs.