D-3.1 Encoding and Decoding
CKKS’s encoding and decoding is fundamentally very similar to BFV’s batch encoding scheme.
BFV designs its batch encoding scheme (Summary D-2.3 in §D-2.3) based on the updated
and
matrices
(Summary D-2.9.3 in §D-2.9). That is, BFV decodes a polynomial into an input slot vector by evaluating the polynomial
at each root of , which
is the primitive -th
root of unity (i.e., ),
and encodes an input slot vector into a polynomial by inversing this operation (i.e.,
). This encoding
and decoding scheme is designed based on Summary A-10.7 (§A-10.7) which designs the isomorphic mapping between
-dimensional vectors in a
ring (finite field) and -degree
(or lesser degree) polynomials as follows:
, where is a root of
(i.e., primitive -th root of
unity) of the -th cyclotomic
polynomial defined
over a prime modulo
ring.
CKKS’s batch encoding scheme uses exactly the same formula for encoding and decoding (i.e.,
and
), but the
-dimensional input slot vector
comprises not in a ring (i.e., ),
but complex numbers (i.e., ).
In Summary A-10.7 (§A-10.7), we also designed the mapping
between polynomials and vectors over complex numbers as follows:
, where is a root (i.e.,
the primitive -th root)
of the -th cyclotomic
polynomial defined over
complex numbers, and
is -dimensional
complex special vector space whose second-half elements are reverse-ordered conjugates of the first-half elements.
And is isomorphic
to , because the
second-half elements of
are automatically determined by its first-half elements. Therefore, the
mapping is essentially an
isomorphism between -dimensional
complex vectors and
-degree (or lesser degree)
real-number polynomials .
Therefore, CKKS’ batch encoding scheme encodes an
-dimensional complex input
slot vector into an -degree
(or lesser degree) real-number polynomial, and the decoding process is a reverse of this.
In addition, remember that in BFV, we updated
and
to
and
(Summary D-2.9.3
in §D-2.9.3) to support homomorphic rotation of input vector slots. Likewise, the CKKS batch encoding scheme
uses and
instead
of and
in
order to support homomorphic rotation. Therefore, the CKKS batch encoding scheme’s isomorphic
mapping is updated as follows:
, where ,
a rotation helper formula.
The encoding schemes of BFV and CKKS have the following differences:
- Type of Input Slot Values: BFV’s input slot values are -dimensional
integer modulo ,
which are encoded into -dimensional
polynomial coefficients (i.e., modulo-
integers). On the other hand, CKKS’s input slot values are -dimensional
complex numbers, which are encoded into -dimensional
polynomial coefficients (i.e., real numbers).
- Type of Polynomial Coefficients: BFV’s encoded polynomial coefficients are integer moduli,
whereas CKKS’s encoded polynomial coefficients are real numbers.
- Scaling Factor: Both BFV and CKKS scales their encoded polynomial coefficients
by
to .
BFV’s suggested scaling factor is ,
but CKKS’s scaling factor
has no suggested formula because its polynomial coefficients are real numbers not bound by
modulus, and thus it can be any value provided that the scaled coefficients do not overflow or
underflow the range
(or ).
- Encoding Precision: In the case of BFV, during its decoding process, BFV’s down-scaled
polynomial coefficients
preserve the precision of input values . On the other hand, CKKS’s down-scaled polynomial
coefficients may lose their precision if their original input values have too many decimal digits so
that the scaling factor cannot left-shift all of them to make them part of the integer domain, which
means that some lower decimal digits of the input value may be rounded off, which loses precision
of the original input. For example, suppose the polynomial coefficient ,
and the scaling factor .
Then, the scaled coefficient ,
and down-scaling it gives .
Since ,
CKKS’s encoding and decoding process does not always guarantee exact precision. Due to this
encoding error, CKKS is called an approximate encryption scheme. The impact of this encoding
error can grow over homomorphic operations which increases the magnitude of error and the
decoded result would gradually become more deviated from the expected exact value. One way to
reduce CKKS’s encoding error is to increase ,
and thereby left-shift more decimal digits to make them part of the scaled integer digits.
Structure of : Note that the
original decoding scheme for
described in Summary A-10.7 (§A-10.7) was:
, which decodes to a Hermitian vector:
, whose second-half elements are reverse-ordered conjugates of the first-half elements.
However, by replacing
and
with
and , we
changed the above decoding scheme to the following that supports homomorphic rotation:
# because
, which decodes to a forward-ordered (not reverse-ordered) Hermitian vector as follows:
, whose second-half elements are conjugates of the first-half elements with the same
order. Upon homomorphic rotation (which will be explained in §D-3.9), just like in
BFV’s homomorphic rotation, the first-half elements and the second-half elements of
rotate
within their own group in a wrapping manner.
We summarize CKKS’s encoding and decoding procedure as follows, which is similar to BFV’s
encoding and decoding procedure (described in Summary D-2.2.5 in §D-2.2.5):
Summary D-3.1
CKKS’s Encoding and Decoding
Input: An -dimensional
complex vector
Encoding:
-
1.
- Convert (i.e., isomorphically transform)
into an -dimensional
forward-ordered Hermitian vector
as follows:
-
2.
- Convert
into a real number vector
by applying the transformation
, where
is a basis of the -dimensional
vector space crafted as follows:
# where
# because
-
3.
- Convert
into a scaled integer vector ,
where
is a scaling factor bigger than 1 such that
never overflows or underflows
(i.e.,
or )
in all cases, even across all homomorphic operations. The finally encoded plaintext polynomial is
.
The rounding process of
during the encoding process causes an encoding error, which makes CKKS an approximate
encryption scheme.
Decoding: From the plaintext polynomial ,
recover . Then,
compute ,
where:
, and extract only the first
elements in the forward-ordered Hermitian vector
to recover the
input vector .
CKKS’s Approximation Property: In the encoding process, when we convert
, we
multiply by
which contains real numbers
with infinite decimals (e.g., )
coming from Euler’s formula, which we should round to the nearest integer by computing
(which we
will denote as
throughout this section for simplicity) and thus we lose some precision. This implies that if we later
decode
into ,
this value would be slightly different from the original input vector
. As
CKKS’s encoding scheme is subject to such a small rounding error, the decryption does not
perfectly match the original input vector. Such errors also propagate across homomorphic
computations, because those computations are done based on approximately encoded plaintext
.
As these errors are caused by throwing away the infinitely long decimal digits, they can
be corrected during the decoding process only if we use an infinitely big scaling factor
, which is impossible because
should not overflow the
ciphertext modulus
of the lowest multiplicative level. Due to this limitation, CKKS is considered an approximate
homomorphic encryption.
D-3.1.1 Example
Suppose our input complex vector’s dimension ,
the bounding polynomial degree
= 4, and the scaling factor .
Our basis of the -dimensional
vector space
, where
Given this setup, suppose we have the input complex vector
to
encode.
First, construct the forward-ordered Hermitian vector
.
Next, convert the complex vector
into a real number vector
by applying the transformation:
Convert the real number vector
into a scaled integer vector
by -scaling
and rounding as follows:
Finally, has been encoded
into the plaintext polynomial
as follows:
To decode ,
we compute:
Extract the first elements
in the Hermitian vector
to recover the input vector:
#
The original input vector
Because of the rounding drifts for converting square roots into integers, the decoded value is slightly
different from the original input complex values. This is why CKKS is called an approximate
homomorphic encryption.
Source Code: Examples of CKKS encoding can be executed by running this Python script.