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=1is the plain [CGGI16] key, one encryption ofs_iper coefficient.functional_bootstrapruns one rotation on the library’s threads (vfhe.util.parallel.set_num_threads), all of them on every step;functional_bootstrap_batchruns one rotation per thread, for throughput. Unfolding multiplies the work and the key traffic by(2^u - 1) / uand divides the number of steps byu, so it pays only where the synchronization between steps dominates: many threads, across sockets.gadget_paramsare the bootstrapping key’s gadget (the RNS one by default; seeGadgetParams), and the rotation decomposes against it; the digits are balanced asschemesays.- 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 underoutput_key: every MGSW key drawn in one batch, seeMGSW_Scheme.encrypt_constants().
- functional_bootstrap_batch(outs: list[LWE], tv: MLWE, inputs: list[LWE], bk: CGGI16_Key, torus_base: int)¶
functional_bootstrapof 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
bkis.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.schemeis the rotation key’s, overR_N. An RLWE sample overR_n(the input and output keys’ scheme, anyn) becomesnaccumulators atscheme’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 overR_nunder the output key, coefficientkat coefficientk. Repacking goes through the trace packing of [CDKS21] with each level halved first [LY26], so the values keep their scale, and a ring switch: forn <= Nthenaccumulators pack into the subringZ[X^(N/n)]ofR_N, which the switch down projects onto; forn > Neach of then / Ninterleaved groups packs intoR_Nand the switch up interleaves them. Level 0 ofschememust 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),
bootstrapis not available:repackofblind_rotategives the trace-packed samples overR_N.blind_rotate_exponentsstarts from the exponents, for a caller that switches the input toZ_2Nitself.gsw_ellandgadget_paramschoose the MGSW gadget as inMGSW_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_layeris 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 valuedtakes 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_layerssays 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
naccumulators byn_bitsbits, least significant first. With two bits per layer, bits pair from the least significant while the pair’s powerp = 2^bsatisfies4 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 ofnaccumulators by each ofvalues, asrotatetakes them: one list per value, in this GP25’s layers. Values are in[0, 2^n_bits), with2^(n_bits - 1) <= n. All keys are drawn in one batch onn_threadsthreads (0: the library limit).
- static gaps(key: MLWE_Key) list[list[int]]¶
The gaps of each component of
key: for non-zero coefficients atj_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
schemewithhnon-zero coefficients per component, uniform in{-1, 1}({1}unlessternary), drawn until every gap fits ingap_bitsbits.gap_bitsis 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. Belowlog2(n) + 1it restricts the key distribution, which the security estimate has to account for.Raises
RuntimeErrorafterattemptsdraws.
- 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(athw_reducing_lvl) to outputs underoutput_key, through the sparseinput_keyand a rotation underrotation_key.input_keyandoutput_keyare keys of one scheme, overR_n;rotation_keyis a key of this one.input_keymust have exactlyhnon-zero coefficients per component, in{-1, 1}({1}unlessternary), and every gap must fit ingap_bitsbits, with2^(gap_bits - 1) <= n:sample_input_keydraws such keys, and says whygap_bitsmust 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 withhw_reducing_hybrid=Falsethe level’s own (a BV key switch; pair it with a radix). Never generate it at a higher level. Its gadget ishw_reducing_gadget_params, by default this GP25’s; passGadgetParams()for the RNS gadget whatever that is, and fewer digits than a prime has only at a level over one prime (seeMLWE_Scheme.gadget_scalars()).With
output_key=Nonethere 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
rotateandrepackread, for a rotation ofnaccumulators the caller schedules: the automorphism key (X -> X^-1) and the trace keys, underrotation_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(inZ_2N, after the input’s switch to2N) readtable[i]forpwithin half a step ofi * N / len(table), and its negative forpwithin half a step ofN + 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)dividesN. 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:
nsamples at level 0, canonical, accumulatorkholdingtv(X^-1) * X^-p_kfor an input of odd rank andtv * X^p_kfor even,p_kthe linear decryption of coefficientkswitched toZ_2N. Their constant coefficients are whattest_vectordescribes.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_exponentsis the rotation alone.
- blind_rotate_exponents(a: Sequence[Sequence[int]], b: Sequence[int], tv: MLWE, key: SAB_Key) list[MLWE]¶
The accumulators of
blind_rotatefor an input already switched toZ_2N:bits body’snexponents anda[i]componenti’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 overR_N: one sample forn <= N(coefficientkatk * N / n, in the subringZ[X^(N/n)]; the other coefficients are not cleared, as a ring switch down drops them) andn / Nforn > N(sampleiholding coefficientsk = i mod n / N, coefficientkatk // (n / N)).
- bootstrap(rlwe_in: MLWE, tv: MLWE, key: SAB_Key) MLWE¶
Bootstraps every coefficient of
rlwe_in(overR_n, under the output key, at the key’shw_reducing_lvl): the result is a sample of the same scheme at the key’soutput_lvl, under the output key, whose coefficientkdecrypts to whattest_vectormaps coefficientk’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]takesacc[k - d], and fork < dsigma(acc[k - d + n]),sigmathe automorphismX -> X^-1thatautomorphism_keyswitches back (anSAB_Key’s, or any level-0 key for2N - 1).keysare the MGSW keys of the layers ofd, asencrypt_rotationsmakes them; with one bit per layer, the encryptions of the bits ofd, least significant first.dhasn_bitsbits,2^(n_bits - 1) <= len(acc).
- class vfhe.fhe.SAB_Key¶
A GP25 bootstrapping key, made by
GP25.generate_bootstrap_key.For an input key of rank
rwithhnon-zero coefficients per component:gaps[i][t]are the MGSW keys of the rotation by gaptof componenti, ofgap_bitsbits, in the layers thatGP25.rotation_layersgives forbits_per_layer(with one bit per layer, the encryptions of its bits, least significant first);signs[i][t]encrypts whether itst-th non-zero coefficient is-1(Nonefor a binary key), all under the rotation key, as isautomorphism_key(X -> X^-1).hw_reducing_keyswitches the input from the output key to the input key, at levelhw_reducing_lvlof their scheme;trace_repack_keyandoutput_switch_keyrepack into the output key at leveloutput_lvl, through the trace and a ring switch (output_switch_keyisNonewhenn == Nand the rotation and output keys are one).- native_handles() tuple¶
(gap_keys, sign_keys)asgp25_blind_rotatereads 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 weighth.schemeis a rank-1 CKKS scheme with special primes (the paper’s auxiliary modulusP) and nested levels. From the top: theceil(log2(h + 1))levels of the product tree, any levels left to the application, the SlotToCoeff level, and the lowest levelq0.bootstraptakes ciphertexts at the SlotToCoeff level, encoded atinput_scale(q0 / gamma), and returns them atoutput_lvlandoutput_scale.thetaandbaseare the paper’sthetaandB: the sizes of the column and mux steps.windowis the paper’sw(defaultmax(175, N / (2 h))): the input key has exactly one non-zero coefficient in each window[c_k - w, c_k + w)modN, withc_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_scaleis the scale of the output. By default it is the one that comes back toinput_scaleat 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_lthe primes levelldrops, so that bootstrap, multiply, bootstrap needs no rescaling of its own.factor_scaleis the scale of thehblind-rotation factors. By default allh + 1terms of the product tree, pt0 included, share one scaled, the one withd^(h+1) / prod_k p_k^(m_k) = output_scale(m_kthe products of tree levelk,p_kthe primes it drops). Every rescale of the tree then rounds at aboutd. With any other value, pt0 alone makes up the difference tooutput_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_paramsis the gadget of every key-switch key.slot_to_coeffdefaults toCKKS_LinearTransform.slot_to_coeffat the SlotToCoeff level, built on first use.- property slot_to_coeff: CKKS_LinearTransform¶
The SlotToCoeff transform
bootstrapapplies 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), throughinput_key(sparse, fromsample_input_key).The switch to
input_keyexists at the lowest level only, so the sparse key need only be secure there. Keys are drawn in batches onn_threads(0: the library limit).
- bootstrap(ciphertext: CKKS_Ciphertext, key: SHIP_Key, n_threads: int = 0) CKKS_Ciphertext¶
Bootstraps the
N/2real slots ofciphertext(at the SlotToCoeff level, scale aboutinput_scale) tooutput_lvlandoutput_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/2holddelta * mu_t(deltaits scale) to one atoutput_lvlandoutput_scalewhose slots holdmu. The other coefficients are ignored.
- blind_rotate(sparse: MLWE, key: SHIP_Key, n_threads: int = 0) list[CKKS_Ciphertext]¶
The
hfactors of [CHKS25, Alg. 1], at level 0 andfactor_scale.sparseis at the lowest level under the input key, and only its maskais read. Factortholds(omega^((-s_j X^j a)_i))_(i < N/2),s_jthe coefficient of windowt.
- class vfhe.fhe.SHIP_Key¶
A SHIP bootstrapping key, from
SHIP.generate_bootstrap_key. Every key is under the output key.Per window
tof the input key:columns[t]: the4 * thetaencrypted masks of the column step, over level 0’s special ring, in the NTT domain;muxes[t][p][d]: the multiplexer key for valuedof digitp;alignments[t]: the rotation key to the window’s start, orNone.
Also
sparse_switch_key(to the input key, at the lowest level),conjugation_key(at the output level) andslot_to_coeff_keys(rotation keys by slot count, at the SlotToCoeff level).