MPI MPI: Arbitrary Precision Integer Arithmetic

News

20-Oct-2003 Another bug fix
Fixed a bug in one of the internal routines which could cause the library to compute the wrong number of digits for an output value. Thanks to Miguel Oyarzun for finding this one.
27-Dec-2002 Some bug fixes
Fixed several sign-related bugs. No new functionality in this release.
25-Aug-2001 Renaming, couple new functions
Some people were confused by the names of the "raw" and "mag" family of functions, so I renamed mp_toraw() to mp_to_signed_bin(), and mp_tomag() to mp_to_unsigned_bin(). The corresponding size functions have also been renamed. Also added mp_init_array() and mp_clear_array() for creating arrays of mp_int values a bit more easily.
01-Aug-2001 New features, bug fix
Version 1.8.2 introduced mp_tomag() to support the encoding model used by PKCS #1. Today's release of 1.8.2p1 fixes a bug in mp_tomag() which would cause it to handle the value zero (0) incorrectly. This version of the library also has most of the internal character values converted to unsigned, so that it should give correct output even on platforms which assume that characters are signed by default.
24-Feb-2001 Bug in s_mp_mul_d() fixed
Version 1.8.1p5 was released today. This fixes a bug in the s_mp_mul_d() routine, which would sometimes neglect to extend the precision of the product, thereby losing the high-order digit.
17-Feb-2001 Minor bug in mp_mul() fixed
Version 1.8.1p4 was released today. This fixes a minor bug in mp_sub() which would give the incorrect sign on the output in certain cases (see CHANGES for a synopsis).
31-Jan-2001 Minor bug in mp_sub() fixed
Version 1.8.1p3 was released today. This fixes a minor bug in mp_sub() which would give the incorrect sign on the output in certain cases (see CHANGES for a synopsis).
13-July-2000 MPI chosen for NSS
The MPI library was selected by the developers of NSS, the security component of the Mozilla.ORG code libraries. The Mozilla developers will be adding to MPI, and their changes will be released under the combination MPL/GPL they use (check out their site for more info). However, you can still download and use this version (if you care to).

What is MPI?

MPI is a simple ANSI C code library that performs arbitrary precision integer arithmetic functions. It is designed to be highly portable, is very easy to use, and is entirely free. It supports modular arithmetic, and the distribution includes code for probabilistic primality testing too, so you can use it for cryptographic purposes, if that's your interest.

The name "MPI" is short for "multiple precision integers". This is perhaps an unfortunate choice, since this acronym is also the name of a standard message-passing interface. (I learned this after I wrote the library). However, this is a sufficiently small project that I hope that minor name clash will not cause anyone much confusion.


What is it good for?

The built-in integer types of most computer programming languages, such as C, are of a fixed and relatively small size. This restriction makes it difficult to work with very large numbers (such as you might encounter in public key cryptography, for example). Using MPI and other libraries like it, you can work with much larger numbers than would ordinarly be possible -- indeed, only processing time and available memory limit the use of arbitrary precision integers.

Of course, not every language needs to go through all this. Java and Perl both include arbitrary-precision integers in their standard library of extensions, and the Python language has them as a built-in type. The Gambit Scheme compiler and interpreter also support arbitrary precision integers, as does GNU bc. Nevertheless, if you are programming in C, and need to do computations with very large integers, this is the sort of code you need.


Where can I get it?

A gzipped tar file with the source code for the most recent release of the library can be downloaded here. The tar file includes documentation in text form for how to compile and use the library in your own programs, as well as a variety of example programs.

The library has been compiled and tested on a variety of platforms, including a plethora of Unix variants (including Linux on x86 and PowerPC, IRIX on MIPS, AIX on RS/6000 and POWER2, Digital Unix on Alpha), MacOS, and Windows 98. The current distribution and build scripts are obviously very Unix centered, but it should be quite simple to build the library on any of these systems, since, at its core, it is merely one single source file and one header file!


How do I use it?

Detailed instructions can be found in the README file that accompanies the MPI distribution. However, here is a quick overview of how it works. You declare variables of type mp_int in your programs, and call functions in the MPI library to operate on them. Arbitrary precision integers require a bit more housekeeping than an ordinary int, but the general paradigm is pretty straightforward. Here is a very simple example:

   #include <stdio.h>
   #include <stdlib.h>
   #include "mpi.h"

   int main(int argc, char *argv[])
   {
      mp_int  a, b;
      char          *buf;
      int            len;

      /* Call mp_init() to initialize the mp_int structures */
      mp_init(&a);
      mp_init(&b);

      /* mp_set() assigns a single-digit value to an mp_int */
      mp_set(&a, 102);

      /* mp_read_radix() reads in a string value in the given base */
      mp_read_radix(&b, "4f923ac01ff0392155c2e98", 16);

      /* Let's multiply these two together... */
      mp_mul(&a, &b, &a);

      /* Print it out in decimal notation */
      len = mp_radix_size(&a, 10);
      buf = calloc(len, sizeof(char));
      mp_todecimal(&a, buf);
      printf("result = %s\n", buf);
      free(buf);

      /* Call mp_clear() to clean up an mp_int you're done with */
      mp_clear(&a); mp_clear(&b);

      return 0;
   }

As you can see, it's a bit more complex than using regular integers, but it's pretty simple once you get used to it. If you want, here are some some more examples.


Why should I use it?

You might be wondering why you would want to use this particular library, instead of one of the many others available. The principal virtues of MPI are that it is small, very portable, and entirely free. In particular, high performance was not one of the main design goals. I've made reasonable efforts to keep the performance as high as possible within the constraints of portability.

However, if you don't like it, I won't be offended; here are some alternatives you can try:


About the Author

MPI was written by Michael J. Fromberger. He will be quite happy to answer e-mail queries about the library, as time permits.


Last modified