Encoding, randomness, and key construction
The CKKS codec maps ordered complex slots to scaled integer coefficients and reconstructs slots after decryption. Cryptographic data provision constructs secret, public, and evaluation keys, while execution consumes their Tensor payloads and live randomness. These mechanisms are implemented in backend/ckks/codec/, backend/ckks/crypto/, and rng/.
How does the embedding become integer data?
For ring dimension _embedding.make_slot_tensor forms the full slot axis. Generator-dependent permutation tables select generator-3 or generator-5 ordering and embed the slots into conjugate-symmetric length-
inverse_embed_slots computes real coefficients using the supplied permutation and twister tables:
Encoding stochastically quantizes
embed_coefficients multiplies by the inverse twister, applies the corresponding inverse FFT, and restores slot order. decode_slots divides the resulting values by the actual scale norm="forward" is part of the implemented normalization; changing it requires compensating the embedding equations.
_codec.py composes these numerical functions, _implementation.py supplies registered encode/decode and integer-to-RNS operations, and _periodic.py handles compact periodic plaintext preparation. The permutation, twister, and rounding-state arrays are ordinary material operands.
How do coefficients enter and leave the residue basis?
Integer-to-RNS conversion reduces an integer coefficient into the ordered supplied prime rows. Centered lifting must retain the sign convention before reduction. After decryption, _decryption.py reconstructs the centered coefficient class modulo the full active Q product using mixed-radix data and half-product comparison. Multiple prime rows cannot generally be replaced by reading one convenient residue.
The mathematical message must remain within the intended centered range modulo Q; otherwise modular wrap changes the recovered value. FFT approximation, stochastic quantization, encryption noise, key switching, and rescale contribute different errors. Decode with the ciphertext's actual scale rather than an assumed prime width or configuration default.
The compressed plaintext article explains encoded-axis layouts, which are distinct from visible slot repetition. The RNS article defines row ordering and representation transitions.
Which random streams are used?
rng.Csprng configures CKKS sampling through the triton-csprng package's ChaCha20 generators and RNS stream interface. It supplies coefficient counts, channel counts, repeated channels, integral dtype, and discrete-Gaussian standard deviation for CPU/CUDA streams.
Secret-key generation samples ternary coefficients. Public-key generation samples uniform components and Gaussian error. Encryption samples a binary masking polynomial and Gaussian errors. Encoding consumes its mutable rounding-state Tensor. These uses have distinct distributions and must retain their intended stream advancement.
Use fixed seeds for reproducible experiments. Cryptographic use requires suitable seed entropy and unique nonces for independent streams under the same key. Reusing a seed/nonce or restoring the same stream state can repeat samples. CKKS security estimates depend on ring dimension, modulus chain, secret distribution, and error parameters.
What relation does each key represent?
KeyGenerationResource groups supplied RNS/NTT contexts, sampler, and numerical construction tables. CkksKeyGenerator exposes key construction as data provision and returns public key value classes.
| Value | Relation or purpose | Stored Tensor axes |
|---|---|---|
| Secret key | Ternary polynomial | [limb, ntt_index] |
| Public key | [key_component=2, limb, ntt_index] | |
| Hybrid key-switch key | Digit relation from | [key_digit, key_component=2, QP_limb, ntt_index] |
| Relinearization key | Switch the | Hybrid key-switch layout |
| Rotation/conjugation key | Switch the automorphed secret relation to the target secret | Hybrid key-switch layout with operation identity |
Secret and public key payloads are constructed at depth zero in NTT/Montgomery representation. Hybrid key-switch keys use depth-zero QP rows and stable key-digit identities. For digit
where
How does encryption use the public key?
For encoded message polynomial
crypto/_encryption.py performs the supplied NTT transitions and modular products. It consumes public-key and parameter/table Tensors plus a bound sampler handle. Coefficient output performs the inverse transition and standard-residue addition; NTT output retains the selected NTT/Montgomery arithmetic form.
crypto/_decryption.py evaluates
How is data provision connected to execution?
Engine factories select device services and invoke key construction. Existing keys can be supplied to Eager or bound as Tensor materials in a Compilation. Capture retains actual supplied key dataflow; optional material preparation can choose among caller-provided keys but never creates an absent evaluation key.
Extend codecs beside their table and embedding owners, and extend key constructions beside the relevant secret relation. Preserve sample distributions, scale and row conventions, and the declared Tensor ABI. For persistence, review included material symbols carefully: Compilation files are unencrypted and can contain secret or stream-state data when requested.