This library provides a way to easily handle arbitrary large integers.

This library provides the following operations :

  • addition, substraction, multiplication, division and modulo
  • bits operators (AND, OR, XOR, left and right shifts)
  • boolean operators
  • modular exponentiation (using montgomery algorithm)
  • modular inverse

Example

In this example, we use a 1024 bits long RSA key to encrypt and decrypt a message. We first encrypt the value 0x41 (65 in decimal) and then decrypt it. At the end, m should be equal to 0x41. The encryption is fast (0, 4 second) while the decryption is really slow. This code will take between 30 seconds and 2 minutes to execute depending on the compiler and optimization flags.

main.cpp

#include "mbed.h"
#include "BigInt.h"
#include <stdlib.h>
#include <stdio.h>

uint8_t modbits[] = {
0xd9, 0x4d, 0x88, 0x9e, 0x88, 0x85, 0x3d, 0xd8, 0x97, 0x69, 0xa1, 0x80, 0x15, 0xa0, 0xa2, 0xe6,
0xbf, 0x82, 0xbf, 0x35, 0x6f, 0xe1, 0x4f, 0x25, 0x1f, 0xb4, 0xf5, 0xe2, 0xdf, 0x0d, 0x9f, 0x9a,
0x94, 0xa6, 0x8a, 0x30, 0xc4, 0x28, 0xb3, 0x9e, 0x33, 0x62, 0xfb, 0x37, 0x79, 0xa4, 0x97, 0xec,
0xea, 0xea, 0x37, 0x10, 0x0f, 0x26, 0x4d, 0x7f, 0xb9, 0xfb, 0x1a, 0x97, 0xfb, 0xf6, 0x21, 0x13,
0x3d, 0xe5, 0x5f, 0xdc, 0xb9, 0xb1, 0xad, 0x0d, 0x7a, 0x31, 0xb3, 0x79, 0x21, 0x6d, 0x79, 0x25,
0x2f, 0x5c, 0x52, 0x7b, 0x9b, 0xc6, 0x3d, 0x83, 0xd4, 0xec, 0xf4, 0xd1, 0xd4, 0x5c, 0xbf, 0x84,
0x3e, 0x84, 0x74, 0xba, 0xbc, 0x65, 0x5e, 0x9b, 0xb6, 0x79, 0x9c, 0xba, 0x77, 0xa4, 0x7e, 0xaf,
0xa8, 0x38, 0x29, 0x64, 0x74, 0xaf, 0xc2, 0x4b, 0xeb, 0x9c, 0x82, 0x5b, 0x73, 0xeb, 0xf5, 0x49
};

uint8_t dbits[] = {
0x04, 0x7b, 0x9c, 0xfd, 0xe8, 0x43, 0x17, 0x6b, 0x88, 0x74, 0x1d, 0x68, 0xcf, 0x09, 0x69, 0x52,
0xe9, 0x50, 0x81, 0x31, 0x51, 0x05, 0x8c, 0xe4, 0x6f, 0x2b, 0x04, 0x87, 0x91, 0xa2, 0x6e, 0x50,
0x7a, 0x10, 0x95, 0x79, 0x3c, 0x12, 0xba, 0xe1, 0xe0, 0x9d, 0x82, 0x21, 0x3a, 0xd9, 0x32, 0x69,
0x28, 0xcf, 0x7c, 0x23, 0x50, 0xac, 0xb1, 0x9c, 0x98, 0xf1, 0x9d, 0x32, 0xd5, 0x77, 0xd6, 0x66,
0xcd, 0x7b, 0xb8, 0xb2, 0xb5, 0xba, 0x62, 0x9d, 0x25, 0xcc, 0xf7, 0x2a, 0x5c, 0xeb, 0x8a, 0x8d,
0xa0, 0x38, 0x90, 0x6c, 0x84, 0xdc, 0xdb, 0x1f, 0xe6, 0x77, 0xdf, 0xfb, 0x2c, 0x02, 0x9f, 0xd8,
0x92, 0x63, 0x18, 0xee, 0xde, 0x1b, 0x58, 0x27, 0x2a, 0xf2, 0x2b, 0xda, 0x5c, 0x52, 0x32, 0xbe,
0x06, 0x68, 0x39, 0x39, 0x8e, 0x42, 0xf5, 0x35, 0x2d, 0xf5, 0x88, 0x48, 0xad, 0xad, 0x11, 0xa1
};

int main() 
{
    BigInt e = 65537, mod, d;
    mod.importData(modbits, sizeof(modbits));
    d.importData(dbits, sizeof(dbits));

    BigInt c = modPow(0x41,e,mod);
    c.print();
    BigInt m = modPow(c,d,mod);
    m.print();
    printf("done\n");
    
    return 0;
}

Committer:
feb11
Date:
Sun May 11 10:33:20 2014 +0000
Revision:
26:94e26bcd229d
Parent:
24:a3453a18388c
Support signed integers. Add invMod function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9d554894785b 1 #ifndef BIGINT_H
feb11 0:9d554894785b 2 #define BIGINT_H
feb11 0:9d554894785b 3
feb11 0:9d554894785b 4 #include <stdint.h>
feb11 0:9d554894785b 5
feb11 26:94e26bcd229d 6 #define POS (true)
feb11 26:94e26bcd229d 7 #define NEG (false)
feb11 26:94e26bcd229d 8
feb11 0:9d554894785b 9 class BigInt
feb11 0:9d554894785b 10 {
feb11 0:9d554894785b 11 public :
feb11 0:9d554894785b 12
feb11 0:9d554894785b 13 BigInt();
feb11 26:94e26bcd229d 14 BigInt(int32_t a);
feb11 0:9d554894785b 15 BigInt(const BigInt &a);
feb11 0:9d554894785b 16 BigInt& operator=(const BigInt& a);
feb11 0:9d554894785b 17 virtual ~BigInt();
feb11 0:9d554894785b 18
feb11 26:94e26bcd229d 19 void importData(uint8_t *data, uint32_t length, bool sign = POS);
feb11 26:94e26bcd229d 20 void exportData(uint8_t *data, uint32_t length, bool &sign);
feb11 0:9d554894785b 21
feb11 0:9d554894785b 22 // Arithmetic operations
feb11 11:2f16a220ebbb 23 friend BigInt operator+(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 24 BigInt& operator+=(const BigInt &b);
feb11 0:9d554894785b 25 BigInt& operator++();
feb11 0:9d554894785b 26 BigInt operator++(int);
feb11 0:9d554894785b 27
feb11 11:2f16a220ebbb 28 friend BigInt operator-(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 29 friend BigInt operator-(const BigInt &a);
feb11 0:9d554894785b 30 BigInt& operator-=(const BigInt &b);
feb11 0:9d554894785b 31 BigInt& operator--();
feb11 0:9d554894785b 32 BigInt operator--(int);
feb11 26:94e26bcd229d 33
feb11 11:2f16a220ebbb 34 friend BigInt operator*(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 35 BigInt& operator*=(const BigInt &b);
feb11 10:116e201f7d89 36
feb11 11:2f16a220ebbb 37 friend BigInt operator/(const BigInt &a, const BigInt &b);
feb11 10:116e201f7d89 38 BigInt& operator/=(const BigInt &b);
feb11 10:116e201f7d89 39
feb11 1:00f2c40d46ed 40 friend BigInt operator>>(const BigInt &a, const uint32_t m);
feb11 1:00f2c40d46ed 41 BigInt& operator>>=(const uint32_t m);
feb11 1:00f2c40d46ed 42 friend BigInt operator<<(const BigInt &a, const uint32_t m);
feb11 1:00f2c40d46ed 43 BigInt& operator<<=(const uint32_t m);
feb11 1:00f2c40d46ed 44
feb11 5:beeb31f340a7 45 friend BigInt operator%(const BigInt &a, const BigInt &b);
feb11 5:beeb31f340a7 46 BigInt& operator%=(const BigInt &a);
feb11 5:beeb31f340a7 47
feb11 0:9d554894785b 48 // Boolean operations
feb11 0:9d554894785b 49 friend bool operator==(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 50 friend bool operator!=(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 51 friend bool operator<(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 52 friend bool operator<=(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 53 friend bool operator>(const BigInt &a, const BigInt &b);
feb11 0:9d554894785b 54 friend bool operator>=(const BigInt &a, const BigInt &b);
feb11 1:00f2c40d46ed 55 friend bool operator!(const BigInt &a);
feb11 0:9d554894785b 56
feb11 7:1aad58757705 57 // Bits operations
feb11 7:1aad58757705 58 friend BigInt operator&(const BigInt &a, const BigInt &b);
feb11 7:1aad58757705 59 BigInt& operator&=(const BigInt &a);
feb11 7:1aad58757705 60 friend BigInt operator|(const BigInt &a, const BigInt &b);
feb11 9:3c191fa04f6e 61 BigInt& operator|=(const BigInt &a);
feb11 8:2a361f1b41da 62 friend BigInt operator^(const BigInt &a, const BigInt &b);
feb11 9:3c191fa04f6e 63 BigInt& operator^=(const BigInt &a);
feb11 9:3c191fa04f6e 64
feb11 9:3c191fa04f6e 65 // modular exponentiation
feb11 9:3c191fa04f6e 66 friend BigInt modPow(const BigInt &a, const BigInt &expn, const BigInt &modulus);
feb11 26:94e26bcd229d 67
feb11 26:94e26bcd229d 68 // invert modular
feb11 26:94e26bcd229d 69 friend BigInt invMod(const BigInt &a, const BigInt &modulus);
feb11 26:94e26bcd229d 70
feb11 26:94e26bcd229d 71 // miscellaneous
feb11 9:3c191fa04f6e 72 bool isValid() const;
feb11 26:94e26bcd229d 73 uint32_t numBits() const;
feb11 9:3c191fa04f6e 74
feb11 0:9d554894785b 75 // debug
feb11 11:2f16a220ebbb 76 void print() const;
feb11 10:116e201f7d89 77
feb11 0:9d554894785b 78 private :
feb11 0:9d554894785b 79
feb11 26:94e26bcd229d 80 friend BigInt add(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 81 friend BigInt sub(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 82 friend BigInt mul(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 83 friend BigInt div(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 84
feb11 26:94e26bcd229d 85 friend bool equals(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 86 friend bool lesser(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 87 friend bool greater(const BigInt &a, const BigInt &b);
feb11 26:94e26bcd229d 88
feb11 24:a3453a18388c 89 // fast operations
feb11 26:94e26bcd229d 90 void fastAdd(const BigInt &b);
feb11 26:94e26bcd229d 91 void fastShr();
feb11 24:a3453a18388c 92
feb11 10:116e201f7d89 93 void trim();
feb11 26:94e26bcd229d 94 void checkZero();
feb11 10:116e201f7d89 95
feb11 10:116e201f7d89 96 friend BigInt montgomeryStep(const BigInt &a, const BigInt &b, const BigInt &m, uint32_t r);
feb11 10:116e201f7d89 97 friend BigInt montgomeryStep2(const BigInt &a, const BigInt &m, uint32_t r);
feb11 26:94e26bcd229d 98
feb11 26:94e26bcd229d 99 bool sign;
feb11 10:116e201f7d89 100 uint32_t size; // smaller number of bytes needed to represent integer
feb11 0:9d554894785b 101 uint32_t *bits;
feb11 0:9d554894785b 102 };
feb11 0:9d554894785b 103
feb11 0:9d554894785b 104
feb11 24:a3453a18388c 105 #endif