密码学libnum和gmpy2模块的安装
终于有空来整理一下安装教程,顺便给新人留点参考资料,避免踩坑。
libnum
libnum库是一个关于各种数学运算的函数库,它包含common maths、modular、modular squre roots、primes、factorization、ECC、converting、stuff等方面的函数,结合gmpy2库、PyCrypto库一起来使用会使计算变得非常简便。
安装
1 |
|
常用的Converting
数字型(不论是十六进制还是十进制)与字符串之间的转换:
1 |
|
1 |
|
二进制与字符串之间的转换:
1 |
|
1 |
|
质数&因数分解
生成质数:
1 |
|
因数分解:
1 |
|
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安装即可。
wheel
先检查一下是否安装了wheel文件包,在cmd中输入wheel,查看一下,如果没有安装,则输入安装:
1 |
|
下载对应whl文件
以我自己的环境为例,我下载的是 gmpy2‑2.0.8‑cp37‑cp37m‑win32.whl
whl文件包需要和你所安装的python3版本一致,建议这个文件下载后放到python文件目录下。
安装
打开cmd,输入 pip install [whl文件的绝对路径]
安装whl文件包
以我为例:
1 |
|
使用
然后输入 import gmpy2
即可进行使用
Linux上安装
gmpy2是依赖GMP、MPFR、MPC三个库,故此在linux上安装前得先安装这3个库。
为了后续安装的方便,先建立2个文件夹。
1 |
|
测试有没有安装m4模块:
1 |
|
如果出现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 |
|
安装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 |
|
安装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 |
|
如果mpfr.org下载太慢,可以换为
1 |
|
安装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 |
|
安装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:
因为新版本的setup.py修改了不少,故得采用以下法子:
1 |
|
安装后,命令行进入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 |
|
如果报错fatal error: Python.h: 没有那个文件或目录
1 |
|
使用
import gmpy2
具体使用姿势可以百度或者Google,看官方文档当然是最好的。
参考: