BigInteger

BigInteger

Version 1.1.2
Library for iOS and Mac OS X

Download

Source code on GitHub

Overview

BigInteger is a free (free as in free beer) library for iOS and Mac OS X developers that implements arbitrary-precision integers. It provides methods to perform usual arithmetic operations, as well as modular arithmetic, greatest common divisor calculation, primality testing, prime generation, and a few other things.

This library was not specifically written with performance in mind. It rather focuses on correctness, portability, and good integration with the Cocoa Framework. If your application heavily relies on multi-precision integer computation, you will be luckier with C-oriented libraries such as GMP. But if you only need a little bit of modular arithmetic in a project, or want to occasionally generate a big prime number with a minimum development effort, then this library is for you.

The BigInteger library can be freely included in any type of application.

Features

The library provides a BigInteger class that supports the following operations on arbitrary-precision integers:

  • Addition
  • Subtraction
  • Multiplication and modular multiplication
  • Division (computation of both quotient and remainder)
  • Bit shifts
  • Modular multiplicative inverse
  • Greatest Common Divisor
  • Exponentiation and modular exponentiation
  • Primality testing (Miller-Rabin test)
  • Random number generation
  • Printing to a string in any radix from 2 to 36
  • Archiving through the NSKeyedArchiver and NSKeyedUnarchiver objects

Sample Code

Using the BigInteger class is straightforward for anyone familiar with Objective-C and Cocoa. For exemple, the following code snippet generates a random, 200-bit long, prime number:

BigInteger * p, * r;
r = [[BigInteger alloc] initWithRandomNumberOfSize:200 exact:YES];
p = [r nextProbablePrime];
NSLog(@"prime = %@", [p toRadix:10]);
[r release];

Pretty easy, isn’t it?

Package Content

You can download the BigInteger as a zip file by clicking the link on the opposite frame. The package contains:

  • A header file to #import in your source files
  • A universal library for iOS development supporting the armv7, armv7s and i386 architectures
  • A universal library for Mac OS X development supporting the i386 and x86_64 architectures
  • A comprehensive documentation

What’s new in version 1.1

Version 1.1.2 comes with the following improvements:

  • Support for iOS 64-bit platforms
  • Support for bitwise logical operators (NOT, AND, OR and XOR)
  • Internally use of arc4random() instead of random() to relieve the programmer from worrying about PRNG initialization
  • Improved performances for very large multiplications
  • Minor bug fixes