Building PyCrypto with fastmath (gmp or mpir) via pip on Windows

I installed PyCrypto on Windows via pip, but I was unable to create Crypto.PublicKey._fastmath because GMP was not found.

I know there is a binary version on voidspace , but I would like to create the latest version of PyCrypto

+6
source share
1 answer

Below is a way to achieve your goal. There are other, possibly better ways (e.g. based on Visual Studio), but this one works for me. In addition, it does not use pip .

All operations are performed on the command line.

  • Install Mingw , including MSYS and Development Tool. This will give you a fairly complete Unix-like development environment.
  • Make sure the Mingw binaries are in the PATH environment variable. You need MinGW\bin and MingGW\msys\1.0\bin .
  • Download MPIR sources in the temporary directory. It is important that you do not use 2.5.1 due to an error that will break the assembly. 2.5.0 is fine.
  • Create the MPIR library. It is pretty simple: do bash configure and then make .
  • HACK # 1 Copy libmpir.a from mpir-2.5.0\.libs to C:\Python2.7.1\libs . This is necessary because distutils broken, and I could not find a way to direct it to the correct library location.
  • HACK # 2 Change C:\Python2.7.1\Lib\distutils\cygwincompiler.py and remove any line origin -mno-cygwin . The reason is explained here .
  • Download PyCrypto sources and unzip them to another temporary directory.
  • Set the CPPFLAGS environment CPPFLAGS in the CPPFLAGS directory that contains mpir.h
  • HACK 3 Modify setup.py and add the following line to the build_extension method:

    self .__ add_compiler_option (os.environ ['CPPFLAGS'])

  • Run bash configure . You should see two lines:

    checking __gmpz_init in -lgmp ... no
    checking __gmpz_init in -lmpir ... yes

  • Run python setup.py build -c mingw32 . You will not see errors.
  • Run python setup.py test to make sure everything is ok.
  • Run python setup.py install to copy the files to the local Python repository.
  • Alternatively, run python setup.py bdist_wininst to create the installer.

I really hate all the different hacks, and I would like to hear if they can be avoided.

+5
source

Source: https://habr.com/ru/post/917555/