<div class="gmail_quote">Hi,<div><br></div><div>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.</div><div><br></div><div>I google for a while and found MPIR (<a href="http://www.mpir.org/" target="_blank">http://www.mpir.org/</a>). 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.</div>
<div><br></div><div>After trying for 4 hours, I got a solution. (it may not accurate right, but it just works.)</div><div><br></div><div>First, I build MPIR as Release|x86 for both lib and dll:</div><div><ul><li>open "Visual Studio 2008 x64 Win64 Command Prompt"</li>
<li>cd mpir-2.1.3\build.vc9 <font color="#009900"># Change to the path that you extract MPIR.</font></li><li>msbuild mpir.sln /p:Configuration=Release /p:Platform=x64 /t:lib_mpir_nehalem <font color="#009900"># <-- my cpu, you can try others.</font></li>
<li>msbuild mpir.sln /p:Configuration=Release /p:Platform=x64 /t:dll_mpir_nehalem</li><li>to_gmp.bat <font color="#009900"># Convert libraries and headers to GMP.</font></li></ul><div>Then, copy *.h except config.h from <b><font color="#FF0000">DLL </font></b>output to your python\PC</div>
</div><div><ul><li>cd mpir-2.1.3\build.vc9\dll\x64\Release</li><li>mkdir C:\Python26\PC</li><li>copy gmp*.h C:\Python26\PC</li><li>copy mpir*.h C:\Python26\PC</li></ul><div>And copy gmp.lib from <b><font color="#FF0000">LIB </font></b>output to your python\PCbuild\amd64 <font color="#FF0000"># <i><b>Sound weird?</b></i> But trust me, I already try other ways and they were failed.</font></div>
</div><div><ul><li>mkdir C:\Python26\PCbuild</li><li>mkdir C:\Python26\PCbuild\amd64</li><li>cd mpir-2.1.3\build.vc9\lib\x64\Release</li><li>copy gmp.lib C:\Python26\PCbuild\amd64</li></ul><div>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.</div>
</div><div>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.</div><div>I also change the setup.py to build the _fastmath.cpp instead of _fastmath.c.</div>
<div>Finally, build the pycrypto and install it.</div><div><ul><li>python setup.py build</li><li>python setup.py install --optimize=1</li></ul><div>If you look in the C:\Python26\Lib\site-packages\Crypto\PublicKey, you will found _fastmath.pyd.</div>
</div><div>Now, the most excited part! Let's benchmark it.</div><div><br></div><div>Without _fastmath (you can try it by rename _fastmath.pyd to something such as _fastmath.xxx):</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><div><font face="'courier new', monospace">In [1]: from datetime import datetime</font></div><div><font face="'courier new', monospace"><br></font></div>
<div><font face="'courier new', monospace">In [2]: from Crypto.PublicKey import pubkey</font></div><div><font face="'courier new', monospace"><br></font></div>
<div><font face="'courier new', monospace">In [3]: def tdiff(n):</font></div><div><font face="'courier new', monospace"> ...: before = datetime.now()</font></div>
<div><font face="'courier new', monospace"> ...: pubkey.getStrongPrime(n)</font></div><div><font face="'courier new', monospace"> ...: print datetime.now()-before</font></div>
<div><font face="'courier new', monospace"> ...:</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [4]: tdiff(512)</font></div>
<div><font face="'courier new', monospace">0:00:00.348000</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [5]: tdiff(1024)</font></div>
<div><font face="'courier new', monospace">0:00:00.565000</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [6]: tdiff(2048)</font></div>
<div><font face="'courier new', monospace">0:00:03.308000</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [7]: tdiff(4096)</font></div>
<div><font face="'courier new', monospace">0:01:39.202000 # == (1*60)+39.202 == 99.202</font></div></div><div><font face="'courier new', monospace"><br></font></div>
</blockquote><font face="arial, helvetica, sans-serif">With _fastmath:</font><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">In [1]: from datetime import datetime</font></div><div><font face="'courier new', monospace"><br></font></div>
<div><font face="'courier new', monospace">In [2]: from Crypto.PublicKey import pubkey</font></div><div><font face="'courier new', monospace"><br></font></div>
<div><font face="'courier new', monospace">In [3]: def tdiff(n):</font></div><div><font face="'courier new', monospace"> ...: before = datetime.now()</font></div>
<div><font face="'courier new', monospace"> ...: pubkey.getStrongPrime(n)</font></div><div><font face="'courier new', monospace"> ...: print datetime.now()-before</font></div>
<div><font face="'courier new', monospace"> ...:</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [4]: tdiff(512)</font></div>
<div><font face="'courier new', monospace">0:00:00.123000</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [5]: tdiff(1024)</font></div>
<div><font face="'courier new', monospace">0:00:00.333000</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [6]: tdiff(2048)</font></div>
<div><font face="'courier new', monospace">0:00:00.675000</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">In [7]: tdiff(4096)</font></div>
<div><font face="'courier new', monospace">0:00:06.446000</font></div></div><div><font face="'courier new', monospace"><br></font></div></blockquote><font face="arial, helvetica, sans-serif">So, the conclusion:</font><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><font face="'courier new', monospace">n without with faster</font></div><div><font face="'courier new', monospace"> _fastmath _fastmath</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">512 0</font><span style="font-family:'courier new', monospace">0.348 00.123 182.93%</span></div>
<div><span style="font-family:'courier new', monospace">1024 00.565 00.333 69.67%</span></div><div><span style="font-family:'courier new', monospace">2048 03.308 00.675 390.07%</span></div>
<div><span style="font-family:'courier new', monospace">4096 99.202 06.446 1438.97%</span></div><div><span style="font-family:'courier new', monospace"><br>
</span></div></blockquote><font face="arial, helvetica, sans-serif">So, I think it's really worth to do (if you're stuck on Windows Platform).</font><div><font face="arial, helvetica, sans-serif"><br>
</font></div><div><font face="arial, helvetica, sans-serif">Regards,</font></div><div><font face="arial, helvetica, sans-serif">Chaiwat.S</font></div>
</div><br>