---
title: "BFV"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{BFV}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

Load libraries that will be used.
```{r setup}
library(polynom)
library(HomomorphicEncryption)
```

Set some parameters.
```{r params}
d  =   4
n  =   2^d
p  =   (n/2)-1
q  = 874
```

Set a working seed for random numbers
```{r}
set.seed(123)
```

Here we create the polynomial modulo.

```{r GenPolyMod}
pm = polynomial( coef=c(1, rep(0, n-1), 1 ) )
print(pm)
```

Create the secret key and the polynomials a and e, which will go into the public key
```{r}
# generate a secret key
s = GenSecretKey(n)
print(s)
```

```{r}
# generate a
a = GenA(n, q)
print(a)
```

Generate the error for the public key.
```{r}
e = GenError(n)
print(e)
```

Generate the public key.
```{r}
pk0 = GenPubKey0(a, s, e, pm, q)
print(pk0)
```


```{r}
pk1 = GenPubKey1(a)
```

Create a polynomial message
```{r}
# create a message
m = polynomial( coef=c(6, 4, 2) )
```

Create polynomials for the encryption
```{r}
# polynomials for encryption
e1 = GenError(n)
e2 = GenError(n)
u  = GenU(n)
print(u)
```

Generate the ciphertext.
```{r}
ct0 = EncryptPoly0(m, pk0, u, e1, p, pm, q)
print(ct0)
```

```{r}
ct1 = EncryptPoly1(   pk1, u, e2,    pm, q)
print(ct1)
```

Decrypt
```{r}
decrypt = (ct1 * s) + ct0
decrypt = decrypt %% pm
decrypt = CoefMod(decrypt, q)

# rescale
decrypt = decrypt * p/q
```

Round (remove the error) then mod p
```{r}
# round then mod p
decrypt = CoefMod(round(decrypt), p)
print(decrypt)
```

Which is indeed the message that we first encrypted.

Next, look at the vignette BFV-2 which does the exact same process, but unpacks all the functions used here into basic mathematical operations.