A detailed walkthrough of the key concepts for LWE to LWE key switching.
By Roman Akhtariev•
This article is a detailed walkthrough of the key concepts for converting LWE-to-LWE under a different secret. The design is described in this paper.
LWE-to-LWE key switching is essential in several cryptographic settings:
Fully homomorphic encryption: After computing on encrypted data, you often need to switch keys — either to refresh ciphertexts, change encryption parameters, or allow a different party to decrypt.
Private information retrieval: key switching enables efficient database queries where the server doesn't learn what the client is searching for.
Multi-party computation: When encrypted data needs to be handed off between parties with different keys.
The key insight is that rather than performing the expensive key switch directly in LWE space, you convert the LWE ciphertext to RLWE first. This enables an asymptotically faster key switch. You then convert back to LWE, encrypted under the new secret.
Pre-requisites
LWE Ciphertext: A pair (b,a)∈Zq×ZqN that encrypts a value μ. Decryption computes μ=b+⟨a,s⟩(modq). (Note: some papers use the opposite sign convention μ=b−⟨a,s⟩; we follow the convention from the referenced paper.)
RLWE Ciphertext: A pair (bp,ap)∈Rq×Rq that encrypts a polynomial μ. Decryption computes μ=bp+ap⋅s(modq).
Negacyclic ring property: In Z[X]/(XN+1), we have XN=−1, so X−1=−XN−1.
Notation
Symbol
Meaning
Zq
Integers modulo q
Rq
Zq[X]/(XN+1), the negacyclic polynomial ring
⟨a,s⟩
Inner product of vectors a and s
(b,a)
LWE ciphertext with scalar b and vector a
(bp,ap)
RLWE ciphertext with polynomials bp and ap
s,s′
Secret keys (old and new)
e
Error/noise term
B,d
Gadget base and digit count
g−1(⋅)
Gadget decomposition (pseudo-inverse: g maps vectors to scalars, g−1 maps scalars to vectors)
Overview
Given an LWE ciphertext (b,a) encrypting μ0 under secret s, we want to convert it to a new LWE ciphertext (b′,a′) encrypting the same value μ0 under a different secret s′.
Why convert to RLWE? The polynomial embedding (described below) enables fast key switching via NTT-accelerated polynomial multiplication, reducing runtime from O~(N2) to O~(N).1 The naive LWE approach requires N scalar-times-vector operations.
Algorithm:
Embed into Ring: Convert vector a into polynomial a(X)=∑i=0N−1ai⋅Xi.
Secret Embedding: Convert s to polynomial s(X)=∑i=0N−1si⋅X−i using the negacyclic property X−1=−XN−1.
Key Switching: Apply RLWE key-switching to switch from s(X) to s′(X).
Extract: Read coefficients from the resulting RLWE ciphertext to get your new LWE ciphertext under s′.
Polynomial Embedding
Polynomial embedding is a technique that converts LWE (vector-based) operations into RLWE (polynomial-based) operations.
The key insight is that an LWE inner product can be computed as the constant term of the product of the LWE ciphertext and the secret.
We'll demonstrate this with N=4: the constant term of a(X)⋅s(X) will equal the inner product ⟨a,s⟩.
Let a=[a0,a1,a2,a3] and s=[s0,s1,s2,s3].
Define polynomials:
a(X)=a0+a1X+a2X2+a3X3
s(X)=s0+s1X−1+s2X−2+s3X−3
The LWE inner product ⟨a,s⟩ is then given by the constant term of the product a(X)⋅s(X).
Now collect terms by power and convert negative powers using X−k=−XN−k with N=4:
X−1=−X3
X−2=−X2
X−3=−X1
The conversion is enabled by the negacyclic ring property X−1=−XN−1.
Power
Terms from expansion
After converting X−k→−XN−k
X0
a0s0+a1s1+a2s2+a3s3
⟨a,s⟩ ✓
X1
a1s0+a2s1+a3s2 and a0s3X−3→−a0s3X1
a1s0+a2s1+a3s2−a0s3
X2
a2s0+a3s1 and a0s2X−2,a1s3X−2→−X2
a2s0+a3s1−a0s2−a1s3
X3
a3s0 and a0s1X−1,a1s2X−1,a2s3X−1→−X3
a3s0−a0s1−a1s2−a2s3
Final result:
a(X)⋅s(X)=constant term =⟨a,s⟩(a0s0+a1s1+a2s2+a3s3)
+(a1s0+a2s1+a3s2−a0s3)X
+(a2s0+a3s1−a0s2−a1s3)X2
+(a3s0−a0s1−a1s2−a2s3)X3
Key insight: The constant term of a(X)⋅s(X) equals the LWE inner product ⟨a,s⟩. This is why embedding LWE into RLWE works.
RLWE Key Switching
After embedding the LWE ciphertext into RLWE (via polynomial embedding above), we have an RLWE ciphertext (bp,ap) under secret s(X). We now need to switch it to secret s′(X).
Decrypting with s gives the encrypted value:
μ0=bp+ap⋅s(modq)
Key switching transforms an RLWE ciphertext under secret s to an RLWE ciphertext (bp′,ap′) under secret s′ while preserving μ0:
bp′+ap′⋅s′≈μ0(modq)
The challenge: You need to "remove" the ap⋅s term and "replace" it with something involving s′, but the switching party cannot compute ap⋅s directly without knowing s.
Insight: Pre-compute an encryption of s under the new secret s′, called Encs′(s). Then exploit RLWE's homomorphic property — multiplying a ciphertext by a polynomial multiplies the underlying message:
ap⋅Encs′(s)≈Encs′(ap⋅s)
This gives you an encryption of ap⋅s under s′.
But what does Encs′(s) actually mean? It's an RLWE ciphertext (bp,ap) where:
bp=−ap⋅s′+s+e′
Component
Description
ap
Random polynomial
s′
New secret — creates the "mask" ap⋅s′
e′
Small noise
s
Old secret — the "message" being encrypted
When you decrypt with s′: bp+ap⋅s′=s+e′≈s
Gadget Decomposition
The key switching technique above has a critical flaw: ap can be as large as q, and multiplying by it causes a major noise blowup.
To solve this, we can use gadget decomposition. This is a technique for decomposing a large number into a vector of smaller numbers under a pre-computed basis B:
g=(1,B,B2,…,Bd−1)
Gadget Decomposition g−1
For any ap, compute:
g−1(ap)=(ap(0),ap(1),…,ap(d−1))
where:
Each ap(i) is a small polynomial (coefficients in [0,B)).
∑i=0d−1ap(i)⋅Bi=ap (reconstruction)
Example with B=256 and d=4:
If a coefficient of ap is 0x12345678:
ap(0)=0x78 (least significant byte)
ap(1)=0x56
ap(2)=0x34
ap(3)=0x12
So g−1(0x12345678)=(0x78,0x56,0x34,0x12).
Key Switching Key
Following the gadget decomposition, the key switching key K consists of d RLWE ciphertexts instead of just one encryption of s:
K[0]=Encs′(s⋅B0)=Encs′(s)
K[1]=Encs′(s⋅B1)=Encs′(s⋅B)
K[2]=Encs′(s⋅B2)
K[3]=Encs′(s⋅B3)
Now, instead of computing ap⋅s (huge multiplier), we can:
Without gadget decomposition, computing ap⋅Encs′(s) directly would give noise ∼ap⋅e, where ap can be as large as q — catastrophic for correctness.
With gadget decomposition, the total noise is ∑iap(i)⋅ei. Since each ap(i)<B and we have d=⌈logBq⌉ digits:
∣noise∣≤d⋅B⋅∣e∣max
Trade-off: Larger B means fewer digits d, but larger multipliers. Smaller B means more digits but smaller multipliers. The optimal choice balances d⋅B.
Example
With B=256 and d=4:
ap=0x12345678
g−1(0x12345678)=(0x78,0x56,0x34,0x12)
ap(0)⋅K[0]=0x78⋅Encs′(s⋅1)
ap(1)⋅K[1]=0x56⋅Encs′(s⋅256)
ap(2)⋅K[2]=0x34⋅Encs′(s⋅65536)
ap(3)⋅K[3]=0x12⋅Encs′(s⋅16777216)
Summing all terms:
∑i=03ap(i)⋅K[i]≈Encs′(s⋅ap)
The maximum noise multiplier is max(ap(i))=0x78=120, compared to ap=0x12345678≈305,419,896 without decomposition — a 2.5 million× reduction in noise growth.
Extracting the LWE Ciphertext
Recall, by now you have an RLWE ciphertext (bp′,ap′) under secret s′.
The constant term of the result polynomial contains the original encrypted value μ0.
Recall the Polynomial Embedding step: the new LWE ciphertext (b′,a′) is constructed by reading coefficients from the RLWE ciphertext:
b′ equals the constant term of bp′
a′ comes from the coefficients of ap′, with appropriate negation due to the negacyclic structure:
a0′ is coefficient 0 of ap′
ai′ for i>0 is -(coefficient N−i) of ap′
The negation arises because we embedded s using negative powers of X (i.e., s(X)=∑i=0N−1si⋅X−i), which map to negated coefficients under the negacyclic property X−k=−XN−k. When extracting back to LWE form, we must "undo" this transformation, hence the negation for indices i>0.
You now have an LWE ciphertext (b′,a′) encrypting the same value μ0 under secret s′.
Putting It All Together
We have shown how to convert an LWE ciphertext to an RLWE ciphertext and back by following these steps:
Embed LWE → RLWE via polynomial embedding
Key-switch RLWE using gadget decomposition for noise control
Extract coefficients to get new LWE ciphertext
The key insight is that the constant term of the result polynomial contains the original encrypted value μ0, and the new LWE ciphertext is constructed by reading coefficients from the RLWE ciphertext.
This conversion has enabled us to reduce the LWE Key Switching runtime from O~(N2) in a naive approach to O~(N).