安装 GMP、NTL、CTMalloc ,编译 OpenFHE

2023-10-29 15:36

本文主要是介绍安装 GMP、NTL、CTMalloc ,编译 OpenFHE,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考文献:

  1. [ABB+22] Al Badawi A, Bates J, Bergamaschi F, et al. Openfhe: Open-source fully homomorphic encryption library[C]//Proceedings of the 10th Workshop on Encrypted Computing & Applied Homomorphic Cryptography. 2022: 53-63.
  2. openfheorg/openfhe-development
  3. Welcome to OpenFHE’s documentation!
  4. 在Ubuntu上安装NTL库以及编译测试
  5. Ubuntu上安装NTL库
  6. 内存优化-如何使用tcmalloc来提升内存性能?
  7. 在Ubuntu上安装Bazel
  8. Google Performance Tools安装以及使用
  9. Git安装教程以及连接Github
  10. GMP:代码,文档
  11. gf2x:代码,文档
  12. NTL:代码,文档
  13. ctmalloc:代码,文档
  14. bazel:代码,文档

文章目录

  • 安装 GMP
  • 安装 gf2x
  • 安装 NTL
  • 安装 Bazel
  • 安装 ctmalloc
  • 编译 OpenFHE
  • FHE 样例

我使用的是 WSL 子系统,

  • 操作系统版本:Ubuntu 22.04.2 LTS,位数 x86_64
  • GMP 版本:6.2.1
  • NTL 版本:11.5.1

安装 GMP

下载 GMP 6.2.1 版本(必须和 NTL 11.5.1 匹配,如果冲突报错了,在你安装的源码目录 gmp-6.3.0 下执行 sudo make uninstall 卸载),解压并进入 gmp-6.2.1 文件夹,

./configure --prefix=$HOME/gmpmakemake checksudo make install

如果 prefix 不指定安装的目录,库文件 lib 会产生在 /usr/local/lib,头文件在 /usr/local/include。我没有指定,让它默认安装到 /usr/local 内。

安装结果:

----------------------------------------------------------------------
Libraries have been installed in:/usr/local/libIf you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:- add LIBDIR to the 'LD_LIBRARY_PATH' environment variableduring execution- add LIBDIR to the 'LD_RUN_PATH' environment variableduring linking- use the '-Wl,-rpath -Wl,LIBDIR' linker flag- have your system administrator add LIBDIR to '/etc/ld.so.conf'See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------/usr/bin/mkdir -p '/usr/local/include'/usr/bin/install -c -m 644 gmp.h '/usr/local/include'/usr/bin/mkdir -p '/usr/local/lib/pkgconfig'/usr/bin/install -c -m 644 gmp.pc '/usr/local/lib/pkgconfig'
make  install-data-hook
make[4]: Entering directory '/mnt/e/ThirdPartyLibrary/GMP/gmp-6.2.1'

安装 gf2x

如果源码文件夹下没有 ./configure,但有 configure.amconfigure.in,我们用 autoconf 命令来生成。

  1. 我们先安装 autoconf 工具,

    sudo apt-get install autoconf
    
  2. 下载 gf2x,我们下载 .tar.gz 版本的压缩文件,并在 Linux 下解压(在 Windows 下解压 .zip 做编译时会报错,因为 Linux 下的软链接被翻译成了形如 ../../crypto/opensslconf.h 的一行代码形式),

    gunzip gf2x-master.tar.gztar xf gf2x-master.tarcd gf2x-master
    
  3. 我们先执行如下指令(直接执行 autoreconf --install./configure 会有找不到 Makefile.in 文件的报错),

    aclocallibtoolize --forceautomake --add-missingautoconf
    
  4. 然后生成 ./configure 文件,

    autoreconf --install
    
  5. 现在可以安装 gf2x 了,

    ./configuremakemake checksudo make install
    

安装结果:

Libraries have been installed in:/home/wqf/sw/libIf you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:- add LIBDIR to the 'LD_LIBRARY_PATH' environment variableduring execution- add LIBDIR to the 'LD_RUN_PATH' environment variableduring linking- use the '-Wl,-rpath -Wl,LIBDIR' linker flag- have your system administrator add LIBDIR to '/etc/ld.so.conf'See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

安装 NTL

安装好 GMP 之后再编译 NTL,计算效率快得多,

For this to work, GMP must already be installed (most Unix distributions already come with GMP installed, but see this page for more details). If you really do not want to use GMP, you can pass the option NTL_GMP_LIP=off to configure; however, NTL will run significantly faster with GMP, so this is strongly discouraged.

安装好 gf2x 之后再编译 NTL,使得 GF(2) 的效率快得多,

If you want very high-performance for polynomial arithmetic over GF(2), you may want to consider using the gf2x library. To do this, gf2x must already be installed. In addition, you should invoke configure with the option NTL_GF2X_LIB=on.

  1. 下载最新版本的 NTL,我们下载 ntl-11.5.1.tar.gz 压缩文件,在 Linux 下解压

    gunzip ntl-11.5.1.tar.gztar xf ntl-11.5.1.tarcd ntl-11.5.1/src/
    
  2. 默认配置了 GMP,我们额外配置 gf2x

    ./configure PREFIX=/usr/local NTL_GF2X_LIB=on GF2X_PREFIX=/usr/local
    
  3. 编译

    makemake checksudo make install
    
  4. 如果执行 make check 时找不到 .so 的软连接,删除后使用绝对路径重新配置下,

    sudo rm libgf2x.so.4sudo rm libgf2x.sosudo ln -s /usr/local/lib/libgf2x.so.4.0.0 /usr/local/lib/libgf2x.so.4sudo ln -s /usr/local/lib/libgf2x.so.4.0.0 /usr/local/lib/libgf2x.sosudo ldconfig
    

安装结果:

mkdir -p -m 755 /usr/local/include
rm -rf /usr/local/include/NTL
mkdir -m 755 /usr/local/include/NTL
cp -p ../include/NTL/*.h /usr/local/include/NTL
chmod -R a+r /usr/local/include/NTL
mkdir -p -m 755 /usr/local/share/doc
rm -rf /usr/local/share/doc/NTL
mkdir -m 755 /usr/local/share/doc/NTL
cp -p ../doc/*.txt /usr/local/share/doc/NTL
cp -p ../doc/*.html /usr/local/share/doc/NTL
cp -p ../doc/*.gif /usr/local/share/doc/NTL
chmod -R a+r /usr/local/share/doc/NTL
mkdir -p -m 755 /usr/local/lib
cp -p ntl.a /usr/local/lib/libntl.a #LSTAT
chmod a+r /usr/local/lib/libntl.a #LSTAT

安装 Bazel

Bazel 是 Google 开源的编译构建工具,以 Monolithic Repository 为理念。与 makefile & CMake 不同,Bazel 另起炉灶,采用 client/server 运行模式,为云编译而生。Bazel 工具将编译过程分三个阶段:Load Phase/Analysis Phase/Execution phase。研发人员实现 workspace/build/.bzl 三种文件,Bazel 执行这些文件生成 action graph,执行 action 来构建项目。

下载 bazel,我们下载 installer-linux-x86_64.sh 二进制安装程序,

chmod +x bazel-6.4.0-installer-linux-x86_64.shsudo cp ./bazel-6.4.0-installer-linux-x86_64.sh /usr/local/bin/cd /usr/local/bin/./bazel-6.4.0-installer-linux-x86_64.sh

安装结果:

Bazel is now installed!Make sure you have "/usr/local/bin" in your path.For bash completion, add the following line to your ~/.bashrc:source /usr/local/lib/bazel/bin/bazel-complete.bashFor fish shell completion, link this file into your
/root/.config/fish/completions/ directory:ln -s /usr/local/lib/bazel/bin/bazel.fish /root/.config/fish/completions/bazel.fishSee http://bazel.build/docs/getting-started.html to start a new project!

使用 Bazel 编译 ctmalloc 源码时,它总是去链接 https://github.com/protocolbuffers/protobuf/archive/13d559beb6967033a467a7517c35d8ad970f8afb.zip 下载 com_google_protobuf,但是报错 Connection refused。明明用浏览器可以打开这个网址啊!无语。。。

安装 ctmalloc

ctmalloc 是一个高效管理内存的工具包,需要使用 bazel 来编译源码,但它连不上 github,编译不了!!!我们通过安装 gperftools 来实现 ctmalloc 的安装。

TCMalloc (Thread-Caching Malloc) 与标准 glibc 库的 malloc 实现一样的功能,但是 TCMalloc 在效率和速度效率都比标准 malloc 高很多。TCMalloc 是 google-perftools 工具中的一个(四个工具分别是:TCMalloc、heap-checker、heap-profiler 和 cpu-profiler)。

  1. 下载 libunwind(追踪函数调用栈),源码安装

    autoreconf --force -v --install./configuremakesudo make install
    
  2. 下载 gperftools(CPU 性能分析器),源码安装

    autoreconf --force -v --install./configuremakesudo make install
    
  3. 最后把 ctmalloc 加载到 Linux 系统中,

    suecho '/usr/local/lib' > /etc/ld.so.conf.d/local.conf/sbin/ldconfigexit
    

编译 OpenFHE

  1. 在 https://github.com/openfheorg/openfhe-development 下载 OpenFHE 的源代码,解压得到 openfhe-development-main

  2. 递归下载依赖的第三方代码(本来是空文件夹),

    cd ./third-partygit initgit submodule update --init --recursive
    
  3. 我们配置 ./CMakeLists 文件,打开 WITH_NTL, WITH_TCM 等优化选项(如果你没有安装它们,就不要打开),构造出 Makefile 文件,

    mkdir buildcd buildcmake .. BUILD_EXTRAS=ON WITH_NTL=ON WITH_TCM=ON
    
  4. OpenFHE 的源码文件很大,我们启用多线程 ,

    make -j 16
    
  5. 根据需求可以安装到系统中(在 ./build 下执行 sudo make uninstall 即可卸载),

    make install
    

现在我们根据 ./unitest 中的三个测试代码,做正确性测试,

make testall

运行了好长时间后(主要是 pke1489 cases 很慢),得到的测试结果,

-- demoData folder already exists
[  0%] Built target third-party
[ 21%] Built target coreobj
[ 21%] Built target OPENFHEcore
[ 26%] Built target binfheobj
[ 28%] Built target OPENFHEbinfhe
[ 34%] Built target binfhe_tests
[ 48%] Built target core_tests
[ 80%] Built target pkeobj
[ 80%] Built target OPENFHEpke
[100%] Built target pke_tests
core:
Testing Backends: 4 Native
****** OpenFHE Version 1.1.1
****** Date 2023-10-29T13:55:17
****** End 159 cases 159 passed 0 failed
pke:
Testing Backends: 4 Native
****** OpenFHE Version 1.1.1
****** Date 2023-10-29T13:55:38
****** End 1489 cases 1489 passed 0 failed
binfhe:
Testing Backends: 4 Native
****** OpenFHE Version 1.1.1
****** Date 2023-10-29T14:24:47
****** End 84 cases 84 passed 0 failed
[100%] Built target testall

FHE 样例

测试 BFV 方案,执行 bin/examples/pke/simple-integers

Plaintext #1: ( 1 2 3 4 5 6 7 8 9 10 11 12 ... )
Plaintext #2: ( 3 2 1 4 5 6 7 8 9 10 11 12 ... )
Plaintext #3: ( 1 2 5 2 5 6 7 8 9 10 11 12 ... )Results of homomorphic computations
#1 + #2 + #3: ( 5 6 9 10 15 18 21 24 27 30 33 36 ... )
#1 * #2 * #3: ( 3 8 15 32 125 216 343 512 729 1000 1331 1728 ... )
Left rotation of #1 by 1: ( 2 3 4 5 6 7 8 9 10 11 12 ... )
Left rotation of #1 by 2: ( 3 4 5 6 7 8 9 10 11 12 ... )
Right rotation of #1 by 1: ( 0 1 2 3 4 5 6 7 8 9 10 11 ... )
Right rotation of #1 by 2: ( 0 0 1 2 3 4 5 6 7 8 9 10 ... )

测试 CKKS 方案,执行 bin/examples/pke/simple-real-numbers

CKKS scheme is using ring dimension 16384Input x1: (0.25, 0.5, 0.75, 1, 2, 3, 4, 5,  ... ); Estimated precision: 50 bitsInput x2: (5, 4, 3, 2, 1, 0.75, 0.5, 0.25,  ... ); Estimated precision: 50 bitsResults of homomorphic computations:
x1 = (0.25, 0.5, 0.75, 1, 2, 3, 4, 5,  ... ); Estimated precision: 43 bits
Estimated precision in bits: 43
x1 + x2 = (5.25, 4.5, 3.75, 3, 3, 3.75, 4.5, 5.25,  ... ); Estimated precision: 43 bits
Estimated precision in bits: 43
x1 - x2 = (-4.75, -3.5, -2.25, -1, 1, 2.25, 3.5, 4.75,  ... ); Estimated precision: 43 bits4 * x1 = (1, 2, 3, 4, 8, 12, 16, 20,  ... ); Estimated precision: 41 bitsx1 * x2 = (1.25, 2, 2.25, 2, 2, 2.25, 2, 1.25,  ... ); Estimated precision: 42 bitsIn rotations, very small outputs (~10^-10 here) correspond to 0's:
x1 rotate by 1 = (0.5, 0.75, 1, 2, 3, 4, 5, 0.25,  ... ); Estimated precision: 43 bitsx1 rotate by -2 = (4, 5, 0.25, 0.5, 0.75, 1, 2, 3,  ... ); Estimated precision: 43 bits

测试 TFHE 方案,执行 bin/examples/binfhe/boolean-truth-tables

Generate cryptocontext
Finished generating cryptocontext
Generating the bootstrapping keys...
Completed the key generation.1 NAND 1 = 0
1 NAND 0 = 1
0 NAND 0 = 1
0 NAND 1 = 11 AND 1 = 1
1 AND 0 = 0
0 AND 0 = 0
0 AND 1 = 01 OR 1 = 1
1 OR 0 = 1
0 OR 0 = 0
0 OR 1 = 11 NOR 1 = 0
1 NOR 0 = 0
0 NOR 0 = 1
0 NOR 1 = 01 XOR 1 = 0
1 XOR 0 = 1
0 XOR 0 = 0
0 XOR 1 = 11 XNOR 1 = 1
1 XNOR 0 = 0
0 XNOR 0 = 1
0 XNOR 1 = 01 XOR_FAST 1 = 0
1 XOR_FAST 0 = 1
0 XOR_FAST 0 = 0
0 XOR_FAST 1 = 11 XNOR_FAST 1 = 1
1 XNOR_FAST 0 = 0
0 XNOR_FAST 0 = 1
0 XNOR_FAST 1 = 0

这篇关于安装 GMP、NTL、CTMalloc ,编译 OpenFHE的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/301616

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

Java SWT库详解与安装指南(最新推荐)

《JavaSWT库详解与安装指南(最新推荐)》:本文主要介绍JavaSWT库详解与安装指南,在本章中,我们介绍了如何下载、安装SWTJAR包,并详述了在Eclipse以及命令行环境中配置Java... 目录1. Java SWT类库概述2. SWT与AWT和Swing的区别2.1 历史背景与设计理念2.1.

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

Pytorch介绍与安装过程

《Pytorch介绍与安装过程》PyTorch因其直观的设计、卓越的灵活性以及强大的动态计算图功能,迅速在学术界和工业界获得了广泛认可,成为当前深度学习研究和开发的主流工具之一,本文给大家介绍Pyto... 目录1、Pytorch介绍1.1、核心理念1.2、核心组件与功能1.3、适用场景与优势总结1.4、优

conda安装GPU版pytorch默认却是cpu版本

《conda安装GPU版pytorch默认却是cpu版本》本文主要介绍了遇到Conda安装PyTorchGPU版本却默认安装CPU的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录一、问题描述二、网上解决方案罗列【此节为反面方案罗列!!!】三、发现的根本原因[独家]3.1 p

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.