aboutsummaryrefslogtreecommitdiffstats
path: root/bench/libtommath.h
blob: 3529118c6a7612591fe31709e9760d0a75598304 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <tommath.h>

#include <setjmp.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#define BIGINT_LIBRARY "libtommath"

#define FAST_RANDOM        0
#define SECURE_RANDOM      0
#define DEFAULT_RANDOM     0
#define FASTEST_RANDOM     0
#define LIBC_RAND_RANDOM   0
#define LIBC_RANDOM_RANDOM 0
#define LIBC_RAND48_RANDOM 0
#define QUASIUNIFORM       0
#define UNIFORM            1
#define MODUNIFORM         2

typedef mp_int z_t[1];

static z_t _0, _1, _a, _b;
static int _tmp, error;
static jmp_buf jbuf;

#ifdef UNSAFE
# define try(expr) (expr)
#else
# define try(expr) do if ((error = (expr))) longjmp(jbuf, 1); while (0)
#endif

static inline void
zsetup(jmp_buf env)
{
	*jbuf = *env;
	try(mp_init_set_int(_0, 0));
	try(mp_init_set_int(_1, 1));
	try(mp_init(_a));
	try(mp_init(_b));
}

static inline void
zunsetup(void)
{
	mp_clear(_0);
	mp_clear(_1);
	mp_clear(_a);
	mp_clear(_b);
}

static inline void
zperror(const char *str)
{
	if (str && *str)
		fprintf(stderr, "%s: %s\n", str, mp_error_to_string(error));
	else
		fprintf(stderr, "%s\n", mp_error_to_string(error));
}

static inline void
zinit(z_t a)
{
	try(mp_init(a));
}

static inline void
zfree(z_t a)
{
	mp_clear(a);
}

static inline void
zset(z_t r, z_t a)
{
	try(mp_copy(a, r));
}

static inline void
zneg(z_t r, z_t a)
{
	try(mp_neg(a, r));
}

static inline void
zabs(z_t r, z_t a)
{
	try(mp_abs(a, r));
}

static inline void
zadd(z_t r, z_t a, z_t b)
{
	try(mp_add(a, b, r));
}

static inline void
zsub(z_t r, z_t a, z_t b)
{
	try(mp_sub(a, b, r));
}

static inline void
zadd_unsigned(z_t r, z_t a, z_t b)
{
	zabs(_a, a);
	zabs(_b, b);
	zadd(r, _a, _b);
}

static inline void
zsub_unsigned(z_t r, z_t a, z_t b)
{
	zabs(_a, a);
	zabs(_b, b);
	zsub(r, _a, _b);
}

static inline size_t
zbits(z_t a)
{
	return mp_count_bits(a);
}

static inline size_t
zlsb(z_t a)
{
	return mp_cnt_lsb(a);
}

static inline int
zeven(z_t a)
{
	return mp_iseven(a);
}

static inline int
zodd(z_t a)
{
	return mp_isodd(a);
}

static inline int
zeven_nonzero(z_t a)
{
	return zeven(a);
}

static inline int
zodd_nonzero(z_t a)
{
	return zodd(a);
}

static inline int
zzero(z_t a)
{
	return mp_iszero(a);
}

static inline void
zand(z_t r, z_t a, z_t b)
{
	try(mp_and(a, b, r));
}

static inline void
zor(z_t r, z_t a, z_t b)
{
	try(mp_or(a, b, r));
}

static inline void
zxor(z_t r, z_t a, z_t b)
{
	try(mp_xor(a, b, r));
}

static inline void
znot(z_t r, z_t a)
{
	try(mp_2expt(_a, (int)zbits(a)));
	try(mp_sub_d(_a, 1, _a));
	zand(r, a, _a);
	zneg(r, r);
}

static inline int
zbtest(z_t a, size_t bit)
{
	try(mp_2expt(_b, (int)bit));
	zand(_b, a, _b);
	return !zzero(_b);
}

static inline void
zbset(z_t r, z_t a, size_t bit, int mode)
{
	if (mode > 0) {
		try(mp_2expt(_b, (int)bit));
		zor(r, a, _b);
	} else if (mode < 0 || zbtest(a, bit)) {
		try(mp_2expt(_b, (int)bit));
		zxor(r, a, _b);
	}
}

static inline void
zswap(z_t a, z_t b)
{
	mp_exch(a, b);
}

static inline void
zlsh(z_t r, z_t a, size_t b)
{
	try(mp_mul_2d(a, (int)b, r));
}

static inline void
zrsh(z_t r, z_t a, size_t b)
{
	try(mp_div_2d(a, (int)b, r, 0));
}

static inline void
ztrunc(z_t r, z_t a, size_t b)
{
	try(mp_mod_2d(a, (int)b, r));
}

static inline void
zsplit(z_t high, z_t low, z_t a, size_t brk)
{
	if (low == a) {
		zrsh(high, a, brk);
		ztrunc(low, a, brk);
	} else {
		ztrunc(low, a, brk);
		zrsh(high, a, brk);
	}
}

static inline void
zsetu(z_t r, unsigned long long int val)
{
	try(mp_set_long_long(r, val));
}

static inline void
zseti(z_t r, long long int val)
{
	if (val >= 0) {
		zsetu(r, (unsigned long long int)val);
	} else {
		zsetu(r, (unsigned long long int)-val);
		zneg(r, r);
	}
}

static inline int
zcmpmag(z_t a, z_t b)
{
	return mp_cmp_mag(a, b);
}

static inline int
zcmp(z_t a, z_t b)
{
	return mp_cmp(a, b);
}

static inline int
zcmpi(z_t a, long long int b)
{
	zseti(_b, b);
	return zcmp(a, _b);
}

static inline int
zcmpu(z_t a, unsigned long long int b)
{
	zsetu(_b, b);
	return zcmp(a, _b);
}

static inline int
zsignum(z_t a)
{
	return zcmp(a, _0);
}

static inline void
zgcd(z_t r, z_t a, z_t b)
{
	try(mp_gcd(a, b, r));
}

static inline void
zmul(z_t r, z_t a, z_t b)
{
	try(mp_mul(a, b, r));
}

static inline void
zsqr(z_t r, z_t a)
{
	try(mp_sqr(a, r));
}

static inline void
zmodmul(z_t r, z_t a, z_t b, z_t m)
{
	try(mp_mulmod(a, b, m, r));
}

static inline void
zmodsqr(z_t r, z_t a, z_t m)
{
	try(mp_sqrmod(a, m, r));
}

static inline void
zpow(z_t r, z_t a, z_t b)
{
	try(mp_expt_d(a, (mp_digit)mp_get_int(b), r));
}

static inline void
zpowu(z_t r, z_t a, unsigned long long int b)
{
	try(mp_expt_d(a, (mp_digit)b, r));
}

static inline void
zmodpow(z_t r, z_t a, z_t b, z_t m)
{
	try(mp_exptmod(a, b, m, r));
}

static inline void
zmodpowu(z_t r, z_t a, unsigned long long int b, z_t m)
{
	try(mp_set_int(_b, b));
	try(mp_exptmod(a, _b, m, r));
}

static inline void
zsets(z_t a, const char *s)
{
	try(mp_read_radix(a, s, 10));
}

static inline size_t
zstr_length(z_t a, size_t b)
{
	try(mp_radix_size(a, b, &_tmp));
	return _tmp;
}

static inline char *
zstr(z_t a, char *s, size_t n)
{
	try(mp_toradix(a, s, 10));
	return s;
	(void) n;
}

static inline int
zptest(z_t w, z_t a, int t)
{
	try(mp_prime_is_prime(a, t, &_tmp));
	return _tmp;
	(void) w; /* Note, the witness is not returned. */
}

static inline size_t
zsave(z_t a, char *b)
{
	_tmp = b ? mp_signed_bin_size(a) : mp_to_signed_bin(a, (unsigned char *)b);
	return _tmp;
}

static inline size_t
zload(z_t a, const char *b) /* Note, requires that zsave was called directly prior. */
{
	return mp_read_signed_bin(a, (const unsigned char *)b, _tmp);
}

static inline void
zdiv(z_t r, z_t a, z_t b)
{
	try(mp_div(a, b, r, 0));
}

static inline void
zmod(z_t r, z_t a, z_t b)
{
	try(mp_mod(a, b, r));
}

static inline void
zdivmod(z_t q, z_t r, z_t a, z_t b)
{
	try(mp_div(a, b, q, r));
}

static inline void
zrand(z_t r, int dev, int dist, z_t n)
{
	static int gave_up = 0;
	int bits;
	(void) dev;

	if (zzero(n)) {
		mp_zero(r);
		return;
	}
	if (zsignum(n) < 0) {
		return;
	}

	bits = zbits(n);

	switch (dist) {
	case QUASIUNIFORM:
		try(mp_rand(r, bits));
		zadd(r, r, _1);
		zmul(r, r, n);
		zrsh(r, r, bits);
		break;

	case UNIFORM:
		if (!gave_up) {
			gave_up = 1;
			printf("I'm sorry, this is too difficult, I give up.\n");
		}
		break;

	case MODUNIFORM:
		try(mp_rand(r, bits));
		if (zcmp(r, n) > 0)
			zsub(r, r, n);
		break;

	default:
		abort();
	}
}