comparison tommath.h @ 1:22d5cf7d4b1a libtommath

Renaming branch
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:23:46 +0000
parents
children a96ff234ff19
comparison
equal deleted inserted replaced
-1:000000000000 1:22d5cf7d4b1a
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2 *
3 * LibTomMath is a library that provides multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
5 *
6 * The library was designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
9 *
10 * The library is free for all purposes without any express
11 * guarantee it works.
12 *
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org
14 */
15 #ifndef BN_H_
16 #define BN_H_
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <limits.h>
23
24 #define NO_LTM_TOOM 1
25 #define NO_LTM_KARATSUBA 1
26
27 #undef MIN
28 #define MIN(x,y) ((x)<(y)?(x):(y))
29 #undef MAX
30 #define MAX(x,y) ((x)>(y)?(x):(y))
31
32 #ifdef __cplusplus
33 extern "C" {
34
35 /* C++ compilers don't like assigning void * to mp_digit * */
36 #define OPT_CAST(x) (x *)
37
38 #else
39
40 /* C on the other hand doesn't care */
41 #define OPT_CAST(x)
42
43 #endif
44
45 /* some default configurations.
46 *
47 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
48 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
49 *
50 * At the very least a mp_digit must be able to hold 7 bits
51 * [any size beyond that is ok provided it doesn't overflow the data type]
52 */
53 #ifdef MP_8BIT
54 typedef unsigned char mp_digit;
55 typedef unsigned short mp_word;
56 #elif defined(MP_16BIT)
57 typedef unsigned short mp_digit;
58 typedef unsigned long mp_word;
59 #elif defined(MP_64BIT)
60 /* for GCC only on supported platforms */
61 #ifndef CRYPT
62 typedef unsigned long long ulong64;
63 typedef signed long long long64;
64 #endif
65
66 typedef ulong64 mp_digit;
67 typedef unsigned long mp_word __attribute__ ((mode(TI)));
68
69 #define DIGIT_BIT 60
70 #else
71 /* this is the default case, 28-bit digits */
72
73 /* this is to make porting into LibTomCrypt easier :-) */
74 #ifndef CRYPT
75 #if defined(_MSC_VER) || defined(__BORLANDC__)
76 typedef unsigned __int64 ulong64;
77 typedef signed __int64 long64;
78 #else
79 typedef unsigned long long ulong64;
80 typedef signed long long long64;
81 #endif
82 #endif
83
84 typedef unsigned long mp_digit;
85 typedef ulong64 mp_word;
86
87 #ifdef MP_31BIT
88 /* this is an extension that uses 31-bit digits */
89 #define DIGIT_BIT 31
90 #else
91 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
92 #define DIGIT_BIT 28
93 #define MP_28BIT
94 #endif
95 #endif
96
97 /* define heap macros */
98 #ifndef CRYPT
99 /* default to libc stuff */
100 #ifndef XMALLOC
101 #define XMALLOC malloc
102 #define XFREE free
103 #define XREALLOC realloc
104 #define XCALLOC calloc
105 #else
106 /* prototypes for our heap functions */
107 extern void *XMALLOC(size_t n);
108 extern void *REALLOC(void *p, size_t n);
109 extern void *XCALLOC(size_t n, size_t s);
110 extern void XFREE(void *p);
111 #endif
112 #endif
113
114
115 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
116 #ifndef DIGIT_BIT
117 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
118 #endif
119
120 #define MP_DIGIT_BIT DIGIT_BIT
121 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
122 #define MP_DIGIT_MAX MP_MASK
123
124 /* equalities */
125 #define MP_LT -1 /* less than */
126 #define MP_EQ 0 /* equal to */
127 #define MP_GT 1 /* greater than */
128
129 #define MP_ZPOS 0 /* positive integer */
130 #define MP_NEG 1 /* negative */
131
132 #define MP_OKAY 0 /* ok result */
133 #define MP_MEM -2 /* out of mem */
134 #define MP_VAL -3 /* invalid input */
135 #define MP_RANGE MP_VAL
136
137 #define MP_YES 1 /* yes response */
138 #define MP_NO 0 /* no response */
139
140 /* Primality generation flags */
141 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
142 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
143 #define LTM_PRIME_2MSB_OFF 0x0004 /* force 2nd MSB to 0 */
144 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
145
146 typedef int mp_err;
147
148 /* you'll have to tune these... */
149 extern int KARATSUBA_MUL_CUTOFF,
150 KARATSUBA_SQR_CUTOFF,
151 TOOM_MUL_CUTOFF,
152 TOOM_SQR_CUTOFF;
153
154 /* define this to use lower memory usage routines (exptmods mostly) */
155 /* #define MP_LOW_MEM */
156
157 /* default precision */
158 #ifndef MP_PREC
159 #ifdef MP_LOW_MEM
160 #define MP_PREC 64 /* default digits of precision */
161 #else
162 #define MP_PREC 8 /* default digits of precision */
163 #endif
164 #endif
165
166 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
167 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
168
169 /* the infamous mp_int structure */
170 typedef struct {
171 int used, alloc, sign;
172 mp_digit *dp;
173 } mp_int;
174
175 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
176 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
177
178
179 #define USED(m) ((m)->used)
180 #define DIGIT(m,k) ((m)->dp[(k)])
181 #define SIGN(m) ((m)->sign)
182
183 /* error code to char* string */
184 char *mp_error_to_string(int code);
185
186 /* ---> init and deinit bignum functions <--- */
187 /* init a bignum */
188 int mp_init(mp_int *a);
189
190 /* free a bignum */
191 void mp_clear(mp_int *a);
192
193 /* init a null terminated series of arguments */
194 int mp_init_multi(mp_int *mp, ...);
195
196 /* clear a null terminated series of arguments */
197 void mp_clear_multi(mp_int *mp, ...);
198
199 /* exchange two ints */
200 void mp_exch(mp_int *a, mp_int *b);
201
202 /* shrink ram required for a bignum */
203 int mp_shrink(mp_int *a);
204
205 /* grow an int to a given size */
206 int mp_grow(mp_int *a, int size);
207
208 /* init to a given number of digits */
209 int mp_init_size(mp_int *a, int size);
210
211 /* ---> Basic Manipulations <--- */
212 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
213 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
214 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
215
216 /* set to zero */
217 void mp_zero(mp_int *a);
218
219 /* set to a digit */
220 void mp_set(mp_int *a, mp_digit b);
221
222 /* set a 32-bit const */
223 int mp_set_int(mp_int *a, unsigned long b);
224
225 /* get a 32-bit value */
226 unsigned long mp_get_int(mp_int * a);
227
228 /* initialize and set a digit */
229 int mp_init_set (mp_int * a, mp_digit b);
230
231 /* initialize and set 32-bit value */
232 int mp_init_set_int (mp_int * a, unsigned long b);
233
234 /* copy, b = a */
235 int mp_copy(mp_int *a, mp_int *b);
236
237 /* inits and copies, a = b */
238 int mp_init_copy(mp_int *a, mp_int *b);
239
240 /* trim unused digits */
241 void mp_clamp(mp_int *a);
242
243 /* ---> digit manipulation <--- */
244
245 /* right shift by "b" digits */
246 void mp_rshd(mp_int *a, int b);
247
248 /* left shift by "b" digits */
249 int mp_lshd(mp_int *a, int b);
250
251 /* c = a / 2**b */
252 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
253
254 /* b = a/2 */
255 int mp_div_2(mp_int *a, mp_int *b);
256
257 /* c = a * 2**b */
258 int mp_mul_2d(mp_int *a, int b, mp_int *c);
259
260 /* b = a*2 */
261 int mp_mul_2(mp_int *a, mp_int *b);
262
263 /* c = a mod 2**d */
264 int mp_mod_2d(mp_int *a, int b, mp_int *c);
265
266 /* computes a = 2**b */
267 int mp_2expt(mp_int *a, int b);
268
269 /* Counts the number of lsbs which are zero before the first zero bit */
270 int mp_cnt_lsb(mp_int *a);
271
272 /* I Love Earth! */
273
274 /* makes a pseudo-random int of a given size */
275 int mp_rand(mp_int *a, int digits);
276
277 /* ---> binary operations <--- */
278 /* c = a XOR b */
279 int mp_xor(mp_int *a, mp_int *b, mp_int *c);
280
281 /* c = a OR b */
282 int mp_or(mp_int *a, mp_int *b, mp_int *c);
283
284 /* c = a AND b */
285 int mp_and(mp_int *a, mp_int *b, mp_int *c);
286
287 /* ---> Basic arithmetic <--- */
288
289 /* b = -a */
290 int mp_neg(mp_int *a, mp_int *b);
291
292 /* b = |a| */
293 int mp_abs(mp_int *a, mp_int *b);
294
295 /* compare a to b */
296 int mp_cmp(mp_int *a, mp_int *b);
297
298 /* compare |a| to |b| */
299 int mp_cmp_mag(mp_int *a, mp_int *b);
300
301 /* c = a + b */
302 int mp_add(mp_int *a, mp_int *b, mp_int *c);
303
304 /* c = a - b */
305 int mp_sub(mp_int *a, mp_int *b, mp_int *c);
306
307 /* c = a * b */
308 int mp_mul(mp_int *a, mp_int *b, mp_int *c);
309
310 /* b = a*a */
311 int mp_sqr(mp_int *a, mp_int *b);
312
313 /* a/b => cb + d == a */
314 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
315
316 /* c = a mod b, 0 <= c < b */
317 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
318
319 /* ---> single digit functions <--- */
320
321 /* compare against a single digit */
322 int mp_cmp_d(mp_int *a, mp_digit b);
323
324 /* c = a + b */
325 int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
326
327 /* c = a - b */
328 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
329
330 /* c = a * b */
331 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
332
333 /* a/b => cb + d == a */
334 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
335
336 /* a/3 => 3c + d == a */
337 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
338
339 /* c = a**b */
340 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
341
342 /* c = a mod b, 0 <= c < b */
343 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
344
345 /* ---> number theory <--- */
346
347 /* d = a + b (mod c) */
348 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
349
350 /* d = a - b (mod c) */
351 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
352
353 /* d = a * b (mod c) */
354 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
355
356 /* c = a * a (mod b) */
357 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
358
359 /* c = 1/a (mod b) */
360 int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
361
362 /* c = (a, b) */
363 int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
364
365 /* produces value such that U1*a + U2*b = U3 */
366 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
367
368 /* c = [a, b] or (a*b)/(a, b) */
369 int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
370
371 /* finds one of the b'th root of a, such that |c|**b <= |a|
372 *
373 * returns error if a < 0 and b is even
374 */
375 int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
376
377 /* special sqrt algo */
378 int mp_sqrt(mp_int *arg, mp_int *ret);
379
380 /* is number a square? */
381 int mp_is_square(mp_int *arg, int *ret);
382
383 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
384 int mp_jacobi(mp_int *a, mp_int *n, int *c);
385
386 /* used to setup the Barrett reduction for a given modulus b */
387 int mp_reduce_setup(mp_int *a, mp_int *b);
388
389 /* Barrett Reduction, computes a (mod b) with a precomputed value c
390 *
391 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
392 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
393 */
394 int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
395
396 /* setups the montgomery reduction */
397 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
398
399 /* computes a = B**n mod b without division or multiplication useful for
400 * normalizing numbers in a Montgomery system.
401 */
402 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
403
404 /* computes x/R == x (mod N) via Montgomery Reduction */
405 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
406
407 /* returns 1 if a is a valid DR modulus */
408 int mp_dr_is_modulus(mp_int *a);
409
410 /* sets the value of "d" required for mp_dr_reduce */
411 void mp_dr_setup(mp_int *a, mp_digit *d);
412
413 /* reduces a modulo b using the Diminished Radix method */
414 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
415
416 /* returns true if a can be reduced with mp_reduce_2k */
417 int mp_reduce_is_2k(mp_int *a);
418
419 /* determines k value for 2k reduction */
420 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
421
422 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
423 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
424
425 /* d = a**b (mod c) */
426 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
427
428 /* ---> Primes <--- */
429
430 /* number of primes */
431 #ifdef MP_8BIT
432 #define PRIME_SIZE 31
433 #else
434 #define PRIME_SIZE 256
435 #endif
436
437 /* table of first PRIME_SIZE primes */
438 extern const mp_digit __prime_tab[];
439
440 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
441 int mp_prime_is_divisible(mp_int *a, int *result);
442
443 /* performs one Fermat test of "a" using base "b".
444 * Sets result to 0 if composite or 1 if probable prime
445 */
446 int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
447
448 /* performs one Miller-Rabin test of "a" using base "b".
449 * Sets result to 0 if composite or 1 if probable prime
450 */
451 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
452
453 /* This gives [for a given bit size] the number of trials required
454 * such that Miller-Rabin gives a prob of failure lower than 2^-96
455 */
456 int mp_prime_rabin_miller_trials(int size);
457
458 /* performs t rounds of Miller-Rabin on "a" using the first
459 * t prime bases. Also performs an initial sieve of trial
460 * division. Determines if "a" is prime with probability
461 * of error no more than (1/4)**t.
462 *
463 * Sets result to 1 if probably prime, 0 otherwise
464 */
465 int mp_prime_is_prime(mp_int *a, int t, int *result);
466
467 /* finds the next prime after the number "a" using "t" trials
468 * of Miller-Rabin.
469 *
470 * bbs_style = 1 means the prime must be congruent to 3 mod 4
471 */
472 int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
473
474 /* makes a truly random prime of a given size (bytes),
475 * call with bbs = 1 if you want it to be congruent to 3 mod 4
476 *
477 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
478 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
479 * so it can be NULL
480 *
481 * The prime generated will be larger than 2^(8*size).
482 */
483 #define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
484
485 /* makes a truly random prime of a given size (bits),
486 *
487 * Flags are as follows:
488 *
489 * LTM_PRIME_BBS - make prime congruent to 3 mod 4
490 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
491 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
492 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one
493 *
494 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
495 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
496 * so it can be NULL
497 *
498 */
499 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
500
501 /* ---> radix conversion <--- */
502 int mp_count_bits(mp_int *a);
503
504 int mp_unsigned_bin_size(mp_int *a);
505 int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
506 int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
507
508 int mp_signed_bin_size(mp_int *a);
509 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
510 int mp_to_signed_bin(mp_int *a, unsigned char *b);
511
512 int mp_read_radix(mp_int *a, char *str, int radix);
513 int mp_toradix(mp_int *a, char *str, int radix);
514 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
515 int mp_radix_size(mp_int *a, int radix, int *size);
516
517 int mp_fread(mp_int *a, int radix, FILE *stream);
518 int mp_fwrite(mp_int *a, int radix, FILE *stream);
519
520 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
521 #define mp_raw_size(mp) mp_signed_bin_size(mp)
522 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
523 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
524 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
525 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
526
527 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
528 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
529 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
530 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
531
532 /* lowlevel functions, do not call! */
533 int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
534 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
535 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
536 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
537 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
538 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
539 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
540 int fast_s_mp_sqr(mp_int *a, mp_int *b);
541 int s_mp_sqr(mp_int *a, mp_int *b);
542 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
543 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
544 int mp_karatsuba_sqr(mp_int *a, mp_int *b);
545 int mp_toom_sqr(mp_int *a, mp_int *b);
546 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
547 int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
548 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
549 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
550 void bn_reverse(unsigned char *s, int len);
551
552 extern const char *mp_s_rmap;
553
554 #ifdef __cplusplus
555 }
556 #endif
557
558 #endif
559