tclTomMath.hGo to the documentation of this file.00001 /* LibTomMath, multiple-precision integer library -- Tom St Denis 00002 * 00003 * LibTomMath is a library that provides multiple-precision 00004 * integer arithmetic as well as number theoretic functionality. 00005 * 00006 * The library was designed directly after the MPI library by 00007 * Michael Fromberger but has been written from scratch with 00008 * additional optimizations in place. 00009 * 00010 * The library is free for all purposes without any express 00011 * guarantee it works. 00012 * 00013 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com 00014 */ 00015 #ifndef BN_H_ 00016 #define BN_H_ 00017 00018 #include <tclTomMathDecls.h> 00019 #ifndef MODULE_SCOPE 00020 #define MODULE_SCOPE extern 00021 #endif 00022 00023 #include <stdio.h> 00024 #include <string.h> 00025 #include <stdlib.h> 00026 #include <ctype.h> 00027 #include <limits.h> 00028 00029 #include <tommath_class.h> 00030 00031 #ifndef MIN 00032 #define MIN(x,y) ((x)<(y)?(x):(y)) 00033 #endif 00034 00035 #ifndef MAX 00036 #define MAX(x,y) ((x)>(y)?(x):(y)) 00037 #endif 00038 00039 #ifdef __cplusplus 00040 extern "C" { 00041 00042 /* C++ compilers don't like assigning void * to mp_digit * */ 00043 #define OPT_CAST(x) (x *) 00044 00045 #else 00046 00047 /* C on the other hand doesn't care */ 00048 #define OPT_CAST(x) 00049 00050 #endif 00051 00052 00053 /* detect 64-bit mode if possible */ 00054 #if defined(NEVER) /* 128-bit ints fail in too many places */ 00055 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT)) 00056 #define MP_64BIT 00057 #endif 00058 #endif 00059 00060 /* some default configurations. 00061 * 00062 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits 00063 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits 00064 * 00065 * At the very least a mp_digit must be able to hold 7 bits 00066 * [any size beyond that is ok provided it doesn't overflow the data type] 00067 */ 00068 #ifdef MP_8BIT 00069 #ifndef MP_DIGIT_DECLARED 00070 typedef unsigned char mp_digit; 00071 #define MP_DIGIT_DECLARED 00072 #endif 00073 typedef unsigned short mp_word; 00074 #elif defined(MP_16BIT) 00075 #ifndef MP_DIGIT_DECLARED 00076 typedef unsigned short mp_digit; 00077 #define MP_DIGIT_DECLARED 00078 #endif 00079 typedef unsigned long mp_word; 00080 #elif defined(MP_64BIT) 00081 /* for GCC only on supported platforms */ 00082 #ifndef CRYPT 00083 typedef unsigned long long ulong64; 00084 typedef signed long long long64; 00085 #endif 00086 00087 #ifndef MP_DIGIT_DECLARED 00088 typedef unsigned long mp_digit; 00089 #define MP_DIGIT_DECLARED 00090 #endif 00091 typedef unsigned long mp_word __attribute__ ((mode(TI))); 00092 00093 #define DIGIT_BIT 60 00094 #else 00095 /* this is the default case, 28-bit digits */ 00096 00097 /* this is to make porting into LibTomCrypt easier :-) */ 00098 #ifndef CRYPT 00099 #if defined(_MSC_VER) || defined(__BORLANDC__) 00100 typedef unsigned __int64 ulong64; 00101 typedef signed __int64 long64; 00102 #else 00103 typedef unsigned long long ulong64; 00104 typedef signed long long long64; 00105 #endif 00106 #endif 00107 00108 #ifndef MP_DIGIT_DECLARED 00109 typedef unsigned int mp_digit; 00110 #define MP_DIGIT_DECLARED 00111 #endif 00112 typedef ulong64 mp_word; 00113 00114 #ifdef MP_31BIT 00115 /* this is an extension that uses 31-bit digits */ 00116 #define DIGIT_BIT 31 00117 #else 00118 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */ 00119 #define DIGIT_BIT 28 00120 #define MP_28BIT 00121 #endif 00122 #endif 00123 00124 /* define heap macros */ 00125 #if 0 /* these are macros in tclTomMathDecls.h */ 00126 #ifndef CRYPT 00127 /* default to libc stuff */ 00128 #ifndef XMALLOC 00129 #define XMALLOC malloc 00130 #define XFREE free 00131 #define XREALLOC realloc 00132 #define XCALLOC calloc 00133 #else 00134 /* prototypes for our heap functions */ 00135 extern void *XMALLOC(size_t n); 00136 extern void *XREALLOC(void *p, size_t n); 00137 extern void *XCALLOC(size_t n, size_t s); 00138 extern void XFREE(void *p); 00139 #endif 00140 #endif 00141 #endif 00142 00143 00144 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ 00145 #ifndef DIGIT_BIT 00146 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */ 00147 #endif 00148 00149 #define MP_DIGIT_BIT DIGIT_BIT 00150 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) 00151 #define MP_DIGIT_MAX MP_MASK 00152 00153 /* equalities */ 00154 #define MP_LT -1 /* less than */ 00155 #define MP_EQ 0 /* equal to */ 00156 #define MP_GT 1 /* greater than */ 00157 00158 #define MP_ZPOS 0 /* positive integer */ 00159 #define MP_NEG 1 /* negative */ 00160 00161 #define MP_OKAY 0 /* ok result */ 00162 #define MP_MEM -2 /* out of mem */ 00163 #define MP_VAL -3 /* invalid input */ 00164 #define MP_RANGE MP_VAL 00165 00166 #define MP_YES 1 /* yes response */ 00167 #define MP_NO 0 /* no response */ 00168 00169 /* Primality generation flags */ 00170 #define LTM_PRIME_BBS 0x0001 /* BBS style prime */ 00171 #define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */ 00172 #define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */ 00173 00174 typedef int mp_err; 00175 00176 /* you'll have to tune these... */ 00177 #if defined(BUILD_tcl) || !defined(_WIN32) 00178 MODULE_SCOPE int KARATSUBA_MUL_CUTOFF, 00179 KARATSUBA_SQR_CUTOFF, 00180 TOOM_MUL_CUTOFF, 00181 TOOM_SQR_CUTOFF; 00182 #endif 00183 00184 /* define this to use lower memory usage routines (exptmods mostly) */ 00185 /* #define MP_LOW_MEM */ 00186 00187 /* default precision */ 00188 #ifndef MP_PREC 00189 #ifndef MP_LOW_MEM 00190 #define MP_PREC 32 /* default digits of precision */ 00191 #else 00192 #define MP_PREC 8 /* default digits of precision */ 00193 #endif 00194 #endif 00195 00196 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ 00197 #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) 00198 00199 /* the infamous mp_int structure */ 00200 #ifndef MP_INT_DECLARED 00201 #define MP_INT_DECLARED 00202 typedef struct mp_int mp_int; 00203 #endif 00204 struct mp_int { 00205 int used, alloc, sign; 00206 mp_digit *dp; 00207 }; 00208 00209 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */ 00210 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); 00211 00212 00213 #define USED(m) ((m)->used) 00214 #define DIGIT(m,k) ((m)->dp[(k)]) 00215 #define SIGN(m) ((m)->sign) 00216 00217 /* error code to char* string */ 00218 /* 00219 char *mp_error_to_string(int code); 00220 */ 00221 00222 /* ---> init and deinit bignum functions <--- */ 00223 /* init a bignum */ 00224 /* 00225 int mp_init(mp_int *a); 00226 */ 00227 00228 /* free a bignum */ 00229 /* 00230 void mp_clear(mp_int *a); 00231 */ 00232 00233 /* init a null terminated series of arguments */ 00234 /* 00235 int mp_init_multi(mp_int *mp, ...); 00236 */ 00237 00238 /* clear a null terminated series of arguments */ 00239 /* 00240 void mp_clear_multi(mp_int *mp, ...); 00241 */ 00242 00243 /* exchange two ints */ 00244 /* 00245 void mp_exch(mp_int *a, mp_int *b); 00246 */ 00247 00248 /* shrink ram required for a bignum */ 00249 /* 00250 int mp_shrink(mp_int *a); 00251 */ 00252 00253 /* grow an int to a given size */ 00254 /* 00255 int mp_grow(mp_int *a, int size); 00256 */ 00257 00258 /* init to a given number of digits */ 00259 /* 00260 int mp_init_size(mp_int *a, int size); 00261 */ 00262 00263 /* ---> Basic Manipulations <--- */ 00264 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) 00265 #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) 00266 #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) 00267 00268 /* set to zero */ 00269 /* 00270 void mp_zero(mp_int *a); 00271 */ 00272 00273 /* set to a digit */ 00274 /* 00275 void mp_set(mp_int *a, mp_digit b); 00276 */ 00277 00278 /* set a 32-bit const */ 00279 /* 00280 int mp_set_int(mp_int *a, unsigned long b); 00281 */ 00282 00283 /* get a 32-bit value */ 00284 unsigned long mp_get_int(mp_int * a); 00285 00286 /* initialize and set a digit */ 00287 /* 00288 int mp_init_set (mp_int * a, mp_digit b); 00289 */ 00290 00291 /* initialize and set 32-bit value */ 00292 /* 00293 int mp_init_set_int (mp_int * a, unsigned long b); 00294 */ 00295 00296 /* copy, b = a */ 00297 /* 00298 int mp_copy(mp_int *a, mp_int *b); 00299 */ 00300 00301 /* inits and copies, a = b */ 00302 /* 00303 int mp_init_copy(mp_int *a, mp_int *b); 00304 */ 00305 00306 /* trim unused digits */ 00307 /* 00308 void mp_clamp(mp_int *a); 00309 */ 00310 00311 /* ---> digit manipulation <--- */ 00312 00313 /* right shift by "b" digits */ 00314 /* 00315 void mp_rshd(mp_int *a, int b); 00316 */ 00317 00318 /* left shift by "b" digits */ 00319 /* 00320 int mp_lshd(mp_int *a, int b); 00321 */ 00322 00323 /* c = a / 2**b */ 00324 /* 00325 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d); 00326 */ 00327 00328 /* b = a/2 */ 00329 /* 00330 int mp_div_2(mp_int *a, mp_int *b); 00331 */ 00332 00333 /* c = a * 2**b */ 00334 /* 00335 int mp_mul_2d(mp_int *a, int b, mp_int *c); 00336 */ 00337 00338 /* b = a*2 */ 00339 /* 00340 int mp_mul_2(mp_int *a, mp_int *b); 00341 */ 00342 00343 /* c = a mod 2**d */ 00344 /* 00345 int mp_mod_2d(mp_int *a, int b, mp_int *c); 00346 */ 00347 00348 /* computes a = 2**b */ 00349 /* 00350 int mp_2expt(mp_int *a, int b); 00351 */ 00352 00353 /* Counts the number of lsbs which are zero before the first zero bit */ 00354 /* 00355 int mp_cnt_lsb(mp_int *a); 00356 */ 00357 00358 /* I Love Earth! */ 00359 00360 /* makes a pseudo-random int of a given size */ 00361 /* 00362 int mp_rand(mp_int *a, int digits); 00363 */ 00364 00365 /* ---> binary operations <--- */ 00366 /* c = a XOR b */ 00367 /* 00368 int mp_xor(mp_int *a, mp_int *b, mp_int *c); 00369 */ 00370 00371 /* c = a OR b */ 00372 /* 00373 int mp_or(mp_int *a, mp_int *b, mp_int *c); 00374 */ 00375 00376 /* c = a AND b */ 00377 /* 00378 int mp_and(mp_int *a, mp_int *b, mp_int *c); 00379 */ 00380 00381 /* ---> Basic arithmetic <--- */ 00382 00383 /* b = -a */ 00384 /* 00385 int mp_neg(mp_int *a, mp_int *b); 00386 */ 00387 00388 /* b = |a| */ 00389 /* 00390 int mp_abs(mp_int *a, mp_int *b); 00391 */ 00392 00393 /* compare a to b */ 00394 /* 00395 int mp_cmp(mp_int *a, mp_int *b); 00396 */ 00397 00398 /* compare |a| to |b| */ 00399 /* 00400 int mp_cmp_mag(mp_int *a, mp_int *b); 00401 */ 00402 00403 /* c = a + b */ 00404 /* 00405 int mp_add(mp_int *a, mp_int *b, mp_int *c); 00406 */ 00407 00408 /* c = a - b */ 00409 /* 00410 int mp_sub(mp_int *a, mp_int *b, mp_int *c); 00411 */ 00412 00413 /* c = a * b */ 00414 /* 00415 int mp_mul(mp_int *a, mp_int *b, mp_int *c); 00416 */ 00417 00418 /* b = a*a */ 00419 /* 00420 int mp_sqr(mp_int *a, mp_int *b); 00421 */ 00422 00423 /* a/b => cb + d == a */ 00424 /* 00425 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00426 */ 00427 00428 /* c = a mod b, 0 <= c < b */ 00429 /* 00430 int mp_mod(mp_int *a, mp_int *b, mp_int *c); 00431 */ 00432 00433 /* ---> single digit functions <--- */ 00434 00435 /* compare against a single digit */ 00436 /* 00437 int mp_cmp_d(mp_int *a, mp_digit b); 00438 */ 00439 00440 /* c = a + b */ 00441 /* 00442 int mp_add_d(mp_int *a, mp_digit b, mp_int *c); 00443 */ 00444 00445 /* c = a - b */ 00446 /* 00447 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); 00448 */ 00449 00450 /* c = a * b */ 00451 /* 00452 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c); 00453 */ 00454 00455 /* a/b => cb + d == a */ 00456 /* 00457 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d); 00458 */ 00459 00460 /* a/3 => 3c + d == a */ 00461 /* 00462 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d); 00463 */ 00464 00465 /* c = a**b */ 00466 /* 00467 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c); 00468 */ 00469 00470 /* c = a mod b, 0 <= c < b */ 00471 /* 00472 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c); 00473 */ 00474 00475 /* ---> number theory <--- */ 00476 00477 /* d = a + b (mod c) */ 00478 /* 00479 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00480 */ 00481 00482 /* d = a - b (mod c) */ 00483 /* 00484 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00485 */ 00486 00487 /* d = a * b (mod c) */ 00488 /* 00489 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00490 */ 00491 00492 /* c = a * a (mod b) */ 00493 /* 00494 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c); 00495 */ 00496 00497 /* c = 1/a (mod b) */ 00498 /* 00499 int mp_invmod(mp_int *a, mp_int *b, mp_int *c); 00500 */ 00501 00502 /* c = (a, b) */ 00503 /* 00504 int mp_gcd(mp_int *a, mp_int *b, mp_int *c); 00505 */ 00506 00507 /* produces value such that U1*a + U2*b = U3 */ 00508 /* 00509 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); 00510 */ 00511 00512 /* c = [a, b] or (a*b)/(a, b) */ 00513 /* 00514 int mp_lcm(mp_int *a, mp_int *b, mp_int *c); 00515 */ 00516 00517 /* finds one of the b'th root of a, such that |c|**b <= |a| 00518 * 00519 * returns error if a < 0 and b is even 00520 */ 00521 /* 00522 int mp_n_root(mp_int *a, mp_digit b, mp_int *c); 00523 */ 00524 00525 /* special sqrt algo */ 00526 /* 00527 int mp_sqrt(mp_int *arg, mp_int *ret); 00528 */ 00529 00530 /* is number a square? */ 00531 /* 00532 int mp_is_square(mp_int *arg, int *ret); 00533 */ 00534 00535 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */ 00536 /* 00537 int mp_jacobi(mp_int *a, mp_int *n, int *c); 00538 */ 00539 00540 /* used to setup the Barrett reduction for a given modulus b */ 00541 /* 00542 int mp_reduce_setup(mp_int *a, mp_int *b); 00543 */ 00544 00545 /* Barrett Reduction, computes a (mod b) with a precomputed value c 00546 * 00547 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely 00548 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code]. 00549 */ 00550 /* 00551 int mp_reduce(mp_int *a, mp_int *b, mp_int *c); 00552 */ 00553 00554 /* setups the montgomery reduction */ 00555 /* 00556 int mp_montgomery_setup(mp_int *a, mp_digit *mp); 00557 */ 00558 00559 /* computes a = B**n mod b without division or multiplication useful for 00560 * normalizing numbers in a Montgomery system. 00561 */ 00562 /* 00563 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); 00564 */ 00565 00566 /* computes x/R == x (mod N) via Montgomery Reduction */ 00567 /* 00568 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); 00569 */ 00570 00571 /* returns 1 if a is a valid DR modulus */ 00572 /* 00573 int mp_dr_is_modulus(mp_int *a); 00574 */ 00575 00576 /* sets the value of "d" required for mp_dr_reduce */ 00577 /* 00578 void mp_dr_setup(mp_int *a, mp_digit *d); 00579 */ 00580 00581 /* reduces a modulo b using the Diminished Radix method */ 00582 /* 00583 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp); 00584 */ 00585 00586 /* returns true if a can be reduced with mp_reduce_2k */ 00587 /* 00588 int mp_reduce_is_2k(mp_int *a); 00589 */ 00590 00591 /* determines k value for 2k reduction */ 00592 /* 00593 int mp_reduce_2k_setup(mp_int *a, mp_digit *d); 00594 */ 00595 00596 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ 00597 /* 00598 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); 00599 */ 00600 00601 /* returns true if a can be reduced with mp_reduce_2k_l */ 00602 /* 00603 int mp_reduce_is_2k_l(mp_int *a); 00604 */ 00605 00606 /* determines k value for 2k reduction */ 00607 /* 00608 int mp_reduce_2k_setup_l(mp_int *a, mp_int *d); 00609 */ 00610 00611 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ 00612 /* 00613 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d); 00614 */ 00615 00616 /* d = a**b (mod c) */ 00617 /* 00618 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 00619 */ 00620 00621 /* ---> Primes <--- */ 00622 00623 /* number of primes */ 00624 #ifdef MP_8BIT 00625 #define PRIME_SIZE 31 00626 #else 00627 #define PRIME_SIZE 256 00628 #endif 00629 00630 /* table of first PRIME_SIZE primes */ 00631 #if defined(BUILD_tcl) || !defined(_WIN32) 00632 MODULE_SCOPE const mp_digit ltm_prime_tab[]; 00633 #endif 00634 00635 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ 00636 /* 00637 int mp_prime_is_divisible(mp_int *a, int *result); 00638 */ 00639 00640 /* performs one Fermat test of "a" using base "b". 00641 * Sets result to 0 if composite or 1 if probable prime 00642 */ 00643 /* 00644 int mp_prime_fermat(mp_int *a, mp_int *b, int *result); 00645 */ 00646 00647 /* performs one Miller-Rabin test of "a" using base "b". 00648 * Sets result to 0 if composite or 1 if probable prime 00649 */ 00650 /* 00651 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result); 00652 */ 00653 00654 /* This gives [for a given bit size] the number of trials required 00655 * such that Miller-Rabin gives a prob of failure lower than 2^-96 00656 */ 00657 /* 00658 int mp_prime_rabin_miller_trials(int size); 00659 */ 00660 00661 /* performs t rounds of Miller-Rabin on "a" using the first 00662 * t prime bases. Also performs an initial sieve of trial 00663 * division. Determines if "a" is prime with probability 00664 * of error no more than (1/4)**t. 00665 * 00666 * Sets result to 1 if probably prime, 0 otherwise 00667 */ 00668 /* 00669 int mp_prime_is_prime(mp_int *a, int t, int *result); 00670 */ 00671 00672 /* finds the next prime after the number "a" using "t" trials 00673 * of Miller-Rabin. 00674 * 00675 * bbs_style = 1 means the prime must be congruent to 3 mod 4 00676 */ 00677 /* 00678 int mp_prime_next_prime(mp_int *a, int t, int bbs_style); 00679 */ 00680 00681 /* makes a truly random prime of a given size (bytes), 00682 * call with bbs = 1 if you want it to be congruent to 3 mod 4 00683 * 00684 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can 00685 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself 00686 * so it can be NULL 00687 * 00688 * The prime generated will be larger than 2^(8*size). 00689 */ 00690 #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) 00691 00692 /* makes a truly random prime of a given size (bits), 00693 * 00694 * Flags are as follows: 00695 * 00696 * LTM_PRIME_BBS - make prime congruent to 3 mod 4 00697 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) 00698 * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero 00699 * LTM_PRIME_2MSB_ON - make the 2nd highest bit one 00700 * 00701 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can 00702 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself 00703 * so it can be NULL 00704 * 00705 */ 00706 /* 00707 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); 00708 */ 00709 00710 /* ---> radix conversion <--- */ 00711 /* 00712 int mp_count_bits(mp_int *a); 00713 */ 00714 00715 /* 00716 int mp_unsigned_bin_size(mp_int *a); 00717 */ 00718 /* 00719 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c); 00720 */ 00721 /* 00722 int mp_to_unsigned_bin(mp_int *a, unsigned char *b); 00723 */ 00724 /* 00725 int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); 00726 */ 00727 00728 /* 00729 int mp_signed_bin_size(mp_int *a); 00730 */ 00731 /* 00732 int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c); 00733 */ 00734 /* 00735 int mp_to_signed_bin(mp_int *a, unsigned char *b); 00736 */ 00737 /* 00738 int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); 00739 */ 00740 00741 /* 00742 int mp_read_radix(mp_int *a, const char *str, int radix); 00743 */ 00744 /* 00745 int mp_toradix(mp_int *a, char *str, int radix); 00746 */ 00747 /* 00748 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); 00749 */ 00750 /* 00751 int mp_radix_size(mp_int *a, int radix, int *size); 00752 */ 00753 00754 /* 00755 int mp_fread(mp_int *a, int radix, FILE *stream); 00756 */ 00757 /* 00758 int mp_fwrite(mp_int *a, int radix, FILE *stream); 00759 */ 00760 00761 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) 00762 #define mp_raw_size(mp) mp_signed_bin_size(mp) 00763 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str)) 00764 #define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len)) 00765 #define mp_mag_size(mp) mp_unsigned_bin_size(mp) 00766 #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str)) 00767 00768 #define mp_tobinary(M, S) mp_toradix((M), (S), 2) 00769 #define mp_tooctal(M, S) mp_toradix((M), (S), 8) 00770 #define mp_todecimal(M, S) mp_toradix((M), (S), 10) 00771 #define mp_tohex(M, S) mp_toradix((M), (S), 16) 00772 00773 /* lowlevel functions, do not call! */ 00774 /* 00775 int s_mp_add(mp_int *a, mp_int *b, mp_int *c); 00776 */ 00777 /* 00778 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c); 00779 */ 00780 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) 00781 /* 00782 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 00783 */ 00784 /* 00785 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 00786 */ 00787 /* 00788 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 00789 */ 00790 /* 00791 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); 00792 */ 00793 /* 00794 int fast_s_mp_sqr(mp_int *a, mp_int *b); 00795 */ 00796 /* 00797 int s_mp_sqr(mp_int *a, mp_int *b); 00798 */ 00799 /* 00800 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c); 00801 */ 00802 /* 00803 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c); 00804 */ 00805 /* 00806 int mp_karatsuba_sqr(mp_int *a, mp_int *b); 00807 */ 00808 /* 00809 int mp_toom_sqr(mp_int *a, mp_int *b); 00810 */ 00811 /* 00812 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c); 00813 */ 00814 /* 00815 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); 00816 */ 00817 /* 00818 int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); 00819 */ 00820 /* 00821 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode); 00822 */ 00823 /* 00824 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode); 00825 */ 00826 /* 00827 void bn_reverse(unsigned char *s, int len); 00828 */ 00829 00830 #if defined(BUILD_tcl) || !defined(_WIN32) 00831 MODULE_SCOPE const char *mp_s_rmap; 00832 #endif 00833 00834 #ifdef __cplusplus 00835 } 00836 #endif 00837 00838 #endif 00839 00840 00841 /* $Source: /cvsroot/tcl/tcl/generic/tclTomMath.h,v $ */ 00842 /* Based on Tom's version 1.8 */ 00843 /* $Revision: 1.10 $ */ 00844 /* $Date: 2007/02/14 17:59:21 $ */ 00845
Generated on Wed Mar 12 12:18:23 2008 by 1.5.1 |