diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd1c0e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,69 @@ +bin/* +build/* +lib/* + +# Created by http://www.gitignore.io + +### C++ ### +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + + +### Windows ### +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + + +### Linux ### +*~ + +# KDE directory preferences +.directory + + +### vim ### +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.un~ +Session.vim +.netrwhist +*~ + +### KDevelop4 ### +*.kdev4 +.kdev4/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..646f651 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The source code is published under a MIT license: + +Copyright (c) 2011, 2014 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..35bea69 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +CC = gcc + +SRCDIR = src +BINDIR = bin +BUILDDIR = build + +PREFIX_LIB = /usr/local/lib + +TARGET = $(BINDIR)/integer-fhe + +LIBRARY_FILENAME = libscarab.so +LIBRARY = $(BUILDDIR)/$(LIBRARY_FILENAME) + + +CFLAGS = -Wall -g3 -ggdb -std=c99 -I/usr/local/include -I/usr/local/include/libflint -L/usr/local/lib +LDFLAGS += -lgmp -lflint +SOURCES = $(shell find $(SRCDIR) -name '*.c') +OBJECTS = $(SOURCES:.c=.o) +.PHONY: clean + +all: $(TARGET) $(LIBRARY) + +$(TARGET): $(OBJECTS) + mkdir -p $(BINDIR) + $(CC) -o $(TARGET) $(SOURCES) $(CFLAGS) $(LDFLAGS) + +clean: + rm -r $(BINDIR) $(BUILDDIR) + +$(LIBRARY): + mkdir -p $(BUILDDIR) + $(CC) -fPIC -shared -o $(LIBRARY) $(SOURCES) $(CFLAGS) $(LDFLAGS) + +install: + cp $(LIBRARY) $(PREFIX_LIB)/ + ldconfig + +uninstall: + rm $(PREFIX_LIB)/$(LIBRARY_FILENAME) + ldconfig diff --git a/README.md b/README.md index 161f2ca..bf3f393 100644 --- a/README.md +++ b/README.md @@ -1,140 +1,86 @@ -Quick Action -============ +# Introduction -To get started quickly, do the following: - -1: Install Ubuntu 12.04 32 bit on a clean box, just to have a common ground (it should come with the default installation but make sure that gcc is installed) - -2: Download libScarab-1.0.0.zip, gmp-5.1.1.tar.lz, flint-1.6.tgz, mpir-2.6.0.tar.bz2, mpfr-3.1.1.tar.bz2 from hcrypt.com and the linked web pages - -3: Install m4 and lzip - -3.1. sudo apt-get install m4 - -3.2. sudo apt-get install lzip - -4: Install gmp - -4.1. lzip -d gmp-5.1.1.tar.lz - -4.2. tar xf gmp-5.1.1.tar - -4.3. cd gmp-5.1.1 - -4.4. ./configure - -4.5. make - -4.6. make check #(never ever skip the checks!) - -4.7. sudo make install - -5: Install mpfr - -5.1. tar xjf mpfr-3.1.1.tar.bz2 - -5.2. cd mpfr-3.1.1 - -5.3. make - -5.4. make check - -5.5. sudo make install - -6: Install mpir - -6.1. tar xjf mpir-2.6.0.tar.bz2 - -6.2. cd mpir-2.6.0 - -6.3. ./configure - -6.4. make +This is an implementation of a fully homomorphic encryption scheme using large integers. The mathematical foundation is based on work by C. Gentry [1] for the initial concept as well as N. Smart and F. Vercauteren [2] for the integer-based approach used in this implementation. -6.5. make check +The library presented here is beta software and should not be used for any mission critical applications. No warranty expressed or implied is given. -6.6. sudo make install -7: Install flint (ok, this is perhaps quite a bit strange) +# Installation and usage -7.1. tar xf flint-1.6.tgz +First, clone this repository on your machine: git clone https://github.com/blindstore/libScarab.git. -7.2. cd flint-1.6 +## Prerequisites -7.3. source flint_env +The library depends on the following libraries: -7.4. make library +* GMP https://gmplib.org Version 6.0.0 https://gmplib.org/download/gmp/gmp-6.0.0a.tar.lz +* MPFR http://www.mpfr.org Version 3.1.1 https://ftp.gnu.org/gnu/mpfr/mpfr-3.1.1.tar.bz2 +* MPIR http://www.mpir.org Version 2.6.0 http://www.mpir.org/mpir-2.6.0.tar.bz2 +* Flint http://www.flintlib.org Version 1.6 http://www.flintlib.org/flint-1.6.tgz -7.5. sudo cp libflint.so /usr/local/lib +### For Debian-based systems -7.6. sudo cp *.h /usr/local/include +* sudo apt-get install build-essential m4 lzip unzip +* Use ./scripts/install-dependencies.sh from this repository to install the dependencies. -7.7. sudo mkdir -p /usr/local/include/zn_poly/src +### For Arch Linux -7.8. sudo cp zn_poly/include/*.h /usr/local/include/zn_poly/src/ +* Install base-devel m4 unzip gmp mpfr from official repositories. +* Install mpir from AUR. Install version 2.6.0 and pay attention to the comment made by Fincer on 2014-06-20 18:32. +* Install Flint manually: Execute the according part at the bottom of ./scripts/install-dependencies.sh, but before create /etc/ld.so.conf.d/locallib.conf with just one line of content: /usr/local/lib. -8: Run libscarab test +### For Mac OS -8.1. mkdir libscarab +* You can install lzip gmp with brew +* Install the other dependencies using ./scripts/install-dependencies.sh -8.2. cd libscarab +## Build -8.3. unzip ../libScarab-1.0.0.zip +The compilation is done using make. -8.4. make + $ make -8.5. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib +This builds an executable bin/integer-fhe that runs a test of the library and a shared library file in build/libscarab.so. -8.6. ./integer-fhe -ta-daaa! +## Install -Introduction -============ + $ sudo make install -This is an implementation of a fully homomorphic encryption scheme using large integers. The mathematical foundation is based on work by C. Gentry [1] for the initial concept as well as N. Smart and F. Vercauteren [2] for the integer-based approach used in this implementation. +will place ./build/libscarab.so in /usr/local/lib and run ldconfig. -The library presented here is beta software and should not be used for any mission critical applications. No warranty expressed or implied is given. -Installation and usage -====================== +## Usage -Requirements ------------- +You can use the library as demonstrated in test.c. The following methods are exposed: -The following libraries are required for building hcrypt: +``` +fhe_keygen(fhe_pk_t pk, fhe_sk_t sk); // Generate a keypair +fhe_encrypt(mpz_t c, fhe_pk_t pk, int m); // Encrypt a message (0 or 1) +fhe_decrypt(mpz_t c, fhe_sk_t sk); // Decrypt a cyphertext +fhe_recrypt(mpz_t c, fhe_pk_t pk, fhe_sk_t sk); // Recrypt a cyphertext (“refreshing” it) +fhe_add(mpz_t res, mpz_t a, mpz_t b, fhe_pk_t pk); // Add cyphertexts (= XOR) +fhe_mul(mpz_t res, mpz_t a, mpz_t b, fhe_pk_t pk); // Multiply cyphertexts (= AND) +fhe_fulladd(mpz_t sum, mpz_t c_out, mpz_t a, mpz_t b, mpz_t c_in, fhe_pk_t pk); // Add with carry in and carry out +fhe_halfadd(mpz_t sum, mpz_t c_out, mpz_t a, mpz_t b, fhe_pk_t pk); // Add with carry out +``` - GMP: GNU Multiple Precision Arithmetic Library — http://gmplib.org/ - FLINT: Fast Library for Number Theory version 1.6 — http://flintlib.org/flint-1.6.tgz - MPIR: Multiple Precision Integers and Rationals — http://mpir.org/ (required by FLINT) - MPFR — http://www.mpfr.org/ (required by FLINT) +## Uninstall -The library expects the flint headers under in your include directory. Also, copy zn_poly/include/*.h to ./zn_poly/src/ relative to your include path. + $ sudo make uninstall -Building --------- +will remove the library from /usr/local/lib. -The compilation is done using make. +### Uninstall dependencies -$ make +If you used ./scripts/install-dependencies, you can uninstall them using -This builds an integer-fhe executeable in the current directory that runs a test of the library. -Usage + $ ./scripts/uninstall-dependencies -You can use the library as demonstrated in test.c. The following methods are exposed: -function prototype meaning +This will basically call sudo make uninstall for each of GMP, MPFR and MPIR and undo the commands executed for the installation of Flint. -fhe_keygen(fhe_pk_t pk, fhe_sk_t sk); Generate a keypair -fhe_encrypt(mpz_t c, fhe_pk_t pk, int m); Encrypt a message (0 or 1) -fhe_decrypt(mpz_t c, fhe_sk_t sk); Decrypt a cyphertext -fhe_recrypt(mpz_t c, fhe_pk_t pk, fhe_sk_t sk); Recrypt a cyphertext (“refreshing” it) -fhe_add(mpz_t res, mpz_t a, mpz_t b, fhe_pk_t pk); Add cyphertexts (= XOR) -fhe_mul(mpz_t res, mpz_t a, mpz_t b, fhe_pk_t pk); Multiply cyphertexts (= AND) -fhe_fulladd(mpz_t sum, mpz_t c_out, mpz_t a, mpz_t b, mpz_t c_in, fhe_pk_t pk); Add with carry in and carry out -fhe_halfadd(mpz_t sum, mpz_t c_out, mpz_t a, mpz_t b, fhe_pk_t pk); Add with carry out -References -========== +# References [1] C. Gentry, A fully homomorphic encryption scheme @@ -142,12 +88,12 @@ References [3] H. Perl, M. Brenner and M. Smith, POSTER: An Implementation of the Fully Homomorphic Smart-Vercauteren Crypto-System -License -======= + +# License The source code is published under a MIT license: -Copyright © 2011 +Copyright © 2011, 2014 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..7dea76e --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.1 diff --git a/libScarab1.0.0/Makefile b/libScarab1.0.0/Makefile deleted file mode 100644 index 1832ff7..0000000 --- a/libScarab1.0.0/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -CC = gcc -CFLAGS = -Wall -g3 -ggdb -std=c99 -I/usr/local/include -I/usr/local/include/libflint -L/usr/local/lib -LDFLAGS += -lgmp -lflint -SOURCE = $(shell find * -name '*.c') -OBJECTS = $(SOURCE:.c=.o) -.PHONY: clean - -all: integer-fhe - -integer-fhe: $(OBJECTS) - $(CC) -o integer-fhe $^ $(LDFLAGS) - -clean: - rm -f $(OBJECTS) integer-fhe - diff --git a/libScarab1.0.0/VERSION b/libScarab1.0.0/VERSION deleted file mode 100644 index 3eefcb9..0000000 --- a/libScarab1.0.0/VERSION +++ /dev/null @@ -1 +0,0 @@ -1.0.0 diff --git a/libScarab1.0.0/integer-fhe.o b/libScarab1.0.0/integer-fhe.o deleted file mode 100644 index b838829..0000000 Binary files a/libScarab1.0.0/integer-fhe.o and /dev/null differ diff --git a/libScarab1.0.0/main.o b/libScarab1.0.0/main.o deleted file mode 100644 index 0b72546..0000000 Binary files a/libScarab1.0.0/main.o and /dev/null differ diff --git a/libScarab1.0.0/test.o b/libScarab1.0.0/test.o deleted file mode 100644 index a267de5..0000000 Binary files a/libScarab1.0.0/test.o and /dev/null differ diff --git a/libScarab1.0.0/types.o b/libScarab1.0.0/types.o deleted file mode 100644 index 2ef5104..0000000 Binary files a/libScarab1.0.0/types.o and /dev/null differ diff --git a/libScarab1.0.0/util.o b/libScarab1.0.0/util.o deleted file mode 100644 index d9807e8..0000000 Binary files a/libScarab1.0.0/util.o and /dev/null differ diff --git a/scripts/install-dependencies.sh b/scripts/install-dependencies.sh new file mode 100755 index 0000000..f73b7a3 --- /dev/null +++ b/scripts/install-dependencies.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +set -e + +# +# Download and build dependencies +# + +mkdir -p lib +cd lib + +# Download libs + +if [ ! -f "gmp-6.0.0a.tar.lz" ]; then + wget https://gmplib.org/download/gmp/gmp-6.0.0a.tar.lz +fi + +if [ ! -f "flint-1.6.tgz" ]; then + wget http://www.flintlib.org/flint-1.6.tgz +fi + +if [ ! -f "mpir-2.6.0.tar.bz2" ]; then + wget http://www.mpir.org/mpir-2.6.0.tar.bz2 +fi + +if [ ! -f "mpfr-3.1.1.tar.bz2" ]; then + wget https://ftp.gnu.org/gnu/mpfr/mpfr-3.1.1.tar.bz2 +fi + +# Install GMP + +if [ ! -d "gmp-6.0.0" ]; then + lzip -d gmp-6.0.0a.tar.lz + tar xf gmp-6.0.0a.tar +fi + +cd gmp-6.0.0 +./configure +make +make check +sudo make install +sudo ldconfig +cd .. + +# Install mpfr + +if [ ! -d "mpfr-3.1.1" ]; then + tar xjf mpfr-3.1.1.tar.bz2 +fi + +cd mpfr-3.1.1 +./configure +make +make check +sudo make install +sudo ldconfig +cd .. + +# Install mpir + +if [ ! -d "mpir-2.6.0" ]; then + tar xjf mpir-2.6.0.tar.bz2 +fi + +cd mpir-2.6.0 +./configure +make +make check +sudo make install +sudo ldconfig +cd .. + +# Install flint (ok, this is perhaps quite a bit strange) + +if [ ! -d "flint-1.6" ]; then + tar xzf flint-1.6.tgz +fi + +cd flint-1.6 +. flint_env +make library +sudo cp libflint.so /usr/local/lib +sudo cp *.h /usr/local/include +sudo mkdir -p /usr/local/include/zn_poly/src +sudo cp zn_poly/include/*.h /usr/local/include/zn_poly/src/ +sudo ldconfig +cd .. + diff --git a/scripts/uninstall-dependencies.sh b/scripts/uninstall-dependencies.sh new file mode 100755 index 0000000..c65e2a9 --- /dev/null +++ b/scripts/uninstall-dependencies.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +set -e + +# +# Download and build dependencies +# + +cd lib + +# Uninstall GMP + +cd gmp-6.0.0 +sudo make uninstall +cd .. + +# Uninstall mpfr + +cd mpfr-3.1.1 +sudo make uninstall +cd .. + +# Uninstall mpir + +cd mpir-2.6.0 +sudo make uninstall +cd .. + +# Uninstall flint + +cd flint-1.6 +sudo rm /usr/local/lib/libflint.so +dir=$(pwd) +cd /usr/local/include +sudo rm d_mat.h flint.h F_mpn_mul-tuning.h fmpz.h F_mpz.h F_mpz_LLL.h F_mpz_mat.h F_mpzmod_mat.h F_mpz_mod_poly.h fmpz_montgomery.h F_mpz_mpoly.h fmpz_poly.h F_mpz_poly.h F_zmod_mat.h long_extras.h longlong.h longlong_wrapper.h memory-manager.h mpfr_mat.h mpn_extras.h mpq_mat.h mpz_extras.h mpz_mat.h mpz_poly.h mpz_poly-tuning.h NTL-interface.h packed_vec.h profiler.h profiler-main.h test-support.h theta.h ZmodF.h ZmodF_mul.h ZmodF_mul-tuning.h ZmodF_poly.h zmod_mat.h zmod_poly.h +cd $dir +sudo rm -rf /usr/local/include/zn_poly/src +cd .. + +sudo ldconfig diff --git a/libScarab1.0.0/integer-fhe.c b/src/integer-fhe.c similarity index 100% rename from libScarab1.0.0/integer-fhe.c rename to src/integer-fhe.c diff --git a/libScarab1.0.0/integer-fhe.h b/src/integer-fhe.h similarity index 100% rename from libScarab1.0.0/integer-fhe.h rename to src/integer-fhe.h diff --git a/libScarab1.0.0/integer-fhe.nb b/src/integer-fhe.nb similarity index 100% rename from libScarab1.0.0/integer-fhe.nb rename to src/integer-fhe.nb diff --git a/libScarab1.0.0/main.c b/src/main.c similarity index 100% rename from libScarab1.0.0/main.c rename to src/main.c diff --git a/libScarab1.0.0/parameters.h b/src/parameters.h similarity index 100% rename from libScarab1.0.0/parameters.h rename to src/parameters.h diff --git a/libScarab1.0.0/test.c b/src/test.c similarity index 100% rename from libScarab1.0.0/test.c rename to src/test.c diff --git a/libScarab1.0.0/test.h b/src/test.h similarity index 100% rename from libScarab1.0.0/test.h rename to src/test.h diff --git a/libScarab1.0.0/types.c b/src/types.c similarity index 100% rename from libScarab1.0.0/types.c rename to src/types.c diff --git a/libScarab1.0.0/types.h b/src/types.h similarity index 100% rename from libScarab1.0.0/types.h rename to src/types.h diff --git a/libScarab1.0.0/util.c b/src/util.c similarity index 100% rename from libScarab1.0.0/util.c rename to src/util.c diff --git a/libScarab1.0.0/util.h b/src/util.h similarity index 100% rename from libScarab1.0.0/util.h rename to src/util.h