aboutsummaryrefslogtreecommitdiffstats
path: root/bench/libtfm.h
blob: b87a2889bb36634cd4cc154f6db2850124c47878 (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <tfm.h>

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

#define BIGINT_LIBRARY "TomsFastMath"

#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 fp_int z_t[1];

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

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

static inline void
fp_set_int(z_t a, uint32_t d)
{
	a->dp[0] = d;
	a->used = !!d;
	a->sign = 0;
}

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

static inline void
zunsetup(void)
{
	/* Do nothing */
}

static inline void
zperror(const char *str)
{
	const char *serr;
	switch (error) {
	case FP_VAL: serr = "FP_VAL";        break;
	case FP_MEM: serr = "FP_MEM";        break;
	default:     serr = "unknown error"; break;
	}
	if (str && *str)
		fprintf(stderr, "%s: %s\n", str, serr);
	else
		fprintf(stderr, "%s\n", serr);
}

static inline void
zinit(z_t a)
{
	fp_init(a);
}

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

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

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

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

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

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

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

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

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

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

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

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

static inline void
zlsh(z_t r, z_t a, size_t b)
{
	fp_mul_2d(a, b, r);
}

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

static inline void
ztrunc(z_t r, z_t a, size_t b)
{
	fp_mod_2d(a, b, r);
}

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

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

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

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

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

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

static inline size_t
zstr_length(z_t a, unsigned long long int b)
{
	try(fp_radix_size(a, b, &_tmp));
	return _tmp;
}

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

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

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

static inline void
zdiv(z_t q, z_t a, z_t b)
{
	zdivmod(q, 0, a, b);
}

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

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

static inline void
zabs(z_t r, z_t a)
{
	fp_abs(a, 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 void
zswap(z_t a, z_t b)
{
	z_t t;
	zset(t, a);
	zset(a, b);
	zset(b, t);
}

static inline void
zand(z_t r, z_t a, z_t b)
{
	int i;
	for (i = 0; i < a->used && i < b->used; i++)
		r->dp[i] = a->dp[i] & b->dp[i];
	r->used = i;
	while (r->used && !r->dp[r->used])
		r->used--;
	r->sign = a->sign & b->sign;
}

static inline void
zor(z_t r, z_t a, z_t b)
{
	int i;
	for (i = 0; i < a->used && i < b->used; i++)
		r->dp[i] = a->dp[i] | b->dp[i];
	for (; i < a->used; i++)
		r->dp[i] = a->dp[i];
	for (; i < b->used; i++)
		r->dp[i] = b->dp[i];
	r->used = i;
	r->sign = a->sign | b->sign;
}

static inline void
zxor(z_t r, z_t a, z_t b)
{
	int i;
	for (i = 0; i < a->used && i < b->used; i++)
		r->dp[i] = a->dp[i] ^ b->dp[i];
	for (; i < a->used; i++)
		r->dp[i] = a->dp[i];
	for (; i < b->used; i++)
		r->dp[i] = b->dp[i];
	r->used = i;
	while (r->used && !r->dp[r->used])
		r->used--;
	r->sign = a->sign ^ b->sign;
}

static inline size_t
zsave(z_t a, char *s)
{
	_tmp = s ? fp_signed_bin_size(a) : (fp_to_signed_bin(a, (unsigned char *)s), 0);
	return _tmp;
}

static inline size_t
zload(z_t a, const char *s)
{
	fp_read_signed_bin(a, (unsigned char *)s, _tmp);
	return _tmp;
}

static inline void
zsetu(z_t r, unsigned long long int val)
{
	uint32_t high = (uint32_t)(val >> 32);
	uint32_t low = (uint32_t)val;

	if (high) {
		fp_set_int(r, high);
		fp_set_int(_a, low);
		fp_lshd(r, 32);
		zadd(r, r, _a);
	} else {
		fp_set_int(r, low);
	}
	
}

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 fp_cmp_mag(a, b);
}

static inline int
zcmp(z_t a, z_t b)
{
	return fp_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 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
znot(z_t r, z_t a)
{
	fp_2expt(_a, zbits(a));
	fp_sub_d(_a, 1, _a);
	zand(r, a, _a);
	zneg(r, r);
}

static inline int
zbtest(z_t a, size_t bit)
{
	fp_2expt(_b, 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) {
		fp_2expt(_b, bit);
		zor(r, a, _b);
	} else if (mode < 0 || zbtest(a, bit)) {
		fp_2expt(_b, bit);
		zxor(r, a, _b);
	}
}

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)) {
		fp_zero(r);
		return;
	}
	if (zsignum(n) < 0) {
		return;
	}

	bits = zbits(n);

	switch (dist) {
	case QUASIUNIFORM:
		fp_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:
		fp_rand(r, bits);
		if (zcmp(r, n) > 0)
			zsub(r, r, n);
		break;

	default:
		abort();
	}
}

static inline void
zpowu(z_t r, z_t a, unsigned long long int b)
{
	if (!b) {
		if (zzero(a)) {
			error = FP_VAL;
			longjmp(jbuf, 1);
		}
		zsetu(r, 1);
		return;
	}
	zset(_a, a);
	zsetu(r, 1);
	for (; b; b >>= 1) {
		if (b & 1)
			zmul(r, r, _a);
		zsqr(_a, _a);
	}
}

static inline void
zpow(z_t r, z_t a, z_t b)
{
	zpowu(r, a, b->used ? b->dp[0] : 0);
}

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

static inline void
zmodpowu(z_t r, z_t a, unsigned long long int b, z_t m)
{
	fp_set_int(_b, b);
	zmodpow(r, a, _b, m);
}