comparison libtommath/tommath.h @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
5 * 5 *
6 * The library was designed directly after the MPI library by 6 * The library was designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with 7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place. 8 * additional optimizations in place.
9 * 9 *
10 * The library is free for all purposes without any express 10 * SPDX-License-Identifier: Unlicense
11 * guarantee it works.
12 *
13 * Tom St Denis, [email protected], http://math.libtomcrypt.com
14 */ 11 */
15 #ifndef BN_H_ 12 #ifndef BN_H_
16 #define BN_H_ 13 #define BN_H_
17 14
18 #include <stdio.h> 15 #include <stdio.h>
22 19
23 #include "tommath_class.h" 20 #include "tommath_class.h"
24 21
25 #ifdef __cplusplus 22 #ifdef __cplusplus
26 extern "C" { 23 extern "C" {
24 #endif
25
26 /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */
27 #if defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)
28 # define MP_32BIT
27 #endif 29 #endif
28 30
29 /* detect 64-bit mode if possible */ 31 /* detect 64-bit mode if possible */
30 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \ 32 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
31 defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \ 33 defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
32 defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \ 34 defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
33 defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \ 35 defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
34 defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \ 36 defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
35 defined(__LP64__) || defined(_LP64) || defined(__64BIT__) 37 defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
36 #if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT)) 38 # if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
37 #define MP_64BIT 39 # if defined(__GNUC__)
38 #endif 40 /* we support 128bit integers only via: __attribute__((mode(TI))) */
41 # define MP_64BIT
42 # else
43 /* otherwise we fall back to MP_32BIT even on 64bit platforms */
44 # define MP_32BIT
45 # endif
46 # endif
39 #endif 47 #endif
40 48
41 /* some default configurations. 49 /* some default configurations.
42 * 50 *
43 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits 51 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
45 * 53 *
46 * At the very least a mp_digit must be able to hold 7 bits 54 * At the very least a mp_digit must be able to hold 7 bits
47 * [any size beyond that is ok provided it doesn't overflow the data type] 55 * [any size beyond that is ok provided it doesn't overflow the data type]
48 */ 56 */
49 #ifdef MP_8BIT 57 #ifdef MP_8BIT
50 typedef uint8_t mp_digit; 58 typedef uint8_t mp_digit;
51 typedef uint16_t mp_word; 59 typedef uint16_t mp_word;
52 #define MP_SIZEOF_MP_DIGIT 1 60 # define MP_SIZEOF_MP_DIGIT 1
53 #ifdef DIGIT_BIT 61 # ifdef DIGIT_BIT
54 #error You must not define DIGIT_BIT when using MP_8BIT 62 # error You must not define DIGIT_BIT when using MP_8BIT
55 #endif 63 # endif
56 #elif defined(MP_16BIT) 64 #elif defined(MP_16BIT)
57 typedef uint16_t mp_digit; 65 typedef uint16_t mp_digit;
58 typedef uint32_t mp_word; 66 typedef uint32_t mp_word;
59 #define MP_SIZEOF_MP_DIGIT 2 67 # define MP_SIZEOF_MP_DIGIT 2
60 #ifdef DIGIT_BIT 68 # ifdef DIGIT_BIT
61 #error You must not define DIGIT_BIT when using MP_16BIT 69 # error You must not define DIGIT_BIT when using MP_16BIT
62 #endif 70 # endif
63 #elif defined(MP_64BIT) 71 #elif defined(MP_64BIT)
64 /* for GCC only on supported platforms */ 72 /* for GCC only on supported platforms */
65 typedef uint64_t mp_digit; 73 typedef uint64_t mp_digit;
66 #if defined(_WIN32) 74 typedef unsigned long mp_word __attribute__((mode(TI)));
67 typedef unsigned __int128 mp_word; 75 # define DIGIT_BIT 60
68 #elif defined(__GNUC__)
69 typedef unsigned long mp_word __attribute__ ((mode(TI)));
70 #else 76 #else
71 /* it seems you have a problem 77 /* this is the default case, 28-bit digits */
72 * but we assume you can somewhere define your own uint128_t */ 78
73 typedef uint128_t mp_word; 79 /* this is to make porting into LibTomCrypt easier :-) */
74 #endif 80 typedef uint32_t mp_digit;
75 81 typedef uint64_t mp_word;
76 #define DIGIT_BIT 60 82
77 #else 83 # ifdef MP_31BIT
78 /* this is the default case, 28-bit digits */ 84 /* this is an extension that uses 31-bit digits */
79 85 # define DIGIT_BIT 31
80 /* this is to make porting into LibTomCrypt easier :-) */ 86 # else
81 typedef uint32_t mp_digit; 87 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
82 typedef uint64_t mp_word; 88 # define DIGIT_BIT 28
83 89 # define MP_28BIT
84 #ifdef MP_31BIT 90 # endif
85 /* this is an extension that uses 31-bit digits */
86 #define DIGIT_BIT 31
87 #else
88 /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
89 #define DIGIT_BIT 28
90 #define MP_28BIT
91 #endif
92 #endif 91 #endif
93 92
94 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */ 93 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
95 #ifndef DIGIT_BIT 94 #ifndef DIGIT_BIT
96 #define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */ 95 # define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */
97 typedef uint_least32_t mp_min_u32; 96 typedef uint_least32_t mp_min_u32;
98 #else 97 #else
99 typedef mp_digit mp_min_u32; 98 typedef mp_digit mp_min_u32;
100 #endif
101
102 /* use arc4random on platforms that support it */
103 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
104 #define MP_GEN_RANDOM() arc4random()
105 #define MP_GEN_RANDOM_MAX 0xffffffff
106 #endif
107
108 /* use rand() as fall-back if there's no better rand function */
109 #ifndef MP_GEN_RANDOM
110 #define MP_GEN_RANDOM() rand()
111 #define MP_GEN_RANDOM_MAX RAND_MAX
112 #endif 99 #endif
113 100
114 #define MP_DIGIT_BIT DIGIT_BIT 101 #define MP_DIGIT_BIT DIGIT_BIT
115 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1)) 102 #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
116 #define MP_DIGIT_MAX MP_MASK 103 #define MP_DIGIT_MAX MP_MASK
125 112
126 #define MP_OKAY 0 /* ok result */ 113 #define MP_OKAY 0 /* ok result */
127 #define MP_MEM -2 /* out of mem */ 114 #define MP_MEM -2 /* out of mem */
128 #define MP_VAL -3 /* invalid input */ 115 #define MP_VAL -3 /* invalid input */
129 #define MP_RANGE MP_VAL 116 #define MP_RANGE MP_VAL
117 #define MP_ITER -4 /* Max. iterations reached */
130 118
131 #define MP_YES 1 /* yes response */ 119 #define MP_YES 1 /* yes response */
132 #define MP_NO 0 /* no response */ 120 #define MP_NO 0 /* no response */
133 121
134 /* Primality generation flags */ 122 /* Primality generation flags */
138 126
139 typedef int mp_err; 127 typedef int mp_err;
140 128
141 /* you'll have to tune these... */ 129 /* you'll have to tune these... */
142 extern int KARATSUBA_MUL_CUTOFF, 130 extern int KARATSUBA_MUL_CUTOFF,
143 KARATSUBA_SQR_CUTOFF, 131 KARATSUBA_SQR_CUTOFF,
144 TOOM_MUL_CUTOFF, 132 TOOM_MUL_CUTOFF,
145 TOOM_SQR_CUTOFF; 133 TOOM_SQR_CUTOFF;
146 134
147 /* define this to use lower memory usage routines (exptmods mostly) */ 135 /* define this to use lower memory usage routines (exptmods mostly) */
148 /* #define MP_LOW_MEM */ 136 /* #define MP_LOW_MEM */
149 137
150 /* default precision */ 138 /* default precision */
151 #ifndef MP_PREC 139 #ifndef MP_PREC
152 #ifndef MP_LOW_MEM 140 # ifndef MP_LOW_MEM
153 #define MP_PREC 32 /* default digits of precision */ 141 # define MP_PREC 32 /* default digits of precision */
154 #else 142 # else
155 #define MP_PREC 8 /* default digits of precision */ 143 # define MP_PREC 8 /* default digits of precision */
156 #endif 144 # endif
157 #endif 145 #endif
158 146
159 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ 147 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
160 #define MP_WARRAY (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1)) 148 #define MP_WARRAY (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
161 149
162 /* the infamous mp_int structure */ 150 /* the infamous mp_int structure */
163 typedef struct { 151 typedef struct {
164 int used, alloc, sign; 152 int used, alloc, sign;
165 mp_digit *dp; 153 mp_digit *dp;
166 } mp_int; 154 } mp_int;
167 155
168 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */ 156 /* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
169 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat); 157 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
170 158
171 159
172 #define USED(m) ((m)->used) 160 #define USED(m) ((m)->used)
173 #define DIGIT(m,k) ((m)->dp[(k)]) 161 #define DIGIT(m, k) ((m)->dp[(k)])
174 #define SIGN(m) ((m)->sign) 162 #define SIGN(m) ((m)->sign)
175 163
176 /* error code to char* string */ 164 /* error code to char* string */
177 const char *mp_error_to_string(int code); 165 const char *mp_error_to_string(int code);
178 166
179 /* ---> init and deinit bignum functions <--- */ 167 /* ---> init and deinit bignum functions <--- */
201 /* init to a given number of digits */ 189 /* init to a given number of digits */
202 int mp_init_size(mp_int *a, int size); 190 int mp_init_size(mp_int *a, int size);
203 191
204 /* ---> Basic Manipulations <--- */ 192 /* ---> Basic Manipulations <--- */
205 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) 193 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
206 #define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO) 194 #define mp_iseven(a) ((((a)->used == 0) || (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
207 #define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO) 195 #define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
208 #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) 196 #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
209 197
210 /* set to zero */ 198 /* set to zero */
211 void mp_zero(mp_int *a); 199 void mp_zero(mp_int *a);
221 209
222 /* set a platform dependent unsigned long long value */ 210 /* set a platform dependent unsigned long long value */
223 int mp_set_long_long(mp_int *a, unsigned long long b); 211 int mp_set_long_long(mp_int *a, unsigned long long b);
224 212
225 /* get a 32-bit value */ 213 /* get a 32-bit value */
226 unsigned long mp_get_int(mp_int * a); 214 unsigned long mp_get_int(const mp_int *a);
227 215
228 /* get a platform dependent unsigned long value */ 216 /* get a platform dependent unsigned long value */
229 unsigned long mp_get_long(mp_int * a); 217 unsigned long mp_get_long(const mp_int *a);
230 218
231 /* get a platform dependent unsigned long long value */ 219 /* get a platform dependent unsigned long long value */
232 unsigned long long mp_get_long_long(mp_int * a); 220 unsigned long long mp_get_long_long(const mp_int *a);
233 221
234 /* initialize and set a digit */ 222 /* initialize and set a digit */
235 int mp_init_set (mp_int * a, mp_digit b); 223 int mp_init_set(mp_int *a, mp_digit b);
236 224
237 /* initialize and set 32-bit value */ 225 /* initialize and set 32-bit value */
238 int mp_init_set_int (mp_int * a, unsigned long b); 226 int mp_init_set_int(mp_int *a, unsigned long b);
239 227
240 /* copy, b = a */ 228 /* copy, b = a */
241 int mp_copy(mp_int *a, mp_int *b); 229 int mp_copy(const mp_int *a, mp_int *b);
242 230
243 /* inits and copies, a = b */ 231 /* inits and copies, a = b */
244 int mp_init_copy(mp_int *a, mp_int *b); 232 int mp_init_copy(mp_int *a, const mp_int *b);
245 233
246 /* trim unused digits */ 234 /* trim unused digits */
247 void mp_clamp(mp_int *a); 235 void mp_clamp(mp_int *a);
248 236
249 /* import binary data */ 237 /* import binary data */
250 int mp_import(mp_int* rop, size_t count, int order, size_t size, int endian, size_t nails, const void* op); 238 int mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails, const void *op);
251 239
252 /* export binary data */ 240 /* export binary data */
253 int mp_export(void* rop, size_t* countp, int order, size_t size, int endian, size_t nails, mp_int* op); 241 int mp_export(void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, const mp_int *op);
254 242
255 /* ---> digit manipulation <--- */ 243 /* ---> digit manipulation <--- */
256 244
257 /* right shift by "b" digits */ 245 /* right shift by "b" digits */
258 void mp_rshd(mp_int *a, int b); 246 void mp_rshd(mp_int *a, int b);
259 247
260 /* left shift by "b" digits */ 248 /* left shift by "b" digits */
261 int mp_lshd(mp_int *a, int b); 249 int mp_lshd(mp_int *a, int b);
262 250
263 /* c = a / 2**b, implemented as c = a >> b */ 251 /* c = a / 2**b, implemented as c = a >> b */
264 int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d); 252 int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d);
265 253
266 /* b = a/2 */ 254 /* b = a/2 */
267 int mp_div_2(mp_int *a, mp_int *b); 255 int mp_div_2(const mp_int *a, mp_int *b);
268 256
269 /* c = a * 2**b, implemented as c = a << b */ 257 /* c = a * 2**b, implemented as c = a << b */
270 int mp_mul_2d(mp_int *a, int b, mp_int *c); 258 int mp_mul_2d(const mp_int *a, int b, mp_int *c);
271 259
272 /* b = a*2 */ 260 /* b = a*2 */
273 int mp_mul_2(mp_int *a, mp_int *b); 261 int mp_mul_2(const mp_int *a, mp_int *b);
274 262
275 /* c = a mod 2**b */ 263 /* c = a mod 2**b */
276 int mp_mod_2d(mp_int *a, int b, mp_int *c); 264 int mp_mod_2d(const mp_int *a, int b, mp_int *c);
277 265
278 /* computes a = 2**b */ 266 /* computes a = 2**b */
279 int mp_2expt(mp_int *a, int b); 267 int mp_2expt(mp_int *a, int b);
280 268
281 /* Counts the number of lsbs which are zero before the first zero bit */ 269 /* Counts the number of lsbs which are zero before the first zero bit */
282 int mp_cnt_lsb(mp_int *a); 270 int mp_cnt_lsb(const mp_int *a);
283 271
284 /* I Love Earth! */ 272 /* I Love Earth! */
285 273
286 /* makes a pseudo-random int of a given size */ 274 /* makes a pseudo-random mp_int of a given size */
287 int mp_rand(mp_int *a, int digits); 275 int mp_rand(mp_int *a, int digits);
276 /* makes a pseudo-random small int of a given size */
277 int mp_rand_digit(mp_digit *r);
278
279 #ifdef MP_PRNG_ENABLE_LTM_RNG
280 /* A last resort to provide random data on systems without any of the other
281 * implemented ways to gather entropy.
282 * It is compatible with `rng_get_bytes()` from libtomcrypt so you could
283 * provide that one and then set `ltm_rng = rng_get_bytes;` */
284 extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
285 extern void (*ltm_rng_callback)(void);
286 #endif
288 287
289 /* ---> binary operations <--- */ 288 /* ---> binary operations <--- */
290 /* c = a XOR b */ 289 /* c = a XOR b */
291 int mp_xor(mp_int *a, mp_int *b, mp_int *c); 290 int mp_xor(const mp_int *a, const mp_int *b, mp_int *c);
292 291
293 /* c = a OR b */ 292 /* c = a OR b */
294 int mp_or(mp_int *a, mp_int *b, mp_int *c); 293 int mp_or(const mp_int *a, const mp_int *b, mp_int *c);
295 294
296 /* c = a AND b */ 295 /* c = a AND b */
297 int mp_and(mp_int *a, mp_int *b, mp_int *c); 296 int mp_and(const mp_int *a, const mp_int *b, mp_int *c);
297
298 /* Checks the bit at position b and returns MP_YES
299 if the bit is 1, MP_NO if it is 0 and MP_VAL
300 in case of error */
301 int mp_get_bit(const mp_int *a, int b);
302
303 /* c = a XOR b (two complement) */
304 int mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c);
305
306 /* c = a OR b (two complement) */
307 int mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c);
308
309 /* c = a AND b (two complement) */
310 int mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c);
311
312 /* right shift (two complement) */
313 int mp_tc_div_2d(const mp_int *a, int b, mp_int *c);
298 314
299 /* ---> Basic arithmetic <--- */ 315 /* ---> Basic arithmetic <--- */
300 316
317 /* b = ~a */
318 int mp_complement(const mp_int *a, mp_int *b);
319
301 /* b = -a */ 320 /* b = -a */
302 int mp_neg(mp_int *a, mp_int *b); 321 int mp_neg(const mp_int *a, mp_int *b);
303 322
304 /* b = |a| */ 323 /* b = |a| */
305 int mp_abs(mp_int *a, mp_int *b); 324 int mp_abs(const mp_int *a, mp_int *b);
306 325
307 /* compare a to b */ 326 /* compare a to b */
308 int mp_cmp(mp_int *a, mp_int *b); 327 int mp_cmp(const mp_int *a, const mp_int *b);
309 328
310 /* compare |a| to |b| */ 329 /* compare |a| to |b| */
311 int mp_cmp_mag(mp_int *a, mp_int *b); 330 int mp_cmp_mag(const mp_int *a, const mp_int *b);
312 331
313 /* c = a + b */ 332 /* c = a + b */
314 int mp_add(mp_int *a, mp_int *b, mp_int *c); 333 int mp_add(const mp_int *a, const mp_int *b, mp_int *c);
315 334
316 /* c = a - b */ 335 /* c = a - b */
317 int mp_sub(mp_int *a, mp_int *b, mp_int *c); 336 int mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
318 337
319 /* c = a * b */ 338 /* c = a * b */
320 int mp_mul(mp_int *a, mp_int *b, mp_int *c); 339 int mp_mul(const mp_int *a, const mp_int *b, mp_int *c);
321 340
322 /* b = a*a */ 341 /* b = a*a */
323 int mp_sqr(mp_int *a, mp_int *b); 342 int mp_sqr(const mp_int *a, mp_int *b);
324 343
325 /* a/b => cb + d == a */ 344 /* a/b => cb + d == a */
326 int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 345 int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
327 346
328 /* c = a mod b, 0 <= c < b */ 347 /* c = a mod b, 0 <= c < b */
329 int mp_mod(mp_int *a, mp_int *b, mp_int *c); 348 int mp_mod(const mp_int *a, const mp_int *b, mp_int *c);
330 349
331 /* ---> single digit functions <--- */ 350 /* ---> single digit functions <--- */
332 351
333 /* compare against a single digit */ 352 /* compare against a single digit */
334 int mp_cmp_d(mp_int *a, mp_digit b); 353 int mp_cmp_d(const mp_int *a, mp_digit b);
335 354
336 /* c = a + b */ 355 /* c = a + b */
337 int mp_add_d(mp_int *a, mp_digit b, mp_int *c); 356 int mp_add_d(const mp_int *a, mp_digit b, mp_int *c);
338 357
339 /* c = a - b */ 358 /* c = a - b */
340 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c); 359 int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c);
341 360
342 /* c = a * b */ 361 /* c = a * b */
343 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c); 362 int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c);
344 363
345 /* a/b => cb + d == a */ 364 /* a/b => cb + d == a */
346 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d); 365 int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
347 366
348 /* a/3 => 3c + d == a */ 367 /* a/3 => 3c + d == a */
349 int mp_div_3(mp_int *a, mp_int *c, mp_digit *d); 368 int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d);
350 369
351 /* c = a**b */ 370 /* c = a**b */
352 int mp_expt_d(mp_int *a, mp_digit b, mp_int *c); 371 int mp_expt_d(const mp_int *a, mp_digit b, mp_int *c);
353 int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast); 372 int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
354 373
355 /* c = a mod b, 0 <= c < b */ 374 /* c = a mod b, 0 <= c < b */
356 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c); 375 int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c);
357 376
358 /* ---> number theory <--- */ 377 /* ---> number theory <--- */
359 378
360 /* d = a + b (mod c) */ 379 /* d = a + b (mod c) */
361 int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 380 int mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
362 381
363 /* d = a - b (mod c) */ 382 /* d = a - b (mod c) */
364 int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 383 int mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
365 384
366 /* d = a * b (mod c) */ 385 /* d = a * b (mod c) */
367 int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 386 int mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d);
368 387
369 /* c = a * a (mod b) */ 388 /* c = a * a (mod b) */
370 int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c); 389 int mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c);
371 390
372 /* c = 1/a (mod b) */ 391 /* c = 1/a (mod b) */
373 int mp_invmod(mp_int *a, mp_int *b, mp_int *c); 392 int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
374 393
375 /* c = (a, b) */ 394 /* c = (a, b) */
376 int mp_gcd(mp_int *a, mp_int *b, mp_int *c); 395 int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c);
377 396
378 /* produces value such that U1*a + U2*b = U3 */ 397 /* produces value such that U1*a + U2*b = U3 */
379 int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3); 398 int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
380 399
381 /* c = [a, b] or (a*b)/(a, b) */ 400 /* c = [a, b] or (a*b)/(a, b) */
382 int mp_lcm(mp_int *a, mp_int *b, mp_int *c); 401 int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c);
383 402
384 /* finds one of the b'th root of a, such that |c|**b <= |a| 403 /* finds one of the b'th root of a, such that |c|**b <= |a|
385 * 404 *
386 * returns error if a < 0 and b is even 405 * returns error if a < 0 and b is even
387 */ 406 */
388 int mp_n_root(mp_int *a, mp_digit b, mp_int *c); 407 int mp_n_root(const mp_int *a, mp_digit b, mp_int *c);
389 int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast); 408 int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast);
390 409
391 /* special sqrt algo */ 410 /* special sqrt algo */
392 int mp_sqrt(mp_int *arg, mp_int *ret); 411 int mp_sqrt(const mp_int *arg, mp_int *ret);
393 412
394 /* special sqrt (mod prime) */ 413 /* special sqrt (mod prime) */
395 int mp_sqrtmod_prime(mp_int *arg, mp_int *prime, mp_int *ret); 414 int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret);
396 415
397 /* is number a square? */ 416 /* is number a square? */
398 int mp_is_square(mp_int *arg, int *ret); 417 int mp_is_square(const mp_int *arg, int *ret);
399 418
400 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */ 419 /* computes the jacobi c = (a | n) (or Legendre if b is prime) */
401 int mp_jacobi(mp_int *a, mp_int *n, int *c); 420 int mp_jacobi(const mp_int *a, const mp_int *n, int *c);
421
422 /* computes the Kronecker symbol c = (a | p) (like jacobi() but with {a,p} in Z */
423 int mp_kronecker(const mp_int *a, const mp_int *p, int *c);
402 424
403 /* used to setup the Barrett reduction for a given modulus b */ 425 /* used to setup the Barrett reduction for a given modulus b */
404 int mp_reduce_setup(mp_int *a, mp_int *b); 426 int mp_reduce_setup(mp_int *a, const mp_int *b);
405 427
406 /* Barrett Reduction, computes a (mod b) with a precomputed value c 428 /* Barrett Reduction, computes a (mod b) with a precomputed value c
407 * 429 *
408 * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely 430 * Assumes that 0 < x <= m*m, note if 0 > x > -(m*m) then you can merely
409 * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code]. 431 * compute the reduction as -1 * mp_reduce(mp_abs(x)) [pseudo code].
410 */ 432 */
411 int mp_reduce(mp_int *a, mp_int *b, mp_int *c); 433 int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu);
412 434
413 /* setups the montgomery reduction */ 435 /* setups the montgomery reduction */
414 int mp_montgomery_setup(mp_int *a, mp_digit *mp); 436 int mp_montgomery_setup(const mp_int *n, mp_digit *rho);
415 437
416 /* computes a = B**n mod b without division or multiplication useful for 438 /* computes a = B**n mod b without division or multiplication useful for
417 * normalizing numbers in a Montgomery system. 439 * normalizing numbers in a Montgomery system.
418 */ 440 */
419 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b); 441 int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b);
420 442
421 /* computes x/R == x (mod N) via Montgomery Reduction */ 443 /* computes x/R == x (mod N) via Montgomery Reduction */
422 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); 444 int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho);
423 445
424 /* returns 1 if a is a valid DR modulus */ 446 /* returns 1 if a is a valid DR modulus */
425 int mp_dr_is_modulus(mp_int *a); 447 int mp_dr_is_modulus(const mp_int *a);
426 448
427 /* sets the value of "d" required for mp_dr_reduce */ 449 /* sets the value of "d" required for mp_dr_reduce */
428 void mp_dr_setup(mp_int *a, mp_digit *d); 450 void mp_dr_setup(const mp_int *a, mp_digit *d);
429 451
430 /* reduces a modulo b using the Diminished Radix method */ 452 /* reduces a modulo n using the Diminished Radix method */
431 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp); 453 int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k);
432 454
433 /* returns true if a can be reduced with mp_reduce_2k */ 455 /* returns true if a can be reduced with mp_reduce_2k */
434 int mp_reduce_is_2k(mp_int *a); 456 int mp_reduce_is_2k(const mp_int *a);
435 457
436 /* determines k value for 2k reduction */ 458 /* determines k value for 2k reduction */
437 int mp_reduce_2k_setup(mp_int *a, mp_digit *d); 459 int mp_reduce_2k_setup(const mp_int *a, mp_digit *d);
438 460
439 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ 461 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
440 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d); 462 int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d);
441 463
442 /* returns true if a can be reduced with mp_reduce_2k_l */ 464 /* returns true if a can be reduced with mp_reduce_2k_l */
443 int mp_reduce_is_2k_l(mp_int *a); 465 int mp_reduce_is_2k_l(const mp_int *a);
444 466
445 /* determines k value for 2k reduction */ 467 /* determines k value for 2k reduction */
446 int mp_reduce_2k_setup_l(mp_int *a, mp_int *d); 468 int mp_reduce_2k_setup_l(const mp_int *a, mp_int *d);
447 469
448 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */ 470 /* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
449 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d); 471 int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d);
450 472
451 /* d = a**b (mod c) */ 473 /* Y = G**X (mod P) */
452 int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); 474 int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y);
453 475
454 /* ---> Primes <--- */ 476 /* ---> Primes <--- */
455 477
456 /* number of primes */ 478 /* number of primes */
457 #ifdef MP_8BIT 479 #ifdef MP_8BIT
458 #define PRIME_SIZE 31 480 # define PRIME_SIZE 31
459 #else 481 #else
460 #define PRIME_SIZE 256 482 # define PRIME_SIZE 256
461 #endif 483 #endif
462 484
463 /* table of first PRIME_SIZE primes */ 485 /* table of first PRIME_SIZE primes */
464 extern const mp_digit ltm_prime_tab[PRIME_SIZE]; 486 extern const mp_digit ltm_prime_tab[PRIME_SIZE];
465 487
466 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ 488 /* result=1 if a is divisible by one of the first PRIME_SIZE primes */
467 int mp_prime_is_divisible(mp_int *a, int *result); 489 int mp_prime_is_divisible(const mp_int *a, int *result);
468 490
469 /* performs one Fermat test of "a" using base "b". 491 /* performs one Fermat test of "a" using base "b".
470 * Sets result to 0 if composite or 1 if probable prime 492 * Sets result to 0 if composite or 1 if probable prime
471 */ 493 */
472 int mp_prime_fermat(mp_int *a, mp_int *b, int *result); 494 int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result);
473 495
474 /* performs one Miller-Rabin test of "a" using base "b". 496 /* performs one Miller-Rabin test of "a" using base "b".
475 * Sets result to 0 if composite or 1 if probable prime 497 * Sets result to 0 if composite or 1 if probable prime
476 */ 498 */
477 int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result); 499 int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result);
478 500
479 /* This gives [for a given bit size] the number of trials required 501 /* This gives [for a given bit size] the number of trials required
480 * such that Miller-Rabin gives a prob of failure lower than 2^-96 502 * such that Miller-Rabin gives a prob of failure lower than 2^-96
481 */ 503 */
482 int mp_prime_rabin_miller_trials(int size); 504 int mp_prime_rabin_miller_trials(int size);
483 505
484 /* performs t rounds of Miller-Rabin on "a" using the first 506 /* performs one strong Lucas-Selfridge test of "a".
485 * t prime bases. Also performs an initial sieve of trial 507 * Sets result to 0 if composite or 1 if probable prime
508 */
509 int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result);
510
511 /* performs one Frobenius test of "a" as described by Paul Underwood.
512 * Sets result to 0 if composite or 1 if probable prime
513 */
514 int mp_prime_frobenius_underwood(const mp_int *N, int *result);
515
516 /* performs t random rounds of Miller-Rabin on "a" additional to
517 * bases 2 and 3. Also performs an initial sieve of trial
486 * division. Determines if "a" is prime with probability 518 * division. Determines if "a" is prime with probability
487 * of error no more than (1/4)**t. 519 * of error no more than (1/4)**t.
520 * Both a strong Lucas-Selfridge to complete the BPSW test
521 * and a separate Frobenius test are available at compile time.
522 * With t<0 a deterministic test is run for primes up to
523 * 318665857834031151167461. With t<13 (abs(t)-13) additional
524 * tests with sequential small primes are run starting at 43.
525 * Is Fips 186.4 compliant if called with t as computed by
526 * mp_prime_rabin_miller_trials();
488 * 527 *
489 * Sets result to 1 if probably prime, 0 otherwise 528 * Sets result to 1 if probably prime, 0 otherwise
490 */ 529 */
491 int mp_prime_is_prime(mp_int *a, int t, int *result); 530 int mp_prime_is_prime(const mp_int *a, int t, int *result);
492 531
493 /* finds the next prime after the number "a" using "t" trials 532 /* finds the next prime after the number "a" using "t" trials
494 * of Miller-Rabin. 533 * of Miller-Rabin.
495 * 534 *
496 * bbs_style = 1 means the prime must be congruent to 3 mod 4 535 * bbs_style = 1 means the prime must be congruent to 3 mod 4
522 * 561 *
523 */ 562 */
524 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat); 563 int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
525 564
526 /* ---> radix conversion <--- */ 565 /* ---> radix conversion <--- */
527 int mp_count_bits(mp_int *a); 566 int mp_count_bits(const mp_int *a);
528 567
529 int mp_unsigned_bin_size(mp_int *a); 568 int mp_unsigned_bin_size(const mp_int *a);
530 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c); 569 int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
531 int mp_to_unsigned_bin(mp_int *a, unsigned char *b); 570 int mp_to_unsigned_bin(const mp_int *a, unsigned char *b);
532 int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); 571 int mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen);
533 572
534 int mp_signed_bin_size(mp_int *a); 573 int mp_signed_bin_size(const mp_int *a);
535 int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c); 574 int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
536 int mp_to_signed_bin(mp_int *a, unsigned char *b); 575 int mp_to_signed_bin(const mp_int *a, unsigned char *b);
537 int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen); 576 int mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen);
538 577
539 int mp_read_radix(mp_int *a, const char *str, int radix); 578 int mp_read_radix(mp_int *a, const char *str, int radix);
540 int mp_toradix(mp_int *a, char *str, int radix); 579 int mp_toradix(const mp_int *a, char *str, int radix);
541 int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen); 580 int mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen);
542 int mp_radix_size(mp_int *a, int radix, int *size); 581 int mp_radix_size(const mp_int *a, int radix, int *size);
543 582
544 #ifndef LTM_NO_FILE 583 #ifndef LTM_NO_FILE
545 int mp_fread(mp_int *a, int radix, FILE *stream); 584 int mp_fread(mp_int *a, int radix, FILE *stream);
546 int mp_fwrite(mp_int *a, int radix, FILE *stream); 585 int mp_fwrite(const mp_int *a, int radix, FILE *stream);
547 #endif 586 #endif
548 587
549 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len)) 588 #define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
550 #define mp_raw_size(mp) mp_signed_bin_size(mp) 589 #define mp_raw_size(mp) mp_signed_bin_size(mp)
551 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str)) 590 #define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
557 #define mp_tooctal(M, S) mp_toradix((M), (S), 8) 596 #define mp_tooctal(M, S) mp_toradix((M), (S), 8)
558 #define mp_todecimal(M, S) mp_toradix((M), (S), 10) 597 #define mp_todecimal(M, S) mp_toradix((M), (S), 10)
559 #define mp_tohex(M, S) mp_toradix((M), (S), 16) 598 #define mp_tohex(M, S) mp_toradix((M), (S), 16)
560 599
561 #ifdef __cplusplus 600 #ifdef __cplusplus
562 } 601 }
563 #endif 602 #endif
564 603
565 #endif 604 #endif
566 605
567 606
568 /* ref: $Format:%D$ */ 607 /* ref: HEAD -> master, tag: v1.1.0 */
569 /* git commit: $Format:%H$ */ 608 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
570 /* commit time: $Format:%ai$ */ 609 /* commit time: 2019-01-28 20:32:32 +0100 */