Fully homomorphic encryption using a C++ transpiler

Value, demand, and supply have a very harmonious relationship, so much so that they tend to uplift each other. Data has always been valuable, but since its recent stardom, it now calls itself big data, grown a bit too big for its britches if you ask me. Anything that gets mined, harvested, or produced excessively, hurts the planet. In the case of data, it’s your privacy that takes the hit.

The most interesting emerging religion is Dataism, which venerates neither gods nor man — it worships data.

Home Deus, Yuval Noah Harari Tweet

On the upside, the benefits of big data are revolutionary and immensely helpful to the user, everything from recommender systems to fraud prevention. Only if there was a way to share data without violating the user’s privacy? Well, good news. There is, and it’s called homomorphic encryption.

Homomorphic encryption is a way to perform mathematical operations on encrypted data without decrypting them. It was first theorized in the late seventies. Homomorphic encryption refers to partially homomorphic encryption(PHE) and fully homomorphic encryption(FHE) in general. The difference between FHE and its lesser counterpart is that FHE supports addition, subtraction, multiplication, and division, while the other doesn’t.

Imagine you work for a bank, and you want to understand what customers spend their money on, so you can offer your customers better bargains. To achieve this goal, you partner with an organization to perform witchcraft on the data to understand customer spending patterns. As unadulterated as your intentions are, how would the customers feel about it? With FHE you can share the customer data in an encrypted form so that the organization performing the witchcraft only see encrypted random data and not the actual customer information. Let’s see how we can put this to practice.

The good folks at Google recently open-sourced a general-purpose FHE transpiler. The example below performs a simple sum of two numbers. Notice the raw values and the encrypted values.h

You share the encrypted data with the server and the server performs the sum operation on the encrypted data. The result is also returned in encrypted form. The following code snippet shows how data is encrypted and sent to the server for processing.

The client code

The actual code that performs the sum is a plain old C++ code

The server-side function

Let’s look at another example where we compare individuals to identify who is older.

The client-side code

The server-side code

The result

As you can see, the raw values are not exposed to the server, and the server performs the mathematical operations on the encrypted data. However, there is a lot more happening here than what meets the eye. Firstly, if you noticed, there is a key used in the operation. And I did mention the library is a transpiler. Let’s look at these two claims.
What the transpiler does here is to convert regular C++ code to FHE-C++. Most of the actual heavy lifting is done by the transpiler, so you, the developer can focus on solving the business problem. And If you are comfortable writing code in C++, then this is as easy as making pot noodles.

Now to the use of keys. Yes, as you may have guessed, this FHE example works on public-key encryption. It can work on private key encryption as well. If you are interested, you could further look up strong and weak homomorphic encryption as well. For the sake of this example, we generated a key pair and used the private key to encrypt and decrypt the data, and it goes without saying that without access to the private key, you can’t decrypt the data.

Although FHE is a complex concept, the transpiler has made it simple to use. Microsoft SEAL is another attempt to take FHE to the masses. FHE is a versatile concept with a multitude of practical uses from healthcare to finance and everything in between. With everything being digitized and more businesses are moving to the cloud, the uses of FHE could be endless.

Written by:

Charith De Silva

Chief Architect at Fortude