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