1436
|
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, [email protected], http://math.libtomcrypt.com |
|
14 */ |
|
15 #ifndef TOMMATH_PRIV_H_ |
|
16 #define TOMMATH_PRIV_H_ |
|
17 |
|
18 #include <tommath.h> |
|
19 #include <ctype.h> |
|
20 |
|
21 #define MIN(x,y) (((x) < (y)) ? (x) : (y)) |
|
22 |
|
23 #define MAX(x,y) (((x) > (y)) ? (x) : (y)) |
|
24 |
|
25 #ifdef __cplusplus |
|
26 extern "C" { |
|
27 |
|
28 /* C++ compilers don't like assigning void * to mp_digit * */ |
|
29 #define OPT_CAST(x) (x *) |
|
30 |
|
31 #else |
|
32 |
|
33 /* C on the other hand doesn't care */ |
|
34 #define OPT_CAST(x) |
|
35 |
|
36 #endif |
|
37 |
|
38 /* define heap macros */ |
|
39 #ifndef XMALLOC |
|
40 /* default to libc stuff */ |
|
41 #define XMALLOC malloc |
|
42 #define XFREE free |
|
43 #define XREALLOC realloc |
|
44 #define XCALLOC calloc |
|
45 #else |
|
46 /* prototypes for our heap functions */ |
|
47 extern void *XMALLOC(size_t n); |
|
48 extern void *XREALLOC(void *p, size_t n); |
|
49 extern void *XCALLOC(size_t n, size_t s); |
|
50 extern void XFREE(void *p); |
|
51 #endif |
|
52 |
|
53 /* lowlevel functions, do not call! */ |
|
54 int s_mp_add(mp_int *a, mp_int *b, mp_int *c); |
|
55 int s_mp_sub(mp_int *a, mp_int *b, mp_int *c); |
|
56 #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1) |
|
57 int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
|
58 int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
|
59 int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
|
60 int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs); |
|
61 int fast_s_mp_sqr(mp_int *a, mp_int *b); |
|
62 int s_mp_sqr(mp_int *a, mp_int *b); |
|
63 int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c); |
|
64 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c); |
|
65 int mp_karatsuba_sqr(mp_int *a, mp_int *b); |
|
66 int mp_toom_sqr(mp_int *a, mp_int *b); |
|
67 int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c); |
|
68 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); |
|
69 int fast_mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho); |
|
70 int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode); |
|
71 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode); |
|
72 void bn_reverse(unsigned char *s, int len); |
|
73 |
|
74 extern const char *mp_s_rmap; |
|
75 |
|
76 /* Fancy macro to set an MPI from another type. |
|
77 * There are several things assumed: |
|
78 * x is the counter and unsigned |
|
79 * a is the pointer to the MPI |
|
80 * b is the original value that should be set in the MPI. |
|
81 */ |
|
82 #define MP_SET_XLONG(func_name, type) \ |
|
83 int func_name (mp_int * a, type b) \ |
|
84 { \ |
|
85 unsigned int x; \ |
|
86 int res; \ |
|
87 \ |
|
88 mp_zero (a); \ |
|
89 \ |
|
90 /* set four bits at a time */ \ |
|
91 for (x = 0; x < (sizeof(type) * 2u); x++) { \ |
|
92 /* shift the number up four bits */ \ |
|
93 if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \ |
|
94 return res; \ |
|
95 } \ |
|
96 \ |
|
97 /* OR in the top four bits of the source */ \ |
|
98 a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \ |
|
99 \ |
|
100 /* shift the source up to the next four bits */ \ |
|
101 b <<= 4; \ |
|
102 \ |
|
103 /* ensure that digits are not clamped off */ \ |
|
104 a->used += 1; \ |
|
105 } \ |
|
106 mp_clamp (a); \ |
|
107 return MP_OKAY; \ |
|
108 } |
|
109 |
|
110 #ifdef __cplusplus |
|
111 } |
|
112 #endif |
|
113 |
|
114 #endif |
|
115 |
|
116 |
|
117 /* $Source$ */ |
|
118 /* $Revision$ */ |
|
119 /* $Date$ */ |