Load libraries that will be used.
library(polynom)
library(HomomorphicEncryption)
Set some parameters.
= 4
d = 2^d
n = (n/2)-1
p = 874
q = GenPolyMod(n) pm
Set a working seed for random numbers
set.seed(123)
Create the secret key and the polynomials a and e, which will go into the public key
# generate a secret key
= GenSecretKey(n)
s
# generate a
= GenA(n, q)
a
# generate the error
= GenError(n) e
Generate the public key.
# generate the public key
= GenPubKey0(a, s, e, pm, q)
pk0 = GenPubKey1(a) pk1
Create a polynomial message
# create a message
= polynomial( coef=c(1, 1, 1) ) m
Create polynomials for the encryption
# polynomials for encryption
= GenError(n)
e1 = GenError(n)
e2 = GenU(n) u
Generate the ciphertext
= EncryptPoly0(m, pk0, u, e1, p, pm, q)
ct0 = EncryptPoly1( pk1, u, e2, pm, q) ct1
Decrypt
= (ct1 * s) + ct0
decrypt = decrypt %% pm
decrypt = CoefMod(decrypt, q)
decrypt
# rescale
= decrypt * p/q decrypt
Round (remove the error) then mod p
# round then mod p
= CoefMod(round(decrypt), p)
decrypt print(decrypt)
#> 1 + x + x^2