# Privacy Preserving Classification of EEG Data Using Machine Learning and Homomorphic Encryption
## 2 Methods
### Encoding Scheme
 \tag{4}\]  
Addition is performed by adding the corresponding coefficients as in (5), while subtraction consists of adding the opposite of the subtrahend that is computed, as in (6). \[A_{1}+A_{2}=(a_{0}+c_{0}-(b_{0}+d_{0}))10^{0}+\ldots+(a_{n}+c_{n}-(b_{n}+d_{n} ))10^{-n} \tag{5}\]\[-A_{2}=(d_{0}-c_{0})10^{0}+(d_{1}-_{1})10^{-1}+\ldots\ +(d_{n}-c_{n})10^{-n} \tag{6}\]  
To compute \(A_{1}A_{2}\), we multiply each coefficient of the first factor with all coefficients of the second factor and group them after the corresponding powers. This may lead to an increase in the number of coefficients. Hence, the implementation of each operation allows for selecting a different number of decimals (\(n\)) to be stored in the result. As can be noticed in (1), the entire integer part of a rational number is stored in a single coefficient. Of course, the same value can be represented symmetrically by distributing individual digits between different positive powers of 10, but since most of the machine learning-based techniques make use of data normalization, all input numbers are sub-unitary. Hence, adding more coefficients to the polynomial representation only to encode a zero value leads to an avoidable computational overhead. In combination with this encoding method, it is possible to use any HE scheme that is homomorphic for addition and multiplication, as these are the only two operations performed on ciphertexts during the encoded computations. Moreover, if a specific scheme enables scalar multiplication, the encoding method can also preserve this property, where a scalar could be, in this context, any integer that is neither encoded nor encrypted, or any encoded (but not encrypted) real value. As explained above, the encoding step transforms the input rational number in a sequence of integers representing coefficients of a polynomial function. In the following experiments, each of these coefficients is encrypted using the HE scheme proposed in [27]. The resulting ciphertext is also a sequence of integers. This scheme allows for homomorphic addition and multiplication, and it also supports scalar multiplication. The encoding method does not affect the homomorphic properties of the ciphertext. Specifically, in [27], the encryption algorithm is described by Equation (7):  
\[E_{p}(x)=smod((x+sign(x)\cdot rand()\cdot p),n) \tag{7}\]  
where \(x\) is the message (the coefficient) to be encrypted, \(p\) is a large prime number, and \(n\) is the product between \(p\) and another large prime number. The \(smod\) operation is described in Equation (8), \(mod\) is the common modulo operation, \(rand\) yields a random positive integer, and the \(sign\) function provides the sign of \(x\). \[smod(m,p)= \begin{cases}mod(m,p),&m\geqslant 0.\\ -(mod(|m|,p)),&m<0.\end{cases}  \tag{8}\]  
An example of a ciphertext obtained by encrypting the coefficients from the Equation (3) with this HE scheme, using a 32-bit key (for a simpler visualization), is represented in Equation (9). \[ \begin{split}-13.8701&=(19214182057-24703948372)10^{0}+(8 234649453-19214182065)10^{-1}\\ &+(13724415755-8234649460)10^{-2}+(5489766302-10979532605)10^{-4} \end{split}  \tag{9}\]  
For the experiments using encoded and encrypted data from the two real use-cases, the encoding precision parameter is set equal to 35, and a 256-bit encryption key is used.