openssl: update release script to use version 0.9.8y
[msysgit.git] / mingw / include / tclTomMath.h
blob550dafa1cbe5073e58b9aacfd829703f89dfab2b
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 * LibTomMath is a library that provides multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
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.
10 * The library is free for all purposes without any express
11 * guarantee it works.
13 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
15 #ifndef BN_H_
16 #define BN_H_
18 #include <tclTomMathDecls.h>
19 #ifndef MODULE_SCOPE
20 #define MODULE_SCOPE extern
21 #endif
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <limits.h>
29 #ifndef MIN
30 #define MIN(x,y) ((x)<(y)?(x):(y))
31 #endif
33 #ifndef MAX
34 #define MAX(x,y) ((x)>(y)?(x):(y))
35 #endif
37 #ifdef __cplusplus
38 extern "C" {
40 /* C++ compilers don't like assigning void * to mp_digit * */
41 #define OPT_CAST(x) (x *)
43 #else
45 /* C on the other hand doesn't care */
46 #define OPT_CAST(x)
48 #endif
51 /* detect 64-bit mode if possible */
52 #if defined(NEVER) /* 128-bit ints fail in too many places */
53 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
54 #define MP_64BIT
55 #endif
56 #endif
58 /* some default configurations.
60 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
61 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
63 * At the very least a mp_digit must be able to hold 7 bits
64 * [any size beyond that is ok provided it doesn't overflow the data type]
66 #ifdef MP_8BIT
67 #ifndef MP_DIGIT_DECLARED
68 typedef unsigned char mp_digit;
69 #define MP_DIGIT_DECLARED
70 #endif
71 typedef unsigned short mp_word;
72 #elif defined(MP_16BIT)
73 #ifndef MP_DIGIT_DECLARED
74 typedef unsigned short mp_digit;
75 #define MP_DIGIT_DECLARED
76 #endif
77 typedef unsigned long mp_word;
78 #elif defined(MP_64BIT)
79 /* for GCC only on supported platforms */
80 #ifndef CRYPT
81 typedef unsigned long long ulong64;
82 typedef signed long long long64;
83 #endif
85 #ifndef MP_DIGIT_DECLARED
86 typedef unsigned long mp_digit;
87 #define MP_DIGIT_DECLARED
88 #endif
89 typedef unsigned long mp_word __attribute__ ((mode(TI)));
91 #define DIGIT_BIT 60
92 #else
93 /* this is the default case, 28-bit digits */
95 /* this is to make porting into LibTomCrypt easier :-) */
96 #ifndef CRYPT
97 #if defined(_MSC_VER) || defined(__BORLANDC__)
98 typedef unsigned __int64 ulong64;
99 typedef signed __int64 long64;
100 #else
101 typedef unsigned long long ulong64;
102 typedef signed long long long64;
103 #endif
104 #endif
106 #ifndef MP_DIGIT_DECLARED
107 typedef unsigned int mp_digit;
108 #define MP_DIGIT_DECLARED
109 #endif
110 typedef ulong64 mp_word;
112 #ifdef MP_31BIT
113 /* this is an extension that uses 31-bit digits */
114 #define DIGIT_BIT 31
115 #else
116 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
117 #define DIGIT_BIT 28
118 #define MP_28BIT
119 #endif
120 #endif
122 /* define heap macros */
123 #if 0 /* these are macros in tclTomMathDecls.h */
124 #ifndef CRYPT
125 /* default to libc stuff */
126 #ifndef XMALLOC
127 #define XMALLOC malloc
128 #define XFREE free
129 #define XREALLOC realloc
130 #define XCALLOC calloc
131 #else
132 /* prototypes for our heap functions */
133 extern void *XMALLOC(size_t n);
134 extern void *XREALLOC(void *p, size_t n);
135 extern void *XCALLOC(size_t n, size_t s);
136 extern void XFREE(void *p);
137 #endif
138 #endif
139 #endif
142 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
143 #ifndef DIGIT_BIT
144 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
145 #endif
147 #define MP_DIGIT_BIT DIGIT_BIT
148 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
149 #define MP_DIGIT_MAX MP_MASK
151 /* equalities */
152 #define MP_LT -1 /* less than */
153 #define MP_EQ 0 /* equal to */
154 #define MP_GT 1 /* greater than */
156 #define MP_ZPOS 0 /* positive integer */
157 #define MP_NEG 1 /* negative */
159 #define MP_OKAY 0 /* ok result */
160 #define MP_MEM -2 /* out of mem */
161 #define MP_VAL -3 /* invalid input */
162 #define MP_RANGE MP_VAL
164 #define MP_YES 1 /* yes response */
165 #define MP_NO 0 /* no response */
167 /* Primality generation flags */
168 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
169 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
170 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
172 typedef int mp_err;
174 /* you'll have to tune these... */
175 #if defined(BUILD_tcl) || !defined(_WIN32)
176 MODULE_SCOPE int KARATSUBA_MUL_CUTOFF,
177 KARATSUBA_SQR_CUTOFF,
178 TOOM_MUL_CUTOFF,
179 TOOM_SQR_CUTOFF;
180 #endif
182 /* define this to use lower memory usage routines (exptmods mostly) */
183 /* #define MP_LOW_MEM */
185 /* default precision */
186 #ifndef MP_PREC
187 #ifndef MP_LOW_MEM
188 #define MP_PREC 32 /* default digits of precision */
189 #else
190 #define MP_PREC 8 /* default digits of precision */
191 #endif
192 #endif
194 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
195 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
197 /* the infamous mp_int structure */
198 #ifndef MP_INT_DECLARED
199 #define MP_INT_DECLARED
200 typedef struct mp_int mp_int;
201 #endif
202 struct mp_int {
203 int used, alloc, sign;
204 mp_digit *dp;
207 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
208 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
211 #define USED(m) ((m)->used)
212 #define DIGIT(m,k) ((m)->dp[(k)])
213 #define SIGN(m) ((m)->sign)
215 /* error code to char* string */
217 char *mp_error_to_string(int code);
220 /* ---> init and deinit bignum functions <--- */
221 /* init a bignum */
223 int mp_init(mp_int *a);
226 /* free a bignum */
228 void mp_clear(mp_int *a);
231 /* init a null terminated series of arguments */
233 int mp_init_multi(mp_int *mp, ...);
236 /* clear a null terminated series of arguments */
238 void mp_clear_multi(mp_int *mp, ...);
241 /* exchange two ints */
243 void mp_exch(mp_int *a, mp_int *b);
246 /* shrink ram required for a bignum */
248 int mp_shrink(mp_int *a);
251 /* grow an int to a given size */
253 int mp_grow(mp_int *a, int size);
256 /* init to a given number of digits */
258 int mp_init_size(mp_int *a, int size);
261 /* ---> Basic Manipulations <--- */
262 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
263 #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
264 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
266 /* set to zero */
268 void mp_zero(mp_int *a);
271 /* set to a digit */
273 void mp_set(mp_int *a, mp_digit b);
276 /* set a 32-bit const */
278 int mp_set_int(mp_int *a, unsigned long b);
281 /* get a 32-bit value */
282 unsigned long mp_get_int(mp_int * a);
284 /* initialize and set a digit */
286 int mp_init_set (mp_int * a, mp_digit b);
289 /* initialize and set 32-bit value */
291 int mp_init_set_int (mp_int * a, unsigned long b);
294 /* copy, b = a */
296 int mp_copy(mp_int *a, mp_int *b);
299 /* inits and copies, a = b */
301 int mp_init_copy(mp_int *a, mp_int *b);
304 /* trim unused digits */
306 void mp_clamp(mp_int *a);
309 /* ---> digit manipulation <--- */
311 /* right shift by "b" digits */
313 void mp_rshd(mp_int *a, int b);
316 /* left shift by "b" digits */
318 int mp_lshd(mp_int *a, int b);
321 /* c = a / 2**b */
323 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
326 /* b = a/2 */
328 int mp_div_2(mp_int *a, mp_int *b);
331 /* c = a * 2**b */
333 int mp_mul_2d(mp_int *a, int b, mp_int *c);
336 /* b = a*2 */
338 int mp_mul_2(mp_int *a, mp_int *b);
341 /* c = a mod 2**d */
343 int mp_mod_2d(mp_int *a, int b, mp_int *c);
346 /* computes a = 2**b */
348 int mp_2expt(mp_int *a, int b);
351 /* Counts the number of lsbs which are zero before the first zero bit */
353 int mp_cnt_lsb(mp_int *a);
356 /* I Love Earth! */
358 /* makes a pseudo-random int of a given size */
360 int mp_rand(mp_int *a, int digits);
363 /* ---> binary operations <--- */
364 /* c = a XOR b */
366 int mp_xor(mp_int *a, mp_int *b, mp_int *c);
369 /* c = a OR b */
371 int mp_or(mp_int *a, mp_int *b, mp_int *c);
374 /* c = a AND b */
376 int mp_and(mp_int *a, mp_int *b, mp_int *c);
379 /* ---> Basic arithmetic <--- */
381 /* b = -a */
383 int mp_neg(mp_int *a, mp_int *b);
386 /* b = |a| */
388 int mp_abs(mp_int *a, mp_int *b);
391 /* compare a to b */
393 int mp_cmp(mp_int *a, mp_int *b);
396 /* compare |a| to |b| */
398 int mp_cmp_mag(mp_int *a, mp_int *b);
401 /* c = a + b */
403 int mp_add(mp_int *a, mp_int *b, mp_int *c);
406 /* c = a - b */
408 int mp_sub(mp_int *a, mp_int *b, mp_int *c);
411 /* c = a * b */
413 int mp_mul(mp_int *a, mp_int *b, mp_int *c);
416 /* b = a*a */
418 int mp_sqr(mp_int *a, mp_int *b);
421 /* a/b => cb + d == a */
423 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
426 /* c = a mod b, 0 <= c < b */
428 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
431 /* ---> single digit functions <--- */
433 /* compare against a single digit */
435 int mp_cmp_d(mp_int *a, mp_digit b);
438 /* c = a + b */
440 int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
443 /* c = a - b */
445 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
448 /* c = a * b */
450 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
453 /* a/b => cb + d == a */
455 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
458 /* a/3 => 3c + d == a */
460 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
463 /* c = a**b */
465 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
468 /* c = a mod b, 0 <= c < b */
470 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
473 /* ---> number theory <--- */
475 /* d = a + b (mod c) */
477 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
480 /* d = a - b (mod c) */
482 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
485 /* d = a * b (mod c) */
487 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
490 /* c = a * a (mod b) */
492 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
495 /* c = 1/a (mod b) */
497 int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
500 /* c = (a, b) */
502 int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
505 /* produces value such that U1*a + U2*b = U3 */
507 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
510 /* c = [a, b] or (a*b)/(a, b) */
512 int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
515 /* finds one of the b'th root of a, such that |c|**b <= |a|
517 * returns error if a < 0 and b is even
520 int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
523 /* special sqrt algo */
525 int mp_sqrt(mp_int *arg, mp_int *ret);
528 /* is number a square? */
530 int mp_is_square(mp_int *arg, int *ret);
533 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
535 int mp_jacobi(mp_int *a, mp_int *n, int *c);
538 /* used to setup the Barrett reduction for a given modulus b */
540 int mp_reduce_setup(mp_int *a, mp_int *b);
543 /* Barrett Reduction, computes a (mod b) with a precomputed value c
545 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
546 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
549 int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
552 /* setups the montgomery reduction */
554 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
557 /* computes a = B**n mod b without division or multiplication useful for
558 * normalizing numbers in a Montgomery system.
561 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
564 /* computes x/R == x (mod N) via Montgomery Reduction */
566 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
569 /* returns 1 if a is a valid DR modulus */
571 int mp_dr_is_modulus(mp_int *a);
574 /* sets the value of "d" required for mp_dr_reduce */
576 void mp_dr_setup(mp_int *a, mp_digit *d);
579 /* reduces a modulo b using the Diminished Radix method */
581 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
584 /* returns true if a can be reduced with mp_reduce_2k */
586 int mp_reduce_is_2k(mp_int *a);
589 /* determines k value for 2k reduction */
591 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
594 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
596 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
599 /* returns true if a can be reduced with mp_reduce_2k_l */
601 int mp_reduce_is_2k_l(mp_int *a);
604 /* determines k value for 2k reduction */
606 int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
609 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
611 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
614 /* d = a**b (mod c) */
616 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
619 /* ---> Primes <--- */
621 /* number of primes */
622 #ifdef MP_8BIT
623 #define PRIME_SIZE 31
624 #else
625 #define PRIME_SIZE 256
626 #endif
628 /* table of first PRIME_SIZE primes */
629 #if defined(BUILD_tcl) || !defined(_WIN32)
630 MODULE_SCOPE const mp_digit ltm_prime_tab[];
631 #endif
633 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
635 int mp_prime_is_divisible(mp_int *a, int *result);
638 /* performs one Fermat test of "a" using base "b".
639 * Sets result to 0 if composite or 1 if probable prime
642 int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
645 /* performs one Miller-Rabin test of "a" using base "b".
646 * Sets result to 0 if composite or 1 if probable prime
649 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
652 /* This gives [for a given bit size] the number of trials required
653 * such that Miller-Rabin gives a prob of failure lower than 2^-96
656 int mp_prime_rabin_miller_trials(int size);
659 /* performs t rounds of Miller-Rabin on "a" using the first
660 * t prime bases. Also performs an initial sieve of trial
661 * division. Determines if "a" is prime with probability
662 * of error no more than (1/4)**t.
664 * Sets result to 1 if probably prime, 0 otherwise
667 int mp_prime_is_prime(mp_int *a, int t, int *result);
670 /* finds the next prime after the number "a" using "t" trials
671 * of Miller-Rabin.
673 * bbs_style = 1 means the prime must be congruent to 3 mod 4
676 int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
679 /* makes a truly random prime of a given size (bytes),
680 * call with bbs = 1 if you want it to be congruent to 3 mod 4
682 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
683 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
684 * so it can be NULL
686 * The prime generated will be larger than 2^(8*size).
688 #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)
690 /* makes a truly random prime of a given size (bits),
692 * Flags are as follows:
694 * LTM_PRIME_BBS - make prime congruent to 3 mod 4
695 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
696 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
697 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one
699 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
700 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
701 * so it can be NULL
705 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
708 /* ---> radix conversion <--- */
710 int mp_count_bits(mp_int *a);
714 int mp_unsigned_bin_size(mp_int *a);
717 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
720 int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
723 int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
727 int mp_signed_bin_size(mp_int *a);
730 int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
733 int mp_to_signed_bin(mp_int *a, unsigned char *b);
736 int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
740 int mp_read_radix(mp_int *a, const char *str, int radix);
743 int mp_toradix(mp_int *a, char *str, int radix);
746 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
749 int mp_radix_size(mp_int *a, int radix, int *size);
753 int mp_fread(mp_int *a, int radix, FILE *stream);
756 int mp_fwrite(mp_int *a, int radix, FILE *stream);
759 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
760 #define mp_raw_size(mp) mp_signed_bin_size(mp)
761 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
762 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
763 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
764 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
766 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
767 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
768 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
769 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
771 /* lowlevel functions, do not call! */
773 int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
776 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
778 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
780 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
783 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
786 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
789 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
792 int fast_s_mp_sqr(mp_int *a, mp_int *b);
795 int s_mp_sqr(mp_int *a, mp_int *b);
798 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
801 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
804 int mp_karatsuba_sqr(mp_int *a, mp_int *b);
807 int mp_toom_sqr(mp_int *a, mp_int *b);
810 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
813 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
816 int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
819 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
822 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
825 void bn_reverse(unsigned char *s, int len);
828 #if defined(BUILD_tcl) || !defined(_WIN32)
829 MODULE_SCOPE const char *mp_s_rmap;
830 #endif
832 #ifdef __cplusplus
834 #endif
836 #endif