Path: blob/main/frontier/11-homomorphic-encryption/sage/11e-ckks-approximate-arithmetic.ipynb
483 views
Notebook 11e: CKKS, Approximate Arithmetic
Module 11: Homomorphic Encryption
Motivating Question. BGV and BFV encrypt integers, decryption is exact or completely wrong. But machine learning, statistics, and signal processing work with real numbers. Can we build an FHE scheme for approximate computation, where small errors in the output are acceptable? CKKS (Cheon-Kim-Kim-Song, 2017) does exactly this: it encodes real numbers, allows homomorphic operations, and treats the noise as... an acceptable rounding error.
Prerequisites. You should be comfortable with:
BFV encryption and the scaling factor (Notebook 11d)
Ring-LWE and polynomial rings (Module 08)
Noise budgets and why multiplication is expensive (Notebooks 11a, 11c)
Learning objectives. By the end of this notebook you will be able to:
Encode real numbers as scaled integer polynomials.
Encrypt and decrypt with CKKS, accepting approximate results.
Perform homomorphic addition and multiplication on encrypted reals.
Apply rescaling to manage the scale after multiplication.
Track precision loss through a chain of operations.
1. The CKKS Insight: Noise Is the Rounding Error
Bridge from Notebook 11d. In BFV, decryption is exact: you either recover the correct integer plaintext, or (if noise exceeds the budget) you get garbage. There's no middle ground. CKKS flips this: decryption always gives an approximate result, and the noise simply adds to the approximation error.
The key insight:
| Scheme | Plaintext type | Noise treatment | Decryption |
|---|---|---|---|
| BGV | Exact integers () | Must stay below threshold | Exact or fail |
| BFV | Exact integers () | Must stay below threshold | Exact or fail |
| CKKS | Approximate reals () | Part of the approximation | Always approximate |
When we encode a real number at scale , we store . The rounding error () is already an approximation. CKKS says: if we're already tolerating rounding errors, we can absorb the encryption noise into that same error budget.
Checkpoint 1. Multiplication doubles the scale: . Rescaling divides by to bring it back. Each rescaling introduces a small error, but this is acceptable in approximate computation. This is exactly analogous to floating-point arithmetic, where every operation has a small rounding error.
2. CKKS Setup: Polynomial Rings
Like BGV and BFV, CKKS works in the ring . But the plaintext space is different:
| Scheme | Plaintext ring |
|---|---|
| BGV/BFV | (integer coefficients mod ) |
| CKKS | (integer coefficients, scaled reals) |
In CKKS, there is no plaintext modulus . Instead, real numbers are encoded by scaling by and rounding to integers.
3. CKKS Encoding and Decoding
In our simplified CKKS, we encode a vector of real numbers as polynomial coefficients:
Misconception alert. Real CKKS uses the canonical embedding (related to the DFT), which maps complex numbers into the polynomial coefficients. Our coefficient packing is a simplification that captures the essential ideas.
4. CKKS Encryption and Decryption
CKKS encryption is similar to BFV:
Key generation: Same as BFV, small, random,
Encrypt encoded polynomial :
Decrypt:
Then decode: divide coefficients by .
The crucial difference: there's no "mod " or "scale and round" in decryption. We just get back the noisy encoded polynomial, and the noise becomes part of the approximation error.
Checkpoint 2. In CKKS, the encryption noise () is absorbed into the encoding's approximation error (). Unlike BGV/BFV where decryption either succeeds exactly or fails catastrophically, CKKS decryption always gives an approximate result. The quality degrades gradually as noise grows.
5. Homomorphic Addition
Addition is straightforward, add ciphertext components. The scale stays the same.
6. Homomorphic Multiplication and the Scale Problem
This is where CKKS gets interesting. When we multiply two ciphertexts:
The scale has doubled from to ! If we do another multiplication, it becomes , then , etc. The scale grows exponentially.
Rescaling is the solution: after multiplication, divide the ciphertext by to bring the scale back down to .
7. Rescaling: The CKKS Innovation
After multiplication, we rescale by dividing the ciphertext coefficients by (and rounding):
This:
Reduces the scale from back to
Reduces the ciphertext modulus from to
Introduces a small rounding error
Just like BFV's modulus switching, but simpler, always divide by the same factor .
Checkpoint 3. Rescaling is the heart of CKKS: after every multiplication, divide the ciphertext by to restore the original scale. This consumes one level of the modulus (from to ), just like BGV consumes one modulus in its chain. The number of multiplications is limited by .
8. Precision Through a Chain of Operations
Let's track how precision degrades as we chain multiple operations. Each operation adds a little error.
9. The CKKS Modulus Chain
Like BGV, CKKS uses a modulus chain, but for a different reason. Each rescaling divides the modulus by :
With and , we get exactly 1 multiplication (one rescale from to ). For deeper computations, we need larger .
10. The FHE Landscape: BGV, BFV, CKKS
With all three schemes under our belt, let's see the full picture.
Crypto foreshadowing. CKKS is the backbone of privacy-preserving machine learning. Combined with techniques from Module 12 (MPC), it enables scenarios where multiple parties can jointly train models or run inference on sensitive data, without any party seeing the others' data.
11. Exercises
Exercise 1 (Worked): Precision vs Scale
Problem. Encode at three different scales: , , . What is the encoding error at each scale?
Solution:
Exercise 2 (Guided): Weighted Average on Encrypted Data
Problem. Compute the weighted average of three encrypted values: where the weights are public (not encrypted).
Fill in the TODOs:
Exercise 3 (Independent): Precision-Depth Tradeoff
Problem.
For a modulus and scale , how many multiplications (with rescaling) can you perform?
If instead you use , how does this change the number of multiplications? What happens to precision?
A neural network inference requires 10 multiplication depths. What minimum do you need for ? How does this compare to BFV's parameter requirements for the same circuit?
Summary
| Concept | Key Fact |
|---|---|
| CKKS plaintext | Real/complex numbers, encoded by scaling by and rounding |
| Noise treatment | Noise becomes part of the approximation error, not a failure mode |
| Encoding | , precision is |
| Multiplication | Scale doubles: , must rescale |
| Rescaling | Divide ciphertext by , reducing scale and modulus |
| Modulus chain | ; each rescaling consumes one level |
| Tradeoff | Larger = more precision but fewer multiplications |
CKKS is the only major FHE scheme designed for approximate real-number arithmetic. By treating noise as rounding error rather than a catastrophic failure, it enables privacy-preserving machine learning, statistical analysis, and signal processing on encrypted data.
Module 11 complete! You've now seen the full FHE landscape: from partially homomorphic schemes (Paillier, ElGamal) through the integer FHE schemes (BGV, BFV) to approximate arithmetic (CKKS). Next: Module 12: Multi-Party Computation