library(polynom)
library(HomomorphicEncryption)
Set some parameters.
= 4
d = 2^d
n = (n/2)-1
p = p
t = 868
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.
= GenPubKey0(a, s, e*p, pm, q)
pk0 = GenPubKey1(a) pk1
Create a polynomial message
# create a message
= polynomial( coef=c(1, 1, 1) )
m1 = polynomial( coef=c(0, 1 ) ) m2
Create polynomials for the encryption
# polynomials for encryption
= GenError(n)
e1 = GenError(n)
e2 = GenU(n) u
Generate the ciphertext
= pk0*u + p*e1 + m1
m1_ct0 = m1_ct0 %% pm
m1_ct0 = CoefMod(m1_ct0, q)
m1_ct0
= pk1*u + p*e2
m1_ct1 = m1_ct1 %% pm
m1_ct1 = CoefMod(m1_ct1, q)
m1_ct1
= pk0*u + p*e1 + m2
m2_ct0 = m2_ct0 %% pm
m2_ct0 = CoefMod(m2_ct0, q)
m2_ct0
= pk1*u + p*e2
m2_ct1 = m2_ct1 %% pm
m2_ct1 = CoefMod(m2_ct1, q) m2_ct1
EvalAdd
= m1_ct0 + m2_ct0
sum_ct0 = m1_ct1 + m2_ct1 sum_ct1
Decrypt
= (sum_ct1 * s) + sum_ct0
decrypt = decrypt %% pm
decrypt = CoefMod(decrypt, q)
decrypt = CoefMod(round(decrypt), p)
decrypt print(decrypt)
#> 1 + 2*x + x^2