Path: blob/main/frontier/11-homomorphic-encryption/sage/11d-bfv-scheme.ipynb
483 views
Notebook 11d: The BFV Scheme
Module 11: Homomorphic Encryption
Motivating Question. BGV encodes the message in the least significant bits and noise as multiples of . Can we flip this around, put the message in the most significant bits and let noise live in the LSBs? This is exactly what BFV (Brakerski/Fan-Vercauteren, 2012) does. The result: a scale-invariant scheme where noise management is simpler, with no explicit modulus chain.
Prerequisites. You should be comfortable with:
BGV encryption, noise growth, and modulus switching (Notebook 11c)
Ring-LWE and polynomial rings (Module 08)
Noise budgets (Notebook 11a)
Learning objectives. By the end of this notebook you will be able to:
Explain how BFV differs from BGV in message encoding.
Implement BFV key generation, encryption, and decryption.
Perform homomorphic addition and multiplication in BFV.
Understand the scale-invariant property and noise budgets.
Compare BGV and BFV side by side.
1. BGV vs BFV: Where Does the Message Live?
Bridge from Notebook 11c. In BGV, decryption computes , then takes to recover . The message sits in the low bits and noise is pushed to multiples of . BFV does the opposite.
The key idea in BFV is the scaling factor :
| BGV | BFV | |
|---|---|---|
| Message encoding | ||
| Where message lives | Least significant bits | Most significant bits |
| Where noise lives | Multiples of (MSBs) | Small residual (LSBs) |
| Decryption | ||
| Noise management | Modulus switching (explicit chain) | Scale-invariant (automatic) |
Think of it this way: if and , then . A message is encoded as . Adding noise gives . Since the message contributes and noise only , the message dominates the upper bits.
Checkpoint 1. In BFV, the message is scaled by , spreading the possible message values evenly across . Noise fills the gaps between these scaled values. Decryption works as long as the noise doesn't push a value past the midpoint between two adjacent slots.
2. BFV Key Generation
BFV key generation is almost identical to BGV, but without the factor of in the public key error:
Secret key: with small (ternary) coefficients
Public key: where , , and
Notice: BGV uses (noise scaled by ), but BFV uses just (raw noise). The factor of is baked into the message encoding instead.
3. BFV Encryption and Decryption
Encrypt message :
Sample small
The message is scaled up by , this is the crucial difference from BGV.
Decrypt ciphertext :
Compute
Return
Why does this work? After decryption: Multiplying by : (since ). Rounding recovers exactly.
Misconception alert. "BFV decryption just does like BGV." No! BFV decryption is scale and round: multiply by , then round. This is because the message is in the upper bits, not the lower bits. The at the end only wraps around the plaintext modulus.
4. Noise Budget in BFV
In BFV, the noise budget measures how much room we have between the noise and the decryption threshold. Specifically:
When the noise exceeds , the scale-and-round decryption "rounds to the wrong integer" and we get garbage.
Checkpoint 2. A fresh BFV ciphertext has a large noise budget because the noise is tiny relative to . Each homomorphic operation consumes some of this budget. When it hits zero, decryption fails.
5. Homomorphic Addition
Addition in BFV works the same as in BGV: add ciphertext components.
After addition: . The noise adds linearly.
6. Homomorphic Multiplication
This is where BFV truly differs from BGV. When we multiply two BFV ciphertexts:
But we need the result to look like . So we divide by (i.e., multiply by and round):
This is the scale-invariant property: the rescaling during multiplication keeps the result at the same scale , unlike BGV where you need to explicitly switch moduli.
7. Why "Scale-Invariant"?
The rescaling by during BFV multiplication has a remarkable property: after multiplication, the result is still encoded at scale , with noise that depends only on the input noise, not on the modulus .
Compare:
BGV: After multiplication, noise grows to . You must modulus-switch (reduce to ) to bring noise back down.
BFV: The rescaling automatically handles the extra factor. No modulus switching needed.
This is why BFV is called "scale-invariant", the noise-to-message scale ratio stays consistent without manual intervention.
Checkpoint 3. The "scale-invariant" property means: after BFV multiplication, the noise level relative to is predictable and doesn't depend on . In BGV, you must explicitly manage the modulus chain; in BFV, the rescaling is baked into the multiplication.
8. A Mini-Computation:
Let's run the same computation from the BGV notebook to see BFV in action.
9. Comprehensive Comparison: BGV vs BFV
Now that we've implemented both, let's compare them systematically.
Crypto foreshadowing. Both BGV and BFV work with integer plaintexts, messages are elements of . But what about real-number computations like machine learning inference? The next notebook introduces CKKS, which encodes approximate real numbers and allows controlled loss of precision, a paradigm shift from exact arithmetic.
10. Exercises
Exercise 1 (Worked): Decryption by Hand
Problem. If , , so . A ciphertext decrypts to the value . What is the plaintext ?
Solution:
Exercise 2 (Guided): Exhausting the Noise Budget
Problem. Encrypt a message and repeatedly multiply it by itself (squaring). Track the noise budget after each squaring. How many squarings before decryption fails?
Fill in the TODOs:
Exercise 3 (Independent): BFV with Different Parameters
Problem.
Change the plaintext modulus to (binary messages). How does this affect and the noise budget of a fresh ciphertext?
What happens if you use (almost no scaling)? Can you still decrypt correctly?
For a circuit of multiplicative depth , derive a rough lower bound on in terms of , , and the initial noise .
Summary
| Concept | Key Fact |
|---|---|
| Scaling factor | , message scaled into MSBs |
| BFV encryption | , noise in LSBs |
| BFV decryption | , scale-and-round |
| Noise budget | bits; zero means garbled output |
| Multiplication | Tensor product then rescale by , removes one factor of |
| Scale-invariant | Noise after multiplication doesn't depend on ; no modulus chain needed |
| BGV vs BFV | Same security, different noise encoding; BFV simpler, BGV more flexible |
BFV flips the BGV encoding: message in the MSBs, noise in the LSBs. The scale-and-round decryption and the rescaling during multiplication make BFV "scale-invariant", noise management is automatic rather than requiring an explicit modulus chain.