C# BigInt: A GNU Multi-Precision Library for .NET
The GNU Multi-Precision Library for .NET is a .NET wrapper (written in C#) of the
GNU MP Bignum Library, which is probably the fastest general purpose large
number arithmatic library in the world. Below, you can download the wrapper along with a pre-built DLL of GNU MP.
- Wrapper for: big integers (mpz_* functions in GNU MP)
- Operations: Add, subtract, negate, multiply, divide, modulus, modular inverse, power, power modulus combination, comparisons, left/right bitwise shift, bitwise AND,
bitwise OR, bitwise XOR, bitwise complemenet, bit testing, converting to and from byte/sbyte/short/ushort/int/uint/long/ulong/float/double/byte/uint/string,
square root, nth root, perfect square testing, GCD, LCM, Legendre symbol, Jacobi symbol, Kronecker symbol, factorial, binomial, fibonacci,
lucas numbers, Miller–Rabin primality testing.
- Object-oriented .NET API.
- No need to install Cygwin.
- Strict adherence to the Microsoft .NET naming conventions.
- Operator overloading (you can use +, -, *, /, %, etc for large number arithmatic).
- Overloads for simple types (int, long, double, etc) -- major performance boost.
- C# implementation of simple operations to avoid interop overhead.
Sample Code (C#)
The wrapper consists of a single class called BigInt. The following is sample code that executes as is. It really is that simple :)
// Define some big integers
BigInt a = new BigInt("3648686172031547129462783484965308369824430041997653001183827180347");
BigInt b = new BigInt("1067825251034421530837885294271156039110655362253362224471523");
BigInt c = new BigInt("41396477494303017258631229711712875916229270588335063106205084757");
BigInt d = new BigInt("580093571242364813507439409980234401659339692870834222904264712629");
// Do some calculations
BigInt x = a * b - c * d + 3573;
BigInt y = a.PowerMod(b, c); // y = a^b mod c
// Output the results
Console.WriteLine("x = " + x);
Console.WriteLine("y = " + y);
// The output is:
// x = -24009934307296823576048658797964517931077618572590008983745178717922409632366854792609208375660857353281797995109339561771906534099
// y = 32406089037863409599698814455378683600755021863921627696454332907
You may download the binaries and source code for the .NET wrapper below. Included is also a pre-built DLL of the GNU MP Bignum Library for Windows.
The source code for the C/C++ GNU MP Library DLL may be downloaded from the official GNU MP website
and the MinGW website (the windows port).
The GNU Multi-Precision Library for .NET is licensed under the GNU LGPL license subject to the
conditions here. Note that the GNU MP Library is also licensed under the GNU LGPL license.