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:
Thu Mar 06 10:58:30 2014 +0000
Revision:
12:a436f15b58b6
Parent:
11:2f16a220ebbb
Child:
13:d0b4d7cfeb98
fixed multiple algorithms (multiplication, operator< & >, modPow)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9d554894785b 1 #include "BigInt.h"
feb11 0:9d554894785b 2 #include <string.h>
feb11 0:9d554894785b 3 #include <stdio.h>
feb11 0:9d554894785b 4 #include <stdlib.h>
feb11 0:9d554894785b 5 #include <iostream>
feb11 0:9d554894785b 6 #include <climits>
feb11 9:3c191fa04f6e 7 #include <cassert>
feb11 10:116e201f7d89 8 #include <algorithm>
feb11 0:9d554894785b 9
feb11 0:9d554894785b 10 static uint32_t num(const uint32_t a)
feb11 0:9d554894785b 11 {
feb11 0:9d554894785b 12 return a/4 + (a%4 ? 1:0);
feb11 0:9d554894785b 13 }
feb11 10:116e201f7d89 14
feb11 10:116e201f7d89 15
feb11 0:9d554894785b 16 BigInt::BigInt():
feb11 0:9d554894785b 17 size(0),
feb11 0:9d554894785b 18 bits(0)
feb11 0:9d554894785b 19 {
feb11 0:9d554894785b 20 }
feb11 0:9d554894785b 21
feb11 2:1001793a090d 22 BigInt::BigInt(const uint32_t a)
feb11 2:1001793a090d 23 {
feb11 2:1001793a090d 24 if(a >> 24)
feb11 2:1001793a090d 25 size = 4;
feb11 2:1001793a090d 26 else if(a >> 16)
feb11 2:1001793a090d 27 size = 3;
feb11 2:1001793a090d 28 else if(a >> 8)
feb11 2:1001793a090d 29 size = 2;
feb11 2:1001793a090d 30 else
feb11 2:1001793a090d 31 size = 1;
feb11 2:1001793a090d 32 bits = new uint32_t[1];
feb11 2:1001793a090d 33 bits[0] = a;
feb11 2:1001793a090d 34 }
feb11 2:1001793a090d 35
feb11 0:9d554894785b 36 BigInt::BigInt(const BigInt &a):
feb11 0:9d554894785b 37 size(a.size)
feb11 0:9d554894785b 38 {
feb11 0:9d554894785b 39 uint32_t l = num(size);
feb11 0:9d554894785b 40 bits = new uint32_t[l];
feb11 0:9d554894785b 41 for(int i = 0; i < l; ++i)
feb11 0:9d554894785b 42 bits[i] = a.bits[i];
feb11 0:9d554894785b 43 }
feb11 0:9d554894785b 44
feb11 0:9d554894785b 45
feb11 0:9d554894785b 46 BigInt::~BigInt()
feb11 0:9d554894785b 47 {
feb11 10:116e201f7d89 48 if(size)
feb11 9:3c191fa04f6e 49 {
feb11 0:9d554894785b 50 delete[] bits;
feb11 9:3c191fa04f6e 51 }
feb11 0:9d554894785b 52 }
feb11 0:9d554894785b 53
feb11 0:9d554894785b 54 BigInt& BigInt::operator=(const BigInt& a)
feb11 0:9d554894785b 55 {
feb11 0:9d554894785b 56 size = a.size;
feb11 0:9d554894785b 57 uint32_t l = num(size);
feb11 0:9d554894785b 58 if(bits)
feb11 0:9d554894785b 59 delete[] bits;
feb11 0:9d554894785b 60 bits = new uint32_t[l];
feb11 0:9d554894785b 61 for(int i = 0; i < l; ++i)
feb11 0:9d554894785b 62 bits[i] = a.bits[i];
feb11 0:9d554894785b 63 return *this;
feb11 0:9d554894785b 64 }
feb11 0:9d554894785b 65
feb11 0:9d554894785b 66 void BigInt::import(uint8_t *data, uint32_t length)
feb11 0:9d554894785b 67 {
feb11 0:9d554894785b 68 size = length;
feb11 0:9d554894785b 69 size_t l = size/4;
feb11 0:9d554894785b 70 if(size % 4 != 0)
feb11 0:9d554894785b 71 l++;
feb11 0:9d554894785b 72 if(bits)
feb11 0:9d554894785b 73 delete[] bits;
feb11 0:9d554894785b 74 bits = new uint32_t[l];
feb11 0:9d554894785b 75 memset(bits, 0, sizeof(uint32_t)*l);
feb11 4:773aed3156c5 76 for(int i = length-1; i >=0; --i)
feb11 0:9d554894785b 77 bits[i/4] |= data[i] << ((i%4)*8);
feb11 0:9d554894785b 78 }
feb11 0:9d554894785b 79
feb11 0:9d554894785b 80 BigInt operator+(const BigInt &a, const BigInt& b)
feb11 0:9d554894785b 81 {
feb11 9:3c191fa04f6e 82 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 83
feb11 0:9d554894785b 84 BigInt result;
feb11 9:3c191fa04f6e 85
feb11 0:9d554894785b 86 result.size = a.size > b.size ? a.size : b.size;
feb11 0:9d554894785b 87 size_t l = result.size/4;
feb11 0:9d554894785b 88 if(result.size % 4 != 0)
feb11 0:9d554894785b 89 l++;
feb11 0:9d554894785b 90 result.bits = new uint32_t[l];
feb11 0:9d554894785b 91 memset(result.bits, 0, sizeof(uint32_t)*l);
feb11 0:9d554894785b 92 uint32_t al = num(a.size);
feb11 0:9d554894785b 93 uint32_t bl = num(b.size);
feb11 0:9d554894785b 94 uint32_t carry = 0;
feb11 0:9d554894785b 95 for(int i = 0; i < l; ++i)
feb11 0:9d554894785b 96 {
feb11 0:9d554894785b 97 uint32_t tmpA = 0, tmpB = 0;
feb11 0:9d554894785b 98 if(i < al)
feb11 0:9d554894785b 99 tmpA = a.bits[i];
feb11 0:9d554894785b 100 if(i < bl)
feb11 0:9d554894785b 101 tmpB = b.bits[i];
feb11 0:9d554894785b 102 result.bits[i] = tmpA + tmpB + carry;
feb11 10:116e201f7d89 103 carry = result.bits[i] < std::max(tmpA, tmpB);
feb11 0:9d554894785b 104 }
feb11 0:9d554894785b 105 if(carry)
feb11 0:9d554894785b 106 {
feb11 0:9d554894785b 107 result.size++;
feb11 0:9d554894785b 108 if(result.size > l*4)
feb11 0:9d554894785b 109 {
feb11 0:9d554894785b 110 l++;
feb11 0:9d554894785b 111 result.bits = (uint32_t*)realloc(result.bits, l * sizeof(uint32_t));
feb11 0:9d554894785b 112 result.bits[l-1] = 0x00000001;
feb11 0:9d554894785b 113 }
feb11 0:9d554894785b 114 else
feb11 0:9d554894785b 115 {
feb11 0:9d554894785b 116 result.bits[l-1] += 1 << (8 *((result.size-1)%4));
feb11 0:9d554894785b 117 }
feb11 0:9d554894785b 118 }
feb11 0:9d554894785b 119
feb11 0:9d554894785b 120 return result;
feb11 0:9d554894785b 121 }
feb11 0:9d554894785b 122
feb11 0:9d554894785b 123 BigInt& BigInt::operator+=(const BigInt &b)
feb11 0:9d554894785b 124 {
feb11 0:9d554894785b 125 return (*this = (*this) + b);
feb11 0:9d554894785b 126 }
feb11 0:9d554894785b 127
feb11 0:9d554894785b 128 BigInt& BigInt::operator++()
feb11 0:9d554894785b 129 {
feb11 3:3fa3ceb0c69d 130 return (*this += 1);
feb11 0:9d554894785b 131 }
feb11 0:9d554894785b 132
feb11 0:9d554894785b 133 BigInt BigInt::operator++(int)
feb11 0:9d554894785b 134 {
feb11 0:9d554894785b 135 BigInt t = *this;
feb11 3:3fa3ceb0c69d 136 *this += 1;
feb11 0:9d554894785b 137 return t;
feb11 0:9d554894785b 138 }
feb11 0:9d554894785b 139
feb11 0:9d554894785b 140 // a - b, if b >= a, returns 0
feb11 0:9d554894785b 141 // No negative number allowed
feb11 0:9d554894785b 142 BigInt operator-(const BigInt &a, const BigInt& b)
feb11 0:9d554894785b 143 {
feb11 9:3c191fa04f6e 144 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 145
feb11 0:9d554894785b 146 BigInt result;
feb11 0:9d554894785b 147
feb11 0:9d554894785b 148 if(b >= a)
feb11 0:9d554894785b 149 {
feb11 3:3fa3ceb0c69d 150 return result = 0;
feb11 0:9d554894785b 151 }
feb11 0:9d554894785b 152 else
feb11 0:9d554894785b 153 {
feb11 0:9d554894785b 154 result.size = a.size;
feb11 0:9d554894785b 155 uint32_t l = num(a.size);
feb11 0:9d554894785b 156 result.bits = new uint32_t[l];
feb11 0:9d554894785b 157 memset(result.bits, 0, sizeof(uint32_t)*l);
feb11 0:9d554894785b 158 uint32_t bl = num(b.size);
feb11 0:9d554894785b 159 uint8_t borrow = 0;
feb11 0:9d554894785b 160 for(uint32_t i = 0; i < l; ++i)
feb11 0:9d554894785b 161 {
feb11 0:9d554894785b 162 uint32_t tmpA = a.bits[i], tmpB = 0;
feb11 0:9d554894785b 163 if(i < bl)
feb11 0:9d554894785b 164 tmpB = b.bits[i];
feb11 0:9d554894785b 165 if(borrow)
feb11 0:9d554894785b 166 {
feb11 0:9d554894785b 167 if(tmpA == 0)
feb11 0:9d554894785b 168 tmpA = ULONG_MAX;
feb11 0:9d554894785b 169 else
feb11 0:9d554894785b 170 --tmpA;
feb11 0:9d554894785b 171
feb11 0:9d554894785b 172 if(tmpA < tmpB)
feb11 0:9d554894785b 173 result.bits[i] = tmpA + 1 + (ULONG_MAX - tmpB);
feb11 0:9d554894785b 174 else
feb11 0:9d554894785b 175 result.bits[i] = tmpA - tmpB;
feb11 0:9d554894785b 176
feb11 0:9d554894785b 177 if(a.bits[i] != 0 && tmpA > tmpB)
feb11 0:9d554894785b 178 borrow = 0;
feb11 0:9d554894785b 179 }
feb11 0:9d554894785b 180 else
feb11 0:9d554894785b 181 {
feb11 0:9d554894785b 182 if(tmpA < tmpB)
feb11 0:9d554894785b 183 result.bits[i] = tmpA + 1 + (ULONG_MAX - tmpB);
feb11 0:9d554894785b 184 else
feb11 0:9d554894785b 185 result.bits[i] = tmpA - tmpB;
feb11 0:9d554894785b 186 borrow = tmpA < tmpB;
feb11 0:9d554894785b 187 }
feb11 0:9d554894785b 188 }
feb11 0:9d554894785b 189
feb11 9:3c191fa04f6e 190 result.trim();
feb11 0:9d554894785b 191
feb11 0:9d554894785b 192 return result;
feb11 0:9d554894785b 193 }
feb11 0:9d554894785b 194 }
feb11 0:9d554894785b 195
feb11 0:9d554894785b 196 BigInt& BigInt::operator-=(const BigInt &b)
feb11 0:9d554894785b 197 {
feb11 0:9d554894785b 198 return (*this = (*this) - b);
feb11 0:9d554894785b 199 }
feb11 0:9d554894785b 200
feb11 0:9d554894785b 201 BigInt& BigInt::operator--()
feb11 0:9d554894785b 202 {
feb11 3:3fa3ceb0c69d 203 return (*this -= 1);
feb11 0:9d554894785b 204 }
feb11 0:9d554894785b 205
feb11 0:9d554894785b 206 BigInt BigInt::operator--(int)
feb11 0:9d554894785b 207 {
feb11 0:9d554894785b 208 BigInt t = *this;
feb11 3:3fa3ceb0c69d 209 *this -= 1;
feb11 0:9d554894785b 210 return t;
feb11 0:9d554894785b 211 }
feb11 3:3fa3ceb0c69d 212
feb11 1:00f2c40d46ed 213 BigInt operator*(const BigInt &a, const BigInt& b)
feb11 1:00f2c40d46ed 214 {
feb11 9:3c191fa04f6e 215 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 216
feb11 1:00f2c40d46ed 217 // if a == 0 or b == 0 then result = 0
feb11 1:00f2c40d46ed 218 if(!a || !b)
feb11 12:a436f15b58b6 219 return 0;
feb11 1:00f2c40d46ed 220
feb11 1:00f2c40d46ed 221 // if a == 1, then result = b
feb11 3:3fa3ceb0c69d 222 if(a == 1)
feb11 12:a436f15b58b6 223 return b;
feb11 1:00f2c40d46ed 224
feb11 1:00f2c40d46ed 225 // if b == 1, then result = a
feb11 3:3fa3ceb0c69d 226 if(b == 1)
feb11 12:a436f15b58b6 227 return a;
feb11 12:a436f15b58b6 228
feb11 12:a436f15b58b6 229 BigInt result;
feb11 4:773aed3156c5 230 result.size = a.size + b.size;
feb11 4:773aed3156c5 231 result.bits = new uint32_t[num(result.size)];
feb11 12:a436f15b58b6 232 memset(result.bits, 0, sizeof(uint32_t)*num(result.size));
feb11 11:2f16a220ebbb 233 for(int i = 0; i < num(a.size); ++i)
feb11 4:773aed3156c5 234 {
feb11 11:2f16a220ebbb 235 uint64_t carry = 0;
feb11 11:2f16a220ebbb 236 for(int j = 0; j < num(b.size); ++j)
feb11 11:2f16a220ebbb 237 {
feb11 11:2f16a220ebbb 238 uint64_t tmp = (uint64_t)a.bits[i] * (uint64_t)b.bits[j] + carry;
feb11 12:a436f15b58b6 239 uint32_t t = result.bits[i+j];
feb11 11:2f16a220ebbb 240 result.bits[i+j] += tmp;
feb11 12:a436f15b58b6 241 carry = tmp >> 32;
feb11 12:a436f15b58b6 242 if(t > result.bits[i+j])
feb11 12:a436f15b58b6 243 ++carry;
feb11 11:2f16a220ebbb 244 }
feb11 11:2f16a220ebbb 245 if(carry != 0)
feb11 11:2f16a220ebbb 246 result.bits[i+num(b.size)] += carry;
feb11 4:773aed3156c5 247 }
feb11 4:773aed3156c5 248
feb11 9:3c191fa04f6e 249 result.trim();
feb11 1:00f2c40d46ed 250
feb11 1:00f2c40d46ed 251 return result;
feb11 1:00f2c40d46ed 252 }
feb11 1:00f2c40d46ed 253
feb11 1:00f2c40d46ed 254 BigInt& BigInt::operator*=(const BigInt &b)
feb11 1:00f2c40d46ed 255 {
feb11 1:00f2c40d46ed 256 return (*this = (*this) * b);
feb11 1:00f2c40d46ed 257 }
feb11 1:00f2c40d46ed 258
feb11 10:116e201f7d89 259
feb11 11:2f16a220ebbb 260 BigInt operator/(const BigInt &a, const BigInt &b)
feb11 10:116e201f7d89 261 {
feb11 10:116e201f7d89 262 assert(a.isValid() && b.isValid() && b != 0);
feb11 10:116e201f7d89 263 if(b == 1)
feb11 10:116e201f7d89 264 return a;
feb11 10:116e201f7d89 265 if(a < b)
feb11 10:116e201f7d89 266 return 0;
feb11 10:116e201f7d89 267 if(a == b)
feb11 10:116e201f7d89 268 return 1;
feb11 11:2f16a220ebbb 269 BigInt u = a;
feb11 11:2f16a220ebbb 270 int m = a.numBits() - b.numBits();
feb11 10:116e201f7d89 271 BigInt q = 0;
feb11 12:a436f15b58b6 272 BigInt tmp = b;
feb11 12:a436f15b58b6 273 tmp <<= m;
feb11 10:116e201f7d89 274 for(int j = m; j >= 0; --j)
feb11 10:116e201f7d89 275 {
feb11 11:2f16a220ebbb 276 if(tmp <= u)
feb11 10:116e201f7d89 277 {
feb11 11:2f16a220ebbb 278 u -= tmp;
feb11 11:2f16a220ebbb 279 BigInt tmp2 = 1;
feb11 11:2f16a220ebbb 280 tmp2 <<= j;
feb11 12:a436f15b58b6 281 q += tmp2;
feb11 10:116e201f7d89 282 }
feb11 10:116e201f7d89 283 tmp >>= 1;
feb11 10:116e201f7d89 284 }
feb11 11:2f16a220ebbb 285 q.trim();
feb11 11:2f16a220ebbb 286 return q;
feb11 10:116e201f7d89 287 }
feb11 10:116e201f7d89 288
feb11 10:116e201f7d89 289 BigInt& BigInt::operator/=(const BigInt &b)
feb11 10:116e201f7d89 290 {
feb11 10:116e201f7d89 291 return (*this = (*this) / b);
feb11 10:116e201f7d89 292 }
feb11 10:116e201f7d89 293
feb11 1:00f2c40d46ed 294 BigInt operator>>(const BigInt &a, const uint32_t m)
feb11 1:00f2c40d46ed 295 {
feb11 9:3c191fa04f6e 296 assert(a.isValid());
feb11 9:3c191fa04f6e 297
feb11 12:a436f15b58b6 298 if(m == 0)
feb11 12:a436f15b58b6 299 return a;
feb11 12:a436f15b58b6 300 if(m/8 >= a.size)
feb11 12:a436f15b58b6 301 return 0;
feb11 12:a436f15b58b6 302
feb11 1:00f2c40d46ed 303 BigInt result;
feb11 1:00f2c40d46ed 304 result.size = a.size - m/8;
feb11 1:00f2c40d46ed 305 result.bits = new uint32_t[num(result.size)];
feb11 1:00f2c40d46ed 306 uint8_t s = m%32;
feb11 1:00f2c40d46ed 307 for(uint32_t i = 0; i < num(result.size); ++i)
feb11 1:00f2c40d46ed 308 {
feb11 1:00f2c40d46ed 309 if(m/32+i+1 < num(a.size))
feb11 1:00f2c40d46ed 310 result.bits[i] = (a.bits[m/32+i+1] << (32-s)) | (a.bits[m/32+i] >> s);
feb11 1:00f2c40d46ed 311 else
feb11 1:00f2c40d46ed 312 result.bits[i] = (a.bits[m/32+i] >> s);
feb11 1:00f2c40d46ed 313 }
feb11 1:00f2c40d46ed 314
feb11 10:116e201f7d89 315 result.trim();
feb11 10:116e201f7d89 316
feb11 1:00f2c40d46ed 317 return result;
feb11 1:00f2c40d46ed 318 }
feb11 1:00f2c40d46ed 319
feb11 1:00f2c40d46ed 320 BigInt& BigInt::operator>>=(const uint32_t m)
feb11 1:00f2c40d46ed 321 {
feb11 1:00f2c40d46ed 322 return *this = *this >> m;
feb11 1:00f2c40d46ed 323 }
feb11 1:00f2c40d46ed 324
feb11 1:00f2c40d46ed 325 BigInt operator<<(const BigInt &a, const uint32_t m)
feb11 1:00f2c40d46ed 326 {
feb11 9:3c191fa04f6e 327 assert(a.isValid());
feb11 9:3c191fa04f6e 328
feb11 1:00f2c40d46ed 329 BigInt result;
feb11 9:3c191fa04f6e 330
feb11 1:00f2c40d46ed 331 if(m == 0)
feb11 1:00f2c40d46ed 332 return result = a;
feb11 1:00f2c40d46ed 333
feb11 1:00f2c40d46ed 334 result.size = m/8 + a.size;
feb11 1:00f2c40d46ed 335 uint32_t h = a.bits[num(a.size)-1];
feb11 12:a436f15b58b6 336 if((m%32)%8 != 0)
feb11 1:00f2c40d46ed 337 ++result.size;
feb11 1:00f2c40d46ed 338 uint32_t l = num(result.size);
feb11 1:00f2c40d46ed 339 result.bits = new uint32_t[l];
feb11 1:00f2c40d46ed 340 memset(result.bits, 0, sizeof(uint32_t)*l);
feb11 1:00f2c40d46ed 341 uint32_t s = m % 32;
feb11 1:00f2c40d46ed 342 for(uint32_t i = 0; i < num(a.size); ++i)
feb11 1:00f2c40d46ed 343 {
feb11 1:00f2c40d46ed 344 if(i == 0)
feb11 1:00f2c40d46ed 345 result.bits[m/32+i] = a.bits[i] << s;
feb11 1:00f2c40d46ed 346 else
feb11 1:00f2c40d46ed 347 result.bits[m/32+i] = (a.bits[i] << s) | (a.bits[i-1] >> (32-s));
feb11 1:00f2c40d46ed 348 }
feb11 12:a436f15b58b6 349 if(a.bits[num(a.size)-1] && s != 0)
feb11 12:a436f15b58b6 350 result.bits[m/32+num(result.size)-1] |= a.bits[num(a.size)-1] >> (32-s);
feb11 1:00f2c40d46ed 351
feb11 12:a436f15b58b6 352 result.trim();
feb11 1:00f2c40d46ed 353
feb11 1:00f2c40d46ed 354 return result;
feb11 1:00f2c40d46ed 355 }
feb11 1:00f2c40d46ed 356
feb11 1:00f2c40d46ed 357 BigInt& BigInt::operator<<=(const uint32_t m)
feb11 1:00f2c40d46ed 358 {
feb11 1:00f2c40d46ed 359 return (*this = *this << m);
feb11 1:00f2c40d46ed 360 }
feb11 0:9d554894785b 361
feb11 5:beeb31f340a7 362 BigInt operator%(const BigInt &a, const BigInt &b)
feb11 5:beeb31f340a7 363 {
feb11 10:116e201f7d89 364 assert(a.isValid() && b.isValid() && b > 0);
feb11 12:a436f15b58b6 365 return a - (a/b)*b;
feb11 5:beeb31f340a7 366 }
feb11 5:beeb31f340a7 367
feb11 5:beeb31f340a7 368 BigInt& BigInt::operator%=(const BigInt &a)
feb11 5:beeb31f340a7 369 {
feb11 5:beeb31f340a7 370 return (*this = *this % a);
feb11 5:beeb31f340a7 371 }
feb11 5:beeb31f340a7 372
feb11 0:9d554894785b 373 bool operator==(const BigInt &a, const BigInt &b)
feb11 0:9d554894785b 374 {
feb11 9:3c191fa04f6e 375 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 376
feb11 0:9d554894785b 377 if(a.size != b.size)
feb11 0:9d554894785b 378 return false;
feb11 0:9d554894785b 379
feb11 0:9d554894785b 380 uint32_t l = num(a.size);
feb11 0:9d554894785b 381 for(int i = 0; i < l; ++i)
feb11 0:9d554894785b 382 if(a.bits[i] != b.bits[i])
feb11 0:9d554894785b 383 return false;
feb11 0:9d554894785b 384 return true;
feb11 0:9d554894785b 385 }
feb11 0:9d554894785b 386
feb11 0:9d554894785b 387 bool operator!=(const BigInt &a, const BigInt &b)
feb11 0:9d554894785b 388 {
feb11 0:9d554894785b 389 return ! (a==b);
feb11 0:9d554894785b 390 }
feb11 0:9d554894785b 391
feb11 0:9d554894785b 392 bool operator<(const BigInt &a, const BigInt &b)
feb11 0:9d554894785b 393 {
feb11 9:3c191fa04f6e 394 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 395
feb11 0:9d554894785b 396 if(a.size < b.size)
feb11 0:9d554894785b 397 return true;
feb11 0:9d554894785b 398 if(a.size > b.size)
feb11 0:9d554894785b 399 return false;
feb11 0:9d554894785b 400 uint32_t l = num(a.size);
feb11 0:9d554894785b 401 for(int i = l-1; i >= 0; --i)
feb11 12:a436f15b58b6 402 {
feb11 0:9d554894785b 403 if(a.bits[i] < b.bits[i])
feb11 0:9d554894785b 404 return true;
feb11 12:a436f15b58b6 405 else if(a.bits[i] > b.bits[i])
feb11 12:a436f15b58b6 406 return false;
feb11 12:a436f15b58b6 407 }
feb11 0:9d554894785b 408 return false;
feb11 0:9d554894785b 409 }
feb11 0:9d554894785b 410
feb11 0:9d554894785b 411 bool operator<=(const BigInt &a, const BigInt &b)
feb11 0:9d554894785b 412 {
feb11 9:3c191fa04f6e 413 return !(a > b);
feb11 0:9d554894785b 414 }
feb11 0:9d554894785b 415
feb11 0:9d554894785b 416 bool operator>(const BigInt &a, const BigInt &b)
feb11 0:9d554894785b 417 {
feb11 9:3c191fa04f6e 418 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 419
feb11 0:9d554894785b 420 if(a.size > b.size)
feb11 0:9d554894785b 421 return true;
feb11 0:9d554894785b 422 if(a.size < b.size)
feb11 0:9d554894785b 423 return false;
feb11 0:9d554894785b 424 uint32_t l = num(a.size);
feb11 0:9d554894785b 425 for(int i = l-1; i >= 0; --i)
feb11 12:a436f15b58b6 426 {
feb11 0:9d554894785b 427 if(a.bits[i] > b.bits[i])
feb11 0:9d554894785b 428 return true;
feb11 12:a436f15b58b6 429 else if(a.bits[i] < b.bits[i])
feb11 12:a436f15b58b6 430 return false;
feb11 12:a436f15b58b6 431 }
feb11 0:9d554894785b 432 return false;
feb11 0:9d554894785b 433 }
feb11 0:9d554894785b 434
feb11 0:9d554894785b 435 bool operator>=(const BigInt &a, const BigInt &b)
feb11 0:9d554894785b 436 {
feb11 9:3c191fa04f6e 437 return !(a < b);
feb11 0:9d554894785b 438 }
feb11 0:9d554894785b 439
feb11 1:00f2c40d46ed 440 bool operator!(const BigInt &a)
feb11 0:9d554894785b 441 {
feb11 9:3c191fa04f6e 442 assert(a.isValid());
feb11 9:3c191fa04f6e 443
feb11 1:00f2c40d46ed 444 if(a.size != 1)
feb11 0:9d554894785b 445 return false;
feb11 1:00f2c40d46ed 446 return a.bits[0] == 0;
feb11 0:9d554894785b 447 }
feb11 0:9d554894785b 448
feb11 7:1aad58757705 449
feb11 7:1aad58757705 450 BigInt operator&(const BigInt &a, const BigInt &b)
feb11 7:1aad58757705 451 {
feb11 9:3c191fa04f6e 452 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 453
feb11 7:1aad58757705 454 BigInt result;
feb11 7:1aad58757705 455
feb11 7:1aad58757705 456 result.size = a.size < b.size ? a.size : b.size;
feb11 7:1aad58757705 457 uint32_t l = num(result.size);
feb11 7:1aad58757705 458 result.bits = new uint32_t[l];
feb11 7:1aad58757705 459 memset(result.bits, 0, l);
feb11 7:1aad58757705 460
feb11 7:1aad58757705 461 for(uint32_t i = 0; i < l; ++i)
feb11 7:1aad58757705 462 result.bits[i] = a.bits[i] & b.bits[i];
feb11 7:1aad58757705 463
feb11 9:3c191fa04f6e 464 result.trim();
feb11 7:1aad58757705 465
feb11 7:1aad58757705 466 return result;
feb11 7:1aad58757705 467 }
feb11 7:1aad58757705 468
feb11 7:1aad58757705 469 BigInt& BigInt::operator&=(const BigInt &a)
feb11 7:1aad58757705 470 {
feb11 7:1aad58757705 471 return (*this = *this & a);
feb11 7:1aad58757705 472 }
feb11 7:1aad58757705 473
feb11 7:1aad58757705 474 BigInt operator|(const BigInt &a, const BigInt &b)
feb11 7:1aad58757705 475 {
feb11 9:3c191fa04f6e 476 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 477
feb11 7:1aad58757705 478 BigInt result;
feb11 7:1aad58757705 479
feb11 7:1aad58757705 480 uint32_t na = num(a.size);
feb11 7:1aad58757705 481 uint32_t nb = num(b.size);
feb11 10:116e201f7d89 482 uint32_t l = std::max(na,nb);
feb11 10:116e201f7d89 483 result.size = std::max(a.size, b.size);
feb11 7:1aad58757705 484 result.bits = new uint32_t[l];
feb11 7:1aad58757705 485 memset(result.bits, 0, l);
feb11 7:1aad58757705 486
feb11 7:1aad58757705 487 for(uint32_t i = 0; i < l; ++i)
feb11 7:1aad58757705 488 {
feb11 7:1aad58757705 489 if(i < na && i < nb)
feb11 7:1aad58757705 490 result.bits[i] = a.bits[i] | b.bits[i];
feb11 7:1aad58757705 491 else if(i < na)
feb11 7:1aad58757705 492 result.bits[i] = a.bits[i];
feb11 7:1aad58757705 493 else
feb11 7:1aad58757705 494 result.bits[i] = b.bits[i];
feb11 7:1aad58757705 495 }
feb11 7:1aad58757705 496
feb11 7:1aad58757705 497 return result;
feb11 7:1aad58757705 498 }
feb11 7:1aad58757705 499
feb11 7:1aad58757705 500 BigInt& BigInt::operator|=(const BigInt &a)
feb11 7:1aad58757705 501 {
feb11 7:1aad58757705 502 return (*this = *this | a);
feb11 7:1aad58757705 503 }
feb11 8:2a361f1b41da 504
feb11 8:2a361f1b41da 505 BigInt operator^(const BigInt &a, const BigInt &b)
feb11 8:2a361f1b41da 506 {
feb11 9:3c191fa04f6e 507 assert(a.isValid() && b.isValid());
feb11 9:3c191fa04f6e 508
feb11 9:3c191fa04f6e 509
feb11 8:2a361f1b41da 510 BigInt result;
feb11 8:2a361f1b41da 511
feb11 8:2a361f1b41da 512 uint32_t na = num(a.size);
feb11 8:2a361f1b41da 513 uint32_t nb = num(b.size);
feb11 10:116e201f7d89 514 uint32_t l = std::max(na,nb);
feb11 10:116e201f7d89 515 result.size = std::max(a.size, b.size);
feb11 8:2a361f1b41da 516 result.bits = new uint32_t[l];
feb11 8:2a361f1b41da 517 memset(result.bits, 0, l);
feb11 7:1aad58757705 518
feb11 8:2a361f1b41da 519 for(uint32_t i = 0; i < l; ++i)
feb11 8:2a361f1b41da 520 {
feb11 8:2a361f1b41da 521 if(i < na && i < nb)
feb11 8:2a361f1b41da 522 result.bits[i] = a.bits[i] ^ b.bits[i];
feb11 8:2a361f1b41da 523 else if(i < na)
feb11 8:2a361f1b41da 524 result.bits[i] = a.bits[i];
feb11 8:2a361f1b41da 525 else
feb11 8:2a361f1b41da 526 result.bits[i] = b.bits[i];
feb11 8:2a361f1b41da 527 }
feb11 8:2a361f1b41da 528
feb11 9:3c191fa04f6e 529 result.trim();
feb11 9:3c191fa04f6e 530
feb11 8:2a361f1b41da 531 return result;
feb11 8:2a361f1b41da 532 }
feb11 8:2a361f1b41da 533
feb11 8:2a361f1b41da 534 BigInt& BigInt::operator^=(const BigInt &a)
feb11 8:2a361f1b41da 535 {
feb11 8:2a361f1b41da 536 return (*this = *this ^ a);
feb11 8:2a361f1b41da 537 }
feb11 9:3c191fa04f6e 538
feb11 10:116e201f7d89 539 BigInt montgomeryStep(const BigInt &a, const BigInt &b, const BigInt &m, uint32_t r)
feb11 10:116e201f7d89 540 {
feb11 10:116e201f7d89 541 BigInt result = 0;
feb11 10:116e201f7d89 542 BigInt tmp = a;
feb11 10:116e201f7d89 543 while(r > 0)
feb11 10:116e201f7d89 544 {
feb11 10:116e201f7d89 545 if(tmp.bits[0] & 0x01)
feb11 10:116e201f7d89 546 result += b;
feb11 10:116e201f7d89 547
feb11 10:116e201f7d89 548 if(result.bits[0] & 0x01)
feb11 10:116e201f7d89 549 result += m;
feb11 10:116e201f7d89 550
feb11 10:116e201f7d89 551 --r;
feb11 10:116e201f7d89 552 result >>= 1;
feb11 10:116e201f7d89 553 tmp >>= 1;
feb11 10:116e201f7d89 554 }
feb11 10:116e201f7d89 555 return result;
feb11 10:116e201f7d89 556 }
feb11 10:116e201f7d89 557
feb11 10:116e201f7d89 558 // Implementation using Montgomery algorithm
feb11 9:3c191fa04f6e 559 BigInt modPow(const BigInt &a, const BigInt &expn, const BigInt &modulus)
feb11 9:3c191fa04f6e 560 {
feb11 9:3c191fa04f6e 561 assert(a.isValid() && expn.isValid() && modulus.isValid());
feb11 9:3c191fa04f6e 562
feb11 10:116e201f7d89 563 // precompute R and R^2
feb11 10:116e201f7d89 564 uint32_t r = 8*modulus.size;
feb11 10:116e201f7d89 565
feb11 10:116e201f7d89 566 // convert a in montgomery world
feb11 11:2f16a220ebbb 567 BigInt tmp2 = 1;
feb11 11:2f16a220ebbb 568 tmp2 = 1 << r;
feb11 11:2f16a220ebbb 569 tmp2 *= a;
feb11 12:a436f15b58b6 570 BigInt montA = tmp2%modulus;
feb11 10:116e201f7d89 571 montA.print();
feb11 10:116e201f7d89 572 BigInt tmp = montA;
feb11 9:3c191fa04f6e 573 BigInt tmpE = expn;
feb11 10:116e201f7d89 574 while(tmpE > 1)
feb11 9:3c191fa04f6e 575 {
feb11 10:116e201f7d89 576 tmp = montgomeryStep(montA, tmp, modulus, r);
feb11 10:116e201f7d89 577 --tmpE;
feb11 9:3c191fa04f6e 578 }
feb11 9:3c191fa04f6e 579
feb11 10:116e201f7d89 580 // convert a to normal world
feb11 10:116e201f7d89 581 return montgomeryStep(tmp, 1, modulus, r);
feb11 9:3c191fa04f6e 582 }
feb11 9:3c191fa04f6e 583
feb11 9:3c191fa04f6e 584 bool BigInt::isValid() const
feb11 9:3c191fa04f6e 585 {
feb11 9:3c191fa04f6e 586 return size != 0 && bits != 0;
feb11 9:3c191fa04f6e 587 }
feb11 9:3c191fa04f6e 588
feb11 11:2f16a220ebbb 589 void BigInt::print() const
feb11 0:9d554894785b 590 {
feb11 11:2f16a220ebbb 591 assert(isValid());
feb11 11:2f16a220ebbb 592
feb11 0:9d554894785b 593 printf("size: %d bytes\n", size);
feb11 0:9d554894785b 594 uint32_t n = num(size);
feb11 0:9d554894785b 595 for(int i = n-1; i >= 0; --i)
feb11 0:9d554894785b 596 {
feb11 1:00f2c40d46ed 597 printf("%08x ", bits[i]);
feb11 0:9d554894785b 598 }
feb11 0:9d554894785b 599 printf("\n");
feb11 0:9d554894785b 600 }
feb11 9:3c191fa04f6e 601
feb11 9:3c191fa04f6e 602 void BigInt::trim()
feb11 9:3c191fa04f6e 603 {
feb11 9:3c191fa04f6e 604 uint8_t *tmp = (uint8_t*)bits;
feb11 9:3c191fa04f6e 605 uint32_t newSize = size;
feb11 9:3c191fa04f6e 606 while(tmp[newSize-1] == 0 && newSize > 0)
feb11 9:3c191fa04f6e 607 newSize--;
feb11 10:116e201f7d89 608 if(newSize == 0)
feb11 10:116e201f7d89 609 newSize = 1;
feb11 9:3c191fa04f6e 610 if(num(newSize) < num(size))
feb11 10:116e201f7d89 611 {
feb11 9:3c191fa04f6e 612 bits = (uint32_t*)realloc(bits, sizeof(uint32_t)*num(newSize));
feb11 10:116e201f7d89 613 }
feb11 9:3c191fa04f6e 614 size = newSize;
feb11 9:3c191fa04f6e 615 }
feb11 9:3c191fa04f6e 616
feb11 11:2f16a220ebbb 617 uint32_t BigInt::numBits() const
feb11 10:116e201f7d89 618 {
feb11 10:116e201f7d89 619 assert(isValid());
feb11 12:a436f15b58b6 620
feb11 10:116e201f7d89 621 uint32_t n = (size-1)*8;
feb11 11:2f16a220ebbb 622 uint8_t a = bits[num(size)-1] >> ((size-1)%4)*8;
feb11 10:116e201f7d89 623 uint8_t tmp2 = 8;
feb11 11:2f16a220ebbb 624 while(!(a & 0x80))
feb11 10:116e201f7d89 625 {
feb11 11:2f16a220ebbb 626 a <<= 1;
feb11 10:116e201f7d89 627 --tmp2;
feb11 10:116e201f7d89 628 }
feb11 10:116e201f7d89 629 n += tmp2;
feb11 10:116e201f7d89 630
feb11 10:116e201f7d89 631 return n;
feb11 10:116e201f7d89 632 }