# Homomorphic Addition and Subtraction using ElGamal

--

The next few years are likely to see an increase in the usage of homomorphic encryption, and where we can perform arithmetic operations on encrypted data. For example, we can perform searches on data, without actually revealing the contents of the data elements. Some methods proposed are partially homomorphic, and which can implement one or more arithmetic methods, but not all of them. In this case, we will look at the ElGamal method, and implement a simple program to add and subtract values in a homomorphic way.

## ElGamal method

With the ElGamal encryption method, we can implement homomorphic addition and subtraction. Initially, we have a private key of *x *and a public key of:

*Y*=*x.G*

and where G is the base point on the curve. A cipher is made up of:

*C*1=*r.G*

and

*C*2=*r.Y*+*M*

The value of M is then:

M=v.G

and where *v* is the integer value that we want to operate on. To decrypt we basically take the private key (*x*) and compute:

S = *x.* C_1

and then recover the message with:

M = C_2-S

## Adding

If we add two ciphertext values (created from two random numbers of r_1 and r_2), we get:

*C*_1=*r*_1 *G*+*r*_2 *G*=(*r*_1+*r*_2)*G*

and:

*C*_2=*r_*1 *Y*+*r_*2 *Y*+*M*_1+*M*_2=(*r*_1+*r*_2)*Y*+(*M_*1+*M*_2)

When we decrypt, we create:

*S*=*x C*_1=*x*(*r_*1+*r*_2)*G*=(*r*_1+*r*_2)*xG*=(*r*_1+*r*_2)*Y*

and:

*M*=*C*_2−*S*=(*r*_1+*r*_2)*Y*+*M_*1+*M*_2=*M*_1+*M*_2−((*r*_1+*r*_2)*Y*)=*M*_1+*M*_2

We then get a point on the elliptic curve that relates to our result (*res*.G), and need to find *res*. We must search for the point to recover the result. The problem we have is:

*R*=*res.G*

and we need to recover *res *to find the result. This can be done by brute force if the result is relatively small, but we can also solve for:

*res*=*log_G*(*R*)