Skip to content

Instantly share code, notes, and snippets.

@bquast
Created October 9, 2022 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bquast/701b2747f55288ec15cfa7dea54356d9 to your computer and use it in GitHub Desktop.
Save bquast/701b2747f55288ec15cfa7dea54356d9 to your computer and use it in GitHub Desktop.
# HE illustrated primer
# define some parameters (small as an example)
# N.B. these parameters are not secure, they are completely insecure
n = 4
d = 2^n
t = 7
q = 874
# load library to create polynomials
library(polynom)
# create secret key
s = polynomial( coef=c(-1, 1, 1, 0, -1, 0, 1, 0, 1, -1, 0, -1, -1, -1, 0, 1 ) )
# define a
a = polynomial( coef=c(84, -60, -282, 186, 322, -138, 70, 52, 107, -212, -369, 447, -229, -393, -256, 42) )
# define the error
e = polynomial( coef=c(1, 4, 0, 4, -4, 3, -1, 0, 4, 1, -6, -6, 7, 1, 1, -3) )
# create the public key
pk0 = -a*s + e
pk1 = a
# remove the error term
rm(e)
# create a message
m = polynomial( coef=c(3, 0, 0, 0, 0, 0, 0, 0, 4) )
# polynimials for encryption
e1 = polynomial( coef=c(4, -6, 2, -3, -3, -4, 5, 4, 4, 1, 3, -4, -1, 3, -2, -5) )
e2 = polynomial( coef=c(2, -2, -4, 1, -2, 2, -3, -4, 4, -1, 2, 5, -4, 0, 2, -7) )
u = polynomial( coef=c(1, 0, 0, -1, 0, 0, -1, 0, -1, 0, 0, 0, 1, 1, 1, 0) )
# create cyphertext
ct0 = pk0*u +e1 +m*floor(q/t) %% q
ct1 = pk1*u +e2 %% q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment