Load libraries that will be used.
library(HomomorphicEncryption)
#> Loading required package: polynom
library(polynom)
Set some parameters.
= 4
d = 2^d
n = (n/2)-1
p = 424242
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 polynomials for the encryption
# polynomials for encryption
= GenError(n)
e1 = GenError(n)
e2 = GenU(n) u
Now create to messages to add.
= polynomial(c(1, 1, 1))
m1 = polynomial(c(0, 1 )) m2
Encrypt the messages.
= EncryptPoly0(m1, pk0, u, e1, p, pm, q)
m1_ct0 = EncryptPoly1( pk1, u, e2, pm, q)
m1_ct1 = EncryptPoly0(m2, pk0, u, e1, p, pm, q)
m2_ct0 = EncryptPoly1( pk1, u, e2, pm, q) m2_ct1
Sum the encrypted messages.
= m1_ct0 + m2_ct0
sum_ct0 = m1_ct1 + m2_ct1
sum_ct1
= sum_ct0 %% pm
sum_ct0 = CoefMod(sum_ct0, q)
sum_ct0
= sum_ct1 %% pm
sum_ct1 = CoefMod(sum_ct1, q) sum_ct1
Decrypt the sum
= (sum_ct1 * s) + sum_ct0
decrypt = decrypt %% pm
decrypt = CoefMod(decrypt, q)
decrypt
# rescale
= decrypt * p/q
decrypt
# round then mod p
= CoefMod(round(decrypt), p)
decrypt print(decrypt)
#> 1 + 2*x + x^2