Linux下HElib库安装记录

1.HElib库简介

  • HElib是一个实现全同态加密的软件库,开发语言是C++,是根据Brakerski,Gentry,Vaikuntanathan(BGV)的全同态方案实现。HElib仍然是一个研究性质的项目。在现阶段,这个库主要面向研究HE及其用途的研究人员。目前它还相当低级,最好把它看成是“面向HE(homomorphic encryption)的汇编语言”。
  • 项目地址:https://github.com/homenc/HElib
  • 相关论文:Halevi S , Shoup V . Algorithms in HElib[J]. Lecture Notes in Computer Science, 2014, 8616:554-571.

2.安装步骤

第一步:下载代码并解压。

  • 解压后如下所示:
[07:52:34][root@dl]19:52:34/home/HK_Workplace/SomeLib/HElib# ls
changes.md  cmake  CMakeLists.txt  dependencies  doc  Dockerfile  DOCKER_USAGE.md  Doxyfile  example_program  INSTALL.md  issue_template.md  LICENSE  mainpage.dox  NOTICE  OLD_INSTALL.txt  README.md  src  TESTS.md

第二步:安装依赖软件。

  • 打开INSTALL.md文件,查看安装说明,其中安装HElib所依赖的软件为:
cmake >= 3.5.1
GNU make
g++ >= 5.4.0 or clang >= 3.8
pthreads
git (if you want to build the tests)
  • 其中cmake可以根据CMakeLists.txt自动生成makefile编译文件;make是执行makefile文件所需的工具;g++或clang为c++编译器;pthreads是多线程编译所需要的;git这里其实不是很需要(如果后面安装过程中选择了DENABLE_TEST=ON的话,会在make test一步中利用git安装google c++测试框架,这一步并不是必须的)。

第三步:配置HElib所需环境。

  • 我们这里选择INSTALL.md文件介绍的第二种安装方法:library build,这种方法需要自己手动配置环境。另一种方法是:package build,这种方法可以自动安装所需环境,但是不幸的是,笔者使用这种方法安装失败了。
  • HElib所需的环境为:GMP和NTL库。其中GMP库的安装可以参考笔者的另一篇文章:Charm-crypto库安装记录。安装好GMP库后,安装NTL库。NTL库安装也很简单,下载代码解压后(地址:https://www.shoup.net/ntl/download.html
    ,选择Unix版本,并且版本号 NTL>=11.0.0),cd进入ntl-11.4.1/src文件夹(笔者下载的是11.4.1版本),依次输入命令:
./configure
make
make check
sudo make install

即可安装好。安装好后,进入/usr/local/lib文件夹中查看,显示有相关的库,如下所示,有libgmp.a,libntl.a文件,即可认为安装成功。

[08:43:49][root@dl]20:43:49/usr/local/lib# ls
libgmp.a  libgmp.la  libgmp.so  libgmp.so.10  libgmp.so.10.3.2  libntl.a  libpbc.a  libpbc.la  libpbc.so  libpbc.so.1  libpbc.so.1.0.0

第四步,按照INSTALL.md文件介绍的library build安装方法进行安装:

  • 首先需要建立一个build文件夹:
cd HElib
mkdir build
cd build
  • 之后需要利用Cmake生成makefile文件:确认处于HElib文件夹下,在HElib文件下输入命令:
cmake -DGMP_DIR="${GMPDIR}" -DNTL_DIR="${NTLDIR}" -DCMAKE_INSTALL_PREFIX=/usr/local
  • 其中,-DGMP_DIR="${GMPDIR}" -DNTL_DIR="${NTLDIR}","${GMPDIR}"和"${NTLDIR}"会指定去哪里寻找NTL和GMP库(这里我们就不画蛇添足,把具体的路径写上去了,笔者曾经尝试写了进去,安装失败),系统会默认去一些文件夹寻找。

  • DCMAKE_INSTALL_PREFIX=/usr/local是我们要安装库所在的地址前缀,这里我选了/usr/local,这样就可以把HElib库安装在/usr/local/include下,编程时正好可以include进去。如果需要自己指定安装目录也可以,只需修改/usr/local为指定安装目录即可,这样的话在编译时需要指定头文件地址(g++里的-I参数)。
    后面还可以添加选项-DENABLE_TEST=ON,这样可以对安装进行测试(笔者这里不推荐)。生成的makefile文件如下图:


    生成makefile文件
  • 之后根据makefile文件进行编译,输入命令:

make -j16

-j16是指定线程数,是一个可选选项。

  • 我们跳过INSTALL.md文件所说的第四步make test(这一步是第二步选择-DENABLE_TEST=ON后才可以的。不过会一直卡在安装google c++测试框架上面),直接输入在HElib文件夹下输入:
make install

之后cd进入/usr/local/lib文件夹,显示有libhelib.a文件,cd进入/usr/local/include文件夹,显示有helib库,即可认为安装成功。如下所示:

[09:25:51][root@dl]21:25:51/usr/local/lib# ls
libgmp.a  libgmp.la  libgmp.so  libgmp.so.10  libgmp.so.10.3.2  libhelib.a  libntl.a  libpbc.a  libpbc.la  libpbc.so  libpbc.so.1  libpbc.so.1.0.0  python3.5
[09:29:44][root@dl]21:29:44/usr/local/include# ls
gmp.h  helib  NTL  pbc

3.测试

  • 测试文件main.cpp如下:

#include <helib/FHE.h>
#include <helib/EncryptedArray.h>


int main(int argc, char *argv[]) {
  /*  Example of BGV scheme  */
  
  // Plaintext prime modulus
  unsigned long p = 4999;
  // Cyclotomic polynomial - defines phi(m)
  unsigned long m = 32109;
  // Hensel lifting (default = 1)
  unsigned long r = 1;
  // Number of bits of the modulus chain
  unsigned long bits = 300;
  // Number of columns of Key-Switching matix (default = 2 or 3)
  unsigned long c = 2;
  
  std::cout << "Initialising context object..." << std::endl;
  // Intialise context
  FHEcontext context(m, p, r);
  // Modify the context, adding primes to the modulus chain
  std::cout  << "Building modulus chain..." << std::endl;
  buildModChain(context, bits, c);

  // Print the context
  context.zMStar.printout();
  std::cout << std::endl;
  
  // Print the security level
  std::cout << "Security: " << context.securityLevel() << std::endl;
  
  // Secret key management
  std::cout << "Creating secret key..." << std::endl;
  // Create a secret key associated with the context
  FHESecKey secret_key(context);
  // Generate the secret key
  secret_key.GenSecKey();
  std::cout << "Generating key-switching matrices..." << std::endl;
  // Compute key-switching matrices that we need
  addSome1DMatrices(secret_key);
  
  // Public key management
  // Set the secret key (upcast: FHESecKey is a subclass of FHEPubKey)
  const FHEPubKey& public_key = secret_key;
  
  // Get the EncryptedArray of the context
  const EncryptedArray& ea = *(context.ea);
  
  // Get the number of slot (phi(m))
  long nslots = ea.size();
  std::cout << "Number of slots: " << nslots << std::endl;
  
  // Create a vector of long with nslots elements
  std::vector<long> ptxt(nslots);
  // Set it with numbers 0..nslots - 1
  for (int i = 0; i < nslots; ++i) {
    ptxt[i] = i;
  }
  // Print the plaintext
  std::cout << "Initial Ptxt: " << ptxt << std::endl;
  
  // Create a ciphertext
  Ctxt ctxt(public_key);
  // Encrypt the plaintext using the public_key
  ea.encrypt(ctxt, public_key, ptxt);
  
  // Square the ciphertext
  ctxt *= ctxt;
  // Double it (using additions)
  ctxt += ctxt;
  
  // Create a plaintext for decryption
  std::vector<long> decrypted(nslots);
  // Decrypt the modified ciphertext
  ea.decrypt(ctxt, secret_key, decrypted);
  
  // Print the decrypted plaintext
  std::cout << "Decrypted Ptxt: " << decrypted << std::endl;
  
  return 0;
}
  • 编译命令:
g++  -std=c++11 -pthread -g -O2   -DFHE_THREADS -fmax-errors=4  -o app1 main.cpp -lhelib  -lntl -lgmp -lm

编译生成可执行文件app1。其中:-lhelib -lntl -lgmp -lm即告诉编译器需要将helib,ntl,gmp,m四个库一起加入编译(helib要放在前面,否则会报错)。

  • 执行app1,输入./app1即可,结果:
Initialising context object...
Building modulus chain...
m = 32109, p = 4999, phi(m) = 16560
  ord(p)=690
  generator 320 has order (== Z_m^*) of 6
  generator 3893 has order (== Z_m^*) of 2
  generator 14596 has order (== Z_m^*) of 2
  T = [1 14596 3893 21407 320 14915 25618 11023 6073 20668 9965 27479 16820 31415 10009 27523 20197 2683 24089 9494 9131 23726 2320 19834 ]

Security: 127.626
Creating secret key...
Generating key-switching matrices..
Number of slots: 24
Initial ptxt:[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
Decrypted Ptxt: [0 2 8 18 32 50 72 98 128 162 200 242 288 338 392 450 512 578 648 722 800 882 968 1058]

正常运行,测试结束。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容