Upgrade to Tcl/Tk 8.5b2
[msysgit.git] / mingw / include / tclTomMath.h
blobd05178c186afb987a32e0b09a2093fa5bbdf0f3c
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 #include <tommath_class.h>
31 #ifndef MIN
32 #define MIN(x,y) ((x)<(y)?(x):(y))
33 #endif
35 #ifndef MAX
36 #define MAX(x,y) ((x)>(y)?(x):(y))
37 #endif
39 #ifdef __cplusplus
40 extern "C" {
42 /* C++ compilers don't like assigning void * to mp_digit * */
43 #define OPT_CAST(x) (x *)
45 #else
47 /* C on the other hand doesn't care */
48 #define OPT_CAST(x)
50 #endif
53 /* detect 64-bit mode if possible */
54 #if defined(NEVER) /* 128-bit ints fail in too many places */
55 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
56 #define MP_64BIT
57 #endif
58 #endif
60 /* some default configurations.
62 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
63 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
65 * At the very least a mp_digit must be able to hold 7 bits
66 * [any size beyond that is ok provided it doesn't overflow the data type]
68 #ifdef MP_8BIT
69 #ifndef MP_DIGIT_DECLARED
70 typedef unsigned char mp_digit;
71 #define MP_DIGIT_DECLARED
72 #endif
73 typedef unsigned short mp_word;
74 #elif defined(MP_16BIT)
75 #ifndef MP_DIGIT_DECLARED
76 typedef unsigned short mp_digit;
77 #define MP_DIGIT_DECLARED
78 #endif
79 typedef unsigned long mp_word;
80 #elif defined(MP_64BIT)
81 /* for GCC only on supported platforms */
82 #ifndef CRYPT
83 typedef unsigned long long ulong64;
84 typedef signed long long long64;
85 #endif
87 #ifndef MP_DIGIT_DECLARED
88 typedef unsigned long mp_digit;
89 #define MP_DIGIT_DECLARED
90 #endif
91 typedef unsigned long mp_word __attribute__ ((mode(TI)));
93 #define DIGIT_BIT 60
94 #else
95 /* this is the default case, 28-bit digits */
97 /* this is to make porting into LibTomCrypt easier :-) */
98 #ifndef CRYPT
99 #if defined(_MSC_VER) || defined(__BORLANDC__)
100 typedef unsigned __int64 ulong64;
101 typedef signed __int64 long64;
102 #else
103 typedef unsigned long long ulong64;
104 typedef signed long long long64;
105 #endif
106 #endif
108 #ifndef MP_DIGIT_DECLARED
109 typedef unsigned int mp_digit;
110 #define MP_DIGIT_DECLARED
111 #endif
112 typedef ulong64 mp_word;
114 #ifdef MP_31BIT
115 /* this is an extension that uses 31-bit digits */
116 #define DIGIT_BIT 31
117 #else
118 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
119 #define DIGIT_BIT 28
120 #define MP_28BIT
121 #endif
122 #endif
124 /* define heap macros */
125 #if 0 /* these are macros in tclTomMathDecls.h */
126 #ifndef CRYPT
127 /* default to libc stuff */
128 #ifndef XMALLOC
129 #define XMALLOC malloc
130 #define XFREE free
131 #define XREALLOC realloc
132 #define XCALLOC calloc
133 #else
134 /* prototypes for our heap functions */
135 extern void *XMALLOC(size_t n);
136 extern void *XREALLOC(void *p, size_t n);
137 extern void *XCALLOC(size_t n, size_t s);
138 extern void XFREE(void *p);
139 #endif
140 #endif
141 #endif
144 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
145 #ifndef DIGIT_BIT
146 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */
147 #endif
149 #define MP_DIGIT_BIT DIGIT_BIT
150 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
151 #define MP_DIGIT_MAX MP_MASK
153 /* equalities */
154 #define MP_LT -1 /* less than */
155 #define MP_EQ 0 /* equal to */
156 #define MP_GT 1 /* greater than */
158 #define MP_ZPOS 0 /* positive integer */
159 #define MP_NEG 1 /* negative */
161 #define MP_OKAY 0 /* ok result */
162 #define MP_MEM -2 /* out of mem */
163 #define MP_VAL -3 /* invalid input */
164 #define MP_RANGE MP_VAL
166 #define MP_YES 1 /* yes response */
167 #define MP_NO 0 /* no response */
169 /* Primality generation flags */
170 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */
171 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
172 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
174 typedef int mp_err;
176 /* you'll have to tune these... */
177 #if defined(BUILD_tcl) || !defined(_WIN32)
178 MODULE_SCOPE int KARATSUBA_MUL_CUTOFF,
179 KARATSUBA_SQR_CUTOFF,
180 TOOM_MUL_CUTOFF,
181 TOOM_SQR_CUTOFF;
182 #endif
184 /* define this to use lower memory usage routines (exptmods mostly) */
185 /* #define MP_LOW_MEM */
187 /* default precision */
188 #ifndef MP_PREC
189 #ifndef MP_LOW_MEM
190 #define MP_PREC 32 /* default digits of precision */
191 #else
192 #define MP_PREC 8 /* default digits of precision */
193 #endif
194 #endif
196 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
197 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
199 /* the infamous mp_int structure */
200 #ifndef MP_INT_DECLARED
201 #define MP_INT_DECLARED
202 typedef struct mp_int mp_int;
203 #endif
204 struct mp_int {
205 int used, alloc, sign;
206 mp_digit *dp;
209 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
210 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
213 #define USED(m) ((m)->used)
214 #define DIGIT(m,k) ((m)->dp[(k)])
215 #define SIGN(m) ((m)->sign)
217 /* error code to char* string */
219 char *mp_error_to_string(int code);
222 /* ---> init and deinit bignum functions <--- */
223 /* init a bignum */
225 int mp_init(mp_int *a);
228 /* free a bignum */
230 void mp_clear(mp_int *a);
233 /* init a null terminated series of arguments */
235 int mp_init_multi(mp_int *mp, ...);
238 /* clear a null terminated series of arguments */
240 void mp_clear_multi(mp_int *mp, ...);
243 /* exchange two ints */
245 void mp_exch(mp_int *a, mp_int *b);
248 /* shrink ram required for a bignum */
250 int mp_shrink(mp_int *a);
253 /* grow an int to a given size */
255 int mp_grow(mp_int *a, int size);
258 /* init to a given number of digits */
260 int mp_init_size(mp_int *a, int size);
263 /* ---> Basic Manipulations <--- */
264 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
265 #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
266 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
268 /* set to zero */
270 void mp_zero(mp_int *a);
273 /* set to a digit */
275 void mp_set(mp_int *a, mp_digit b);
278 /* set a 32-bit const */
280 int mp_set_int(mp_int *a, unsigned long b);
283 /* get a 32-bit value */
284 unsigned long mp_get_int(mp_int * a);
286 /* initialize and set a digit */
288 int mp_init_set (mp_int * a, mp_digit b);
291 /* initialize and set 32-bit value */
293 int mp_init_set_int (mp_int * a, unsigned long b);
296 /* copy, b = a */
298 int mp_copy(mp_int *a, mp_int *b);
301 /* inits and copies, a = b */
303 int mp_init_copy(mp_int *a, mp_int *b);
306 /* trim unused digits */
308 void mp_clamp(mp_int *a);
311 /* ---> digit manipulation <--- */
313 /* right shift by "b" digits */
315 void mp_rshd(mp_int *a, int b);
318 /* left shift by "b" digits */
320 int mp_lshd(mp_int *a, int b);
323 /* c = a / 2**b */
325 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
328 /* b = a/2 */
330 int mp_div_2(mp_int *a, mp_int *b);
333 /* c = a * 2**b */
335 int mp_mul_2d(mp_int *a, int b, mp_int *c);
338 /* b = a*2 */
340 int mp_mul_2(mp_int *a, mp_int *b);
343 /* c = a mod 2**d */
345 int mp_mod_2d(mp_int *a, int b, mp_int *c);
348 /* computes a = 2**b */
350 int mp_2expt(mp_int *a, int b);
353 /* Counts the number of lsbs which are zero before the first zero bit */
355 int mp_cnt_lsb(mp_int *a);
358 /* I Love Earth! */
360 /* makes a pseudo-random int of a given size */
362 int mp_rand(mp_int *a, int digits);
365 /* ---> binary operations <--- */
366 /* c = a XOR b */
368 int mp_xor(mp_int *a, mp_int *b, mp_int *c);
371 /* c = a OR b */
373 int mp_or(mp_int *a, mp_int *b, mp_int *c);
376 /* c = a AND b */
378 int mp_and(mp_int *a, mp_int *b, mp_int *c);
381 /* ---> Basic arithmetic <--- */
383 /* b = -a */
385 int mp_neg(mp_int *a, mp_int *b);
388 /* b = |a| */
390 int mp_abs(mp_int *a, mp_int *b);
393 /* compare a to b */
395 int mp_cmp(mp_int *a, mp_int *b);
398 /* compare |a| to |b| */
400 int mp_cmp_mag(mp_int *a, mp_int *b);
403 /* c = a + b */
405 int mp_add(mp_int *a, mp_int *b, mp_int *c);
408 /* c = a - b */
410 int mp_sub(mp_int *a, mp_int *b, mp_int *c);
413 /* c = a * b */
415 int mp_mul(mp_int *a, mp_int *b, mp_int *c);
418 /* b = a*a */
420 int mp_sqr(mp_int *a, mp_int *b);
423 /* a/b => cb + d == a */
425 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
428 /* c = a mod b, 0 <= c < b */
430 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
433 /* ---> single digit functions <--- */
435 /* compare against a single digit */
437 int mp_cmp_d(mp_int *a, mp_digit b);
440 /* c = a + b */
442 int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
445 /* c = a - b */
447 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
450 /* c = a * b */
452 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
455 /* a/b => cb + d == a */
457 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
460 /* a/3 => 3c + d == a */
462 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
465 /* c = a**b */
467 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
470 /* c = a mod b, 0 <= c < b */
472 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
475 /* ---> number theory <--- */
477 /* d = a + b (mod c) */
479 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
482 /* d = a - b (mod c) */
484 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
487 /* d = a * b (mod c) */
489 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
492 /* c = a * a (mod b) */
494 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
497 /* c = 1/a (mod b) */
499 int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
502 /* c = (a, b) */
504 int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
507 /* produces value such that U1*a + U2*b = U3 */
509 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
512 /* c = [a, b] or (a*b)/(a, b) */
514 int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
517 /* finds one of the b'th root of a, such that |c|**b <= |a|
519 * returns error if a < 0 and b is even
522 int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
525 /* special sqrt algo */
527 int mp_sqrt(mp_int *arg, mp_int *ret);
530 /* is number a square? */
532 int mp_is_square(mp_int *arg, int *ret);
535 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
537 int mp_jacobi(mp_int *a, mp_int *n, int *c);
540 /* used to setup the Barrett reduction for a given modulus b */
542 int mp_reduce_setup(mp_int *a, mp_int *b);
545 /* Barrett Reduction, computes a (mod b) with a precomputed value c
547 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
548 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
551 int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
554 /* setups the montgomery reduction */
556 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
559 /* computes a = B**n mod b without division or multiplication useful for
560 * normalizing numbers in a Montgomery system.
563 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
566 /* computes x/R == x (mod N) via Montgomery Reduction */
568 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
571 /* returns 1 if a is a valid DR modulus */
573 int mp_dr_is_modulus(mp_int *a);
576 /* sets the value of "d" required for mp_dr_reduce */
578 void mp_dr_setup(mp_int *a, mp_digit *d);
581 /* reduces a modulo b using the Diminished Radix method */
583 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
586 /* returns true if a can be reduced with mp_reduce_2k */
588 int mp_reduce_is_2k(mp_int *a);
591 /* determines k value for 2k reduction */
593 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
596 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
598 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
601 /* returns true if a can be reduced with mp_reduce_2k_l */
603 int mp_reduce_is_2k_l(mp_int *a);
606 /* determines k value for 2k reduction */
608 int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
611 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
613 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
616 /* d = a**b (mod c) */
618 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
621 /* ---> Primes <--- */
623 /* number of primes */
624 #ifdef MP_8BIT
625 #define PRIME_SIZE 31
626 #else
627 #define PRIME_SIZE 256
628 #endif
630 /* table of first PRIME_SIZE primes */
631 #if defined(BUILD_tcl) || !defined(_WIN32)
632 MODULE_SCOPE const mp_digit ltm_prime_tab[];
633 #endif
635 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
637 int mp_prime_is_divisible(mp_int *a, int *result);
640 /* performs one Fermat test of "a" using base "b".
641 * Sets result to 0 if composite or 1 if probable prime
644 int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
647 /* performs one Miller-Rabin test of "a" using base "b".
648 * Sets result to 0 if composite or 1 if probable prime
651 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
654 /* This gives [for a given bit size] the number of trials required
655 * such that Miller-Rabin gives a prob of failure lower than 2^-96
658 int mp_prime_rabin_miller_trials(int size);
661 /* performs t rounds of Miller-Rabin on "a" using the first
662 * t prime bases. Also performs an initial sieve of trial
663 * division. Determines if "a" is prime with probability
664 * of error no more than (1/4)**t.
666 * Sets result to 1 if probably prime, 0 otherwise
669 int mp_prime_is_prime(mp_int *a, int t, int *result);
672 /* finds the next prime after the number "a" using "t" trials
673 * of Miller-Rabin.
675 * bbs_style = 1 means the prime must be congruent to 3 mod 4
678 int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
681 /* makes a truly random prime of a given size (bytes),
682 * call with bbs = 1 if you want it to be congruent to 3 mod 4
684 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
685 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
686 * so it can be NULL
688 * The prime generated will be larger than 2^(8*size).
690 #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)
692 /* makes a truly random prime of a given size (bits),
694 * Flags are as follows:
696 * LTM_PRIME_BBS - make prime congruent to 3 mod 4
697 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
698 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
699 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one
701 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
702 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
703 * so it can be NULL
707 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
710 /* ---> radix conversion <--- */
712 int mp_count_bits(mp_int *a);
716 int mp_unsigned_bin_size(mp_int *a);
719 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
722 int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
725 int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
729 int mp_signed_bin_size(mp_int *a);
732 int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
735 int mp_to_signed_bin(mp_int *a, unsigned char *b);
738 int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
742 int mp_read_radix(mp_int *a, const char *str, int radix);
745 int mp_toradix(mp_int *a, char *str, int radix);
748 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
751 int mp_radix_size(mp_int *a, int radix, int *size);
755 int mp_fread(mp_int *a, int radix, FILE *stream);
758 int mp_fwrite(mp_int *a, int radix, FILE *stream);
761 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
762 #define mp_raw_size(mp) mp_signed_bin_size(mp)
763 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
764 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
765 #define mp_mag_size(mp) mp_unsigned_bin_size(mp)
766 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
768 #define mp_tobinary(M, S) mp_toradix((M), (S), 2)
769 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
770 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
771 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
773 /* lowlevel functions, do not call! */
775 int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
778 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
780 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
782 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
785 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
788 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
791 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
794 int fast_s_mp_sqr(mp_int *a, mp_int *b);
797 int s_mp_sqr(mp_int *a, mp_int *b);
800 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
803 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
806 int mp_karatsuba_sqr(mp_int *a, mp_int *b);
809 int mp_toom_sqr(mp_int *a, mp_int *b);
812 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
815 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
818 int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
821 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
824 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
827 void bn_reverse(unsigned char *s, int len);
830 #if defined(BUILD_tcl) || !defined(_WIN32)
831 MODULE_SCOPE const char *mp_s_rmap;
832 #endif
834 #ifdef __cplusplus
836 #endif
838 #endif
841 /* $Source: /cvsroot/tcl/tcl/generic/tclTomMath.h,v $ */
842 /* Based on Tom's version 1.8 */
843 /* $Revision: 1.10 $ */
844 /* $Date: 2007/02/14 17:59:21 $ */