Linux下HElib库安装记录

2019-12-22  本文已影响0人  不懂球的2大业

1.HElib库简介

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

第二步:安装依赖软件。

cmake >= 3.5.1
GNU make
g++ >= 5.4.0 or clang >= 3.8
pthreads
git (if you want to build the tests)

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

./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安装方法进行安装:

cd HElib
mkdir build
cd build
cmake -DGMP_DIR="${GMPDIR}" -DNTL_DIR="${NTLDIR}" -DCMAKE_INSTALL_PREFIX=/usr/local
make -j16

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

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.测试


#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要放在前面,否则会报错)。

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]

正常运行,测试结束。

上一篇下一篇

猜你喜欢

热点阅读