Load libraries that will be used.
Set a working seed for random numbers (so that random numbers can be replicated exactly).
Set some parameters.
Create the (complex) numbers we will encode.
Now we encode the vector of complex numbers to a polynomial.
pi_z <- pi_inverse(z)
scaled_pi_z <- scale * pi_z
rounded_scale_pi_zi <- sigma_R_discretization(xi, M, scaled_pi_z)
m <- sigma_inverse(xi, M, rounded_scale_pi_zi)
coef <- as.vector(round(Re(m)))
m <- polynomial(coef)
Let’s view the result.
Set some parameters:
Create the secret key and the polynomials a and e, which will go into the public key:
# generate a secret key
s = polynomial( sample.int(3, n, replace=TRUE)-2 )
# generate a
a = polynomial(sample.int(q, n, replace=TRUE))
# generate the error
e = polynomial( coef=round(stats::rnorm(n, 0, n/3)) )
Generate the public key:
Create polynomials for the encryption:
# polynomials for encryption
e1 = polynomial( coef=round(stats::rnorm(n, 0, n/3)) )
e2 = polynomial( coef=round(stats::rnorm(n, 0, n/3)) )
u = polynomial( coef=sample.int(3, (n-1), replace=TRUE)-2 )
Generate the ciphertext (encryption):
Decrypt:
decrypt <- (ct1 * s) + ct0
decrypt <- decrypt %% pm
decrypt <- CoefMod(decrypt, q)
print(decrypt[1:length(coef(m))])
#> [1] 450 254 515 119
Let’s decode to obtain the original numbers:
rescaled_p <- decrypt[1:length(m)] / scale
z <- sigma_function(xi, M, rescaled_p)
decoded_z <- pi_function(M, z)
print(decoded_z)
#> [1] 2.727297+3.893754i 1.772703-1.256246i
The decoded z is indeed very close to the original z, we round the result to make the clearer.