Your First Encrypted Dot Product With CKKS
Run a packed encrypted dot product in TenSEAL and measure numerical error, ciphertext size, and evaluation time.
Background reading Fully Homomorphic Encryption
Define the workload
The data owner encrypts [1.5, −2, 0.25]. The evaluator holds public weights [2, 0.5, 4]. The reference result is 3 − 1 + 1 = 3. The evaluator loads a context serialized without the secret key, computes the dot product, and returns a serialized ciphertext for the owner to decrypt.
Run the example
Download the Python example. Use a Python environment with a compatible TenSEAL wheel.
python -m pip install tenseal==0.3.18
python ckks-dot-product.pyThe example uses polynomial modulus degree 8192, coefficient modulus bit sizes [60, 40, 40, 60], and scale 2⁴⁰. The dot product multiplies slots by the plaintext weights and sums them with rotations and additions. Galois keys enable the rotations. This computation uses one multiplication level and requires no bootstrapping.
Read the measured result
- Plaintext reference 3.000000. Decrypted result 2.999998903. Absolute error about 1.1 × 10⁻⁶.
- Input ciphertext 331,433 bytes. Result ciphertext 235,081 bytes. These sizes exclude the separately serialized evaluation context and keys.
- Dot-product evaluation took 2.256 ms in one arm64 macOS 26.3.1 run with TenSEAL 0.3.18 and Python 3.14. This excludes key generation, encryption, serialization, and decryption.
Run it again and compare the absolute error and ciphertext sizes. Encryption is randomized and timing varies. Look for a result near 3 and an error below the example’s 10⁻⁴ tolerance. The script also reports three coefficient modulus primes in the input ciphertext and two in the result. Try multiplying encrypted_result by 2 after timing the dot product. Expect one prime left and a decrypted value close to 6. TenSEAL documentation and examples.