Installing GMP


Now, you will want to download and build the GMP library. You can obtain the most recent version of GMP in source code format from http://www.gnu.org/directory/gnump.html. At the time of writing, the most recent version was gmp-4.0.1.tar.gz , as shown in Figure C-12.

Figure C-12. Getting GnuMP.

graphics/cfig12.jpg

After you download the GMP tarball (i.e., gmp-4.0.1.tar.gz), you will have to decompress it, using WinZip or an equivalent utility. This creates a directory named gmp-4.0.1 (depending on the specific version you have downloaded), which you can put in some convenient place, such as at the root c:\ directory. At this point, the c:\gmp-4.0.1 directory contains the GMP source tree, but it is not compiled and linked yet. To do this, you must start up a Cygwin command window by selecting Start Programs Cygwin Cygwin Bash Shell.

To build the GMP library, enter the following commands at the Cygwin Bash Shell prompt.

 cd c:/gmp-4.0.1 ./configure --disable-static --enable-shared make make install 

Your working directory may be different, according to where you unzipped the GMP tarball. We do not want a static library for linking with C/C++ programs. For .NET programs, we need a DLL that provides PInvoke services to access the functionality of the GMP library. This means that we must generate a DLL rather than a statically linkable LIB file. This is why we are using the --disable-static --enable-shared parameters in the configure command. The first make command is used to perform a build on the GMP project, and the second make command installs it on your Cygwin system. The result of this installation is that a header file named gmp.h is placed in the c:\gmp-4.0.1 directory, and a DLL named cyggmp-3.dll is placed in the c:\gmp-4.0.1\.libs directory. You should make this DLL available to the DLL search path by adding c:\gmp-4.0.1\.libs to the PATH environment variable.

The details on how to use the entire functionality of the GMP library are beyond the scope of this book. To learn more about all the functions supported by the GMP library, see the documentation at http://www.swox.com/gmp/manual/. However, to get you started and to test the newly built GMP library with a C# .NET program, try creating the following C# client program. If you want to access any of the other GMP functions exposed by cyggmp-3.dll , just add the appropriate DllImport attributed external methods to the CygGmpWrapper class, according to the signature you find in gmp.h .

 //MyCSharpGMPClient.cs using System; using System.Runtime.InteropServices; //GMP's structure for multiprecision integer [StructLayout(LayoutKind.Sequential)] unsafe public struct mpz_struct {    public int _mp_alloc;    public int _mp_size;    public uint *_mp_d; }; //NOTE: cyggmp-3.dll must be in dll search path class CygGmpWrapper //wrapper for calling GMP library {     [DllImport("cyggmp-3.dll")]    unsafe public static extern       void __gmpz_init(       mpz_struct* val);     [DllImport("cyggmp-3.dll")]    unsafe public static extern       int __gmpz_set_str(       mpz_struct* val,       String strval,       int baseval);     [DllImport("cyggmp-3.dll")]    unsafe public static extern       int __gmpz_mul(       mpz_struct* valprod,       mpz_struct* val1,       mpz_struct* val2);     [DllImport("cyggmp-3.dll")]    unsafe public static extern       String __gmpz_get_str(       String str,       int baseval,       mpz_struct* val); }; class MyCSharpGMPClient {    unsafe static void Main(string[] args)    {       //initialize gmp integer variables a, b, and p       mpz_struct mpzs_a;       mpz_struct* a = &mpzs_a;       CygGmpWrapper.__gmpz_init(a);       mpz_struct mpzs_b;       mpz_struct* b = &mpzs_b;       CygGmpWrapper.__gmpz_init(b);       mpz_struct mpzs_prod;       mpz_struct* prod = &mpzs_prod;       CygGmpWrapper.__gmpz_init(prod);       //assign a and b using base 10 strings       CygGmpWrapper.__gmpz_set_str (          a, "123456789123456789", 10);       CygGmpWrapper.__gmpz_set_str (          b, "123456789123456789", 10);       //multiply a and b and put the result in prod       //result is 15241578780673678515622620750190521       CygGmpWrapper.__gmpz_mul(prod, a, b);       //print prod in base 10       String strProd =          CygGmpWrapper.__gmpz_get_str(null, 10, prod);       Console.WriteLine(          "123456789123456789 * 123456789123456789 = \n" +          strProd);    } } 

When you run this program, you should see the following output.

 123456789123456789 * 123456789123456789 = 15241578780673678515622620750190521 


.NET Security and Cryptography
.NET Security and Cryptography
ISBN: 013100851X
EAN: 2147483647
Year: 2003
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net