D-5.10 Applying RNS Techniques to FHE Operations

This subsection will explain how the RNS primitives we have learned so far are used to handle FHE operations for RNS-based ciphertexts in BFV, CKKS, and BGV.

D-5.10.1 Addition and Multiplication of Polynomials

In BFV, CKKS, and BGV, ciphertext-to-plaintext addition, ciphertext-to-ciphertext addition, and ciphertext-to-plaintext multiplication are performed by only involving modulo additions and multiplications among polynomial coefficients. Therefore, we can represent each polynomial coefficient as an RNS residue vector and compute coefficient-wise additions and multiplications by using RNS-based addition and multiplication of residues as explained in Summary A-13.1 (§A-13.1). For example, suppose we have the following two polynomials P1 and P2:

P1 = a=0n1ca1Xa Rn,q

P2 = b=0n1cb2Xb Rn,q

In the RNS-variant FHE schemes, we express each polynomial’s each coefficient as an RNS residue vector as follows:

ca1 = (ca,11,ca,21,,ca,k1) q1 × q2 × × qk # for a [0,n 1]

cb2 = (cb,12,cb,22,,cb,k2) q1 × q2 × × qk # for b [0,n 1]

Given the above RNS setup, when we add or multiply two polynomials, each coefficient-to-coefficient addition is computed as element-wise additions of two RNS residue vectors as follows:

ca1+ cb2 i=1k(ca,i1+ cb,i2)yizi mod q # where yi = q qi, zi = |yi1|qi

(ca,11+ cb,12, ca,21+ cb,22,, ca,k1+ cb,k2) q1 × q2 × × qk

Similarly, each coefficient-to-coefficient multiplication is computed as element-wise multiplications of two RNS residue vectors as follows:

ca1cb2 i=1k(ca,i1cb,i2)yizi mod q

(ca,11cb,12, ca,21cb,22,, ca,k1cb,k2) q1 × q2 × × qk

Using the above isomorphism, we can efficiently compute ciphertext-to-plaintext addition, ciphertext-to-ciphertext addition, and ciphertext-to-plaintext multiplication of big polynomial coefficients (e.g., 1000 bits big) based on small RNS residues (e.g., 30 bits each).

D-5.10.2 Key Switching

In BFV or CKKS, an RNS-based ciphertext’s key-switching operation from S S is performed by computing the following formula in RNS vectors:

𝖱𝖫𝖶𝖤S,σ(ΔM) = B + 𝖣𝖾𝖼𝗈𝗆𝗉β,l(A), 𝖱𝖫𝖾𝗏S,σβ,l(S)

In the above formula, the computation of 𝖣𝖾𝖼𝗈𝗆𝗉β,l(A), 𝖱𝖫𝖾𝗏S,σβ,l(S) can be performed by using 𝖣𝖾𝖼𝗈𝗆𝗉𝖬𝗎𝗅𝗍𝖱𝖭𝖲 (Summary D-5.9 in §D-5.9), after which B can be added to it by using regular RNS-based addition.

Similarly, in the case of the BGV, an RNS-based key-switching operation on a ciphertext from S S is performed by computing the following in RNS vectors:

𝖱𝖫𝖶𝖤S,σ(M) = B + 𝖣𝖾𝖼𝗈𝗆𝗉β,l(A), 𝖱𝖫𝖾𝗏S,σβ,l(S)

D-5.10.3 Input Slot Rotation

In BFV or CKKS, an RNS-based ciphertext’s input slot rotation is performed by computing the following formulas in RNS vectors:

1.
𝖱𝖫𝖶𝖤S(XJ(h)),σ(ΔM(XJ(h))) = (A(XJ(h)), B(XJ(h))) # where J(h) = 5h mod 2n
2.
Key-switch 𝖱𝖫𝖶𝖤S(XJ(h)),σ(ΔM(XJ(h))) to 𝖱𝖫𝖶𝖤S(X),σ(ΔM(XJ(h)))

Step 1 is equivalent to re-positioning the coefficients within each polynomial and flipping their signs whenever they cross the boundary of the n-th degree term. This step can be done with RNS-based coefficients by moving around each set of RNS residue vectors as a whole whenever the coefficient they represent is re-positioned to a new degree term, and flipping the signs of the residues in the same RNS vector altogether whenever their representing coefficient’s sign is to be flipped. Step 2’s RNS-based key switching can be done in the same way as explained in the previous subsection (§D-5.10.2).

Similarly, in BGV, an RNS-based ciphertext’s input slot rotation is performed by computing the following formulas in RNS vectors:

1.
𝖱𝖫𝖶𝖤S(XJ(h)),σ(M(XJ(h))) = (A(XJ(h)), B(XJ(h))) # where J(h) = 5h mod 2n
2.
Key-switch 𝖱𝖫𝖶𝖤S(XJ(h)),σ(M(XJ(h))) to 𝖱𝖫𝖶𝖤S(X),σ(M(XJ(h)))

We can compute the above formulas in RNS by using the same strategy explained for BFV or CKKS.

D-5.10.4 BFV’s Ciphertext-to-Ciphertext Multiplication

BFV’s ciphertext-to-ciphertext multiplication (Summary D-2.7.5 in §D-2.7.5) comprises ModRaise polynomial multiplication relinearization rescaling, where the order of relinearization and rescaling can be swapped. In RNS-based ciphertext-to-ciphertext multiplication, we will swap the order of these two steps. The procedure is as follows: (1) ModRaiseRNS from q 𝑞𝑏; (2) polynomial multiplication; (3) constant multiplication by t; (4) ModSwitch from 𝑞𝑏 b; (5) FastBConvEx from b q; and (6) relinearization. Among these, step 3 5 corresponds to the rescaling operation. We will explain how each of these steps works.

1.
ModRaiseRNS from q 𝑞𝑏bα:

Let b be a new RNS base where b > Δ so that 𝑞𝑏 is large enough to prevent a multiplied scaled plaintext in ciphertexts (i.e., Δ2M1M2) from exceeding its allowed limit (Summary D-2.3 in §D-2.3) during ciphertext-to-ciphertext multiplication. bα is also added for exact fast base conversion to be performed later. Specifically, we mod-raise the modulus of each polynomial coefficient of ciphertexts (A1,B1) and (A2,B2) as follows:

(A^1,B^1) = (A1+ UA1q,B1+ UB1q) mod 𝑞𝑏bα

(A^2,B^2) = (A2+ UA2q,B2+ UB2q) mod 𝑞𝑏bα

, where each coefficient of UA1,UB1,UA2,UB2 is either {1,0,1}. Decrypting these two (noisy) ciphertexts with the private key S would give the following outputs:

A^1S + B^1mod 𝑞𝑏bα

= (A1+ UA1q) S + (B1+ UB1q) mod 𝑞𝑏bα

= A1S + B1+ UA1q S + UB1q mod 𝑞𝑏bα

= ΔM1+ E1+ UA1q S + UB1q + K1q mod 𝑞𝑏bα # where + K1q is the q-overflows of the decryption process

A^2S + B^2mod 𝑞𝑏bα

= (A2+ UA2q) S + (B2+ UB2q) mod 𝑞𝑏bα

= A2S + B2+ UA2q S + UB2q mod 𝑞𝑏bα

= ΔM2+ E2+ UA2q S + UB2q + K2q mod 𝑞𝑏bα

2.
Polynomial Multiplication:

Compute (B^1B^2,A^1B^2+ A^2B1,A^1A^2) mod 𝑞𝑏bα, whose decryption relation is as follows:

B^1B^2+ (A^1B^2+ A^2B1) S + (A^1A^2) S2 mod 𝑞𝑏bα

= (A^1S + B^1) (A^2S + B^2) mod 𝑞𝑏bα

= (ΔM1+E1+UA1qS+UB1q+K1q)(ΔM2+E2+UA2qS+UB2q+K2q)mod𝑞𝑏bα

3.
Constant Multiplication by 𝐭:

Step 3 5 are equivalent to rescaling the plaintext’s scaling factor from Δ2 Δ as well as switching the ciphertext’s modulus from 𝑞𝑏bα to q. In this step, we multiply t to each coefficient of the resulting polynomials from the previous step as follows:

(t B^1B^2, t A^1B^2+ t A^2B1, t A^1A^2) mod 𝑞𝑏bα

, which is equivalent to a ciphertext encrypting the following plaintext:

t(ΔM1+E1+UA1qS+UB1q+K1q)(ΔM2+E2+UA2qS+UB2q+K2q)mod𝑞𝑏bα

4.
ModSwitchRNS from 𝑞𝑏bα bbα:

We switch the modulus of the ciphertext from 𝑞𝑏 to b by using ModSwitchRNS as follows:

(t B^1B^2 q , t A^1B^2+ t A^2B1 q , t A^1A^2 q ) mod bbα

, which is (almost, considering the rounding error) equivalent to a ciphertext encrypting the following plaintext:

t (ΔM1+ E1+ UA1q S + UB1q + K1q) (ΔM2+ E2+ UA2q S + UB2q + K2q) q modbbα

= (M1+ t q E1+ UA1t S + UB1t + K1t + 𝜖d) (ΔM2+ E2+ UA2q S + UB2q + K2q)modbbα

# where 𝜖d = t q ΔM1M1 is a rounding error caused by treating q t q t = Δ

5.
FastBConvExRNS from b q:

We exactly convert the base of the ciphertext from bbα q as follows:

(t B^1B^2 q , t A^1B^2+ t A^2B1 q , t A^1A^2 q ) mod q

, which is equivalent to a ciphertext encrypting the following plaintext:

= (M1+ t q E1+ UA1t S + UB1t + K1t + 𝜖d) (ΔM2+ E2+ UA2q S + UB2q + K2q)modq

= ΔM1M2+ t q ΔM2E1+ UA1tΔM2S + UB1tΔM2+ M1E2+ t q E1E2

+UA1E2tS+UB1E2t+K1tΔM2+K1tE2+𝜖d(ΔM2+E2+UA2qS+UB2q+K2q)modq

ΔM1M2mod q # all other terms are relatively much smaller than ΔM1M2 in modulo q

6.
Relinearization:

Once we have derived the rescaled polynomial triple (D0,D1,D2) mod q, the final relinearization step is equivalent to deriving the synthetic ciphertexts 𝖼𝗍α and 𝖼𝗍β and then computing 𝖼𝗍α + 𝖼𝗍β. 𝖼𝗍α is simply (D0,D1), and we can derive 𝖼𝗍β = 𝖱𝖫𝖶𝖤S,σ(D2 S2) by using the DecompMultRNS operation (Summary D-5.9 in §D-5.9). The final ciphertext-to-ciphertext addition of 𝖼𝗍α + 𝖼𝗍β can be performed by using regular RNS addition.

D-5.10.5 CKKS’s Ciphertext-to-Ciphertext Multiplication

CKKS’s ciphertext-to-ciphertext multiplication (Summary D-3.5.6 in §D-3.5.6) is almost the same as BFV’s, except that CKKS does not need the ModRaise operation in the beginning (because each multiplicative level’s modulus ql is large enough to hold a multiplied scaled plaintext Δ2M1M2). Therefore, CKKS’s RNS-based multiplication is the same as BFV’s except that it does not require step 1 (ModRaise), step 3 (constant multiplication by t), and step 5 (exact fast base conversion). Since a CKKS ciphertext’s scaling factor Δ is approximately the same as the prime modulus factor of each multiplicative level, each ciphertext-to-ciphertext multiplication only needs to perform a modulus switch to a lower level.

D-5.10.6 BGV’s Ciphertext-to-Ciphertext Multiplication

BGV’s ciphertext-to-ciphertext multiplication (Summary D-4.8 in §D-4.8) is almost the same as CKKS’s, except that BGV uses its own modulus switch (𝖬𝗈𝖽𝖲𝗐𝗂𝗍𝖼𝗁𝖱𝖭𝖲𝖡𝖦𝖵 as described in Summary D-4.7 in §D-4.7) during the rescaling step. Therefore, BGV’s RNS-based ciphertext-to-ciphertext multiplication is the same as CKKS’s, except that ModSwitchRNS is replaced by 𝖬𝗈𝖽𝖲𝗐𝗂𝗍𝖼𝗁 𝖱𝖭𝖲𝖡𝖦𝖵.

D-5.10.7 BFV’s Bootstrapping

BFV’s original bootstrapping procedure (Summary D-2.11.7 in §D-2.11.7) is as follows: (1) modulus switch from q p𝜀; (2) homomorphic decryption; (3) CoeffToSlot; (4) EvalExp; (5) SlotToCoeff; and (6) re-interpretation.

However, in RNS, we cannot mod-switch to p𝜀 because RNS’s base moduli have to be co-prime to each other, whereas the factors of p𝜀 are not. To avoid this issue, RNS-based BFV’s bootstrapping instead performs the following: (1) ModRaiseRNS from q 𝑞𝑏bα, where bbα is an auxiliary base; (2) coefficient multiplication by p𝜀; (3) ModSwitchRNS from 𝑞𝑏bα bbα; (4) FastBConvEx from bbα q; (5) homomorphic decryption to adjust the scaling factor of the plaintext; (6) CoeffToSlot; (7) EvalExp; (8) SlotToCoeff; and (9) re-interpretation. The detailed procedure is described as follows:

Input: The input BFV ciphertext to bootstrap is (A,B) mod q, which would decrypt to:

A S + B = ΔM + E + 𝐾𝑞 # where Δ = q pr

1.
ModRaiseRNS from q 𝑞𝑏bα:

Mod-raise ciphertext (A,B) mod q to (A,B) mod 𝑞𝑏bα, which would decrypt to:

A S + B = ΔM + E + 𝐾𝑞 + 𝑈𝑞(𝑚𝑜𝑑𝑞𝑏bα)

# where 𝑈𝑞 is the FastBConv + SmallMont error, and U’s coefficients are either {1,0,1}

2.
Coefficient Multiplication by p𝜀:

Multiply the coefficients of (A,B) mod q by p𝜀 to update the ciphertext to (p𝜀A,p𝜀B) mod 𝑝𝑏bα, which would decrypt to:

p𝜀A S + p𝜀B = Δp𝜀M + p𝜀E + p𝜀𝐾𝑞 + p𝜀𝑈𝑞(𝑚𝑜𝑑𝑞𝑏bα)

3.
ModSwitchRNS from 𝑞𝑏bα bbα:

Mod-switch the ciphertext (p𝜀A,p𝜀B) mod 𝑝𝑏bα to (p𝜀A q , p𝜀B q )(𝑚𝑜𝑑bbα), which would decrypt to:

p𝜀A q S + p𝜀B q = Δp𝜀M q + p𝜀E q + p𝜀𝐾𝑞 q + p𝜀𝑈𝑞 q + 𝜖(𝑚𝑜𝑑bbα)

# 𝜖 is a small rounding error

= p𝜀rM + p𝜀E q + p𝜀K + p𝜀U + 𝜖(𝑚𝑜𝑑bbα)

4.
FastBConvEx from bbα q:

Exact fast base conversion of (p𝜀A,p𝜀B) mod bbα to (p𝜀A,p𝜀B) mod q, which would decrypt to:

p𝜀rM + p𝜀E q + p𝜀K + p𝜀U + 𝜖 mod q

5.
Homomorphic Decryption:

Now, we have the ciphertext (p𝜀A,p𝜀B) mod q = 𝖱𝖫𝖶𝖤S,σ (p𝜀rM + p𝜀E q + p𝜀K + p𝜀U + 𝜖) mod q. We do homomorphic decryption by using the encrypted private key 𝖱𝖫𝖶𝖤S,σ(Δ^S), where Δ^ = q p𝜀. The output is 𝖱𝖫𝖶𝖤S,σ(Δ^ (p𝜀rM + p𝜀E q + p𝜀K + p𝜀U + 𝜖)) mod q.

6.
Perform CoeffToSlot, digit extraction, and SlotToCoeff. These operations can be performed by only regular RNS-based additions and multiplications. The final digit-extracted ciphertext is 𝖱𝖫𝖶𝖤S,σ(Δ^ (p𝜀r M + Kp𝜀 + Up𝜀)), where all noise values smaller than the (base-p) (𝜀 r)-th digits are eliminated.

7.
Scaling Factor Re-interpretation:

Theoretically re-interpret the ciphertext without any additional mathematical computation. The ciphertext 𝖱𝖫𝖶𝖤S,σ(Δ^ (p𝜀r M + Kp𝜀 + Up𝜀)) is mathematically the same as:

𝖱𝖫𝖶𝖤S,σ(Δ^ (p𝜀r M + Kp𝜀 + Up𝜀))

= 𝖱𝖫𝖶𝖤S,σ(ΔM + (K + U) q) # since Δ = q pr, and Δ^ = a p𝜀

= 𝖱𝖫𝖶𝖤S,σ(ΔM) mod q.

D-5.10.8 CKKS’s Bootstrapping

CKKS’s original bootstrapping procedure (Summary D-3.13.7 in §D-3.13.7) is as follows: (1) Modraise; (2) homomorphic decryption; (3) CoeffToSlot; (4) EvalExp; (5) CoeffToSlot; (6) Re-interpretation. In the RNS-based CKKS bootstrapping, we perform ModRaiseRNS at step 1, and all other steps are computed by using regular RNS-based addition and multiplication operations. Step 1’s ModRaiseRNS operation generates a u q0 noise (where u {1,0,1} using SmallMont) for each polynomial coefficient during FastBConv. Therefore, step 2’s homomorphic decryption outputs ΔM + E + W q0 + K q0, where W q0 represents the aggregation of all coefficient noise terms which are multiplied with the q0-overflow noises generated by FastBConv and SmallMont. The W q0 + K q0 term gets eliminated by step 4’s EvalExp which performs approximated modulo reduction based on a sine-graph evaluation whose period is q0.

D-5.10.9 BGV’s Bootstrapping

BGV’s original bootstrapping procedure (§D-4.11) is as follows: (1) modulus switch from ql q^; (2) ciphertext coefficient multiplication by p𝜀1; (3) ModRaise; (4) CoeffToSlot; (5) EvalExp; (6) homomorphic multiplication by |p(𝜀1)|p𝜀; (7) SlottToCoeff; (8) noise term re-interpretation. Given this procedure, the RNS-based bootstrapping steps are as follows:

Suppose the target BGV ciphertext to bootstrap is (A,B) mod ql, where the plaintext modulus (i.e., noise scaling factor) is p.

1.
𝖬𝗈𝖽𝖲𝗐𝗂𝗍𝖼𝗁𝖱𝖭𝖲𝖡𝖦𝖵 from ql q^ where q^ is a special modulus satisfying the following requirements: q^ 1 mod p𝜀, q^ 1 mod p, q^ and ql are co-prime, and q^ < ql.

2.
Constant multiplication by p𝜀1 to the coefficients of the ciphertext polynomials (A^,B^), which increases the underlying plaintext’s noise scaling factor Δ and the plaintext modulus from p p𝜀. This effectively updates the underlying plaintext to p𝜀1M + p𝜀E.

3.
ModRaiseRNS from q^ qL, which generates an additional noise |u q^|qL (where u {1,0,1}using SmallMont). At this point, the ciphertext is 𝖱𝖫𝖶𝖤S,σ(p𝜀1M + p𝜀E + q^K) mod qL, whose underlying plaintext is:

p𝜀1M + p𝜀E + q^K

= p𝜀1M + K mod p𝜀 # since q^ 1 mod p𝜀

After the above steps, the remaining steps (i.e., CoeffToSlot, EvalExp, homomorphic multiplication by |p(𝜀1)|p𝜀, SlotToCoeff, and re-interpretation) can be performed by regular RNS-based addition and multiplication operations.

D-5.10.10 Noise Impact of RNS Operations

When RNS techniques are used in FHE operations, the noise generated by FastBConvExRNS, ModRaiseRNS, and ModSwitchRNS is directly added to each coefficient of the ciphertext polynomials A and B. Since the decryption relation is A S + B, even the noise added to the coefficients of the polynomial A gets multiplied by a large factor due to the polynomial multiplication with S. Therefore, it is important to always ensure to reduce the generated noise of each FastBConvExRNS by using it with SmallMont.

D-5.10.11 Python Source Code of RNS Primitives

We provide a Python script implementing the following exemplary RNS primitives: FastBConv, ModRaiseRNS, ModDropRNS, ModSwitchRNS, SmallMont, and BaseBConvEx.