16 | 16 |
|
17 | 17 |
#include <stdio.h>
|
18 | 18 |
#include <stdlib.h>
|
19 | |
#ifdef LTM_NO_STDINT_H
|
20 | |
typedef unsigned char mp_uint8;
|
21 | |
typedef unsigned short mp_uint16;
|
22 | |
typedef unsigned int mp_uint32;
|
23 | |
typedef unsigned int mp_uint_least32;
|
24 | |
#ifdef _MSC_VER
|
25 | |
typedef unsigned __int64 mp_uint64;
|
26 | |
#else
|
27 | |
typedef unsigned long long mp_uint64;
|
28 | |
#endif
|
29 | |
#else
|
30 | |
#include <stdint.h>
|
31 | |
typedef uint8_t mp_uint8;
|
32 | |
typedef uint16_t mp_uint16;
|
33 | |
typedef uint32_t mp_uint32;
|
34 | |
typedef uint_least32_t mp_uint_least32;
|
35 | |
typedef uint64_t mp_uint64;
|
36 | |
#endif
|
37 | 19 |
#include <limits.h>
|
38 | 20 |
|
39 | 21 |
#include <tommath_class.h>
|
|
42 | 24 |
extern "C" {
|
43 | 25 |
#endif
|
44 | 26 |
|
|
27 |
/* unsigned int types */
|
|
28 |
typedef unsigned char mp_uint8;
|
|
29 |
typedef unsigned short mp_uint16;
|
|
30 |
typedef unsigned int mp_uint32;
|
|
31 |
#ifdef _MSC_VER
|
|
32 |
typedef unsigned __int64 mp_uint64;
|
|
33 |
#else
|
|
34 |
typedef unsigned long long mp_uint64;
|
|
35 |
#endif
|
|
36 |
|
45 | 37 |
/* detect 64-bit mode if possible */
|
46 | |
#if defined(__x86_64__)
|
47 | |
#if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
|
48 | |
#define MP_64BIT
|
|
38 |
#if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
|
|
39 |
#if defined(__x86_64__)
|
|
40 |
#if defined(__GNUC__)
|
|
41 |
typedef unsigned long mp_uint128 __attribute__ ((mode(TI)));
|
|
42 |
#define MP_64BIT
|
|
43 |
#elif defined(_MSC_VER)
|
|
44 |
typedef unsigned __int128 mp_uint128;
|
|
45 |
#define MP_64BIT
|
|
46 |
#endif
|
49 | 47 |
#endif
|
50 | 48 |
#endif
|
51 | 49 |
|
|
58 | 56 |
* [any size beyond that is ok provided it doesn't overflow the data type]
|
59 | 57 |
*/
|
60 | 58 |
#ifdef MP_8BIT
|
61 | |
typedef mp_uint8 mp_digit;
|
62 | |
typedef mp_uint16 mp_word;
|
63 | |
#define MP_SIZEOF_MP_DIGIT 1
|
64 | |
#ifdef DIGIT_BIT
|
65 | |
#error You must not define DIGIT_BIT when using MP_8BIT
|
66 | |
#endif
|
|
59 |
typedef mp_uint8 mp_digit;
|
|
60 |
typedef mp_uint16 mp_word;
|
|
61 |
#define DIGIT_BIT 7
|
67 | 62 |
#elif defined(MP_16BIT)
|
68 | |
typedef mp_uint16 mp_digit;
|
69 | |
typedef mp_uint32 mp_word;
|
70 | |
#define MP_SIZEOF_MP_DIGIT 2
|
71 | |
#ifdef DIGIT_BIT
|
72 | |
#error You must not define DIGIT_BIT when using MP_16BIT
|
73 | |
#endif
|
|
63 |
typedef mp_uint16 mp_digit;
|
|
64 |
typedef mp_uint32 mp_word;
|
|
65 |
#define DIGIT_BIT 15
|
74 | 66 |
#elif defined(MP_64BIT)
|
75 | |
/* for GCC only on supported platforms */
|
76 | |
typedef mp_uint64 mp_digit;
|
77 | |
#if defined(_WIN32)
|
78 | |
typedef unsigned __int128 mp_word;
|
79 | |
#elif defined(__GNUC__)
|
80 | |
typedef unsigned long mp_word __attribute__ ((mode(TI)));
|
|
67 |
typedef mp_uint64 mp_digit;
|
|
68 |
typedef mp_uint128 mp_word;
|
|
69 |
#define DIGIT_BIT 60
|
|
70 |
#elif defined(MP_32BIT)
|
|
71 |
typedef mp_uint32 mp_digit;
|
|
72 |
typedef mp_uint64 mp_word;
|
|
73 |
#define DIGIT_BIT 31
|
81 | 74 |
#else
|
82 | |
/* it seems you have a problem
|
83 | |
* but we assume you can somewhere define your own uint128_t */
|
84 | |
typedef uint128_t mp_word;
|
85 | |
#endif
|
86 | |
|
87 | |
#define DIGIT_BIT 60
|
88 | |
#else
|
89 | |
/* this is the default case, 28-bit digits */
|
90 | |
typedef mp_uint32 mp_digit;
|
91 | |
typedef mp_uint64 mp_word;
|
92 | |
|
93 | |
#ifdef MP_31BIT
|
94 | |
/* this is an extension that uses 31-bit digits */
|
95 | |
#define DIGIT_BIT 31
|
96 | |
#else
|
97 | |
/* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
|
98 | |
#define DIGIT_BIT 28
|
|
75 |
typedef mp_uint32 mp_digit;
|
|
76 |
typedef mp_uint64 mp_word;
|
|
77 |
#define DIGIT_BIT 28
|
99 | 78 |
#define MP_28BIT
|
100 | |
#endif
|
101 | |
#endif
|
102 | |
|
103 | |
/* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
|
104 | |
#ifndef DIGIT_BIT
|
105 | |
#define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */
|
106 | |
typedef mp_uint_least32 mp_min_u32;
|
107 | |
#else
|
108 | |
typedef mp_digit mp_min_u32;
|
109 | 79 |
#endif
|
110 | 80 |
|
111 | 81 |
/* platforms that can use a better rand function */
|
|
228 | 198 |
/* set a platform dependent unsigned long value */
|
229 | 199 |
int mp_set_long(mp_int *a, unsigned long b);
|
230 | 200 |
|
231 | |
/* set a platform dependent mp_uint64 value */
|
232 | |
int mp_set_long_long(mp_int *a, mp_uint64 b);
|
|
201 |
/* set a platform dependent unsigned long long value */
|
|
202 |
int mp_set_long_long(mp_int *a, unsigned long long b);
|
233 | 203 |
|
234 | 204 |
/* get a 32-bit value */
|
235 | 205 |
unsigned long mp_get_int(mp_int * a);
|
|
237 | 207 |
/* get a platform dependent unsigned long value */
|
238 | 208 |
unsigned long mp_get_long(mp_int * a);
|
239 | 209 |
|
240 | |
/* get a platform dependent mp_uint64 value */
|
241 | |
mp_uint64 mp_get_long_long(mp_int * a);
|
|
210 |
/* get a platform dependent unsigned long long value */
|
|
211 |
unsigned long long mp_get_long_long(mp_int * a);
|
242 | 212 |
|
243 | 213 |
/* initialize and set a digit */
|
244 | 214 |
int mp_init_set (mp_int * a, mp_digit b);
|