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.
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 = GenSecretKey(n)
# generate a
a = GenA(n, q)
# generate the error
e = GenError(n)
Generate the public key.
Create polynomials for the encryption
Generate the ciphertext
Decrypt
decrypt = (ct1 * s) + ct0
decrypt = decrypt %% pm
decrypt = CoefMod(decrypt, q)
print(decrypt[1:length(m)])
#> [1] 450 254 515 119
Let’s decode to obtain the original number:
decoded_z <- decode(xi, M, scale, polynomial(decrypt[1:length(m)]))
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.