Awesome
tfhe_ml
Machine learning on homomorphically encrypted data
This repo relies on the TFHE homomorphic encryption library linked at the bottom of this page. We provide two things:
-
An implementation of basic arithmetic operations using TFHE
-
An application of the above operations to logistic regression
Example usage
Package dependencies
Languages: C++, Python
NOTE: This code has only been tested on an Ubuntu 16.04 machine.
C++
This code depends on the TFHE library.
Python
See src/python/requirements.txt
From within src/python
, run pip install -r requirements.txt
to install python dependencies.
Installing TFHE library
-
Clone the TFHE repo to the directory
$TFHE_DIR
(assuming that's where you've stored the location) and run the following commands:cd $TFHE_DIR git submodule init git submodule update mkdir build cd build cmake .. ${BUILD_FLAGS:-'-DENABLE_FFTW=on -DCMAKE_BUILD_TYPE=optim -DENABLE_TESTS=off'} make
The library is now built. See tfhe_ml/src/Makefile to see how to compile the program.
Encrypting two integers and adding the results, then decrypting
#include "encryption.hpp"
#include "alu.hpp"
#include <iostream>
int main() {
typdedef num_type int8_t;
size_t size = sizeof(num_type) * 8;
// setup
const int minimum_lambda = 128;
TFheGateBootstrappingParameterSet* params = new_default_gate_bootstrapping_parameters(minimum_lambda);
const TFheGateBootstrappingSecretKeySet* sk = new_random_gate_bootstrapping_secret_keyset(params);
const TFheGateBootstrappingCloudKeySet* ck = &sk->cloud;
LweSample *enc_a = new_gate_bootstrapping_ciphertext_array(size, params),
*enc_b = new_gate_bootstrapping_ciphertext_array(size, params);
// define integer
num_type pt = 8, pt2 = 45;
encrypt<num_type>(enc_a, pt, sk);
encrypt<num_type>(enc_b, pt2, sk);
// create new encrypted object to hold sum
LweSample *sum = new_gate_bootstrapping_ciphertext_array(size, params);
// add the two integers homomorphically
add(sum, enc_a, enc_b, ck, size);
// decrypt and print out
num_type plain_sum = decrypt<num_type>(sum, sk);
std::cout << pt << " + " << pt2 << " = " << plain_sum << std::endl;
}
Built-in tests
Two test programs have been written:
-
src/test_arithmetic.cpp
- This tests the timing characteristics of the arithmetic library. To build, runmake test_arithmetic
from withinsrc
directory and then run./test_arithmetic
to run. -
src/test_logistic.cpp
- This tests the logistic regression algorithm. To build, runmake test_logistic
from withinsrc
directory and then run./test_logistic
to run.