[pycrypto] Changes for _fastmath.c to be able to built on MSVC 9.0 (aka VS2008)

cwt cwt at bashell.com
Fri Oct 15 06:33:29 CST 2010


Hi,

I just tried to build pycrypto for python-2.6.6 (x64) on Windows 7, for
found that it can't build _fastmath because there is no GMP on Windows.

I google for a while and found MPIR (http://www.mpir.org/). There is a
script 'to_gmp.bat' in the MPIR build folder that rename mpir.lib, mpir.dll,
and the relevant header files to gmp.

After trying for 4 hours, I got a solution. (it may not accurate right, but
it just works.)

First, I build MPIR as Release|x86 for both lib and dll:

   - open "Visual Studio 2008 x64 Win64 Command Prompt"
   - cd mpir-2.1.3\build.vc9 # Change to the path that you extract MPIR.
   - msbuild mpir.sln /p:Configuration=Release /p:Platform=x64
   /t:lib_mpir_nehalem  # <-- my cpu, you can try others.
   - msbuild mpir.sln /p:Configuration=Release /p:Platform=x64
   /t:dll_mpir_nehalem
   - to_gmp.bat # Convert libraries and headers to GMP.

Then, copy *.h except config.h from *DLL *output to your python\PC

   - cd mpir-2.1.3\build.vc9\dll\x64\Release
   - mkdir C:\Python26\PC
   - copy gmp*.h C:\Python26\PC
   - copy mpir*.h C:\Python26\PC

And copy gmp.lib from *LIB *output to your python\PCbuild\amd64 # *Sound
weird?* But trust me, I already try other ways and they were failed.

   - mkdir C:\Python26\PCbuild
   - mkdir C:\Python26\PCbuild\amd64
   - cd mpir-2.1.3\build.vc9\lib\x64\Release
   - copy gmp.lib C:\Python26\PCbuild\amd64

Now, in the extracted pycrypto folder, I copy src\_fastmath.c to
src\_fastmath.cpp and do some changes on _fastmath.cpp to make it C++
friendly.
Well, the patch is quite large because I move the whole sieve_base to the
very top of the file. So you better see the patch file that I'm attached to
this mail.
I also change the setup.py to build the _fastmath.cpp instead of
_fastmath.c.
Finally, build the pycrypto and install it.

   - python setup.py build
   - python setup.py install --optimize=1

If you look in the C:\Python26\Lib\site-packages\Crypto\PublicKey, you will
found _fastmath.pyd.
Now, the most excited part! Let's benchmark it.

Without _fastmath (you can try it by rename _fastmath.pyd to something such
as _fastmath.xxx):

In [1]: from datetime import datetime

In [2]: from Crypto.PublicKey import pubkey

In [3]: def tdiff(n):
   ...:     before = datetime.now()
   ...:     pubkey.getStrongPrime(n)
   ...:     print datetime.now()-before
   ...:

In [4]: tdiff(512)
0:00:00.348000

In [5]: tdiff(1024)
0:00:00.565000

In [6]: tdiff(2048)
0:00:03.308000

In [7]: tdiff(4096)
0:01:39.202000 # == (1*60)+39.202 == 99.202

With _fastmath:


In [1]: from datetime import datetime

In [2]: from Crypto.PublicKey import pubkey

In [3]: def tdiff(n):
   ...:     before = datetime.now()
   ...:     pubkey.getStrongPrime(n)
   ...:     print datetime.now()-before
   ...:

In [4]: tdiff(512)
0:00:00.123000

In [5]: tdiff(1024)
0:00:00.333000

In [6]: tdiff(2048)
0:00:00.675000

In [7]: tdiff(4096)
0:00:06.446000

So, the conclusion:

n       without      with         faster
        _fastmath    _fastmath

512     00.348       00.123        182.93%
1024    00.565       00.333         69.67%
2048    03.308       00.675        390.07%
4096    99.202       06.446       1438.97%

So, I think it's really worth to do (if you're stuck on Windows Platform).

Regards,
Chaiwat.S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.dlitz.net/pipermail/pycrypto/attachments/20101015/ad987b34/attachment.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: msvc9-patches.7z
Type: application/octet-stream
Size: 13240 bytes
Desc: not available
Url : http://lists.dlitz.net/pipermail/pycrypto/attachments/20101015/ad987b34/attachment-0001.obj 


More information about the pycrypto mailing list