# A question about digit extraction of BFV/BGV's bootstrapping

**URL:** <https://openfhe.discourse.group/t/a-question-about-digit-extraction-of-bfv-bgvs-bootstrapping/1788>\
**Category:** FHE Questions\
**Created:** [December 19, 2024, 6:29am UTC](https://openfhe.discourse.group/t/a-question-about-digit-extraction-of-bfv-bgvs-bootstrapping/1788 "2024-12-19T06:29:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gogo9th](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gogo9th](https://openfhe.discourse.group/u/gogo9th)\
**Post date:** [December 19, 2024, 6:29am UTC](https://openfhe.discourse.group/t/a-question-about-digit-extraction-of-bfv-bgvs-bootstrapping/1788/1 "2024-12-19T06:29:38Z")

</div>

Hi,

I am reading [this paper](https://eprint.iacr.org/2014/873.pdf) to learn how BFV’s (and BGV’s) bootstrapping works. But I don’t understand how the digit extraction works. In the paper’s section:

5.3 Digit-Extraction for Plaintext Space Modulo p^r

The key algorithm of digit extraction is as follows:

![image|690x55](upload://fmCMIMOVLHjoB9trSfBUcmtiRgp.png

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/openfhe/original/1X/a3687dd3278fc96aa6bfc8c946e3cf62c05cbe42.png)

However, I don’t follow the correctness of this algorithm. Specifically, I break down my question into 3.

Q1. In Line 5 of the above algorithm, is it correct that F\_e(w\_{j, k}) extracts the LSD (least significant digit) of base-p representation of w\_{j, k}? If yes, then I suppose Line 5’s w\_{j, k + 1} \leftarrow F\_e(w\_{j, k}) specifically means:  
w\_{j, k + 1} \leftarrow F\_e(w\_{j, k}) \bmod p

Is this correct?

Q2. If Q1 is yes, then I don’t understand how Line 5 works over iterations. For example, in the 1st outer for-loop iteration (k = 0), at Line 5 we store w\_{0, 1} \leftarrow F\_e(w\_{0, 0}), which is the LSD of base-p representation of the initial input z. However, in the 2nd outer for-loop iteration (k = 1), at Line 5 we store w\_{0, 2} \leftarrow F\_e(w\_{0, 1}), where F\_e(w\_{0, 1}) does not make sense if F\_e(w\_{0, 1}) returns the LSD of base-p representation of w\_{0, 1} (which is the LSD of base-p representation of the initial input z).

Q3. Does Line 6 remove y's LSD in base-p representation and then shift the digit by 1 position to the right? In other words, if y was originally c\_0 + c\_1p + c\_2p^2 + \cdots, then does Line 6 update y as c\_1 + c\_2p + c\_3p^2 + \cdots?

Q4. I wonder why we have to use 2 for-loops instead of 1 for-loop. I think the following algorithm could work:

\text{INPUT: } (z, e)  
\text{for } k = 0 \text{ to } e - 1:  
\text{ } \text{ }\text{ } y \leftarrow F\_e(z) \bmod p  
\text{ } \text{ }\text{ } z \leftarrow \dfrac{z - y}{p}  
\text{return } z

The above algorithm shifts the original input z by e digits to the right in base-p representation. Why can’t we use the above algorithm for BFV’s digit extraction? (In my above suggestion, I set e’ = 0, but I believe this still guarantees correctness of Lemma 5.3 in the paper because the binomial expansion’s correctness still holds even if e’ = 0, and therefore this also guarantees correctness of Lemma Corollary 5.4 and 5.5).

---

<div class="post-metadata">

**Author:** ![gogo9th](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gogo9th](https://openfhe.discourse.group/u/gogo9th)\
**Post date:** [December 20, 2024, 6:30pm UTC](https://openfhe.discourse.group/t/a-question-about-digit-extraction-of-bfv-bgvs-bootstrapping/1788/2 "2024-12-20T18:30:52Z")

</div>

I found a solution for this question: [https://arxiv.org/pdf/1906.02867](https://arxiv.org/pdf/1906.02867)

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/openfhe/original/1X/70d17eb1d06e24d2025e7870e5987053e391aceb.png)

Actually, my understanding on the F\_e(X) function was a bit wrong. I present my correct understanding below.

Suppose we have an input value z = z\_0 + z\_1p + z\_2p^2 + \cdots + z\_{e-1}p^{e-1}, which is a base-p representation of the input value z, and our goal is to extract the lowest digit z\_0. First, we can re-write z as z = z\_0 + z'p, where  
e' = 1  
z' = z\_1 + z\_2p + z\_3p^2 + \cdots + z\_{e-1}p^{e-2}.  
Now, we evaluate F\_e(z\_0 + z'\_1p) \equiv z\_0 \bmod p^2.  
This means that  
F\_e(z\_0 + z'\_1p) \bmod p^2 = z\_0 + 0p + z^{\langle 1 \rangle}\_2p^2 + z^{\langle 1 \rangle}\_3p^3 + \cdots + z^{\langle 1 \rangle}\_{e-1}p^{e-1} \bmod p^2  
for some \{z^{\langle 1 \rangle}\_2, z^{\langle 1 \rangle}\_3, \cdots, z^{\langle 1 \rangle}\_{e-1}\} that we don’t know (and we don’t need to know). Notice that applying F\_e(X) once to z successfully removes the second-lowest digit of z in base-p representation.

In the 2nd round, we let  
y\_1 = F\_e(z\_0 + z'\_1p) \bmod p^2  
e' = 2  
z' = z^{\langle 1 \rangle}\_2 + z^{\langle 1 \rangle}\_3p + z^{\langle 1 \rangle}\_4p^2 + \cdots + z^{\langle 1 \rangle}\_{e-1}p^{e-3}  
Then, we compute:  
F\_e(y\_1) \bmod p^3 = F\_e(z\_0 + z'p^2) \bmod p^3  
 = z\_0 + 0p + 0p^2 + z^{\langle 1 \rangle}\_3p^3 + \cdots + z^{\langle 1 \rangle}\_{e-1}p^{e-1}  
Notice that applying F\_e(X) twice to z successfully removes the second-lowest and third-lowest digits of z in base-p representation.

We continue the above rounds for total e - 1 times. Then, we get:  
F\_e \circ \cdots \circ F\_e \circ F\_e \circ F\_e (z) \equiv z\_0 \bmod p^e  
, which eliminates all higher digits of z except for the least significant digit’s value z\_0 in base-p representation.

After extracting the least significant digit, we subtract it from the original value z and divide it by p, which effectively shifts z by 1 digit to the right while keeping all other upper-digit values the same as before. We do this right shifting for total r times, given the noise values are within the lowest r digits of z in base-p reprentation. Then, we eliminate all noise from z and extract only the noise-free plaintext value. Then, we can scale up the plaintext value again by the original scaling factor \Delta, and then do SlotToCoeff transition (i.e., apply decoding to it, homomorphically). Then, the noise-removed plaintext values will be placed in the coefficients of the plaintext polynomial as a scaled form: \Delta M. However, the new noise E generated during digit extraction and SlotToCoeff will be also added to it. Thus, we will have a form of \Delta M + E.
