Bootstrapping

class vfhe.fhe.CGGI16(scheme: MLWE_Scheme, gsw_ell: int | None = None, unfolding: int = 1, gadget_params: GadgetParams | None = None)

The [CGGI16] functional bootstrap, with an optionally unfolded loop.

Each step of the blind rotation consumes unfolding (u) input coefficients at the price of one gadget decomposition of the accumulator [ZYL+17]. A step’s keys are MGSW encryptions of the indicators of the non-zero bit patterns its coefficients can take, the all-zero one left out [BMMP18, Alg. 1], so the input key must be binary and the key grows by (2^u - 1) / u. unfolding=1 is the plain [CGGI16] key, one encryption of s_i per coefficient.

functional_bootstrap runs one rotation on the library’s threads (vfhe.util.parallel.set_num_threads), all of them on every step; functional_bootstrap_batch runs one rotation per thread, for throughput. Unfolding multiplies the work and the key traffic by (2^u - 1) / u and divides the number of steps by u, so it pays only where the synchronization between steps dominates: many threads, across sockets.

gadget_params are the bootstrapping key’s gadget (the RNS one by default; see GadgetParams), and the rotation decomposes against it; the digits are balanced as scheme says.

generate_bootstrap_key(input_key: MLWE_Key | LWE_Key, output_key: MLWE_Key, n_threads: int = 0) → CGGI16_Key

The bootstrapping key for input_key, encrypted under output_key: every MGSW key drawn in one batch, see MGSW_Scheme.encrypt_constants().

functional_bootstrap_batch(outs: list[LWE], tv: MLWE, inputs: list[LWE], bk: CGGI16_Key, torus_base: int)

functional_bootstrap of every input, in parallel over the inputs.

class vfhe.fhe.CGGI16_Key

A bootstrapping key: its MGSW keys, step after step (see CGGI16).

native_handles()

The key as the C kernel reads it, built once and kept while bk is.

Moves every key to the NTT domain on the calling thread first.

class vfhe.fhe.GP25(scheme: MLWE_Scheme, gsw_ell: int | None = None, gadget_params: GadgetParams | None = None, bits_per_layer: int = 1)

The sparse-amortized bootstrap [GP25], rotating over scheme.

scheme is the rotation key’s, over R_N. An RLWE sample over R_n (the input and output keys’ scheme, any n) becomes n accumulators at scheme’s level 0, one per coefficient, each holding the test vector rotated by that coefficient’s linear decryption; they are then repacked into one sample over R_n under the output key, coefficient k at coefficient k. Repacking goes through the trace packing of [CDKS21] with each level halved first [LY26], so the values keep their scale, and a ring switch: for n <= N the n accumulators pack into the subring Z[X^(N/n)] of R_N, which the switch down projects onto; for n > N each of the n / N interleaved groups packs into R_N and the switch up interleaves them. Level 0 of scheme must have the primes of a level of the output key’s scheme (by value: the two rings have their own bases), which is where outputs land.

Without an output key (see the module), bootstrap is not available: repack of blind_rotate gives the trace-packed samples over R_N. blind_rotate_exponents starts from the exponents, for a caller that switches the input to Z_2N itself.

gsw_ell and gadget_params choose the MGSW gadget as in MGSW_Scheme, and every other key uses the same parameters unless told otherwise (GadgetParams; fewer digits than a prime has, the approximate decomposition, needs a rotation level 0 over one prime). Everything runs on the library’s threads (vfhe.util.parallel.set_num_threads).

bits_per_layer is the number of rotation bits per layer of the blind rotation. With 1, a layer is one CMUX per accumulator. With 2, BatchBoot’s multi-bit rotation [LWZ+26], a pair of bits of value d takes three MGSW keys, of [d = 1], [d = 2] and [d = 3], and each accumulator is decomposed once per pair rather than once per bit; rotation_layers says which bits pair. That takes 1.5 times the MGSW keys and, with the RNS gadget, adds about 0.8 bit of rotation noise.

rotation_layers(n: int, n_bits: int) → list[int]

The number of bits of each layer of a rotation of n accumulators by n_bits bits, least significant first. With two bits per layer, bits pair from the least significant while the pair’s power p = 2^b satisfies 4 p <= n; the remaining bits take one layer each.

encrypt_rotations(values: Sequence[int], n_bits: int, n: int, key: MLWE_Key, n_threads: int = 0) → list[list[MGSW]]

The MGSW keys, under key, of rotations of n accumulators by each of values, as rotate takes them: one list per value, in this GP25’s layers. Values are in [0, 2^n_bits), with 2^(n_bits - 1) <= n. All keys are drawn in one batch on n_threads threads (0: the library limit).

static gaps(key: MLWE_Key) → list[list[int]]

The gaps of each component of key: for non-zero coefficients at j_1 > ... > j_h, [n - j_1, j_1 - j_2, ..., j_h].

static sample_input_key(scheme: MLWE_Scheme, h: int, gap_bits: int, sigma_err: float, ternary: bool = True, attempts: int = 32768) → MLWE_Key

A key for scheme with h non-zero coefficients per component, uniform in {-1, 1} ({1} unless ternary), drawn until every gap fits in gap_bits bits.

gap_bits is public: the bootstrapping key holds that many MGSW keys per gap. It must be a parameter fixed before the key is drawn, never derived from a key – the fewest bits that fit a given key would publish a bound on its gaps. Below log2(n) + 1 it restricts the key distribution, which the security estimate has to account for.

Raises RuntimeError after attempts draws.

generate_bootstrap_key(input_key: MLWE_Key, rotation_key: MLWE_Key, output_key: MLWE_Key | None, h: int, gap_bits: int, ternary: bool = True, hw_reducing_lvl: int | None = None, hw_reducing_hybrid: bool = True, hw_reducing_gadget_params: GadgetParams | None = None, n_threads: int = 0) → SAB_Key

The bootstrapping key: inputs under output_key (at hw_reducing_lvl) to outputs under output_key, through the sparse input_key and a rotation under rotation_key.

input_key and output_key are keys of one scheme, over R_n; rotation_key is a key of this one. input_key must have exactly h non-zero coefficients per component, in {-1, 1} ({1} unless ternary), and every gap must fit in gap_bits bits, with 2^(gap_bits - 1) <= n: sample_input_key draws such keys, and says why gap_bits must not come from the key. Errors about the input key do not say which coefficient or gap failed.

The Hamming-weight-reducing key switch, output_key -> input_key, is generated for one level only, hw_reducing_lvl (default: the last, the lowest modulus), and inputs must arrive there. It encrypts the dense key under the sparse one, so the sparse key only has to be secure at that level’s modulus: its special ring’s, or with hw_reducing_hybrid=False the level’s own (a BV key switch; pair it with a radix). Never generate it at a higher level. Its gadget is hw_reducing_gadget_params, by default this GP25’s; pass GadgetParams() for the RNS gadget whatever that is, and fewer digits than a prime has only at a level over one prime (see MLWE_Scheme.gadget_scalars()).

With output_key=None there is neither that switch nor an output switch (see the module).

Every MGSW key is drawn in one batch, on n_threads (0: the library limit), and so are the other keys.

generate_repacking_key(rotation_key: MLWE_Key, n: int, n_threads: int = 0) → SAB_Key

The keys of a bootstrapping key that rotate and repack read, for a rotation of n accumulators the caller schedules: the automorphism key (X -> X^-1) and the trace keys, under rotation_key. No MGSW keys, no Hamming-weight-reducing or output switch.

test_vector(table: Sequence[int]) → MLWE

The test vector that makes the bootstrap of a coefficient whose linear decryption is p (in Z_2N, after the input’s switch to 2N) read table[i] for p within half a step of i * N / len(table), and its negative for p within half a step of N + i * N / len(table).

Values are integers mod the level-0 modulus q, the message as it is to be decrypted (scaled already); len(table) divides N. The rounding offset of half a step is part of the test vector.

blind_rotate(rlwe_in: MLWE, tv: MLWE, key: SAB_Key) → list[MLWE]

The accumulators: n samples at level 0, canonical, accumulator k holding tv(X^-1) * X^-p_k for an input of odd rank and tv * X^p_k for even, p_k the linear decryption of coefficient k switched to Z_2N. Their constant coefficients are what test_vector describes.

With an output key, the input is under it, at the level of the key’s Hamming-weight-reducing switch, and is switched to the input key first; without one, the input is under the input key. blind_rotate_exponents is the rotation alone.

blind_rotate_exponents(a: Sequence[Sequence[int]], b: Sequence[int], tv: MLWE, key: SAB_Key) → list[MLWE]

The accumulators of blind_rotate for an input already switched to Z_2N: b its body’s n exponents and a[i] component i’s mask, all in [0, 2N), under the input key. Nothing is switched or rounded here.

repack(acc: list[MLWE], key: SAB_Key) → list[MLWE]

The accumulators of blind_rotate, packed under the rotation key over R_N: one sample for n <= N (coefficient k at k * N / n, in the subring Z[X^(N/n)]; the other coefficients are not cleared, as a ring switch down drops them) and n / N for n > N (sample i holding coefficients k = i mod n / N, coefficient k at k // (n / N)).

bootstrap(rlwe_in: MLWE, tv: MLWE, key: SAB_Key) → MLWE

Bootstraps every coefficient of rlwe_in (over R_n, under the output key, at the key’s hw_reducing_lvl): the result is a sample of the same scheme at the key’s output_lvl, under the output key, whose coefficient k decrypts to what test_vector maps coefficient k’s linear decryption to. Needs a key with an output key.

rotate(acc: list[MLWE], keys: list[MGSW], automorphism_key: MLWE_Set) → None

Rotates the accumulators in place by d: acc[k] takes acc[k - d], and for k < d sigma(acc[k - d + n]), sigma the automorphism X -> X^-1 that automorphism_key switches back (an SAB_Key’s, or any level-0 key for 2N - 1).

keys are the MGSW keys of the layers of d, as encrypt_rotations makes them; with one bit per layer, the encryptions of the bits of d, least significant first. d has n_bits bits, 2^(n_bits - 1) <= len(acc).

multiply_by_signed_monomials(acc: list[MLWE], a: list[int], sign: MGSW | None) → None

acc[k] *= X^a[k] if sign encrypts 0, X^-a[k] if it encrypts 1; with no sign, X^a[k]. In place; a[k] < 2N.

class vfhe.fhe.SAB_Key

A GP25 bootstrapping key, made by GP25.generate_bootstrap_key.

For an input key of rank r with h non-zero coefficients per component: gaps[i][t] are the MGSW keys of the rotation by gap t of component i, of gap_bits bits, in the layers that GP25.rotation_layers gives for bits_per_layer (with one bit per layer, the encryptions of its bits, least significant first); signs[i][t] encrypts whether its t-th non-zero coefficient is -1 (None for a binary key), all under the rotation key, as is automorphism_key (X -> X^-1). hw_reducing_key switches the input from the output key to the input key, at level hw_reducing_lvl of their scheme; trace_repack_key and output_switch_key repack into the output key at level output_lvl, through the trace and a ring switch (output_switch_key is None when n == N and the rotation and output keys are one).

native_handles() → tuple

(gap_keys, sign_keys) as gp25_blind_rotate reads them, built once; every key moves to the NTT domain on the calling thread first.

class vfhe.fhe.SHIP(scheme: CKKS_Scheme, *, h: int, theta: int, base: int = 4, gamma: float = 32.0, window: int | None = None, gadget_params: GadgetParams | None = None, slot_to_coeff: CKKS_LinearTransform | None = None, output_scale: float | None = None, factor_scale: float | None = None)

The SHIP bootstrap [CHKS25] over scheme, for input keys of Hamming weight h.

scheme is a rank-1 CKKS scheme with special primes (the paper’s auxiliary modulus P) and nested levels. From the top: the ceil(log2(h + 1)) levels of the product tree, any levels left to the application, the SlotToCoeff level, and the lowest level q0. bootstrap takes ciphertexts at the SlotToCoeff level, encoded at input_scale (q0 / gamma), and returns them at output_lvl and output_scale.

theta and base are the paper’s theta and B: the sizes of the column and mux steps. window is the paper’s w (default max(175, N / (2 h))): the input key has exactly one non-zero coefficient in each window [c_k - w, c_k + w) mod N, with c_k = floor(k N / h). The windows are public, which narrows the key distribution; its security estimate must account for that [CHKS25, Section 5.1].

output_scale is the scale of the output. By default it is the one that comes back to input_scale at the SlotToCoeff level when every level in between multiplies two ciphertexts at a common scale and rescales: s_l = sqrt(p_l * s_(l+1)), p_l the primes level l drops, so that bootstrap, multiply, bootstrap needs no rescaling of its own.

factor_scale is the scale of the h blind-rotation factors. By default all h + 1 terms of the product tree, pt0 included, share one scale d, the one with d^(h+1) / prod_k p_k^(m_k) = output_scale (m_k the products of tree level k, p_k the primes it drops). Every rescale of the tree then rounds at about d. With any other value, pt0 alone makes up the difference to output_scale, and every node on its path is rescaled at pt0’s scale; when that is lower than the factors’, the rounding noise of those rescales, which grows with the key’s norm, dominates the output’s error. The keys do not depend on it.

gadget_params is the gadget of every key-switch key. slot_to_coeff defaults to CKKS_LinearTransform.slot_to_coeff at the SlotToCoeff level, built on first use.

property output_lvl: int

The level bootstrap returns at.

property input_scale: float

The scale bootstrap’s inputs are expected at, q0 / gamma.

property slot_to_coeff: CKKS_LinearTransform

The SlotToCoeff transform bootstrap applies first.

sample_input_key(sigma_err: float, attempts: int = 1024) → MLWE_Key

A key with one non-zero coefficient per window, uniform in {-1, 1}, at a uniform free position of the window.

generate_bootstrap_key(output_key: MLWE_Key, input_key: MLWE_Key, n_threads: int = 0) → SHIP_Key

The bootstrapping key for inputs and outputs under output_key (dense), through input_key (sparse, from sample_input_key).

The switch to input_key exists at the lowest level only, so the sparse key need only be secure there. Keys are drawn in batches on n_threads (0: the library limit).

bootstrap(ciphertext: CKKS_Ciphertext, key: SHIP_Key, n_threads: int = 0) → CKKS_Ciphertext

Bootstraps the N/2 real slots of ciphertext (at the SlotToCoeff level, scale about input_scale) to output_lvl and output_scale.

The slots’ imaginary parts must be zero. The product tree uses the scheme’s relinearization key (MLWE_Scheme.rlk).

half_bootstrap(ciphertext: CKKS_Ciphertext, key: SHIP_Key, n_threads: int = 0) → CKKS_Ciphertext

[CHKS25, Alg. 1]: from a ciphertext at the lowest level whose coefficients t < N/2 hold delta * mu_t (delta its scale) to one at output_lvl and output_scale whose slots hold mu. The other coefficients are ignored.

blind_rotate(sparse: MLWE, key: SHIP_Key, n_threads: int = 0) → list[CKKS_Ciphertext]

The h factors of [CHKS25, Alg. 1], at level 0 and factor_scale. sparse is at the lowest level under the input key, and only its mask a is read. Factor t holds (omega^((-s_j X^j a)_i))_(i < N/2), s_j the coefficient of window t.

class vfhe.fhe.SHIP_Key

A SHIP bootstrapping key, from SHIP.generate_bootstrap_key. Every key is under the output key.

Per window t of the input key:

  • columns[t]: the 4 * theta encrypted masks of the column step, over level 0’s special ring, in the NTT domain;

  • muxes[t][p][d]: the multiplexer key for value d of digit p;

  • alignments[t]: the rotation key to the window’s start, or None.

Also sparse_switch_key (to the input key, at the lowest level), conjugation_key (at the output level) and slot_to_coeff_keys (rotation keys by slot count, at the SlotToCoeff level).