密码学libnum和gmpy2模块的安装

终于有空来整理一下安装教程,顺便给新人留点参考资料,避免踩坑。

libnum

libnum库是一个关于各种数学运算的函数库,它包含common maths、modular、modular squre roots、primes、factorization、ECC、converting、stuff等方面的函数,结合gmpy2库PyCrypto库一起来使用会使计算变得非常简便。

安装

1
2
3
git clone https://github.com/hellman/libnum
cd libnum
python setup.py install

常用的Converting

数字型(不论是十六进制还是十进制)与字符串之间的转换:

1
2
3
4
import libnum
s="flag{52Hertz_is_caiji}"
print(libnum.s2n(s))
# 38321129010630712848744725072051706353616984535099773
1
2
3
4
import libnum
n=0x666c61677b3532486572747a5f69735f6361696a697d
print libnum.n2s(n)
#这个转换不用在意十六进制的位数是否为偶数

二进制与字符串之间的转换:

1
2
3
import libnum
s='52Hertz'
print(libnum.s2b(s))
1
2
3
4
import libnum
b='00110101001100100100100001100101011100100111010001111010'
print libnum.b2s(b)
#二进制的位数最好是8的倍数

质数&因数分解

生成质数:

1
print(libnum.generate_prime(1024))

因数分解:

1
print(libnum.factorize(1024))

gmpy2

GMP(GNU Multiple Precision Arithmetic Library,即GNU高精度算术运算库),它是一个开源的高精度运算库,其中不但有普通的整数、实数、浮点数的高精度运算,还有随机数生成,尤其是提供了非常完备的数论中的运算接口,比如Miller-Rabin素数测试算法、大素数生成、欧几里德算法、求域中元素的逆、Jacobi符号、legendre符号等。
gmpy2是Python的一个扩展库,是对GMP的封装,它的前身是gmpy,经过其作者的调整和封装,使得gmpy2的使用大大简化。

Windows上安装

在windows上直接安装wheel文件就方便多了。

https://pypi.org/project/gmpy2/#files

这里面有python2.6、2.7、3.2、3.3、3.4版本的wheel文件,下载后用pip安装即可。

如果有高版本的可以参考 Github项目,或者 Here

wheel

先检查一下是否安装了wheel文件包,在cmd中输入wheel,查看一下,如果没有安装,则输入安装:

1
pip install wheel

下载对应whl文件

以我自己的环境为例,我下载的是 gmpy2‑2.0.8‑cp37‑cp37m‑win32.whl

whl文件包需要和你所安装的python3版本一致,建议这个文件下载后放到python文件目录下。

安装

打开cmd,输入 pip install [whl文件的绝对路径] 安装whl文件包

以我为例:

1
pip install C:\Users\mcwin\AppData\Local\Programs\Python\Python37-32\gmpy2-2.0.8-cp37-cp37m -win32.whl

使用

然后输入 import gmpy2 即可进行使用

Linux上安装

gmpy2是依赖GMP、MPFR、MPC三个库,故此在linux上安装前得先安装这3个库。

为了后续安装的方便,先建立2个文件夹。

1
2
mkdir -p $HOME/src
mkdir -p $HOME/static

测试有没有安装m4模块:

1
man m4

如果出现No manual entry for m4,就说明没有安装m4模块。

如果没安装m4模块,在编译GMP时候会报错checking for suitable m4... configure: error: No usable m4 in $PATH or /usr/5bin (see config.log for reasons).

安装m4

GNU M4 is an implementation of the traditional Unix macro processor. It is mostly SVR4 compatible although it has some extensions (for example, handling more than 9 positional parameters to macros). GNU M4 also has built-in functions for including files, running shell commands, doing arithmetic, etc.

1.4.18 版本为例

1
2
3
4
5
6
v=1.4.18
cd $HOME/src
wget http://ftp.gnu.org/gnu/m4/m4-${v}.tar.gz
tar xf m4-${v}.tar.gz && cd m4-${v}
./configure -prefix=/usr/local
make && make check && make install

安装GMP

GMP(The GNU Multiple Precision Arithmetic Library) is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.
https://gmplib.org/

6.1.2 版本为例

1
2
3
4
5
6
v=6.1.2
cd $HOME/src
wget https://gmplib.org/download/gmp/gmp-${v}.tar.bz2
tar -jxvf gmp-${v}.tar.bz2 && cd gmp-${v}
./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic
make && make check && make install

安装MPFR

The MPFR library is a C library for multiple-precision floating-point computations with correct rounding.
http://www.mpfr.org/mpfr-current/#download
4.0.1 为例 (请自己访问官网,替换成最新的版本号)

1
2
3
4
5
6
v=4.0.1
cd $HOME/src
wget http://www.mpfr.org/mpfr-current/mpfr-${v}.tar.bz2
tar -jxvf mpfr-${v}.tar.bz2 && cd mpfr-${v}
./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static
make && make check && make install

如果mpfr.org下载太慢,可以换为

1
wget http://ftp.gnu.org/gnu/mpfr/mpfr-${v}.tar.bz2

安装MPC

GNU MPC is a C library for the arithmetic of complex numbers with arbitrarily high precision and correct rounding of the result.
http://www.multiprecision.org/mpc/download.html (这里最新是1.0.3)
但当mpfr版本为4.x以上会报错Makefile:532: recipe for target ‘mul.lo’ failed

ftp://ftp.gnu.org/gnu/mpc/ 可以找到更新的1.1.0版本

1
2
3
4
5
6
v=1.1.0
cd $HOME/src
wget ftp://ftp.gnu.org/gnu/mpc/mpc-${v}.tar.gz
tar -zxvf mpc-${v}.tar.gz && cd mpc-${v}
./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static --with-mpfr=$HOME/static
make && make check && make install

安装gmpy2

github项目:https://github.com/aleaxit/gmpy

现在新的版本(2-2.1.0b1以上版本)在执行python setup.py build_ext –static=$HOME/static install

会报错error: option –static must not have an argument

解决法子1:

releases版本(2-2.1.0a1以下版本)来安装

1
2
3
4
5
v=2-2.1.0a1
cd $HOME/src
wget https://github.com/aleaxit/gmpy/releases/download/gmpy${v}/gmpy${v}.tar.gz
tar xf gmpy${v}.tar.gz && cd gmpy${v}
python setup.py build_ext --static=$HOME/static install

解决法子2:

因为新版本的setup.py修改了不少,故得采用以下法子:

1
python setup.py build_ext --static-dir=$HOME/static install

安装后,命令行进入python模式后,输入import gmpy2没报错就成功了。

如果使用wget下载时候一直卡在:

Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.227.24|:443...

可以尝试:

1
2
echo 52.216.16.16 github-production-release-asset-2e65be.s3.amazonaws.com >>/etc/hosts
/etc/init.d/networking restart

如果报错fatal error: Python.h: 没有那个文件或目录

1
apt-get install python-dev

使用

import gmpy2

具体使用姿势可以百度或者Google,看官方文档当然是最好的。

参考:

pcat


密码学libnum和gmpy2模块的安装
https://52hertz.tech/2020/02/28/libnum_gmpy2/
作者
Ustin1an
发布于
2020年2月28日
许可协议