1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 * LibTomMath is library that provides for multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
6 * The library is designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
10 * The library is free for all purposes without any express
13 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
27 #define MIN(x,y) ((x)<(y)?(x):(y))
29 #define MAX(x,y) ((x)>(y)?(x):(y))
34 /* C++ compilers don't like assigning void * to mp_digit * */
35 #define OPT_CAST (mp_digit *)
39 /* C on the other hand doesn't care */
44 /* some default configurations.
46 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
47 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
49 * At the very least a mp_digit must be able to hold 7 bits
50 * [any size beyond that is ok provided it doesn't overflow the data type]
53 typedef unsigned char mp_digit;
54 typedef unsigned short mp_word;
55 #elif defined(MP_16BIT)
56 typedef unsigned short mp_digit;
57 typedef unsigned long mp_word;
58 #elif defined(MP_64BIT)
59 /* for GCC only on supported platforms */
61 typedef unsigned long long ulong64;
62 typedef signed long long long64;
65 typedef ulong64 mp_digit;
66 typedef unsigned long mp_word __attribute__ ((mode(TI)));
70 /* this is the default case, 28-bit digits */
72 /* this is to make porting into LibTomCrypt easier :-) */
74 #if defined(_MSC_VER) || defined(__BORLANDC__)
75 typedef unsigned __int64 ulong64;
76 typedef signed __int64 long64;
78 typedef unsigned long long ulong64;
79 typedef signed long long long64;
83 typedef unsigned long mp_digit;
84 typedef ulong64 mp_word;
93 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
95 #define DIGIT_BIT ((CHAR_BIT * sizeof(mp_digit) - 1)) /* bits per digit */
99 #define MP_DIGIT_BIT DIGIT_BIT
100 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
101 #define MP_DIGIT_MAX MP_MASK
104 #define MP_LT -1 /* less than */
105 #define MP_EQ 0 /* equal to */
106 #define MP_GT 1 /* greater than */
108 #define MP_ZPOS 0 /* positive integer */
109 #define MP_NEG 1 /* negative */
111 #define MP_OKAY 0 /* ok result */
112 #define MP_MEM -2 /* out of mem */
113 #define MP_VAL -3 /* invalid input */
114 #define MP_RANGE MP_VAL
118 /* you'll have to tune these... */
119 extern int KARATSUBA_MUL_CUTOFF,
120 KARATSUBA_SQR_CUTOFF,
124 /* various build options */
125 #define MP_PREC 64 /* default digits of precision (must be power of two) */
127 /* define this to use lower memory usage routines (exptmods mostly) */
128 /* #define MP_LOW_MEM */
130 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
131 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
134 int used, alloc, sign;
138 #define USED(m) ((m)->used)
139 #define DIGIT(m,k) ((m)->dp[k])
140 #define SIGN(m) ((m)->sign)
142 /* ---> init and deinit bignum functions <--- */
145 int mp_init(mp_int *a);
148 void mp_clear(mp_int *a);
150 /* init a null terminated series of arguments */
151 int mp_init_multi(mp_int *mp, ...);
153 /* clear a null terminated series of arguments */
154 void mp_clear_multi(mp_int *mp, ...);
156 /* exchange two ints */
157 void mp_exch(mp_int *a, mp_int *b);
159 /* shrink ram required for a bignum */
160 int mp_shrink(mp_int *a);
162 /* grow an int to a given size */
163 int mp_grow(mp_int *a, int size);
165 /* init to a given number of digits */
166 int mp_init_size(mp_int *a, int size);
168 /* ---> Basic Manipulations <--- */
170 #define mp_iszero(a) (((a)->used == 0) ? 1 : 0)
171 #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? 1 : 0)
172 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? 1 : 0)
175 void mp_zero(mp_int *a);
178 void mp_set(mp_int *a, mp_digit b);
180 /* set a 32-bit const */
181 int mp_set_int(mp_int *a, unsigned int b);
184 int mp_copy(mp_int *a, mp_int *b);
186 /* inits and copies, a = b */
187 int mp_init_copy(mp_int *a, mp_int *b);
189 /* trim unused digits */
190 void mp_clamp(mp_int *a);
192 /* ---> digit manipulation <--- */
194 /* right shift by "b" digits */
195 void mp_rshd(mp_int *a, int b);
197 /* left shift by "b" digits */
198 int mp_lshd(mp_int *a, int b);
201 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
204 int mp_div_2(mp_int *a, mp_int *b);
207 int mp_mul_2d(mp_int *a, int b, mp_int *c);
210 int mp_mul_2(mp_int *a, mp_int *b);
213 int mp_mod_2d(mp_int *a, int b, mp_int *c);
215 /* computes a = 2**b */
216 int mp_2expt(mp_int *a, int b);
218 /* makes a pseudo-random int of a given size */
219 int mp_rand(mp_int *a, int digits);
221 /* ---> binary operations <--- */
223 int mp_xor(mp_int *a, mp_int *b, mp_int *c);
226 int mp_or(mp_int *a, mp_int *b, mp_int *c);
229 int mp_and(mp_int *a, mp_int *b, mp_int *c);
231 /* ---> Basic arithmetic <--- */
234 int mp_neg(mp_int *a, mp_int *b);
237 int mp_abs(mp_int *a, mp_int *b);
240 int mp_cmp(mp_int *a, mp_int *b);
242 /* compare |a| to |b| */
243 int mp_cmp_mag(mp_int *a, mp_int *b);
246 int mp_add(mp_int *a, mp_int *b, mp_int *c);
249 int mp_sub(mp_int *a, mp_int *b, mp_int *c);
252 int mp_mul(mp_int *a, mp_int *b, mp_int *c);
255 int mp_sqr(mp_int *a, mp_int *b);
257 /* a/b => cb + d == a */
258 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
260 /* c = a mod b, 0 <= c < b */
261 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
263 /* ---> single digit functions <--- */
265 /* compare against a single digit */
266 int mp_cmp_d(mp_int *a, mp_digit b);
269 int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
272 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
275 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
277 /* a/b => cb + d == a */
278 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
280 /* a/3 => 3c + d == a */
281 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
284 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
286 /* c = a mod b, 0 <= c < b */
287 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
289 /* ---> number theory <--- */
291 /* d = a + b (mod c) */
292 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
294 /* d = a - b (mod c) */
295 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
297 /* d = a * b (mod c) */
298 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
300 /* c = a * a (mod b) */
301 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
303 /* c = 1/a (mod b) */
304 int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
307 int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
309 /* c = [a, b] or (a*b)/(a, b) */
310 int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
312 /* finds one of the b'th root of a, such that |c|**b <= |a|
314 * returns error if a < 0 and b is even
316 int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
318 /* shortcut for square root */
319 #define mp_sqrt(a, b) mp_n_root(a, 2, b)
321 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
322 int mp_jacobi(mp_int *a, mp_int *n, int *c);
324 /* used to setup the Barrett reduction for a given modulus b */
325 int mp_reduce_setup(mp_int *a, mp_int *b);
327 /* Barrett Reduction, computes a (mod b) with a precomputed value c
329 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
330 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
332 int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
334 /* setups the montgomery reduction */
335 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
337 /* computes a = B**n mod b without division or multiplication useful for
338 * normalizing numbers in a Montgomery system.
340 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
342 /* computes x/R == x (mod N) via Montgomery Reduction */
343 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
345 /* returns 1 if a is a valid DR modulus */
346 int mp_dr_is_modulus(mp_int *a);
348 /* sets the value of "d" required for mp_dr_reduce */
349 void mp_dr_setup(mp_int *a, mp_digit *d);
351 /* reduces a modulo b using the Diminished Radix method */
352 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
354 /* returns true if a can be reduced with mp_reduce_2k */
355 int mp_reduce_is_2k(mp_int *a);
357 /* determines k value for 2k reduction */
358 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
360 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
361 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit k);
363 /* d = a**b (mod c) */
364 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
366 /* ---> Primes <--- */
368 /* number of primes */
370 #define PRIME_SIZE 31
372 #define PRIME_SIZE 256
375 /* table of first PRIME_SIZE primes */
376 extern const mp_digit __prime_tab[];
378 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
379 int mp_prime_is_divisible(mp_int *a, int *result);
381 /* performs one Fermat test of "a" using base "b".
382 * Sets result to 0 if composite or 1 if probable prime
384 int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
386 /* performs one Miller-Rabin test of "a" using base "b".
387 * Sets result to 0 if composite or 1 if probable prime
389 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
391 /* performs t rounds of Miller-Rabin on "a" using the first
392 * t prime bases. Also performs an initial sieve of trial
393 * division. Determines if "a" is prime with probability
394 * of error no more than (1/4)**t.
396 * Sets result to 1 if probably prime, 0 otherwise
398 int mp_prime_is_prime(mp_int *a, int t, int *result);
400 /* finds the next prime after the number "a" using "t" trials
403 int mp_prime_next_prime(mp_int *a, int t);
406 /* ---> radix conversion <--- */
407 int mp_count_bits(mp_int *a);
409 int mp_unsigned_bin_size(mp_int *a);
410 int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
411 int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
413 int mp_signed_bin_size(mp_int *a);
414 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
415 int mp_to_signed_bin(mp_int *a, unsigned char *b);
417 int mp_read_radix(mp_int *a, char *str, int radix);
418 int mp_toradix(mp_int *a, char *str, int radix);
419 int mp_radix_size(mp_int *a, int radix);
421 int mp_fread(mp_int *a, int radix, FILE *stream);
422 int mp_fwrite(mp_int *a, int radix, FILE *stream);
424 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
425 #define mp_raw_size(mp) mp_signed_bin_size(mp)
426 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
427 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
428 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
429 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
431 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
432 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
433 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
434 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
436 /* lowlevel functions, do not call! */
437 int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
438 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
439 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
440 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
441 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
442 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
443 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
444 int fast_s_mp_sqr(mp_int *a, mp_int *b);
445 int s_mp_sqr(mp_int *a, mp_int *b);
446 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
447 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
448 int mp_karatsuba_sqr(mp_int *a, mp_int *b);
449 int mp_toom_sqr(mp_int *a, mp_int *b);
450 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
451 int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
452 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
453 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
454 void bn_reverse(unsigned char *s, int len);