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.
ringsis either a singleRNSRing(the level chain is derived automatically) or an explicit list of per-level rings paired withspecial_ringswhenspecial_primes > 0– e.g. non-nested levels for rational rescaling. SeeMLWE_Schemefor both modes, and forbalancedandrandom.- 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 divideN/2; fewer values are repeated to fill the slots (sparse packing), anddecode(..., slots=len(values))reads them back.ringis the plaintext’s ring (default: level 0’s) andscalemultiplies the values (default:scaling_factor). A contiguous buffer of complex doubles (a numpycomplex128array) 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 ofvalues, 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/2slots, or the firstslots(a divisor ofN/2) of a sparsely packed message. Exact over any number of primes, and cheapest over few, which is howdecrypt()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 ofmessages, 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 (seeMLWE_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_boundbounds 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 largerdelta(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=Falsekeeps 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 2Nthat rotates the slots byksteps; 5 has orderN/2, sokis taken modN/2.
- multiply_plain(ciphertext: CKKS_Ciphertext, plaintext: RNSPolynomial, scale: float | None = None) CKKS_Ciphertext¶
Multiplies by a plaintext, without rescaling.
scaleis the plaintext’s encoding scale (default:scaling_factor; 1 for an unscaled plaintext such as a monomial). The result’sdeltais the ciphertext’s timesscale, so several products can be summed before onerescale().
- 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 atscale(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()ofctsper row of coefficients.Noneskips a term. With anIntegerMatrixthe weights are integers, not encoded at a scale: the outputs keep the ciphertexts’ scaling factor, andscalemust 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 fromself.rings. When the next level’s ring is not a quotient of the current one, this isrational_rescale().Each component
cbecomesround(c / q_drop)modulo the next level’s modulus, whereq_dropis 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, withrthe centered residue ofcmoduloq_drop, so|r| <= (q_drop - 1)/2. For example, withq_drop = 7,c = 10becomes 1 andc = 11becomes 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, whererational_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), withdeltaset 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 everyi, relinearized withksk.Not rescaled, unless
lvlis 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 ofn - 1. Each product is relinearized withrlkand 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 unlessnis a power of two.
- rational_rescale(ct: CKKS_Ciphertext, n_threads: int = 0) CKKS_Ciphertext¶
Rescale from level
lvltolvl + 1when the next level’s ring is not a quotient of this one.Each component
cis lifted into the ring holding both levels’ primes asP_l * c, whereP_lis the product of the primes the next level adds. That value is divided as inrescale()byq_drop, the product of this level’s primes that the next level lacks. The result isround(P_l * c / q_drop)modulo the next level’s modulus, that is(P_l * c - r) / q_dropwithrthe centered residue ofP_l * cmoduloq_drop(|r| <= (q_drop - 1)/2). The scaling factor is multiplied byP_l / q_drop.Returns a new ciphertext, and leaves
ctin 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
MLWEsample carrying its scaledelta, which multiplication raises andrescalelowers.
- 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.
diagonalsmapsdtodiag_d(lengthn), or is an iterable of such pairs; only the nonzero diagonals need be given. Each is rotated, tiled toN/2slots and encoded once, inscheme.rings[lvl]atscale(default:scaling_factor), asCKKS_Scheme.encode_batch()does: a diagonal given as a buffer of complex doubles (a numpycomplex128array) is read in place.baby_stepsisb(default:ceil(sqrt(n))).- apply(ciphertext: CKKS_Ciphertext, ksks: Mapping[int, MLWE_Set | list[MLWE_Set]], n_threads: int = 0) CKKS_Ciphertext¶
Aapplied to the slots ofciphertext, without rescaling.ksks[k]is the rotation key forkslots (CKKS_Scheme.gen_rotation_key), for everykinrotations. The result’sdeltais the input’s timesscale.
- 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(defaultN/2) andR = N/(2n), thenslot valueszbecome the plaintext whose coefficients atR*tandR*t + N/2aredelta * Re z_tanddelta * Im z_t, all others zero. The matrix isU_n[j][t] = eta^(5^j * t)witheta = 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
tholds(m[R*t] + i*m[R*t + N/2]) / deltafor the input plaintextm, in every block ofnslots. Exact whenmhas nonzero coefficients only at multiples ofR, always the case forn = N/2.