annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/tclTomMath.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
jpayne@69 2 /* SPDX-License-Identifier: Unlicense */
jpayne@69 3
jpayne@69 4 #ifndef BN_H_
jpayne@69 5 #define BN_H_
jpayne@69 6
jpayne@69 7 #ifndef MODULE_SCOPE
jpayne@69 8 #define MODULE_SCOPE extern
jpayne@69 9 #endif
jpayne@69 10
jpayne@69 11
jpayne@69 12
jpayne@69 13 #ifdef __cplusplus
jpayne@69 14 extern "C" {
jpayne@69 15 #endif
jpayne@69 16
jpayne@69 17 /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */
jpayne@69 18 #if (defined(_WIN32) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)) && !defined(MP_64BIT)
jpayne@69 19 # define MP_32BIT
jpayne@69 20 #endif
jpayne@69 21
jpayne@69 22 /* detect 64-bit mode if possible */
jpayne@69 23 #if defined(NEVER)
jpayne@69 24 # if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
jpayne@69 25 # if defined(__GNUC__)
jpayne@69 26 /* we support 128bit integers only via: __attribute__((mode(TI))) */
jpayne@69 27 # define MP_64BIT
jpayne@69 28 # else
jpayne@69 29 /* otherwise we fall back to MP_32BIT even on 64bit platforms */
jpayne@69 30 # define MP_32BIT
jpayne@69 31 # endif
jpayne@69 32 # endif
jpayne@69 33 #endif
jpayne@69 34
jpayne@69 35 #ifdef MP_DIGIT_BIT
jpayne@69 36 # error Defining MP_DIGIT_BIT is disallowed, use MP_8/16/31/32/64BIT
jpayne@69 37 #endif
jpayne@69 38
jpayne@69 39 /* some default configurations.
jpayne@69 40 *
jpayne@69 41 * A "mp_digit" must be able to hold MP_DIGIT_BIT + 1 bits
jpayne@69 42 * A "mp_word" must be able to hold 2*MP_DIGIT_BIT + 1 bits
jpayne@69 43 *
jpayne@69 44 * At the very least a mp_digit must be able to hold 7 bits
jpayne@69 45 * [any size beyond that is ok provided it doesn't overflow the data type]
jpayne@69 46 */
jpayne@69 47
jpayne@69 48 #ifdef MP_8BIT
jpayne@69 49 #ifndef MP_DIGIT_DECLARED
jpayne@69 50 typedef unsigned char mp_digit;
jpayne@69 51 #define MP_DIGIT_DECLARED
jpayne@69 52 #endif
jpayne@69 53 #ifndef MP_WORD_DECLARED
jpayne@69 54 typedef unsigned short private_mp_word;
jpayne@69 55 #define MP_WORD_DECLARED
jpayne@69 56 #endif
jpayne@69 57 # define MP_SIZEOF_MP_DIGIT 1
jpayne@69 58 # ifdef MP_DIGIT_BIT
jpayne@69 59 # error You must not define MP_DIGIT_BIT when using MP_8BIT
jpayne@69 60 # endif
jpayne@69 61 #elif defined(MP_16BIT)
jpayne@69 62 #ifndef MP_DIGIT_DECLARED
jpayne@69 63 typedef unsigned short mp_digit;
jpayne@69 64 #define MP_DIGIT_DECLARED
jpayne@69 65 #endif
jpayne@69 66 #ifndef MP_WORD_DECLARED
jpayne@69 67 typedef unsigned int private_mp_word;
jpayne@69 68 #define MP_WORD_DECLARED
jpayne@69 69 #endif
jpayne@69 70 # define MP_SIZEOF_MP_DIGIT 2
jpayne@69 71 # ifdef MP_DIGIT_BIT
jpayne@69 72 # error You must not define MP_DIGIT_BIT when using MP_16BIT
jpayne@69 73 # endif
jpayne@69 74 #elif defined(MP_64BIT)
jpayne@69 75 /* for GCC only on supported platforms */
jpayne@69 76 #ifndef MP_DIGIT_DECLARED
jpayne@69 77 typedef unsigned long long mp_digit;
jpayne@69 78 #define MP_DIGIT_DECLARED
jpayne@69 79 #endif
jpayne@69 80 typedef unsigned long private_mp_word __attribute__((mode(TI)));
jpayne@69 81 # define MP_DIGIT_BIT 60
jpayne@69 82 #else
jpayne@69 83 /* this is the default case, 28-bit digits */
jpayne@69 84
jpayne@69 85 /* this is to make porting into LibTomCrypt easier :-) */
jpayne@69 86 #ifndef MP_DIGIT_DECLARED
jpayne@69 87 typedef unsigned int mp_digit;
jpayne@69 88 #define MP_DIGIT_DECLARED
jpayne@69 89 #endif
jpayne@69 90 #ifndef MP_WORD_DECLARED
jpayne@69 91 #ifdef _WIN32
jpayne@69 92 typedef unsigned __int64 private_mp_word;
jpayne@69 93 #else
jpayne@69 94 typedef unsigned long long private_mp_word;
jpayne@69 95 #endif
jpayne@69 96 #define MP_WORD_DECLARED
jpayne@69 97 #endif
jpayne@69 98
jpayne@69 99 # ifdef MP_31BIT
jpayne@69 100 /*
jpayne@69 101 * This is an extension that uses 31-bit digits.
jpayne@69 102 * Please be aware that not all functions support this size, especially s_mp_mul_digs_fast
jpayne@69 103 * will be reduced to work on small numbers only:
jpayne@69 104 * Up to 8 limbs, 248 bits instead of up to 512 limbs, 15872 bits with MP_28BIT.
jpayne@69 105 */
jpayne@69 106 # define MP_DIGIT_BIT 31
jpayne@69 107 # else
jpayne@69 108 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
jpayne@69 109 # define MP_DIGIT_BIT 28
jpayne@69 110 # define MP_28BIT
jpayne@69 111 # endif
jpayne@69 112 #endif
jpayne@69 113
jpayne@69 114 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
jpayne@69 115 #ifndef MP_DIGIT_BIT
jpayne@69 116 # define MP_DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */
jpayne@69 117 #endif
jpayne@69 118
jpayne@69 119 #define MP_MASK ((((mp_digit)1)<<((mp_digit)MP_DIGIT_BIT))-((mp_digit)1))
jpayne@69 120 #define MP_DIGIT_MAX MP_MASK
jpayne@69 121
jpayne@69 122 /* Primality generation flags */
jpayne@69 123 #define MP_PRIME_BBS 0x0001 /* BBS style prime */
jpayne@69 124 #define MP_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
jpayne@69 125 #define MP_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
jpayne@69 126
jpayne@69 127 #define LTM_PRIME_BBS (MP_DEPRECATED_PRAGMA("LTM_PRIME_BBS has been deprecated, use MP_PRIME_BBS") MP_PRIME_BBS)
jpayne@69 128 #define LTM_PRIME_SAFE (MP_DEPRECATED_PRAGMA("LTM_PRIME_SAFE has been deprecated, use MP_PRIME_SAFE") MP_PRIME_SAFE)
jpayne@69 129 #define LTM_PRIME_2MSB_ON (MP_DEPRECATED_PRAGMA("LTM_PRIME_2MSB_ON has been deprecated, use MP_PRIME_2MSB_ON") MP_PRIME_2MSB_ON)
jpayne@69 130
jpayne@69 131 #ifdef MP_USE_ENUMS
jpayne@69 132 typedef enum {
jpayne@69 133 MP_ZPOS = 0, /* positive */
jpayne@69 134 MP_NEG = 1 /* negative */
jpayne@69 135 } mp_sign;
jpayne@69 136 typedef enum {
jpayne@69 137 MP_LT = -1, /* less than */
jpayne@69 138 MP_EQ = 0, /* equal */
jpayne@69 139 MP_GT = 1 /* greater than */
jpayne@69 140 } mp_ord;
jpayne@69 141 typedef enum {
jpayne@69 142 MP_NO = 0,
jpayne@69 143 MP_YES = 1
jpayne@69 144 } mp_bool;
jpayne@69 145 typedef enum {
jpayne@69 146 MP_OKAY = 0, /* no error */
jpayne@69 147 MP_ERR = -1, /* unknown error */
jpayne@69 148 MP_MEM = -2, /* out of mem */
jpayne@69 149 MP_VAL = -3, /* invalid input */
jpayne@69 150 MP_ITER = -4, /* maximum iterations reached */
jpayne@69 151 MP_BUF = -5 /* buffer overflow, supplied buffer too small */
jpayne@69 152 } mp_err;
jpayne@69 153 typedef enum {
jpayne@69 154 MP_LSB_FIRST = -1,
jpayne@69 155 MP_MSB_FIRST = 1
jpayne@69 156 } mp_order;
jpayne@69 157 typedef enum {
jpayne@69 158 MP_LITTLE_ENDIAN = -1,
jpayne@69 159 MP_NATIVE_ENDIAN = 0,
jpayne@69 160 MP_BIG_ENDIAN = 1
jpayne@69 161 } mp_endian;
jpayne@69 162 #else
jpayne@69 163 typedef int mp_sign;
jpayne@69 164 #define MP_ZPOS 0 /* positive integer */
jpayne@69 165 #define MP_NEG 1 /* negative */
jpayne@69 166 typedef int mp_ord;
jpayne@69 167 #define MP_LT -1 /* less than */
jpayne@69 168 #define MP_EQ 0 /* equal to */
jpayne@69 169 #define MP_GT 1 /* greater than */
jpayne@69 170 typedef int mp_bool;
jpayne@69 171 #define MP_YES 1
jpayne@69 172 #define MP_NO 0
jpayne@69 173 typedef int mp_err;
jpayne@69 174 #define MP_OKAY 0 /* no error */
jpayne@69 175 #define MP_ERR -1 /* unknown error */
jpayne@69 176 #define MP_MEM -2 /* out of mem */
jpayne@69 177 #define MP_VAL -3 /* invalid input */
jpayne@69 178 #define MP_RANGE (MP_DEPRECATED_PRAGMA("MP_RANGE has been deprecated in favor of MP_VAL") MP_VAL)
jpayne@69 179 #define MP_ITER -4 /* maximum iterations reached */
jpayne@69 180 #define MP_BUF -5 /* buffer overflow, supplied buffer too small */
jpayne@69 181 typedef int mp_order;
jpayne@69 182 #define MP_LSB_FIRST -1
jpayne@69 183 #define MP_MSB_FIRST 1
jpayne@69 184 typedef int mp_endian;
jpayne@69 185 #define MP_LITTLE_ENDIAN -1
jpayne@69 186 #define MP_NATIVE_ENDIAN 0
jpayne@69 187 #define MP_BIG_ENDIAN 1
jpayne@69 188 #endif
jpayne@69 189
jpayne@69 190 /* tunable cutoffs */
jpayne@69 191
jpayne@69 192 #ifndef MP_FIXED_CUTOFFS
jpayne@69 193 extern int
jpayne@69 194 KARATSUBA_MUL_CUTOFF,
jpayne@69 195 KARATSUBA_SQR_CUTOFF,
jpayne@69 196 TOOM_MUL_CUTOFF,
jpayne@69 197 TOOM_SQR_CUTOFF;
jpayne@69 198 #endif
jpayne@69 199
jpayne@69 200 /* define this to use lower memory usage routines (exptmods mostly) */
jpayne@69 201 /* #define MP_LOW_MEM */
jpayne@69 202
jpayne@69 203 /* default precision */
jpayne@69 204 #ifndef MP_PREC
jpayne@69 205 # ifndef MP_LOW_MEM
jpayne@69 206 # define MP_PREC 32 /* default digits of precision */
jpayne@69 207 # elif defined(MP_8BIT)
jpayne@69 208 # define MP_PREC 16 /* default digits of precision */
jpayne@69 209 # else
jpayne@69 210 # define MP_PREC 8 /* default digits of precision */
jpayne@69 211 # endif
jpayne@69 212 #endif
jpayne@69 213
jpayne@69 214 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
jpayne@69 215 #define PRIVATE_MP_WARRAY (int)(1 << (((CHAR_BIT * sizeof(private_mp_word)) - (2 * MP_DIGIT_BIT)) + 1))
jpayne@69 216
jpayne@69 217 #if defined(__GNUC__) && __GNUC__ >= 4
jpayne@69 218 # define MP_NULL_TERMINATED __attribute__((sentinel))
jpayne@69 219 #else
jpayne@69 220 # define MP_NULL_TERMINATED
jpayne@69 221 #endif
jpayne@69 222
jpayne@69 223 /*
jpayne@69 224 * MP_WUR - warn unused result
jpayne@69 225 * ---------------------------
jpayne@69 226 *
jpayne@69 227 * The result of functions annotated with MP_WUR must be
jpayne@69 228 * checked and cannot be ignored.
jpayne@69 229 *
jpayne@69 230 * Most functions in libtommath return an error code.
jpayne@69 231 * This error code must be checked in order to prevent crashes or invalid
jpayne@69 232 * results.
jpayne@69 233 *
jpayne@69 234 * If you still want to avoid the error checks for quick and dirty programs
jpayne@69 235 * without robustness guarantees, you can `#define MP_WUR` before including
jpayne@69 236 * tommath.h, disabling the warnings.
jpayne@69 237 */
jpayne@69 238 #ifndef MP_WUR
jpayne@69 239 # if defined(__GNUC__) && __GNUC__ >= 4
jpayne@69 240 # define MP_WUR __attribute__((warn_unused_result))
jpayne@69 241 # else
jpayne@69 242 # define MP_WUR
jpayne@69 243 # endif
jpayne@69 244 #endif
jpayne@69 245
jpayne@69 246 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405)
jpayne@69 247 # define MP_DEPRECATED(x) __attribute__((deprecated("replaced by " #x)))
jpayne@69 248 #elif defined(_MSC_VER) && _MSC_VER >= 1500
jpayne@69 249 # define MP_DEPRECATED(x) __declspec(deprecated("replaced by " #x))
jpayne@69 250 #else
jpayne@69 251 # define MP_DEPRECATED(x)
jpayne@69 252 #endif
jpayne@69 253
jpayne@69 254 #ifndef MP_NO_DEPRECATED_PRAGMA
jpayne@69 255 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301)
jpayne@69 256 # define PRIVATE_MP_DEPRECATED_PRAGMA(s) _Pragma(#s)
jpayne@69 257 # define MP_DEPRECATED_PRAGMA(s) PRIVATE_MP_DEPRECATED_PRAGMA(GCC warning s)
jpayne@69 258 #elif defined(_MSC_VER) && _MSC_VER >= 1500
jpayne@69 259 # define MP_DEPRECATED_PRAGMA(s) __pragma(message(s))
jpayne@69 260 #endif
jpayne@69 261 #endif
jpayne@69 262
jpayne@69 263 #ifndef MP_DEPRECATED_PRAGMA
jpayne@69 264 # define MP_DEPRECATED_PRAGMA(s)
jpayne@69 265 #endif
jpayne@69 266
jpayne@69 267 #define DIGIT_BIT MP_DIGIT_BIT
jpayne@69 268 #define USED(m) ((m)->used)
jpayne@69 269 #define DIGIT(m,k) ((m)->dp[(k)])
jpayne@69 270 #define SIGN(m) ((m)->sign)
jpayne@69 271
jpayne@69 272 /* the infamous mp_int structure */
jpayne@69 273 #ifndef MP_INT_DECLARED
jpayne@69 274 #define MP_INT_DECLARED
jpayne@69 275 typedef struct mp_int mp_int;
jpayne@69 276 #endif
jpayne@69 277 struct mp_int {
jpayne@69 278 int used, alloc;
jpayne@69 279 mp_sign sign;
jpayne@69 280 mp_digit *dp;
jpayne@69 281 };
jpayne@69 282
jpayne@69 283 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
jpayne@69 284 typedef int private_mp_prime_callback(unsigned char *dst, int len, void *dat);
jpayne@69 285 typedef private_mp_prime_callback MP_DEPRECATED(mp_rand_source) ltm_prime_callback;
jpayne@69 286
jpayne@69 287 /* error code to char* string */
jpayne@69 288 /*
jpayne@69 289 const char *mp_error_to_string(mp_err code) MP_WUR;
jpayne@69 290 */
jpayne@69 291
jpayne@69 292 /* ---> init and deinit bignum functions <--- */
jpayne@69 293 /* init a bignum */
jpayne@69 294 /*
jpayne@69 295 mp_err mp_init(mp_int *a) MP_WUR;
jpayne@69 296 */
jpayne@69 297
jpayne@69 298 /* free a bignum */
jpayne@69 299 /*
jpayne@69 300 void mp_clear(mp_int *a);
jpayne@69 301 */
jpayne@69 302
jpayne@69 303 /* init a null terminated series of arguments */
jpayne@69 304 /*
jpayne@69 305 mp_err mp_init_multi(mp_int *mp, ...) MP_NULL_TERMINATED MP_WUR;
jpayne@69 306 */
jpayne@69 307
jpayne@69 308 /* clear a null terminated series of arguments */
jpayne@69 309 /*
jpayne@69 310 void mp_clear_multi(mp_int *mp, ...) MP_NULL_TERMINATED;
jpayne@69 311 */
jpayne@69 312
jpayne@69 313 /* exchange two ints */
jpayne@69 314 /*
jpayne@69 315 void mp_exch(mp_int *a, mp_int *b);
jpayne@69 316 */
jpayne@69 317
jpayne@69 318 /* shrink ram required for a bignum */
jpayne@69 319 /*
jpayne@69 320 mp_err mp_shrink(mp_int *a) MP_WUR;
jpayne@69 321 */
jpayne@69 322
jpayne@69 323 /* grow an int to a given size */
jpayne@69 324 /*
jpayne@69 325 mp_err mp_grow(mp_int *a, int size) MP_WUR;
jpayne@69 326 */
jpayne@69 327
jpayne@69 328 /* init to a given number of digits */
jpayne@69 329 /*
jpayne@69 330 mp_err mp_init_size(mp_int *a, int size) MP_WUR;
jpayne@69 331 */
jpayne@69 332
jpayne@69 333 /* ---> Basic Manipulations <--- */
jpayne@69 334 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
jpayne@69 335 #define mp_isodd(a) (((a)->used != 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
jpayne@69 336 #define mp_iseven(a) (((a)->used == 0 || (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
jpayne@69 337 #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
jpayne@69 338
jpayne@69 339 /* set to zero */
jpayne@69 340 /*
jpayne@69 341 void mp_zero(mp_int *a);
jpayne@69 342 */
jpayne@69 343
jpayne@69 344 /* get and set doubles */
jpayne@69 345 /*
jpayne@69 346 double mp_get_double(const mp_int *a) MP_WUR;
jpayne@69 347 */
jpayne@69 348 /*
jpayne@69 349 mp_err mp_set_double(mp_int *a, double b) MP_WUR;
jpayne@69 350 */
jpayne@69 351
jpayne@69 352 /* get integer, set integer and init with integer (int32_t) */
jpayne@69 353 #ifndef MP_NO_STDINT
jpayne@69 354 /*
jpayne@69 355 int32_t mp_get_i32(const mp_int *a) MP_WUR;
jpayne@69 356 */
jpayne@69 357 /*
jpayne@69 358 void mp_set_i32(mp_int *a, int32_t b);
jpayne@69 359 */
jpayne@69 360 /*
jpayne@69 361 mp_err mp_init_i32(mp_int *a, int32_t b) MP_WUR;
jpayne@69 362 */
jpayne@69 363
jpayne@69 364 /* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint32_t) */
jpayne@69 365 #define mp_get_u32(a) ((uint32_t)mp_get_i32(a))
jpayne@69 366 /*
jpayne@69 367 void mp_set_u32(mp_int *a, uint32_t b);
jpayne@69 368 */
jpayne@69 369 /*
jpayne@69 370 mp_err mp_init_u32(mp_int *a, uint32_t b) MP_WUR;
jpayne@69 371 */
jpayne@69 372
jpayne@69 373 /* get integer, set integer and init with integer (int64_t) */
jpayne@69 374 /*
jpayne@69 375 int64_t mp_get_i64(const mp_int *a) MP_WUR;
jpayne@69 376 */
jpayne@69 377 /*
jpayne@69 378 void mp_set_i64(mp_int *a, int64_t b);
jpayne@69 379 */
jpayne@69 380 /*
jpayne@69 381 mp_err mp_init_i64(mp_int *a, int64_t b) MP_WUR;
jpayne@69 382 */
jpayne@69 383
jpayne@69 384 /* get integer, set integer and init with integer, behaves like two complement for negative numbers (uint64_t) */
jpayne@69 385 #define mp_get_u64(a) ((uint64_t)mp_get_i64(a))
jpayne@69 386 /*
jpayne@69 387 void mp_set_u64(mp_int *a, uint64_t b);
jpayne@69 388 */
jpayne@69 389 /*
jpayne@69 390 mp_err mp_init_u64(mp_int *a, uint64_t b) MP_WUR;
jpayne@69 391 */
jpayne@69 392
jpayne@69 393 /* get magnitude */
jpayne@69 394 /*
jpayne@69 395 uint32_t mp_get_mag_u32(const mp_int *a) MP_WUR;
jpayne@69 396 */
jpayne@69 397 /*
jpayne@69 398 uint64_t mp_get_mag_u64(const mp_int *a) MP_WUR;
jpayne@69 399 */
jpayne@69 400 #endif
jpayne@69 401 /*
jpayne@69 402 unsigned long mp_get_mag_ul(const mp_int *a) MP_WUR;
jpayne@69 403 */
jpayne@69 404 /*
jpayne@69 405 Tcl_WideUInt mp_get_mag_ull(const mp_int *a) MP_WUR;
jpayne@69 406 */
jpayne@69 407
jpayne@69 408 /* get integer, set integer (long) */
jpayne@69 409 /*
jpayne@69 410 long mp_get_l(const mp_int *a) MP_WUR;
jpayne@69 411 */
jpayne@69 412 /*
jpayne@69 413 void mp_set_l(mp_int *a, long b);
jpayne@69 414 */
jpayne@69 415 /*
jpayne@69 416 mp_err mp_init_l(mp_int *a, long b) MP_WUR;
jpayne@69 417 */
jpayne@69 418
jpayne@69 419 /* get integer, set integer (unsigned long) */
jpayne@69 420 #define mp_get_ul(a) ((unsigned long)mp_get_l(a))
jpayne@69 421 /*
jpayne@69 422 void mp_set_ul(mp_int *a, unsigned long b);
jpayne@69 423 */
jpayne@69 424 /*
jpayne@69 425 mp_err mp_init_ul(mp_int *a, unsigned long b) MP_WUR;
jpayne@69 426 */
jpayne@69 427
jpayne@69 428 /* get integer, set integer (Tcl_WideInt) */
jpayne@69 429 /*
jpayne@69 430 Tcl_WideInt mp_get_ll(const mp_int *a) MP_WUR;
jpayne@69 431 */
jpayne@69 432 /*
jpayne@69 433 void mp_set_ll(mp_int *a, Tcl_WideInt b);
jpayne@69 434 */
jpayne@69 435 /*
jpayne@69 436 mp_err mp_init_ll(mp_int *a, Tcl_WideInt b) MP_WUR;
jpayne@69 437 */
jpayne@69 438
jpayne@69 439 /* get integer, set integer (Tcl_WideUInt) */
jpayne@69 440 #define mp_get_ull(a) ((Tcl_WideUInt)mp_get_ll(a))
jpayne@69 441 /*
jpayne@69 442 void mp_set_ull(mp_int *a, Tcl_WideUInt b);
jpayne@69 443 */
jpayne@69 444 /*
jpayne@69 445 mp_err mp_init_ull(mp_int *a, Tcl_WideUInt b) MP_WUR;
jpayne@69 446 */
jpayne@69 447
jpayne@69 448 /* set to single unsigned digit, up to MP_DIGIT_MAX */
jpayne@69 449 /*
jpayne@69 450 void mp_set(mp_int *a, mp_digit b);
jpayne@69 451 */
jpayne@69 452 /*
jpayne@69 453 mp_err mp_init_set(mp_int *a, mp_digit b) MP_WUR;
jpayne@69 454 */
jpayne@69 455
jpayne@69 456 /* get integer, set integer and init with integer (deprecated) */
jpayne@69 457 /*
jpayne@69 458 MP_DEPRECATED(mp_get_mag_u32/mp_get_u32) unsigned long mp_get_int(const mp_int *a) MP_WUR;
jpayne@69 459 */
jpayne@69 460 /*
jpayne@69 461 MP_DEPRECATED(mp_get_mag_ul/mp_get_ul) unsigned long mp_get_long(const mp_int *a) MP_WUR;
jpayne@69 462 */
jpayne@69 463 /*
jpayne@69 464 MP_DEPRECATED(mp_get_mag_ull/mp_get_ull) Tcl_WideUInt mp_get_long_long(const mp_int *a) MP_WUR;
jpayne@69 465 */
jpayne@69 466 /*
jpayne@69 467 MP_DEPRECATED(mp_set_ul) mp_err mp_set_int(mp_int *a, unsigned long b);
jpayne@69 468 */
jpayne@69 469 /*
jpayne@69 470 MP_DEPRECATED(mp_set_ul) mp_err mp_set_long(mp_int *a, unsigned long b);
jpayne@69 471 */
jpayne@69 472 /*
jpayne@69 473 MP_DEPRECATED(mp_set_ull) mp_err mp_set_long_long(mp_int *a, Tcl_WideUInt b);
jpayne@69 474 */
jpayne@69 475 /*
jpayne@69 476 MP_DEPRECATED(mp_init_ul) mp_err mp_init_set_int(mp_int *a, unsigned long b) MP_WUR;
jpayne@69 477 */
jpayne@69 478
jpayne@69 479 /* copy, b = a */
jpayne@69 480 /*
jpayne@69 481 mp_err mp_copy(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 482 */
jpayne@69 483
jpayne@69 484 /* inits and copies, a = b */
jpayne@69 485 /*
jpayne@69 486 mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR;
jpayne@69 487 */
jpayne@69 488
jpayne@69 489 /* trim unused digits */
jpayne@69 490 /*
jpayne@69 491 void mp_clamp(mp_int *a);
jpayne@69 492 */
jpayne@69 493
jpayne@69 494 /* export binary data */
jpayne@69 495 /*
jpayne@69 496 MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
jpayne@69 497 int endian, size_t nails, const mp_int *op) MP_WUR;
jpayne@69 498 */
jpayne@69 499
jpayne@69 500 /* import binary data */
jpayne@69 501 /*
jpayne@69 502 MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int *rop, size_t count, int order,
jpayne@69 503 size_t size, int endian, size_t nails,
jpayne@69 504 const void *op) MP_WUR;
jpayne@69 505 */
jpayne@69 506
jpayne@69 507 /* unpack binary data */
jpayne@69 508 /*
jpayne@69 509 mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian,
jpayne@69 510 size_t nails, const void *op) MP_WUR;
jpayne@69 511 */
jpayne@69 512
jpayne@69 513 /* pack binary data */
jpayne@69 514 /*
jpayne@69 515 size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) MP_WUR;
jpayne@69 516 */
jpayne@69 517 /*
jpayne@69 518 mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size,
jpayne@69 519 mp_endian endian, size_t nails, const mp_int *op) MP_WUR;
jpayne@69 520 */
jpayne@69 521
jpayne@69 522 /* ---> digit manipulation <--- */
jpayne@69 523
jpayne@69 524 /* right shift by "b" digits */
jpayne@69 525 /*
jpayne@69 526 void mp_rshd(mp_int *a, int b);
jpayne@69 527 */
jpayne@69 528
jpayne@69 529 /* left shift by "b" digits */
jpayne@69 530 /*
jpayne@69 531 mp_err mp_lshd(mp_int *a, int b) MP_WUR;
jpayne@69 532 */
jpayne@69 533
jpayne@69 534 /* c = a / 2**b, implemented as c = a >> b */
jpayne@69 535 /*
jpayne@69 536 mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) MP_WUR;
jpayne@69 537 */
jpayne@69 538
jpayne@69 539 /* b = a/2 */
jpayne@69 540 /*
jpayne@69 541 mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 542 */
jpayne@69 543
jpayne@69 544 /* a/3 => 3c + d == a */
jpayne@69 545 /*
jpayne@69 546 mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
jpayne@69 547 */
jpayne@69 548
jpayne@69 549 /* c = a * 2**b, implemented as c = a << b */
jpayne@69 550 /*
jpayne@69 551 mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
jpayne@69 552 */
jpayne@69 553
jpayne@69 554 /* b = a*2 */
jpayne@69 555 /*
jpayne@69 556 mp_err mp_mul_2(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 557 */
jpayne@69 558
jpayne@69 559 /* c = a mod 2**b */
jpayne@69 560 /*
jpayne@69 561 mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
jpayne@69 562 */
jpayne@69 563
jpayne@69 564 /* computes a = 2**b */
jpayne@69 565 /*
jpayne@69 566 mp_err mp_2expt(mp_int *a, int b) MP_WUR;
jpayne@69 567 */
jpayne@69 568
jpayne@69 569 /* Counts the number of lsbs which are zero before the first zero bit */
jpayne@69 570 /*
jpayne@69 571 int mp_cnt_lsb(const mp_int *a) MP_WUR;
jpayne@69 572 */
jpayne@69 573
jpayne@69 574 /* I Love Earth! */
jpayne@69 575
jpayne@69 576 /* makes a pseudo-random mp_int of a given size */
jpayne@69 577 /*
jpayne@69 578 mp_err mp_rand(mp_int *a, int digits) MP_WUR;
jpayne@69 579 */
jpayne@69 580 /* makes a pseudo-random small int of a given size */
jpayne@69 581 /*
jpayne@69 582 MP_DEPRECATED(mp_rand) mp_err mp_rand_digit(mp_digit *r) MP_WUR;
jpayne@69 583 */
jpayne@69 584 /* use custom random data source instead of source provided the platform */
jpayne@69 585 /*
jpayne@69 586 void mp_rand_source(mp_err(*source)(void *out, size_t size));
jpayne@69 587 */
jpayne@69 588
jpayne@69 589 #ifdef MP_PRNG_ENABLE_LTM_RNG
jpayne@69 590 /* A last resort to provide random data on systems without any of the other
jpayne@69 591 * implemented ways to gather entropy.
jpayne@69 592 * It is compatible with `rng_get_bytes()` from libtomcrypt so you could
jpayne@69 593 * provide that one and then set `ltm_rng = rng_get_bytes;` */
jpayne@69 594 extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
jpayne@69 595 extern void (*ltm_rng_callback)(void);
jpayne@69 596 #endif
jpayne@69 597
jpayne@69 598 /* ---> binary operations <--- */
jpayne@69 599
jpayne@69 600 /* Checks the bit at position b and returns MP_YES
jpayne@69 601 * if the bit is 1, MP_NO if it is 0 and MP_VAL
jpayne@69 602 * in case of error
jpayne@69 603 */
jpayne@69 604 /*
jpayne@69 605 MP_DEPRECATED(s_mp_get_bit) int mp_get_bit(const mp_int *a, int b) MP_WUR;
jpayne@69 606 */
jpayne@69 607
jpayne@69 608 /* c = a XOR b (two complement) */
jpayne@69 609 /*
jpayne@69 610 MP_DEPRECATED(mp_xor) mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 611 */
jpayne@69 612 /*
jpayne@69 613 mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 614 */
jpayne@69 615
jpayne@69 616 /* c = a OR b (two complement) */
jpayne@69 617 /*
jpayne@69 618 MP_DEPRECATED(mp_or) mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 619 */
jpayne@69 620 /*
jpayne@69 621 mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 622 */
jpayne@69 623
jpayne@69 624 /* c = a AND b (two complement) */
jpayne@69 625 /*
jpayne@69 626 MP_DEPRECATED(mp_and) mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 627 */
jpayne@69 628 /*
jpayne@69 629 mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 630 */
jpayne@69 631
jpayne@69 632 /* b = ~a (bitwise not, two complement) */
jpayne@69 633 /*
jpayne@69 634 mp_err mp_complement(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 635 */
jpayne@69 636
jpayne@69 637 /* right shift with sign extension */
jpayne@69 638 /*
jpayne@69 639 MP_DEPRECATED(mp_signed_rsh) mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c) MP_WUR;
jpayne@69 640 */
jpayne@69 641 /*
jpayne@69 642 mp_err mp_signed_rsh(const mp_int *a, int b, mp_int *c) MP_WUR;
jpayne@69 643 */
jpayne@69 644
jpayne@69 645 /* ---> Basic arithmetic <--- */
jpayne@69 646
jpayne@69 647 /* b = -a */
jpayne@69 648 /*
jpayne@69 649 mp_err mp_neg(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 650 */
jpayne@69 651
jpayne@69 652 /* b = |a| */
jpayne@69 653 /*
jpayne@69 654 mp_err mp_abs(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 655 */
jpayne@69 656
jpayne@69 657 /* compare a to b */
jpayne@69 658 /*
jpayne@69 659 mp_ord mp_cmp(const mp_int *a, const mp_int *b) MP_WUR;
jpayne@69 660 */
jpayne@69 661
jpayne@69 662 /* compare |a| to |b| */
jpayne@69 663 /*
jpayne@69 664 mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b) MP_WUR;
jpayne@69 665 */
jpayne@69 666
jpayne@69 667 /* c = a + b */
jpayne@69 668 /*
jpayne@69 669 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 670 */
jpayne@69 671
jpayne@69 672 /* c = a - b */
jpayne@69 673 /*
jpayne@69 674 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 675 */
jpayne@69 676
jpayne@69 677 /* c = a * b */
jpayne@69 678 /*
jpayne@69 679 mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 680 */
jpayne@69 681
jpayne@69 682 /* b = a*a */
jpayne@69 683 /*
jpayne@69 684 mp_err mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
jpayne@69 685 */
jpayne@69 686
jpayne@69 687 /* a/b => cb + d == a */
jpayne@69 688 /*
jpayne@69 689 mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
jpayne@69 690 */
jpayne@69 691
jpayne@69 692 /* c = a mod b, 0 <= c < b */
jpayne@69 693 /*
jpayne@69 694 mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 695 */
jpayne@69 696
jpayne@69 697 /* Increment "a" by one like "a++". Changes input! */
jpayne@69 698 /*
jpayne@69 699 mp_err mp_incr(mp_int *a) MP_WUR;
jpayne@69 700 */
jpayne@69 701
jpayne@69 702 /* Decrement "a" by one like "a--". Changes input! */
jpayne@69 703 /*
jpayne@69 704 mp_err mp_decr(mp_int *a) MP_WUR;
jpayne@69 705 */
jpayne@69 706
jpayne@69 707 /* ---> single digit functions <--- */
jpayne@69 708
jpayne@69 709 /* compare against a single digit */
jpayne@69 710 /*
jpayne@69 711 mp_ord mp_cmp_d(const mp_int *a, mp_digit b) MP_WUR;
jpayne@69 712 */
jpayne@69 713
jpayne@69 714 /* c = a + b */
jpayne@69 715 /*
jpayne@69 716 mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
jpayne@69 717 */
jpayne@69 718
jpayne@69 719 /* c = a - b */
jpayne@69 720 /*
jpayne@69 721 mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
jpayne@69 722 */
jpayne@69 723
jpayne@69 724 /* c = a * b */
jpayne@69 725 /*
jpayne@69 726 mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
jpayne@69 727 */
jpayne@69 728
jpayne@69 729 /* a/b => cb + d == a */
jpayne@69 730 /*
jpayne@69 731 mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) MP_WUR;
jpayne@69 732 */
jpayne@69 733
jpayne@69 734 /* c = a mod b, 0 <= c < b */
jpayne@69 735 /*
jpayne@69 736 mp_err mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) MP_WUR;
jpayne@69 737 */
jpayne@69 738
jpayne@69 739 /* ---> number theory <--- */
jpayne@69 740
jpayne@69 741 /* d = a + b (mod c) */
jpayne@69 742 /*
jpayne@69 743 mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
jpayne@69 744 */
jpayne@69 745
jpayne@69 746 /* d = a - b (mod c) */
jpayne@69 747 /*
jpayne@69 748 mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
jpayne@69 749 */
jpayne@69 750
jpayne@69 751 /* d = a * b (mod c) */
jpayne@69 752 /*
jpayne@69 753 mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) MP_WUR;
jpayne@69 754 */
jpayne@69 755
jpayne@69 756 /* c = a * a (mod b) */
jpayne@69 757 /*
jpayne@69 758 mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 759 */
jpayne@69 760
jpayne@69 761 /* c = 1/a (mod b) */
jpayne@69 762 /*
jpayne@69 763 mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 764 */
jpayne@69 765
jpayne@69 766 /* c = (a, b) */
jpayne@69 767 /*
jpayne@69 768 mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 769 */
jpayne@69 770
jpayne@69 771 /* produces value such that U1*a + U2*b = U3 */
jpayne@69 772 /*
jpayne@69 773 mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) MP_WUR;
jpayne@69 774 */
jpayne@69 775
jpayne@69 776 /* c = [a, b] or (a*b)/(a, b) */
jpayne@69 777 /*
jpayne@69 778 mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
jpayne@69 779 */
jpayne@69 780
jpayne@69 781 /* finds one of the b'th root of a, such that |c|**b <= |a|
jpayne@69 782 *
jpayne@69 783 * returns error if a < 0 and b is even
jpayne@69 784 */
jpayne@69 785 /*
jpayne@69 786 mp_err mp_root_u32(const mp_int *a, unsigned int b, mp_int *c) MP_WUR;
jpayne@69 787 */
jpayne@69 788 /*
jpayne@69 789 MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
jpayne@69 790 */
jpayne@69 791 /*
jpayne@69 792 MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
jpayne@69 793 */
jpayne@69 794
jpayne@69 795 /* special sqrt algo */
jpayne@69 796 /*
jpayne@69 797 mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR;
jpayne@69 798 */
jpayne@69 799
jpayne@69 800 /* special sqrt (mod prime) */
jpayne@69 801 /*
jpayne@69 802 mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) MP_WUR;
jpayne@69 803 */
jpayne@69 804
jpayne@69 805 /* is number a square? */
jpayne@69 806 /*
jpayne@69 807 mp_err mp_is_square(const mp_int *arg, mp_bool *ret) MP_WUR;
jpayne@69 808 */
jpayne@69 809
jpayne@69 810 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
jpayne@69 811 /*
jpayne@69 812 MP_DEPRECATED(mp_kronecker) mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c) MP_WUR;
jpayne@69 813 */
jpayne@69 814
jpayne@69 815 /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
jpayne@69 816 /*
jpayne@69 817 mp_err mp_kronecker(const mp_int *a, const mp_int *p, int *c) MP_WUR;
jpayne@69 818 */
jpayne@69 819
jpayne@69 820 /* used to setup the Barrett reduction for a given modulus b */
jpayne@69 821 /*
jpayne@69 822 mp_err mp_reduce_setup(mp_int *a, const mp_int *b) MP_WUR;
jpayne@69 823 */
jpayne@69 824
jpayne@69 825 /* Barrett Reduction, computes a (mod b) with a precomputed value c
jpayne@69 826 *
jpayne@69 827 * Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
jpayne@69 828 * compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
jpayne@69 829 */
jpayne@69 830 /*
jpayne@69 831 mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) MP_WUR;
jpayne@69 832 */
jpayne@69 833
jpayne@69 834 /* setups the montgomery reduction */
jpayne@69 835 /*
jpayne@69 836 mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho) MP_WUR;
jpayne@69 837 */
jpayne@69 838
jpayne@69 839 /* computes a = B**n mod b without division or multiplication useful for
jpayne@69 840 * normalizing numbers in a Montgomery system.
jpayne@69 841 */
jpayne@69 842 /*
jpayne@69 843 mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) MP_WUR;
jpayne@69 844 */
jpayne@69 845
jpayne@69 846 /* computes x/R == x (mod N) via Montgomery Reduction */
jpayne@69 847 /*
jpayne@69 848 mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
jpayne@69 849 */
jpayne@69 850
jpayne@69 851 /* returns 1 if a is a valid DR modulus */
jpayne@69 852 /*
jpayne@69 853 mp_bool mp_dr_is_modulus(const mp_int *a) MP_WUR;
jpayne@69 854 */
jpayne@69 855
jpayne@69 856 /* sets the value of "d" required for mp_dr_reduce */
jpayne@69 857 /*
jpayne@69 858 void mp_dr_setup(const mp_int *a, mp_digit *d);
jpayne@69 859 */
jpayne@69 860
jpayne@69 861 /* reduces a modulo n using the Diminished Radix method */
jpayne@69 862 /*
jpayne@69 863 mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k) MP_WUR;
jpayne@69 864 */
jpayne@69 865
jpayne@69 866 /* returns true if a can be reduced with mp_reduce_2k */
jpayne@69 867 /*
jpayne@69 868 mp_bool mp_reduce_is_2k(const mp_int *a) MP_WUR;
jpayne@69 869 */
jpayne@69 870
jpayne@69 871 /* determines k value for 2k reduction */
jpayne@69 872 /*
jpayne@69 873 mp_err mp_reduce_2k_setup(const mp_int *a, mp_digit *d) MP_WUR;
jpayne@69 874 */
jpayne@69 875
jpayne@69 876 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
jpayne@69 877 /*
jpayne@69 878 mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) MP_WUR;
jpayne@69 879 */
jpayne@69 880
jpayne@69 881 /* returns true if a can be reduced with mp_reduce_2k_l */
jpayne@69 882 /*
jpayne@69 883 mp_bool mp_reduce_is_2k_l(const mp_int *a) MP_WUR;
jpayne@69 884 */
jpayne@69 885
jpayne@69 886 /* determines k value for 2k reduction */
jpayne@69 887 /*
jpayne@69 888 mp_err mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) MP_WUR;
jpayne@69 889 */
jpayne@69 890
jpayne@69 891 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
jpayne@69 892 /*
jpayne@69 893 mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) MP_WUR;
jpayne@69 894 */
jpayne@69 895
jpayne@69 896 /* Y = G**X (mod P) */
jpayne@69 897 /*
jpayne@69 898 mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) MP_WUR;
jpayne@69 899 */
jpayne@69 900
jpayne@69 901 /* ---> Primes <--- */
jpayne@69 902
jpayne@69 903 /* number of primes */
jpayne@69 904 #ifdef MP_8BIT
jpayne@69 905 # define PRIVATE_MP_PRIME_TAB_SIZE 31
jpayne@69 906 #else
jpayne@69 907 # define PRIVATE_MP_PRIME_TAB_SIZE 256
jpayne@69 908 #endif
jpayne@69 909 #define PRIME_SIZE (MP_DEPRECATED_PRAGMA("PRIME_SIZE has been made internal") PRIVATE_MP_PRIME_TAB_SIZE)
jpayne@69 910
jpayne@69 911 /* table of first PRIME_SIZE primes */
jpayne@69 912 #if defined(BUILD_tcl) || !defined(_WIN32)
jpayne@69 913 MODULE_SCOPE const mp_digit ltm_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE];
jpayne@69 914 #endif
jpayne@69 915
jpayne@69 916 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
jpayne@69 917 /*
jpayne@69 918 MP_DEPRECATED(mp_prime_is_prime) mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result) MP_WUR;
jpayne@69 919 */
jpayne@69 920
jpayne@69 921 /* performs one Fermat test of "a" using base "b".
jpayne@69 922 * Sets result to 0 if composite or 1 if probable prime
jpayne@69 923 */
jpayne@69 924 /*
jpayne@69 925 mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
jpayne@69 926 */
jpayne@69 927
jpayne@69 928 /* performs one Miller-Rabin test of "a" using base "b".
jpayne@69 929 * Sets result to 0 if composite or 1 if probable prime
jpayne@69 930 */
jpayne@69 931 /*
jpayne@69 932 mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, mp_bool *result) MP_WUR;
jpayne@69 933 */
jpayne@69 934
jpayne@69 935 /* This gives [for a given bit size] the number of trials required
jpayne@69 936 * such that Miller-Rabin gives a prob of failure lower than 2^-96
jpayne@69 937 */
jpayne@69 938 /*
jpayne@69 939 int mp_prime_rabin_miller_trials(int size) MP_WUR;
jpayne@69 940 */
jpayne@69 941
jpayne@69 942 /* performs one strong Lucas-Selfridge test of "a".
jpayne@69 943 * Sets result to 0 if composite or 1 if probable prime
jpayne@69 944 */
jpayne@69 945 /*
jpayne@69 946 mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) MP_WUR;
jpayne@69 947 */
jpayne@69 948
jpayne@69 949 /* performs one Frobenius test of "a" as described by Paul Underwood.
jpayne@69 950 * Sets result to 0 if composite or 1 if probable prime
jpayne@69 951 */
jpayne@69 952 /*
jpayne@69 953 mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) MP_WUR;
jpayne@69 954 */
jpayne@69 955
jpayne@69 956 /* performs t random rounds of Miller-Rabin on "a" additional to
jpayne@69 957 * bases 2 and 3. Also performs an initial sieve of trial
jpayne@69 958 * division. Determines if "a" is prime with probability
jpayne@69 959 * of error no more than (1/4)**t.
jpayne@69 960 * Both a strong Lucas-Selfridge to complete the BPSW test
jpayne@69 961 * and a separate Frobenius test are available at compile time.
jpayne@69 962 * With t<0 a deterministic test is run for primes up to
jpayne@69 963 * 318665857834031151167461. With t<13 (abs(t)-13) additional
jpayne@69 964 * tests with sequential small primes are run starting at 43.
jpayne@69 965 * Is Fips 186.4 compliant if called with t as computed by
jpayne@69 966 * mp_prime_rabin_miller_trials();
jpayne@69 967 *
jpayne@69 968 * Sets result to 1 if probably prime, 0 otherwise
jpayne@69 969 */
jpayne@69 970 /*
jpayne@69 971 mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) MP_WUR;
jpayne@69 972 */
jpayne@69 973
jpayne@69 974 /* finds the next prime after the number "a" using "t" trials
jpayne@69 975 * of Miller-Rabin.
jpayne@69 976 *
jpayne@69 977 * bbs_style = 1 means the prime must be congruent to 3 mod 4
jpayne@69 978 */
jpayne@69 979 /*
jpayne@69 980 mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) MP_WUR;
jpayne@69 981 */
jpayne@69 982
jpayne@69 983 /* makes a truly random prime of a given size (bytes),
jpayne@69 984 * call with bbs = 1 if you want it to be congruent to 3 mod 4
jpayne@69 985 *
jpayne@69 986 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
jpayne@69 987 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
jpayne@69 988 * so it can be NULL
jpayne@69 989 *
jpayne@69 990 * The prime generated will be larger than 2^(8*size).
jpayne@69 991 */
jpayne@69 992 #define mp_prime_random(a, t, size, bbs, cb, dat) (MP_DEPRECATED_PRAGMA("mp_prime_random has been deprecated, use mp_prime_rand instead") mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?MP_PRIME_BBS:0, cb, dat))
jpayne@69 993
jpayne@69 994 /* makes a truly random prime of a given size (bits),
jpayne@69 995 *
jpayne@69 996 * Flags are as follows:
jpayne@69 997 *
jpayne@69 998 * MP_PRIME_BBS - make prime congruent to 3 mod 4
jpayne@69 999 * MP_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies MP_PRIME_BBS)
jpayne@69 1000 * MP_PRIME_2MSB_ON - make the 2nd highest bit one
jpayne@69 1001 *
jpayne@69 1002 * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
jpayne@69 1003 * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
jpayne@69 1004 * so it can be NULL
jpayne@69 1005 *
jpayne@69 1006 */
jpayne@69 1007 /*
jpayne@69 1008 MP_DEPRECATED(mp_prime_rand) mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags,
jpayne@69 1009 private_mp_prime_callback cb, void *dat) MP_WUR;
jpayne@69 1010 */
jpayne@69 1011 /*
jpayne@69 1012 mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR;
jpayne@69 1013 */
jpayne@69 1014
jpayne@69 1015 /* Integer logarithm to integer base */
jpayne@69 1016 /*
jpayne@69 1017 mp_err mp_log_u32(const mp_int *a, unsigned int base, unsigned int *c) MP_WUR;
jpayne@69 1018 */
jpayne@69 1019
jpayne@69 1020 /* c = a**b */
jpayne@69 1021 /*
jpayne@69 1022 mp_err mp_expt_u32(const mp_int *a, unsigned int b, mp_int *c) MP_WUR;
jpayne@69 1023 */
jpayne@69 1024 /*
jpayne@69 1025 MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR;
jpayne@69 1026 */
jpayne@69 1027 /*
jpayne@69 1028 MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR;
jpayne@69 1029 */
jpayne@69 1030
jpayne@69 1031 /* ---> radix conversion <--- */
jpayne@69 1032 /*
jpayne@69 1033 int mp_count_bits(const mp_int *a) MP_WUR;
jpayne@69 1034 */
jpayne@69 1035
jpayne@69 1036
jpayne@69 1037 /*
jpayne@69 1038 MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR;
jpayne@69 1039 */
jpayne@69 1040 /*
jpayne@69 1041 MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
jpayne@69 1042 */
jpayne@69 1043 /*
jpayne@69 1044 MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR;
jpayne@69 1045 */
jpayne@69 1046 /*
jpayne@69 1047 MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
jpayne@69 1048 */
jpayne@69 1049
jpayne@69 1050 /*
jpayne@69 1051 MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int *a) MP_WUR;
jpayne@69 1052 */
jpayne@69 1053 /*
jpayne@69 1054 MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR;
jpayne@69 1055 */
jpayne@69 1056 /*
jpayne@69 1057 MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR;
jpayne@69 1058 */
jpayne@69 1059 /*
jpayne@69 1060 MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR;
jpayne@69 1061 */
jpayne@69 1062
jpayne@69 1063 /*
jpayne@69 1064 size_t mp_ubin_size(const mp_int *a) MP_WUR;
jpayne@69 1065 */
jpayne@69 1066 /*
jpayne@69 1067 mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
jpayne@69 1068 */
jpayne@69 1069 /*
jpayne@69 1070 mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
jpayne@69 1071 */
jpayne@69 1072
jpayne@69 1073 /*
jpayne@69 1074 size_t mp_sbin_size(const mp_int *a) MP_WUR;
jpayne@69 1075 */
jpayne@69 1076 /*
jpayne@69 1077 mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR;
jpayne@69 1078 */
jpayne@69 1079 /*
jpayne@69 1080 mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR;
jpayne@69 1081 */
jpayne@69 1082
jpayne@69 1083 /*
jpayne@69 1084 mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
jpayne@69 1085 */
jpayne@69 1086 /*
jpayne@69 1087 MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR;
jpayne@69 1088 */
jpayne@69 1089 /*
jpayne@69 1090 MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR;
jpayne@69 1091 */
jpayne@69 1092 /*
jpayne@69 1093 mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;
jpayne@69 1094 */
jpayne@69 1095 /*
jpayne@69 1096 mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
jpayne@69 1097 */
jpayne@69 1098
jpayne@69 1099 #ifndef MP_NO_FILE
jpayne@69 1100 /*
jpayne@69 1101 mp_err mp_fread(mp_int *a, int radix, FILE *stream) MP_WUR;
jpayne@69 1102 */
jpayne@69 1103 /*
jpayne@69 1104 mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR;
jpayne@69 1105 */
jpayne@69 1106 #endif
jpayne@69 1107
jpayne@69 1108 #define mp_read_raw(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_signed_bin") mp_read_signed_bin((mp), (str), (len)))
jpayne@69 1109 #define mp_raw_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_signed_bin_size") mp_signed_bin_size(mp))
jpayne@69 1110 #define mp_toraw(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_signed_bin") mp_to_signed_bin((mp), (str)))
jpayne@69 1111 #define mp_read_mag(mp, str, len) (MP_DEPRECATED_PRAGMA("replaced by mp_read_unsigned_bin") mp_read_unsigned_bin((mp), (str), (len))
jpayne@69 1112 #define mp_mag_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_unsigned_bin_size") mp_unsigned_bin_size(mp))
jpayne@69 1113 #define mp_tomag(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_unsigned_bin") mp_to_unsigned_bin((mp), (str)))
jpayne@69 1114
jpayne@69 1115 #define mp_tobinary(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_binary") mp_toradix((M), (S), 2))
jpayne@69 1116 #define mp_tooctal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_octal") mp_toradix((M), (S), 8))
jpayne@69 1117 #define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10))
jpayne@69 1118 #define mp_tohex(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex") mp_toradix((M), (S), 16))
jpayne@69 1119
jpayne@69 1120 #define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), NULL, 2)
jpayne@69 1121 #define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), NULL, 8)
jpayne@69 1122 #define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), NULL, 10)
jpayne@69 1123 #define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), NULL, 16)
jpayne@69 1124
jpayne@69 1125 #ifdef __cplusplus
jpayne@69 1126 }
jpayne@69 1127 #endif
jpayne@69 1128
jpayne@69 1129 #include "tclTomMathDecls.h"
jpayne@69 1130
jpayne@69 1131 #endif