[25] | 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, tomstdenis@gmail.com, http://math.libtomcrypt.com |
---|
| 14 | */ |
---|
| 15 | #ifndef BN_H_ |
---|
| 16 | #define BN_H_ |
---|
| 17 | |
---|
| 18 | #include <tclTomMathDecls.h> |
---|
| 19 | #ifndef MODULE_SCOPE |
---|
| 20 | #define MODULE_SCOPE extern |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | #include <stdio.h> |
---|
| 24 | #include <string.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <ctype.h> |
---|
| 27 | #include <limits.h> |
---|
| 28 | |
---|
| 29 | #include <tommath_class.h> |
---|
| 30 | |
---|
| 31 | #ifndef MIN |
---|
| 32 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | #ifndef MAX |
---|
| 36 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | #ifdef __cplusplus |
---|
| 40 | extern "C" { |
---|
| 41 | |
---|
| 42 | /* C++ compilers don't like assigning void * to mp_digit * */ |
---|
| 43 | #define OPT_CAST(x) (x *) |
---|
| 44 | |
---|
| 45 | #else |
---|
| 46 | |
---|
| 47 | /* C on the other hand doesn't care */ |
---|
| 48 | #define OPT_CAST(x) |
---|
| 49 | |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | |
---|
| 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 |
---|
| 59 | |
---|
| 60 | /* some default configurations. |
---|
| 61 | * |
---|
| 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 |
---|
| 64 | * |
---|
| 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] |
---|
| 67 | */ |
---|
| 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 |
---|
| 86 | |
---|
| 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))); |
---|
| 92 | |
---|
| 93 | #define DIGIT_BIT 60 |
---|
| 94 | #else |
---|
| 95 | /* this is the default case, 28-bit digits */ |
---|
| 96 | |
---|
| 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 |
---|
| 107 | |
---|
| 108 | #ifndef MP_DIGIT_DECLARED |
---|
| 109 | typedef unsigned int mp_digit; |
---|
| 110 | #define MP_DIGIT_DECLARED |
---|
| 111 | #endif |
---|
| 112 | typedef ulong64 mp_word; |
---|
| 113 | |
---|
| 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 |
---|
| 123 | |
---|
| 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 |
---|
| 142 | |
---|
| 143 | |
---|
| 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 |
---|
| 148 | |
---|
| 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 |
---|
| 152 | |
---|
| 153 | /* equalities */ |
---|
| 154 | #define MP_LT -1 /* less than */ |
---|
| 155 | #define MP_EQ 0 /* equal to */ |
---|
| 156 | #define MP_GT 1 /* greater than */ |
---|
| 157 | |
---|
| 158 | #define MP_ZPOS 0 /* positive integer */ |
---|
| 159 | #define MP_NEG 1 /* negative */ |
---|
| 160 | |
---|
| 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 |
---|
| 165 | |
---|
| 166 | #define MP_YES 1 /* yes response */ |
---|
| 167 | #define MP_NO 0 /* no response */ |
---|
| 168 | |
---|
| 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 */ |
---|
| 173 | |
---|
| 174 | typedef int mp_err; |
---|
| 175 | |
---|
| 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 |
---|
| 183 | |
---|
| 184 | /* define this to use lower memory usage routines (exptmods mostly) */ |
---|
| 185 | /* #define MP_LOW_MEM */ |
---|
| 186 | |
---|
| 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 |
---|
| 195 | |
---|
| 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)) |
---|
| 198 | |
---|
| 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; |
---|
| 207 | }; |
---|
| 208 | |
---|
| 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); |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | #define USED(m) ((m)->used) |
---|
| 214 | #define DIGIT(m,k) ((m)->dp[(k)]) |
---|
| 215 | #define SIGN(m) ((m)->sign) |
---|
| 216 | |
---|
| 217 | /* error code to char* string */ |
---|
| 218 | /* |
---|
| 219 | char *mp_error_to_string(int code); |
---|
| 220 | */ |
---|
| 221 | |
---|
| 222 | /* ---> init and deinit bignum functions <--- */ |
---|
| 223 | /* init a bignum */ |
---|
| 224 | /* |
---|
| 225 | int mp_init(mp_int *a); |
---|
| 226 | */ |
---|
| 227 | |
---|
| 228 | /* free a bignum */ |
---|
| 229 | /* |
---|
| 230 | void mp_clear(mp_int *a); |
---|
| 231 | */ |
---|
| 232 | |
---|
| 233 | /* init a null terminated series of arguments */ |
---|
| 234 | /* |
---|
| 235 | int mp_init_multi(mp_int *mp, ...); |
---|
| 236 | */ |
---|
| 237 | |
---|
| 238 | /* clear a null terminated series of arguments */ |
---|
| 239 | /* |
---|
| 240 | void mp_clear_multi(mp_int *mp, ...); |
---|
| 241 | */ |
---|
| 242 | |
---|
| 243 | /* exchange two ints */ |
---|
| 244 | /* |
---|
| 245 | void mp_exch(mp_int *a, mp_int *b); |
---|
| 246 | */ |
---|
| 247 | |
---|
| 248 | /* shrink ram required for a bignum */ |
---|
| 249 | /* |
---|
| 250 | int mp_shrink(mp_int *a); |
---|
| 251 | */ |
---|
| 252 | |
---|
| 253 | /* grow an int to a given size */ |
---|
| 254 | /* |
---|
| 255 | int mp_grow(mp_int *a, int size); |
---|
| 256 | */ |
---|
| 257 | |
---|
| 258 | /* init to a given number of digits */ |
---|
| 259 | /* |
---|
| 260 | int mp_init_size(mp_int *a, int size); |
---|
| 261 | */ |
---|
| 262 | |
---|
| 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) |
---|
| 267 | |
---|
| 268 | /* set to zero */ |
---|
| 269 | /* |
---|
| 270 | void mp_zero(mp_int *a); |
---|
| 271 | */ |
---|
| 272 | |
---|
| 273 | /* set to a digit */ |
---|
| 274 | /* |
---|
| 275 | void mp_set(mp_int *a, mp_digit b); |
---|
| 276 | */ |
---|
| 277 | |
---|
| 278 | /* set a 32-bit const */ |
---|
| 279 | /* |
---|
| 280 | int mp_set_int(mp_int *a, unsigned long b); |
---|
| 281 | */ |
---|
| 282 | |
---|
| 283 | /* get a 32-bit value */ |
---|
| 284 | unsigned long mp_get_int(mp_int * a); |
---|
| 285 | |
---|
| 286 | /* initialize and set a digit */ |
---|
| 287 | /* |
---|
| 288 | int mp_init_set (mp_int * a, mp_digit b); |
---|
| 289 | */ |
---|
| 290 | |
---|
| 291 | /* initialize and set 32-bit value */ |
---|
| 292 | /* |
---|
| 293 | int mp_init_set_int (mp_int * a, unsigned long b); |
---|
| 294 | */ |
---|
| 295 | |
---|
| 296 | /* copy, b = a */ |
---|
| 297 | /* |
---|
| 298 | int mp_copy(mp_int *a, mp_int *b); |
---|
| 299 | */ |
---|
| 300 | |
---|
| 301 | /* inits and copies, a = b */ |
---|
| 302 | /* |
---|
| 303 | int mp_init_copy(mp_int *a, mp_int *b); |
---|
| 304 | */ |
---|
| 305 | |
---|
| 306 | /* trim unused digits */ |
---|
| 307 | /* |
---|
| 308 | void mp_clamp(mp_int *a); |
---|
| 309 | */ |
---|
| 310 | |
---|
| 311 | /* ---> digit manipulation <--- */ |
---|
| 312 | |
---|
| 313 | /* right shift by "b" digits */ |
---|
| 314 | /* |
---|
| 315 | void mp_rshd(mp_int *a, int b); |
---|
| 316 | */ |
---|
| 317 | |
---|
| 318 | /* left shift by "b" digits */ |
---|
| 319 | /* |
---|
| 320 | int mp_lshd(mp_int *a, int b); |
---|
| 321 | */ |
---|
| 322 | |
---|
| 323 | /* c = a / 2**b */ |
---|
| 324 | /* |
---|
| 325 | int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d); |
---|
| 326 | */ |
---|
| 327 | |
---|
| 328 | /* b = a/2 */ |
---|
| 329 | /* |
---|
| 330 | int mp_div_2(mp_int *a, mp_int *b); |
---|
| 331 | */ |
---|
| 332 | |
---|
| 333 | /* c = a * 2**b */ |
---|
| 334 | /* |
---|
| 335 | int mp_mul_2d(mp_int *a, int b, mp_int *c); |
---|
| 336 | */ |
---|
| 337 | |
---|
| 338 | /* b = a*2 */ |
---|
| 339 | /* |
---|
| 340 | int mp_mul_2(mp_int *a, mp_int *b); |
---|
| 341 | */ |
---|
| 342 | |
---|
| 343 | /* c = a mod 2**d */ |
---|
| 344 | /* |
---|
| 345 | int mp_mod_2d(mp_int *a, int b, mp_int *c); |
---|
| 346 | */ |
---|
| 347 | |
---|
| 348 | /* computes a = 2**b */ |
---|
| 349 | /* |
---|
| 350 | int mp_2expt(mp_int *a, int b); |
---|
| 351 | */ |
---|
| 352 | |
---|
| 353 | /* Counts the number of lsbs which are zero before the first zero bit */ |
---|
| 354 | /* |
---|
| 355 | int mp_cnt_lsb(mp_int *a); |
---|
| 356 | */ |
---|
| 357 | |
---|
| 358 | /* I Love Earth! */ |
---|
| 359 | |
---|
| 360 | /* makes a pseudo-random int of a given size */ |
---|
| 361 | /* |
---|
| 362 | int mp_rand(mp_int *a, int digits); |
---|
| 363 | */ |
---|
| 364 | |
---|
| 365 | /* ---> binary operations <--- */ |
---|
| 366 | /* c = a XOR b */ |
---|
| 367 | /* |
---|
| 368 | int mp_xor(mp_int *a, mp_int *b, mp_int *c); |
---|
| 369 | */ |
---|
| 370 | |
---|
| 371 | /* c = a OR b */ |
---|
| 372 | /* |
---|
| 373 | int mp_or(mp_int *a, mp_int *b, mp_int *c); |
---|
| 374 | */ |
---|
| 375 | |
---|
| 376 | /* c = a AND b */ |
---|
| 377 | /* |
---|
| 378 | int mp_and(mp_int *a, mp_int *b, mp_int *c); |
---|
| 379 | */ |
---|
| 380 | |
---|
| 381 | /* ---> Basic arithmetic <--- */ |
---|
| 382 | |
---|
| 383 | /* b = -a */ |
---|
| 384 | /* |
---|
| 385 | int mp_neg(mp_int *a, mp_int *b); |
---|
| 386 | */ |
---|
| 387 | |
---|
| 388 | /* b = |a| */ |
---|
| 389 | /* |
---|
| 390 | int mp_abs(mp_int *a, mp_int *b); |
---|
| 391 | */ |
---|
| 392 | |
---|
| 393 | /* compare a to b */ |
---|
| 394 | /* |
---|
| 395 | int mp_cmp(mp_int *a, mp_int *b); |
---|
| 396 | */ |
---|
| 397 | |
---|
| 398 | /* compare |a| to |b| */ |
---|
| 399 | /* |
---|
| 400 | int mp_cmp_mag(mp_int *a, mp_int *b); |
---|
| 401 | */ |
---|
| 402 | |
---|
| 403 | /* c = a + b */ |
---|
| 404 | /* |
---|
| 405 | int mp_add(mp_int *a, mp_int *b, mp_int *c); |
---|
| 406 | */ |
---|
| 407 | |
---|
| 408 | /* c = a - b */ |
---|
| 409 | /* |
---|
| 410 | int mp_sub(mp_int *a, mp_int *b, mp_int *c); |
---|
| 411 | */ |
---|
| 412 | |
---|
| 413 | /* c = a * b */ |
---|
| 414 | /* |
---|
| 415 | int mp_mul(mp_int *a, mp_int *b, mp_int *c); |
---|
| 416 | */ |
---|
| 417 | |
---|
| 418 | /* b = a*a */ |
---|
| 419 | /* |
---|
| 420 | int mp_sqr(mp_int *a, mp_int *b); |
---|
| 421 | */ |
---|
| 422 | |
---|
| 423 | /* a/b => cb + d == a */ |
---|
| 424 | /* |
---|
| 425 | int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d); |
---|
| 426 | */ |
---|
| 427 | |
---|
| 428 | /* c = a mod b, 0 <= c < b */ |
---|
| 429 | /* |
---|
| 430 | int mp_mod(mp_int *a, mp_int *b, mp_int *c); |
---|
| 431 | */ |
---|
| 432 | |
---|
| 433 | /* ---> single digit functions <--- */ |
---|
| 434 | |
---|
| 435 | /* compare against a single digit */ |
---|
| 436 | /* |
---|
| 437 | int mp_cmp_d(mp_int *a, mp_digit b); |
---|
| 438 | */ |
---|
| 439 | |
---|
| 440 | /* c = a + b */ |
---|
| 441 | /* |
---|
| 442 | int mp_add_d(mp_int *a, mp_digit b, mp_int *c); |
---|
| 443 | */ |
---|
| 444 | |
---|
| 445 | /* c = a - b */ |
---|
| 446 | /* |
---|
| 447 | int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); |
---|
| 448 | */ |
---|
| 449 | |
---|
| 450 | /* c = a * b */ |
---|
| 451 | /* |
---|
| 452 | int mp_mul_d(mp_int *a, mp_digit b, mp_int *c); |
---|
| 453 | */ |
---|
| 454 | |
---|
| 455 | /* a/b => cb + d == a */ |
---|
| 456 | /* |
---|
| 457 | int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d); |
---|
| 458 | */ |
---|
| 459 | |
---|
| 460 | /* a/3 => 3c + d == a */ |
---|
| 461 | /* |
---|
| 462 | int mp_div_3(mp_int *a, mp_int *c, mp_digit *d); |
---|
| 463 | */ |
---|
| 464 | |
---|
| 465 | /* c = a**b */ |
---|
| 466 | /* |
---|
| 467 | int mp_expt_d(mp_int *a, mp_digit b, mp_int *c); |
---|
| 468 | */ |
---|
| 469 | |
---|
| 470 | /* c = a mod b, 0 <= c < b */ |
---|
| 471 | /* |
---|
| 472 | int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c); |
---|
| 473 | */ |
---|
| 474 | |
---|
| 475 | /* ---> number theory <--- */ |
---|
| 476 | |
---|
| 477 | /* d = a + b (mod c) */ |
---|
| 478 | /* |
---|
| 479 | int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); |
---|
| 480 | */ |
---|
| 481 | |
---|
| 482 | /* d = a - b (mod c) */ |
---|
| 483 | /* |
---|
| 484 | int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); |
---|
| 485 | */ |
---|
| 486 | |
---|
| 487 | /* d = a * b (mod c) */ |
---|
| 488 | /* |
---|
| 489 | int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); |
---|
| 490 | */ |
---|
| 491 | |
---|
| 492 | /* c = a * a (mod b) */ |
---|
| 493 | /* |
---|
| 494 | int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c); |
---|
| 495 | */ |
---|
| 496 | |
---|
| 497 | /* c = 1/a (mod b) */ |
---|
| 498 | /* |
---|
| 499 | int mp_invmod(mp_int *a, mp_int *b, mp_int *c); |
---|
| 500 | */ |
---|
| 501 | |
---|
| 502 | /* c = (a, b) */ |
---|
| 503 | /* |
---|
| 504 | int mp_gcd(mp_int *a, mp_int *b, mp_int *c); |
---|
| 505 | */ |
---|
| 506 | |
---|
| 507 | /* produces value such that U1*a + U2*b = U3 */ |
---|
| 508 | /* |
---|
| 509 | int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); |
---|
| 510 | */ |
---|
| 511 | |
---|
| 512 | /* c = [a, b] or (a*b)/(a, b) */ |
---|
| 513 | /* |
---|
| 514 | int mp_lcm(mp_int *a, mp_int *b, mp_int *c); |
---|
| 515 | */ |
---|
| 516 | |
---|
| 517 | /* finds one of the b'th root of a, such that |c|**b <= |a| |
---|
| 518 | * |
---|
| 519 | * returns error if a < 0 and b is even |
---|
| 520 | */ |
---|
| 521 | /* |
---|
| 522 | int mp_n_root(mp_int *a, mp_digit b, mp_int *c); |
---|
| 523 | */ |
---|
| 524 | |
---|
| 525 | /* special sqrt algo */ |
---|
| 526 | /* |
---|
| 527 | int mp_sqrt(mp_int *arg, mp_int *ret); |
---|
| 528 | */ |
---|
| 529 | |
---|
| 530 | /* is number a square? */ |
---|
| 531 | /* |
---|
| 532 | int mp_is_square(mp_int *arg, int *ret); |
---|
| 533 | */ |
---|
| 534 | |
---|
| 535 | /* computes the jacobi c = (a | n) (or Legendre if b is prime) */ |
---|
| 536 | /* |
---|
| 537 | int mp_jacobi(mp_int *a, mp_int *n, int *c); |
---|
| 538 | */ |
---|
| 539 | |
---|
| 540 | /* used to setup the Barrett reduction for a given modulus b */ |
---|
| 541 | /* |
---|
| 542 | int mp_reduce_setup(mp_int *a, mp_int *b); |
---|
| 543 | */ |
---|
| 544 | |
---|
| 545 | /* Barrett Reduction, computes a (mod b) with a precomputed value c |
---|
| 546 | * |
---|
| 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]. |
---|
| 549 | */ |
---|
| 550 | /* |
---|
| 551 | int mp_reduce(mp_int *a, mp_int *b, mp_int *c); |
---|
| 552 | */ |
---|
| 553 | |
---|
| 554 | /* setups the montgomery reduction */ |
---|
| 555 | /* |
---|
| 556 | int mp_montgomery_setup(mp_int *a, mp_digit *mp); |
---|
| 557 | */ |
---|
| 558 | |
---|
| 559 | /* computes a = B**n mod b without division or multiplication useful for |
---|
| 560 | * normalizing numbers in a Montgomery system. |
---|
| 561 | */ |
---|
| 562 | /* |
---|
| 563 | int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); |
---|
| 564 | */ |
---|
| 565 | |
---|
| 566 | /* computes x/R == x (mod N) via Montgomery Reduction */ |
---|
| 567 | /* |
---|
| 568 | int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); |
---|
| 569 | */ |
---|
| 570 | |
---|
| 571 | /* returns 1 if a is a valid DR modulus */ |
---|
| 572 | /* |
---|
| 573 | int mp_dr_is_modulus(mp_int *a); |
---|
| 574 | */ |
---|
| 575 | |
---|
| 576 | /* sets the value of "d" required for mp_dr_reduce */ |
---|
| 577 | /* |
---|
| 578 | void mp_dr_setup(mp_int *a, mp_digit *d); |
---|
| 579 | */ |
---|
| 580 | |
---|
| 581 | /* reduces a modulo b using the Diminished Radix method */ |
---|
| 582 | /* |
---|
| 583 | int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp); |
---|
| 584 | */ |
---|
| 585 | |
---|
| 586 | /* returns true if a can be reduced with mp_reduce_2k */ |
---|
| 587 | /* |
---|
| 588 | int mp_reduce_is_2k(mp_int *a); |
---|
| 589 | */ |
---|
| 590 | |
---|
| 591 | /* determines k value for 2k reduction */ |
---|
| 592 | /* |
---|
| 593 | int mp_reduce_2k_setup(mp_int *a, mp_digit *d); |
---|
| 594 | */ |
---|
| 595 | |
---|
| 596 | /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ |
---|
| 597 | /* |
---|
| 598 | int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); |
---|
| 599 | */ |
---|
| 600 | |
---|
| 601 | /* returns true if a can be reduced with mp_reduce_2k_l */ |
---|
| 602 | /* |
---|
| 603 | int mp_reduce_is_2k_l(mp_int *a); |
---|
| 604 | */ |
---|
| 605 | |
---|
| 606 | /* determines k value for 2k reduction */ |
---|
| 607 | /* |
---|
| 608 | int mp_reduce_2k_setup_l(mp_int *a, mp_int *d); |
---|
| 609 | */ |
---|
| 610 | |
---|
| 611 | /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ |
---|
| 612 | /* |
---|
| 613 | int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d); |
---|
| 614 | */ |
---|
| 615 | |
---|
| 616 | /* d = a**b (mod c) */ |
---|
| 617 | /* |
---|
| 618 | int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); |
---|
| 619 | */ |
---|
| 620 | |
---|
| 621 | /* ---> Primes <--- */ |
---|
| 622 | |
---|
| 623 | /* number of primes */ |
---|
| 624 | #ifdef MP_8BIT |
---|
| 625 | #define PRIME_SIZE 31 |
---|
| 626 | #else |
---|
| 627 | #define PRIME_SIZE 256 |
---|
| 628 | #endif |
---|
| 629 | |
---|
| 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 |
---|
| 634 | |
---|
| 635 | /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ |
---|
| 636 | /* |
---|
| 637 | int mp_prime_is_divisible(mp_int *a, int *result); |
---|
| 638 | */ |
---|
| 639 | |
---|
| 640 | /* performs one Fermat test of "a" using base "b". |
---|
| 641 | * Sets result to 0 if composite or 1 if probable prime |
---|
| 642 | */ |
---|
| 643 | /* |
---|
| 644 | int mp_prime_fermat(mp_int *a, mp_int *b, int *result); |
---|
| 645 | */ |
---|
| 646 | |
---|
| 647 | /* performs one Miller-Rabin test of "a" using base "b". |
---|
| 648 | * Sets result to 0 if composite or 1 if probable prime |
---|
| 649 | */ |
---|
| 650 | /* |
---|
| 651 | int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result); |
---|
| 652 | */ |
---|
| 653 | |
---|
| 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 |
---|
| 656 | */ |
---|
| 657 | /* |
---|
| 658 | int mp_prime_rabin_miller_trials(int size); |
---|
| 659 | */ |
---|
| 660 | |
---|
| 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. |
---|
| 665 | * |
---|
| 666 | * Sets result to 1 if probably prime, 0 otherwise |
---|
| 667 | */ |
---|
| 668 | /* |
---|
| 669 | int mp_prime_is_prime(mp_int *a, int t, int *result); |
---|
| 670 | */ |
---|
| 671 | |
---|
| 672 | /* finds the next prime after the number "a" using "t" trials |
---|
| 673 | * of Miller-Rabin. |
---|
| 674 | * |
---|
| 675 | * bbs_style = 1 means the prime must be congruent to 3 mod 4 |
---|
| 676 | */ |
---|
| 677 | /* |
---|
| 678 | int mp_prime_next_prime(mp_int *a, int t, int bbs_style); |
---|
| 679 | */ |
---|
| 680 | |
---|
| 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 |
---|
| 683 | * |
---|
| 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 |
---|
| 687 | * |
---|
| 688 | * The prime generated will be larger than 2^(8*size). |
---|
| 689 | */ |
---|
| 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) |
---|
| 691 | |
---|
| 692 | /* makes a truly random prime of a given size (bits), |
---|
| 693 | * |
---|
| 694 | * Flags are as follows: |
---|
| 695 | * |
---|
| 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 |
---|
| 700 | * |
---|
| 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 |
---|
| 704 | * |
---|
| 705 | */ |
---|
| 706 | /* |
---|
| 707 | int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); |
---|
| 708 | */ |
---|
| 709 | |
---|
| 710 | /* ---> radix conversion <--- */ |
---|
| 711 | /* |
---|
| 712 | int mp_count_bits(mp_int *a); |
---|
| 713 | */ |
---|
| 714 | |
---|
| 715 | /* |
---|
| 716 | int mp_unsigned_bin_size(mp_int *a); |
---|
| 717 | */ |
---|
| 718 | /* |
---|
| 719 | int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c); |
---|
| 720 | */ |
---|
| 721 | /* |
---|
| 722 | int mp_to_unsigned_bin(mp_int *a, unsigned char *b); |
---|
| 723 | */ |
---|
| 724 | /* |
---|
| 725 | int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); |
---|
| 726 | */ |
---|
| 727 | |
---|
| 728 | /* |
---|
| 729 | int mp_signed_bin_size(mp_int *a); |
---|
| 730 | */ |
---|
| 731 | /* |
---|
| 732 | int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c); |
---|
| 733 | */ |
---|
| 734 | /* |
---|
| 735 | int mp_to_signed_bin(mp_int *a, unsigned char *b); |
---|
| 736 | */ |
---|
| 737 | /* |
---|
| 738 | int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); |
---|
| 739 | */ |
---|
| 740 | |
---|
| 741 | /* |
---|
| 742 | int mp_read_radix(mp_int *a, const char *str, int radix); |
---|
| 743 | */ |
---|
| 744 | /* |
---|
| 745 | int mp_toradix(mp_int *a, char *str, int radix); |
---|
| 746 | */ |
---|
| 747 | /* |
---|
| 748 | int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); |
---|
| 749 | */ |
---|
| 750 | /* |
---|
| 751 | int mp_radix_size(mp_int *a, int radix, int *size); |
---|
| 752 | */ |
---|
| 753 | |
---|
| 754 | /* |
---|
| 755 | int mp_fread(mp_int *a, int radix, FILE *stream); |
---|
| 756 | */ |
---|
| 757 | /* |
---|
| 758 | int mp_fwrite(mp_int *a, int radix, FILE *stream); |
---|
| 759 | */ |
---|
| 760 | |
---|
| 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)) |
---|
| 767 | |
---|
| 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) |
---|
| 772 | |
---|
| 773 | /* lowlevel functions, do not call! */ |
---|
| 774 | /* |
---|
| 775 | int s_mp_add(mp_int *a, mp_int *b, mp_int *c); |
---|
| 776 | */ |
---|
| 777 | /* |
---|
| 778 | int s_mp_sub(mp_int *a, mp_int *b, mp_int *c); |
---|
| 779 | */ |
---|
| 780 | #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) |
---|
| 781 | /* |
---|
| 782 | int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
---|
| 783 | */ |
---|
| 784 | /* |
---|
| 785 | int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
---|
| 786 | */ |
---|
| 787 | /* |
---|
| 788 | int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
---|
| 789 | */ |
---|
| 790 | /* |
---|
| 791 | int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
---|
| 792 | */ |
---|
| 793 | /* |
---|
| 794 | int fast_s_mp_sqr(mp_int *a, mp_int *b); |
---|
| 795 | */ |
---|
| 796 | /* |
---|
| 797 | int s_mp_sqr(mp_int *a, mp_int *b); |
---|
| 798 | */ |
---|
| 799 | /* |
---|
| 800 | int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c); |
---|
| 801 | */ |
---|
| 802 | /* |
---|
| 803 | int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c); |
---|
| 804 | */ |
---|
| 805 | /* |
---|
| 806 | int mp_karatsuba_sqr(mp_int *a, mp_int *b); |
---|
| 807 | */ |
---|
| 808 | /* |
---|
| 809 | int mp_toom_sqr(mp_int *a, mp_int *b); |
---|
| 810 | */ |
---|
| 811 | /* |
---|
| 812 | int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c); |
---|
| 813 | */ |
---|
| 814 | /* |
---|
| 815 | int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); |
---|
| 816 | */ |
---|
| 817 | /* |
---|
| 818 | int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); |
---|
| 819 | */ |
---|
| 820 | /* |
---|
| 821 | int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode); |
---|
| 822 | */ |
---|
| 823 | /* |
---|
| 824 | int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode); |
---|
| 825 | */ |
---|
| 826 | /* |
---|
| 827 | void bn_reverse(unsigned char *s, int len); |
---|
| 828 | */ |
---|
| 829 | |
---|
| 830 | #if defined(BUILD_tcl) || !defined(_WIN32) |
---|
| 831 | MODULE_SCOPE const char *mp_s_rmap; |
---|
| 832 | #endif |
---|
| 833 | |
---|
| 834 | #ifdef __cplusplus |
---|
| 835 | } |
---|
| 836 | #endif |
---|
| 837 | |
---|
| 838 | #endif |
---|
| 839 | |
---|
| 840 | |
---|
| 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 $ */ |
---|
| 845 | |
---|