# CKKS encode

Load libraries that will be used.

library(polynom)
library(HomomorphicEncryption)

Set a working seed for random numbers (so that random numbers can be replicated exactly).

set.seed(123)

Set some parameters.

M     <- 8
N     <- M / 2
scale <- 4
xi    <- complex(real = cos(2 * pi / M), imaginary = sin(2 * pi / M))

Create the (complex) numbers we will encode.

z <- c(complex(real=3, imaginary=4), complex(real=2, imaginary=-1))
print(z)
#> [1] 3+4i 2-1i

Now we encode the vector of complex numbers to a polynomial.

p <- encode(xi, M, scale, z)

Let’s view the result.

print(p)
#> 10 + 5*x + 10*x^2 + 2*x^3

Let’s decode to obtain the original number:

decoded_z <- decode(xi, M, scale, p)
print(decoded_z)
#> [1] 3.03033+3.737437i 1.96967-1.262563i

The decoded z is indeed very close to the original z, we round the result to make the clearer.

round(decoded_z)
#> [1] 3+4i 2-1i

Next, work through the CKKS-encode-2 vignette, which breaks down the encode and decode functions into the individual steps.