CKKS

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

Create a CKKS 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 – e.g. non-nested levels for rational rescaling. See MLWE_Scheme for both modes, and for balanced and random.

property parameters: SchemeParameters

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

encode(values: Sequence[complex | float], *, ring: RNSRing | None = None, scale: float | None = None) → RNSPolynomial

Encodes complex values into a plaintext polynomial.

len(values) must divide N/2; fewer values are repeated to fill the slots (sparse packing), and decode(..., slots=len(values)) reads them back. ring is the plaintext’s ring (default: level 0’s) and scale multiplies the values (default: scaling_factor). A contiguous buffer of complex doubles (a numpy complex128 array) is read in place.

encode_batch(values: Sequence[Sequence[complex | float]], *, ring: RNSRing | None = None, scale: float | None = None, n_threads: int = 0) → list[RNSPolynomial]

encode() of each of values, in one native call.

decode(poly: RNSPolynomial, scaling_factor: float | None = None, *, slots: int | None = None) → list[complex]

Decodes a plaintext polynomial into its slot values, divided by scaling_factor (default: the scheme’s).

Returns all N/2 slots, or the first slots (a divisor of N/2) of a sparsely packed message. Exact over any number of primes, and cheapest over few, which is how decrypt() returns plaintexts.

encrypt(message: RNSPolynomial, key: MLWE_Key) → CKKS_Ciphertext

Encrypts a plaintext polynomial message under the given MLWE key.

encrypt_batch(messages: Sequence[RNSPolynomial], key: MLWE_Key, n_threads: int = 0) → list[CKKS_Ciphertext]

encrypt() of each of messages, in one native call.

Every ciphertext has a fresh mask and noise of its own, as from encrypt(), and they do not depend on the thread count (see MLWE_Scheme.sample_batch()). They come back in the NTT domain.

decrypt(ciphertext: MLWE, key: MLWE_Key, *, message_bound: float | None = None, drop: bool = True) → RNSPolynomial

Decrypts a ciphertext into its plaintext polynomial.

To keep decoding cheap, the plaintext is returned over the fewest primes that hold it: the lowest level (a quotient of the ciphertext’s ring) whose modulus exceeds 2 * delta * message_bound. message_bound bounds the slots’ magnitude; by default it is what the smallest level holds at the scheme’s scaling factor, so ordinary ciphertexts drop to that level and those with a larger delta (e.g. unrescaled products) stay higher. A level with less than two bits above the scaling factor cannot hold a message and does not count as the smallest. drop=False keeps every prime.

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

Rotates the slots of the ciphertext by k steps.

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

Generates the rotation key (automorphism key-switching key) for rotation by k slots.

rotation_generator(k: int) → int

The Galois element 5^k mod 2N that rotates the slots by k steps; 5 has order N/2, so k is taken mod N/2.

multiply_plain(ciphertext: CKKS_Ciphertext, plaintext: RNSPolynomial, scale: float | None = None) → CKKS_Ciphertext

Multiplies by a plaintext, without rescaling.

scale is the plaintext’s encoding scale (default: scaling_factor; 1 for an unscaled plaintext such as a monomial). The result’s delta is the ciphertext’s times scale, so several products can be summed before one rescale().

linear_combination(cts: Sequence[CKKS_Ciphertext], coefficients: Sequence[RNSPolynomial], scale: float | None = None) → CKKS_Ciphertext

sum_i coefficients[i] * cts[i], without rescaling.

The ciphertexts must share one delta, and the coefficients must all be encoded at scale (default: scaling_factor).

linear_combinations(cts: Sequence[CKKS_Ciphertext], rows: Sequence[Sequence[RNSPolynomial | None]] | PlaintextMatrix | IntegerMatrix, n_threads: int = 0, *, scale: float | None = None) → list[CKKS_Ciphertext]

One linear_combination() of cts per row of coefficients. None skips a term. With an IntegerMatrix the weights are integers, not encoded at a scale: the outputs keep the ciphertexts’ scaling factor, and scale must be None.

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

Conjugates every slot.

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

Generates the key-switching key conjugate() needs.

rescale(ciphertext: CKKS_Ciphertext, n_threads: int = 0) → CKKS_Ciphertext

Rescale down one level: move the next ring in the chain of rings.

Levels start at 0 and grow as primes are consumed, so a rescale increments lvl. The destination ring is taken from self.rings. When the next level’s ring is not a quotient of the current one, this is rational_rescale().

Each component c becomes round(c / q_drop) modulo the next level’s modulus, where q_drop is the product of the primes the next level drops and the rounding is to the nearest integer. In other words, the result is (c - r) / q_drop, with r the centered residue of c modulo q_drop, so |r| <= (q_drop - 1)/2. For example, with q_drop = 7, c = 10 becomes 1 and c = 11 becomes 2. The primes are divided one at a time, each after adding half of it; for odd primes this gives the same rounding whatever their order.

rescale_batch(cts: Sequence[CKKS_Ciphertext], n_threads: int = 0) → list[CKKS_Ciphertext]

rescale() of each ciphertext (distinct, at one level).

In place like rescale(), except on non-nested chains, where rational_rescale_batch() returns new ciphertexts.

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

MLWE_Scheme.multiply() (not rescaled), with delta set to the product of the operands’.

multiply_batch(lhs: Sequence[CKKS_Ciphertext], rhs: Sequence[MLWE], ksk: MLWE_Set | list[MLWE_Set] | None = None, n_threads: int = 0, *, lvl: int | None = None) → list[CKKS_Ciphertext]

lhs[i] * rhs[i] for every i, relinearized with ksk.

Not rescaled, unless lvl is given: each product is then rescaled to that level, whose ring must be a quotient of the operands’.

product(cts: Sequence[CKKS_Ciphertext], n_threads: int = 0) → CKKS_Ciphertext

The product of cts, computed as a balanced binary tree.

The tree is ceil(log2(n)) multiplications deep, one level of the chain each, instead of n - 1. Each product is relinearized with rlk and rescaled. When a tree level has an odd count, its last factor is carried down a level unmultiplied (MLWE.mod_reduce(), on a copy), which needs nested levels unless n is a power of two.

rational_rescale(ct: CKKS_Ciphertext, n_threads: int = 0) → CKKS_Ciphertext

Rescale from level lvl to lvl + 1 when the next level’s ring is not a quotient of this one.

Each component c is lifted into the ring holding both levels’ primes as P_l * c, where P_l is the product of the primes the next level adds. That value is divided as in rescale() by q_drop, the product of this level’s primes that the next level lacks. The result is round(P_l * c / q_drop) modulo the next level’s modulus, that is (P_l * c - r) / q_drop with r the centered residue of P_l * c modulo q_drop (|r| <= (q_drop - 1)/2). The scaling factor is multiplied by P_l / q_drop.

Returns a new ciphertext, and leaves ct in the coefficient domain.

rational_rescale_batch(cts: Sequence[CKKS_Ciphertext], n_threads: int = 0) → list[CKKS_Ciphertext]

rational_rescale() of each ciphertext, all at one level, with one native call for the divisions.

ciphertext_type

alias of CKKS_Ciphertext

class vfhe.fhe.CKKS_Ciphertext(scheme: CKKS_Scheme, lvl: int | None = None, ring: RNSRing | None = None, rank: int | None = None, obj: Any = None)

A CKKS ciphertext: an MLWE sample carrying its scale delta, which multiplication raises and rescale lowers.

class vfhe.fhe.CKKS_LinearTransform(scheme: CKKS_Scheme, diagonals: Mapping[int, Sequence[complex]] | Iterable[tuple[int, Sequence[complex]]], *, lvl: int = 0, scale: float | None = None, baby_steps: int | None = None, n_threads: int = 0)

A slot matrix encoded for one level, ready to apply to ciphertexts there.

diagonals maps d to diag_d (length n), or is an iterable of such pairs; only the nonzero diagonals need be given. Each is rotated, tiled to N/2 slots and encoded once, in scheme.rings[lvl] at scale (default: scaling_factor), as CKKS_Scheme.encode_batch() does: a diagonal given as a buffer of complex doubles (a numpy complex128 array) is read in place. baby_steps is b (default: ceil(sqrt(n))).

property rotations: list[int]

The slot rotations apply() needs a key for.

apply(ciphertext: CKKS_Ciphertext, ksks: Mapping[int, MLWE_Set | list[MLWE_Set]], n_threads: int = 0) → CKKS_Ciphertext

A applied to the slots of ciphertext, without rescaling.

ksks[k] is the rotation key for k slots (CKKS_Scheme.gen_rotation_key), for every k in rotations. The result’s delta is the input’s times scale.

classmethod slot_to_coeff(scheme: CKKS_Scheme, *, slots: int | None = None, lvl: int = 0, scale: float | None = None, baby_steps: int | None = None) → CKKS_LinearTransform

SlotToCoeff: afterwards the plaintext’s coefficients hold the slots.

For n = slots (default N/2) and R = N/(2n), the n slot values z become the plaintext whose coefficients at R*t and R*t + N/2 are delta * Re z_t and delta * Im z_t, all others zero. The matrix is U_n[j][t] = eta^(5^j * t) with eta = exp(2 pi i / 4n).

classmethod coeff_to_slot(scheme: CKKS_Scheme, *, slots: int | None = None, lvl: int = 0, scale: float | None = None, baby_steps: int | None = None) → CKKS_LinearTransform

CoeffToSlot, the inverse of slot_to_coeff() (U_n^H / n).

Afterwards slot t holds (m[R*t] + i*m[R*t + N/2]) / delta for the input plaintext m, in every block of n slots. Exact when m has nonzero coefficients only at multiples of R, always the case for n = N/2.