|
1 | 1 | # Understanding the parameters of Learning With Errors (LWE) |
2 | 2 |
|
3 | | -In this report we will briefly explain the Learning with Errors (LWE) method, which is a post-quantum public-key cryptography algorithm. We provide the mathematical background then detail our implementation based on Python 3 and `numpy`, and finally our experiments, whose objective is to understand how changing the parameters of the algorithm affects the expected result. Our experiments are based on the encryption and decryption of a single bit message of value `1`, which are executed ten thousand times for each configuration tested. Graphs are created to visualise the results. |
| 3 | +In this notebook I will briefly explain the Learning with Errors (LWE) method, which is a post-quantum public-key cryptography algorithm. We provide the mathematical background then detail our implementation based on Python 3 and `numpy`, and finally our experiments, whose objective is to understand how changing the parameters of the algorithm affects the expected result. Our experiments are based on the encryption and decryption of a single bit message of value `1`, which are executed ten thousand times for each configuration tested. Graphs are created to visualise the results. |
| 4 | + |
| 5 | +**Try live [here](https://mybinder.org/v2/gh/fredericoschardong/learning-with-errors-parameters/HEAD?filepath=Index.ipynb)** |
4 | 6 |
|
5 | 7 | ## LWE Decription |
6 | 8 |
|
7 | 9 | LWE is a post-quantum publick-key algorithm, see [this presentation](https://summerschool-croatia.cs.ru.nl/2018/slides/Introduction%20to%20post-quantum%20cryptography%20and%20learning%20with%20errors.pdf) for more information and [the original paper](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.205.2622&rep=rep1&type=pdf). This method can be resumed to the computations described in this section. First, to create a public and private key: |
8 | 10 |
|
9 | | -$$ |
10 | | -A_{m}^{n \times 1} \times S_{m}^{1 \times 1} + E_{m}^{n \times 1} = B_{m}^{n \times 1} |
11 | | -$$ |
| 11 | + |
12 | 12 |
|
13 | | -$A$ and $B$ are the public key, $S$ is the private key and $E$ is the random error, and $A,B,S,E \in \mathbb{Z}$. The matrixes $A,B,E$ have dimension $n \times 1$, that is, they are single column, because in this report we implement a single-bit encryption and decryption. |
| 13 | + and  are the public key,  is the private key and  is the random error, and . The matrixes  have dimension , that is, they are single column, because in this report we implement a single-bit encryption and decryption. |
14 | 14 |
|
15 | | -To encrypt a single-bit message $x$ using the public key $A,B$ we obtain the encrypted message composed of $(u,v)$ with: |
| 15 | +To encrypt a single-bit message  using the public key  we obtain the encrypted message composed of  with: |
16 | 16 |
|
17 | | -$$ |
18 | | -u = \left(\sum A_{samples}\right) \bmod m \\ |
19 | | -v = \left(\sum B_{samples}\right) + \frac{q}{2}x \bmod m |
20 | | -$$ |
| 17 | + |
21 | 18 |
|
22 | | -Where $samples$ are randomly chosen samples from $A$ and $B$. Finally, to decrypt the message $(u,v)$ and find the value of bit message $x$: |
| 19 | +Where  are randomly chosen samples from  and . Finally, to decrypt the message  and find the value of bit message : |
23 | 20 |
|
24 | | -$$ |
25 | | -x' = |
| 21 | + |
31 | 26 |
|
32 | 27 | ## LWE Implementation |
33 | 28 |
|
34 | 29 | The following `run` function was based on [this material](https://medium.com/asecuritysite-when-bob-met-alice/learning-with-errors-and-ring-learning-with-errors-23516a502406), where LWE is implemented to encrypt and decrypt a single bit of value `1`. All parameters required by the algorithm are passed as parameters for this function. They are: |
35 | | -+ `n` and `m`, where $n$ is the number of rows of the single column matrixes $A,B,E$, and $m$ is the modulo for all the operations |
36 | | -+ `err` sets the largest value of the interval $[1,err] \in \mathbb{Z}$, from which error values are randomly drawn and then added to the result of $A_{m}^{n \times 1} * S_{m}^{1 \times 1}$, as described above |
37 | | -+ `sample` sets the number of samples drawn from the public key $(A,B)$ to encrypt the bit-message `1` |
| 30 | ++ `n` and `m`, where `n` is the number of rows of the single column matrixes , and `m` is the modulo for all the operations |
| 31 | ++ `err` sets the largest value of the interval ![[1,err] \in \mathbb{Z}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5B1%2Cerr%5D+%5Cin+%5Cmathbb%7BZ%7D), from which error values are randomly drawn and then added to the result of , as described above |
| 32 | ++ `sample` sets the number of samples drawn from the public key  to encrypt the bit-message `1` |
38 | 33 | + `times` sets how many times the experiment is reproduced to find a statistically relevant result |
39 | 34 |
|
40 | | -The returned value is in the range $[0,1] \in \mathbb{R}$ and represents how many experiments ran successfully, that is, correctly encrypted and decrypted the bit `1`. |
| 35 | +The returned value is in the range ![[0,1] \in \mathbb{R}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5B0%2C1%5D+%5Cin+%5Cmathbb%7BR%7D) and represents how many experiments ran successfully, that is, correctly encrypted and decrypted the bit `1`. |
41 | 36 |
|
42 | 37 |
|
43 | 38 | ```python |
@@ -176,9 +171,9 @@ plt.show() |
176 | 171 |  |
177 | 172 |
|
178 | 173 |
|
179 | | -Empircally testing LWE the parameters seem to have to respect the ratio of $err \leq \frac{m}{2 \times sample}$ to correctly encrypt and decrypt. |
| 174 | +Empircally testing LWE the parameters seem to have to respect the ratio of  to correctly encrypt and decrypt. |
180 | 175 |
|
181 | | -Let's put this claim to the test. First we calculate all the prime numbers in the range $[101,100000]$ and select 1 for every 100 primes in the list. Then, for all these primes we select the number of samples at random (limiting at 1% of each prime) and calculate the uper bound value for the error range following the formula $\frac{m}{2 \times sample}$. Next, we run the LWE algorithm with the aforementioned parameters and plot the result. If our empirically found relation holds, then we shall see no point off the `1.0` value in the y axis. |
| 176 | +Let's put this claim to the test. First we calculate all the prime numbers in the range ![[101,100000]](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5B101%2C100000%5D) and select 1 for every 100 primes in the list. Then, for all these primes we select the number of samples at random (limiting at 1% of each prime) and calculate the uper bound value for the error range following the formula . Next, we run the LWE algorithm with the aforementioned parameters and plot the result. If our empirically found relation holds, then we shall see no point off the `1.0` value in the y axis. |
182 | 177 |
|
183 | 178 |
|
184 | 179 | ```python |
@@ -268,4 +263,4 @@ for index, prime in enumerate(list_of_primes_to_1M): |
268 | 263 |  |
269 | 264 |
|
270 | 265 |
|
271 | | -The relation we found empirically doesn't seem to hold for larger $m$ values. There are probably more complex relations at play in LWE, our equation $err \leq \frac{m}{2 \times sample}$ is a good start, though. |
| 266 | +The relation we found empirically doesn't seem to hold for larger `m` values. There are probably more complex relations at play in LWE, our equation  is a good start, though. |
0 commit comments